From e30b0bd1212c29a142cd9a16223162e63f8a30d8 Mon Sep 17 00:00:00 2001 From: "Todd C. Miller" Date: Thu, 28 Feb 2013 09:01:08 -0500 Subject: [PATCH] Use pstat() on HP-UX to determine the tty device. --- config.h.in | 3 +++ configure | 11 +++++++++++ configure.in | 1 + src/ttyname.c | 35 +++++++++++++++++++++++++++++++---- 4 files changed, 46 insertions(+), 4 deletions(-) diff --git a/config.h.in b/config.h.in index 45e8088ce..95c53549e 100644 --- a/config.h.in +++ b/config.h.in @@ -430,6 +430,9 @@ /* Define to 1 if you have the header file. */ #undef HAVE_PROJECT_H +/* Define to 1 if you have the `pstat_getproc' function. */ +#undef HAVE_PSTAT_GETPROC + /* Define to 1 if you have the header file. */ #undef HAVE_PTY_H diff --git a/configure b/configure index 6ab9da295..bb61d1bc7 100755 --- a/configure +++ b/configure @@ -14181,6 +14181,17 @@ $as_echo "$sudo_cv_var_daportable" >&6; } test -z "$with_pam" && AUTH_EXCL_DEF="PAM" ;; esac + for ac_func in pstat_getproc +do : + ac_fn_c_check_func "$LINENO" "pstat_getproc" "ac_cv_func_pstat_getproc" +if test "x$ac_cv_func_pstat_getproc" = xyes; then : + cat >>confdefs.h <<_ACEOF +#define HAVE_PSTAT_GETPROC 1 +_ACEOF + +fi +done + ;; *-dec-osf*) # ignore envariables wrt dynamic lib path diff --git a/configure.in b/configure.in index c5642b83a..f07af5dfe 100644 --- a/configure.in +++ b/configure.in @@ -1742,6 +1742,7 @@ case "$host" in test -z "$with_pam" && AUTH_EXCL_DEF="PAM" ;; esac + AC_CHECK_FUNCS(pstat_getproc) ;; *-dec-osf*) # ignore envariables wrt dynamic lib path diff --git a/src/ttyname.c b/src/ttyname.c index 40b3ff797..de077ad39 100644 --- a/src/ttyname.c +++ b/src/ttyname.c @@ -82,6 +82,10 @@ #elif defined(HAVE_SYS_PROCFS_H) # include #endif +#ifdef HAVE_PSTAT_GETPROC +# include +# include +#endif #include "sudo.h" @@ -156,7 +160,7 @@ sudo_ttyname_dev(dev_t tdev) debug_return_str(estrdup(tty)); } -#elif defined(HAVE_STRUCT_PSINFO_PR_TTYDEV) || defined(__linux__) +#elif defined(HAVE_STRUCT_PSINFO_PR_TTYDEV) || defined(HAVE_PSTAT_GETPROC) || defined(__linux__) /* * Devices to search before doing a breadth-first scan. */ @@ -335,7 +339,6 @@ sudo_ttyname_dev(dev_t rdev) * 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 * parent). First tries sysctl using the current pid, then the parent's pid. - * Falls back on ttyname of std{in,out,err} if that fails. */ char * get_process_ttyname(void) @@ -385,7 +388,6 @@ get_process_ttyname(void) * 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 * parent). First tries /proc/pid/psinfo, then /proc/ppid/psinfo. - * Falls back on ttyname of std{in,out,err} if that fails. */ char * get_process_ttyname(void) @@ -416,7 +418,6 @@ get_process_ttyname(void) * 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 * parent). First tries field 7 in /proc/pid/stat, then /proc/ppid/stat. - * Falls back on ttyname of std{in,out,err} if that fails. */ char * get_process_ttyname(void) @@ -457,6 +458,32 @@ get_process_ttyname(void) debug_return_str(tty); } +#elif HAVE_PSTAT_GETPROC +/* + * 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 + * parent). + */ +char * +get_process_ttyname(void) +{ + struct pst_status pstat; + char *tty = NULL; + int i; + debug_decl(get_process_ttyname, SUDO_DEBUG_UTIL) + + /* Try to determine the tty from psdev in struct pst_status. */ + for (i = 0; tty == NULL && i < 2; i++) { + 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) { + tty = sudo_ttyname_dev(makedev(pstat.pst_term.psd_major, + pstat.pst_term.psd_minor)); + } + } + } + debug_return_str(tty); +} #else /* * Return a string from ttyname() containing the tty to which the process is