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/stat.h>
21#include <sys/sysctl.h>
22#include <sys/user.h>
23
24#include <err.h>
25#include <errno.h>
26#include <stdint.h>
27#include <stdlib.h>
28#include <string.h>
29#include <unistd.h>
30
31struct kinfo_proc	*cmp_procs(struct kinfo_proc *, struct kinfo_proc *);
32char			*osdep_get_name(int, char *);
33char			*osdep_get_cwd(int);
34struct event_base	*osdep_event_init(void);
35
36#ifndef nitems
37#define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
38#endif
39
40#define is_runnable(p) \
41	((p)->kp_stat == SACTIVE || (p)->kp_stat == SIDL)
42#define is_stopped(p) \
43	((p)->kp_stat == SSTOP || (p)->kp_stat == SZOMB)
44
45struct kinfo_proc *
46cmp_procs(struct kinfo_proc *p1, struct kinfo_proc *p2)
47{
48	if (is_runnable(p1) && !is_runnable(p2))
49		return (p1);
50	if (!is_runnable(p1) && is_runnable(p2))
51		return (p2);
52
53	if (is_stopped(p1) && !is_stopped(p2))
54		return (p1);
55	if (!is_stopped(p1) && is_stopped(p2))
56		return (p2);
57
58	if (strcmp(p1->kp_comm, p2->kp_comm) < 0)
59		return (p1);
60	if (strcmp(p1->kp_comm, p2->kp_comm) > 0)
61		return (p2);
62
63	if (p1->kp_pid > p2->kp_pid)
64		return (p1);
65	return (p2);
66}
67
68char *
69osdep_get_name(int fd, char *tty)
70{
71	int		 mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PGRP, 0 };
72	struct stat	 sb;
73	size_t		 len;
74	struct kinfo_proc *buf, *newbuf, *bestp;
75	u_int		 i;
76	char		*name;
77
78	buf = NULL;
79
80	if (stat(tty, &sb) == -1)
81		return (NULL);
82	if ((mib[3] = tcgetpgrp(fd)) == -1)
83		return (NULL);
84
85retry:
86	if (sysctl(mib, nitems(mib), NULL, &len, NULL, 0) == -1)
87		return (NULL);
88	len = (len * 5) / 4;
89
90	if ((newbuf = realloc(buf, len)) == NULL)
91		goto error;
92	buf = newbuf;
93
94	if (sysctl(mib, nitems(mib), buf, &len, NULL, 0) == -1) {
95		if (errno == ENOMEM)
96			goto retry;
97		goto error;
98	}
99
100	bestp = NULL;
101	for (i = 0; i < len / sizeof (struct kinfo_proc); i++) {
102		if (buf[i].kp_tdev != sb.st_rdev)
103			continue;
104		if (bestp == NULL)
105			bestp = &buf[i];
106		else
107			bestp = cmp_procs(&buf[i], bestp);
108	}
109
110	name = NULL;
111	if (bestp != NULL)
112		name = strdup(bestp->kp_comm);
113
114	free(buf);
115	return (name);
116
117error:
118	free(buf);
119	return (NULL);
120}
121
122char *
123osdep_get_cwd(int fd)
124{
125	return (NULL);
126}
127
128struct event_base *
129osdep_event_init(void)
130{
131	return (event_init());
132}
133