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);
}
}
- 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));
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<char> 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;
}
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<char> 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;
}