xref: /netbsd-src/external/bsd/tmux/dist/osdep-freebsd.c (revision 154bfe8e089c1a0a4e9ed8414f08d3da90949162)
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>
20 #include <sys/proc.h>
21 #include <sys/stat.h>
22 #include <sys/sysctl.h>
23 #include <sys/user.h>
24 
25 #include <err.h>
26 #include <errno.h>
27 #include <event.h>
28 #include <stdint.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <libutil.h>
33 
34 #include "compat.h"
35 
36 struct kinfo_proc	*cmp_procs(struct kinfo_proc *, struct kinfo_proc *);
37 char			*osdep_get_name(int, char *);
38 char			*osdep_get_cwd(int);
39 struct event_base	*osdep_event_init(void);
40 
41 #ifndef nitems
42 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
43 #endif
44 
45 #define is_runnable(p) \
46 	((p)->ki_stat == SRUN || (p)->ki_stat == SIDL)
47 #define is_stopped(p) \
48 	((p)->ki_stat == SSTOP || (p)->ki_stat == SZOMB)
49 
50 struct kinfo_proc *
51 cmp_procs(struct kinfo_proc *p1, struct kinfo_proc *p2)
52 {
53 	if (is_runnable(p1) && !is_runnable(p2))
54 		return (p1);
55 	if (!is_runnable(p1) && is_runnable(p2))
56 		return (p2);
57 
58 	if (is_stopped(p1) && !is_stopped(p2))
59 		return (p1);
60 	if (!is_stopped(p1) && is_stopped(p2))
61 		return (p2);
62 
63 	if (p1->ki_estcpu > p2->ki_estcpu)
64 		return (p1);
65 	if (p1->ki_estcpu < p2->ki_estcpu)
66 		return (p2);
67 
68 	if (p1->ki_slptime < p2->ki_slptime)
69 		return (p1);
70 	if (p1->ki_slptime > p2->ki_slptime)
71 		return (p2);
72 
73 	if (strcmp(p1->ki_comm, p2->ki_comm) < 0)
74 		return (p1);
75 	if (strcmp(p1->ki_comm, p2->ki_comm) > 0)
76 		return (p2);
77 
78 	if (p1->ki_pid > p2->ki_pid)
79 		return (p1);
80 	return (p2);
81 }
82 
83 char *
84 osdep_get_name(int fd, char *tty)
85 {
86 	int		 mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PGRP, 0 };
87 	struct stat	 sb;
88 	size_t		 len;
89 	struct kinfo_proc *buf, *newbuf, *bestp;
90 	u_int		 i;
91 	char		*name;
92 
93 	buf = NULL;
94 
95 	if (stat(tty, &sb) == -1)
96 		return (NULL);
97 	if ((mib[3] = tcgetpgrp(fd)) == -1)
98 		return (NULL);
99 
100 retry:
101 	if (sysctl(mib, nitems(mib), NULL, &len, NULL, 0) == -1)
102 		return (NULL);
103 	len = (len * 5) / 4;
104 
105 	if ((newbuf = realloc(buf, len)) == NULL)
106 		goto error;
107 	buf = newbuf;
108 
109 	if (sysctl(mib, nitems(mib), buf, &len, NULL, 0) == -1) {
110 		if (errno == ENOMEM)
111 			goto retry;
112 		goto error;
113 	}
114 
115 	bestp = NULL;
116 	for (i = 0; i < len / sizeof (struct kinfo_proc); i++) {
117 		if (buf[i].ki_tdev != sb.st_rdev)
118 			continue;
119 		if (bestp == NULL)
120 			bestp = &buf[i];
121 		else
122 			bestp = cmp_procs(&buf[i], bestp);
123 	}
124 
125 	name = NULL;
126 	if (bestp != NULL)
127 		name = strdup(bestp->ki_comm);
128 
129 	free(buf);
130 	return (name);
131 
132 error:
133 	free(buf);
134 	return (NULL);
135 }
136 
137 static char *
138 osdep_get_cwd_fallback(int fd)
139 {
140 	static char		 wd[PATH_MAX];
141 	struct kinfo_file	*info = NULL;
142 	pid_t			 pgrp;
143 	int			 nrecords, i;
144 
145 	if ((pgrp = tcgetpgrp(fd)) == -1)
146 		return (NULL);
147 
148 	if ((info = kinfo_getfile(pgrp, &nrecords)) == NULL)
149 		return (NULL);
150 
151 	for (i = 0; i < nrecords; i++) {
152 		if (info[i].kf_fd == KF_FD_TYPE_CWD) {
153 			strlcpy(wd, info[i].kf_path, sizeof wd);
154 			free(info);
155 			return (wd);
156 		}
157 	}
158 
159 	free(info);
160 	return (NULL);
161 }
162 
163 #ifdef KERN_PROC_CWD
164 char *
165 osdep_get_cwd(int fd)
166 {
167 	static struct kinfo_file	info;
168 	static int			fallback;
169 	int	name[] = { CTL_KERN, KERN_PROC, KERN_PROC_CWD, 0 };
170 	size_t	len = sizeof info;
171 
172 	if (fallback)
173 		return (osdep_get_cwd_fallback(fd));
174 
175 	if ((name[3] = tcgetpgrp(fd)) == -1)
176 		return (NULL);
177 
178 	if (sysctl(name, 4, &info, &len, NULL, 0) == -1) {
179 		if (errno == ENOENT) {
180 			fallback = 1;
181 			return (osdep_get_cwd_fallback(fd));
182 		}
183 		return (NULL);
184 	}
185 	return (info.kf_path);
186 }
187 #else /* !KERN_PROC_CWD */
188 char *
189 osdep_get_cwd(int fd)
190 {
191 	return (osdep_get_cwd_fallback(fd));
192 }
193 #endif /* KERN_PROC_CWD */
194 
195 struct event_base *
196 osdep_event_init(void)
197 {
198 	struct event_base	*base;
199 
200 	/*
201 	 * On some versions of FreeBSD, kqueue doesn't work properly on tty
202 	 * file descriptors. This is fixed in recent FreeBSD versions.
203 	 */
204 	setenv("EVENT_NOKQUEUE", "1", 1);
205 
206 	base = event_init();
207 	unsetenv("EVENT_NOKQUEUE");
208 	return (base);
209 }
210