diff options
author | Ron Yorston <rmy@pobox.com> | 2024-05-14 14:03:25 +0100 |
---|---|---|
committer | Ron Yorston <rmy@pobox.com> | 2024-05-14 14:03:25 +0100 |
commit | 4e0359ce93c4b0185c5340d03be505b609417b04 (patch) | |
tree | d6e876e08778e88f162657ef237400cdb438de61 /win32 | |
parent | 569de936abb90f4c7cdca9da111a6ea780b135bf (diff) | |
download | busybox-w32-4e0359ce93c4b0185c5340d03be505b609417b04.tar.gz busybox-w32-4e0359ce93c4b0185c5340d03be505b609417b04.tar.bz2 busybox-w32-4e0359ce93c4b0185c5340d03be505b609417b04.zip |
ps: report unknown parent PID as 1
If the parent PID doesn't appear in the process table, report it
as 1. This more closely matches how orphaned children are handled
on UNIX.
Adds 96-128 bytes.
(GitHub issue #416)
Diffstat (limited to 'win32')
-rw-r--r-- | win32/process.c | 22 |
1 files changed, 20 insertions, 2 deletions
diff --git a/win32/process.c b/win32/process.c index b993bb2be..8931eae2e 100644 --- a/win32/process.c +++ b/win32/process.c | |||
@@ -583,6 +583,8 @@ static char *get_bb_string(DWORD pid, const char *exe, char *string) | |||
583 | return name; | 583 | return name; |
584 | } | 584 | } |
585 | 585 | ||
586 | #define NPIDS 128 | ||
587 | |||
586 | /* POSIX version in libbb/procps.c */ | 588 | /* POSIX version in libbb/procps.c */ |
587 | procps_status_t* FAST_FUNC procps_scan(procps_status_t* sp, int flags | 589 | procps_status_t* FAST_FUNC procps_scan(procps_status_t* sp, int flags |
588 | #if !ENABLE_FEATURE_PS_TIME && !ENABLE_FEATURE_PS_LONG | 590 | #if !ENABLE_FEATURE_PS_TIME && !ENABLE_FEATURE_PS_LONG |
@@ -603,6 +605,8 @@ UNUSED_PARAM | |||
603 | free(sp); | 605 | free(sp); |
604 | return NULL; | 606 | return NULL; |
605 | } | 607 | } |
608 | sp->pids = xmalloc(sizeof(unsigned) * NPIDS); | ||
609 | sp->maxpids = NPIDS; | ||
606 | ret = Process32First(sp->snapshot, &pe); | 610 | ret = Process32First(sp->snapshot, &pe); |
607 | } | 611 | } |
608 | else { | 612 | else { |
@@ -611,6 +615,7 @@ UNUSED_PARAM | |||
611 | 615 | ||
612 | if (!ret) { | 616 | if (!ret) { |
613 | CloseHandle(sp->snapshot); | 617 | CloseHandle(sp->snapshot); |
618 | free(sp->pids); | ||
614 | free(sp); | 619 | free(sp); |
615 | return NULL; | 620 | return NULL; |
616 | } | 621 | } |
@@ -653,8 +658,21 @@ UNUSED_PARAM | |||
653 | } | 658 | } |
654 | } | 659 | } |
655 | 660 | ||
656 | sp->pid = pe.th32ProcessID; | 661 | /* The parent of PID 0 is 0. If the parent is a PID we haven't |
657 | sp->ppid = pe.th32ParentProcessID; | 662 | * seen set PPID to 1. */ |
663 | sp->ppid = pe.th32ProcessID != 0; | ||
664 | for (int i = 0; i < sp->npids; ++i) { | ||
665 | if (sp->pids[i] == pe.th32ParentProcessID) { | ||
666 | sp->ppid = pe.th32ParentProcessID; | ||
667 | break; | ||
668 | } | ||
669 | } | ||
670 | |||
671 | if (sp->npids == sp->maxpids) { | ||
672 | sp->maxpids += NPIDS; | ||
673 | sp->pids = xrealloc(sp->pids, sizeof(unsigned) * sp->maxpids); | ||
674 | } | ||
675 | sp->pids[sp->npids++] = sp->pid = pe.th32ProcessID; | ||
658 | 676 | ||
659 | if (sp->pid == getpid()) { | 677 | if (sp->pid == getpid()) { |
660 | comm = applet_name; | 678 | comm = applet_name; |