xref: /openbsd-src/usr.bin/tmux/procname.c (revision 6f05df2d9be0954bec42d51d943d77bd250fb664)
1 /* $OpenBSD: procname.c,v 1.13 2014/07/04 05:58:31 guenther Exp $ */
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/proc.h>
21 #include <sys/sysctl.h>
22 #include <sys/stat.h>
23 
24 #include <errno.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 
29 #ifndef nitems
30 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
31 #endif
32 
33 #define is_runnable(p) \
34 	((p)->p_stat == SRUN || (p)->p_stat == SIDL || (p)->p_stat == SONPROC)
35 #define is_stopped(p) \
36 	((p)->p_stat == SSTOP || (p)->p_stat == SDEAD)
37 
38 struct kinfo_proc	*cmp_procs(struct kinfo_proc *, struct kinfo_proc *);
39 char			*get_proc_name(int, char *);
40 
41 struct kinfo_proc *
42 cmp_procs(struct kinfo_proc *p1, struct kinfo_proc *p2)
43 {
44 	if (is_runnable(p1) && !is_runnable(p2))
45 		return (p1);
46 	if (!is_runnable(p1) && is_runnable(p2))
47 		return (p2);
48 
49 	if (is_stopped(p1) && !is_stopped(p2))
50 		return (p1);
51 	if (!is_stopped(p1) && is_stopped(p2))
52 		return (p2);
53 
54 	if (p1->p_estcpu > p2->p_estcpu)
55 		return (p1);
56 	if (p1->p_estcpu < p2->p_estcpu)
57 		return (p2);
58 
59 	if (p1->p_slptime < p2->p_slptime)
60 		return (p1);
61 	if (p1->p_slptime > p2->p_slptime)
62 		return (p2);
63 
64 	if ((p1->p_flag & P_SINTR) && !(p2->p_flag & P_SINTR))
65 		return (p1);
66 	if (!(p1->p_flag & P_SINTR) && (p2->p_flag & P_SINTR))
67 		return (p2);
68 
69 	if (strcmp(p1->p_comm, p2->p_comm) < 0)
70 		return (p1);
71 	if (strcmp(p1->p_comm, p2->p_comm) > 0)
72 		return (p2);
73 
74 	if (p1->p_pid > p2->p_pid)
75 		return (p1);
76 	return (p2);
77 }
78 
79 char *
80 get_proc_name(int fd, char *tty)
81 {
82 	int		 mib[6] = { CTL_KERN, KERN_PROC, KERN_PROC_PGRP, 0,
83 				    sizeof(struct kinfo_proc), 0 };
84 	struct stat	 sb;
85 	size_t		 len;
86 	struct kinfo_proc *buf, *newbuf, *bestp;
87 	u_int		 i;
88 	char		*name;
89 
90 	buf = NULL;
91 
92 	if (stat(tty, &sb) == -1)
93 		return (NULL);
94 	if ((mib[3] = tcgetpgrp(fd)) == -1)
95 		return (NULL);
96 
97 retry:
98 	if (sysctl(mib, nitems(mib), NULL, &len, NULL, 0) == -1)
99 		goto error;
100 	len = (len * 5) / 4;
101 
102 	if ((newbuf = realloc(buf, len)) == NULL)
103 		goto error;
104 	buf = newbuf;
105 
106 	mib[5] = (int)(len / sizeof(struct kinfo_proc));
107 	if (sysctl(mib, nitems(mib), buf, &len, NULL, 0) == -1) {
108 		if (errno == ENOMEM)
109 			goto retry;
110 		goto error;
111 	}
112 
113 	bestp = NULL;
114 	for (i = 0; i < len / sizeof (struct kinfo_proc); i++) {
115 		if ((dev_t)buf[i].p_tdev != sb.st_rdev)
116 			continue;
117 		if (bestp == NULL)
118 			bestp = &buf[i];
119 		else
120 			bestp = cmp_procs(&buf[i], bestp);
121 	}
122 
123 	name = NULL;
124 	if (bestp != NULL)
125 		name = strdup(bestp->p_comm);
126 
127 	free(buf);
128 	return (name);
129 
130 error:
131 	free(buf);
132 	return (NULL);
133 }
134