DragonFlyBSDProcessTable.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. htop - DragonFlyBSDProcessTable.c
  3. (C) 2014 Hisham H. Muhammad
  4. (C) 2017 Diederik de Groot
  5. Released under the GNU GPLv2+, see the COPYING file
  6. in the source distribution for its full text.
  7. */
  8. #include "dragonflybsd/DragonFlyBSDProcessTable.h"
  9. #include <fcntl.h>
  10. #include <limits.h>
  11. #include <stddef.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <unistd.h>
  15. #include <sys/types.h>
  16. #include <sys/sysctl.h>
  17. #include <sys/user.h>
  18. #include <sys/param.h>
  19. #include "CRT.h"
  20. #include "Macros.h"
  21. #include "dragonflybsd/DragonFlyBSDMachine.h"
  22. #include "dragonflybsd/DragonFlyBSDProcess.h"
  23. ProcessTable* ProcessTable_new(Machine* host, Hashtable* pidMatchList) {
  24. DragonFlyBSDProcessTable* this = xCalloc(1, sizeof(DragonFlyBSDProcessTable));
  25. Object_setClass(this, Class(ProcessTable));
  26. ProcessTable* super = (ProcessTable*) this;
  27. ProcessTable_init(super, Class(DragonFlyBSDProcess), host, pidMatchList);
  28. return super;
  29. }
  30. void ProcessTable_delete(Object* cast) {
  31. DragonFlyBSDProcessTable* this = (DragonFlyBSDProcessTable*) cast;
  32. ProcessTable_done(&this->super);
  33. free(this);
  34. }
  35. //static void DragonFlyBSDProcessTable_updateExe(const struct kinfo_proc* kproc, Process* proc) {
  36. // const int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, kproc->kp_pid };
  37. // char buffer[2048];
  38. // size_t size = sizeof(buffer);
  39. // if (sysctl(mib, 4, buffer, &size, NULL, 0) != 0) {
  40. // Process_updateExe(proc, NULL);
  41. // return;
  42. // }
  43. //
  44. // /* Kernel threads return an empty buffer */
  45. // if (buffer[0] == '\0') {
  46. // Process_updateExe(proc, NULL);
  47. // return;
  48. // }
  49. //
  50. // Process_updateExe(proc, buffer);
  51. //}
  52. static void DragonFlyBSDProcessTable_updateExe(const struct kinfo_proc* kproc, Process* proc) {
  53. if (Process_isKernelThread(proc))
  54. return;
  55. char path[32];
  56. xSnprintf(path, sizeof(path), "/proc/%d/file", kproc->kp_pid);
  57. char target[PATH_MAX];
  58. ssize_t ret = readlink(path, target, sizeof(target) - 1);
  59. if (ret <= 0)
  60. return;
  61. target[ret] = '\0';
  62. Process_updateExe(proc, target);
  63. }
  64. static void DragonFlyBSDProcessTable_updateCwd(const struct kinfo_proc* kproc, Process* proc) {
  65. const int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_CWD, kproc->kp_pid };
  66. char buffer[2048];
  67. size_t size = sizeof(buffer);
  68. if (sysctl(mib, 4, buffer, &size, NULL, 0) != 0) {
  69. free(proc->procCwd);
  70. proc->procCwd = NULL;
  71. return;
  72. }
  73. /* Kernel threads return an empty buffer */
  74. if (buffer[0] == '\0') {
  75. free(proc->procCwd);
  76. proc->procCwd = NULL;
  77. return;
  78. }
  79. free_and_xStrdup(&proc->procCwd, buffer);
  80. }
  81. static void DragonFlyBSDProcessTable_updateProcessName(kvm_t* kd, const struct kinfo_proc* kproc, Process* proc) {
  82. Process_updateComm(proc, kproc->kp_comm);
  83. char** argv = kvm_getargv(kd, kproc, 0);
  84. if (!argv || !argv[0]) {
  85. Process_updateCmdline(proc, kproc->kp_comm, 0, strlen(kproc->kp_comm));
  86. return;
  87. }
  88. size_t len = 0;
  89. for (int i = 0; argv[i]; i++) {
  90. len += strlen(argv[i]) + 1;
  91. }
  92. char* cmdline = xMalloc(len);
  93. char* at = cmdline;
  94. int end = 0;
  95. for (int i = 0; argv[i]; i++) {
  96. at = stpcpy(at, argv[i]);
  97. if (end == 0) {
  98. end = at - cmdline;
  99. }
  100. *at++ = ' ';
  101. }
  102. at--;
  103. *at = '\0';
  104. Process_updateCmdline(proc, cmdline, 0, end);
  105. free(cmdline);
  106. }
  107. void ProcessTable_goThroughEntries(ProcessTable* super) {
  108. const Machine* host = super->super.host;
  109. const DragonFlyBSDMachine* dhost = (const DragonFlyBSDMachine*) host;
  110. const Settings* settings = host->settings;
  111. bool hideKernelThreads = settings->hideKernelThreads;
  112. bool hideUserlandThreads = settings->hideUserlandThreads;
  113. int count = 0;
  114. const struct kinfo_proc* kprocs = kvm_getprocs(dhost->kd, KERN_PROC_ALL | (!hideUserlandThreads ? KERN_PROC_FLAG_LWP : 0), 0, &count);
  115. for (int i = 0; i < count; i++) {
  116. const struct kinfo_proc* kproc = &kprocs[i];
  117. bool preExisting = false;
  118. bool ATTR_UNUSED isIdleProcess = false;
  119. // note: dragonflybsd kernel processes all have the same pid, so we misuse the kernel thread address to give them a unique identifier
  120. Process* proc = ProcessTable_getProcess(super, kproc->kp_ktaddr ? (pid_t)kproc->kp_ktaddr : kproc->kp_pid, &preExisting, DragonFlyBSDProcess_new);
  121. DragonFlyBSDProcess* dfp = (DragonFlyBSDProcess*) proc;
  122. if (!preExisting) {
  123. dfp->jid = kproc->kp_jailid;
  124. if (kproc->kp_ktaddr && kproc->kp_flags & P_SYSTEM) {
  125. // dfb kernel threads all have the same pid, so we misuse the kernel thread address to give them a unique identifier
  126. Process_setPid(proc, (pid_t)kproc->kp_ktaddr);
  127. proc->isKernelThread = true;
  128. } else {
  129. Process_setPid(proc, kproc->kp_pid); // process ID
  130. proc->isKernelThread = false;
  131. }
  132. proc->isUserlandThread = kproc->kp_nthreads > 1;
  133. Process_setParent(proc, kproc->kp_ppid); // parent process id
  134. proc->tpgid = kproc->kp_tpgid; // tty process group id
  135. //Process_setThreadGroup(proc, kproc->kp_lwp.kl_tid); // thread group id
  136. Process_setThreadGroup(proc, kproc->kp_pid);
  137. proc->pgrp = kproc->kp_pgid; // process group id
  138. proc->session = kproc->kp_sid;
  139. proc->st_uid = kproc->kp_uid; // user ID
  140. proc->processor = kproc->kp_lwp.kl_origcpu;
  141. proc->starttime_ctime = kproc->kp_start.tv_sec;
  142. Process_fillStarttimeBuffer(proc);
  143. proc->user = UsersTable_getRef(host->usersTable, proc->st_uid);
  144. proc->tty_nr = kproc->kp_tdev; // control terminal device number
  145. const char* name = (kproc->kp_tdev != NODEV) ? devname(kproc->kp_tdev, S_IFCHR) : NULL;
  146. if (!name) {
  147. free(proc->tty_name);
  148. proc->tty_name = NULL;
  149. } else {
  150. free_and_xStrdup(&proc->tty_name, name);
  151. }
  152. DragonFlyBSDProcessTable_updateExe(kproc, proc);
  153. DragonFlyBSDProcessTable_updateProcessName(dhost->kd, kproc, proc);
  154. if (settings->ss->flags & PROCESS_FLAG_CWD) {
  155. DragonFlyBSDProcessTable_updateCwd(kproc, proc);
  156. }
  157. ProcessTable_add(super, proc);
  158. dfp->jname = DragonFlyBSDMachine_readJailName(dhost, kproc->kp_jailid);
  159. } else {
  160. proc->processor = kproc->kp_lwp.kl_cpuid;
  161. if (dfp->jid != kproc->kp_jailid) { // process can enter jail anytime
  162. dfp->jid = kproc->kp_jailid;
  163. free(dfp->jname);
  164. dfp->jname = DragonFlyBSDMachine_readJailName(dhost, kproc->kp_jailid);
  165. }
  166. // if there are reapers in the system, process can get reparented anytime
  167. Process_setParent(proc, kproc->kp_ppid);
  168. if (proc->st_uid != kproc->kp_uid) { // some processes change users (eg. to lower privs)
  169. proc->st_uid = kproc->kp_uid;
  170. proc->user = UsersTable_getRef(host->usersTable, proc->st_uid);
  171. }
  172. if (settings->updateProcessNames) {
  173. DragonFlyBSDProcessTable_updateProcessName(dhost->kd, kproc, proc);
  174. }
  175. }
  176. proc->m_virt = kproc->kp_vm_map_size / ONE_K;
  177. proc->m_resident = kproc->kp_vm_rssize * dhost->pageSizeKb;
  178. proc->nlwp = kproc->kp_nthreads; // number of lwp thread
  179. proc->time = (kproc->kp_lwp.kl_uticks + kproc->kp_lwp.kl_sticks + kproc->kp_lwp.kl_iticks) / 10000;
  180. proc->percent_cpu = 100.0 * ((double)kproc->kp_lwp.kl_pctcpu / (double)dhost->kernelFScale);
  181. proc->percent_mem = 100.0 * proc->m_resident / (double)(super->super.host->totalMem);
  182. Process_updateCPUFieldWidths(proc->percent_cpu);
  183. if (proc->percent_cpu > 0.1) {
  184. // system idle process should own all CPU time left regardless of CPU count
  185. if (String_eq("idle", kproc->kp_comm)) {
  186. isIdleProcess = true;
  187. }
  188. }
  189. if (kproc->kp_lwp.kl_pid != -1)
  190. proc->priority = kproc->kp_lwp.kl_prio;
  191. else
  192. proc->priority = -kproc->kp_lwp.kl_tdprio;
  193. switch (kproc->kp_lwp.kl_rtprio.type) {
  194. case RTP_PRIO_REALTIME:
  195. proc->nice = PRIO_MIN - 1 - RTP_PRIO_MAX + kproc->kp_lwp.kl_rtprio.prio;
  196. break;
  197. case RTP_PRIO_IDLE:
  198. proc->nice = PRIO_MAX + 1 + kproc->kp_lwp.kl_rtprio.prio;
  199. break;
  200. case RTP_PRIO_THREAD:
  201. proc->nice = PRIO_MIN - 1 - RTP_PRIO_MAX - kproc->kp_lwp.kl_rtprio.prio;
  202. break;
  203. default:
  204. proc->nice = kproc->kp_nice;
  205. break;
  206. }
  207. // would be nice if we could store multiple states in proc->state (as enum) and have writeField render them
  208. /* Taken from: https://github.com/DragonFlyBSD/DragonFlyBSD/blob/c163a4d7ee9c6857ee4e04a3a2cbb50c3de29da1/sys/sys/proc_common.h */
  209. switch (kproc->kp_stat) {
  210. case SIDL:
  211. proc->state = IDLE;
  212. isIdleProcess = true;
  213. break;
  214. case SACTIVE:
  215. switch (kproc->kp_lwp.kl_stat) {
  216. case LSSLEEP:
  217. if (kproc->kp_lwp.kl_flags & LWP_SINTR) { // interruptible wait short/long
  218. if (kproc->kp_lwp.kl_slptime >= MAXSLP) {
  219. proc->state = IDLE;
  220. isIdleProcess = true;
  221. } else {
  222. proc->state = SLEEPING;
  223. }
  224. } else if (kproc->kp_lwp.kl_tdflags & TDF_SINTR) { // interruptible lwkt wait
  225. proc->state = SLEEPING;
  226. } else if (kproc->kp_paddr) { // uninterruptible wait
  227. proc->state = UNINTERRUPTIBLE_WAIT;
  228. } else { // uninterruptible lwkt wait
  229. proc->state = UNINTERRUPTIBLE_WAIT;
  230. }
  231. break;
  232. case LSRUN:
  233. if (kproc->kp_lwp.kl_stat == LSRUN) {
  234. if (!(kproc->kp_lwp.kl_tdflags & (TDF_RUNNING | TDF_RUNQ))) {
  235. proc->state = QUEUED;
  236. } else {
  237. proc->state = RUNNING;
  238. }
  239. }
  240. break;
  241. case LSSTOP:
  242. proc->state = STOPPED;
  243. break;
  244. default:
  245. proc->state = PAGING;
  246. break;
  247. }
  248. break;
  249. case SSTOP:
  250. proc->state = STOPPED;
  251. break;
  252. case SZOMB:
  253. proc->state = ZOMBIE;
  254. break;
  255. case SCORE:
  256. proc->state = BLOCKED;
  257. break;
  258. default:
  259. proc->state = UNKNOWN;
  260. }
  261. if (kproc->kp_flags & P_SWAPPEDOUT)
  262. proc->state = SLEEPING;
  263. if (kproc->kp_flags & P_TRACED)
  264. proc->state = TRACED;
  265. if (kproc->kp_flags & P_JAILED)
  266. proc->state = TRACED;
  267. if (Process_isKernelThread(proc))
  268. super->kernelThreads++;
  269. super->totalTasks++;
  270. if (proc->state == RUNNING)
  271. super->runningTasks++;
  272. proc->super.show = ! ((hideKernelThreads && Process_isKernelThread(proc)) || (hideUserlandThreads && Process_isUserlandThread(proc)));
  273. proc->super.updated = true;
  274. }
  275. }