From e6a5a3684a4ad5907bd6e820d850cca1bbd7d380 Mon Sep 17 00:00:00 2001 From: Alex Richardson Date: Wed, 14 Jul 2021 14:01:15 +0100 Subject: [PATCH] Fix build on systems without HOST_NAME_MAX Some operating systems such as FreeBSD don't define a HOST_NAME_MAX macro. The portable approach to determine the real host name limit is calling sysconf(_SC_HOST_NAME_MAX) so do that instead. --- unix/vncserver/vncsession.c | 13 +++++++++++-- unix/x0vncserver/x0vncserver.cxx | 22 +++++++++------------- unix/xserver/hw/vnc/vncExtInit.cc | 22 +++++++++------------- 3 files changed, 29 insertions(+), 28 deletions(-) diff --git a/unix/vncserver/vncsession.c b/unix/vncserver/vncsession.c index 784d5dae..3573e5e9 100644 --- a/unix/vncserver/vncsession.c +++ b/unix/vncserver/vncsession.c @@ -339,7 +339,8 @@ static void redir_stdio(const char *homedir, const char *display) { int fd; - char hostname[HOST_NAME_MAX+1]; + size_t hostlen; + char* hostname = NULL; char logfile[PATH_MAX]; fd = open("/dev/null", O_RDONLY); @@ -361,13 +362,21 @@ redir_stdio(const char *homedir, const char *display) } } - if (gethostname(hostname, sizeof(hostname)) == -1) { + hostlen = sysconf(_SC_HOST_NAME_MAX); + if (hostlen < 0) { + syslog(LOG_CRIT, "sysconf(_SC_HOST_NAME_MAX): %s", strerror(errno)); + _exit(EX_OSERR); + } + hostname = malloc(hostlen + 1); + if (gethostname(hostname, hostlen + 1) == -1) { syslog(LOG_CRIT, "gethostname: %s", strerror(errno)); + free(hostname); _exit(EX_OSERR); } snprintf(logfile, sizeof(logfile), "%s/.vnc/%s%s.log", homedir, hostname, display); + free(hostname); fd = open(logfile, O_CREAT | O_WRONLY | O_TRUNC, 0644); if (fd == -1) { syslog(LOG_CRIT, "Failure creating log file \"%s\": %s", logfile, strerror(errno)); diff --git a/unix/x0vncserver/x0vncserver.cxx b/unix/x0vncserver/x0vncserver.cxx index 89c9817e..29bc3d4c 100644 --- a/unix/x0vncserver/x0vncserver.cxx +++ b/unix/x0vncserver/x0vncserver.cxx @@ -71,29 +71,25 @@ BoolParameter localhostOnly("localhost", static const char* defaultDesktopName() { - static char* name = NULL; - - char hostname[HOST_NAME_MAX + 1]; - struct passwd* pwent; - - size_t len; - - delete [] name; + size_t host_max = sysconf(_SC_HOST_NAME_MAX); + if (host_max < 0) + return ""; - if (gethostname(hostname, sizeof(hostname)) == -1) + std::vector hostname(host_max + 1); + if (gethostname(hostname.data(), hostname.size()) == -1) return ""; - pwent = getpwuid(getuid()); + struct passwd* pwent = getpwuid(getuid()); if (pwent == NULL) return ""; - len = snprintf(NULL, 0, "%s@%s", pwent->pw_name, hostname); + size_t len = snprintf(NULL, 0, "%s@%s", pwent->pw_name, hostname.data()); if (len < 0) return ""; - name = new char[len + 1]; + char* name = new char[len + 1]; - snprintf(name, len + 1, "%s@%s", pwent->pw_name, hostname); + snprintf(name, len + 1, "%s@%s", pwent->pw_name, hostname.data()); return name; } diff --git a/unix/xserver/hw/vnc/vncExtInit.cc b/unix/xserver/hw/vnc/vncExtInit.cc index e6e802e3..6025de91 100644 --- a/unix/xserver/hw/vnc/vncExtInit.cc +++ b/unix/xserver/hw/vnc/vncExtInit.cc @@ -102,29 +102,25 @@ rfb::BoolParameter sendPrimary("SendPrimary", static const char* defaultDesktopName() { - static char* name = NULL; - - char hostname[HOST_NAME_MAX + 1]; - struct passwd* pwent; - - size_t len; - - delete [] name; + size_t host_max = sysconf(_SC_HOST_NAME_MAX); + if (host_max < 0) + return ""; - if (gethostname(hostname, sizeof(hostname)) == -1) + std::vector hostname(host_max + 1); + if (gethostname(hostname.data(), hostname.size()) == -1) return ""; - pwent = getpwuid(getuid()); + struct passwd* pwent = getpwuid(getuid()); if (pwent == NULL) return ""; - len = snprintf(NULL, 0, "%s@%s", pwent->pw_name, hostname); + size_t len = snprintf(NULL, 0, "%s@%s", pwent->pw_name, hostname.data()); if (len < 0) return ""; - name = new char[len + 1]; + char* name = new char[len + 1]; - snprintf(name, len + 1, "%s@%s", pwent->pw_name, hostname); + snprintf(name, len + 1, "%s@%s", pwent->pw_name, hostname.data()); return name; } -- 2.39.5