osdep-dragonfly.c 3.0 KB

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