diff options
author | Ron Yorston <rmy@pobox.com> | 2024-05-16 12:42:46 +0100 |
---|---|---|
committer | Ron Yorston <rmy@pobox.com> | 2024-05-16 12:42:46 +0100 |
commit | 2a01fe0325524f7745f60a528d1f3076c098d89d (patch) | |
tree | 6fe1b772cc59cd258ca84f2ee29b58be6da150ab | |
parent | f1fc5ab9d6980b88eede93051736305408774122 (diff) | |
download | busybox-w32-2a01fe0325524f7745f60a528d1f3076c098d89d.tar.gz busybox-w32-2a01fe0325524f7745f60a528d1f3076c098d89d.tar.bz2 busybox-w32-2a01fe0325524f7745f60a528d1f3076c098d89d.zip |
win32: ensure PIDs are read early in procps_scan()
Recent changes to allow orphaned processes to report a parent PID
of 1 rely on the assumption that Process32First/Process32Next
return parents before children. This isn't guaranteed by the API.
Obtain all known PIDs on the first call to procps_scan() so that
dead parents can be detected reliably.
Costs 48 bytes.
-rw-r--r-- | include/libbb.h | 3 | ||||
-rw-r--r-- | win32/process.c | 25 |
2 files changed, 16 insertions, 12 deletions
diff --git a/include/libbb.h b/include/libbb.h index 8d5537114..d37d4e64c 100644 --- a/include/libbb.h +++ b/include/libbb.h | |||
@@ -2148,9 +2148,8 @@ typedef struct procps_status_t { | |||
2148 | DIR *dir; | 2148 | DIR *dir; |
2149 | #else | 2149 | #else |
2150 | HANDLE snapshot; | 2150 | HANDLE snapshot; |
2151 | unsigned *pids; | 2151 | DWORD *pids; |
2152 | int npids; | 2152 | int npids; |
2153 | int maxpids; | ||
2154 | #endif | 2153 | #endif |
2155 | IF_FEATURE_SHOW_THREADS(DIR *task_dir;) | 2154 | IF_FEATURE_SHOW_THREADS(DIR *task_dir;) |
2156 | uint8_t shift_pages_to_bytes; | 2155 | uint8_t shift_pages_to_bytes; |
diff --git a/win32/process.c b/win32/process.c index 9dd4bc1db..bbb04678f 100644 --- a/win32/process.c +++ b/win32/process.c | |||
@@ -605,6 +605,16 @@ UNUSED_PARAM | |||
605 | free(sp); | 605 | free(sp); |
606 | return NULL; | 606 | return NULL; |
607 | } | 607 | } |
608 | if (Process32First(sp->snapshot, &pe)) { | ||
609 | int maxpids = 0; | ||
610 | do { | ||
611 | if (sp->npids == maxpids) { | ||
612 | maxpids += NPIDS; | ||
613 | sp->pids = xrealloc(sp->pids, sizeof(DWORD) * maxpids); | ||
614 | } | ||
615 | sp->pids[sp->npids++] = pe.th32ProcessID; | ||
616 | } while (Process32Next(sp->snapshot, &pe)); | ||
617 | } | ||
608 | ret = Process32First(sp->snapshot, &pe); | 618 | ret = Process32First(sp->snapshot, &pe); |
609 | } | 619 | } |
610 | else { | 620 | else { |
@@ -665,12 +675,7 @@ UNUSED_PARAM | |||
665 | break; | 675 | break; |
666 | } | 676 | } |
667 | } | 677 | } |
668 | 678 | sp->pid = pe.th32ProcessID; | |
669 | if (sp->npids == sp->maxpids) { | ||
670 | sp->maxpids += NPIDS; | ||
671 | sp->pids = xrealloc(sp->pids, sizeof(unsigned) * sp->maxpids); | ||
672 | } | ||
673 | sp->pids[sp->npids++] = sp->pid = pe.th32ProcessID; | ||
674 | 679 | ||
675 | if (sp->pid == getpid()) { | 680 | if (sp->pid == getpid()) { |
676 | comm = applet_name; | 681 | comm = applet_name; |
@@ -787,10 +792,10 @@ static int kill_signal(pid_t pid, int sig) | |||
787 | } | 792 | } |
788 | 793 | ||
789 | /** | 794 | /** |
790 | * If the process ID is positive invoke the callback for that process | 795 | * If the process ID is positive signal that process only. If negative |
791 | * only. If negative or zero invoke the callback for all descendants | 796 | * or zero signal all descendants of the indicated process. Zero |
792 | * of the indicated process. Zero indicates the current process; negative | 797 | * indicates the current process; negative indicates the process with |
793 | * indicates the process with process ID -pid. | 798 | * process ID -pid. |
794 | */ | 799 | */ |
795 | static int kill_pids(pid_t pid, int sig) | 800 | static int kill_pids(pid_t pid, int sig) |
796 | { | 801 | { |