1 /* $OpenBSD$ */ 2 3 /* 4 * Copyright (c) 2011 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/param.h> 20 #include <sys/procfs.h> 21 22 #include <stdlib.h> 23 #include <string.h> 24 #include <fcntl.h> 25 26 #include "tmux.h" 27 28 char * 29 osdep_get_name(__unused int fd, char *tty) 30 { 31 struct psinfo p; 32 char *path; 33 ssize_t bytes; 34 int f, ttyfd, retval; 35 pid_t pgrp; 36 37 if ((ttyfd = open(tty, O_RDONLY|O_NOCTTY)) == -1) 38 return (NULL); 39 40 retval = ioctl(ttyfd, TIOCGPGRP, &pgrp); 41 close(ttyfd); 42 if (retval == -1) 43 return (NULL); 44 45 xasprintf(&path, "/proc/%u/psinfo", (u_int) pgrp); 46 f = open(path, O_RDONLY); 47 free(path); 48 if (f < 0) 49 return (NULL); 50 51 bytes = read(f, &p, sizeof(p)); 52 close(f); 53 if (bytes != sizeof(p)) 54 return (NULL); 55 56 return (xstrdup(p.pr_fname)); 57 } 58 59 char * 60 osdep_get_cwd(int fd) 61 { 62 static char target[MAXPATHLEN + 1]; 63 char *path; 64 const char *ttypath; 65 ssize_t n; 66 pid_t pgrp; 67 int len, retval, ttyfd; 68 69 if ((ttypath = ptsname(fd)) == NULL) 70 return (NULL); 71 if ((ttyfd = open(ttypath, O_RDONLY|O_NOCTTY)) == -1) 72 return (NULL); 73 74 retval = ioctl(ttyfd, TIOCGPGRP, &pgrp); 75 close(ttyfd); 76 if (retval == -1) 77 return (NULL); 78 79 xasprintf(&path, "/proc/%u/cwd", (u_int) pgrp); 80 n = readlink(path, target, MAXPATHLEN); 81 free(path); 82 if (n > 0) { 83 target[n] = '\0'; 84 if ((len = strlen(target)) > 1 && target[len - 1] == '/') 85 target[len - 1] = '\0'; 86 return (target); 87 } 88 return (NULL); 89 } 90 91 struct event_base * 92 osdep_event_init(void) 93 { 94 return (event_init()); 95 } 96