From dd9f552bb1fee66f3155bbbb81c677def861ada0 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Fri, 1 Sep 2017 17:50:39 +0100 Subject: ps: add support for CPU and elapsed time columns It may be necessary to run ps as administrator to get information about processes belonging to other users. The code to detect GetTickCount64 at run-time was imported from Git for Windows. --- configs/mingw32_defconfig | 2 +- configs/mingw64_defconfig | 2 +- include/mingw.h | 7 +++++++ procps/ps.c | 7 ++++--- win32/lazyload.h | 43 +++++++++++++++++++++++++++++++++++++++++++ win32/mingw.c | 18 +++++++++++++++++- win32/process.c | 47 ++++++++++++++++++++++++++++++++++++++++++++++- 7 files changed, 119 insertions(+), 7 deletions(-) create mode 100644 win32/lazyload.h diff --git a/configs/mingw32_defconfig b/configs/mingw32_defconfig index abe56f619..3a30cf366 100644 --- a/configs/mingw32_defconfig +++ b/configs/mingw32_defconfig @@ -991,7 +991,7 @@ CONFIG_FEATURE_PIDOF_OMIT=y CONFIG_PS=y # CONFIG_FEATURE_PS_WIDE is not set # CONFIG_FEATURE_PS_LONG is not set -# CONFIG_FEATURE_PS_TIME is not set +CONFIG_FEATURE_PS_TIME=y # CONFIG_FEATURE_PS_UNUSUAL_SYSTEMS is not set # CONFIG_FEATURE_PS_ADDITIONAL_COLUMNS is not set # CONFIG_PSTREE is not set diff --git a/configs/mingw64_defconfig b/configs/mingw64_defconfig index 286846f95..c474c13ee 100644 --- a/configs/mingw64_defconfig +++ b/configs/mingw64_defconfig @@ -991,7 +991,7 @@ CONFIG_FEATURE_PIDOF_OMIT=y CONFIG_PS=y # CONFIG_FEATURE_PS_WIDE is not set # CONFIG_FEATURE_PS_LONG is not set -# CONFIG_FEATURE_PS_TIME is not set +CONFIG_FEATURE_PS_TIME=y # CONFIG_FEATURE_PS_UNUSUAL_SYSTEMS is not set # CONFIG_FEATURE_PS_ADDITIONAL_COLUMNS is not set # CONFIG_PSTREE is not set diff --git a/include/mingw.h b/include/mingw.h index dd676bf2c..87abd077b 100644 --- a/include/mingw.h +++ b/include/mingw.h @@ -374,6 +374,10 @@ clock_t times(struct tms *buf); #define _SC_CLK_TCK 2 +#define TICKS_PER_SECOND 100 +#define MS_PER_TICK 10 +#define HNSEC_PER_TICK 100000 + IMPL(alarm,unsigned int,0,unsigned int seconds UNUSED_PARAM); IMPL(chown,int,0,const char *path UNUSED_PARAM, uid_t uid UNUSED_PARAM, gid_t gid UNUSED_PARAM); NOIMPL(chroot,const char *root UNUSED_PARAM); @@ -475,3 +479,6 @@ int has_exe_suffix(const char *p); char *file_is_win32_executable(const char *p); int err_win_to_posix(DWORD winerr); + +ULONGLONG CompatGetTickCount64(void); +#define GetTickCount64 CompatGetTickCount64 diff --git a/procps/ps.c b/procps/ps.c index a8541e6cf..de062fe8b 100644 --- a/procps/ps.c +++ b/procps/ps.c @@ -35,7 +35,6 @@ //config: bool "Enable -o time and -o etime specifiers" //config: default y //config: depends on (PS || MINIPS) && DESKTOP -//config: select PLATFORM_LINUX //config: //config:config FEATURE_PS_UNUSUAL_SYSTEMS //config: bool "Support Linux prior to 2.4.0 and non-ELF systems" @@ -126,7 +125,7 @@ static unsigned long get_uptime(void) if (sysinfo(&info) < 0) return 0; return info.uptime; -#elif 1 +#elif !ENABLE_PLATFORM_MINGW32 unsigned long uptime; char buf[sizeof(uptime)*3 + 2]; /* /proc/uptime is "UPTIME_SEC.NN IDLE_SEC.NN\n" @@ -137,6 +136,8 @@ static unsigned long get_uptime(void) buf[sizeof(buf)-1] = '\0'; sscanf(buf, "%lu", &uptime); return uptime; +#elif ENABLE_PLATFORM_MINGW32 + return GetTickCount64()/1000; #else struct timespec ts; if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0) @@ -556,7 +557,7 @@ static void format_process(const procps_status_t *ps) # 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,ppid,comm") +# define DEFAULT_O_STR ("pid,ppid" IF_FEATURE_PS_TIME(",time,etime") ",comm") #else # define DEFAULT_O_STR ("pid,user" IF_FEATURE_PS_TIME(",time") ",args") #endif diff --git a/win32/lazyload.h b/win32/lazyload.h new file mode 100644 index 000000000..9d1a05550 --- /dev/null +++ b/win32/lazyload.h @@ -0,0 +1,43 @@ +#ifndef LAZYLOAD_H +#define LAZYLOAD_H + +/* simplify loading of DLL functions */ + +struct proc_addr { + const char *const dll; + const char *const function; + FARPROC pfunction; + unsigned initialized : 1; +}; + +/* Declares a function to be loaded dynamically from a DLL. */ +#define DECLARE_PROC_ADDR(dll, rettype, function, ...) \ + static struct proc_addr proc_addr_##function = \ + { #dll, #function, NULL, 0 }; \ + static rettype (WINAPI *function)(__VA_ARGS__) + +/* + * Loads a function from a DLL (once-only). + * Returns non-NULL function pointer on success. + * Returns NULL + errno == ENOSYS on failure. + */ +#define INIT_PROC_ADDR(function) (function = get_proc_addr(&proc_addr_##function)) + +static inline void *get_proc_addr(struct proc_addr *proc) +{ + /* only do this once */ + if (!proc->initialized) { + HANDLE hnd; + proc->initialized = 1; + hnd = LoadLibraryExA(proc->dll, NULL, + LOAD_LIBRARY_SEARCH_SYSTEM32); + if (hnd) + proc->pfunction = GetProcAddress(hnd, proc->function); + } + /* set ENOSYS if DLL or function was not found */ + if (!proc->pfunction) + errno = ENOSYS; + return proc->pfunction; +} + +#endif diff --git a/win32/mingw.c b/win32/mingw.c index 7f37eadda..afcb6c6d2 100644 --- a/win32/mingw.c +++ b/win32/mingw.c @@ -707,7 +707,7 @@ int getlogin_r(char *buf, size_t len) long sysconf(int name) { if ( name == _SC_CLK_TCK ) { - return 100; + return TICKS_PER_SECOND; } errno = EINVAL; return -1; @@ -1064,3 +1064,19 @@ off_t mingw_lseek(int fd, off_t offset, int whence) } return _lseeki64(fd, offset, whence); } + +#if ENABLE_FEATURE_PS_TIME || ENABLE_FEATURE_PS_LONG +#undef GetTickCount64 +#include "lazyload.h" + +ULONGLONG CompatGetTickCount64(void) +{ + DECLARE_PROC_ADDR(kernel32.dll, ULONGLONG, GetTickCount64, void); + + if (!INIT_PROC_ADDR(GetTickCount64)) { + return (ULONGLONG)GetTickCount(); + } + + return GetTickCount64(); +} +#endif diff --git a/win32/process.c b/win32/process.c index c0ff78105..f88a4898c 100644 --- a/win32/process.c +++ b/win32/process.c @@ -334,8 +334,18 @@ mingw_execv(const char *cmd, char *const *argv) return mingw_execve(cmd, argv, environ); } +static inline long long filetime_to_ticks(const FILETIME *ft) +{ + return (((long long)ft->dwHighDateTime << 32) + ft->dwLowDateTime)/ + HNSEC_PER_TICK; +} + /* POSIX version in libbb/procps.c */ -procps_status_t* FAST_FUNC procps_scan(procps_status_t* sp, int flags UNUSED_PARAM) +procps_status_t* FAST_FUNC procps_scan(procps_status_t* sp, int flags +#if !ENABLE_FEATURE_PS_TIME && !ENABLE_FEATURE_PS_LONG +UNUSED_PARAM +#endif +) { PROCESSENTRY32 pe; @@ -361,6 +371,41 @@ procps_status_t* FAST_FUNC procps_scan(procps_status_t* sp, int flags UNUSED_PAR } } +#if ENABLE_FEATURE_PS_TIME || ENABLE_FEATURE_PS_LONG + if (flags & (PSSCAN_STIME|PSSCAN_UTIME|PSSCAN_START_TIME)) { + HANDLE proc; + FILETIME crTime, exTime, keTime, usTime; + + if ((proc=OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, + FALSE, pe.th32ProcessID))) { + if (GetProcessTimes(proc, &crTime, &exTime, &keTime, &usTime)) { + /* times in ticks since 1 January 1601 */ + static long long boot_time = 0; + long long start_time; + + if (boot_time == 0) { + long long ticks_since_boot; + FILETIME now; + + ticks_since_boot = GetTickCount64()/MS_PER_TICK; + GetSystemTimeAsFileTime(&now); + boot_time = filetime_to_ticks(&now) - ticks_since_boot; + } + + start_time = filetime_to_ticks(&crTime); + sp->start_time = (unsigned long)(start_time - boot_time); + + sp->stime = (unsigned long)filetime_to_ticks(&keTime); + sp->utime = (unsigned long)filetime_to_ticks(&usTime); + } + else { + sp->start_time = sp->stime = sp->utime = 0; + } + CloseHandle(proc); + } + } +#endif + sp->pid = pe.th32ProcessID; sp->ppid = pe.th32ParentProcessID; safe_strncpy(sp->comm, pe.szExeFile, COMM_LEN); -- cgit v1.2.3-55-g6feb