1 /* $OpenBSD$ */
2 
3 /*
4  * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
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>	/* MAXCOMLEN */
20 #include <sys/types.h>
21 #include <sys/proc.h>
22 #include <sys/sysctl.h>
23 #include <sys/stat.h>
24 
25 #include <errno.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 
30 #include "compat.h"
31 
32 #ifndef nitems
33 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
34 #endif
35 
36 #define is_runnable(p) \
37 	((p)->p_stat == SRUN || (p)->p_stat == SIDL || (p)->p_stat == SONPROC)
38 #define is_stopped(p) \
39 	((p)->p_stat == SSTOP || (p)->p_stat == SDEAD)
40 
41 static struct kinfo_proc *cmp_procs(struct kinfo_proc *, struct kinfo_proc *);
42 char			 *osdep_get_name(int, char *);
43 char			 *osdep_get_cwd(int);
44 struct event_base	 *osdep_event_init(void);
45 
46 static struct kinfo_proc *
cmp_procs(struct kinfo_proc * p1,struct kinfo_proc * p2)47 cmp_procs(struct kinfo_proc *p1, struct kinfo_proc *p2)
48 {
49 	if (is_runnable(p1) && !is_runnable(p2))
50 		return (p1);
51 	if (!is_runnable(p1) && is_runnable(p2))
52 		return (p2);
53 
54 	if (is_stopped(p1) && !is_stopped(p2))
55 		return (p1);
56 	if (!is_stopped(p1) && is_stopped(p2))
57 		return (p2);
58 
59 	if (p1->p_estcpu > p2->p_estcpu)
60 		return (p1);
61 	if (p1->p_estcpu < p2->p_estcpu)
62 		return (p2);
63 
64 	if (p1->p_slptime < p2->p_slptime)
65 		return (p1);
66 	if (p1->p_slptime > p2->p_slptime)
67 		return (p2);
68 
69 	if ((p1->p_flag & P_SINTR) && !(p2->p_flag & P_SINTR))
70 		return (p1);
71 	if (!(p1->p_flag & P_SINTR) && (p2->p_flag & P_SINTR))
72 		return (p2);
73 
74 	if (strcmp(p1->p_comm, p2->p_comm) < 0)
75 		return (p1);
76 	if (strcmp(p1->p_comm, p2->p_comm) > 0)
77 		return (p2);
78 
79 	if (p1->p_pid > p2->p_pid)
80 		return (p1);
81 	return (p2);
82 }
83 
84 char *
osdep_get_name(int fd,char * tty)85 osdep_get_name(int fd, char *tty)
86 {
87 	int		 mib[6] = { CTL_KERN, KERN_PROC, KERN_PROC_PGRP, 0,
88 				    sizeof(struct kinfo_proc), 0 };
89 	struct stat	 sb;
90 	size_t		 len;
91 	struct kinfo_proc *buf, *newbuf, *bestp;
92 	u_int		 i;
93 	char		*name;
94 
95 	buf = NULL;
96 
97 	if (stat(tty, &sb) == -1)
98 		return (NULL);
99 	if ((mib[3] = tcgetpgrp(fd)) == -1)
100 		return (NULL);
101 
102 retry:
103 	if (sysctl(mib, nitems(mib), NULL, &len, NULL, 0) == -1)
104 		goto error;
105 	len = (len * 5) / 4;
106 
107 	if ((newbuf = realloc(buf, len)) == NULL)
108 		goto error;
109 	buf = newbuf;
110 
111 	mib[5] = (int)(len / sizeof(struct kinfo_proc));
112 	if (sysctl(mib, nitems(mib), buf, &len, NULL, 0) == -1) {
113 		if (errno == ENOMEM)
114 			goto retry;
115 		goto error;
116 	}
117 
118 	bestp = NULL;
119 	for (i = 0; i < len / sizeof (struct kinfo_proc); i++) {
120 		if ((dev_t)buf[i].p_tdev != sb.st_rdev)
121 			continue;
122 		if (bestp == NULL)
123 			bestp = &buf[i];
124 		else
125 			bestp = cmp_procs(&buf[i], bestp);
126 	}
127 
128 	name = NULL;
129 	if (bestp != NULL)
130 		name = strdup(bestp->p_comm);
131 
132 	free(buf);
133 	return (name);
134 
135 error:
136 	free(buf);
137 	return (NULL);
138 }
139 
140 char *
osdep_get_cwd(int fd)141 osdep_get_cwd(int fd)
142 {
143 	int		name[] = { CTL_KERN, KERN_PROC_CWD, 0 };
144 	static char	path[MAXPATHLEN];
145 	size_t		pathlen = sizeof path;
146 
147 	if ((name[2] = tcgetpgrp(fd)) == -1)
148 		return (NULL);
149 	if (sysctl(name, 3, path, &pathlen, NULL, 0) != 0)
150 		return (NULL);
151 	return (path);
152 }
153 
154 struct event_base *
osdep_event_init(void)155 osdep_event_init(void)
156 {
157 	return (event_init());
158 }
159