diff --git a/src/sudo.c b/src/sudo.c index 3700a79ee3..49f5bd2337 100644 --- a/src/sudo.c +++ b/src/sudo.c @@ -622,10 +622,13 @@ get_user_info(struct user_details *ud) if (ttydev != (dev_t)-1) { if (asprintf(&info[++i], "ttydev=%lld", (long long)ttydev) == -1) goto oom; - info[++i] = sudo_new_key_val("tty", path); - if (info[i] == NULL) - goto oom; - ud->tty = info[i] + sizeof("tty=") - 1; + /* The terminal device file may be missing in a chroot() jail. */ + if (path[0] != '\0') { + info[++i] = sudo_new_key_val("tty", path); + if (info[i] == NULL) + goto oom; + ud->tty = info[i] + sizeof("tty=") - 1; + } } else { /* tty may not always be present */ if (errno != ENOENT) diff --git a/src/ttyname.c b/src/ttyname.c index a36b6da2fa..ec3160d4b8 100644 --- a/src/ttyname.c +++ b/src/ttyname.c @@ -94,8 +94,10 @@ #if defined(sudo_kp_tdev) /* - * Store the name of the tty to which the process is attached in name. - * Returns name on success and NULL on failure, setting errno. + * Look up terminal device that the process is attached to and + * fill in its name, if available. Sets name to the empty string + * if the device number cannot be mapped to a device name. + * Returns the tty device number on success and -1 on failure, setting errno. */ dev_t get_process_ttyname(char *name, size_t namelen) @@ -135,10 +137,11 @@ get_process_ttyname(char *name, size_t namelen) errno = serrno; ttydev = (dev_t)ki_proc->sudo_kp_tdev; if (sudo_ttyname_dev(ttydev, name, namelen) == NULL) { - sudo_warnx( - U_("unable to find terminal name for device %u, %u"), + sudo_debug_printf(SUDO_DEBUG_WARN|SUDO_DEBUG_LINENO, + "unable to find terminal name for device %u, %u", (unsigned int)major(ttydev), (unsigned int)minor(ttydev)); - ttydev = (dev_t)-1; + if (namelen != 0) + *name = '\0'; } } } else { @@ -151,8 +154,10 @@ get_process_ttyname(char *name, size_t namelen) } #elif defined(HAVE_STRUCT_PSINFO_PR_TTYDEV) /* - * Store the name of the tty to which the process is attached in name. - * Returns name on success and NULL on failure, setting errno. + * Look up terminal device that the process is attached to and + * fill in its name, if available. Sets name to the empty string + * if the device number cannot be mapped to a device name. + * Returns the tty device number on success and -1 on failure, setting errno. */ dev_t get_process_ttyname(char *name, size_t namelen) @@ -179,10 +184,11 @@ get_process_ttyname(char *name, size_t namelen) if (ttydev != 0 && ttydev != (dev_t)-1) { errno = serrno; if (sudo_ttyname_dev(ttydev, name, namelen) == NULL) { - sudo_warnx( - U_("unable to find terminal name for device %u, %u"), + sudo_debug_printf(SUDO_DEBUG_WARN|SUDO_DEBUG_LINENO, + "unable to find terminal name for device %u, %u", (unsigned int)major(ttydev), (unsigned int)minor(ttydev)); - ttydev = (dev_t)-1; + if (namelen != 0) + *name = '\0'; } goto done; } @@ -197,10 +203,11 @@ get_process_ttyname(char *name, size_t namelen) if (sudo_isatty(i, &sb)) { ttydev = sb.st_rdev; if (sudo_ttyname_dev(ttydev, name, namelen) == NULL) { - sudo_warnx( - U_("unable to find terminal name for device %u, %u"), + sudo_debug_printf(SUDO_DEBUG_WARN|SUDO_DEBUG_LINENO, + "unable to find terminal name for device %u, %u", (unsigned int)major(ttydev), (unsigned int)minor(ttydev)); - ttydev = (dev_t)-1; + if (namelen != 0) + *name = '\0'; } goto done; } @@ -217,8 +224,10 @@ get_process_ttyname(char *name, size_t namelen) } #elif defined(__linux__) /* - * Store the name of the tty to which the process is attached in name. - * Returns name on success and NULL on failure, setting errno. + * Look up terminal device that the process is attached to and + * fill in its name, if available. Sets name to the empty string + * if the device number cannot be mapped to a device name. + * Returns the tty device number on success and -1 on failure, setting errno. */ dev_t get_process_ttyname(char *name, size_t namelen) @@ -282,10 +291,11 @@ get_process_ttyname(char *name, size_t namelen) ttydev = (unsigned int)tty_nr; errno = serrno; if (sudo_ttyname_dev(ttydev, name, namelen) == NULL) { - sudo_warnx( - U_("unable to find terminal name for device %u, %u"), + sudo_debug_printf(SUDO_DEBUG_WARN|SUDO_DEBUG_LINENO, + "unable to find terminal name for device %u, %u", (unsigned int)major(ttydev), (unsigned int)minor(ttydev)); - ttydev = (dev_t)-1; + if (namelen != 0) + *name = '\0'; } goto done; } @@ -310,10 +320,11 @@ get_process_ttyname(char *name, size_t namelen) if (sudo_isatty(i, &sb)) { ttydev = sb.st_rdev; if (sudo_ttyname_dev(sb.st_rdev, name, namelen) == NULL) { - sudo_warnx( - U_("unable to find terminal name for device %u, %u"), + sudo_debug_printf(SUDO_DEBUG_WARN|SUDO_DEBUG_LINENO, + "unable to find terminal name for device %u, %u", (unsigned int)major(ttydev), (unsigned int)minor(ttydev)); - ttydev = (dev_t)-1; + if (namelen != 0) + *name = '\0'; } goto done; } @@ -332,8 +343,10 @@ get_process_ttyname(char *name, size_t namelen) } #elif defined(HAVE_PSTAT_GETPROC) /* - * Store the name of the tty to which the process is attached in name. - * Returns name on success and NULL on failure, setting errno. + * Look up terminal device that the process is attached to and + * fill in its name, if available. Sets name to the empty string + * if the device number cannot be mapped to a device name. + * Returns the tty device number on success and -1 on failure, setting errno. */ dev_t get_process_ttyname(char *name, size_t namelen) @@ -354,11 +367,12 @@ get_process_ttyname(char *name, size_t namelen) errno = serrno; ttydev = makedev(pst.pst_term.psd_major, pst.pst_term.psd_minor); if (sudo_ttyname_dev(ttydev, name, namelen) == NULL) { - sudo_warnx( - U_("unable to find terminal name for device %u, %u"), + sudo_debug_printf(SUDO_DEBUG_WARN|SUDO_DEBUG_LINENO, + "unable to find terminal name for device %u, %u", (unsigned int)pst.pst_term.psd_major, (unsigned int)pst.pst_term.psd_minor); - ttydev = (dev_t)-1; + if (namelen != 0) + *name = '\0'; } goto done; } @@ -373,8 +387,8 @@ get_process_ttyname(char *name, size_t namelen) } #else /* - * Store the name of the tty to which the process is attached in name. - * Returns name on success and NULL on failure, setting errno. + * Look up terminal device that the process is attached to and fill in name. + * Returns the tty device number on success and -1 on failure, setting errno. */ dev_t get_process_ttyname(char *name, size_t namelen)