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 21 #include <event.h> 22 #include <libproc.h> 23 #include <stdlib.h> 24 #include <string.h> 25 #include <unistd.h> 26 27 char *osdep_get_name(int, char *); 28 char *osdep_get_cwd(int); 29 struct event_base *osdep_event_init(void); 30 31 #define unused __attribute__ ((unused)) 32 33 char * 34 osdep_get_name(int fd, unused char *tty) 35 { 36 struct proc_bsdinfo bsdinfo; 37 pid_t pgrp; 38 int ret; 39 40 if ((pgrp = tcgetpgrp(fd)) == -1) 41 return (NULL); 42 43 ret = proc_pidinfo(pgrp, PROC_PIDTBSDINFO, 0, 44 &bsdinfo, sizeof bsdinfo); 45 if (ret == sizeof bsdinfo && *bsdinfo.pbi_comm != '\0') 46 return (strdup(bsdinfo.pbi_comm)); 47 return (NULL); 48 } 49 50 char * 51 osdep_get_cwd(int fd) 52 { 53 static char wd[PATH_MAX]; 54 struct proc_vnodepathinfo pathinfo; 55 pid_t pgrp; 56 int ret; 57 58 if ((pgrp = tcgetpgrp(fd)) == -1) 59 return (NULL); 60 61 ret = proc_pidinfo(pgrp, PROC_PIDVNODEPATHINFO, 0, 62 &pathinfo, sizeof pathinfo); 63 if (ret == sizeof pathinfo) { 64 strlcpy(wd, pathinfo.pvi_cdir.vip_path, sizeof wd); 65 return (wd); 66 } 67 return (NULL); 68 } 69 70 struct event_base * 71 osdep_event_init(void) 72 { 73 /* 74 * On OS X, kqueue and poll are both completely broken and don't 75 * work on anything except socket file descriptors (yes, really). 76 */ 77 setenv("EVENT_NOKQUEUE", "1", 1); 78 setenv("EVENT_NOPOLL", "1", 1); 79 return (event_init()); 80 } 81