osdep-freebsd.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /* $OpenBSD$ */
  2. /*
  3. * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
  14. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  15. * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include <sys/param.h>
  18. #include <sys/proc.h>
  19. #include <sys/stat.h>
  20. #include <sys/sysctl.h>
  21. #include <sys/user.h>
  22. #include <err.h>
  23. #include <errno.h>
  24. #include <event.h>
  25. #include <stdint.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <unistd.h>
  29. #include <libutil.h>
  30. struct kinfo_proc *cmp_procs(struct kinfo_proc *, struct kinfo_proc *);
  31. char *osdep_get_name(int, char *);
  32. char *osdep_get_cwd(int);
  33. struct event_base *osdep_event_init(void);
  34. #ifndef nitems
  35. #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
  36. #endif
  37. #define is_runnable(p) \
  38. ((p)->ki_stat == SRUN || (p)->ki_stat == SIDL)
  39. #define is_stopped(p) \
  40. ((p)->ki_stat == SSTOP || (p)->ki_stat == SZOMB)
  41. struct kinfo_proc *
  42. cmp_procs(struct kinfo_proc *p1, struct kinfo_proc *p2)
  43. {
  44. if (is_runnable(p1) && !is_runnable(p2))
  45. return (p1);
  46. if (!is_runnable(p1) && is_runnable(p2))
  47. return (p2);
  48. if (is_stopped(p1) && !is_stopped(p2))
  49. return (p1);
  50. if (!is_stopped(p1) && is_stopped(p2))
  51. return (p2);
  52. if (p1->ki_estcpu > p2->ki_estcpu)
  53. return (p1);
  54. if (p1->ki_estcpu < p2->ki_estcpu)
  55. return (p2);
  56. if (p1->ki_slptime < p2->ki_slptime)
  57. return (p1);
  58. if (p1->ki_slptime > p2->ki_slptime)
  59. return (p2);
  60. if (strcmp(p1->ki_comm, p2->ki_comm) < 0)
  61. return (p1);
  62. if (strcmp(p1->ki_comm, p2->ki_comm) > 0)
  63. return (p2);
  64. if (p1->ki_pid > p2->ki_pid)
  65. return (p1);
  66. return (p2);
  67. }
  68. char *
  69. osdep_get_name(int fd, char *tty)
  70. {
  71. int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PGRP, 0 };
  72. struct stat sb;
  73. size_t len;
  74. struct kinfo_proc *buf, *newbuf, *bestp;
  75. u_int i;
  76. char *name;
  77. buf = NULL;
  78. if (stat(tty, &sb) == -1)
  79. return (NULL);
  80. if ((mib[3] = tcgetpgrp(fd)) == -1)
  81. return (NULL);
  82. retry:
  83. if (sysctl(mib, nitems(mib), NULL, &len, NULL, 0) == -1)
  84. return (NULL);
  85. len = (len * 5) / 4;
  86. if ((newbuf = realloc(buf, len)) == NULL)
  87. goto error;
  88. buf = newbuf;
  89. if (sysctl(mib, nitems(mib), buf, &len, NULL, 0) == -1) {
  90. if (errno == ENOMEM)
  91. goto retry;
  92. goto error;
  93. }
  94. bestp = NULL;
  95. for (i = 0; i < len / sizeof (struct kinfo_proc); i++) {
  96. if (buf[i].ki_tdev != sb.st_rdev)
  97. continue;
  98. if (bestp == NULL)
  99. bestp = &buf[i];
  100. else
  101. bestp = cmp_procs(&buf[i], bestp);
  102. }
  103. name = NULL;
  104. if (bestp != NULL)
  105. name = strdup(bestp->ki_comm);
  106. free(buf);
  107. return (name);
  108. error:
  109. free(buf);
  110. return (NULL);
  111. }
  112. static char *
  113. osdep_get_cwd_fallback(int fd)
  114. {
  115. static char wd[PATH_MAX];
  116. struct kinfo_file *info = NULL;
  117. pid_t pgrp;
  118. int nrecords, i;
  119. if ((pgrp = tcgetpgrp(fd)) == -1)
  120. return (NULL);
  121. if ((info = kinfo_getfile(pgrp, &nrecords)) == NULL)
  122. return (NULL);
  123. for (i = 0; i < nrecords; i++) {
  124. if (info[i].kf_fd == KF_FD_TYPE_CWD) {
  125. strlcpy(wd, info[i].kf_path, sizeof wd);
  126. free(info);
  127. return (wd);
  128. }
  129. }
  130. free(info);
  131. return (NULL);
  132. }
  133. #ifdef KERN_PROC_CWD
  134. char *
  135. osdep_get_cwd(int fd)
  136. {
  137. static struct kinfo_file info;
  138. static int fallback;
  139. int name[] = { CTL_KERN, KERN_PROC, KERN_PROC_CWD, 0 };
  140. size_t len = sizeof info;
  141. if (fallback)
  142. return (osdep_get_cwd_fallback(fd));
  143. if ((name[3] = tcgetpgrp(fd)) == -1)
  144. return (NULL);
  145. if (sysctl(name, 4, &info, &len, NULL, 0) == -1) {
  146. if (errno == ENOENT) {
  147. fallback = 1;
  148. return (osdep_get_cwd_fallback(fd));
  149. }
  150. return (NULL);
  151. }
  152. return (info.kf_path);
  153. }
  154. #else /* !KERN_PROC_CWD */
  155. char *
  156. osdep_get_cwd(int fd)
  157. {
  158. return (osdep_get_cwd_fallback(fd));
  159. }
  160. #endif /* KERN_PROC_CWD */
  161. struct event_base *
  162. osdep_event_init(void)
  163. {
  164. /*
  165. * On some versions of FreeBSD, kqueue doesn't work properly on tty
  166. * file descriptors. This is fixed in recent FreeBSD versions.
  167. */
  168. setenv("EVENT_NOKQUEUE", "1", 1);
  169. return (event_init());
  170. }