cloudius-systems / osv

OSv, a new operating system for the cloud.
osv.io
Other
4.08k stars 603 forks source link

gethostname() is not 100% compatible with Linux's #939

Open nyh opened 6 years ago

nyh commented 6 years ago

Our gethostname(), from musl/src/unistd/gethostname.c, if len is shorter than the actual name, truncates the name (but makes sure the truncated name ends with a null). If I understand correctly, on Linux, it's supposed to return -1 and ENAMETOOLONG in this. We'll only see this difference if we try gethostname() with a very short len.

geraldo-netto commented 6 years ago

hope this is relevant:

https://git.musl-libc.org/cgit/musl/tree/src/unistd/gethostname.c:

include

include <sys/utsname.h>

int gethostname(char *name, size_t len) { size_t i; struct utsname uts; if (uname(&uts)) return -1; if (len > sizeof uts.nodename) len = sizeof uts.nodename; for (i=0; i<len && (name[i] = uts.nodename[i]); i++); if (i==len) name[i-1] = 0; return 0; }

https://github.com/bminor/glibc/blob/09533208febe923479261a27b7691abef297d604/sysdeps/posix/gethostname.c: int __gethostname (char *name, size_t len) { struct utsname buf; size_t node_len;

if (__uname (&buf)) return -1;

node_len = strlen (buf.nodename) + 1; memcpy (name, buf.nodename, len < node_len ? len : node_len);

if (node_len > len) { __set_errno (ENAMETOOLONG); return -1; } return 0; }

nyh commented 6 years ago

@geraldo-netto, the former is the same code we already have (in musl/src/unistd/gethostname.c), it doesn't fix this (minor) bug. The second is indeed glibc's implementation, which shows ENAMETOOLONG as I said. You can't just copy this code because it has a different license. But it's trivial to fix anyway :-)