xgethostname()
Tito
farmatito at tiscali.it
Sat Feb 23 21:00:38 UTC 2008
On Saturday 23 February 2008 18:16:27 walter harms wrote:
>
> Tito wrote:
> > On Saturday 23 February 2008 02:50:11 Clem Taylor wrote:
> >> I'm trying to switch from an old busybox 1.5.0.svn (2007-11-19) to
> >> busybox 1.9.1 and syslogd segfaults on startup:
> >> #0 0x2ab609cc in strcpy () from /lib/libc.so.0
> >> #1 0x2ab6376c in gethostname () from /lib/libc.so.0
> >> #2 0x004384bc in syslogd_main (argc=64, argv=0x40) at sysklogd/syslogd.c:614
> >> #3 0x0040878c in run_applet_no_and_exit (applet_no=86, argv=0x7f8295c8)
> >> at libbb/appletlib.c:649
> >> #4 0x00408800 in run_applet_and_exit (name=0x7f829e81 "syslogd",
> >> argv=0x7f8295c8) at libbb/appletlib.c:656
> >> #5 0x004089c4 in run_applet_and_exit (name=0x7f8295c8 "\201\236\202\177",
> >> argv=0x7f8295c4) at libbb/appletlib.c:629
> >> #6 0x00408a9c in main (argc=64, argv=0x7f8295c4) at libbb/appletlib.c:684
> >>
> >> 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.
> >>
> >> Is anyone else having problems with 1.9.1 syslogd?
> >>
> >> Thanks,
> >> Clem
> >
> > 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()....
> >
> > Ciao,
> > Tito
> >
>
> 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
> re,
> wh
>
>
>
>
> #include <stdio.h>
> #include <string.h>
> #include <sys/utsname.h>
>
> char *xgethostname()
> {
> char *buf=NULL;
> struct utsname ubuf;
> int ret=uname(&ubuf);
>
> if (!ret)
> buf=strdup(ubuf.nodename);
>
> return buf;
> }
>
>
> int main()
> {
>
> printf("%s\n",xgethostname());
>
> }
>
>
>
>
Hi,
I was thinking about something a little more complex due to the fact
that some of the applets using gethostname do some operations on the output
even on error.
gethostname is used in this files:
file:///busybox/libbb/lineedit.c
file:///busybox/libbb/login.c
file:///busybox/libbb/obscure.c
file:///busybox/miscutils/devfsd.c
file:///busybox/networking/hostname.c
file:///busybox/sysklogd/syslogd.c
The xgethostname() at the moment is needed only in devfsd.c,
so in case it could be omitted, but i think that it could be useful
in the future....
---------------------------------------------------------------------------------------------------------------------------
/* vi: set sw=4 ts=4: */
/*
* Safe gethostname implementation for busybox
*
* Copyright (C) 2008 Tito Ragusa <farmatito at tiscali.it>
*
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
*/
/*
* SUSv2 guarantees that ‘Host names are limited to 255 bytes’.
* POSIX.1-2001 guarantees that ‘Host names (not including the terminating
* null byte) are limited to HOST_NAME_MAX bytes’.
*/
#include "libbb.h"
#include <sys/utsname.h>
char *safe_gethostname(void)
{
struct utsname buf;
return xstrndup((uname(&buf) < 0 || !*(buf.nodename)) ? "?" : buf.nodename, HOST_NAME_MAX);
}
char *xgethostname(void)
{
char *hostname = safe_gethostname();
if (*hostname == '?')
bb_error_msg_and_die("gethostname");
return hostname;
}
-------------------------------------------------------------------------------------------------------------------
With:
#ifndef HOST_NAME_MAX
#define HOST_NAME_MAX 255
#endif
in libbb.h.
Ciao,
Tito
More information about the busybox
mailing list