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