osdep-netbsd.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 <errno.h>
  22. #include <event.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <unistd.h>
  26. #define is_runnable(p) \
  27. ((p)->p_stat == LSRUN || (p)->p_stat == SIDL)
  28. #define is_stopped(p) \
  29. ((p)->p_stat == SSTOP || (p)->p_stat == SZOMB)
  30. struct kinfo_proc2 *cmp_procs(struct kinfo_proc2 *, struct kinfo_proc2 *);
  31. char *osdep_get_name(int, char *);
  32. char *osdep_get_cwd(int);
  33. struct event_base *osdep_event_init(void);
  34. struct kinfo_proc2 *
  35. cmp_procs(struct kinfo_proc2 *p1, struct kinfo_proc2 *p2)
  36. {
  37. if (is_runnable(p1) && !is_runnable(p2))
  38. return (p1);
  39. if (!is_runnable(p1) && is_runnable(p2))
  40. return (p2);
  41. if (is_stopped(p1) && !is_stopped(p2))
  42. return (p1);
  43. if (!is_stopped(p1) && is_stopped(p2))
  44. return (p2);
  45. if (p1->p_estcpu > p2->p_estcpu)
  46. return (p1);
  47. if (p1->p_estcpu < p2->p_estcpu)
  48. return (p2);
  49. if (p1->p_slptime < p2->p_slptime)
  50. return (p1);
  51. if (p1->p_slptime > p2->p_slptime)
  52. return (p2);
  53. if (p1->p_pid > p2->p_pid)
  54. return (p1);
  55. return (p2);
  56. }
  57. char *
  58. osdep_get_name(int fd, __unused char *tty)
  59. {
  60. int mib[6];
  61. struct stat sb;
  62. size_t len, i;
  63. struct kinfo_proc2 *buf, *newbuf, *bestp;
  64. char *name;
  65. if (stat(tty, &sb) == -1)
  66. return (NULL);
  67. if ((mib[3] = tcgetpgrp(fd)) == -1)
  68. return (NULL);
  69. buf = NULL;
  70. len = sizeof(bestp);
  71. mib[0] = CTL_KERN;
  72. mib[1] = KERN_PROC2;
  73. mib[2] = KERN_PROC_PGRP;
  74. mib[4] = sizeof (*buf);
  75. mib[5] = 0;
  76. retry:
  77. if (sysctl(mib, __arraycount(mib), NULL, &len, NULL, 0) == -1)
  78. return (NULL);
  79. if ((newbuf = realloc(buf, len * sizeof (*buf))) == NULL)
  80. goto error;
  81. buf = newbuf;
  82. mib[5] = len / sizeof(*buf);
  83. if (sysctl(mib, __arraycount(mib), buf, &len, NULL, 0) == -1) {
  84. if (errno == ENOMEM)
  85. goto retry; /* possible infinite loop? */
  86. goto error;
  87. }
  88. bestp = NULL;
  89. for (i = 0; i < len / sizeof (*buf); i++) {
  90. if (buf[i].p_tdev != sb.st_rdev)
  91. continue;
  92. if (bestp == NULL)
  93. bestp = &buf[i];
  94. else
  95. bestp = cmp_procs(&buf[i], bestp);
  96. }
  97. name = NULL;
  98. if (bestp != NULL)
  99. name = strdup(bestp->p_comm);
  100. free(buf);
  101. return (name);
  102. error:
  103. free(buf);
  104. return (NULL);
  105. }
  106. char *
  107. osdep_get_cwd(int fd)
  108. {
  109. return (NULL);
  110. }
  111. struct event_base *
  112. osdep_event_init(void)
  113. {
  114. return (event_init());
  115. }