The device name returned by devname() does not include the /dev/

prefix so we need to add it ourselves.
This commit is contained in:
Todd C. Miller
2012-01-11 15:38:18 -05:00
parent f7b04c32ae
commit f05de3f3f1

View File

@@ -473,24 +473,32 @@ get_process_tty(void)
rc = sysctl(mib, sudo_kp_namelen, ki_proc, &size, NULL, 0);
} while (rc == -1 && errno == ENOMEM);
if (rc != -1) {
tty = devname(ki_proc->sudo_kp_tdev, S_IFCHR);
if (*tty != '/') {
char *dev = devname(ki_proc->sudo_kp_tdev, S_IFCHR);
/* Some versions of devname() return NULL, others do not. */
if (dev == NULL || *dev == '?' || *dev == '#') {
sudo_debug_printf(SUDO_DEBUG_WARN,
"unable to map device number %u to name: %s",
ki_proc->sudo_kp_tdev, tty);
tty = NULL;
"unable to map device number %u to name",
ki_proc->sudo_kp_tdev);
} else {
/* devname() doesn't use the /dev/ prefix, add one... */
size_t len = sizeof(_PATH_DEV) + strlen(dev);
tty = emalloc(len);
strlcpy(tty, _PATH_DEV, len);
strlcat(tty, dev, len);
}
} else {
sudo_debug_printf(SUDO_DEBUG_WARN,
"unable to resolve tty via KERN_PROC: %s", strerror(errno));
}
efree(ki_proc);
}
/* If all else fails, fall back on ttyname(). */
if (tty == NULL) {
if ((tty = ttyname(STDIN_FILENO)) == NULL &&
(tty = ttyname(STDOUT_FILENO)) == NULL)
tty = ttyname(STDERR_FILENO);
if ((tty = ttyname(STDIN_FILENO)) != NULL ||
(tty = ttyname(STDOUT_FILENO)) != NULL ||
(tty = ttyname(STDERR_FILENO)) != NULL)
tty = estrdup(tty);
}
debug_return_str(tty);
@@ -526,7 +534,7 @@ get_process_tty(void)
}
}
debug_return_str(tty);
debug_return_str(estrdup(tty));
}
#endif /* sudo_kp_tdev */
@@ -585,6 +593,7 @@ get_user_info(struct user_details *ud)
if (user_info[i] == NULL)
errorx(1, _("unable to allocate memory"));
ud->tty = user_info[i] + sizeof("tty=") - 1;
efree(cp);
}
if (gethostname(host, sizeof(host)) == 0)