Don't check the tty of the parent process. Now that we get the
controlling tty device number from the kernel there is no need. If the process has really disassociated from the tty then reporting "unknown" is appropriate.
This commit is contained in:
@@ -334,8 +334,7 @@ sudo_ttyname_dev(dev_t rdev)
|
|||||||
#if defined(sudo_kp_tdev)
|
#if defined(sudo_kp_tdev)
|
||||||
/*
|
/*
|
||||||
* Return a string from ttyname() containing the tty to which the process is
|
* Return a string from ttyname() containing the tty to which the process is
|
||||||
* attached or NULL if there is no tty associated with the process (or its
|
* attached or NULL if the process has no controlling tty.
|
||||||
* parent). First tries sysctl using the current pid, then the parent's pid.
|
|
||||||
*/
|
*/
|
||||||
char *
|
char *
|
||||||
get_process_ttyname(void)
|
get_process_ttyname(void)
|
||||||
@@ -343,18 +342,17 @@ get_process_ttyname(void)
|
|||||||
char *tty = NULL;
|
char *tty = NULL;
|
||||||
struct sudo_kinfo_proc *ki_proc = NULL;
|
struct sudo_kinfo_proc *ki_proc = NULL;
|
||||||
size_t size = sizeof(*ki_proc);
|
size_t size = sizeof(*ki_proc);
|
||||||
int i, mib[6], rc;
|
int mib[6], rc;
|
||||||
debug_decl(get_process_ttyname, SUDO_DEBUG_UTIL)
|
debug_decl(get_process_ttyname, SUDO_DEBUG_UTIL)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Lookup tty for this process and, failing that, our parent.
|
* Lookup controlling tty for this process via sysctl.
|
||||||
* Even if we redirect std{in,out,err} the kernel should still know.
|
* This will work even if std{in,out,err} are redirected.
|
||||||
*/
|
*/
|
||||||
for (i = 0; tty == NULL && i < 2; i++) {
|
|
||||||
mib[0] = CTL_KERN;
|
mib[0] = CTL_KERN;
|
||||||
mib[1] = SUDO_KERN_PROC;
|
mib[1] = SUDO_KERN_PROC;
|
||||||
mib[2] = KERN_PROC_PID;
|
mib[2] = KERN_PROC_PID;
|
||||||
mib[3] = i ? (int)getppid() : (int)getpid();
|
mib[3] = (int)getpid();
|
||||||
mib[4] = sizeof(*ki_proc);
|
mib[4] = sizeof(*ki_proc);
|
||||||
mib[5] = 1;
|
mib[5] = 1;
|
||||||
do {
|
do {
|
||||||
@@ -375,7 +373,6 @@ get_process_ttyname(void)
|
|||||||
sudo_debug_printf(SUDO_DEBUG_WARN,
|
sudo_debug_printf(SUDO_DEBUG_WARN,
|
||||||
"unable to resolve tty via KERN_PROC: %s", strerror(errno));
|
"unable to resolve tty via KERN_PROC: %s", strerror(errno));
|
||||||
}
|
}
|
||||||
}
|
|
||||||
efree(ki_proc);
|
efree(ki_proc);
|
||||||
|
|
||||||
debug_return_str(tty);
|
debug_return_str(tty);
|
||||||
@@ -383,8 +380,7 @@ get_process_ttyname(void)
|
|||||||
#elif defined(HAVE_STRUCT_PSINFO_PR_TTYDEV)
|
#elif defined(HAVE_STRUCT_PSINFO_PR_TTYDEV)
|
||||||
/*
|
/*
|
||||||
* Return a string from ttyname() containing the tty to which the process is
|
* Return a string from ttyname() containing the tty to which the process is
|
||||||
* attached or NULL if there is no tty associated with the process (or its
|
* attached or NULL if the process has no controlling tty.
|
||||||
* parent). First tries /proc/pid/psinfo, then /proc/ppid/psinfo.
|
|
||||||
*/
|
*/
|
||||||
char *
|
char *
|
||||||
get_process_ttyname(void)
|
get_process_ttyname(void)
|
||||||
@@ -392,20 +388,17 @@ get_process_ttyname(void)
|
|||||||
char path[PATH_MAX], *tty = NULL;
|
char path[PATH_MAX], *tty = NULL;
|
||||||
struct psinfo psinfo;
|
struct psinfo psinfo;
|
||||||
ssize_t nread;
|
ssize_t nread;
|
||||||
int i, fd;
|
int fd;
|
||||||
debug_decl(get_process_ttyname, SUDO_DEBUG_UTIL)
|
debug_decl(get_process_ttyname, SUDO_DEBUG_UTIL)
|
||||||
|
|
||||||
/* Try to determine the tty from pr_ttydev in /proc/pid/psinfo. */
|
/* Try to determine the tty from pr_ttydev in /proc/pid/psinfo. */
|
||||||
for (i = 0; tty == NULL && i < 2; i++) {
|
snprintf(path, sizeof(path), "/proc/%u/psinfo", (unsigned int)getpid());
|
||||||
(void)snprintf(path, sizeof(path), "/proc/%u/psinfo",
|
if ((fd = open(path, O_RDONLY, 0)) != -1) {
|
||||||
i ? (unsigned int)getppid() : (unsigned int)getpid());
|
|
||||||
if ((fd = open(path, O_RDONLY, 0)) == -1)
|
|
||||||
continue;
|
|
||||||
nread = read(fd, &psinfo, sizeof(psinfo));
|
nread = read(fd, &psinfo, sizeof(psinfo));
|
||||||
close(fd);
|
close(fd);
|
||||||
if (nread == (ssize_t)sizeof(psinfo)) {
|
if (nread == (ssize_t)sizeof(psinfo)) {
|
||||||
dev_t rdev = (dev_t)psinfo.pr_ttydev;
|
dev_t rdev = (dev_t)psinfo.pr_ttydev;
|
||||||
#ifdef DEVNO64
|
#if defined(_AIX) && defined(DEVNO64)
|
||||||
if (psinfo.pr_ttydev & DEVNO64)
|
if (psinfo.pr_ttydev & DEVNO64)
|
||||||
rdev = makedev(major64(psinfo.pr_ttydev), minor64(psinfo.pr_ttydev));
|
rdev = makedev(major64(psinfo.pr_ttydev), minor64(psinfo.pr_ttydev));
|
||||||
#endif
|
#endif
|
||||||
@@ -419,26 +412,20 @@ get_process_ttyname(void)
|
|||||||
#elif defined(__linux__)
|
#elif defined(__linux__)
|
||||||
/*
|
/*
|
||||||
* Return a string from ttyname() containing the tty to which the process is
|
* Return a string from ttyname() containing the tty to which the process is
|
||||||
* attached or NULL if there is no tty associated with the process (or its
|
* attached or NULL if the process has no controlling tty.
|
||||||
* parent). First tries field 7 in /proc/pid/stat, then /proc/ppid/stat.
|
|
||||||
*/
|
*/
|
||||||
char *
|
char *
|
||||||
get_process_ttyname(void)
|
get_process_ttyname(void)
|
||||||
{
|
{
|
||||||
char *line = NULL, *tty = NULL;
|
char path[PATH_MAX], *line = NULL, *tty = NULL;
|
||||||
size_t linesize = 0;
|
size_t linesize = 0;
|
||||||
ssize_t len;
|
ssize_t len;
|
||||||
int i;
|
FILE *fp;
|
||||||
debug_decl(get_process_ttyname, SUDO_DEBUG_UTIL)
|
debug_decl(get_process_ttyname, SUDO_DEBUG_UTIL)
|
||||||
|
|
||||||
/* Try to determine the tty from tty_nr in /proc/pid/stat. */
|
/* Try to determine the tty from tty_nr in /proc/pid/stat. */
|
||||||
for (i = 0; tty == NULL && i < 2; i++) {
|
snprintf(path, sizeof(path), "/proc/%u/stat", (unsigned int)getpid());
|
||||||
FILE *fp;
|
if ((fp = fopen(path, "r")) != NULL) {
|
||||||
char path[PATH_MAX];
|
|
||||||
(void)snprintf(path, sizeof(path), "/proc/%u/stat",
|
|
||||||
i ? (unsigned int)getppid() : (unsigned int)getpid());
|
|
||||||
if ((fp = fopen(path, "r")) == NULL)
|
|
||||||
continue;
|
|
||||||
len = getline(&line, &linesize, fp);
|
len = getline(&line, &linesize, fp);
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
if (len != -1) {
|
if (len != -1) {
|
||||||
@@ -456,42 +443,36 @@ get_process_ttyname(void)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
efree(line);
|
efree(line);
|
||||||
|
}
|
||||||
|
|
||||||
debug_return_str(tty);
|
debug_return_str(tty);
|
||||||
}
|
}
|
||||||
#elif HAVE_PSTAT_GETPROC
|
#elif HAVE_PSTAT_GETPROC
|
||||||
/*
|
/*
|
||||||
* Return a string from ttyname() containing the tty to which the process is
|
* Return a string from ttyname() containing the tty to which the process is
|
||||||
* attached or NULL if there is no tty associated with the process (or its
|
* attached or NULL if the process has no controlling tty.
|
||||||
* parent).
|
|
||||||
*/
|
*/
|
||||||
char *
|
char *
|
||||||
get_process_ttyname(void)
|
get_process_ttyname(void)
|
||||||
{
|
{
|
||||||
struct pst_status pstat;
|
struct pst_status pstat;
|
||||||
char *tty = NULL;
|
char *tty = NULL;
|
||||||
int i;
|
|
||||||
debug_decl(get_process_ttyname, SUDO_DEBUG_UTIL)
|
debug_decl(get_process_ttyname, SUDO_DEBUG_UTIL)
|
||||||
|
|
||||||
/* Try to determine the tty from psdev in struct pst_status. */
|
/* Try to determine the tty from psdev in struct pst_status. */
|
||||||
for (i = 0; tty == NULL && i < 2; i++) {
|
if (pstat_getproc(&pstat, sizeof(pstat), 0, (int)getpid()) != -1) {
|
||||||
const int pid = i ? (int)getppid() : (int)getpid();
|
|
||||||
if (pstat_getproc(&pstat, sizeof(pstat), 0, pid) != -1) {
|
|
||||||
if (pstat.pst_term.psd_major != -1 && pstat.pst_term.psd_minor != -1) {
|
if (pstat.pst_term.psd_major != -1 && pstat.pst_term.psd_minor != -1) {
|
||||||
tty = sudo_ttyname_dev(makedev(pstat.pst_term.psd_major,
|
tty = sudo_ttyname_dev(makedev(pstat.pst_term.psd_major,
|
||||||
pstat.pst_term.psd_minor));
|
pstat.pst_term.psd_minor));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
debug_return_str(tty);
|
debug_return_str(tty);
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
/*
|
/*
|
||||||
* Return a string from ttyname() containing the tty to which the process is
|
* Return a string from ttyname() containing the tty to which the process is
|
||||||
* attached or NULL if there is no tty associated with the process.
|
* attached or NULL if the process has no controlling tty.
|
||||||
* parent).
|
|
||||||
*/
|
*/
|
||||||
char *
|
char *
|
||||||
get_process_ttyname(void)
|
get_process_ttyname(void)
|
||||||
|
Reference in New Issue
Block a user