The sethostname() system call is not standardized by POSIX, only by the LSB: https://refspecs.linuxbase.org/LSB_5.0.0/LSB-Core-generic/LSB-Core-generic/baselib-sethostname-2.html
In particular, it should fail with EINVAL if "len is negative or larger than the maximum allowed size". In Cygwin 3.5.3, a too long hostname is not rejected. Witness: The test program below, when run on the Cygwin 3.5.3 machines on GitHub (under a user account that is in the Administrators group), fails with the message "setting a too long hostname succeeded." The implementation is in winsup/cygwin/net.cc line 773. Bruno ====================== Test program essentially ===================== #include <assert.h> #include <unistd.h> /* for HOST_NAME_MAX */ #include <limits.h> /* for strlen */ #include <string.h> #include <errno.h> #include <stdio.h> #define TESTHOSTNAME "gnulib-hostname" /* mingw and MSVC 9 lack geteuid, so setup a dummy value. On Cygwin, geteuid() may return non-zero even for user accounts with administrator privileges, so use a dummy value as well. */ #if defined __CYGWIN__ # define geteuid() 0 #endif int main (int argc, char *argv[]) { char origname[HOST_NAME_MAX]; char newname[HOST_NAME_MAX]; char longname[HOST_NAME_MAX + 2]; int rcs, i; /* skip the tests if we don't have root privilege. this does not consider things like CAP_SYS_ADMIN (linux) or PRIV_SYS_ADMIN (solaris), etc. systems without a working geteuid (mingw, MSVC 9) will always skip this test. */ if (geteuid () != 0) { fprintf (stderr, "Skipping test: insufficient permissions.\n"); return 77; } /* we want to ensure we can do a get/set/get check to ensure the change is accepted. record the current name so it can be restored later */ assert (gethostname (origname, sizeof (origname)) == 0); /* try setting a valid hostname. if it fails -1/ENOSYS, we will skip the test for long names as this is an indication we're using the stub function that doesn't do anything on this platform. */ rcs = sethostname (TESTHOSTNAME, strlen (TESTHOSTNAME)); if (rcs != 0) { if (rcs == -1 && errno == ENOSYS) { fprintf (stderr, "Skipping test: sethostname is not really implemented.\n"); return 77; } else if (rcs == -1 && (errno == EPERM || errno == EACCES)) /* Cygwin */ { fprintf (stderr, "Skipping test: insufficient permissions.\n"); return 77; } else { fprintf (stderr, "error setting valid hostname.\n"); return 1; } } else { assert (gethostname (newname, sizeof (newname)) == 0); /* On Windows, a hostname change becomes effective only after a reboot. */ #if !(defined _WIN32 || defined __CYGWIN__) /* if we don't get back what we put in, there is no need to restore the original name as we will assume it was not properly changed. */ if (strcmp (newname, TESTHOSTNAME) != 0) { fprintf (stderr, "set/get comparison failed.\n"); return 1; } #endif } /* glibc does allow setting a zero length name, so the lower bound needs no test. validate that we are constrained by HOST_NAME_MAX */ for (i = 0; i < (HOST_NAME_MAX + 1); i++) longname[i] = 'a'; longname[i] = '\0'; rcs = sethostname (longname, (HOST_NAME_MAX + 1)); if (rcs != -1) { /* attempt to restore the original name. */ assert (sethostname (origname, strlen (origname)) == 0); fprintf (stderr, "setting a too long hostname succeeded.\n"); return 1; } /* restore the original name. */ assert (sethostname (origname, strlen (origname)) == 0); return 0; } -- Problem reports: https://cygwin.com/problems.html FAQ: https://cygwin.com/faq/ Documentation: https://cygwin.com/docs.html Unsubscribe info: https://cygwin.com/ml/#unsubscribe-simple