aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2024-05-16 12:48:38 +0100
committerRon Yorston <rmy@pobox.com>2024-05-16 12:48:38 +0100
commit4588cd440e68af132a212d93cd3c4efd73e530b8 (patch)
tree9b84291dffc0a664cf101e7739261f88c7599ea6
parent2a01fe0325524f7745f60a528d1f3076c098d89d (diff)
downloadbusybox-w32-4588cd440e68af132a212d93cd3c4efd73e530b8.tar.gz
busybox-w32-4588cd440e68af132a212d93cd3c4efd73e530b8.tar.bz2
busybox-w32-4588cd440e68af132a212d93cd3c4efd73e530b8.zip
win32: implement getppid(2)
busybox-w32 had a dummy implementation of getppid(2) which always returned 1. Provide a more realistic version. The effect is limited: - The PPID shell variable should report a sensible value. - The special value to omit the parent PID 'pidof -o %PPID' should work. Costs 48 bytes.
-rw-r--r--include/mingw.h2
-rw-r--r--win32/process.c33
2 files changed, 25 insertions, 10 deletions
diff --git a/include/mingw.h b/include/mingw.h
index 56a7285e5..2a6cae4ee 100644
--- a/include/mingw.h
+++ b/include/mingw.h
@@ -477,7 +477,7 @@ int getuid(void);
477#define geteuid getuid 477#define geteuid getuid
478#define getegid getuid 478#define getegid getuid
479int getgroups(int n, gid_t *groups); 479int getgroups(int n, gid_t *groups);
480IMPL(getppid,int,1,void); 480pid_t getppid(void);
481NOIMPL(getsid,pid_t pid UNUSED_PARAM); 481NOIMPL(getsid,pid_t pid UNUSED_PARAM);
482int getlogin_r(char *buf, size_t len); 482int getlogin_r(char *buf, size_t len);
483int fcntl(int fd, int cmd, ...); 483int fcntl(int fd, int cmd, ...);
diff --git a/win32/process.c b/win32/process.c
index bbb04678f..bf98ef746 100644
--- a/win32/process.c
+++ b/win32/process.c
@@ -583,6 +583,19 @@ static char *get_bb_string(DWORD pid, const char *exe, char *string)
583 return name; 583 return name;
584} 584}
585 585
586pid_t getppid(void)
587{
588 procps_status_t *sp = NULL;
589 int my_pid = getpid();
590
591 while ((sp = procps_scan(sp, 0)) != NULL) {
592 if (sp->pid == my_pid) {
593 return sp->ppid;
594 }
595 }
596 return 1;
597}
598
586#define NPIDS 128 599#define NPIDS 128
587 600
588/* POSIX version in libbb/procps.c */ 601/* POSIX version in libbb/procps.c */
@@ -677,16 +690,18 @@ UNUSED_PARAM
677 } 690 }
678 sp->pid = pe.th32ProcessID; 691 sp->pid = pe.th32ProcessID;
679 692
680 if (sp->pid == getpid()) { 693 if (flags & PSSCAN_COMM) {
681 comm = applet_name; 694 if (sp->pid == getpid()) {
682 } 695 comm = applet_name;
683 else if ((name=get_bb_string(sp->pid, pe.szExeFile, bb_comm)) != NULL) { 696 }
684 comm = name; 697 else if ((name=get_bb_string(sp->pid, pe.szExeFile, bb_comm)) != NULL) {
685 } 698 comm = name;
686 else { 699 }
687 comm = pe.szExeFile; 700 else {
701 comm = pe.szExeFile;
702 }
703 safe_strncpy(sp->comm, comm, COMM_LEN);
688 } 704 }
689 safe_strncpy(sp->comm, comm, COMM_LEN);
690 705
691 return sp; 706 return sp;
692} 707}