1 /* $OpenBSD$ */ 2 3 /* 4 * Copyright (c) 2009 Joshua Elsasser <josh@elsasser.org> 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/types.h> 20 #include <sys/sysctl.h> 21 22 #include <Availability.h> 23 #include <event.h> 24 #include <libproc.h> 25 #include <stdlib.h> 26 #include <string.h> 27 #include <unistd.h> 28 29 char *osdep_get_name(int, char *); 30 char *osdep_get_cwd(int); 31 struct event_base *osdep_event_init(void); 32 33 #ifndef __unused 34 #define __unused __attribute__ ((__unused__)) 35 #endif 36 37 char * 38 osdep_get_name(int fd, __unused char *tty) 39 { 40 #ifdef __MAC_10_7 41 struct proc_bsdshortinfo bsdinfo; 42 pid_t pgrp; 43 int ret; 44 45 if ((pgrp = tcgetpgrp(fd)) == -1) 46 return (NULL); 47 48 ret = proc_pidinfo(pgrp, PROC_PIDT_SHORTBSDINFO, 0, 49 &bsdinfo, sizeof bsdinfo); 50 if (ret == sizeof bsdinfo && *bsdinfo.pbsi_comm != '\0') 51 return (strdup(bsdinfo.pbsi_comm)); 52 return (NULL); 53 #else 54 int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, 0 }; 55 size_t size; 56 struct kinfo_proc kp; 57 58 if ((mib[3] = tcgetpgrp(fd)) == -1) 59 return (NULL); 60 61 size = sizeof kp; 62 if (sysctl(mib, 4, &kp, &size, NULL, 0) == -1) 63 return (NULL); 64 if (*kp.kp_proc.p_comm == '\0') 65 return (NULL); 66 67 return (strdup(kp.kp_proc.p_comm)); 68 #endif 69 } 70 71 char * 72 osdep_get_cwd(int fd) 73 { 74 static char wd[PATH_MAX]; 75 struct proc_vnodepathinfo pathinfo; 76 pid_t pgrp; 77 int ret; 78 79 if ((pgrp = tcgetpgrp(fd)) == -1) 80 return (NULL); 81 82 ret = proc_pidinfo(pgrp, PROC_PIDVNODEPATHINFO, 0, 83 &pathinfo, sizeof pathinfo); 84 if (ret == sizeof pathinfo) { 85 strlcpy(wd, pathinfo.pvi_cdir.vip_path, sizeof wd); 86 return (wd); 87 } 88 return (NULL); 89 } 90 91 struct event_base * 92 osdep_event_init(void) 93 { 94 struct event_base *base; 95 96 /* 97 * On OS X, kqueue and poll are both completely broken and don't 98 * work on anything except socket file descriptors (yes, really). 99 */ 100 setenv("EVENT_NOKQUEUE", "1", 1); 101 setenv("EVENT_NOPOLL", "1", 1); 102 103 base = event_init(); 104 unsetenv("EVENT_NOKQUEUE"); 105 unsetenv("EVENT_NOPOLL"); 106 return (base); 107 } 108