1 /* $Id: osdep-freebsd.c,v 1.1.1.2 2011/08/17 18:40:06 jmmv Exp $ */ 2 3 /* 4 * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.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/proc.h> 21 #include <sys/stat.h> 22 #include <sys/sysctl.h> 23 #include <sys/user.h> 24 25 #include <err.h> 26 #include <errno.h> 27 #include <event.h> 28 #include <stdint.h> 29 #include <stdlib.h> 30 #include <string.h> 31 #include <unistd.h> 32 33 struct kinfo_proc *cmp_procs(struct kinfo_proc *, struct kinfo_proc *); 34 char *osdep_get_name(int, char *); 35 struct event_base *osdep_event_init(void); 36 37 #ifndef nitems 38 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0])) 39 #endif 40 41 #define is_runnable(p) \ 42 ((p)->ki_stat == SRUN || (p)->ki_stat == SIDL) 43 #define is_stopped(p) \ 44 ((p)->ki_stat == SSTOP || (p)->ki_stat == SZOMB) 45 46 struct kinfo_proc * 47 cmp_procs(struct kinfo_proc *p1, struct kinfo_proc *p2) 48 { 49 if (is_runnable(p1) && !is_runnable(p2)) 50 return (p1); 51 if (!is_runnable(p1) && is_runnable(p2)) 52 return (p2); 53 54 if (is_stopped(p1) && !is_stopped(p2)) 55 return (p1); 56 if (!is_stopped(p1) && is_stopped(p2)) 57 return (p2); 58 59 if (p1->ki_estcpu > p2->ki_estcpu) 60 return (p1); 61 if (p1->ki_estcpu < p2->ki_estcpu) 62 return (p2); 63 64 if (p1->ki_slptime < p2->ki_slptime) 65 return (p1); 66 if (p1->ki_slptime > p2->ki_slptime) 67 return (p2); 68 69 if (strcmp(p1->ki_comm, p2->ki_comm) < 0) 70 return (p1); 71 if (strcmp(p1->ki_comm, p2->ki_comm) > 0) 72 return (p2); 73 74 if (p1->ki_pid > p2->ki_pid) 75 return (p1); 76 return (p2); 77 } 78 79 char * 80 osdep_get_name(int fd, char *tty) 81 { 82 int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PGRP, 0 }; 83 struct stat sb; 84 size_t len; 85 struct kinfo_proc *buf, *newbuf, *bestp; 86 u_int i; 87 char *name; 88 89 buf = NULL; 90 91 if (stat(tty, &sb) == -1) 92 return (NULL); 93 if ((mib[3] = tcgetpgrp(fd)) == -1) 94 return (NULL); 95 96 retry: 97 if (sysctl(mib, nitems(mib), NULL, &len, NULL, 0) == -1) 98 return (NULL); 99 len = (len * 5) / 4; 100 101 if ((newbuf = realloc(buf, len)) == NULL) 102 goto error; 103 buf = newbuf; 104 105 if (sysctl(mib, nitems(mib), buf, &len, NULL, 0) == -1) { 106 if (errno == ENOMEM) 107 goto retry; 108 goto error; 109 } 110 111 bestp = NULL; 112 for (i = 0; i < len / sizeof (struct kinfo_proc); i++) { 113 if (buf[i].ki_tdev != sb.st_rdev) 114 continue; 115 if (bestp == NULL) 116 bestp = &buf[i]; 117 else 118 bestp = cmp_procs(&buf[i], bestp); 119 } 120 121 name = NULL; 122 if (bestp != NULL) 123 name = strdup(bestp->ki_comm); 124 125 free(buf); 126 return (name); 127 128 error: 129 free(buf); 130 return (NULL); 131 } 132 133 struct event_base * 134 osdep_event_init(void) 135 { 136 /* 137 * On some versions of FreeBSD, kqueue doesn't work properly on tty 138 * file descriptors. This is fixed in recent FreeBSD versions. 139 */ 140 setenv("EVENT_NOKQUEUE", "1", 1); 141 return (event_init()); 142 } 143