diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2011-06-18 15:51:16 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2011-06-18 15:51:16 +0200 |
commit | 5331e382f72a606c026424e95fcc7dc50a25608c (patch) | |
tree | 83417a34ba920632a9bd9a6dfb9120d9cd3976df | |
parent | 12ac6287eedf45d896557b95270a6e0323951917 (diff) | |
download | busybox-w32-5331e382f72a606c026424e95fcc7dc50a25608c.tar.gz busybox-w32-5331e382f72a606c026424e95fcc7dc50a25608c.tar.bz2 busybox-w32-5331e382f72a606c026424e95fcc7dc50a25608c.zip |
libbb/read_cmdline: prepend {comm} if different from argv0. Closes 3835.
function old new delta
read_cmdline 114 233 +119
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | libbb/procps.c | 35 |
1 files changed, 32 insertions, 3 deletions
diff --git a/libbb/procps.c b/libbb/procps.c index 9207e9254..1dea61518 100644 --- a/libbb/procps.c +++ b/libbb/procps.c | |||
@@ -566,18 +566,47 @@ procps_status_t* FAST_FUNC procps_scan(procps_status_t* sp, int flags) | |||
566 | void FAST_FUNC read_cmdline(char *buf, int col, unsigned pid, const char *comm) | 566 | void FAST_FUNC read_cmdline(char *buf, int col, unsigned pid, const char *comm) |
567 | { | 567 | { |
568 | int sz; | 568 | int sz; |
569 | char filename[sizeof("/proc//cmdline") + sizeof(int)*3]; | 569 | char filename[sizeof("/proc/%u/cmdline") + sizeof(int)*3]; |
570 | 570 | ||
571 | sprintf(filename, "/proc/%u/cmdline", pid); | 571 | sprintf(filename, "/proc/%u/cmdline", pid); |
572 | sz = open_read_close(filename, buf, col - 1); | 572 | sz = open_read_close(filename, buf, col - 1); |
573 | if (sz > 0) { | 573 | if (sz > 0) { |
574 | const char *base; | ||
575 | int comm_len; | ||
576 | |||
574 | buf[sz] = '\0'; | 577 | buf[sz] = '\0'; |
575 | while (--sz >= 0 && buf[sz] == '\0') | 578 | while (--sz >= 0 && buf[sz] == '\0') |
576 | continue; | 579 | continue; |
577 | do { | 580 | base = bb_basename(buf); /* before we replace argv0's NUL with space */ |
581 | while (sz >= 0) { | ||
578 | if ((unsigned char)(buf[sz]) < ' ') | 582 | if ((unsigned char)(buf[sz]) < ' ') |
579 | buf[sz] = ' '; | 583 | buf[sz] = ' '; |
580 | } while (--sz >= 0); | 584 | sz--; |
585 | } | ||
586 | |||
587 | /* If comm differs from argv0, prepend "{comm} ". | ||
588 | * It allows to see thread names set by prctl(PR_SET_NAME). | ||
589 | */ | ||
590 | if (base[0] == '-') /* "-sh" (login shell)? */ | ||
591 | base++; | ||
592 | comm_len = strlen(comm); | ||
593 | /* Why compare up to comm_len, not COMM_LEN-1? | ||
594 | * Well, some processes rewrite argv, and use _spaces_ there | ||
595 | * while rewriting. (KDE is observed to do it). | ||
596 | * I prefer to still treat argv0 "process foo bar" | ||
597 | * as 'equal' to comm "process". | ||
598 | */ | ||
599 | if (strncmp(base, comm, comm_len) != 0) { | ||
600 | comm_len += 3; | ||
601 | if (col > comm_len) | ||
602 | memmove(buf + comm_len, buf, col - comm_len); | ||
603 | snprintf(buf, col, "{%s}", comm); | ||
604 | if (col <= comm_len) | ||
605 | return; | ||
606 | buf[comm_len - 1] = ' '; | ||
607 | buf[col - 1] = '\0'; | ||
608 | } | ||
609 | |||
581 | } else { | 610 | } else { |
582 | snprintf(buf, col, "[%s]", comm); | 611 | snprintf(buf, col, "[%s]", comm); |
583 | } | 612 | } |