2014-11-08 12:27:43 +00:00
|
|
|
/* $OpenBSD$ */
|
2009-01-20 19:35:03 +00:00
|
|
|
|
|
|
|
/*
|
2016-01-19 18:07:25 +00:00
|
|
|
* Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
|
2009-01-20 19:35:03 +00:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
|
|
|
|
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
2009-01-26 22:57:20 +00:00
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/stat.h>
|
2009-01-20 19:35:03 +00:00
|
|
|
#include <sys/sysctl.h>
|
2009-01-26 22:57:20 +00:00
|
|
|
#include <sys/user.h>
|
2009-01-20 19:35:03 +00:00
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
2010-12-30 20:41:08 +00:00
|
|
|
#include <event.h>
|
2009-01-20 19:35:03 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2011-12-09 16:37:29 +00:00
|
|
|
#include <libutil.h>
|
2009-01-20 19:35:03 +00:00
|
|
|
|
2018-11-21 08:50:22 +00:00
|
|
|
#include "compat.h"
|
|
|
|
|
2009-08-09 16:37:05 +00:00
|
|
|
struct kinfo_proc *cmp_procs(struct kinfo_proc *, struct kinfo_proc *);
|
|
|
|
char *osdep_get_name(int, char *);
|
2012-09-24 13:05:38 +00:00
|
|
|
char *osdep_get_cwd(int);
|
2010-12-30 20:41:08 +00:00
|
|
|
struct event_base *osdep_event_init(void);
|
2009-01-26 22:57:20 +00:00
|
|
|
|
2009-06-26 15:31:15 +00:00
|
|
|
#ifndef nitems
|
2009-01-26 22:57:20 +00:00
|
|
|
#define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
|
2009-06-26 15:31:15 +00:00
|
|
|
#endif
|
2009-01-20 19:35:03 +00:00
|
|
|
|
2009-02-07 19:16:25 +00:00
|
|
|
#define is_runnable(p) \
|
|
|
|
((p)->ki_stat == SRUN || (p)->ki_stat == SIDL)
|
|
|
|
#define is_stopped(p) \
|
2009-02-07 19:24:50 +00:00
|
|
|
((p)->ki_stat == SSTOP || (p)->ki_stat == SZOMB)
|
2009-02-07 19:16:25 +00:00
|
|
|
|
2009-08-09 16:37:05 +00:00
|
|
|
struct kinfo_proc *
|
|
|
|
cmp_procs(struct kinfo_proc *p1, struct kinfo_proc *p2)
|
|
|
|
{
|
|
|
|
if (is_runnable(p1) && !is_runnable(p2))
|
|
|
|
return (p1);
|
|
|
|
if (!is_runnable(p1) && is_runnable(p2))
|
|
|
|
return (p2);
|
|
|
|
|
|
|
|
if (is_stopped(p1) && !is_stopped(p2))
|
|
|
|
return (p1);
|
|
|
|
if (!is_stopped(p1) && is_stopped(p2))
|
|
|
|
return (p2);
|
|
|
|
|
|
|
|
if (p1->ki_estcpu > p2->ki_estcpu)
|
|
|
|
return (p1);
|
|
|
|
if (p1->ki_estcpu < p2->ki_estcpu)
|
|
|
|
return (p2);
|
|
|
|
|
|
|
|
if (p1->ki_slptime < p2->ki_slptime)
|
|
|
|
return (p1);
|
|
|
|
if (p1->ki_slptime > p2->ki_slptime)
|
|
|
|
return (p2);
|
|
|
|
|
|
|
|
if (strcmp(p1->ki_comm, p2->ki_comm) < 0)
|
|
|
|
return (p1);
|
|
|
|
if (strcmp(p1->ki_comm, p2->ki_comm) > 0)
|
|
|
|
return (p2);
|
|
|
|
|
|
|
|
if (p1->ki_pid > p2->ki_pid)
|
|
|
|
return (p1);
|
|
|
|
return (p2);
|
|
|
|
}
|
|
|
|
|
2009-02-13 00:43:04 +00:00
|
|
|
char *
|
|
|
|
osdep_get_name(int fd, char *tty)
|
2009-01-26 22:57:20 +00:00
|
|
|
{
|
2009-02-08 12:33:03 +00:00
|
|
|
int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PGRP, 0 };
|
2009-01-26 22:57:20 +00:00
|
|
|
struct stat sb;
|
|
|
|
size_t len;
|
2009-08-09 18:00:45 +00:00
|
|
|
struct kinfo_proc *buf, *newbuf, *bestp;
|
2009-01-26 22:57:20 +00:00
|
|
|
u_int i;
|
2009-02-13 00:43:04 +00:00
|
|
|
char *name;
|
2009-02-09 18:08:01 +00:00
|
|
|
|
2009-01-26 22:57:20 +00:00
|
|
|
buf = NULL;
|
|
|
|
|
|
|
|
if (stat(tty, &sb) == -1)
|
2009-02-13 00:43:04 +00:00
|
|
|
return (NULL);
|
2009-02-08 12:33:03 +00:00
|
|
|
if ((mib[3] = tcgetpgrp(fd)) == -1)
|
2009-02-13 00:43:04 +00:00
|
|
|
return (NULL);
|
2009-01-26 22:57:20 +00:00
|
|
|
|
|
|
|
retry:
|
|
|
|
if (sysctl(mib, nitems(mib), NULL, &len, NULL, 0) == -1)
|
2009-02-13 00:43:04 +00:00
|
|
|
return (NULL);
|
2009-01-26 22:57:20 +00:00
|
|
|
len = (len * 5) / 4;
|
|
|
|
|
2009-08-09 16:37:05 +00:00
|
|
|
if ((newbuf = realloc(buf, len)) == NULL)
|
|
|
|
goto error;
|
2009-01-26 22:57:20 +00:00
|
|
|
buf = newbuf;
|
|
|
|
|
|
|
|
if (sysctl(mib, nitems(mib), buf, &len, NULL, 0) == -1) {
|
|
|
|
if (errno == ENOMEM)
|
|
|
|
goto retry;
|
2009-08-09 16:37:05 +00:00
|
|
|
goto error;
|
2009-01-26 22:57:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bestp = NULL;
|
|
|
|
for (i = 0; i < len / sizeof (struct kinfo_proc); i++) {
|
2009-01-27 23:10:18 +00:00
|
|
|
if (buf[i].ki_tdev != sb.st_rdev)
|
|
|
|
continue;
|
2009-08-09 16:37:05 +00:00
|
|
|
if (bestp == NULL)
|
|
|
|
bestp = &buf[i];
|
|
|
|
else
|
|
|
|
bestp = cmp_procs(&buf[i], bestp);
|
2009-01-26 22:57:20 +00:00
|
|
|
}
|
2009-01-27 23:10:18 +00:00
|
|
|
|
2009-02-13 00:43:04 +00:00
|
|
|
name = NULL;
|
|
|
|
if (bestp != NULL)
|
|
|
|
name = strdup(bestp->ki_comm);
|
2009-01-26 22:57:20 +00:00
|
|
|
|
|
|
|
free(buf);
|
2009-02-13 00:43:04 +00:00
|
|
|
return (name);
|
2009-08-09 16:37:05 +00:00
|
|
|
|
|
|
|
error:
|
|
|
|
free(buf);
|
|
|
|
return (NULL);
|
2009-01-20 19:35:03 +00:00
|
|
|
}
|
2010-12-30 20:41:08 +00:00
|
|
|
|
2014-11-06 14:00:56 +00:00
|
|
|
static char *
|
|
|
|
osdep_get_cwd_fallback(int fd)
|
2011-12-09 16:37:29 +00:00
|
|
|
{
|
|
|
|
static char wd[PATH_MAX];
|
|
|
|
struct kinfo_file *info = NULL;
|
2012-09-24 13:05:38 +00:00
|
|
|
pid_t pgrp;
|
2011-12-09 16:37:29 +00:00
|
|
|
int nrecords, i;
|
|
|
|
|
2012-09-24 13:05:38 +00:00
|
|
|
if ((pgrp = tcgetpgrp(fd)) == -1)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
if ((info = kinfo_getfile(pgrp, &nrecords)) == NULL)
|
2011-12-09 16:37:29 +00:00
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
for (i = 0; i < nrecords; i++) {
|
|
|
|
if (info[i].kf_fd == KF_FD_TYPE_CWD) {
|
|
|
|
strlcpy(wd, info[i].kf_path, sizeof wd);
|
|
|
|
free(info);
|
|
|
|
return (wd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(info);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2014-11-06 14:00:56 +00:00
|
|
|
#ifdef KERN_PROC_CWD
|
|
|
|
char *
|
|
|
|
osdep_get_cwd(int fd)
|
|
|
|
{
|
|
|
|
static struct kinfo_file info;
|
|
|
|
static int fallback;
|
|
|
|
int name[] = { CTL_KERN, KERN_PROC, KERN_PROC_CWD, 0 };
|
|
|
|
size_t len = sizeof info;
|
|
|
|
|
|
|
|
if (fallback)
|
|
|
|
return (osdep_get_cwd_fallback(fd));
|
|
|
|
|
|
|
|
if ((name[3] = tcgetpgrp(fd)) == -1)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
if (sysctl(name, 4, &info, &len, NULL, 0) == -1) {
|
|
|
|
if (errno == ENOENT) {
|
|
|
|
fallback = 1;
|
|
|
|
return (osdep_get_cwd_fallback(fd));
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
return (info.kf_path);
|
|
|
|
}
|
|
|
|
#else /* !KERN_PROC_CWD */
|
|
|
|
char *
|
|
|
|
osdep_get_cwd(int fd)
|
|
|
|
{
|
|
|
|
return (osdep_get_cwd_fallback(fd));
|
|
|
|
}
|
|
|
|
#endif /* KERN_PROC_CWD */
|
|
|
|
|
2010-12-30 20:41:08 +00:00
|
|
|
struct event_base *
|
|
|
|
osdep_event_init(void)
|
|
|
|
{
|
2018-03-21 08:15:15 +00:00
|
|
|
struct event_base *base;
|
|
|
|
|
2010-12-30 20:41:08 +00:00
|
|
|
/*
|
|
|
|
* On some versions of FreeBSD, kqueue doesn't work properly on tty
|
|
|
|
* file descriptors. This is fixed in recent FreeBSD versions.
|
|
|
|
*/
|
|
|
|
setenv("EVENT_NOKQUEUE", "1", 1);
|
2018-03-21 08:15:15 +00:00
|
|
|
|
|
|
|
base = event_init();
|
|
|
|
unsetenv("EVENT_NOKQUEUE");
|
|
|
|
return (base);
|
2010-12-30 20:41:08 +00:00
|
|
|
}
|