OpenBSDProcessTable.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. htop - OpenBSDProcessTable.c
  3. (C) 2014 Hisham H. Muhammad
  4. (C) 2015 Michael McConville
  5. Released under the GNU GPLv2+, see the COPYING file
  6. in the source distribution for its full text.
  7. */
  8. #include "config.h" // IWYU pragma: keep
  9. #include "openbsd/OpenBSDProcessTable.h"
  10. #include <kvm.h>
  11. #include <limits.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <unistd.h>
  15. #include <sys/mount.h>
  16. #include <sys/param.h>
  17. #include <sys/proc.h>
  18. #include <sys/sched.h>
  19. #include <sys/sysctl.h>
  20. #include <sys/types.h>
  21. #include <uvm/uvmexp.h>
  22. #include "CRT.h"
  23. #include "Macros.h"
  24. #include "Object.h"
  25. #include "Process.h"
  26. #include "ProcessTable.h"
  27. #include "Settings.h"
  28. #include "XUtils.h"
  29. #include "openbsd/OpenBSDMachine.h"
  30. #include "openbsd/OpenBSDProcess.h"
  31. ProcessTable* ProcessTable_new(Machine* host, Hashtable* pidMatchList) {
  32. OpenBSDProcessTable* this = xCalloc(1, sizeof(OpenBSDProcessTable));
  33. Object_setClass(this, Class(ProcessTable));
  34. ProcessTable* super = &this->super;
  35. ProcessTable_init(super, Class(OpenBSDProcess), host, pidMatchList);
  36. return super;
  37. }
  38. void ProcessTable_delete(Object* cast) {
  39. OpenBSDProcessTable* this = (OpenBSDProcessTable*) cast;
  40. ProcessTable_done(&this->super);
  41. free(this);
  42. }
  43. static void OpenBSDProcessTable_updateCwd(const struct kinfo_proc* kproc, Process* proc) {
  44. const int mib[] = { CTL_KERN, KERN_PROC_CWD, kproc->p_pid };
  45. char buffer[2048];
  46. size_t size = sizeof(buffer);
  47. if (sysctl(mib, 3, buffer, &size, NULL, 0) != 0) {
  48. free(proc->procCwd);
  49. proc->procCwd = NULL;
  50. return;
  51. }
  52. /* Kernel threads return an empty buffer */
  53. if (buffer[0] == '\0') {
  54. free(proc->procCwd);
  55. proc->procCwd = NULL;
  56. return;
  57. }
  58. free_and_xStrdup(&proc->procCwd, buffer);
  59. }
  60. static void OpenBSDProcessTable_updateProcessName(kvm_t* kd, const struct kinfo_proc* kproc, Process* proc) {
  61. Process_updateComm(proc, kproc->p_comm);
  62. /*
  63. * Like OpenBSD's top(1), we try to fall back to the command name
  64. * (argv[0]) if we fail to construct the full command.
  65. */
  66. char** arg = kvm_getargv(kd, kproc, 500);
  67. if (arg == NULL || *arg == NULL) {
  68. Process_updateCmdline(proc, kproc->p_comm, 0, strlen(kproc->p_comm));
  69. return;
  70. }
  71. size_t len = 0;
  72. for (int i = 0; arg[i] != NULL; i++) {
  73. len += strlen(arg[i]) + 1; /* room for arg and trailing space or NUL */
  74. }
  75. /* don't use xMalloc here - we want to handle huge argv's gracefully */
  76. char* s;
  77. if ((s = malloc(len)) == NULL) {
  78. Process_updateCmdline(proc, kproc->p_comm, 0, strlen(kproc->p_comm));
  79. return;
  80. }
  81. *s = '\0';
  82. int start = 0;
  83. int end = 0;
  84. for (int i = 0; arg[i] != NULL; i++) {
  85. size_t n = strlcat(s, arg[i], len);
  86. if (i == 0) {
  87. end = MINIMUM(n, len - 1);
  88. /* check if cmdline ended earlier, e.g 'kdeinit5: Running...' */
  89. for (int j = end; j > 0; j--) {
  90. if (arg[0][j] == ' ' && arg[0][j - 1] != '\\') {
  91. end = (arg[0][j - 1] == ':') ? (j - 1) : j;
  92. }
  93. }
  94. }
  95. /* the trailing space should get truncated anyway */
  96. strlcat(s, " ", len);
  97. }
  98. Process_updateCmdline(proc, s, start, end);
  99. free(s);
  100. }
  101. /*
  102. * Taken from OpenBSD's ps(1).
  103. */
  104. static double getpcpu(const OpenBSDMachine* ohost, const struct kinfo_proc* kp) {
  105. if (ohost->fscale == 0)
  106. return 0.0;
  107. return 100.0 * (double)kp->p_pctcpu / ohost->fscale;
  108. }
  109. static void OpenBSDProcessTable_scanProcs(OpenBSDProcessTable* this) {
  110. Machine* host = this->super.super.host;
  111. OpenBSDMachine* ohost = (OpenBSDMachine*) host;
  112. const Settings* settings = host->settings;
  113. const bool hideKernelThreads = settings->hideKernelThreads;
  114. const bool hideUserlandThreads = settings->hideUserlandThreads;
  115. int count = 0;
  116. const struct kinfo_proc* kprocs = kvm_getprocs(ohost->kd, KERN_PROC_KTHREAD | KERN_PROC_SHOW_THREADS, 0, sizeof(struct kinfo_proc), &count);
  117. for (int i = 0; i < count; i++) {
  118. const struct kinfo_proc* kproc = &kprocs[i];
  119. /* Ignore main threads */
  120. if (kproc->p_tid != -1) {
  121. Process* containingProcess = ProcessTable_findProcess(&this->super, kproc->p_pid);
  122. if (containingProcess) {
  123. if (((OpenBSDProcess*)containingProcess)->addr == kproc->p_addr)
  124. continue;
  125. containingProcess->nlwp++;
  126. }
  127. }
  128. bool preExisting = false;
  129. Process* proc = ProcessTable_getProcess(&this->super, (kproc->p_tid == -1) ? kproc->p_pid : kproc->p_tid, &preExisting, OpenBSDProcess_new);
  130. OpenBSDProcess* op = (OpenBSDProcess*) proc;
  131. if (!preExisting) {
  132. Process_setParent(proc, kproc->p_ppid);
  133. Process_setThreadGroup(proc, kproc->p_pid);
  134. proc->tpgid = kproc->p_tpgid;
  135. proc->session = kproc->p_sid;
  136. proc->pgrp = kproc->p__pgid;
  137. proc->isKernelThread = proc->pgrp == 0;
  138. proc->isUserlandThread = kproc->p_tid != -1;
  139. proc->starttime_ctime = kproc->p_ustart_sec;
  140. Process_fillStarttimeBuffer(proc);
  141. ProcessTable_add(&this->super, proc);
  142. OpenBSDProcessTable_updateProcessName(ohost->kd, kproc, proc);
  143. if (settings->ss->flags & PROCESS_FLAG_CWD) {
  144. OpenBSDProcessTable_updateCwd(kproc, proc);
  145. }
  146. proc->tty_nr = kproc->p_tdev;
  147. const char* name = ((dev_t)kproc->p_tdev != NODEV) ? devname(kproc->p_tdev, S_IFCHR) : NULL;
  148. if (!name || String_eq(name, "??")) {
  149. free(proc->tty_name);
  150. proc->tty_name = NULL;
  151. } else {
  152. free_and_xStrdup(&proc->tty_name, name);
  153. }
  154. } else {
  155. if (settings->updateProcessNames) {
  156. OpenBSDProcessTable_updateProcessName(ohost->kd, kproc, proc);
  157. }
  158. }
  159. op->addr = kproc->p_addr;
  160. proc->m_virt = kproc->p_vm_dsize * ohost->pageSizeKB;
  161. proc->m_resident = kproc->p_vm_rssize * ohost->pageSizeKB;
  162. proc->percent_mem = proc->m_resident / (float)host->totalMem * 100.0F;
  163. proc->percent_cpu = CLAMP(getpcpu(ohost, kproc), 0.0F, host->activeCPUs * 100.0F);
  164. Process_updateCPUFieldWidths(proc->percent_cpu);
  165. proc->nice = kproc->p_nice - 20;
  166. proc->time = 100 * (kproc->p_rtime_sec + ((kproc->p_rtime_usec + 500000) / 1000000));
  167. proc->priority = kproc->p_priority - PZERO;
  168. proc->processor = kproc->p_cpuid;
  169. proc->minflt = kproc->p_uru_minflt;
  170. proc->majflt = kproc->p_uru_majflt;
  171. proc->nlwp = 1;
  172. if (proc->st_uid != kproc->p_uid) {
  173. proc->st_uid = kproc->p_uid;
  174. proc->user = UsersTable_getRef(host->usersTable, proc->st_uid);
  175. }
  176. /* Taken from: https://github.com/openbsd/src/blob/6a38af0976a6870911f4b2de19d8da28723a5778/sys/sys/proc.h#L420 */
  177. switch (kproc->p_stat) {
  178. case SIDL: proc->state = IDLE; break;
  179. case SRUN: proc->state = RUNNABLE; break;
  180. case SSLEEP: proc->state = SLEEPING; break;
  181. case SSTOP: proc->state = STOPPED; break;
  182. case SZOMB: proc->state = ZOMBIE; break;
  183. case SDEAD: proc->state = DEFUNCT; break;
  184. case SONPROC: proc->state = RUNNING; break;
  185. default: proc->state = UNKNOWN;
  186. }
  187. if (Process_isKernelThread(proc)) {
  188. this->super.kernelThreads++;
  189. } else if (Process_isUserlandThread(proc)) {
  190. this->super.userlandThreads++;
  191. }
  192. this->super.totalTasks++;
  193. if (proc->state == RUNNING) {
  194. this->super.runningTasks++;
  195. }
  196. proc->super.show = ! ((hideKernelThreads && Process_isKernelThread(proc)) || (hideUserlandThreads && Process_isUserlandThread(proc)));
  197. proc->super.updated = true;
  198. }
  199. }
  200. void ProcessTable_goThroughEntries(ProcessTable* super) {
  201. OpenBSDProcessTable* this = (OpenBSDProcessTable*) super;
  202. OpenBSDProcessTable_scanProcs(this);
  203. }