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/types.h> 20 #include <sys/stat.h> 21 #include <sys/param.h> 22 23 #include <event.h> 24 #include <stdio.h> 25 #include <stdlib.h> 26 #include <unistd.h> 27 28 #include "tmux.h" 29 30 char * 31 osdep_get_name(int fd, __unused char *tty) 32 { 33 FILE *f; 34 char *path, *buf; 35 size_t len; 36 int ch; 37 pid_t pgrp; 38 39 if ((pgrp = tcgetpgrp(fd)) == -1) 40 return (NULL); 41 42 xasprintf(&path, "/proc/%lld/cmdline", (long long) pgrp); 43 if ((f = fopen(path, "r")) == NULL) { 44 free(path); 45 return (NULL); 46 } 47 free(path); 48 49 len = 0; 50 buf = NULL; 51 while ((ch = fgetc(f)) != EOF) { 52 if (ch == '\0') 53 break; 54 buf = xrealloc(buf, len + 2); 55 buf[len++] = ch; 56 } 57 if (buf != NULL) 58 buf[len] = '\0'; 59 60 fclose(f); 61 return (buf); 62 } 63 64 char * 65 osdep_get_cwd(int fd) 66 { 67 static char target[MAXPATHLEN + 1]; 68 char *path; 69 pid_t pgrp, sid; 70 ssize_t n; 71 72 if ((pgrp = tcgetpgrp(fd)) == -1) 73 return (NULL); 74 75 xasprintf(&path, "/proc/%lld/cwd", (long long) pgrp); 76 n = readlink(path, target, MAXPATHLEN); 77 free(path); 78 79 if (n == -1 && ioctl(fd, TIOCGSID, &sid) != -1) { 80 xasprintf(&path, "/proc/%lld/cwd", (long long) sid); 81 n = readlink(path, target, MAXPATHLEN); 82 free(path); 83 } 84 85 if (n > 0) { 86 target[n] = '\0'; 87 return (target); 88 } 89 return (NULL); 90 } 91 92 struct event_base * 93 osdep_event_init(void) 94 { 95 struct event_base *base; 96 97 /* On Linux, epoll doesn't work on /dev/null (yes, really). */ 98 setenv("EVENT_NOEPOLL", "1", 1); 99 100 base = event_init(); 101 unsetenv("EVENT_NOEPOLL"); 102 return (base); 103 } 104