osdep-netbsd.c revision 1.1.1.10
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/param.h> 20#include <sys/proc.h> 21#include <sys/stat.h> 22#include <sys/sysctl.h> 23 24#include <errno.h> 25#include <limits.h> 26#include <stdlib.h> 27#include <string.h> 28#include <unistd.h> 29 30#include "tmux.h" 31 32#define is_runnable(p) \ 33 ((p)->p_stat == LSRUN || (p)->p_stat == SIDL) 34#define is_stopped(p) \ 35 ((p)->p_stat == SSTOP || (p)->p_stat == SZOMB) 36 37struct kinfo_proc2 *cmp_procs(struct kinfo_proc2 *, struct kinfo_proc2 *); 38char *osdep_get_name(int, char *); 39char *osdep_get_cwd(int); 40struct event_base *osdep_event_init(void); 41 42struct kinfo_proc2 * 43cmp_procs(struct kinfo_proc2 *p1, struct kinfo_proc2 *p2) 44{ 45 if (is_runnable(p1) && !is_runnable(p2)) 46 return (p1); 47 if (!is_runnable(p1) && is_runnable(p2)) 48 return (p2); 49 50 if (is_stopped(p1) && !is_stopped(p2)) 51 return (p1); 52 if (!is_stopped(p1) && is_stopped(p2)) 53 return (p2); 54 55 if (p1->p_estcpu > p2->p_estcpu) 56 return (p1); 57 if (p1->p_estcpu < p2->p_estcpu) 58 return (p2); 59 60 if (p1->p_slptime < p2->p_slptime) 61 return (p1); 62 if (p1->p_slptime > p2->p_slptime) 63 return (p2); 64 65 if (p1->p_pid > p2->p_pid) 66 return (p1); 67 return (p2); 68} 69 70char * 71osdep_get_name(int fd, __unused char *tty) 72{ 73 int mib[6]; 74 struct stat sb; 75 size_t len, i; 76 struct kinfo_proc2 *buf, *newbuf, *bestp; 77 char *name; 78 79 if (stat(tty, &sb) == -1) 80 return (NULL); 81 if ((mib[3] = tcgetpgrp(fd)) == -1) 82 return (NULL); 83 84 buf = NULL; 85 len = sizeof bestp; 86 87 mib[0] = CTL_KERN; 88 mib[1] = KERN_PROC2; 89 mib[2] = KERN_PROC_PGRP; 90 mib[4] = sizeof *buf; 91 92retry: 93 mib[5] = 0; 94 95 if (sysctl(mib, __arraycount(mib), NULL, &len, NULL, 0) == -1) 96 return (NULL); 97 98 if ((newbuf = realloc(buf, len)) == NULL) 99 goto error; 100 buf = newbuf; 101 102 mib[5] = len / (sizeof *buf); 103 if (sysctl(mib, __arraycount(mib), buf, &len, NULL, 0) == -1) { 104 if (errno == ENOMEM) 105 goto retry; 106 goto error; 107 } 108 109 bestp = NULL; 110 for (i = 0; i < len / (sizeof *buf); i++) { 111 if (buf[i].p_tdev != sb.st_rdev) 112 continue; 113 if (bestp == NULL) 114 bestp = &buf[i]; 115 else 116 bestp = cmp_procs(&buf[i], bestp); 117 } 118 119 name = NULL; 120 if (bestp != NULL) 121 name = strdup(bestp->p_comm); 122 123 free(buf); 124 return (name); 125 126error: 127 free(buf); 128 return (NULL); 129} 130 131char * 132osdep_get_cwd(int fd) 133{ 134 static char target[PATH_MAX + 1]; 135 pid_t pgrp; 136#ifdef KERN_PROC_CWD 137 int mib[4]; 138 size_t len; 139#else 140 char *path; 141 ssize_t n; 142#endif 143 144 if ((pgrp = tcgetpgrp(fd)) == -1) 145 return (NULL); 146 147#ifdef KERN_PROC_CWD 148 mib[0] = CTL_KERN; 149 mib[1] = KERN_PROC_ARGS; 150 mib[2] = pgrp; 151 mib[3] = KERN_PROC_CWD; 152 len = sizeof(target); 153 if (sysctl(mib, __arraycount(mib), target, &len, NULL, 0) == 0) 154 return (target); 155#else 156 xasprintf(&path, "/proc/%lld/cwd", (long long) pgrp); 157 n = readlink(path, target, sizeof(target) - 1); 158 free(path); 159 if (n > 0) { 160 target[n] = '\0'; 161 return (target); 162 } 163#endif 164 165 return (NULL); 166} 167 168struct event_base * 169osdep_event_init(void) 170{ 171 return (event_init()); 172} 173