FreeBSDProcessTable.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. htop - FreeBSDProcessTable.c
  3. (C) 2014 Hisham H. Muhammad
  4. Released under the GNU GPLv2+, see the COPYING file
  5. in the source distribution for its full text.
  6. */
  7. #include "config.h" // IWYU pragma: keep
  8. #include "freebsd/FreeBSDProcessTable.h"
  9. #include <assert.h>
  10. #include <limits.h>
  11. #include <math.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <sys/_iovec.h>
  15. #include <sys/errno.h>
  16. #include <sys/param.h> // needs to be included before <sys/jail.h> for MAXPATHLEN
  17. #include <sys/jail.h>
  18. #include <sys/priority.h>
  19. #include <sys/proc.h>
  20. #include <sys/resource.h>
  21. #include <sys/sysctl.h>
  22. #include <sys/time.h>
  23. #include <sys/types.h>
  24. #include <sys/user.h>
  25. #include <sys/vmmeter.h>
  26. #include "CRT.h"
  27. #include "Compat.h"
  28. #include "Macros.h"
  29. #include "Object.h"
  30. #include "Process.h"
  31. #include "ProcessTable.h"
  32. #include "Scheduling.h"
  33. #include "Settings.h"
  34. #include "XUtils.h"
  35. #include "freebsd/FreeBSDMachine.h"
  36. #include "freebsd/FreeBSDProcess.h"
  37. ProcessTable* ProcessTable_new(Machine* host, Hashtable* pidMatchList) {
  38. FreeBSDProcessTable* this = xCalloc(1, sizeof(FreeBSDProcessTable));
  39. Object_setClass(this, Class(ProcessTable));
  40. ProcessTable* super = &this->super;
  41. ProcessTable_init(super, Class(FreeBSDProcess), host, pidMatchList);
  42. return super;
  43. }
  44. void ProcessTable_delete(Object* cast) {
  45. FreeBSDProcessTable* this = (FreeBSDProcessTable*) cast;
  46. ProcessTable_done(&this->super);
  47. free(this);
  48. }
  49. static void FreeBSDProcessTable_updateExe(const struct kinfo_proc* kproc, Process* proc) {
  50. if (Process_isKernelThread(proc)) {
  51. Process_updateExe(proc, NULL);
  52. return;
  53. }
  54. const int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, kproc->ki_pid };
  55. char buffer[2048];
  56. size_t size = sizeof(buffer);
  57. if (sysctl(mib, 4, buffer, &size, NULL, 0) != 0) {
  58. Process_updateExe(proc, NULL);
  59. return;
  60. }
  61. Process_updateExe(proc, buffer);
  62. }
  63. static void FreeBSDProcessTable_updateCwd(const struct kinfo_proc* kproc, Process* proc) {
  64. #ifdef KERN_PROC_CWD
  65. const int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_CWD, kproc->ki_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. #else
  81. proc->procCwd = NULL;
  82. #endif
  83. }
  84. static void FreeBSDProcessTable_updateProcessName(kvm_t* kd, const struct kinfo_proc* kproc, Process* proc) {
  85. Process_updateComm(proc, kproc->ki_comm);
  86. char** argv = kvm_getargv(kd, kproc, 0);
  87. if (!argv || !argv[0]) {
  88. Process_updateCmdline(proc, kproc->ki_comm, 0, strlen(kproc->ki_comm));
  89. return;
  90. }
  91. size_t len = 0;
  92. for (int i = 0; argv[i]; i++) {
  93. len += strlen(argv[i]) + 1;
  94. }
  95. char* cmdline = xMalloc(len);
  96. char* at = cmdline;
  97. int end = 0;
  98. for (int i = 0; argv[i]; i++) {
  99. at = stpcpy(at, argv[i]);
  100. if (end == 0) {
  101. end = at - cmdline;
  102. }
  103. *at++ = ' ';
  104. }
  105. at--;
  106. *at = '\0';
  107. Process_updateCmdline(proc, cmdline, 0, end);
  108. free(cmdline);
  109. }
  110. static char* FreeBSDProcessTable_readJailName(const struct kinfo_proc* kproc) {
  111. if (kproc->ki_jid == 0)
  112. return xStrdup("-");
  113. char jnamebuf[MAXHOSTNAMELEN] = {0};
  114. struct iovec jiov[4];
  115. IGNORE_WCASTQUAL_BEGIN
  116. *(const void**)&jiov[0].iov_base = "jid";
  117. jiov[0].iov_len = sizeof("jid");
  118. jiov[1].iov_base = (void*) &kproc->ki_jid;
  119. jiov[1].iov_len = sizeof(kproc->ki_jid);
  120. *(const void**)&jiov[2].iov_base = "name";
  121. jiov[2].iov_len = sizeof("name");
  122. jiov[3].iov_base = jnamebuf;
  123. jiov[3].iov_len = sizeof(jnamebuf);
  124. IGNORE_WCASTQUAL_END
  125. int jid = jail_get(jiov, 4, 0);
  126. if (jid == kproc->ki_jid)
  127. return xStrdup(jnamebuf);
  128. return NULL;
  129. }
  130. void ProcessTable_goThroughEntries(ProcessTable* super) {
  131. const Machine* host = super->super.host;
  132. const FreeBSDMachine* fhost = (const FreeBSDMachine*) host;
  133. const Settings* settings = host->settings;
  134. bool hideKernelThreads = settings->hideKernelThreads;
  135. bool hideUserlandThreads = settings->hideUserlandThreads;
  136. int count = 0;
  137. const struct kinfo_proc* kprocs = kvm_getprocs(fhost->kd, KERN_PROC_PROC, 0, &count);
  138. for (int i = 0; i < count; i++) {
  139. const struct kinfo_proc* kproc = &kprocs[i];
  140. bool preExisting = false;
  141. Process* proc = ProcessTable_getProcess(super, kproc->ki_pid, &preExisting, FreeBSDProcess_new);
  142. FreeBSDProcess* fp = (FreeBSDProcess*) proc;
  143. if (!preExisting) {
  144. fp->jid = kproc->ki_jid;
  145. Process_setPid(proc, kproc->ki_pid);
  146. Process_setThreadGroup(proc, kproc->ki_pid);
  147. Process_setParent(proc, kproc->ki_ppid);
  148. proc->isKernelThread = kproc->ki_pid != 1 && (kproc->ki_flag & P_SYSTEM);
  149. proc->isUserlandThread = false;
  150. proc->tpgid = kproc->ki_tpgid;
  151. proc->session = kproc->ki_sid;
  152. proc->pgrp = kproc->ki_pgid;
  153. proc->st_uid = kproc->ki_uid;
  154. proc->starttime_ctime = kproc->ki_start.tv_sec;
  155. if (proc->starttime_ctime < 0) {
  156. proc->starttime_ctime = host->realtimeMs / 1000;
  157. }
  158. Process_fillStarttimeBuffer(proc);
  159. proc->user = UsersTable_getRef(host->usersTable, proc->st_uid);
  160. ProcessTable_add(super, proc);
  161. FreeBSDProcessTable_updateExe(kproc, proc);
  162. FreeBSDProcessTable_updateProcessName(fhost->kd, kproc, proc);
  163. if (settings->ss->flags & PROCESS_FLAG_CWD) {
  164. FreeBSDProcessTable_updateCwd(kproc, proc);
  165. }
  166. fp->jname = FreeBSDProcessTable_readJailName(kproc);
  167. proc->tty_nr = kproc->ki_tdev;
  168. const char* name = (kproc->ki_tdev != NODEV) ? devname(kproc->ki_tdev, S_IFCHR) : NULL;
  169. if (!name) {
  170. free(proc->tty_name);
  171. proc->tty_name = NULL;
  172. } else {
  173. free_and_xStrdup(&proc->tty_name, name);
  174. }
  175. } else {
  176. if (fp->jid != kproc->ki_jid) {
  177. // process can enter jail anytime
  178. fp->jid = kproc->ki_jid;
  179. free(fp->jname);
  180. fp->jname = FreeBSDProcessTable_readJailName(kproc);
  181. }
  182. // if there are reapers in the system, process can get reparented anytime
  183. Process_setParent(proc, kproc->ki_ppid);
  184. if (proc->st_uid != kproc->ki_uid) {
  185. // some processes change users (eg. to lower privs)
  186. proc->st_uid = kproc->ki_uid;
  187. proc->user = UsersTable_getRef(host->usersTable, proc->st_uid);
  188. }
  189. if (settings->updateProcessNames) {
  190. FreeBSDProcessTable_updateProcessName(fhost->kd, kproc, proc);
  191. }
  192. }
  193. free_and_xStrdup(&fp->emul, kproc->ki_emul);
  194. // from FreeBSD source /src/usr.bin/top/machine.c
  195. proc->m_virt = kproc->ki_size / ONE_K;
  196. proc->m_resident = kproc->ki_rssize * fhost->pageSizeKb;
  197. proc->nlwp = kproc->ki_numthreads;
  198. proc->time = (kproc->ki_runtime + 5000) / 10000;
  199. proc->percent_cpu = 100.0 * ((double)kproc->ki_pctcpu / (double)fhost->kernelFScale);
  200. proc->percent_mem = 100.0 * proc->m_resident / (double)(host->totalMem);
  201. Process_updateCPUFieldWidths(proc->percent_cpu);
  202. if (kproc->ki_stat == SRUN && kproc->ki_oncpu != NOCPU) {
  203. proc->processor = kproc->ki_oncpu;
  204. } else {
  205. proc->processor = kproc->ki_lastcpu;
  206. }
  207. proc->majflt = kproc->ki_cow;
  208. proc->priority = kproc->ki_pri.pri_level - PZERO;
  209. switch (PRI_BASE(kproc->ki_pri.pri_class)) {
  210. /* Handling of the below is explained in the FreeBSD base system in:
  211. * /usr/src/usr.bin/top/machine.c (function format_nice) */
  212. case PRI_ITHD:
  213. fp->sched_class = SCHEDCLASS_INTR_THREAD;
  214. proc->nice = 0;
  215. break;
  216. case PRI_REALTIME:
  217. fp->sched_class = SCHEDCLASS_REALTIME;
  218. /* Different for KPROCs and user procs */
  219. if (kproc->ki_flag & P_KPROC) {
  220. proc->nice = kproc->ki_pri.pri_native - PRI_MIN_REALTIME;
  221. } else {
  222. proc->nice = kproc->ki_pri.pri_user - PRI_MIN_REALTIME;
  223. }
  224. break;
  225. case PRI_IDLE:
  226. fp->sched_class = SCHEDCLASS_IDLE;
  227. /* Different for KPROCs and user procs */
  228. if (kproc->ki_flag & P_KPROC) {
  229. proc->nice = kproc->ki_pri.pri_native - PRI_MIN_IDLE;
  230. } else {
  231. proc->nice = kproc->ki_pri.pri_user - PRI_MIN_IDLE;
  232. }
  233. break;
  234. case PRI_TIMESHARE:
  235. fp->sched_class = SCHEDCLASS_TIMESHARE;
  236. proc->nice = kproc->ki_nice - NZERO;
  237. break;
  238. default:
  239. fp->sched_class = SCHEDCLASS_UNKNOWN;
  240. proc->nice = PROCESS_NICE_UNKNOWN;
  241. break;
  242. }
  243. /* Taken from: https://github.com/freebsd/freebsd-src/blob/1ad2d87778970582854082bcedd2df0394fd4933/sys/sys/proc.h#L851 */
  244. switch (kproc->ki_stat) {
  245. case SIDL: proc->state = IDLE; break;
  246. case SRUN: proc->state = RUNNING; break;
  247. case SSLEEP: proc->state = SLEEPING; break;
  248. case SSTOP: proc->state = STOPPED; break;
  249. case SZOMB: proc->state = ZOMBIE; break;
  250. case SWAIT: proc->state = WAITING; break;
  251. case SLOCK: proc->state = BLOCKED; break;
  252. default: proc->state = UNKNOWN;
  253. }
  254. if (Process_isKernelThread(proc))
  255. super->kernelThreads++;
  256. #ifdef SCHEDULER_SUPPORT
  257. if (settings->ss->flags & PROCESS_FLAG_SCHEDPOL)
  258. Scheduling_readProcessPolicy(proc);
  259. #endif
  260. proc->super.show = ! ((hideKernelThreads && Process_isKernelThread(proc)) || (hideUserlandThreads && Process_isUserlandThread(proc)));
  261. super->totalTasks++;
  262. if (proc->state == RUNNING)
  263. super->runningTasks++;
  264. proc->super.updated = true;
  265. }
  266. }