xgethostname()

Denys Vlasenko vda.linux at googlemail.com
Sun Feb 24 19:06:17 UTC 2008


On Saturday 23 February 2008 18:16, walter harms wrote:
> >> I don't see any obvious reason why this is segfaulting,
> >> *ptr_to_globals seems to have reasonable contents. I'm using
> >> uClibc-0.9.28.2 and I haven't had problems with gethostname() before.
> >> 'hostname -f' also calls gethostname() and it doesn't segfault.
> > 
> > can you try:
> > 
> > /* Store away localhost's name before the fork */
> > 	if (gethostname(G.localHostName, 64 -1) != 0)
> > 		bb_error_msg_and_die("gethostname");
> > 	/* "It is unspecified whether the truncated hostname
> > 	 * will be null-terminated". Idiots! */
> > 	G.localHostName[64 -1] = '\0';
> > 	*strchrnul(G.localHostName, '.') = '\0';
> > 
> > This used to work in devfsd.c.
> > Maybe there is a need for a bb_gethostname()....
> 
> according to the gethostname() man pages gethostname() returns a copy from
> the uname structure. here is a POC for a new xgethostname() that returns
> a real string. i have no idea how this may behave outside the glibc/linux
> combo. It is basicly what glibc does except that this here is a allocating
> memory for the copy.
> 
> It is not saving bytes but preventing chrashes like the one above

Wait a second. I do not understand how it crashes in the first place.

> char *xgethostname()
> {
>         char *buf=NULL;
>         struct utsname ubuf;
>         int ret=uname(&ubuf);
> 
>         if (!ret)
>                 buf=strdup(ubuf.nodename);
> 
>         return buf;
> }

--
vda



More information about the busybox mailing list