From 903abcceb656701cd57a94404f1e13626618b1c9 Mon Sep 17 00:00:00 2001 From: Nguyễn Thái Ngọc Duy Date: Wed, 15 Sep 2010 09:44:01 +1000 Subject: win32: reimplement procps_scan() On Linux, procps_scan() relies on /proc, which is obviously unavailable on Windows. This implementation currently supports procps_status_t.{pid,comm} only. --- include/libbb.h | 3 +++ libbb/procps.c | 3 +++ win32/process.c | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+) diff --git a/include/libbb.h b/include/libbb.h index 31e733d77..e82954fd0 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -1466,6 +1466,9 @@ typedef struct procps_status_t { #if ENABLE_FEATURE_TOP_SMP_PROCESS int last_seen_on_cpu; #endif +#if ENABLE_PLATFORM_MINGW32 + HANDLE snapshot; +#endif } procps_status_t; /* flag bits for procps_scan(xx, flags) calls */ enum { diff --git a/libbb/procps.c b/libbb/procps.c index 14d4481bd..7ffcd8dbc 100644 --- a/libbb/procps.c +++ b/libbb/procps.c @@ -75,6 +75,7 @@ const char* FAST_FUNC get_cached_groupname(gid_t gid) return get_cached(&groupname, gid, gid2group_utoa); } +#if !ENABLE_PLATFORM_MINGW32 #define PROCPS_BUFSIZE 1024 @@ -552,6 +553,8 @@ procps_status_t* FAST_FUNC procps_scan(procps_status_t* sp, int flags) return sp; } +#endif /* ENABLE_PLATFORM_MINGW32 */ + void FAST_FUNC read_cmdline(char *buf, int col, unsigned pid, const char *comm) { int sz; diff --git a/win32/process.c b/win32/process.c index 13bf81797..1b9b61878 100644 --- a/win32/process.c +++ b/win32/process.c @@ -1,4 +1,5 @@ #include "libbb.h" +#include int waitpid(pid_t pid, int *status, unsigned options) { @@ -284,3 +285,35 @@ mingw_execv(const char *cmd, const char *const *argv) { return mingw_execve(cmd, argv, (const char *const *)environ); } + +/* POSIX version in libbb/procps.c */ +procps_status_t* FAST_FUNC procps_scan(procps_status_t* sp, int flags) +{ + PROCESSENTRY32 pe; + + pe.dwSize = sizeof(pe); + if (!sp) { + sp = xzalloc(sizeof(struct procps_status_t)); + sp->snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); + if (sp->snapshot == INVALID_HANDLE_VALUE) { + free(sp); + return NULL; + } + if (!Process32First(sp->snapshot, &pe)) { + CloseHandle(sp->snapshot); + free(sp); + return NULL; + } + } + else { + if (!Process32Next(sp->snapshot, &pe)) { + CloseHandle(sp->snapshot); + free(sp); + return NULL; + } + } + + sp->pid = pe.th32ProcessID; + strncpy(sp->comm, pe.szExeFile, COMM_LEN); + return sp; +} -- cgit v1.2.3-55-g6feb From 9e713dacbf3b6f8d114a337271c57dfccec58af7 Mon Sep 17 00:00:00 2001 From: Nguyễn Thái Ngọc Duy Date: Wed, 15 Sep 2010 09:48:03 +1000 Subject: win32: enable ps Due to limitations in procps_scan(), only -o pid,comm is supported --- procps/ps.c | 10 ++++++++++ scripts/defconfig.mingw32 | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/procps/ps.c b/procps/ps.c index 48b55a785..3f7437b38 100644 --- a/procps/ps.c +++ b/procps/ps.c @@ -25,6 +25,8 @@ enum { MAX_WIDTH = 2*1024 }; #if ENABLE_SELINUX #define SELINUX_O_PREFIX "label," #define DEFAULT_O_STR (SELINUX_O_PREFIX "pid,user" IF_FEATURE_PS_TIME(",time") ",args") +#elif ENABLE_PLATFORM_MINGW32 +#define DEFAULT_O_STR ("pid,comm") #else #define DEFAULT_O_STR ("pid,user" IF_FEATURE_PS_TIME(",time") ",args") #endif @@ -301,13 +303,19 @@ static void func_pcpu(char *buf, int size, const procps_status_t *ps) static const ps_out_t out_spec[] = { // Mandated by POSIX: +#if !ENABLE_PLATFORM_MINGW32 { 8 , "user" ,"USER" ,func_user ,PSSCAN_UIDGID }, { 8 , "group" ,"GROUP" ,func_group ,PSSCAN_UIDGID }, +#endif { 16 , "comm" ,"COMMAND",func_comm ,PSSCAN_COMM }, +#if !ENABLE_PLATFORM_MINGW32 { MAX_WIDTH , "args" ,"COMMAND",func_args ,PSSCAN_COMM }, +#endif { 5 , "pid" ,"PID" ,func_pid ,PSSCAN_PID }, +#if !ENABLE_PLATFORM_MINGW32 { 5 , "ppid" ,"PPID" ,func_ppid ,PSSCAN_PPID }, { 5 , "pgid" ,"PGID" ,func_pgid ,PSSCAN_PGID }, +#endif #if ENABLE_FEATURE_PS_TIME { sizeof("ELAPSED")-1, "etime" ,"ELAPSED",func_etime ,PSSCAN_START_TIME }, #endif @@ -320,10 +328,12 @@ static const ps_out_t out_spec[] = { #if ENABLE_FEATURE_PS_TIME { 6 , "time" ,"TIME" ,func_time ,PSSCAN_STIME | PSSCAN_UTIME }, #endif +#if !ENABLE_PLATFORM_MINGW32 { 6 , "tty" ,"TT" ,func_tty ,PSSCAN_TTY }, { 4 , "vsz" ,"VSZ" ,func_vsz ,PSSCAN_VSZ }, // Not mandated by POSIX, but useful: { 4 , "rss" ,"RSS" ,func_rss ,PSSCAN_RSS }, +#endif #if ENABLE_SELINUX { 35 , "label" ,"LABEL" ,func_label ,PSSCAN_CONTEXT }, #endif diff --git a/scripts/defconfig.mingw32 b/scripts/defconfig.mingw32 index 943c3f4ab..7f5cbc574 100644 --- a/scripts/defconfig.mingw32 +++ b/scripts/defconfig.mingw32 @@ -854,7 +854,7 @@ CONFIG_FEATURE_MIME_CHARSET="" # CONFIG_FEATURE_PIDOF_SINGLE is not set # CONFIG_FEATURE_PIDOF_OMIT is not set # CONFIG_PKILL is not set -# CONFIG_PS is not set +CONFIG_PS=y # CONFIG_FEATURE_PS_WIDE is not set # CONFIG_FEATURE_PS_TIME is not set # CONFIG_FEATURE_PS_ADDITIONAL_COLUMNS is not set -- cgit v1.2.3-55-g6feb From 507a5a689e7441782fcdbe96d88068dee9a3145e Mon Sep 17 00:00:00 2001 From: Nguyễn Thái Ngọc Duy Date: Wed, 15 Sep 2010 12:38:10 +1000 Subject: win32: process.c: implement kill(), SIGTERM only --- include/mingw.h | 2 +- win32/process.c | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/include/mingw.h b/include/mingw.h index 53a0a0d67..a43e8eca2 100644 --- a/include/mingw.h +++ b/include/mingw.h @@ -285,7 +285,7 @@ IMPL(getuid,int,1,void); int fcntl(int fd, int cmd, ...); #define fork() -1 IMPL(fsync,int,0,int fd UNUSED_PARAM); -NOIMPL(kill,pid_t pid UNUSED_PARAM, int sig UNUSED_PARAM); +int kill(pid_t pid, int sig); int link(const char *oldpath, const char *newpath); NOIMPL(mknod,const char *name UNUSED_PARAM, mode_t mode UNUSED_PARAM, dev_t device UNUSED_PARAM); int mingw_open (const char *filename, int oflags, ...); diff --git a/win32/process.c b/win32/process.c index 1b9b61878..cdd71182b 100644 --- a/win32/process.c +++ b/win32/process.c @@ -317,3 +317,20 @@ procps_status_t* FAST_FUNC procps_scan(procps_status_t* sp, int flags) strncpy(sp->comm, pe.szExeFile, COMM_LEN); return sp; } + +int kill(pid_t pid, int sig) +{ + HANDLE h; + + if (sig != SIGTERM) { + bb_error_msg("kill only supports SIGTERM"); + errno = ENOSYS; + return -1; + } + h = OpenProcess(PROCESS_TERMINATE, FALSE, pid); + if (h == NULL) + return -1; + if (TerminateProcess(h, 0) == 0) + return -1; + return 0; +} -- cgit v1.2.3-55-g6feb From 28c65da05b29f34f2ed3e35821ad8549eb143e19 Mon Sep 17 00:00:00 2001 From: Nguyễn Thái Ngọc Duy Date: Wed, 15 Sep 2010 12:52:19 +1000 Subject: win32: find_pid_by_name: skip argv checks as we do not have that info --- libbb/find_pid_by_name.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/libbb/find_pid_by_name.c b/libbb/find_pid_by_name.c index db823d05b..d4bea8ab5 100644 --- a/libbb/find_pid_by_name.c +++ b/libbb/find_pid_by_name.c @@ -56,6 +56,7 @@ static int comm_match(procps_status_t *p, const char *procName) * This can be crazily_long_script_name.sh! * The telltale sign is basename(argv[1]) == procName */ +#if !ENABLE_PLATFORM_MINGW32 if (!p->argv0) return 0; @@ -66,6 +67,7 @@ static int comm_match(procps_status_t *p, const char *procName) if (strcmp(bb_basename(argv1), procName) != 0) return 0; +#endif return 1; } @@ -88,10 +90,12 @@ pid_t* FAST_FUNC find_pid_by_name(const char *procName) pidList = xzalloc(sizeof(*pidList)); while ((p = procps_scan(p, PSSCAN_PID|PSSCAN_COMM|PSSCAN_ARGVN|PSSCAN_EXE))) { if (comm_match(p, procName) +#if !ENABLE_PLATFORM_MINGW32 /* or we require argv0 to match (essential for matching reexeced /proc/self/exe)*/ || (p->argv0 && strcmp(bb_basename(p->argv0), procName) == 0) /* or we require /proc/PID/exe link to match */ || (p->exe && strcmp(bb_basename(p->exe), procName) == 0) +#endif ) { pidList = xrealloc_vector(pidList, 2, i); pidList[i++] = p->pid; -- cgit v1.2.3-55-g6feb From 53c5d85398dd61c52a3f82e4aca2094115c201e3 Mon Sep 17 00:00:00 2001 From: Nguyễn Thái Ngọc Duy Date: Wed, 15 Sep 2010 12:52:31 +1000 Subject: win32: enable kill and killall --- procps/kill.c | 2 ++ scripts/defconfig.mingw32 | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/procps/kill.c b/procps/kill.c index b51d44a70..3da39f030 100644 --- a/procps/kill.c +++ b/procps/kill.c @@ -50,6 +50,7 @@ int kill_main(int argc, char **argv) goto do_it_now; } +#if !ENABLE_PLATFORM_MINGW32 /* The -l option, which prints out signal names. * Intended usage in shell: * echo "Died of SIG`kill -l $?`" @@ -115,6 +116,7 @@ int kill_main(int argc, char **argv) } arg = *++argv; argc--; +#endif /* ENABLE_PLATFORM_MINGW32 */ do_it_now: pid = getpid(); diff --git a/scripts/defconfig.mingw32 b/scripts/defconfig.mingw32 index 7f5cbc574..b60e05b48 100644 --- a/scripts/defconfig.mingw32 +++ b/scripts/defconfig.mingw32 @@ -845,8 +845,8 @@ CONFIG_FEATURE_MIME_CHARSET="" # CONFIG_SMEMCAP is not set # CONFIG_FREE is not set # CONFIG_FUSER is not set -# CONFIG_KILL is not set -# CONFIG_KILLALL is not set +CONFIG_KILL=y +CONFIG_KILLALL=y # CONFIG_KILLALL5 is not set # CONFIG_NMETER is not set # CONFIG_PGREP is not set -- cgit v1.2.3-55-g6feb From 3a55b6ad3a842eab971016dbebc0f32281d52fd7 Mon Sep 17 00:00:00 2001 From: Nguyễn Thái Ngọc Duy Date: Wed, 15 Sep 2010 16:16:42 +1000 Subject: win32: enable pidof --- scripts/defconfig.mingw32 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/scripts/defconfig.mingw32 b/scripts/defconfig.mingw32 index b60e05b48..a02600c83 100644 --- a/scripts/defconfig.mingw32 +++ b/scripts/defconfig.mingw32 @@ -850,9 +850,9 @@ CONFIG_KILLALL=y # CONFIG_KILLALL5 is not set # CONFIG_NMETER is not set # CONFIG_PGREP is not set -# CONFIG_PIDOF is not set -# CONFIG_FEATURE_PIDOF_SINGLE is not set -# CONFIG_FEATURE_PIDOF_OMIT is not set +CONFIG_PIDOF=y +CONFIG_FEATURE_PIDOF_SINGLE=y +CONFIG_FEATURE_PIDOF_OMIT=y # CONFIG_PKILL is not set CONFIG_PS=y # CONFIG_FEATURE_PS_WIDE is not set -- cgit v1.2.3-55-g6feb From 4fd7461aeaac63edb004696e722bf36e7f6f402c Mon Sep 17 00:00:00 2001 From: Nguyễn Thái Ngọc Duy Date: Wed, 15 Sep 2010 16:30:21 +1000 Subject: win32: enable pgrep --- scripts/defconfig.mingw32 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/defconfig.mingw32 b/scripts/defconfig.mingw32 index a02600c83..0234a4e57 100644 --- a/scripts/defconfig.mingw32 +++ b/scripts/defconfig.mingw32 @@ -849,7 +849,7 @@ CONFIG_KILL=y CONFIG_KILLALL=y # CONFIG_KILLALL5 is not set # CONFIG_NMETER is not set -# CONFIG_PGREP is not set +CONFIG_PGREP=y CONFIG_PIDOF=y CONFIG_FEATURE_PIDOF_SINGLE=y CONFIG_FEATURE_PIDOF_OMIT=y -- cgit v1.2.3-55-g6feb