1 /* $OpenBSD$ */ 2 3 /* 4 * Copyright (c) 2009 Todd Carson <toc@daybefore.net> 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/param.h> 20 #include <sys/stat.h> 21 22 #include <fcntl.h> 23 #include <procfs.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(__unused int fd, char *tty) 32 { 33 struct psinfo p; 34 struct stat st; 35 char *path; 36 ssize_t bytes; 37 int f; 38 pid_t pgrp; 39 40 if ((f = open(tty, O_RDONLY)) < 0) 41 return (NULL); 42 43 if (fstat(f, &st) != 0 || ioctl(f, TIOCGPGRP, &pgrp) != 0) { 44 close(f); 45 return (NULL); 46 } 47 close(f); 48 49 xasprintf(&path, "/proc/%u/psinfo", (u_int) pgrp); 50 f = open(path, O_RDONLY); 51 free(path); 52 if (f < 0) 53 return (NULL); 54 55 bytes = read(f, &p, sizeof(p)); 56 close(f); 57 if (bytes != sizeof(p)) 58 return (NULL); 59 60 if (p.pr_ttydev != st.st_rdev) 61 return (NULL); 62 63 return (xstrdup(p.pr_fname)); 64 } 65 66 char * 67 osdep_get_cwd(int fd) 68 { 69 static char target[MAXPATHLEN + 1]; 70 char *path; 71 const char *ttypath; 72 ssize_t n; 73 pid_t pgrp; 74 int retval, ttyfd; 75 76 if ((ttypath = ptsname(fd)) == NULL) 77 return (NULL); 78 if ((ttyfd = open(ttypath, O_RDONLY|O_NOCTTY)) == -1) 79 return (NULL); 80 81 retval = ioctl(ttyfd, TIOCGPGRP, &pgrp); 82 close(ttyfd); 83 if (retval == -1) 84 return (NULL); 85 86 xasprintf(&path, "/proc/%u/path/cwd", (u_int) pgrp); 87 n = readlink(path, target, MAXPATHLEN); 88 free(path); 89 if (n > 0) { 90 target[n] = '\0'; 91 return (target); 92 } 93 return (NULL); 94 } 95 96 struct event_base * 97 osdep_event_init(void) 98 { 99 struct event_base *base; 100 101 /* 102 * On Illumos, evports don't seem to work properly. It is not clear if 103 * this a problem in libevent, with the way tmux uses file descriptors, 104 * or with some types of file descriptor. But using poll instead is 105 * fine. 106 */ 107 setenv("EVENT_NOEVPORT", "1", 1); 108 109 base = event_init(); 110 unsetenv("EVENT_NOEVPORT"); 111 return (base); 112 } 113