summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2019-01-08 15:51:04 +0000
committerRon Yorston <rmy@pobox.com>2019-01-08 15:51:04 +0000
commitbade8b3460ea3cf1182cd1bda9faf8b6ccee6a25 (patch)
tree6b0c855e3d0688e07f512e61d17d76d09c64c6cb
parent59701e7f6a483d4aad4dc7088c673cd69fe294c8 (diff)
downloadbusybox-w32-bade8b3460ea3cf1182cd1bda9faf8b6ccee6a25.tar.gz
busybox-w32-bade8b3460ea3cf1182cd1bda9faf8b6ccee6a25.tar.bz2
busybox-w32-bade8b3460ea3cf1182cd1bda9faf8b6ccee6a25.zip
ps: add support for the args column
Implement read_cmdline() for WIN32 by storing the command line in the same way as the applet name. The applet name is actually used for the comm column which is truncated to COMM_LEN. Using this as the size of the bb_comm array avoids the need to calculate MAX_APPLET_NAME_LEN.
-rw-r--r--applets/applet_tables.c9
-rw-r--r--include/libbb.h7
-rw-r--r--libbb/appletlib.c29
-rw-r--r--procps/ps.c6
-rw-r--r--shell/ash.c1
-rw-r--r--win32/process.c40
6 files changed, 55 insertions, 37 deletions
diff --git a/applets/applet_tables.c b/applets/applet_tables.c
index 6a20c8b38..02352113f 100644
--- a/applets/applet_tables.c
+++ b/applets/applet_tables.c
@@ -62,7 +62,6 @@ static int str_isalnum_(const char *s)
62int main(int argc, char **argv) 62int main(int argc, char **argv)
63{ 63{
64 int i, j; 64 int i, j;
65 unsigned MAX_APPLET_NAME_LEN = 1;
66 char tmp1[PATH_MAX], tmp2[PATH_MAX]; 65 char tmp1[PATH_MAX], tmp2[PATH_MAX];
67 66
68 // In find_applet_by_name(), before linear search, narrow it down 67 // In find_applet_by_name(), before linear search, narrow it down
@@ -136,8 +135,8 @@ int main(int argc, char **argv)
136 printf("const char applet_names[] ALIGN1 = \"\"\n"); 135 printf("const char applet_names[] ALIGN1 = \"\"\n");
137 for (i = 0; i < NUM_APPLETS; i++) { 136 for (i = 0; i < NUM_APPLETS; i++) {
138 printf("\"%s\" \"\\0\"\n", applets[i].name); 137 printf("\"%s\" \"\\0\"\n", applets[i].name);
139 if (MAX_APPLET_NAME_LEN < strlen(applets[i].name)) 138// if (MAX_APPLET_NAME_LEN < strlen(applets[i].name))
140 MAX_APPLET_NAME_LEN = strlen(applets[i].name); 139// MAX_APPLET_NAME_LEN = strlen(applets[i].name);
141 } 140 }
142 printf(";\n\n"); 141 printf(";\n\n");
143 142
@@ -205,8 +204,8 @@ int main(int argc, char **argv)
205#endif 204#endif
206 //printf("#endif /* SKIP_definitions */\n"); 205 //printf("#endif /* SKIP_definitions */\n");
207 206
208 printf("\n"); 207// printf("\n");
209 printf("#define MAX_APPLET_NAME_LEN %u\n", MAX_APPLET_NAME_LEN); 208// printf("#define MAX_APPLET_NAME_LEN %u\n", MAX_APPLET_NAME_LEN);
210 209
211 if (argv[2]) { 210 if (argv[2]) {
212 FILE *fp; 211 FILE *fp;
diff --git a/include/libbb.h b/include/libbb.h
index 8b18782c8..dc1c81865 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -1969,11 +1969,7 @@ void free_procps_scan(procps_status_t* sp) FAST_FUNC;
1969procps_status_t* procps_scan(procps_status_t* sp, int flags) FAST_FUNC; 1969procps_status_t* procps_scan(procps_status_t* sp, int flags) FAST_FUNC;
1970/* Format cmdline (up to col chars) into char buf[size] */ 1970/* Format cmdline (up to col chars) into char buf[size] */
1971/* Puts [comm] if cmdline is empty (-> process is a kernel thread) */ 1971/* Puts [comm] if cmdline is empty (-> process is a kernel thread) */
1972#if !ENABLE_PLATFORM_MINGW32
1973void read_cmdline(char *buf, int size, unsigned pid, const char *comm) FAST_FUNC; 1972void read_cmdline(char *buf, int size, unsigned pid, const char *comm) FAST_FUNC;
1974#else
1975#define read_cmdline(buf, size, pid, comm) snprintf(buf, size, "[%s]", comm)
1976#endif
1977pid_t *find_pid_by_name(const char* procName) FAST_FUNC; 1973pid_t *find_pid_by_name(const char* procName) FAST_FUNC;
1978pid_t *pidlist_reverse(pid_t *pidList) FAST_FUNC; 1974pid_t *pidlist_reverse(pid_t *pidList) FAST_FUNC;
1979int starts_with_cpu(const char *str) FAST_FUNC; 1975int starts_with_cpu(const char *str) FAST_FUNC;
@@ -2122,7 +2118,8 @@ extern const char bb_path_wtmp_file[] ALIGN1;
2122#define bb_dev_null "/dev/null" 2118#define bb_dev_null "/dev/null"
2123#if ENABLE_PLATFORM_MINGW32 2119#if ENABLE_PLATFORM_MINGW32
2124#define bb_busybox_exec_path get_busybox_exec_path() 2120#define bb_busybox_exec_path get_busybox_exec_path()
2125extern char bb_applet_name[]; 2121extern char bb_comm[];
2122extern char bb_command_line[];
2126#else 2123#else
2127extern const char bb_busybox_exec_path[] ALIGN1; 2124extern const char bb_busybox_exec_path[] ALIGN1;
2128#endif 2125#endif
diff --git a/libbb/appletlib.c b/libbb/appletlib.c
index ba52ef581..b3a144aee 100644
--- a/libbb/appletlib.c
+++ b/libbb/appletlib.c
@@ -1067,30 +1067,37 @@ int busybox_main(int argc UNUSED_PARAM, char **argv)
1067# if NUM_APPLETS > 0 1067# if NUM_APPLETS > 0
1068 1068
1069# if ENABLE_PLATFORM_MINGW32 1069# if ENABLE_PLATFORM_MINGW32
1070char bb_applet_name[MAX_APPLET_NAME_LEN+1]; 1070char bb_comm[COMM_LEN];
1071char bb_command_line[128];
1071# endif 1072# endif
1072 1073
1073void FAST_FUNC run_applet_no_and_exit(int applet_no, const char *name, char **argv) 1074void FAST_FUNC run_applet_no_and_exit(int applet_no, const char *name, char **argv)
1074{ 1075{
1075 int argc = string_array_len(argv); 1076 int argc = string_array_len(argv);
1077# if ENABLE_PLATFORM_MINGW32
1078 int i;
1079 const char *vmask;
1080 unsigned int mask;
1081# endif
1076 1082
1077 /* 1083 /*
1078 * We do not use argv[0]: do not want to repeat massaging of 1084 * We do not use argv[0]: do not want to repeat massaging of
1079 * "-/sbin/halt" -> "halt", for example. 1085 * "-/sbin/halt" -> "halt", for example.
1080 */ 1086 */
1081 applet_name = name; 1087 applet_name = name;
1082#if ENABLE_PLATFORM_MINGW32 1088# if ENABLE_PLATFORM_MINGW32
1083 strcpy(bb_applet_name, applet_name); 1089 safe_strncpy(bb_comm, applet_name, sizeof(bb_comm));
1084
1085 {
1086 const char *vmask;
1087 unsigned int mask;
1088 1090
1089 vmask = getenv("BB_UMASK"); 1091 safe_strncpy(bb_command_line, applet_name, sizeof(bb_command_line));
1090 if (vmask && sscanf(vmask, "%o", &mask) == 1) 1092 for (i=1; i < argc && argv[i] &&
1091 umask((mode_t)(mask&0777)); 1093 strlen(bb_command_line) + strlen(argv[i]) + 2 < 128; ++i) {
1094 strcat(strcat(bb_command_line, " "), argv[i]);
1092 } 1095 }
1093#endif 1096
1097 vmask = getenv("BB_UMASK");
1098 if (vmask && sscanf(vmask, "%o", &mask) == 1)
1099 umask((mode_t)(mask&0777));
1100# endif
1094 1101
1095 /* Special case. POSIX says "test --help" 1102 /* Special case. POSIX says "test --help"
1096 * should be no different from e.g. "test --foo". 1103 * should be no different from e.g. "test --foo".
diff --git a/procps/ps.c b/procps/ps.c
index 1f14844e6..c08d03146 100644
--- a/procps/ps.c
+++ b/procps/ps.c
@@ -244,12 +244,12 @@ static void func_state(char *buf, int size, const procps_status_t *ps)
244{ 244{
245 safe_strncpy(buf, ps->state, size+1); 245 safe_strncpy(buf, ps->state, size+1);
246} 246}
247#endif
247 248
248static void func_args(char *buf, int size, const procps_status_t *ps) 249static void func_args(char *buf, int size, const procps_status_t *ps)
249{ 250{
250 read_cmdline(buf, size+1, ps->pid, ps->comm); 251 read_cmdline(buf, size+1, ps->pid, ps->comm);
251} 252}
252#endif
253 253
254static void func_pid(char *buf, int size, const procps_status_t *ps) 254static void func_pid(char *buf, int size, const procps_status_t *ps)
255{ 255{
@@ -386,9 +386,7 @@ static const ps_out_t out_spec[] = {
386 { 8 , "user" ,"USER" ,func_user ,PSSCAN_UIDGID }, 386 { 8 , "user" ,"USER" ,func_user ,PSSCAN_UIDGID },
387 { 8 , "group" ,"GROUP" ,func_group ,PSSCAN_UIDGID }, 387 { 8 , "group" ,"GROUP" ,func_group ,PSSCAN_UIDGID },
388 { 16 , "comm" ,"COMMAND",func_comm ,PSSCAN_COMM }, 388 { 16 , "comm" ,"COMMAND",func_comm ,PSSCAN_COMM },
389#if !ENABLE_PLATFORM_MINGW32
390 { MAX_WIDTH , "args" ,"COMMAND",func_args ,PSSCAN_COMM }, 389 { MAX_WIDTH , "args" ,"COMMAND",func_args ,PSSCAN_COMM },
391#endif
392 { 5 , "pid" ,"PID" ,func_pid ,PSSCAN_PID }, 390 { 5 , "pid" ,"PID" ,func_pid ,PSSCAN_PID },
393 { 5 , "ppid" ,"PPID" ,func_ppid ,PSSCAN_PPID }, 391 { 5 , "ppid" ,"PPID" ,func_ppid ,PSSCAN_PPID },
394#if !ENABLE_PLATFORM_MINGW32 392#if !ENABLE_PLATFORM_MINGW32
@@ -553,7 +551,7 @@ static void format_process(const procps_status_t *ps)
553# define SELINUX_O_PREFIX "label," 551# define SELINUX_O_PREFIX "label,"
554# define DEFAULT_O_STR (SELINUX_O_PREFIX "pid,user" IF_FEATURE_PS_TIME(",time") ",args") 552# define DEFAULT_O_STR (SELINUX_O_PREFIX "pid,user" IF_FEATURE_PS_TIME(",time") ",args")
555#elif ENABLE_PLATFORM_MINGW32 553#elif ENABLE_PLATFORM_MINGW32
556# define DEFAULT_O_STR ("pid,ppid,user" IF_FEATURE_PS_TIME(",time,etime") ",comm") 554# define DEFAULT_O_STR ("pid,ppid,user" IF_FEATURE_PS_TIME(",time,etime") ",args")
557#else 555#else
558# define DEFAULT_O_STR ("pid,user" IF_FEATURE_PS_TIME(",time") ",args") 556# define DEFAULT_O_STR ("pid,user" IF_FEATURE_PS_TIME(",time") ",args")
559#endif 557#endif
diff --git a/shell/ash.c b/shell/ash.c
index d9a4a8cfa..6d5c55d70 100644
--- a/shell/ash.c
+++ b/shell/ash.c
@@ -14892,7 +14892,6 @@ int ash_main(int argc UNUSED_PARAM, char **argv)
14892 SetConsoleCtrlHandler(ctrl_handler, TRUE); 14892 SetConsoleCtrlHandler(ctrl_handler, TRUE);
14893 14893
14894 if (argc == 3 && !strcmp(argv[1], "--fs")) { 14894 if (argc == 3 && !strcmp(argv[1], "--fs")) {
14895 strcpy(bb_applet_name, "[sh]");
14896 forkshell_init(argv[2]); 14895 forkshell_init(argv[2]);
14897 14896
14898 /* NOTREACHED */ 14897 /* NOTREACHED */
diff --git a/win32/process.c b/win32/process.c
index b29780be8..49dddf864 100644
--- a/win32/process.c
+++ b/win32/process.c
@@ -377,11 +377,11 @@ static inline long long filetime_to_ticks(const FILETIME *ft)
377} 377}
378 378
379/* 379/*
380 * Attempt to get the applet name from another instance of busybox.exe. 380 * Attempt to get a string from another instance of busybox.exe.
381 * This will only work if the other process is using the same binary 381 * This will only work if the other process is using the same binary
382 * as the current process. If anything goes wrong just give up. 382 * as the current process. If anything goes wrong just give up.
383 */ 383 */
384static char *get_applet_name(DWORD pid, char *exe) 384static char *get_bb_string(DWORD pid, const char *exe, char *string)
385{ 385{
386 HANDLE proc; 386 HANDLE proc;
387 HMODULE mlist[32]; 387 HMODULE mlist[32];
@@ -389,6 +389,7 @@ static char *get_applet_name(DWORD pid, char *exe)
389 void *address; 389 void *address;
390 char *my_base; 390 char *my_base;
391 char buffer[128]; 391 char buffer[128];
392 char exepath[PATH_MAX];
392 char *name = NULL; 393 char *name = NULL;
393 int i; 394 int i;
394 395
@@ -397,6 +398,12 @@ static char *get_applet_name(DWORD pid, char *exe)
397 return NULL; 398 return NULL;
398 } 399 }
399 400
401 if (exe == NULL) {
402 if (GetProcessImageFileName(proc, exepath, PATH_MAX) != 0) {
403 exe = bb_basename(exepath);
404 }
405 }
406
400 /* 407 /*
401 * Search for the module that matches the name of the executable. 408 * Search for the module that matches the name of the executable.
402 * The values returned in mlist are actually the base address of 409 * The values returned in mlist are actually the base address of
@@ -407,7 +414,7 @@ static char *get_applet_name(DWORD pid, char *exe)
407 goto finish; 414 goto finish;
408 } 415 }
409 416
410 for (i=0; i<needed/sizeof(HMODULE); ++i) { 417 for (i=0; exe != NULL && i<needed/sizeof(HMODULE); ++i) {
411 char modname[MAX_PATH]; 418 char modname[MAX_PATH];
412 if (GetModuleFileNameEx(proc, mlist[i], modname, sizeof(modname))) { 419 if (GetModuleFileNameEx(proc, mlist[i], modname, sizeof(modname))) {
413 if (strcasecmp(bb_basename(modname), exe) == 0) { 420 if (strcasecmp(bb_basename(modname), exe) == 0) {
@@ -432,17 +439,14 @@ static char *get_applet_name(DWORD pid, char *exe)
432 goto finish; 439 goto finish;
433 } 440 }
434 441
435 /* attempt to read the applet name */ 442 /* attempt to read the required string */
436 address = (char *)mlist[i] + ((char *)bb_applet_name - my_base); 443 address = (char *)mlist[i] + ((char *)string - my_base);
437 if (!ReadProcessMemory(proc, address, buffer, 128, NULL)) { 444 if (!ReadProcessMemory(proc, address, buffer, 128, NULL)) {
438 goto finish; 445 goto finish;
439 } 446 }
440 447
441 /* check that the string really is an applet name */ 448 buffer[127] = '\0';
442 buffer[31] = '\0'; 449 name = auto_string(xstrdup(buffer));
443 if (find_applet_by_name(buffer) >= 0 || !strcmp(buffer, "[sh]")) {
444 name = auto_string(xstrdup(buffer));
445 }
446 450
447 finish: 451 finish:
448 CloseHandle(proc); 452 CloseHandle(proc);
@@ -522,7 +526,7 @@ UNUSED_PARAM
522 if (sp->pid == GetProcessId(GetCurrentProcess())) { 526 if (sp->pid == GetProcessId(GetCurrentProcess())) {
523 comm = applet_name; 527 comm = applet_name;
524 } 528 }
525 else if ((name=get_applet_name(pe.th32ProcessID, pe.szExeFile)) != NULL) { 529 else if ((name=get_bb_string(sp->pid, pe.szExeFile, bb_comm)) != NULL) {
526 comm = name; 530 comm = name;
527 } 531 }
528 else { 532 else {
@@ -533,6 +537,20 @@ UNUSED_PARAM
533 return sp; 537 return sp;
534} 538}
535 539
540void FAST_FUNC read_cmdline(char *buf, int col, unsigned pid, const char *comm)
541{
542 const char *str, *cmdline;
543
544 *buf = '\0';
545 if (pid == GetProcessId(GetCurrentProcess()))
546 cmdline = bb_command_line;
547 else if ((str=get_bb_string(pid, NULL, bb_command_line)) != NULL)
548 cmdline = str;
549 else
550 cmdline = comm;
551 safe_strncpy(buf, cmdline, col);
552}
553
536/** 554/**
537 * If the process ID is positive invoke the callback for that process 555 * If the process ID is positive invoke the callback for that process
538 * only. If negative or zero invoke the callback for all descendants 556 * only. If negative or zero invoke the callback for all descendants