From 32d2c5ca638aad48233d2b68683a75e905ef9821 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Wed, 28 Mar 2018 13:28:03 +0100 Subject: ps: display applet names in process listing In standalone shell mode all busybox-w32 applets are displayed as 'busybox.exe' in a process listing. I haven't found a satisfactory way to query a running instance of busybox-w32 to determine which applet it's running. Handle a couple of cases: - the process running the process scan knows its own PID and knows which applet it is; - just before invoking applet_main set an environment variable whose name contains the PID and whose value is the current applet name. Children running a process scan will inherit their parent's environment and can therefore match the parent's PID to its applet name. --- libbb/appletlib.c | 9 +++++++++ win32/process.c | 17 ++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/libbb/appletlib.c b/libbb/appletlib.c index 1bae940c2..733d9ca12 100644 --- a/libbb/appletlib.c +++ b/libbb/appletlib.c @@ -990,6 +990,15 @@ void FAST_FUNC run_applet_no_and_exit(int applet_no, const char *name, char **ar } if (ENABLE_FEATURE_SUID) check_suid(applet_no); + +#if ENABLE_PLATFORM_MINGW32 + { + char *var = xasprintf("BB_APPLET_%d=%s", getpid(), applet_name); + putenv(var); + free(var); + } +#endif + xfunc_error_retval = applet_main[applet_no](argc, argv); /* Note: applet_main() may also not return (die on a xfunc or such) */ xfunc_die(); diff --git a/win32/process.c b/win32/process.c index e835be63a..95508264b 100644 --- a/win32/process.c +++ b/win32/process.c @@ -410,6 +410,7 @@ UNUSED_PARAM ) { PROCESSENTRY32 pe; + const char *comm; pe.dwSize = sizeof(pe); if (!sp) { @@ -472,7 +473,21 @@ UNUSED_PARAM sp->pid = pe.th32ProcessID; sp->ppid = pe.th32ParentProcessID; - safe_strncpy(sp->comm, pe.szExeFile, COMM_LEN); + + comm = pe.szExeFile; + if (sp->pid == GetProcessId(GetCurrentProcess())) { + comm = applet_name; + } + else { + char *name, *value; + + name = xasprintf("BB_APPLET_%d", sp->pid); + if ((value=getenv(name)) != NULL) { + comm = value; + } + free(name); + } + safe_strncpy(sp->comm, comm, COMM_LEN); return sp; } -- cgit v1.2.3-55-g6feb