LinuxProcessTable.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924
  1. /*
  2. htop - LinuxProcessTable.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 "linux/LinuxProcessTable.h"
  9. #include <assert.h>
  10. #include <ctype.h>
  11. #include <dirent.h>
  12. #include <fcntl.h>
  13. #include <inttypes.h>
  14. #include <limits.h>
  15. #include <math.h>
  16. #include <stdbool.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <syscall.h>
  21. #include <unistd.h>
  22. #include <linux/capability.h> // raw syscall, no libcap // IWYU pragma: keep // IWYU pragma: no_include <sys/capability.h>
  23. #include <sys/stat.h>
  24. #include "Compat.h"
  25. #include "Hashtable.h"
  26. #include "Machine.h"
  27. #include "Macros.h"
  28. #include "Object.h"
  29. #include "Process.h"
  30. #include "Row.h"
  31. #include "RowField.h"
  32. #include "Scheduling.h"
  33. #include "Settings.h"
  34. #include "Table.h"
  35. #include "UsersTable.h"
  36. #include "XUtils.h"
  37. #include "linux/CGroupUtils.h"
  38. #include "linux/GPU.h"
  39. #include "linux/GPUMeter.h"
  40. #include "linux/LinuxMachine.h"
  41. #include "linux/LinuxProcess.h"
  42. #include "linux/Platform.h" // needed for GNU/hurd to get PATH_MAX // IWYU pragma: keep
  43. #ifdef HAVE_DELAYACCT
  44. #include "linux/LibNl.h"
  45. #endif
  46. #if defined(MAJOR_IN_MKDEV)
  47. #include <sys/mkdev.h>
  48. #elif defined(MAJOR_IN_SYSMACROS)
  49. #include <sys/sysmacros.h>
  50. #endif
  51. /* Not exposed yet. Defined at include/linux/sched.h */
  52. #ifndef PF_KTHREAD
  53. #define PF_KTHREAD 0x00200000
  54. #endif
  55. /* Inode number of the PID namespace of htop */
  56. static ino_t rootPidNs = (ino_t)-1;
  57. static FILE* fopenat(openat_arg_t openatArg, const char* pathname, const char* mode) {
  58. assert(String_eq(mode, "r")); /* only currently supported mode */
  59. int fd = Compat_openat(openatArg, pathname, O_RDONLY);
  60. if (fd < 0)
  61. return NULL;
  62. FILE* fp = fdopen(fd, mode);
  63. if (!fp)
  64. close(fd);
  65. return fp;
  66. }
  67. static inline uint64_t fast_strtoull_dec(char** str, int maxlen) {
  68. uint64_t result = 0;
  69. if (!maxlen)
  70. maxlen = 20; // length of maximum value of 18446744073709551615
  71. while (maxlen-- && **str >= '0' && **str <= '9') {
  72. result *= 10;
  73. result += **str - '0';
  74. (*str)++;
  75. }
  76. return result;
  77. }
  78. static long long fast_strtoll_dec(char** str, int maxlen) {
  79. bool neg = false;
  80. if (**str == '-') {
  81. neg = true;
  82. (*str)++;
  83. }
  84. unsigned long long res = fast_strtoull_dec(str, maxlen);
  85. assert(res <= LLONG_MAX);
  86. long long result = (long long)res;
  87. return neg ? -result : result;
  88. }
  89. static long fast_strtol_dec(char** str, int maxlen) {
  90. long long result = fast_strtoll_dec(str, maxlen);
  91. assert(result <= LONG_MAX);
  92. assert(result >= LONG_MIN);
  93. return (long)result;
  94. }
  95. static unsigned long fast_strtoul_dec(char** str, int maxlen) {
  96. unsigned long long result = fast_strtoull_dec(str, maxlen);
  97. assert(result <= ULONG_MAX);
  98. return (unsigned long)result;
  99. }
  100. static inline uint64_t fast_strtoull_hex(char** str, int maxlen) {
  101. register uint64_t result = 0;
  102. register int nibble, letter;
  103. const long valid_mask = 0x03FF007E;
  104. if (!maxlen)
  105. --maxlen;
  106. while (maxlen--) {
  107. nibble = (unsigned char)**str;
  108. if (!(valid_mask & (1 << (nibble & 0x1F))))
  109. break;
  110. if ((nibble < '0') || (nibble & ~0x20) > 'F')
  111. break;
  112. letter = (nibble & 0x40) ? 'A' - '9' - 1 : 0;
  113. nibble &=~0x20; // to upper
  114. nibble ^= 0x10; // switch letters and digits
  115. nibble -= letter;
  116. nibble &= 0x0f;
  117. result <<= 4;
  118. result += (uint64_t)nibble;
  119. (*str)++;
  120. }
  121. return result;
  122. }
  123. static int sortTtyDrivers(const void* va, const void* vb) {
  124. const TtyDriver* a = (const TtyDriver*) va;
  125. const TtyDriver* b = (const TtyDriver*) vb;
  126. int r = SPACESHIP_NUMBER(a->major, b->major);
  127. if (r)
  128. return r;
  129. return SPACESHIP_NUMBER(a->minorFrom, b->minorFrom);
  130. }
  131. static void LinuxProcessTable_initTtyDrivers(LinuxProcessTable* this) {
  132. TtyDriver* ttyDrivers;
  133. char buf[16384];
  134. ssize_t r = xReadfile(PROCTTYDRIVERSFILE, buf, sizeof(buf));
  135. if (r < 0)
  136. return;
  137. size_t numDrivers = 0;
  138. size_t allocd = 10;
  139. ttyDrivers = xMallocArray(allocd, sizeof(TtyDriver));
  140. char* at = buf;
  141. char* path = NULL;
  142. while (at && *at != '\0') {
  143. /*
  144. * Format:
  145. * [name] [node path] [major] [minor range] [type]
  146. * serial /dev/ttyS 4 64-95 serial
  147. */
  148. at = strchr(at, ' '); // skip first token
  149. if (!at)
  150. goto finish; // bail out on truncation
  151. while (*at == ' ') at++; // skip spaces
  152. const char* token = at; // mark beginning of path
  153. at = strchr(at, ' '); // find end of path
  154. if (!at)
  155. goto finish; // bail out on truncation
  156. *at = '\0'; at++; // clear and skip
  157. path = xStrdup(token); // save
  158. while (*at == ' ') at++; // skip spaces
  159. token = at; // mark beginning of major
  160. at = strchr(at, ' '); // find end of major
  161. if (!at)
  162. goto finish; // bail out on truncation
  163. *at = '\0'; at++; // clear and skip
  164. ttyDrivers[numDrivers].major = atoi(token); // save
  165. while (*at == ' ') at++; // skip spaces
  166. token = at; // mark beginning of minorFrom
  167. while (*at >= '0' && *at <= '9') at++; //find end of minorFrom
  168. if (*at == '-') { // if has range
  169. *at = '\0'; at++; // clear and skip
  170. ttyDrivers[numDrivers].minorFrom = atoi(token); // save
  171. token = at; // mark beginning of minorTo
  172. at = strchr(at, ' '); // find end of minorTo
  173. if (!at)
  174. goto finish; // bail out on truncation
  175. *at = '\0'; at++; // clear and skip
  176. ttyDrivers[numDrivers].minorTo = atoi(token); // save
  177. } else { // no range
  178. *at = '\0'; at++; // clear and skip
  179. ttyDrivers[numDrivers].minorFrom = atoi(token); // save
  180. ttyDrivers[numDrivers].minorTo = atoi(token); // save
  181. }
  182. at = strchr(at, '\n'); // go to end of line
  183. if (at)
  184. at++; // skip
  185. ttyDrivers[numDrivers].path = path;
  186. path = NULL;
  187. numDrivers++;
  188. if (numDrivers == allocd) {
  189. allocd += 10;
  190. ttyDrivers = xReallocArray(ttyDrivers, allocd, sizeof(TtyDriver));
  191. }
  192. }
  193. finish:
  194. free(path);
  195. ttyDrivers = xRealloc(ttyDrivers, sizeof(TtyDriver) * (numDrivers + 1));
  196. ttyDrivers[numDrivers].path = NULL;
  197. qsort(ttyDrivers, numDrivers, sizeof(TtyDriver), sortTtyDrivers);
  198. this->ttyDrivers = ttyDrivers;
  199. }
  200. ProcessTable* ProcessTable_new(Machine* host, Hashtable* pidMatchList) {
  201. LinuxProcessTable* this = xCalloc(1, sizeof(LinuxProcessTable));
  202. Object_setClass(this, Class(ProcessTable));
  203. ProcessTable* super = &this->super;
  204. ProcessTable_init(super, Class(LinuxProcess), host, pidMatchList);
  205. LinuxProcessTable_initTtyDrivers(this);
  206. // Test /proc/PID/smaps_rollup availability (faster to parse, Linux 4.14+)
  207. this->haveSmapsRollup = (access(PROCDIR "/self/smaps_rollup", R_OK) == 0);
  208. // Read PID namespace inode number
  209. {
  210. struct stat sb;
  211. int r = stat(PROCDIR "/self/ns/pid", &sb);
  212. if (r == 0) {
  213. rootPidNs = sb.st_ino;
  214. } else {
  215. rootPidNs = (ino_t)-1;
  216. }
  217. }
  218. return super;
  219. }
  220. void ProcessTable_delete(Object* cast) {
  221. LinuxProcessTable* this = (LinuxProcessTable*) cast;
  222. ProcessTable_done(&this->super);
  223. if (this->ttyDrivers) {
  224. for (int i = 0; this->ttyDrivers[i].path; i++) {
  225. free(this->ttyDrivers[i].path);
  226. }
  227. free(this->ttyDrivers);
  228. }
  229. #ifdef HAVE_DELAYACCT
  230. LibNl_destroyNetlinkSocket(this);
  231. #endif
  232. free(this);
  233. }
  234. static inline unsigned long long LinuxProcessTable_adjustTime(const LinuxMachine* lhost, unsigned long long t) {
  235. return t * 100 / lhost->jiffies;
  236. }
  237. /* Taken from: https://github.com/torvalds/linux/blob/64570fbc14f8d7cb3fe3995f20e26bc25ce4b2cc/fs/proc/array.c#L120 */
  238. static inline ProcessState LinuxProcessTable_getProcessState(char state) {
  239. switch (state) {
  240. case 'S': return SLEEPING;
  241. case 'X': return DEFUNCT;
  242. case 'Z': return ZOMBIE;
  243. case 't': return TRACED;
  244. case 'T': return STOPPED;
  245. case 'D': return UNINTERRUPTIBLE_WAIT;
  246. case 'R': return RUNNING;
  247. case 'P': return BLOCKED;
  248. case 'I': return IDLE;
  249. default: return UNKNOWN;
  250. }
  251. }
  252. /*
  253. * Read /proc/<pid>/stat (thread-specific data)
  254. */
  255. static bool LinuxProcessTable_readStatFile(LinuxProcess* lp, openat_arg_t procFd, const LinuxMachine* lhost, bool scanMainThread, char* command, size_t commLen) {
  256. Process* process = &lp->super;
  257. char buf[MAX_READ + 1];
  258. char path[22] = "stat";
  259. if (scanMainThread) {
  260. xSnprintf(path, sizeof(path), "task/%"PRIi32"/stat", (int32_t)Process_getPid(process));
  261. }
  262. ssize_t r = xReadfileat(procFd, path, buf, sizeof(buf));
  263. if (r < 0)
  264. return false;
  265. /* (1) pid - %d */
  266. assert(Process_getPid(process) == atoi(buf));
  267. char* location = strchr(buf, ' ');
  268. if (!location)
  269. return false;
  270. /* (2) comm - (%s) */
  271. if (!location[0] || !location[1])
  272. return false;
  273. location += 2;
  274. char* end = strrchr(location, ')');
  275. if (!end)
  276. return false;
  277. if (end < location)
  278. return false;
  279. String_safeStrncpy(command, location, MINIMUM((size_t)(end - location + 1), commLen));
  280. if (!end[0] || !end[1])
  281. return false;
  282. location = end + 2;
  283. /* (3) state - %c */
  284. process->state = LinuxProcessTable_getProcessState(location[0]);
  285. if (!location[0] || !location[1])
  286. return false;
  287. location += 2;
  288. /* (4) ppid - %d */
  289. Process_setParent(process, fast_strtol_dec(&location, 0));
  290. if (!location[0])
  291. return false;
  292. location += 1;
  293. /* (5) pgrp - %d */
  294. process->pgrp = fast_strtol_dec(&location, 0);
  295. if (!location[0])
  296. return false;
  297. location += 1;
  298. /* (6) session - %d */
  299. process->session = fast_strtol_dec(&location, 0);
  300. if (!location[0])
  301. return false;
  302. location += 1;
  303. /* (7) tty_nr - %d */
  304. process->tty_nr = fast_strtoul_dec(&location, 0);
  305. if (!location[0])
  306. return false;
  307. location += 1;
  308. /* (8) tpgid - %d */
  309. process->tpgid = fast_strtol_dec(&location, 0);
  310. if (!location[0])
  311. return false;
  312. location += 1;
  313. /* (9) flags - %u */
  314. lp->flags = fast_strtoul_dec(&location, 0);
  315. if (!location[0])
  316. return false;
  317. location += 1;
  318. /* (10) minflt - %lu */
  319. process->minflt = fast_strtoull_dec(&location, 0);
  320. if (!location[0])
  321. return false;
  322. location += 1;
  323. /* (11) cminflt - %lu */
  324. lp->cminflt = fast_strtoull_dec(&location, 0);
  325. if (!location[0])
  326. return false;
  327. location += 1;
  328. /* (12) majflt - %lu */
  329. process->majflt = fast_strtoull_dec(&location, 0);
  330. if (!location[0])
  331. return false;
  332. location += 1;
  333. /* (13) cmajflt - %lu */
  334. lp->cmajflt = fast_strtoull_dec(&location, 0);
  335. if (!location[0])
  336. return false;
  337. location += 1;
  338. /* (14) utime - %lu */
  339. lp->utime = LinuxProcessTable_adjustTime(lhost, fast_strtoull_dec(&location, 0));
  340. if (!location[0])
  341. return false;
  342. location += 1;
  343. /* (15) stime - %lu */
  344. lp->stime = LinuxProcessTable_adjustTime(lhost, fast_strtoull_dec(&location, 0));
  345. if (!location[0])
  346. return false;
  347. location += 1;
  348. /* (16) cutime - %ld */
  349. lp->cutime = LinuxProcessTable_adjustTime(lhost, fast_strtoull_dec(&location, 0));
  350. if (!location[0])
  351. return false;
  352. location += 1;
  353. /* (17) cstime - %ld */
  354. lp->cstime = LinuxProcessTable_adjustTime(lhost, fast_strtoull_dec(&location, 0));
  355. if (!location[0])
  356. return false;
  357. location += 1;
  358. /* (18) priority - %ld */
  359. process->priority = fast_strtol_dec(&location, 0);
  360. if (!location[0])
  361. return false;
  362. location += 1;
  363. /* (19) nice - %ld */
  364. process->nice = fast_strtol_dec(&location, 0);
  365. if (!location[0])
  366. return false;
  367. location += 1;
  368. /* (20) num_threads - %ld */
  369. process->nlwp = fast_strtol_dec(&location, 0);
  370. if (!location[0])
  371. return false;
  372. location += 1;
  373. /* Skip (21) itrealvalue - %ld */
  374. location = strchr(location, ' ');
  375. if (!location)
  376. return false;
  377. location += 1;
  378. /* (22) starttime - %llu */
  379. if (process->starttime_ctime == 0) {
  380. process->starttime_ctime = lhost->boottime + LinuxProcessTable_adjustTime(lhost, fast_strtoll_dec(&location, 0)) / 100;
  381. } else {
  382. location = strchr(location, ' ');
  383. if (!location)
  384. return false;
  385. }
  386. location += 1;
  387. /* Skip (23) - (38) */
  388. for (int i = 0; i < 16; i++) {
  389. location = strchr(location, ' ');
  390. if (!location)
  391. return false;
  392. location += 1;
  393. }
  394. assert(location != NULL);
  395. /* (39) processor - %d */
  396. process->processor = fast_strtol_dec(&location, 0);
  397. /* Ignore further fields */
  398. process->time = lp->utime + lp->stime;
  399. return true;
  400. }
  401. /*
  402. * Read /proc/<pid>/status (thread-specific data)
  403. */
  404. static bool LinuxProcessTable_readStatusFile(Process* process, openat_arg_t procFd) {
  405. LinuxProcess* lp = (LinuxProcess*) process;
  406. unsigned long ctxt = 0;
  407. process->isRunningInContainer = TRI_OFF;
  408. #ifdef HAVE_VSERVER
  409. lp->vxid = 0;
  410. #endif
  411. FILE* statusfile = fopenat(procFd, "status", "r");
  412. if (!statusfile)
  413. return false;
  414. char buffer[PROC_LINE_LENGTH + 1] = {0};
  415. while (fgets(buffer, sizeof(buffer), statusfile)) {
  416. if (String_startsWith(buffer, "NSpid:")) {
  417. const char* ptr = buffer;
  418. int pid_ns_count = 0;
  419. while (*ptr && *ptr != '\n' && !isdigit((unsigned char)*ptr))
  420. ++ptr;
  421. while (*ptr && *ptr != '\n') {
  422. if (isdigit(*ptr))
  423. pid_ns_count++;
  424. while (isdigit((unsigned char)*ptr))
  425. ++ptr;
  426. while (*ptr && *ptr != '\n' && !isdigit((unsigned char)*ptr))
  427. ++ptr;
  428. }
  429. if (pid_ns_count > 1)
  430. process->isRunningInContainer = TRI_ON;
  431. } else if (String_startsWith(buffer, "voluntary_ctxt_switches:")) {
  432. unsigned long vctxt;
  433. int ok = sscanf(buffer, "voluntary_ctxt_switches:\t%lu", &vctxt);
  434. if (ok == 1) {
  435. ctxt += vctxt;
  436. }
  437. } else if (String_startsWith(buffer, "nonvoluntary_ctxt_switches:")) {
  438. unsigned long nvctxt;
  439. int ok = sscanf(buffer, "nonvoluntary_ctxt_switches:\t%lu", &nvctxt);
  440. if (ok == 1) {
  441. ctxt += nvctxt;
  442. }
  443. #ifdef HAVE_VSERVER
  444. } else if (String_startsWith(buffer, "VxID:")) {
  445. int vxid;
  446. int ok = sscanf(buffer, "VxID:\t%32d", &vxid);
  447. if (ok == 1) {
  448. lp->vxid = vxid;
  449. }
  450. #ifdef HAVE_ANCIENT_VSERVER
  451. } else if (String_startsWith(buffer, "s_context:")) {
  452. int vxid;
  453. int ok = sscanf(buffer, "s_context:\t%32d", &vxid);
  454. if (ok == 1) {
  455. lp->vxid = vxid;
  456. }
  457. #endif /* HAVE_ANCIENT_VSERVER */
  458. #endif /* HAVE_VSERVER */
  459. }
  460. }
  461. fclose(statusfile);
  462. lp->ctxt_diff = (ctxt > lp->ctxt_total) ? (ctxt - lp->ctxt_total) : 0;
  463. lp->ctxt_total = ctxt;
  464. return true;
  465. }
  466. /*
  467. * Gather user of task (process-shared data)
  468. */
  469. static bool LinuxProcessTable_updateUser(const Machine* host, Process* process, openat_arg_t procFd, const LinuxProcess* mainTask) {
  470. if (mainTask) {
  471. process->st_uid = mainTask->super.st_uid;
  472. process->user = mainTask->super.user;
  473. return true;
  474. }
  475. struct stat sb;
  476. #ifdef HAVE_OPENAT
  477. int statok = fstat(procFd, &sb);
  478. #else
  479. int statok = stat(procFd, &sb);
  480. #endif
  481. if (statok == -1)
  482. return false;
  483. if (process->st_uid != sb.st_uid) {
  484. process->st_uid = sb.st_uid;
  485. process->user = UsersTable_getRef(host->usersTable, sb.st_uid);
  486. }
  487. return true;
  488. }
  489. /*
  490. * Read /proc/<pid>/io (thread-specific data)
  491. */
  492. static void LinuxProcessTable_readIoFile(LinuxProcess* lp, openat_arg_t procFd, bool scanMainThread) {
  493. Process* process = &lp->super;
  494. const Machine* host = process->super.host;
  495. char path[20] = "io";
  496. char buffer[1024];
  497. if (scanMainThread) {
  498. xSnprintf(path, sizeof(path), "task/%"PRIi32"/io", (int32_t)Process_getPid(process));
  499. }
  500. ssize_t r = xReadfileat(procFd, path, buffer, sizeof(buffer));
  501. if (r < 0) {
  502. lp->io_rate_read_bps = NAN;
  503. lp->io_rate_write_bps = NAN;
  504. lp->io_rchar = ULLONG_MAX;
  505. lp->io_wchar = ULLONG_MAX;
  506. lp->io_syscr = ULLONG_MAX;
  507. lp->io_syscw = ULLONG_MAX;
  508. lp->io_read_bytes = ULLONG_MAX;
  509. lp->io_write_bytes = ULLONG_MAX;
  510. lp->io_cancelled_write_bytes = ULLONG_MAX;
  511. lp->io_last_scan_time_ms = host->realtimeMs;
  512. return;
  513. }
  514. unsigned long long last_read = lp->io_read_bytes;
  515. unsigned long long last_write = lp->io_write_bytes;
  516. unsigned long long time_delta = saturatingSub(host->realtimeMs, lp->io_last_scan_time_ms);
  517. // Note: Linux Kernel documentation states that /proc/<pid>/io may be racy
  518. // on 32-bit machines. (Documentation/filesystems/proc.rst)
  519. char* buf = buffer;
  520. const char* line;
  521. while ((line = strsep(&buf, "\n")) != NULL) {
  522. switch (line[0]) {
  523. case 'r':
  524. if (line[1] == 'c' && String_startsWith(line + 2, "har: ")) {
  525. lp->io_rchar = strtoull(line + 7, NULL, 10);
  526. } else if (String_startsWith(line + 1, "ead_bytes: ")) {
  527. lp->io_read_bytes = strtoull(line + 12, NULL, 10);
  528. lp->io_rate_read_bps = time_delta ? saturatingSub(lp->io_read_bytes, last_read) * /*ms to s*/1000. / time_delta : NAN;
  529. }
  530. break;
  531. case 'w':
  532. if (line[1] == 'c' && String_startsWith(line + 2, "har: ")) {
  533. lp->io_wchar = strtoull(line + 7, NULL, 10);
  534. } else if (String_startsWith(line + 1, "rite_bytes: ")) {
  535. lp->io_write_bytes = strtoull(line + 13, NULL, 10);
  536. lp->io_rate_write_bps = time_delta ? saturatingSub(lp->io_write_bytes, last_write) * /*ms to s*/1000. / time_delta : NAN;
  537. }
  538. break;
  539. case 's':
  540. if (line[4] == 'r' && String_startsWith(line + 1, "yscr: ")) {
  541. lp->io_syscr = strtoull(line + 7, NULL, 10);
  542. } else if (String_startsWith(line + 1, "yscw: ")) {
  543. lp->io_syscw = strtoull(line + 7, NULL, 10);
  544. }
  545. break;
  546. case 'c':
  547. if (String_startsWith(line + 1, "ancelled_write_bytes: ")) {
  548. lp->io_cancelled_write_bytes = strtoull(line + 23, NULL, 10);
  549. }
  550. }
  551. }
  552. lp->io_last_scan_time_ms = host->realtimeMs;
  553. }
  554. typedef struct LibraryData_ {
  555. uint64_t size;
  556. bool exec;
  557. } LibraryData;
  558. static void LinuxProcessTable_calcLibSize_helper(ATTR_UNUSED ht_key_t key, void* value, void* data) {
  559. if (!data)
  560. return;
  561. if (!value)
  562. return;
  563. const LibraryData* v = (const LibraryData*)value;
  564. uint64_t* d = (uint64_t*)data;
  565. if (!v->exec)
  566. return;
  567. *d += v->size;
  568. }
  569. /*
  570. * Read /proc/<pid>/maps (process-shared data)
  571. */
  572. static void LinuxProcessTable_readMaps(LinuxProcess* process, openat_arg_t procFd, const LinuxMachine* host, bool calcSize, bool checkDeletedLib) {
  573. Process* proc = (Process*)process;
  574. proc->usesDeletedLib = false;
  575. FILE* mapsfile = fopenat(procFd, "maps", "r");
  576. if (!mapsfile)
  577. return;
  578. Hashtable* ht = NULL;
  579. if (calcSize)
  580. ht = Hashtable_new(64, true);
  581. char buffer[1024];
  582. while (fgets(buffer, sizeof(buffer), mapsfile)) {
  583. uint64_t map_start;
  584. uint64_t map_end;
  585. bool map_execute;
  586. unsigned int map_devmaj;
  587. unsigned int map_devmin;
  588. uint64_t map_inode;
  589. // Short circuit test: Look for a slash
  590. if (!strchr(buffer, '/'))
  591. continue;
  592. // Parse format: "%Lx-%Lx %4s %x %2x:%2x %Ld"
  593. char* readptr = buffer;
  594. map_start = fast_strtoull_hex(&readptr, 16);
  595. if ('-' != *readptr++)
  596. continue;
  597. map_end = fast_strtoull_hex(&readptr, 16);
  598. if (' ' != *readptr++)
  599. continue;
  600. if (!readptr[0] || !readptr[1] || !readptr[2] || !readptr[3])
  601. continue;
  602. map_execute = (readptr[2] == 'x');
  603. readptr += 4;
  604. if (' ' != *readptr++)
  605. continue;
  606. while (*readptr > ' ')
  607. readptr++; // Skip parsing this hex value
  608. if (' ' != *readptr++)
  609. continue;
  610. map_devmaj = fast_strtoull_hex(&readptr, 4);
  611. if (':' != *readptr++)
  612. continue;
  613. map_devmin = fast_strtoull_hex(&readptr, 4);
  614. if (' ' != *readptr++)
  615. continue;
  616. //Minor shortcut: Once we know there's no file for this region, we skip
  617. if (!map_devmaj && !map_devmin)
  618. continue;
  619. map_inode = fast_strtoull_dec(&readptr, 0);
  620. if (!map_inode)
  621. continue;
  622. if (calcSize) {
  623. LibraryData* libdata = Hashtable_get(ht, map_inode);
  624. if (!libdata) {
  625. libdata = xCalloc(1, sizeof(LibraryData));
  626. Hashtable_put(ht, map_inode, libdata);
  627. }
  628. libdata->size += map_end - map_start;
  629. libdata->exec |= map_execute;
  630. }
  631. if (checkDeletedLib && map_execute && !proc->usesDeletedLib) {
  632. while (*readptr == ' ')
  633. readptr++;
  634. if (*readptr != '/')
  635. continue;
  636. if (String_startsWith(readptr, "/memfd:"))
  637. continue;
  638. /* Virtualbox maps /dev/zero for memory allocation. That results in
  639. * false positive, so ignore. */
  640. if (String_eq(readptr, "/dev/zero (deleted)\n"))
  641. continue;
  642. if (strstr(readptr, " (deleted)\n")) {
  643. proc->usesDeletedLib = true;
  644. if (!calcSize)
  645. break;
  646. }
  647. }
  648. }
  649. fclose(mapsfile);
  650. if (calcSize) {
  651. uint64_t total_size = 0;
  652. Hashtable_foreach(ht, LinuxProcessTable_calcLibSize_helper, &total_size);
  653. Hashtable_delete(ht);
  654. process->m_lrs = total_size / host->pageSize;
  655. }
  656. }
  657. /*
  658. * Read /proc/<pid>/statm (process-shared data)
  659. */
  660. static bool LinuxProcessTable_readStatmFile(LinuxProcess* process, openat_arg_t procFd, const LinuxMachine* host, const LinuxProcess* mainTask) {
  661. if (mainTask) {
  662. process->super.m_virt = mainTask->super.m_virt;
  663. process->super.m_resident = mainTask->super.m_resident;
  664. return true;
  665. }
  666. char statmdata[128] = {0};
  667. if (xReadfileat(procFd, "statm", statmdata, sizeof(statmdata)) < 1) {
  668. return false;
  669. }
  670. long int dummy, dummy2;
  671. int r = sscanf(statmdata, "%ld %ld %ld %ld %ld %ld %ld",
  672. &process->super.m_virt,
  673. &process->super.m_resident,
  674. &process->m_share,
  675. &process->m_trs,
  676. &dummy, /* unused since Linux 2.6; always 0 */
  677. &process->m_drs,
  678. &dummy2); /* unused since Linux 2.6; always 0 */
  679. if (r == 7) {
  680. process->super.m_virt *= host->pageSizeKB;
  681. process->super.m_resident *= host->pageSizeKB;
  682. process->m_priv = process->super.m_resident - (process->m_share * host->pageSizeKB);
  683. }
  684. return r == 7;
  685. }
  686. /*
  687. * Read /proc/<pid>/smaps (process-shared data)
  688. */
  689. static bool LinuxProcessTable_readSmapsFile(LinuxProcess* process, openat_arg_t procFd, bool haveSmapsRollup) {
  690. //http://elixir.free-electrons.com/linux/v4.10/source/fs/proc/task_mmu.c#L719
  691. //kernel will return data in chunks of size PAGE_SIZE or less.
  692. FILE* fp = fopenat(procFd, haveSmapsRollup ? "smaps_rollup" : "smaps", "r");
  693. if (!fp)
  694. return false;
  695. process->m_pss = 0;
  696. process->m_swap = 0;
  697. process->m_psswp = 0;
  698. char buffer[256];
  699. while (fgets(buffer, sizeof(buffer), fp)) {
  700. if (!strchr(buffer, '\n')) {
  701. // Partial line, skip to end of this line
  702. if (!skipEndOfLine(fp)) {
  703. fclose(fp);
  704. return false;
  705. }
  706. }
  707. if (String_startsWith(buffer, "Pss:")) {
  708. process->m_pss += strtol(buffer + 4, NULL, 10);
  709. } else if (String_startsWith(buffer, "Swap:")) {
  710. process->m_swap += strtol(buffer + 5, NULL, 10);
  711. } else if (String_startsWith(buffer, "SwapPss:")) {
  712. process->m_psswp += strtol(buffer + 8, NULL, 10);
  713. }
  714. }
  715. fclose(fp);
  716. return true;
  717. }
  718. #ifdef HAVE_OPENVZ
  719. static void LinuxProcessTable_readOpenVZData(LinuxProcess* process, openat_arg_t procFd) {
  720. if (access(PROCDIR "/vz", R_OK) != 0) {
  721. free(process->ctid);
  722. process->ctid = NULL;
  723. process->vpid = Process_getPid(&process->super);
  724. return;
  725. }
  726. FILE* file = fopenat(procFd, "status", "r");
  727. if (!file) {
  728. free(process->ctid);
  729. process->ctid = NULL;
  730. process->vpid = Process_getPid(&process->super);
  731. return;
  732. }
  733. bool foundEnvID = false;
  734. bool foundVPid = false;
  735. char linebuf[256];
  736. while (fgets(linebuf, sizeof(linebuf), file) != NULL) {
  737. if (strchr(linebuf, '\n') == NULL) {
  738. // Partial line, skip to end of this line
  739. if (!skipEndOfLine(file)) {
  740. break;
  741. }
  742. }
  743. char* name_value_sep = strchr(linebuf, ':');
  744. if (name_value_sep == NULL) {
  745. continue;
  746. }
  747. int field;
  748. if (0 == strncasecmp(linebuf, "envID", name_value_sep - linebuf)) {
  749. field = 1;
  750. } else if (0 == strncasecmp(linebuf, "VPid", name_value_sep - linebuf)) {
  751. field = 2;
  752. } else {
  753. continue;
  754. }
  755. do {
  756. name_value_sep++;
  757. } while (*name_value_sep != '\0' && *name_value_sep <= 32);
  758. char* value_end = name_value_sep;
  759. while (*value_end > 32) {
  760. value_end++;
  761. }
  762. if (name_value_sep == value_end) {
  763. continue;
  764. }
  765. *value_end = '\0';
  766. switch (field) {
  767. case 1:
  768. foundEnvID = true;
  769. if (!String_eq(name_value_sep, process->ctid ? process->ctid : ""))
  770. free_and_xStrdup(&process->ctid, name_value_sep);
  771. break;
  772. case 2:
  773. foundVPid = true;
  774. process->vpid = strtoul(name_value_sep, NULL, 0);
  775. break;
  776. default:
  777. //Sanity Check: Should never reach here, or the implementation is missing something!
  778. assert(false && "OpenVZ handling: Unimplemented case for field handling reached.");
  779. }
  780. }
  781. fclose(file);
  782. if (!foundEnvID) {
  783. free(process->ctid);
  784. process->ctid = NULL;
  785. }
  786. if (!foundVPid) {
  787. process->vpid = Process_getPid(&process->super);
  788. }
  789. }
  790. #endif /* HAVE_OPENVZ */
  791. /*
  792. * Read /proc/<pid>/cgroup (thread-specific data)
  793. */
  794. static void LinuxProcessTable_readCGroupFile(LinuxProcess* process, openat_arg_t procFd) {
  795. FILE* file = fopenat(procFd, "cgroup", "r");
  796. if (!file) {
  797. if (process->cgroup) {
  798. free(process->cgroup);
  799. process->cgroup = NULL;
  800. }
  801. if (process->cgroup_short) {
  802. free(process->cgroup_short);
  803. process->cgroup_short = NULL;
  804. }
  805. if (process->container_short) {
  806. free(process->container_short);
  807. process->container_short = NULL;
  808. }
  809. return;
  810. }
  811. char output[PROC_LINE_LENGTH + 1];
  812. output[0] = '\0';
  813. char* at = output;
  814. int left = PROC_LINE_LENGTH;
  815. while (!feof(file) && left > 0) {
  816. char buffer[PROC_LINE_LENGTH + 1];
  817. const char* ok = fgets(buffer, PROC_LINE_LENGTH, file);
  818. if (!ok)
  819. break;
  820. char* group = buffer;
  821. for (size_t i = 0; i < 2; i++) {
  822. group = String_strchrnul(group, ':');
  823. if (!*group)
  824. break;
  825. group++;
  826. }
  827. char* eol = String_strchrnul(group, '\n');
  828. *eol = '\0';
  829. if (at != output) {
  830. *at = ';';
  831. at++;
  832. left--;
  833. }
  834. int wrote = snprintf(at, left, "%s", group);
  835. left -= wrote;
  836. }
  837. fclose(file);
  838. bool changed = !process->cgroup || !String_eq(process->cgroup, output);
  839. Row_updateFieldWidth(CGROUP, strlen(output));
  840. free_and_xStrdup(&process->cgroup, output);
  841. if (!changed) {
  842. if (process->cgroup_short) {
  843. Row_updateFieldWidth(CCGROUP, strlen(process->cgroup_short));
  844. } else {
  845. //CCGROUP is alias to normal CGROUP if shortening fails
  846. Row_updateFieldWidth(CCGROUP, strlen(process->cgroup));
  847. }
  848. if (process->container_short) {
  849. Row_updateFieldWidth(CONTAINER, strlen(process->container_short));
  850. } else {
  851. Row_updateFieldWidth(CONTAINER, strlen("N/A"));
  852. }
  853. return;
  854. }
  855. char* cgroup_short = CGroup_filterName(process->cgroup);
  856. if (cgroup_short) {
  857. Row_updateFieldWidth(CCGROUP, strlen(cgroup_short));
  858. free_and_xStrdup(&process->cgroup_short, cgroup_short);
  859. free(cgroup_short);
  860. } else {
  861. //CCGROUP is alias to normal CGROUP if shortening fails
  862. Row_updateFieldWidth(CCGROUP, strlen(process->cgroup));
  863. free(process->cgroup_short);
  864. process->cgroup_short = NULL;
  865. }
  866. char* container_short = CGroup_filterContainer(process->cgroup);
  867. if (container_short) {
  868. Row_updateFieldWidth(CONTAINER, strlen(container_short));
  869. free_and_xStrdup(&process->container_short, container_short);
  870. free(container_short);
  871. } else {
  872. //CONTAINER is just "N/A" if shortening fails
  873. Row_updateFieldWidth(CONTAINER, strlen("N/A"));
  874. free(process->container_short);
  875. process->container_short = NULL;
  876. }
  877. }
  878. /*
  879. * Read /proc/<pid>/oom_score (process-shared data)
  880. */
  881. static void LinuxProcessTable_readOomData(LinuxProcess* process, openat_arg_t procFd, const LinuxProcess* mainTask) {
  882. if (mainTask) {
  883. process->oom = mainTask->oom;
  884. return;
  885. }
  886. char buffer[PROC_LINE_LENGTH + 1] = {0};
  887. ssize_t oomRead = xReadfileat(procFd, "oom_score", buffer, sizeof(buffer));
  888. if (oomRead < 1) {
  889. return;
  890. }
  891. char* oomPtr = buffer;
  892. uint64_t oom = fast_strtoull_dec(&oomPtr, oomRead);
  893. if (*oomPtr && *oomPtr != '\n' && *oomPtr != ' ') {
  894. return;
  895. }
  896. if (oom > UINT_MAX) {
  897. return;
  898. }
  899. process->oom = oom;
  900. }
  901. /*
  902. * Read /proc/<pid>/autogroup (process-shared data)
  903. */
  904. static void LinuxProcessTable_readAutogroup(LinuxProcess* process, openat_arg_t procFd, const LinuxProcess* mainTask) {
  905. if (mainTask) {
  906. process->autogroup_id = mainTask->autogroup_id;
  907. return;
  908. }
  909. process->autogroup_id = -1;
  910. char autogroup[64]; // space for two numeric values and fixed length strings
  911. ssize_t amtRead = xReadfileat(procFd, "autogroup", autogroup, sizeof(autogroup));
  912. if (amtRead < 0)
  913. return;
  914. long int identity;
  915. int nice;
  916. int ok = sscanf(autogroup, "/autogroup-%ld nice %d", &identity, &nice);
  917. if (ok == 2) {
  918. process->autogroup_id = identity;
  919. process->autogroup_nice = nice;
  920. }
  921. }
  922. /*
  923. * Read /proc/<pid>/attr/current (process-shared data)
  924. */
  925. static void LinuxProcessTable_readSecattrData(LinuxProcess* process, openat_arg_t procFd, const LinuxProcess* mainTask) {
  926. if (mainTask) {
  927. const char* mainSecAttr = mainTask->secattr;
  928. if (mainSecAttr) {
  929. free_and_xStrdup(&process->secattr, mainSecAttr);
  930. } else {
  931. free(process->secattr);
  932. process->secattr = NULL;
  933. }
  934. return;
  935. }
  936. char buffer[PROC_LINE_LENGTH + 1] = {0};
  937. ssize_t attrdata = xReadfileat(procFd, "attr/current", buffer, sizeof(buffer));
  938. if (attrdata < 1) {
  939. free(process->secattr);
  940. process->secattr = NULL;
  941. return;
  942. }
  943. char* newline = strchr(buffer, '\n');
  944. if (newline) {
  945. *newline = '\0';
  946. }
  947. Row_updateFieldWidth(SECATTR, strlen(buffer));
  948. free_and_xStrdup(&process->secattr, buffer);
  949. }
  950. /*
  951. * Read /proc/<pid>/cwd (process-shared data)
  952. */
  953. static void LinuxProcessTable_readCwd(LinuxProcess* process, openat_arg_t procFd, const LinuxProcess* mainTask) {
  954. if (mainTask) {
  955. const char* mainCwd = mainTask->super.procCwd;
  956. if (mainCwd) {
  957. free_and_xStrdup(&process->super.procCwd, mainCwd);
  958. } else {
  959. free(process->super.procCwd);
  960. process->super.procCwd = NULL;
  961. }
  962. return;
  963. }
  964. char pathBuffer[PATH_MAX + 1] = {0};
  965. #if defined(HAVE_READLINKAT) && defined(HAVE_OPENAT)
  966. ssize_t r = readlinkat(procFd, "cwd", pathBuffer, sizeof(pathBuffer) - 1);
  967. #else
  968. ssize_t r = Compat_readlink(procFd, "cwd", pathBuffer, sizeof(pathBuffer) - 1);
  969. #endif
  970. if (r < 0) {
  971. free(process->super.procCwd);
  972. process->super.procCwd = NULL;
  973. return;
  974. }
  975. pathBuffer[r] = '\0';
  976. free_and_xStrdup(&process->super.procCwd, pathBuffer);
  977. }
  978. /*
  979. * Read /proc/<pid>/exe (process-shared data)
  980. */
  981. static void LinuxProcessList_readExe(Process* process, openat_arg_t procFd, const LinuxProcess* mainTask) {
  982. if (mainTask) {
  983. Process_updateExe(process, mainTask->super.procExe);
  984. process->procExeDeleted = mainTask->super.procExeDeleted;
  985. return;
  986. }
  987. char filename[PATH_MAX + 1];
  988. #if defined(HAVE_READLINKAT) && defined(HAVE_OPENAT)
  989. ssize_t amtRead = readlinkat(procFd, "exe", filename, sizeof(filename) - 1);
  990. #else
  991. ssize_t amtRead = Compat_readlink(procFd, "exe", filename, sizeof(filename) - 1);
  992. #endif
  993. if (amtRead > 0) {
  994. filename[amtRead] = 0;
  995. if (!process->procExe ||
  996. (!process->procExeDeleted && !String_eq(filename, process->procExe)) ||
  997. process->procExeDeleted) {
  998. const char* deletedMarker = " (deleted)";
  999. const size_t markerLen = strlen(deletedMarker);
  1000. const size_t filenameLen = strlen(filename);
  1001. if (filenameLen > markerLen) {
  1002. bool oldExeDeleted = process->procExeDeleted;
  1003. process->procExeDeleted = String_eq(filename + filenameLen - markerLen, deletedMarker);
  1004. if (process->procExeDeleted)
  1005. filename[filenameLen - markerLen] = '\0';
  1006. if (oldExeDeleted != process->procExeDeleted)
  1007. process->mergedCommand.lastUpdate = 0;
  1008. }
  1009. Process_updateExe(process, filename);
  1010. }
  1011. } else if (process->procExe) {
  1012. Process_updateExe(process, NULL);
  1013. process->procExeDeleted = false;
  1014. }
  1015. }
  1016. /*
  1017. * Read /proc/<pid>/cmdline (process-shared data)
  1018. */
  1019. static bool LinuxProcessTable_readCmdlineFile(Process* process, openat_arg_t procFd, const LinuxProcess* mainTask) {
  1020. LinuxProcessList_readExe(process, procFd, mainTask);
  1021. char command[4096 + 1]; // max cmdline length on Linux
  1022. ssize_t amtRead = xReadfileat(procFd, "cmdline", command, sizeof(command));
  1023. if (amtRead <= 0)
  1024. return false;
  1025. int tokenEnd = -1;
  1026. int tokenStart = -1;
  1027. int lastChar = 0;
  1028. bool argSepNUL = false;
  1029. bool argSepSpace = false;
  1030. for (int i = 0; i < amtRead; i++) {
  1031. // If this is true, there's a NUL byte in the middle of command
  1032. if (tokenEnd >= 0) {
  1033. argSepNUL = true;
  1034. }
  1035. const char argChar = command[i];
  1036. /* newline used as delimiter - when forming the mergedCommand, newline is
  1037. * converted to space by Process_makeCommandStr */
  1038. if (argChar == '\n') {
  1039. /* Set to some other non-printable character */
  1040. command[i] = '\r';
  1041. continue;
  1042. }
  1043. if (argChar == '\0') {
  1044. command[i] = '\n';
  1045. // Set tokenEnd to the NUL byte
  1046. if (tokenEnd < 0) {
  1047. tokenEnd = i;
  1048. }
  1049. continue;
  1050. }
  1051. /* Record some information for the argument parsing heuristic below. */
  1052. if (argChar <= ' ') {
  1053. argSepSpace = true;
  1054. }
  1055. /* Detect the last / before the end of the token as
  1056. * the start of the basename in cmdline, see Process_writeCommand */
  1057. if (argChar == '/' && tokenEnd < 0) {
  1058. tokenStart = i + 1;
  1059. }
  1060. lastChar = i;
  1061. }
  1062. command[lastChar + 1] = '\0';
  1063. if (!argSepNUL && argSepSpace) {
  1064. /* Argument parsing heuristic.
  1065. *
  1066. * This heuristic is used for processes that rewrite their command line.
  1067. * Normally the command line is split by using NUL bytes between each argument.
  1068. * But some programs like chrome flatten this using spaces.
  1069. *
  1070. * This heuristic tries its best to undo this loss of information.
  1071. * To achieve this, we treat every character <= 32 as argument separators
  1072. * (i.e. all of ASCII control sequences and space).
  1073. * We then search for the basename of the cmdline in the first argument we found that way.
  1074. * As path names may contain we try to cross-validate if the path we got that way exists.
  1075. */
  1076. tokenStart = -1;
  1077. tokenEnd = -1;
  1078. size_t exeLen = process->procExe ? strlen(process->procExe) : 0;
  1079. if (process->procExe && String_startsWith(command, process->procExe) &&
  1080. exeLen < (size_t)lastChar && command[exeLen] <= ' ') {
  1081. tokenStart = process->procExeBasenameOffset;
  1082. tokenEnd = exeLen;
  1083. }
  1084. // From initial scan we know there's at least one space.
  1085. // Check if that's part of a filename for an existing file.
  1086. else if (Compat_faccessat(AT_FDCWD, command, F_OK, AT_SYMLINK_NOFOLLOW) != 0) {
  1087. // If we reach here the path does not exist.
  1088. // Thus begin searching for the part of it that actually does.
  1089. int tokenArg0Start = -1;
  1090. for (int i = 0; i <= lastChar; i++) {
  1091. const char cmdChar = command[i];
  1092. /* Any ASCII control or space used as delimiter */
  1093. if (cmdChar <= ' ') {
  1094. if (tokenEnd >= 0) {
  1095. // Split on every further separator, regardless of path correctness
  1096. command[i] = '\n';
  1097. continue;
  1098. }
  1099. // Found our first argument
  1100. command[i] = '\0';
  1101. bool found = Compat_faccessat(AT_FDCWD, command, F_OK, AT_SYMLINK_NOFOLLOW) == 0;
  1102. // Restore if this wasn't it
  1103. command[i] = found ? '\n' : cmdChar;
  1104. if (found)
  1105. tokenEnd = i;
  1106. if (tokenArg0Start < 0)
  1107. tokenArg0Start = tokenStart < 0 ? 0 : tokenStart;
  1108. continue;
  1109. }
  1110. if (tokenEnd >= 0) {
  1111. continue;
  1112. }
  1113. if (cmdChar == '/') {
  1114. // Normal path separator
  1115. tokenStart = i + 1;
  1116. } else if (cmdChar == '\\' && (tokenStart < 1 || command[tokenStart - 1] == '\\')) {
  1117. // Windows Path separator (WINE)
  1118. tokenStart = i + 1;
  1119. } else if (cmdChar == ':' && (command[i + 1] != '/' && command[i + 1] != '\\')) {
  1120. // Colon not part of a Windows Path
  1121. tokenEnd = i;
  1122. } else if (tokenStart < 0) {
  1123. // Relative path
  1124. tokenStart = i;
  1125. }
  1126. }
  1127. if (tokenEnd < 0) {
  1128. tokenStart = tokenArg0Start;
  1129. // No token delimiter found, forcibly split
  1130. for (int i = 0; i <= lastChar; i++) {
  1131. if (command[i] <= ' ') {
  1132. command[i] = '\n';
  1133. if (tokenEnd < 0) {
  1134. tokenEnd = i;
  1135. }
  1136. }
  1137. }
  1138. }
  1139. }
  1140. /* Some command lines are hard to parse, like
  1141. * file.so [kdeinit5] file local:/run/user/1000/klauncherdqbouY.1.slave-socket local:/run/user/1000/kded5TwsDAx.1.slave-socket
  1142. * Reset if start is behind end.
  1143. */
  1144. if (tokenStart >= tokenEnd) {
  1145. tokenStart = -1;
  1146. tokenEnd = -1;
  1147. }
  1148. }
  1149. if (tokenStart < 0) {
  1150. tokenStart = 0;
  1151. }
  1152. if (tokenEnd < 0) {
  1153. tokenEnd = lastChar + 1;
  1154. }
  1155. Process_updateCmdline(process, command, tokenStart, tokenEnd);
  1156. return true;
  1157. }
  1158. /*
  1159. * Read /proc/<pid>/comm (thread-specific data)
  1160. */
  1161. static void LinuxProcessList_readComm(Process* process, openat_arg_t procFd) {
  1162. char command[4096 + 1]; // max cmdline length on Linux
  1163. ssize_t amtRead = xReadfileat(procFd, "comm", command, sizeof(command));
  1164. if (amtRead > 0) {
  1165. command[amtRead - 1] = '\0';
  1166. Process_updateComm(process, command);
  1167. } else {
  1168. Process_updateComm(process, NULL);
  1169. }
  1170. }
  1171. static char* LinuxProcessTable_updateTtyDevice(TtyDriver* ttyDrivers, unsigned long int tty_nr) {
  1172. unsigned int maj = major(tty_nr);
  1173. unsigned int min = minor(tty_nr);
  1174. int i = -1;
  1175. for (;;) {
  1176. i++;
  1177. if ((!ttyDrivers[i].path) || maj < ttyDrivers[i].major) {
  1178. break;
  1179. }
  1180. if (maj > ttyDrivers[i].major) {
  1181. continue;
  1182. }
  1183. if (min < ttyDrivers[i].minorFrom) {
  1184. break;
  1185. }
  1186. if (min > ttyDrivers[i].minorTo) {
  1187. continue;
  1188. }
  1189. unsigned int idx = min - ttyDrivers[i].minorFrom;
  1190. struct stat sb;
  1191. char* fullPath;
  1192. for (;;) {
  1193. xAsprintf(&fullPath, "%s/%d", ttyDrivers[i].path, idx);
  1194. int err = stat(fullPath, &sb);
  1195. if (err == 0 && major(sb.st_rdev) == maj && minor(sb.st_rdev) == min) {
  1196. return fullPath;
  1197. }
  1198. free(fullPath);
  1199. xAsprintf(&fullPath, "%s%d", ttyDrivers[i].path, idx);
  1200. err = stat(fullPath, &sb);
  1201. if (err == 0 && major(sb.st_rdev) == maj && minor(sb.st_rdev) == min) {
  1202. return fullPath;
  1203. }
  1204. free(fullPath);
  1205. if (idx == min) {
  1206. break;
  1207. }
  1208. idx = min;
  1209. }
  1210. int err = stat(ttyDrivers[i].path, &sb);
  1211. if (err == 0 && tty_nr == sb.st_rdev) {
  1212. return xStrdup(ttyDrivers[i].path);
  1213. }
  1214. }
  1215. char* out;
  1216. xAsprintf(&out, "/dev/%u:%u", maj, min);
  1217. return out;
  1218. }
  1219. static bool isOlderThan(const Process* proc, unsigned int seconds) {
  1220. const Machine* host = proc->super.host;
  1221. assert(host->realtimeMs > 0);
  1222. /* Starttime might not yet be parsed */
  1223. if (proc->starttime_ctime <= 0)
  1224. return false;
  1225. uint64_t realtime = host->realtimeMs / 1000;
  1226. if (realtime < (uint64_t)proc->starttime_ctime)
  1227. return false;
  1228. return realtime - proc->starttime_ctime > seconds;
  1229. }
  1230. static bool LinuxProcessTable_recurseProcTree(LinuxProcessTable* this, openat_arg_t parentFd, const LinuxMachine* lhost, const char* dirname, const LinuxProcess* mainTask) {
  1231. ProcessTable* pt = (ProcessTable*) this;
  1232. const Machine* host = &lhost->super;
  1233. const Settings* settings = host->settings;
  1234. const ScreenSettings* ss = settings->ss;
  1235. const struct dirent* entry;
  1236. /* set runningTasks from /proc/stat (from Machine_scanCPUTime) */
  1237. pt->runningTasks = lhost->runningTasks;
  1238. #ifdef HAVE_OPENAT
  1239. int dirFd = openat(parentFd, dirname, O_RDONLY | O_DIRECTORY | O_NOFOLLOW);
  1240. if (dirFd < 0)
  1241. return false;
  1242. DIR* dir = fdopendir(dirFd);
  1243. #else
  1244. char dirFd[4096];
  1245. xSnprintf(dirFd, sizeof(dirFd), "%s/%s", parentFd, dirname);
  1246. DIR* dir = opendir(dirFd);
  1247. #endif
  1248. if (!dir) {
  1249. Compat_openatArgClose(dirFd);
  1250. return false;
  1251. }
  1252. const bool hideKernelThreads = settings->hideKernelThreads;
  1253. const bool hideUserlandThreads = settings->hideUserlandThreads;
  1254. const bool hideRunningInContainer = settings->hideRunningInContainer;
  1255. while ((entry = readdir(dir)) != NULL) {
  1256. const char* name = entry->d_name;
  1257. // Ignore all non-directories
  1258. if (entry->d_type != DT_DIR && entry->d_type != DT_UNKNOWN) {
  1259. continue;
  1260. }
  1261. // The RedHat kernel hides threads with a dot.
  1262. // I believe this is non-standard.
  1263. if (name[0] == '.') {
  1264. name++;
  1265. }
  1266. // Just skip all non-number directories.
  1267. if (name[0] < '0' || name[0] > '9') {
  1268. continue;
  1269. }
  1270. // filename is a number: process directory
  1271. int pid;
  1272. {
  1273. char* endptr;
  1274. unsigned long parsedPid = strtoul(name, &endptr, 10);
  1275. if (parsedPid == 0 || parsedPid == ULONG_MAX || *endptr != '\0')
  1276. continue;
  1277. pid = parsedPid;
  1278. }
  1279. // Skip task directory of main thread
  1280. if (mainTask && pid == Process_getPid(&mainTask->super))
  1281. continue;
  1282. #ifdef HAVE_OPENAT
  1283. int procFd = openat(dirFd, entry->d_name, O_RDONLY | O_DIRECTORY | O_NOFOLLOW);
  1284. if (procFd < 0)
  1285. continue;
  1286. #else
  1287. char procFd[4096];
  1288. xSnprintf(procFd, sizeof(procFd), "%s/%s", dirFd, entry->d_name);
  1289. #endif
  1290. bool preExisting;
  1291. Process* proc = ProcessTable_getProcess(pt, pid, &preExisting, LinuxProcess_new);
  1292. LinuxProcess* lp = (LinuxProcess*) proc;
  1293. Process_setThreadGroup(proc, mainTask ? Process_getPid(&mainTask->super) : pid);
  1294. proc->isUserlandThread = Process_getPid(proc) != Process_getThreadGroup(proc);
  1295. assert(proc->isUserlandThread == (mainTask != NULL));
  1296. LinuxProcessTable_recurseProcTree(this, procFd, lhost, "task", lp);
  1297. /*
  1298. * These conditions will not trigger on first occurrence, cause we need to
  1299. * add the process to the ProcessTable and do all one time scans
  1300. * (e.g. parsing the cmdline to detect a kernel thread)
  1301. * But it will short-circuit subsequent scans.
  1302. */
  1303. if (preExisting && hideKernelThreads && Process_isKernelThread(proc)) {
  1304. proc->super.updated = true;
  1305. proc->super.show = false;
  1306. pt->kernelThreads++;
  1307. pt->totalTasks++;
  1308. Compat_openatArgClose(procFd);
  1309. continue;
  1310. }
  1311. if (preExisting && hideUserlandThreads && Process_isUserlandThread(proc)) {
  1312. proc->super.updated = true;
  1313. proc->super.show = false;
  1314. pt->userlandThreads++;
  1315. pt->totalTasks++;
  1316. Compat_openatArgClose(procFd);
  1317. continue;
  1318. }
  1319. if (preExisting && hideRunningInContainer && proc->isRunningInContainer == TRI_ON) {
  1320. proc->super.updated = true;
  1321. proc->super.show = false;
  1322. Compat_openatArgClose(procFd);
  1323. continue;
  1324. }
  1325. const bool scanMainThread = !hideUserlandThreads && !Process_isKernelThread(proc) && !mainTask;
  1326. if (!LinuxProcessTable_readStatmFile(lp, procFd, lhost, mainTask))
  1327. goto errorReadingProcess;
  1328. {
  1329. bool prev = proc->usesDeletedLib;
  1330. if (!proc->isKernelThread && !proc->isUserlandThread &&
  1331. ((ss->flags & PROCESS_FLAG_LINUX_LRS_FIX) || (settings->highlightDeletedExe && !proc->procExeDeleted && isOlderThan(proc, 10)))) {
  1332. // Check if we really should recalculate the M_LRS value for this process
  1333. uint64_t passedTimeInMs = host->realtimeMs - lp->last_mlrs_calctime;
  1334. uint64_t recheck = ((uint64_t)rand()) % 2048;
  1335. if (passedTimeInMs > recheck) {
  1336. lp->last_mlrs_calctime = host->realtimeMs;
  1337. LinuxProcessTable_readMaps(lp, procFd, lhost, ss->flags & PROCESS_FLAG_LINUX_LRS_FIX, settings->highlightDeletedExe);
  1338. }
  1339. } else {
  1340. /* Copy from process structure in threads and reset if setting got disabled */
  1341. proc->usesDeletedLib = (proc->isUserlandThread && mainTask) ? mainTask->super.usesDeletedLib : false;
  1342. lp->m_lrs = (proc->isUserlandThread && mainTask) ? mainTask->m_lrs : 0;
  1343. }
  1344. if (prev != proc->usesDeletedLib)
  1345. proc->mergedCommand.lastUpdate = 0;
  1346. }
  1347. char statCommand[MAX_NAME + 1];
  1348. unsigned long long int lasttimes = (lp->utime + lp->stime);
  1349. unsigned long int last_tty_nr = proc->tty_nr;
  1350. if (!LinuxProcessTable_readStatFile(lp, procFd, lhost, scanMainThread, statCommand, sizeof(statCommand)))
  1351. goto errorReadingProcess;
  1352. if (lp->flags & PF_KTHREAD) {
  1353. proc->isKernelThread = true;
  1354. }
  1355. if (last_tty_nr != proc->tty_nr && this->ttyDrivers) {
  1356. free(proc->tty_name);
  1357. proc->tty_name = LinuxProcessTable_updateTtyDevice(this->ttyDrivers, proc->tty_nr);
  1358. }
  1359. proc->percent_cpu = NAN;
  1360. /* lhost->period might be 0 after system sleep */
  1361. if (lhost->period > 0.0) {
  1362. float percent_cpu = saturatingSub(lp->utime + lp->stime, lasttimes) / lhost->period * 100.0;
  1363. proc->percent_cpu = MINIMUM(percent_cpu, host->activeCPUs * 100.0F);
  1364. }
  1365. proc->percent_mem = proc->m_resident / (double)(host->totalMem) * 100.0;
  1366. Process_updateCPUFieldWidths(proc->percent_cpu);
  1367. if (!LinuxProcessTable_updateUser(host, proc, procFd, mainTask))
  1368. goto errorReadingProcess;
  1369. /* Check if the process is inside a different PID namespace. */
  1370. if (proc->isRunningInContainer == TRI_INITIAL && rootPidNs != (ino_t)-1) {
  1371. struct stat sb;
  1372. #if defined(HAVE_OPENAT) && defined(HAVE_FSTATAT)
  1373. int res = fstatat(procFd, "ns/pid", &sb, 0);
  1374. #else
  1375. char path[PATH_MAX];
  1376. xSnprintf(path, sizeof(path), "%s/ns/pid", procFd);
  1377. int res = stat(path, &sb);
  1378. #endif
  1379. if (res == 0) {
  1380. proc->isRunningInContainer = (sb.st_ino != rootPidNs) ? TRI_ON : TRI_OFF;
  1381. }
  1382. }
  1383. if (ss->flags & PROCESS_FLAG_LINUX_CTXT
  1384. || ((hideRunningInContainer || ss->flags & PROCESS_FLAG_LINUX_CONTAINER) && proc->isRunningInContainer == TRI_INITIAL)
  1385. #ifdef HAVE_VSERVER
  1386. || ss->flags & PROCESS_FLAG_LINUX_VSERVER
  1387. #endif
  1388. ) {
  1389. proc->isRunningInContainer = TRI_OFF;
  1390. if (!LinuxProcessTable_readStatusFile(proc, procFd))
  1391. goto errorReadingProcess;
  1392. }
  1393. if (!preExisting) {
  1394. #ifdef HAVE_OPENVZ
  1395. if (ss->flags & PROCESS_FLAG_LINUX_OPENVZ) {
  1396. LinuxProcessTable_readOpenVZData(lp, procFd);
  1397. }
  1398. #endif
  1399. if (proc->isKernelThread) {
  1400. Process_updateCmdline(proc, NULL, 0, 0);
  1401. } else {
  1402. if (!LinuxProcessTable_readCmdlineFile(proc, procFd, mainTask)) {
  1403. Process_updateCmdline(proc, statCommand, 0, strlen(statCommand));
  1404. }
  1405. LinuxProcessList_readComm(proc, procFd);
  1406. }
  1407. Process_fillStarttimeBuffer(proc);
  1408. ProcessTable_add(pt, proc);
  1409. } else {
  1410. if (settings->updateProcessNames && proc->state != ZOMBIE) {
  1411. if (proc->isKernelThread) {
  1412. Process_updateCmdline(proc, NULL, 0, 0);
  1413. } else {
  1414. if (!LinuxProcessTable_readCmdlineFile(proc, procFd, mainTask)) {
  1415. Process_updateCmdline(proc, statCommand, 0, strlen(statCommand));
  1416. }
  1417. LinuxProcessList_readComm(proc, procFd);
  1418. }
  1419. }
  1420. }
  1421. /*
  1422. * Section gathering non-critical information that is independent from
  1423. * each other.
  1424. */
  1425. /* Gather permitted capabilities (thread-specific data) for non-root process. */
  1426. if (proc->st_uid != 0 && proc->elevated_priv != TRI_OFF) {
  1427. struct __user_cap_header_struct header = { .version = _LINUX_CAPABILITY_VERSION_3, .pid = Process_getPid(proc) };
  1428. struct __user_cap_data_struct data;
  1429. long res = syscall(SYS_capget, &header, &data);
  1430. if (res == 0) {
  1431. proc->elevated_priv = (data.permitted != 0) ? TRI_ON : TRI_OFF;
  1432. } else {
  1433. proc->elevated_priv = TRI_OFF;
  1434. }
  1435. }
  1436. if (ss->flags & PROCESS_FLAG_LINUX_CGROUP)
  1437. LinuxProcessTable_readCGroupFile(lp, procFd);
  1438. if ((ss->flags & PROCESS_FLAG_LINUX_SMAPS) && !Process_isKernelThread(proc)) {
  1439. if (!mainTask) {
  1440. // Read smaps file of each process only every second pass to improve performance
  1441. static int smaps_flag = 0;
  1442. if ((pid & 1) == smaps_flag) {
  1443. LinuxProcessTable_readSmapsFile(lp, procFd, this->haveSmapsRollup);
  1444. }
  1445. if (pid == 1) {
  1446. smaps_flag = !smaps_flag;
  1447. }
  1448. } else {
  1449. lp->m_pss = mainTask->m_pss;
  1450. lp->m_swap = mainTask->m_swap;
  1451. lp->m_psswp = mainTask->m_psswp;
  1452. }
  1453. }
  1454. if (ss->flags & PROCESS_FLAG_IO) {
  1455. LinuxProcessTable_readIoFile(lp, procFd, scanMainThread);
  1456. }
  1457. #ifdef HAVE_DELAYACCT
  1458. if (ss->flags & PROCESS_FLAG_LINUX_DELAYACCT) {
  1459. LibNl_readDelayAcctData(this, lp);
  1460. }
  1461. #endif
  1462. if (ss->flags & PROCESS_FLAG_LINUX_OOM) {
  1463. LinuxProcessTable_readOomData(lp, procFd, mainTask);
  1464. }
  1465. if (ss->flags & PROCESS_FLAG_LINUX_IOPRIO) {
  1466. LinuxProcess_updateIOPriority(proc);
  1467. }
  1468. if (ss->flags & PROCESS_FLAG_LINUX_SECATTR) {
  1469. LinuxProcessTable_readSecattrData(lp, procFd, mainTask);
  1470. }
  1471. if (ss->flags & PROCESS_FLAG_CWD) {
  1472. LinuxProcessTable_readCwd(lp, procFd, mainTask);
  1473. }
  1474. if ((ss->flags & PROCESS_FLAG_LINUX_AUTOGROUP) && this->haveAutogroup) {
  1475. LinuxProcessTable_readAutogroup(lp, procFd, mainTask);
  1476. }
  1477. #ifdef SCHEDULER_SUPPORT
  1478. if (ss->flags & PROCESS_FLAG_SCHEDPOL) {
  1479. Scheduling_readProcessPolicy(proc);
  1480. }
  1481. #endif
  1482. if (ss->flags & PROCESS_FLAG_LINUX_GPU || GPUMeter_active()) {
  1483. if (mainTask) {
  1484. lp->gpu_time = mainTask->gpu_time;
  1485. } else {
  1486. GPU_readProcessData(this, lp, procFd);
  1487. }
  1488. }
  1489. /*
  1490. * Final section after all data has been gathered
  1491. */
  1492. if (!proc->cmdline && statCommand[0] &&
  1493. (proc->state == ZOMBIE || Process_isKernelThread(proc) || settings->showThreadNames)) {
  1494. Process_updateCmdline(proc, statCommand, 0, strlen(statCommand));
  1495. }
  1496. proc->super.updated = true;
  1497. Compat_openatArgClose(procFd);
  1498. if (hideRunningInContainer && proc->isRunningInContainer == TRI_ON) {
  1499. proc->super.show = false;
  1500. continue;
  1501. }
  1502. if (Process_isKernelThread(proc)) {
  1503. pt->kernelThreads++;
  1504. } else if (Process_isUserlandThread(proc)) {
  1505. pt->userlandThreads++;
  1506. }
  1507. /* Set at the end when we know if a new entry is a thread */
  1508. proc->super.show = ! ((hideKernelThreads && Process_isKernelThread(proc)) || (hideUserlandThreads && Process_isUserlandThread(proc)));
  1509. pt->totalTasks++;
  1510. /* runningTasks is set in Machine_scanCPUTime() from /proc/stat */
  1511. continue;
  1512. // Exception handler.
  1513. errorReadingProcess:
  1514. {
  1515. #ifdef HAVE_OPENAT
  1516. if (procFd >= 0)
  1517. close(procFd);
  1518. #endif
  1519. if (preExisting) {
  1520. /*
  1521. * The only real reason for coming here (apart from Linux violating the /proc API)
  1522. * would be the process going away with its /proc files disappearing (!HAVE_OPENAT).
  1523. * However, we want to keep in the process list for now for the "highlight dying" mode.
  1524. */
  1525. } else {
  1526. /* A really short-lived process that we don't have full info about */
  1527. assert(ProcessTable_findProcess(pt, Process_getPid(proc)) == NULL);
  1528. Process_delete((Object*)proc);
  1529. }
  1530. }
  1531. }
  1532. closedir(dir);
  1533. return true;
  1534. }
  1535. void ProcessTable_goThroughEntries(ProcessTable* super) {
  1536. LinuxProcessTable* this = (LinuxProcessTable*) super;
  1537. Machine* host = super->super.host;
  1538. const Settings* settings = host->settings;
  1539. LinuxMachine* lhost = (LinuxMachine*) host;
  1540. if (settings->ss->flags & PROCESS_FLAG_LINUX_AUTOGROUP) {
  1541. // Refer to sched(7) 'autogroup feature' section
  1542. // The kernel feature can be enabled/disabled through procfs at
  1543. // any time, so check for it at the start of each sample - only
  1544. // read from per-process procfs files if it's globally enabled.
  1545. this->haveAutogroup = LinuxProcess_isAutogroupEnabled();
  1546. } else {
  1547. this->haveAutogroup = false;
  1548. }
  1549. /* Shift GPU values */
  1550. {
  1551. lhost->prevGpuTime = lhost->curGpuTime;
  1552. lhost->curGpuTime = 0;
  1553. for (GPUEngineData* engine = lhost->gpuEngineData; engine; engine = engine->next) {
  1554. engine->prevTime = engine->curTime;
  1555. engine->curTime = 0;
  1556. }
  1557. }
  1558. /* PROCDIR is an absolute path */
  1559. assert(PROCDIR[0] == '/');
  1560. #ifdef HAVE_OPENAT
  1561. openat_arg_t rootFd = AT_FDCWD;
  1562. #else
  1563. openat_arg_t rootFd = "";
  1564. #endif
  1565. LinuxProcessTable_recurseProcTree(this, rootFd, lhost, PROCDIR, NULL);
  1566. }