| 503 | } |
| 504 | |
| 505 | static bool readProcessCmdline(int pid, ProcessInfo* info) { |
| 506 | char path[64]; |
| 507 | snprintf(path, sizeof(path), "/proc/%d/cmdline", pid); |
| 508 | |
| 509 | int fd = open(path, O_RDONLY); |
| 510 | if (fd == -1) { |
| 511 | return false; |
| 512 | } |
| 513 | |
| 514 | const size_t max_read = sizeof(info->cmdline) - 1; |
| 515 | size_t len = 0; |
| 516 | |
| 517 | ssize_t r; |
| 518 | while (r = read(fd, info->cmdline + len, max_read - len)) { |
| 519 | if (r > 0) { |
| 520 | len += (size_t)r; |
| 521 | if (len == max_read) break; |
| 522 | } else { |
| 523 | if (errno == EINTR) continue; |
| 524 | close(fd); |
| 525 | return false; |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | close(fd); |
| 530 | |
| 531 | // Replace null bytes with spaces (arguments are separated by null bytes) |
| 532 | for (size_t i = 0; i < len; i++) { |
| 533 | if (info->cmdline[i] == '\0') { |
| 534 | info->cmdline[i] = ' '; |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | // Ensure null termination |
| 539 | info->cmdline[len] = '\0'; |
| 540 | |
| 541 | // Remove trailing space if present |
| 542 | while (len > 0 && info->cmdline[len - 1] == ' ') { |
| 543 | info->cmdline[--len] = '\0'; |
| 544 | } |
| 545 | |
| 546 | return true; |
| 547 | } |
| 548 | |
| 549 | static bool readProcessStats(int pid, ProcessInfo* info) { |
| 550 | char path[64]; |
no outgoing calls
no test coverage detected