summaryrefslogtreecommitdiffstats
path: root/osdep-openbsd.c
diff options
context:
space:
mode:
authorNicholas Marriott <nicholas.marriott@gmail.com>2009-01-26 22:57:20 +0000
committerNicholas Marriott <nicholas.marriott@gmail.com>2009-01-26 22:57:20 +0000
commit9cde0c24779e8f43b6d122bc62fa22b6022d9ef0 (patch)
tree833af8a6751da50421ae9db66ae10e2e6a05823c /osdep-openbsd.c
parent4d7e555a48a6fb0732565b24482d24e968f49eae (diff)
Be more clever about picking window name.
Diffstat (limited to 'osdep-openbsd.c')
-rw-r--r--osdep-openbsd.c72
1 files changed, 68 insertions, 4 deletions
diff --git a/osdep-openbsd.c b/osdep-openbsd.c
index 6c39416d..e35a355a 100644
--- a/osdep-openbsd.c
+++ b/osdep-openbsd.c
@@ -1,4 +1,4 @@
-/* $Id: osdep-openbsd.c,v 1.2 2009-01-20 22:17:53 nicm Exp $ */
+/* $Id: osdep-openbsd.c,v 1.3 2009-01-26 22:57:19 nicm Exp $ */
/*
* Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -20,6 +20,7 @@
#include <sys/param.h>
#include <sys/sysctl.h>
+#include <sys/stat.h>
#include <err.h>
#include <errno.h>
@@ -27,10 +28,73 @@
#include <string.h>
#include <unistd.h>
-char *get_argv0(pid_t);
+char *get_argv0(int, char *);
+char *get_proc_argv0(pid_t);
char *
-get_argv0(pid_t pgrp)
+get_argv0(__attribute__ ((unused)) int fd, char *tty)
+{
+ int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_TTY, 0 };
+ struct stat sb;
+ size_t len;
+ struct kinfo_proc *buf, *newbuf;
+ struct proc *p, *bestp;
+ char *procname;
+ u_int i;
+
+ buf = NULL;
+
+ if (stat(tty, &sb) == -1)
+ return (NULL);
+ mib[3] = sb.st_rdev;
+
+retry:
+ if (sysctl(mib, nitems(mib), NULL, &len, NULL, 0) == -1)
+ return (NULL);
+ len = (len * 5) / 4;
+
+ if ((newbuf = realloc(buf, len)) == NULL) {
+ free(buf);
+ return (NULL);
+ }
+ buf = newbuf;
+
+ if (sysctl(mib, nitems(mib), buf, &len, NULL, 0) == -1) {
+ if (errno == ENOMEM)
+ goto retry;
+ free(buf);
+ return (NULL);
+ }
+
+ bestp = NULL;
+ for (i = 0; i < len / sizeof (struct kinfo_proc); i++) {
+ p = &buf[i].kp_proc;
+ if (bestp == NULL)
+ bestp = p;
+
+ if (p->p_stat != SRUN &&
+ p->p_stat != SIDL &&
+ p->p_stat != SONPROC)
+ continue;
+ if (p->p_estcpu < bestp->p_estcpu)
+ continue;
+ if (p->p_slptime > bestp->p_slptime)
+ continue;
+ bestp = p;
+ }
+
+ procname = get_proc_argv0(bestp->p_pid);
+ if (procname == NULL || *procname == '\0') {
+ free(procname);
+ procname = strdup(bestp->p_comm);
+ }
+
+ free(buf);
+ return (procname);
+}
+
+char *
+get_proc_argv0(pid_t pid)
{
int mib[4] = { CTL_KERN, KERN_PROC_ARGS, 0, KERN_PROC_ARGV };
size_t size;
@@ -38,7 +102,7 @@ get_argv0(pid_t pgrp)
procname = NULL;
- mib[2] = pgrp;
+ mib[2] = pid;
args = NULL;
size = 128;