Annotation of src/usr.bin/tmux/procname.c, Revision 1.6
1.6 ! guenther 1: /* $OpenBSD: procname.c,v 1.5 2009/08/09 15:17:50 nicm Exp $ */
1.1 nicm 2:
3: /*
4: * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
5: *
6: * Permission to use, copy, modify, and distribute this software for any
7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
9: *
10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14: * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15: * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16: * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17: */
18:
19: #include <sys/param.h>
20: #include <sys/sysctl.h>
21: #include <sys/stat.h>
22:
23: #include <errno.h>
24: #include <stdlib.h>
25: #include <string.h>
26: #include <unistd.h>
27:
1.2 nicm 28: #ifndef nitems
1.1 nicm 29: #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
1.2 nicm 30: #endif
1.1 nicm 31:
32: #define is_runnable(p) \
33: ((p)->p_stat == SRUN || (p)->p_stat == SIDL || (p)->p_stat == SONPROC)
34: #define is_stopped(p) \
35: ((p)->p_stat == SSTOP || (p)->p_stat == SZOMB || (p)->p_stat == SDEAD)
36:
1.6 ! guenther 37: struct kinfo_proc2 *cmp_procs(struct kinfo_proc2 *, struct kinfo_proc2 *);
1.4 nicm 38: char *get_proc_name(int, char *);
39:
1.6 ! guenther 40: struct kinfo_proc2 *
! 41: cmp_procs(struct kinfo_proc2 *p1, struct kinfo_proc2 *p2)
1.4 nicm 42: {
43: if (is_runnable(p1) && !is_runnable(p2))
44: return (p1);
45: if (!is_runnable(p1) && is_runnable(p2))
46: return (p2);
47:
48: if (is_stopped(p1) && !is_stopped(p2))
49: return (p1);
50: if (!is_stopped(p1) && is_stopped(p2))
51: return (p2);
52:
53: if (p1->p_estcpu > p2->p_estcpu)
54: return (p1);
55: if (p1->p_estcpu < p2->p_estcpu)
56: return (p2);
57:
58: if (p1->p_slptime < p2->p_slptime)
59: return (p1);
60: if (p1->p_slptime > p2->p_slptime)
61: return (p2);
62:
63: if ((p1->p_flag & P_SINTR) && !(p2->p_flag & P_SINTR))
64: return (p1);
65: if (!(p1->p_flag & P_SINTR) && (p2->p_flag & P_SINTR))
66: return (p2);
67:
68: if (strcmp(p1->p_comm, p2->p_comm) < 0)
69: return (p1);
70: if (strcmp(p1->p_comm, p2->p_comm) > 0)
71: return (p2);
72:
73: if (p1->p_pid > p2->p_pid)
74: return (p1);
75: return (p2);
76: }
1.1 nicm 77:
78: char *
79: get_proc_name(int fd, char *tty)
80: {
1.6 ! guenther 81: int mib[6] = { CTL_KERN, KERN_PROC2, KERN_PROC_PGRP, 0,
! 82: sizeof(struct kinfo_proc2), 0 };
1.1 nicm 83: struct stat sb;
84: size_t len;
1.6 ! guenther 85: struct kinfo_proc2 *buf, *newbuf, *bestp;
1.1 nicm 86: u_int i;
87: char *name;
88:
89: buf = NULL;
90:
91: if (stat(tty, &sb) == -1)
92: return (NULL);
93: if ((mib[3] = tcgetpgrp(fd)) == -1)
94: return (NULL);
95:
96: retry:
97: if (sysctl(mib, nitems(mib), NULL, &len, NULL, 0) == -1)
98: return (NULL);
99: len = (len * 5) / 4;
100:
1.4 nicm 101: if ((newbuf = realloc(buf, len)) == NULL)
102: goto error;
1.1 nicm 103: buf = newbuf;
104:
1.6 ! guenther 105: mib[5] = (int)(len / sizeof(struct kinfo_proc2));
1.1 nicm 106: if (sysctl(mib, nitems(mib), buf, &len, NULL, 0) == -1) {
107: if (errno == ENOMEM)
108: goto retry;
1.4 nicm 109: goto error;
1.1 nicm 110: }
111:
112: bestp = NULL;
1.6 ! guenther 113: for (i = 0; i < len / sizeof (struct kinfo_proc2); i++) {
! 114: if ((dev_t)buf[i].p_tdev != sb.st_rdev)
1.1 nicm 115: continue;
1.4 nicm 116: if (bestp == NULL)
1.6 ! guenther 117: bestp = &buf[i];
1.4 nicm 118: else
1.6 ! guenther 119: bestp = cmp_procs(&buf[i], bestp);
1.1 nicm 120: }
121:
122: name = NULL;
123: if (bestp != NULL)
124: name = strdup(bestp->p_comm);
125:
126: free(buf);
127: return (name);
1.4 nicm 128:
129: error:
130: free(buf);
131: return (NULL);
1.1 nicm 132: }