diff options
author | Ron Yorston <rmy@pobox.com> | 2017-09-01 17:50:39 +0100 |
---|---|---|
committer | Ron Yorston <rmy@pobox.com> | 2017-09-01 18:00:01 +0100 |
commit | dd9f552bb1fee66f3155bbbb81c677def861ada0 (patch) | |
tree | 21c7f1f333a2a58361c813311188b286ea803c9c /win32/process.c | |
parent | ec3ecac715b299766c101be92caf8b7cffb2a8b5 (diff) | |
download | busybox-w32-dd9f552bb1fee66f3155bbbb81c677def861ada0.tar.gz busybox-w32-dd9f552bb1fee66f3155bbbb81c677def861ada0.tar.bz2 busybox-w32-dd9f552bb1fee66f3155bbbb81c677def861ada0.zip |
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.
Diffstat (limited to 'win32/process.c')
-rw-r--r-- | win32/process.c | 47 |
1 files changed, 46 insertions, 1 deletions
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) | |||
334 | return mingw_execve(cmd, argv, environ); | 334 | return mingw_execve(cmd, argv, environ); |
335 | } | 335 | } |
336 | 336 | ||
337 | static inline long long filetime_to_ticks(const FILETIME *ft) | ||
338 | { | ||
339 | return (((long long)ft->dwHighDateTime << 32) + ft->dwLowDateTime)/ | ||
340 | HNSEC_PER_TICK; | ||
341 | } | ||
342 | |||
337 | /* POSIX version in libbb/procps.c */ | 343 | /* POSIX version in libbb/procps.c */ |
338 | procps_status_t* FAST_FUNC procps_scan(procps_status_t* sp, int flags UNUSED_PARAM) | 344 | procps_status_t* FAST_FUNC procps_scan(procps_status_t* sp, int flags |
345 | #if !ENABLE_FEATURE_PS_TIME && !ENABLE_FEATURE_PS_LONG | ||
346 | UNUSED_PARAM | ||
347 | #endif | ||
348 | ) | ||
339 | { | 349 | { |
340 | PROCESSENTRY32 pe; | 350 | PROCESSENTRY32 pe; |
341 | 351 | ||
@@ -361,6 +371,41 @@ procps_status_t* FAST_FUNC procps_scan(procps_status_t* sp, int flags UNUSED_PAR | |||
361 | } | 371 | } |
362 | } | 372 | } |
363 | 373 | ||
374 | #if ENABLE_FEATURE_PS_TIME || ENABLE_FEATURE_PS_LONG | ||
375 | if (flags & (PSSCAN_STIME|PSSCAN_UTIME|PSSCAN_START_TIME)) { | ||
376 | HANDLE proc; | ||
377 | FILETIME crTime, exTime, keTime, usTime; | ||
378 | |||
379 | if ((proc=OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, | ||
380 | FALSE, pe.th32ProcessID))) { | ||
381 | if (GetProcessTimes(proc, &crTime, &exTime, &keTime, &usTime)) { | ||
382 | /* times in ticks since 1 January 1601 */ | ||
383 | static long long boot_time = 0; | ||
384 | long long start_time; | ||
385 | |||
386 | if (boot_time == 0) { | ||
387 | long long ticks_since_boot; | ||
388 | FILETIME now; | ||
389 | |||
390 | ticks_since_boot = GetTickCount64()/MS_PER_TICK; | ||
391 | GetSystemTimeAsFileTime(&now); | ||
392 | boot_time = filetime_to_ticks(&now) - ticks_since_boot; | ||
393 | } | ||
394 | |||
395 | start_time = filetime_to_ticks(&crTime); | ||
396 | sp->start_time = (unsigned long)(start_time - boot_time); | ||
397 | |||
398 | sp->stime = (unsigned long)filetime_to_ticks(&keTime); | ||
399 | sp->utime = (unsigned long)filetime_to_ticks(&usTime); | ||
400 | } | ||
401 | else { | ||
402 | sp->start_time = sp->stime = sp->utime = 0; | ||
403 | } | ||
404 | CloseHandle(proc); | ||
405 | } | ||
406 | } | ||
407 | #endif | ||
408 | |||
364 | sp->pid = pe.th32ProcessID; | 409 | sp->pid = pe.th32ProcessID; |
365 | sp->ppid = pe.th32ParentProcessID; | 410 | sp->ppid = pe.th32ParentProcessID; |
366 | safe_strncpy(sp->comm, pe.szExeFile, COMM_LEN); | 411 | safe_strncpy(sp->comm, pe.szExeFile, COMM_LEN); |