2014-11-08 12:27:43 +00:00
|
|
|
/* $OpenBSD$ */
|
2009-01-20 22:17:53 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2009 Joshua Elsasser <josh@elsasser.org>
|
|
|
|
*
|
|
|
|
* 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/types.h>
|
2016-09-12 10:06:35 +00:00
|
|
|
#include <sys/sysctl.h>
|
2009-01-20 22:17:53 +00:00
|
|
|
|
2016-09-12 10:06:35 +00:00
|
|
|
#include <Availability.h>
|
2010-12-30 20:41:08 +00:00
|
|
|
#include <event.h>
|
2012-01-27 09:01:06 +00:00
|
|
|
#include <libproc.h>
|
2009-01-20 22:17:53 +00:00
|
|
|
#include <stdlib.h>
|
2009-02-07 19:41:35 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2009-01-20 22:17:53 +00:00
|
|
|
|
2010-12-30 20:41:08 +00:00
|
|
|
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-02-09 18:08:01 +00:00
|
|
|
|
2019-04-10 18:27:09 +00:00
|
|
|
#ifndef __unused
|
2015-11-18 16:45:44 +00:00
|
|
|
#define __unused __attribute__ ((__unused__))
|
2019-04-10 18:27:09 +00:00
|
|
|
#endif
|
2009-01-20 22:17:53 +00:00
|
|
|
|
2009-02-13 00:43:04 +00:00
|
|
|
char *
|
2015-11-18 16:45:44 +00:00
|
|
|
osdep_get_name(int fd, __unused char *tty)
|
2009-01-20 22:17:53 +00:00
|
|
|
{
|
2016-09-12 10:06:35 +00:00
|
|
|
#ifdef __MAC_10_7
|
|
|
|
struct proc_bsdshortinfo bsdinfo;
|
2013-02-12 09:40:22 +00:00
|
|
|
pid_t pgrp;
|
|
|
|
int ret;
|
2009-01-26 22:57:20 +00:00
|
|
|
|
2013-02-12 09:40:22 +00:00
|
|
|
if ((pgrp = tcgetpgrp(fd)) == -1)
|
2009-02-13 00:43:04 +00:00
|
|
|
return (NULL);
|
2009-01-20 22:17:53 +00:00
|
|
|
|
2016-09-12 10:06:35 +00:00
|
|
|
ret = proc_pidinfo(pgrp, PROC_PIDT_SHORTBSDINFO, 0,
|
|
|
|
&bsdinfo, sizeof bsdinfo);
|
|
|
|
if (ret == sizeof bsdinfo && *bsdinfo.pbsi_comm != '\0')
|
|
|
|
return (strdup(bsdinfo.pbsi_comm));
|
2019-04-10 18:27:09 +00:00
|
|
|
return (NULL);
|
2016-09-12 10:06:35 +00:00
|
|
|
#else
|
|
|
|
int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, 0 };
|
|
|
|
size_t size;
|
|
|
|
struct kinfo_proc kp;
|
|
|
|
|
|
|
|
if ((mib[3] = tcgetpgrp(fd)) == -1)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
size = sizeof kp;
|
|
|
|
if (sysctl(mib, 4, &kp, &size, NULL, 0) == -1)
|
|
|
|
return (NULL);
|
2020-04-15 12:04:20 +00:00
|
|
|
if (size != (sizeof kp) || *kp.kp_proc.p_comm == '\0')
|
2016-09-12 10:06:35 +00:00
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
return (strdup(kp.kp_proc.p_comm));
|
|
|
|
#endif
|
2009-01-20 22:17:53 +00:00
|
|
|
}
|
2010-12-30 20:41:08 +00:00
|
|
|
|
2011-12-09 16:37:29 +00:00
|
|
|
char *
|
2012-09-24 13:05:38 +00:00
|
|
|
osdep_get_cwd(int fd)
|
2011-12-09 16:37:29 +00:00
|
|
|
{
|
2013-02-12 09:40:22 +00:00
|
|
|
static char wd[PATH_MAX];
|
2012-01-27 09:01:06 +00:00
|
|
|
struct proc_vnodepathinfo pathinfo;
|
2012-09-24 13:05:38 +00:00
|
|
|
pid_t pgrp;
|
2012-01-27 09:01:06 +00:00
|
|
|
int ret;
|
|
|
|
|
2012-09-24 13:05:38 +00:00
|
|
|
if ((pgrp = tcgetpgrp(fd)) == -1)
|
|
|
|
return (NULL);
|
|
|
|
|
2013-02-12 09:40:22 +00:00
|
|
|
ret = proc_pidinfo(pgrp, PROC_PIDVNODEPATHINFO, 0,
|
|
|
|
&pathinfo, sizeof pathinfo);
|
2012-01-27 09:01:06 +00:00
|
|
|
if (ret == sizeof pathinfo) {
|
|
|
|
strlcpy(wd, pathinfo.pvi_cdir.vip_path, sizeof wd);
|
|
|
|
return (wd);
|
|
|
|
}
|
2011-12-09 16:37:29 +00:00
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2020-04-18 10:45:49 +00:00
|
|
|
#ifndef __MAC_10_7
|
2010-12-30 20:41:08 +00:00
|
|
|
/*
|
|
|
|
* On OS X, kqueue and poll are both completely broken and don't
|
|
|
|
* work on anything except socket file descriptors (yes, really).
|
|
|
|
*/
|
|
|
|
setenv("EVENT_NOKQUEUE", "1", 1);
|
|
|
|
setenv("EVENT_NOPOLL", "1", 1);
|
2020-04-18 10:45:49 +00:00
|
|
|
#endif
|
2018-03-21 08:15:15 +00:00
|
|
|
|
|
|
|
base = event_init();
|
2020-04-18 10:45:49 +00:00
|
|
|
#ifndef __MAC_10_7
|
2018-03-21 08:15:15 +00:00
|
|
|
unsetenv("EVENT_NOKQUEUE");
|
|
|
|
unsetenv("EVENT_NOPOLL");
|
2020-04-18 10:45:49 +00:00
|
|
|
#endif
|
2018-03-21 08:15:15 +00:00
|
|
|
return (base);
|
2010-12-30 20:41:08 +00:00
|
|
|
}
|