summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlan Coopersmith <alan.coopersmith@oracle.com>2019-02-17 14:56:26 -0800
committerAlan Coopersmith <alan.coopersmith@oracle.com>2019-02-17 14:56:26 -0800
commitd840ae5e5438402d752f83ddc297cf08cf2ae067 (patch)
tree5b979f113a3895a5af4b9a87f4dae4c021419d5d
parent4f4c7bf594dd58db0d98c79e884c0117ded2b472 (diff)
Add support for process ID and names on Solarish systems
Signed-off-by: Alan Coopersmith <alan.coopersmith@oracle.com>
-rw-r--r--configure.ac2
-rw-r--r--peerinfo.c52
2 files changed, 53 insertions, 1 deletions
diff --git a/configure.ac b/configure.ac
index 528581c..99767c0 100644
--- a/configure.ac
+++ b/configure.ac
@@ -72,7 +72,7 @@ fi
# Checks for pkg-config packages
PKG_CHECK_MODULES(XSCOPE, [xproto >= 7.0.17 $XTRANS_PKG])
-AC_CHECK_FUNCS([getdtablesize])
+AC_CHECK_FUNCS([getdtablesize getpeerucred])
AC_CHECK_HEADERS([sys/filio.h langinfo.h])
dnl Allow checking code with lint, sparse, etc.
diff --git a/peerinfo.c b/peerinfo.c
index 2e9c8e0..298dd21 100644
--- a/peerinfo.c
+++ b/peerinfo.c
@@ -31,6 +31,13 @@
#include <stdio.h>
#include <sys/types.h> /* for pid_t */
+#ifdef HAVE_GETPEERUCRED /* Solaris-derived systems */
+#include <ucred.h>
+#endif
+
+#ifdef __sun
+#include <procfs.h>
+#endif
static pid_t
GetPidFromFd(FD fd)
@@ -43,6 +50,16 @@ GetPidFromFd(FD fd)
cred_len == sizeof(cred)) {
return cred.pid;
}
+#elif defined(HAVE_GETPEERUCRED)
+ ucred_t *peercred = NULL;
+
+ if (getpeerucred(fd, &peercred) == 0) {
+ pid_t peerpid = ucred_getpid(peercred);
+ ucred_free(peercred);
+ if (peerpid != -1) {
+ return peerpid;
+ }
+ }
#endif
return 0;
}
@@ -50,6 +67,40 @@ GetPidFromFd(FD fd)
static const char *
GetProcnameForPid(pid_t pid)
{
+#ifdef __sun /* Solaris-derived /proc */
+ static char comm[PRARGSZ];
+ char path[32];
+ int totsize = 0;
+ int fd = 0;
+ psinfo_t psinfo = { 0 };
+ char *sp;
+
+ if (snprintf(path, sizeof(path), "/proc/%d/psinfo", pid) < 0)
+ return NULL;
+ fd = open(path, O_RDONLY);
+ if (fd < 0)
+ return NULL;
+
+ totsize = read(fd, &psinfo, sizeof(psinfo_t));
+ close(fd);
+ if (totsize <= 0)
+ return NULL;
+
+ /* pr_psargs is the first PRARGSZ (80) characters of the command
+ * line string - assume up to the first space is the command name,
+ * since it's not delimited. While there is also pr_fname, that's
+ * more limited, giving only the first 16 chars of the basename of
+ * the file that was exec'ed, thus cutting off many long gnome
+ * command names, or returning "isapython2.6" for all python scripts.
+ */
+ psinfo.pr_psargs[PRARGSZ - 1] = '\0';
+ sp = strchr(psinfo.pr_psargs, ' ');
+ if (sp)
+ *sp = '\0';
+
+ strlcpy(comm, psinfo.pr_psargs, sizeof(comm));
+ return comm;
+#else /* Linux-style /proc */
/* comm is no longer than TASK_COMM_LEN (16) */
static char comm[17];
char comm_path[32];
@@ -69,6 +120,7 @@ GetProcnameForPid(pid_t pid)
comm_len--;
comm[comm_len] = '\0';
return comm;
+#endif
}
/*