Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | ef8b6c7 | 1999-10-20 08:05:35 +0000 | [diff] [blame] | 2 | /* |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 3 | * Mini ps implementation(s) for busybox |
| 4 | * |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
Denis Vlasenko | 0696b8a | 2007-06-06 07:40:16 +0000 | [diff] [blame] | 6 | * Fix for SELinux Support:(c)2007 Hiroshi Shinji <shiroshi@my.email.ne.jp> |
Denis Vlasenko | c85bfca | 2008-02-03 22:04:19 +0000 | [diff] [blame] | 7 | * (c)2007 Yuichi Nakamura <ynakam@hitachisoft.jp> |
Eric Andersen | ef8b6c7 | 1999-10-20 08:05:35 +0000 | [diff] [blame] | 8 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 9 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 10 | */ |
Denys Vlasenko | f8f81ed | 2016-11-23 06:23:44 +0100 | [diff] [blame] | 11 | //config:config PS |
Denys Vlasenko | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame] | 12 | //config: bool "ps (11 kb)" |
Denys Vlasenko | f8f81ed | 2016-11-23 06:23:44 +0100 | [diff] [blame] | 13 | //config: default y |
| 14 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 15 | //config: ps gives a snapshot of the current processes. |
Denys Vlasenko | f8f81ed | 2016-11-23 06:23:44 +0100 | [diff] [blame] | 16 | //config: |
| 17 | //config:config FEATURE_PS_WIDE |
Denys Vlasenko | 00c1811 | 2017-08-05 22:25:00 +0200 | [diff] [blame] | 18 | //config: bool "Enable wide output (-w)" |
Denys Vlasenko | f8f81ed | 2016-11-23 06:23:44 +0100 | [diff] [blame] | 19 | //config: default y |
Kang-Che Sung | 753c404 | 2017-08-19 14:18:27 +0800 | [diff] [blame] | 20 | //config: depends on (PS || MINIPS) && !DESKTOP |
Denys Vlasenko | f8f81ed | 2016-11-23 06:23:44 +0100 | [diff] [blame] | 21 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 22 | //config: Support argument 'w' for wide output. |
| 23 | //config: If given once, 132 chars are printed, and if given more |
| 24 | //config: than once, the length is unlimited. |
Denys Vlasenko | f8f81ed | 2016-11-23 06:23:44 +0100 | [diff] [blame] | 25 | //config: |
| 26 | //config:config FEATURE_PS_LONG |
Denys Vlasenko | 00c1811 | 2017-08-05 22:25:00 +0200 | [diff] [blame] | 27 | //config: bool "Enable long output (-l)" |
Denys Vlasenko | f8f81ed | 2016-11-23 06:23:44 +0100 | [diff] [blame] | 28 | //config: default y |
Kang-Che Sung | 753c404 | 2017-08-19 14:18:27 +0800 | [diff] [blame] | 29 | //config: depends on (PS || MINIPS) && !DESKTOP |
Denys Vlasenko | f8f81ed | 2016-11-23 06:23:44 +0100 | [diff] [blame] | 30 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 31 | //config: Support argument 'l' for long output. |
| 32 | //config: Adds fields PPID, RSS, START, TIME & TTY |
Denys Vlasenko | f8f81ed | 2016-11-23 06:23:44 +0100 | [diff] [blame] | 33 | //config: |
| 34 | //config:config FEATURE_PS_TIME |
Denys Vlasenko | 00c1811 | 2017-08-05 22:25:00 +0200 | [diff] [blame] | 35 | //config: bool "Enable -o time and -o etime specifiers" |
Denys Vlasenko | f8f81ed | 2016-11-23 06:23:44 +0100 | [diff] [blame] | 36 | //config: default y |
Kang-Che Sung | 753c404 | 2017-08-19 14:18:27 +0800 | [diff] [blame] | 37 | //config: depends on (PS || MINIPS) && DESKTOP |
Denys Vlasenko | f8f81ed | 2016-11-23 06:23:44 +0100 | [diff] [blame] | 38 | //config: select PLATFORM_LINUX |
Denys Vlasenko | f8f81ed | 2016-11-23 06:23:44 +0100 | [diff] [blame] | 39 | //config: |
| 40 | //config:config FEATURE_PS_UNUSUAL_SYSTEMS |
| 41 | //config: bool "Support Linux prior to 2.4.0 and non-ELF systems" |
| 42 | //config: default n |
| 43 | //config: depends on FEATURE_PS_TIME |
| 44 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 45 | //config: Include support for measuring HZ on old kernels and non-ELF systems |
| 46 | //config: (if you are on Linux 2.4.0+ and use ELF, you don't need this) |
Kang-Che Sung | 753c404 | 2017-08-19 14:18:27 +0800 | [diff] [blame] | 47 | //config: |
| 48 | //config:config FEATURE_PS_ADDITIONAL_COLUMNS |
| 49 | //config: bool "Enable -o rgroup, -o ruser, -o nice specifiers" |
| 50 | //config: default y |
| 51 | //config: depends on (PS || MINIPS) && DESKTOP |
Denys Vlasenko | f8f81ed | 2016-11-23 06:23:44 +0100 | [diff] [blame] | 52 | |
Denys Vlasenko | ab77e81 | 2017-08-18 19:15:29 +0200 | [diff] [blame] | 53 | // APPLET_NOEXEC:name main location suid_type help |
| 54 | //applet:IF_PS( APPLET_NOEXEC(ps, ps, BB_DIR_BIN, BB_SUID_DROP, ps)) |
| 55 | //applet:IF_MINIPS(APPLET_NOEXEC(minips, ps, BB_DIR_BIN, BB_SUID_DROP, ps)) |
Denys Vlasenko | f8f81ed | 2016-11-23 06:23:44 +0100 | [diff] [blame] | 56 | |
| 57 | //kbuild:lib-$(CONFIG_PS) += ps.o |
Denys Vlasenko | ab77e81 | 2017-08-18 19:15:29 +0200 | [diff] [blame] | 58 | //kbuild:lib-$(CONFIG_MINIPS) += ps.o |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 59 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 60 | //usage:#if ENABLE_DESKTOP |
| 61 | //usage: |
| 62 | //usage:#define ps_trivial_usage |
| 63 | //usage: "[-o COL1,COL2=HEADER]" IF_FEATURE_SHOW_THREADS(" [-T]") |
| 64 | //usage:#define ps_full_usage "\n\n" |
| 65 | //usage: "Show list of processes\n" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 66 | //usage: "\n -o COL1,COL2=HEADER Select columns for display" |
| 67 | //usage: IF_FEATURE_SHOW_THREADS( |
| 68 | //usage: "\n -T Show threads" |
| 69 | //usage: ) |
| 70 | //usage: |
| 71 | //usage:#else /* !ENABLE_DESKTOP */ |
| 72 | //usage: |
| 73 | //usage:#if !ENABLE_SELINUX && !ENABLE_FEATURE_PS_WIDE |
| 74 | //usage:#define USAGE_PS "\nThis version of ps accepts no options" |
| 75 | //usage:#else |
Denys Vlasenko | d7efa06 | 2011-06-05 15:07:31 +0200 | [diff] [blame] | 76 | //usage:#define USAGE_PS "" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 77 | //usage:#endif |
| 78 | //usage: |
| 79 | //usage:#define ps_trivial_usage |
| 80 | //usage: "" |
| 81 | //usage:#define ps_full_usage "\n\n" |
| 82 | //usage: "Show list of processes\n" |
| 83 | //usage: USAGE_PS |
| 84 | //usage: IF_SELINUX( |
| 85 | //usage: "\n -Z Show selinux context" |
| 86 | //usage: ) |
| 87 | //usage: IF_FEATURE_PS_WIDE( |
| 88 | //usage: "\n w Wide output" |
| 89 | //usage: ) |
Denys Vlasenko | 8d9ac30 | 2011-09-26 02:56:08 +0200 | [diff] [blame] | 90 | //usage: IF_FEATURE_PS_LONG( |
| 91 | //usage: "\n l Long output" |
| 92 | //usage: ) |
| 93 | //usage: IF_FEATURE_SHOW_THREADS( |
| 94 | //usage: "\n T Show threads" |
| 95 | //usage: ) |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 96 | //usage: |
| 97 | //usage:#endif /* ENABLE_DESKTOP */ |
| 98 | //usage: |
| 99 | //usage:#define ps_example_usage |
| 100 | //usage: "$ ps\n" |
| 101 | //usage: " PID Uid Gid State Command\n" |
| 102 | //usage: " 1 root root S init\n" |
| 103 | //usage: " 2 root root S [kflushd]\n" |
| 104 | //usage: " 3 root root S [kupdate]\n" |
| 105 | //usage: " 4 root root S [kpiod]\n" |
| 106 | //usage: " 5 root root S [kswapd]\n" |
| 107 | //usage: " 742 andersen andersen S [bash]\n" |
| 108 | //usage: " 743 andersen andersen S -bash\n" |
| 109 | //usage: " 745 root root S [getty]\n" |
| 110 | //usage: " 2990 andersen andersen R ps\n" |
| 111 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 112 | #include "libbb.h" |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 113 | #include "common_bufsiz.h" |
Denys Vlasenko | 8d9ac30 | 2011-09-26 02:56:08 +0200 | [diff] [blame] | 114 | #ifdef __linux__ |
| 115 | # include <sys/sysinfo.h> |
| 116 | #endif |
Erik Andersen | 2ac2fae | 2000-03-07 23:32:17 +0000 | [diff] [blame] | 117 | |
Denis Vlasenko | 7ce7594 | 2007-07-25 17:27:58 +0000 | [diff] [blame] | 118 | /* Absolute maximum on output line length */ |
| 119 | enum { MAX_WIDTH = 2*1024 }; |
| 120 | |
Michael Tokarev | ac42e3d | 2012-06-13 02:51:56 +0200 | [diff] [blame] | 121 | #if ENABLE_FEATURE_PS_TIME || ENABLE_FEATURE_PS_LONG |
Denys Vlasenko | 6aab061 | 2013-01-22 10:07:23 +0100 | [diff] [blame] | 122 | static unsigned long get_uptime(void) |
Michael Tokarev | ac42e3d | 2012-06-13 02:51:56 +0200 | [diff] [blame] | 123 | { |
| 124 | #ifdef __linux__ |
| 125 | struct sysinfo info; |
| 126 | if (sysinfo(&info) < 0) |
| 127 | return 0; |
| 128 | return info.uptime; |
| 129 | #elif 1 |
Denys Vlasenko | 6aab061 | 2013-01-22 10:07:23 +0100 | [diff] [blame] | 130 | unsigned long uptime; |
| 131 | char buf[sizeof(uptime)*3 + 2]; |
| 132 | /* /proc/uptime is "UPTIME_SEC.NN IDLE_SEC.NN\n" |
| 133 | * (where IDLE is cumulative over all CPUs) |
| 134 | */ |
Michael Tokarev | ac42e3d | 2012-06-13 02:51:56 +0200 | [diff] [blame] | 135 | if (open_read_close("/proc/uptime", buf, sizeof(buf)) <= 0) |
Denys Vlasenko | 6aab061 | 2013-01-22 10:07:23 +0100 | [diff] [blame] | 136 | bb_perror_msg_and_die("can't read '%s'", "/proc/uptime"); |
Michael Tokarev | ac42e3d | 2012-06-13 02:51:56 +0200 | [diff] [blame] | 137 | buf[sizeof(buf)-1] = '\0'; |
Denys Vlasenko | 6aab061 | 2013-01-22 10:07:23 +0100 | [diff] [blame] | 138 | sscanf(buf, "%lu", &uptime); |
Michael Tokarev | ac42e3d | 2012-06-13 02:51:56 +0200 | [diff] [blame] | 139 | return uptime; |
| 140 | #else |
| 141 | struct timespec ts; |
| 142 | if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0) |
| 143 | return 0; |
| 144 | return ts.tv_sec; |
| 145 | #endif |
| 146 | } |
| 147 | #endif |
| 148 | |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 149 | #if ENABLE_DESKTOP |
Denys Vlasenko | 099e528 | 2011-02-02 00:59:35 +0100 | [diff] [blame] | 150 | /* TODO: |
| 151 | * http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ps.html |
| 152 | * specifies (for XSI-conformant systems) following default columns |
| 153 | * (l and f mark columns shown with -l and -f respectively): |
| 154 | * F l Flags (octal and additive) associated with the process (??) |
| 155 | * S l The state of the process |
| 156 | * UID f,l The user ID; the login name is printed with -f |
| 157 | * PID The process ID |
| 158 | * PPID f,l The parent process |
| 159 | * C f,l Processor utilization |
| 160 | * PRI l The priority of the process; higher numbers mean lower priority |
| 161 | * NI l Nice value |
| 162 | * ADDR l The address of the process |
| 163 | * SZ l The size in blocks of the core image of the process |
| 164 | * WCHAN l The event for which the process is waiting or sleeping |
| 165 | * STIME f Starting time of the process |
| 166 | * TTY The controlling terminal for the process |
| 167 | * TIME The cumulative execution time for the process |
| 168 | * CMD The command name; the full command line is shown with -f |
| 169 | */ |
Denis Vlasenko | 5fee2e1 | 2008-01-05 03:26:41 +0000 | [diff] [blame] | 170 | typedef struct { |
| 171 | uint16_t width; |
Denys Vlasenko | fca70a8 | 2009-07-03 22:16:17 +0200 | [diff] [blame] | 172 | char name6[6]; |
Denis Vlasenko | 5fee2e1 | 2008-01-05 03:26:41 +0000 | [diff] [blame] | 173 | const char *header; |
| 174 | void (*f)(char *buf, int size, const procps_status_t *ps); |
| 175 | int ps_flags; |
| 176 | } ps_out_t; |
| 177 | |
| 178 | struct globals { |
| 179 | ps_out_t* out; |
| 180 | int out_cnt; |
| 181 | int print_header; |
| 182 | int need_flags; |
| 183 | char *buffer; |
| 184 | unsigned terminal_width; |
| 185 | #if ENABLE_FEATURE_PS_TIME |
Denys Vlasenko | 1fd8e66 | 2017-08-10 14:10:56 +0200 | [diff] [blame] | 186 | # if ENABLE_FEATURE_PS_UNUSUAL_SYSTEMS || !defined(__linux__) |
Denis Vlasenko | 5fee2e1 | 2008-01-05 03:26:41 +0000 | [diff] [blame] | 187 | unsigned kernel_HZ; |
Denys Vlasenko | 1fd8e66 | 2017-08-10 14:10:56 +0200 | [diff] [blame] | 188 | # endif |
Denys Vlasenko | 17d4436 | 2013-01-26 14:21:15 +0100 | [diff] [blame] | 189 | unsigned long seconds_since_boot; |
Denis Vlasenko | 5fee2e1 | 2008-01-05 03:26:41 +0000 | [diff] [blame] | 190 | #endif |
Denys Vlasenko | 98a4c7c | 2010-02-04 15:00:15 +0100 | [diff] [blame] | 191 | } FIX_ALIASING; |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 192 | #define G (*(struct globals*)bb_common_bufsiz1) |
Denis Vlasenko | 5fee2e1 | 2008-01-05 03:26:41 +0000 | [diff] [blame] | 193 | #define out (G.out ) |
| 194 | #define out_cnt (G.out_cnt ) |
| 195 | #define print_header (G.print_header ) |
| 196 | #define need_flags (G.need_flags ) |
| 197 | #define buffer (G.buffer ) |
| 198 | #define terminal_width (G.terminal_width ) |
Denys Vlasenko | 47cfbf3 | 2016-04-21 18:18:48 +0200 | [diff] [blame] | 199 | #define INIT_G() do { setup_common_bufsiz(); } while (0) |
Denis Vlasenko | 5fee2e1 | 2008-01-05 03:26:41 +0000 | [diff] [blame] | 200 | |
| 201 | #if ENABLE_FEATURE_PS_TIME |
Denys Vlasenko | 1fd8e66 | 2017-08-10 14:10:56 +0200 | [diff] [blame] | 202 | # if ENABLE_FEATURE_PS_UNUSUAL_SYSTEMS || !defined(__linux__) |
| 203 | # define get_kernel_HZ() (G.kernel_HZ) |
Denys Vlasenko | 00c1811 | 2017-08-05 22:25:00 +0200 | [diff] [blame] | 204 | # else |
Denys Vlasenko | 1fd8e66 | 2017-08-10 14:10:56 +0200 | [diff] [blame] | 205 | /* non-ancient Linux standardized on 100 for "times" freq */ |
| 206 | # define get_kernel_HZ() ((unsigned)100) |
Denys Vlasenko | 00c1811 | 2017-08-05 22:25:00 +0200 | [diff] [blame] | 207 | # endif |
Denis Vlasenko | 5fee2e1 | 2008-01-05 03:26:41 +0000 | [diff] [blame] | 208 | #endif |
| 209 | |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 210 | /* Print value to buf, max size+1 chars (including trailing '\0') */ |
| 211 | |
Denis Vlasenko | 769d1e0 | 2007-01-22 23:04:27 +0000 | [diff] [blame] | 212 | static void func_user(char *buf, int size, const procps_status_t *ps) |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 213 | { |
Denis Vlasenko | 5de754a | 2008-02-02 16:43:51 +0000 | [diff] [blame] | 214 | #if 1 |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 215 | safe_strncpy(buf, get_cached_username(ps->uid), size+1); |
Denis Vlasenko | 5de754a | 2008-02-02 16:43:51 +0000 | [diff] [blame] | 216 | #else |
| 217 | /* "compatible" version, but it's larger */ |
| 218 | /* procps 2.18 shows numeric UID if name overflows the field */ |
| 219 | /* TODO: get_cached_username() returns numeric string if |
| 220 | * user has no passwd record, we will display it |
| 221 | * left-justified here; too long usernames are shown |
| 222 | * as _right-justified_ IDs. Is it worth fixing? */ |
| 223 | const char *user = get_cached_username(ps->uid); |
| 224 | if (strlen(user) <= size) |
Denis Vlasenko | c85bfca | 2008-02-03 22:04:19 +0000 | [diff] [blame] | 225 | safe_strncpy(buf, user, size+1); |
Denis Vlasenko | 5de754a | 2008-02-02 16:43:51 +0000 | [diff] [blame] | 226 | else |
| 227 | sprintf(buf, "%*u", size, (unsigned)ps->uid); |
| 228 | #endif |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 229 | } |
| 230 | |
Denys Vlasenko | fca70a8 | 2009-07-03 22:16:17 +0200 | [diff] [blame] | 231 | static void func_group(char *buf, int size, const procps_status_t *ps) |
| 232 | { |
| 233 | safe_strncpy(buf, get_cached_groupname(ps->gid), size+1); |
| 234 | } |
| 235 | |
Denis Vlasenko | 769d1e0 | 2007-01-22 23:04:27 +0000 | [diff] [blame] | 236 | static void func_comm(char *buf, int size, const procps_status_t *ps) |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 237 | { |
| 238 | safe_strncpy(buf, ps->comm, size+1); |
| 239 | } |
| 240 | |
Denys Vlasenko | 12ac628 | 2011-06-18 15:49:47 +0200 | [diff] [blame] | 241 | static void func_state(char *buf, int size, const procps_status_t *ps) |
Denys Vlasenko | 099e528 | 2011-02-02 00:59:35 +0100 | [diff] [blame] | 242 | { |
| 243 | safe_strncpy(buf, ps->state, size+1); |
| 244 | } |
| 245 | |
Denis Vlasenko | 769d1e0 | 2007-01-22 23:04:27 +0000 | [diff] [blame] | 246 | static void func_args(char *buf, int size, const procps_status_t *ps) |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 247 | { |
Denys Vlasenko | 3a0f6f2 | 2009-09-12 00:15:34 +0200 | [diff] [blame] | 248 | read_cmdline(buf, size+1, ps->pid, ps->comm); |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Denis Vlasenko | 769d1e0 | 2007-01-22 23:04:27 +0000 | [diff] [blame] | 251 | static void func_pid(char *buf, int size, const procps_status_t *ps) |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 252 | { |
Denis Vlasenko | 516a0ca | 2007-04-19 14:46:14 +0000 | [diff] [blame] | 253 | sprintf(buf, "%*u", size, ps->pid); |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Denis Vlasenko | 769d1e0 | 2007-01-22 23:04:27 +0000 | [diff] [blame] | 256 | static void func_ppid(char *buf, int size, const procps_status_t *ps) |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 257 | { |
Denis Vlasenko | 516a0ca | 2007-04-19 14:46:14 +0000 | [diff] [blame] | 258 | sprintf(buf, "%*u", size, ps->ppid); |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Denis Vlasenko | 769d1e0 | 2007-01-22 23:04:27 +0000 | [diff] [blame] | 261 | static void func_pgid(char *buf, int size, const procps_status_t *ps) |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 262 | { |
Denis Vlasenko | 516a0ca | 2007-04-19 14:46:14 +0000 | [diff] [blame] | 263 | sprintf(buf, "%*u", size, ps->pgid); |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 264 | } |
| 265 | |
Denys Vlasenko | 5d725fd | 2017-07-29 02:20:17 +0200 | [diff] [blame] | 266 | static void func_sid(char *buf, int size, const procps_status_t *ps) |
| 267 | { |
| 268 | sprintf(buf, "%*u", size, ps->sid); |
| 269 | } |
| 270 | |
Denis Vlasenko | fc1e995 | 2007-08-29 18:23:36 +0000 | [diff] [blame] | 271 | static void put_lu(char *buf, int size, unsigned long u) |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 272 | { |
Denis Vlasenko | 9b4c072 | 2008-01-07 16:51:25 +0000 | [diff] [blame] | 273 | char buf4[5]; |
Denis Vlasenko | 56ea65c | 2008-01-06 03:26:53 +0000 | [diff] [blame] | 274 | |
| 275 | /* see http://en.wikipedia.org/wiki/Tera */ |
Denys Vlasenko | a407cf7 | 2013-09-06 12:53:14 +0200 | [diff] [blame] | 276 | smart_ulltoa4(u, buf4, " mgtpezy")[0] = '\0'; |
Denis Vlasenko | 9b4c072 | 2008-01-07 16:51:25 +0000 | [diff] [blame] | 277 | sprintf(buf, "%.*s", size, buf4); |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 278 | } |
| 279 | |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 280 | static void func_vsz(char *buf, int size, const procps_status_t *ps) |
| 281 | { |
Denis Vlasenko | fc1e995 | 2007-08-29 18:23:36 +0000 | [diff] [blame] | 282 | put_lu(buf, size, ps->vsz); |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Denis Vlasenko | 516a0ca | 2007-04-19 14:46:14 +0000 | [diff] [blame] | 285 | static void func_rss(char *buf, int size, const procps_status_t *ps) |
| 286 | { |
Denis Vlasenko | fc1e995 | 2007-08-29 18:23:36 +0000 | [diff] [blame] | 287 | put_lu(buf, size, ps->rss); |
Denis Vlasenko | 516a0ca | 2007-04-19 14:46:14 +0000 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | static void func_tty(char *buf, int size, const procps_status_t *ps) |
| 291 | { |
Denis Vlasenko | 31789a8 | 2007-06-19 14:43:17 +0000 | [diff] [blame] | 292 | buf[0] = '?'; |
| 293 | buf[1] = '\0'; |
| 294 | if (ps->tty_major) /* tty field of "0" means "no tty" */ |
| 295 | snprintf(buf, size+1, "%u,%u", ps->tty_major, ps->tty_minor); |
Denis Vlasenko | 516a0ca | 2007-04-19 14:46:14 +0000 | [diff] [blame] | 296 | } |
Denis Vlasenko | 0696b8a | 2007-06-06 07:40:16 +0000 | [diff] [blame] | 297 | |
Denys Vlasenko | fca70a8 | 2009-07-03 22:16:17 +0200 | [diff] [blame] | 298 | #if ENABLE_FEATURE_PS_ADDITIONAL_COLUMNS |
Denys Vlasenko | fca70a8 | 2009-07-03 22:16:17 +0200 | [diff] [blame] | 299 | static void func_rgroup(char *buf, int size, const procps_status_t *ps) |
| 300 | { |
| 301 | safe_strncpy(buf, get_cached_groupname(ps->rgid), size+1); |
| 302 | } |
Denys Vlasenko | fca70a8 | 2009-07-03 22:16:17 +0200 | [diff] [blame] | 303 | static void func_ruser(char *buf, int size, const procps_status_t *ps) |
| 304 | { |
| 305 | safe_strncpy(buf, get_cached_username(ps->ruid), size+1); |
| 306 | } |
Denys Vlasenko | fca70a8 | 2009-07-03 22:16:17 +0200 | [diff] [blame] | 307 | static void func_nice(char *buf, int size, const procps_status_t *ps) |
| 308 | { |
| 309 | sprintf(buf, "%*d", size, ps->niceness); |
| 310 | } |
Denys Vlasenko | 9e07219 | 2010-05-23 00:34:42 +0200 | [diff] [blame] | 311 | #endif |
Denys Vlasenko | fca70a8 | 2009-07-03 22:16:17 +0200 | [diff] [blame] | 312 | |
Denis Vlasenko | 5fee2e1 | 2008-01-05 03:26:41 +0000 | [diff] [blame] | 313 | #if ENABLE_FEATURE_PS_TIME |
Denys Vlasenko | 9c7c63b | 2017-08-10 15:01:43 +0200 | [diff] [blame] | 314 | static void format_time(char *buf, int size, unsigned long tt) |
| 315 | { |
| 316 | unsigned ff; |
| 317 | |
| 318 | /* Used to show "14453:50" if tt is large. Ugly. |
| 319 | * procps-ng 3.3.10 uses "[[dd-]hh:]mm:ss" format. |
| 320 | * TODO: switch to that? |
| 321 | */ |
| 322 | |
| 323 | /* Formatting for 5-char TIME column. |
| 324 | * NB: "size" is not always 5: ELAPSED is wider (7), |
| 325 | * not taking advantage of that (yet?). |
| 326 | */ |
| 327 | ff = tt % 60; |
| 328 | tt /= 60; |
| 329 | if (tt < 60) { |
| 330 | snprintf(buf, size+1, "%2u:%02u", (unsigned)tt, ff); |
| 331 | return; |
| 332 | } |
| 333 | ff = tt % 60; |
| 334 | tt /= 60; |
| 335 | if (tt < 24) { |
| 336 | snprintf(buf, size+1, "%2uh%02u", (unsigned)tt, ff); |
| 337 | return; |
| 338 | } |
| 339 | ff = tt % 24; |
| 340 | tt /= 24; |
| 341 | if (tt < 100) { |
| 342 | snprintf(buf, size+1, "%2ud%02u", (unsigned)tt, ff); |
| 343 | return; |
| 344 | } |
| 345 | snprintf(buf, size+1, "%4lud", tt); |
| 346 | } |
Denis Vlasenko | 5fee2e1 | 2008-01-05 03:26:41 +0000 | [diff] [blame] | 347 | static void func_etime(char *buf, int size, const procps_status_t *ps) |
| 348 | { |
| 349 | /* elapsed time [[dd-]hh:]mm:ss; here only mm:ss */ |
| 350 | unsigned long mm; |
Denis Vlasenko | 5fee2e1 | 2008-01-05 03:26:41 +0000 | [diff] [blame] | 351 | |
| 352 | mm = ps->start_time / get_kernel_HZ(); |
Denys Vlasenko | 6aab061 | 2013-01-22 10:07:23 +0100 | [diff] [blame] | 353 | mm = G.seconds_since_boot - mm; |
Denys Vlasenko | 9c7c63b | 2017-08-10 15:01:43 +0200 | [diff] [blame] | 354 | format_time(buf, size, mm); |
Denis Vlasenko | 5fee2e1 | 2008-01-05 03:26:41 +0000 | [diff] [blame] | 355 | } |
Denis Vlasenko | 5fee2e1 | 2008-01-05 03:26:41 +0000 | [diff] [blame] | 356 | static void func_time(char *buf, int size, const procps_status_t *ps) |
| 357 | { |
| 358 | /* cumulative time [[dd-]hh:]mm:ss; here only mm:ss */ |
| 359 | unsigned long mm; |
Denis Vlasenko | 5fee2e1 | 2008-01-05 03:26:41 +0000 | [diff] [blame] | 360 | |
| 361 | mm = (ps->utime + ps->stime) / get_kernel_HZ(); |
Denys Vlasenko | 9c7c63b | 2017-08-10 15:01:43 +0200 | [diff] [blame] | 362 | format_time(buf, size, mm); |
Denis Vlasenko | 5fee2e1 | 2008-01-05 03:26:41 +0000 | [diff] [blame] | 363 | } |
| 364 | #endif |
| 365 | |
Denis Vlasenko | 0696b8a | 2007-06-06 07:40:16 +0000 | [diff] [blame] | 366 | #if ENABLE_SELINUX |
| 367 | static void func_label(char *buf, int size, const procps_status_t *ps) |
| 368 | { |
| 369 | safe_strncpy(buf, ps->context ? ps->context : "unknown", size+1); |
| 370 | } |
| 371 | #endif |
| 372 | |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 373 | /* |
Denis Vlasenko | 516a0ca | 2007-04-19 14:46:14 +0000 | [diff] [blame] | 374 | static void func_pcpu(char *buf, int size, const procps_status_t *ps) |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 375 | { |
| 376 | } |
| 377 | */ |
| 378 | |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 379 | static const ps_out_t out_spec[] = { |
Denys Vlasenko | 099e528 | 2011-02-02 00:59:35 +0100 | [diff] [blame] | 380 | /* Mandated by http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ps.html: */ |
Denis Vlasenko | 98ebab8 | 2007-06-30 14:47:41 +0000 | [diff] [blame] | 381 | { 8 , "user" ,"USER" ,func_user ,PSSCAN_UIDGID }, |
Denys Vlasenko | fca70a8 | 2009-07-03 22:16:17 +0200 | [diff] [blame] | 382 | { 8 , "group" ,"GROUP" ,func_group ,PSSCAN_UIDGID }, |
Denis Vlasenko | 98ebab8 | 2007-06-30 14:47:41 +0000 | [diff] [blame] | 383 | { 16 , "comm" ,"COMMAND",func_comm ,PSSCAN_COMM }, |
Denys Vlasenko | a05b2b8 | 2009-10-09 19:47:01 +0200 | [diff] [blame] | 384 | { MAX_WIDTH , "args" ,"COMMAND",func_args ,PSSCAN_COMM }, |
Denis Vlasenko | 98ebab8 | 2007-06-30 14:47:41 +0000 | [diff] [blame] | 385 | { 5 , "pid" ,"PID" ,func_pid ,PSSCAN_PID }, |
| 386 | { 5 , "ppid" ,"PPID" ,func_ppid ,PSSCAN_PPID }, |
| 387 | { 5 , "pgid" ,"PGID" ,func_pgid ,PSSCAN_PGID }, |
Denis Vlasenko | 5fee2e1 | 2008-01-05 03:26:41 +0000 | [diff] [blame] | 388 | #if ENABLE_FEATURE_PS_TIME |
| 389 | { sizeof("ELAPSED")-1, "etime" ,"ELAPSED",func_etime ,PSSCAN_START_TIME }, |
| 390 | #endif |
Denys Vlasenko | fca70a8 | 2009-07-03 22:16:17 +0200 | [diff] [blame] | 391 | #if ENABLE_FEATURE_PS_ADDITIONAL_COLUMNS |
| 392 | { 5 , "nice" ,"NI" ,func_nice ,PSSCAN_NICE }, |
| 393 | { 8 , "rgroup","RGROUP" ,func_rgroup,PSSCAN_RUIDGID }, |
| 394 | { 8 , "ruser" ,"RUSER" ,func_ruser ,PSSCAN_RUIDGID }, |
| 395 | // { 5 , "pcpu" ,"%CPU" ,func_pcpu ,PSSCAN_ }, |
| 396 | #endif |
Denis Vlasenko | 5fee2e1 | 2008-01-05 03:26:41 +0000 | [diff] [blame] | 397 | #if ENABLE_FEATURE_PS_TIME |
Denys Vlasenko | 9c7c63b | 2017-08-10 15:01:43 +0200 | [diff] [blame] | 398 | { 5 , "time" ,"TIME" ,func_time ,PSSCAN_STIME | PSSCAN_UTIME }, |
Denis Vlasenko | 5fee2e1 | 2008-01-05 03:26:41 +0000 | [diff] [blame] | 399 | #endif |
Denis Vlasenko | 98ebab8 | 2007-06-30 14:47:41 +0000 | [diff] [blame] | 400 | { 6 , "tty" ,"TT" ,func_tty ,PSSCAN_TTY }, |
| 401 | { 4 , "vsz" ,"VSZ" ,func_vsz ,PSSCAN_VSZ }, |
Denys Vlasenko | 099e528 | 2011-02-02 00:59:35 +0100 | [diff] [blame] | 402 | /* Not mandated, but useful: */ |
Denys Vlasenko | 5d725fd | 2017-07-29 02:20:17 +0200 | [diff] [blame] | 403 | { 5 , "sid" ,"SID" ,func_sid ,PSSCAN_SID }, |
Denys Vlasenko | 12ac628 | 2011-06-18 15:49:47 +0200 | [diff] [blame] | 404 | { 4 , "stat" ,"STAT" ,func_state ,PSSCAN_STATE }, |
Denis Vlasenko | 98ebab8 | 2007-06-30 14:47:41 +0000 | [diff] [blame] | 405 | { 4 , "rss" ,"RSS" ,func_rss ,PSSCAN_RSS }, |
Denis Vlasenko | 0696b8a | 2007-06-06 07:40:16 +0000 | [diff] [blame] | 406 | #if ENABLE_SELINUX |
Denis Vlasenko | 98ebab8 | 2007-06-30 14:47:41 +0000 | [diff] [blame] | 407 | { 35 , "label" ,"LABEL" ,func_label ,PSSCAN_CONTEXT }, |
Denis Vlasenko | 0696b8a | 2007-06-06 07:40:16 +0000 | [diff] [blame] | 408 | #endif |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 409 | }; |
| 410 | |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 411 | static ps_out_t* new_out_t(void) |
| 412 | { |
Denis Vlasenko | deeed59 | 2008-07-08 05:14:36 +0000 | [diff] [blame] | 413 | out = xrealloc_vector(out, 2, out_cnt); |
| 414 | return &out[out_cnt++]; |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | static const ps_out_t* find_out_spec(const char *name) |
| 418 | { |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 419 | unsigned i; |
Denys Vlasenko | 9e07219 | 2010-05-23 00:34:42 +0200 | [diff] [blame] | 420 | char buf[ARRAY_SIZE(out_spec)*7 + 1]; |
| 421 | char *p = buf; |
Denys Vlasenko | 9e07219 | 2010-05-23 00:34:42 +0200 | [diff] [blame] | 422 | |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 423 | for (i = 0; i < ARRAY_SIZE(out_spec); i++) { |
Denys Vlasenko | 9e07219 | 2010-05-23 00:34:42 +0200 | [diff] [blame] | 424 | if (strncmp(name, out_spec[i].name6, 6) == 0) |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 425 | return &out_spec[i]; |
Denys Vlasenko | 9e07219 | 2010-05-23 00:34:42 +0200 | [diff] [blame] | 426 | p += sprintf(p, "%.6s,", out_spec[i].name6); |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 427 | } |
Denys Vlasenko | 9e07219 | 2010-05-23 00:34:42 +0200 | [diff] [blame] | 428 | p[-1] = '\0'; |
| 429 | bb_error_msg_and_die("bad -o argument '%s', supported arguments: %s", name, buf); |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | static void parse_o(char* opt) |
| 433 | { |
| 434 | ps_out_t* new; |
| 435 | // POSIX: "-o is blank- or comma-separated list" (FIXME) |
| 436 | char *comma, *equal; |
| 437 | while (1) { |
| 438 | comma = strchr(opt, ','); |
| 439 | equal = strchr(opt, '='); |
| 440 | if (comma && (!equal || equal > comma)) { |
| 441 | *comma = '\0'; |
| 442 | *new_out_t() = *find_out_spec(opt); |
| 443 | *comma = ','; |
| 444 | opt = comma + 1; |
| 445 | continue; |
| 446 | } |
| 447 | break; |
| 448 | } |
Denis Vlasenko | 516a0ca | 2007-04-19 14:46:14 +0000 | [diff] [blame] | 449 | // opt points to last spec in comma separated list. |
| 450 | // This one can have =HEADER part. |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 451 | new = new_out_t(); |
| 452 | if (equal) |
| 453 | *equal = '\0'; |
| 454 | *new = *find_out_spec(opt); |
| 455 | if (equal) { |
| 456 | *equal = '='; |
| 457 | new->header = equal + 1; |
| 458 | // POSIX: the field widths shall be ... at least as wide as |
| 459 | // the header text (default or overridden value). |
| 460 | // If the header text is null, such as -o user=, |
| 461 | // the field width shall be at least as wide as the |
| 462 | // default header text |
| 463 | if (new->header[0]) { |
| 464 | new->width = strlen(new->header); |
| 465 | print_header = 1; |
| 466 | } |
| 467 | } else |
| 468 | print_header = 1; |
| 469 | } |
| 470 | |
Denys Vlasenko | a05b2b8 | 2009-10-09 19:47:01 +0200 | [diff] [blame] | 471 | static void alloc_line_buffer(void) |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 472 | { |
| 473 | int i; |
| 474 | int width = 0; |
| 475 | for (i = 0; i < out_cnt; i++) { |
Denis Vlasenko | 8581863 | 2007-04-19 14:47:11 +0000 | [diff] [blame] | 476 | need_flags |= out[i].ps_flags; |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 477 | if (out[i].header[0]) { |
| 478 | print_header = 1; |
| 479 | } |
| 480 | width += out[i].width + 1; /* "FIELD " */ |
Denys Vlasenko | a05b2b8 | 2009-10-09 19:47:01 +0200 | [diff] [blame] | 481 | if ((int)(width - terminal_width) > 0) { |
| 482 | /* The rest does not fit on the screen */ |
| 483 | //out[i].width -= (width - terminal_width - 1); |
| 484 | out_cnt = i + 1; |
| 485 | break; |
| 486 | } |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 487 | } |
Denis Vlasenko | 0696b8a | 2007-06-06 07:40:16 +0000 | [diff] [blame] | 488 | #if ENABLE_SELINUX |
| 489 | if (!is_selinux_enabled()) |
| 490 | need_flags &= ~PSSCAN_CONTEXT; |
| 491 | #endif |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 492 | buffer = xmalloc(width + 1); /* for trailing \0 */ |
| 493 | } |
| 494 | |
| 495 | static void format_header(void) |
| 496 | { |
| 497 | int i; |
| 498 | ps_out_t* op; |
Denis Vlasenko | 516a0ca | 2007-04-19 14:46:14 +0000 | [diff] [blame] | 499 | char *p; |
| 500 | |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 501 | if (!print_header) |
| 502 | return; |
Denis Vlasenko | 516a0ca | 2007-04-19 14:46:14 +0000 | [diff] [blame] | 503 | p = buffer; |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 504 | i = 0; |
| 505 | if (out_cnt) { |
| 506 | while (1) { |
| 507 | op = &out[i]; |
| 508 | if (++i == out_cnt) /* do not pad last field */ |
| 509 | break; |
| 510 | p += sprintf(p, "%-*s ", op->width, op->header); |
| 511 | } |
| 512 | strcpy(p, op->header); |
| 513 | } |
| 514 | printf("%.*s\n", terminal_width, buffer); |
| 515 | } |
| 516 | |
| 517 | static void format_process(const procps_status_t *ps) |
| 518 | { |
| 519 | int i, len; |
| 520 | char *p = buffer; |
| 521 | i = 0; |
| 522 | if (out_cnt) while (1) { |
| 523 | out[i].f(p, out[i].width, ps); |
| 524 | // POSIX: Any field need not be meaningful in all |
| 525 | // implementations. In such a case a hyphen ( '-' ) |
| 526 | // should be output in place of the field value. |
Denis Vlasenko | 6dca70a | 2006-12-18 14:20:01 +0000 | [diff] [blame] | 527 | if (!p[0]) { |
| 528 | p[0] = '-'; |
| 529 | p[1] = '\0'; |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 530 | } |
| 531 | len = strlen(p); |
| 532 | p += len; |
| 533 | len = out[i].width - len + 1; |
| 534 | if (++i == out_cnt) /* do not pad last field */ |
| 535 | break; |
Denis Vlasenko | 6dca70a | 2006-12-18 14:20:01 +0000 | [diff] [blame] | 536 | p += sprintf(p, "%*s", len, ""); |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 537 | } |
| 538 | printf("%.*s\n", terminal_width, buffer); |
| 539 | } |
| 540 | |
Denys Vlasenko | 12ac628 | 2011-06-18 15:49:47 +0200 | [diff] [blame] | 541 | #if ENABLE_SELINUX |
| 542 | # define SELINUX_O_PREFIX "label," |
| 543 | # define DEFAULT_O_STR (SELINUX_O_PREFIX "pid,user" IF_FEATURE_PS_TIME(",time") ",args") |
| 544 | #else |
| 545 | # define DEFAULT_O_STR ("pid,user" IF_FEATURE_PS_TIME(",time") ",args") |
| 546 | #endif |
| 547 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 548 | int ps_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 549 | int ps_main(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 550 | { |
| 551 | procps_status_t *p; |
| 552 | llist_t* opt_o = NULL; |
Denys Vlasenko | 12ac628 | 2011-06-18 15:49:47 +0200 | [diff] [blame] | 553 | char default_o[sizeof(DEFAULT_O_STR)]; |
Denys Vlasenko | 4e08a12 | 2017-01-16 17:31:05 +0100 | [diff] [blame] | 554 | #if ENABLE_SELINUX || ENABLE_FEATURE_SHOW_THREADS |
Denys Vlasenko | b410d4a | 2009-09-19 22:29:42 +0200 | [diff] [blame] | 555 | int opt; |
Denys Vlasenko | 4e08a12 | 2017-01-16 17:31:05 +0100 | [diff] [blame] | 556 | #endif |
Denys Vlasenko | b410d4a | 2009-09-19 22:29:42 +0200 | [diff] [blame] | 557 | enum { |
| 558 | OPT_Z = (1 << 0), |
| 559 | OPT_o = (1 << 1), |
| 560 | OPT_a = (1 << 2), |
| 561 | OPT_A = (1 << 3), |
| 562 | OPT_d = (1 << 4), |
| 563 | OPT_e = (1 << 5), |
| 564 | OPT_f = (1 << 6), |
| 565 | OPT_l = (1 << 7), |
| 566 | OPT_T = (1 << 8) * ENABLE_FEATURE_SHOW_THREADS, |
| 567 | }; |
| 568 | |
| 569 | INIT_G(); |
Ron Yorston | 0ec0fac | 2017-08-22 11:21:56 +0100 | [diff] [blame] | 570 | #if ENABLE_FEATURE_PS_TIME |
Denys Vlasenko | 1fd8e66 | 2017-08-10 14:10:56 +0200 | [diff] [blame] | 571 | G.seconds_since_boot = get_uptime(); |
Ron Yorston | 0ec0fac | 2017-08-22 11:21:56 +0100 | [diff] [blame] | 572 | # if ENABLE_FEATURE_PS_UNUSUAL_SYSTEMS || !defined(__linux__) |
Denys Vlasenko | 1fd8e66 | 2017-08-10 14:10:56 +0200 | [diff] [blame] | 573 | G.kernel_HZ = bb_clk_tck(); /* this is sysconf(_SC_CLK_TCK) */ |
Ron Yorston | 0ec0fac | 2017-08-22 11:21:56 +0100 | [diff] [blame] | 574 | # endif |
Denys Vlasenko | 1fd8e66 | 2017-08-10 14:10:56 +0200 | [diff] [blame] | 575 | #endif |
Denis Vlasenko | 8581863 | 2007-04-19 14:47:11 +0000 | [diff] [blame] | 576 | |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 577 | // POSIX: |
| 578 | // -a Write information for all processes associated with terminals |
| 579 | // Implementations may omit session leaders from this list |
| 580 | // -A Write information for all processes |
| 581 | // -d Write information for all processes, except session leaders |
Denys Vlasenko | b410d4a | 2009-09-19 22:29:42 +0200 | [diff] [blame] | 582 | // -e Write information for all processes (equivalent to -A) |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 583 | // -f Generate a full listing |
| 584 | // -l Generate a long listing |
| 585 | // -o col1,col2,col3=header |
Denis Vlasenko | 516a0ca | 2007-04-19 14:46:14 +0000 | [diff] [blame] | 586 | // Select which columns to display |
Denys Vlasenko | 887d963 | 2009-11-24 03:52:24 +0100 | [diff] [blame] | 587 | /* We allow (and ignore) most of the above. FIXME. |
Denys Vlasenko | 6aab061 | 2013-01-22 10:07:23 +0100 | [diff] [blame] | 588 | * -T is picked for threads (POSIX hasn't standardized it). |
Denys Vlasenko | 887d963 | 2009-11-24 03:52:24 +0100 | [diff] [blame] | 589 | * procps v3.2.7 supports -T and shows tids as SPID column, |
| 590 | * it also supports -L where it shows tids as LWP column. |
| 591 | */ |
Denys Vlasenko | 4e08a12 | 2017-01-16 17:31:05 +0100 | [diff] [blame] | 592 | #if ENABLE_SELINUX || ENABLE_FEATURE_SHOW_THREADS |
| 593 | opt = |
| 594 | #endif |
| 595 | getopt32(argv, "Zo:*aAdefl"IF_FEATURE_SHOW_THREADS("T"), &opt_o); |
| 596 | |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 597 | if (opt_o) { |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 598 | do { |
Denis Vlasenko | d50dda8 | 2008-06-15 05:40:56 +0000 | [diff] [blame] | 599 | parse_o(llist_pop(&opt_o)); |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 600 | } while (opt_o); |
Denis Vlasenko | 0696b8a | 2007-06-06 07:40:16 +0000 | [diff] [blame] | 601 | } else { |
Denys Vlasenko | 6aab061 | 2013-01-22 10:07:23 +0100 | [diff] [blame] | 602 | /* Below: parse_o() needs char*, NOT const char*, |
| 603 | * can't pass it constant string. Need to make a copy first. |
| 604 | */ |
Denis Vlasenko | 0696b8a | 2007-06-06 07:40:16 +0000 | [diff] [blame] | 605 | #if ENABLE_SELINUX |
Denys Vlasenko | b410d4a | 2009-09-19 22:29:42 +0200 | [diff] [blame] | 606 | if (!(opt & OPT_Z) || !is_selinux_enabled()) { |
Denis Vlasenko | 0696b8a | 2007-06-06 07:40:16 +0000 | [diff] [blame] | 607 | /* no -Z or no SELinux: do not show LABEL */ |
Denis Vlasenko | 83cea0e | 2008-07-16 21:11:09 +0000 | [diff] [blame] | 608 | strcpy(default_o, DEFAULT_O_STR + sizeof(SELINUX_O_PREFIX)-1); |
Denis Vlasenko | 0696b8a | 2007-06-06 07:40:16 +0000 | [diff] [blame] | 609 | } else |
| 610 | #endif |
| 611 | { |
| 612 | strcpy(default_o, DEFAULT_O_STR); |
| 613 | } |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 614 | parse_o(default_o); |
Denis Vlasenko | 0696b8a | 2007-06-06 07:40:16 +0000 | [diff] [blame] | 615 | } |
Denys Vlasenko | b410d4a | 2009-09-19 22:29:42 +0200 | [diff] [blame] | 616 | #if ENABLE_FEATURE_SHOW_THREADS |
| 617 | if (opt & OPT_T) |
| 618 | need_flags |= PSSCAN_TASKS; |
| 619 | #endif |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 620 | |
Denis Vlasenko | 450196c | 2007-03-28 21:57:12 +0000 | [diff] [blame] | 621 | /* Was INT_MAX, but some libc's go belly up with printf("%.*s") |
| 622 | * and such large widths */ |
Denis Vlasenko | 7ce7594 | 2007-07-25 17:27:58 +0000 | [diff] [blame] | 623 | terminal_width = MAX_WIDTH; |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 624 | if (isatty(1)) { |
Denys Vlasenko | 641caae | 2015-10-23 01:44:22 +0200 | [diff] [blame] | 625 | terminal_width = get_terminal_width(0); |
Denis Vlasenko | 7ce7594 | 2007-07-25 17:27:58 +0000 | [diff] [blame] | 626 | if (--terminal_width > MAX_WIDTH) |
| 627 | terminal_width = MAX_WIDTH; |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 628 | } |
Denys Vlasenko | a05b2b8 | 2009-10-09 19:47:01 +0200 | [diff] [blame] | 629 | alloc_line_buffer(); |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 630 | format_header(); |
| 631 | |
| 632 | p = NULL; |
Denys Vlasenko | b410d4a | 2009-09-19 22:29:42 +0200 | [diff] [blame] | 633 | while ((p = procps_scan(p, need_flags)) != NULL) { |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 634 | format_process(p); |
| 635 | } |
| 636 | |
| 637 | return EXIT_SUCCESS; |
| 638 | } |
| 639 | |
| 640 | |
| 641 | #else /* !ENABLE_DESKTOP */ |
| 642 | |
| 643 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 644 | int ps_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 645 | int ps_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | ef8b6c7 | 1999-10-20 08:05:35 +0000 | [diff] [blame] | 646 | { |
Denys Vlasenko | d75295c | 2009-09-21 23:58:43 +0200 | [diff] [blame] | 647 | procps_status_t *p; |
Denys Vlasenko | d75295c | 2009-09-21 23:58:43 +0200 | [diff] [blame] | 648 | int psscan_flags = PSSCAN_PID | PSSCAN_UIDGID |
| 649 | | PSSCAN_STATE | PSSCAN_VSZ | PSSCAN_COMM; |
Denys Vlasenko | 3960184 | 2010-04-07 18:03:34 +0200 | [diff] [blame] | 650 | unsigned terminal_width IF_NOT_FEATURE_PS_WIDE(= 79); |
| 651 | enum { |
| 652 | OPT_Z = (1 << 0) * ENABLE_SELINUX, |
| 653 | OPT_T = (1 << ENABLE_SELINUX) * ENABLE_FEATURE_SHOW_THREADS, |
Denys Vlasenko | 8d9ac30 | 2011-09-26 02:56:08 +0200 | [diff] [blame] | 654 | OPT_l = (1 << ENABLE_SELINUX) * (1 << ENABLE_FEATURE_SHOW_THREADS) * ENABLE_FEATURE_PS_LONG, |
Denys Vlasenko | 3960184 | 2010-04-07 18:03:34 +0200 | [diff] [blame] | 655 | }; |
Denys Vlasenko | 8d9ac30 | 2011-09-26 02:56:08 +0200 | [diff] [blame] | 656 | #if ENABLE_FEATURE_PS_LONG |
Denys Vlasenko | c9091d8 | 2015-07-01 19:07:24 +0200 | [diff] [blame] | 657 | time_t now = now; /* for compiler */ |
| 658 | unsigned long uptime = uptime; |
Denys Vlasenko | 8d9ac30 | 2011-09-26 02:56:08 +0200 | [diff] [blame] | 659 | #endif |
Denys Vlasenko | 3960184 | 2010-04-07 18:03:34 +0200 | [diff] [blame] | 660 | /* If we support any options, parse argv */ |
Denys Vlasenko | 8d9ac30 | 2011-09-26 02:56:08 +0200 | [diff] [blame] | 661 | #if ENABLE_SELINUX || ENABLE_FEATURE_SHOW_THREADS || ENABLE_FEATURE_PS_WIDE || ENABLE_FEATURE_PS_LONG |
Denys Vlasenko | 42be921 | 2012-09-25 12:48:10 +0200 | [diff] [blame] | 662 | int opts = 0; |
Denys Vlasenko | d75295c | 2009-09-21 23:58:43 +0200 | [diff] [blame] | 663 | # if ENABLE_FEATURE_PS_WIDE |
Denys Vlasenko | 3960184 | 2010-04-07 18:03:34 +0200 | [diff] [blame] | 664 | /* -w is a bit complicated */ |
Denys Vlasenko | d75295c | 2009-09-21 23:58:43 +0200 | [diff] [blame] | 665 | int w_count = 0; |
Denys Vlasenko | 727948e | 2017-08-04 16:23:42 +0200 | [diff] [blame] | 666 | make_all_argv_opts(argv); |
Denys Vlasenko | 22542ec | 2017-08-08 21:55:02 +0200 | [diff] [blame] | 667 | opts = getopt32(argv, "^" |
| 668 | IF_SELINUX("Z")IF_FEATURE_SHOW_THREADS("T")IF_FEATURE_PS_LONG("l")"w" |
| 669 | "\0" "ww", |
| 670 | &w_count |
| 671 | ); |
"Vladimir N. Oleynik" | be0ed3d | 2005-10-04 16:48:26 +0000 | [diff] [blame] | 672 | /* if w is given once, GNU ps sets the width to 132, |
| 673 | * if w is given more than once, it is "unlimited" |
| 674 | */ |
Denis Vlasenko | 621204b | 2006-10-27 09:03:24 +0000 | [diff] [blame] | 675 | if (w_count) { |
Denys Vlasenko | d75295c | 2009-09-21 23:58:43 +0200 | [diff] [blame] | 676 | terminal_width = (w_count == 1) ? 132 : MAX_WIDTH; |
"Vladimir N. Oleynik" | be0ed3d | 2005-10-04 16:48:26 +0000 | [diff] [blame] | 677 | } else { |
Denys Vlasenko | 641caae | 2015-10-23 01:44:22 +0200 | [diff] [blame] | 678 | terminal_width = get_terminal_width(0); |
"Vladimir N. Oleynik" | be0ed3d | 2005-10-04 16:48:26 +0000 | [diff] [blame] | 679 | /* Go one less... */ |
Denis Vlasenko | 7ce7594 | 2007-07-25 17:27:58 +0000 | [diff] [blame] | 680 | if (--terminal_width > MAX_WIDTH) |
| 681 | terminal_width = MAX_WIDTH; |
Bernhard Reutner-Fischer | 6d6a40c | 2005-10-04 14:31:18 +0000 | [diff] [blame] | 682 | } |
Denys Vlasenko | 3960184 | 2010-04-07 18:03:34 +0200 | [diff] [blame] | 683 | # else |
| 684 | /* -w is not supported, only -Z and/or -T */ |
Denys Vlasenko | 727948e | 2017-08-04 16:23:42 +0200 | [diff] [blame] | 685 | make_all_argv_opts(argv); |
Denys Vlasenko | 8d9ac30 | 2011-09-26 02:56:08 +0200 | [diff] [blame] | 686 | opts = getopt32(argv, IF_SELINUX("Z")IF_FEATURE_SHOW_THREADS("T")IF_FEATURE_PS_LONG("l")); |
Denys Vlasenko | d75295c | 2009-09-21 23:58:43 +0200 | [diff] [blame] | 687 | # endif |
Denys Vlasenko | 3960184 | 2010-04-07 18:03:34 +0200 | [diff] [blame] | 688 | |
Denys Vlasenko | 8d9ac30 | 2011-09-26 02:56:08 +0200 | [diff] [blame] | 689 | # if ENABLE_SELINUX |
Denys Vlasenko | 3960184 | 2010-04-07 18:03:34 +0200 | [diff] [blame] | 690 | if ((opts & OPT_Z) && is_selinux_enabled()) { |
Denys Vlasenko | d75295c | 2009-09-21 23:58:43 +0200 | [diff] [blame] | 691 | psscan_flags = PSSCAN_PID | PSSCAN_CONTEXT |
| 692 | | PSSCAN_STATE | PSSCAN_COMM; |
Denis Vlasenko | 56ea65c | 2008-01-06 03:26:53 +0000 | [diff] [blame] | 693 | puts(" PID CONTEXT STAT COMMAND"); |
Denys Vlasenko | 3960184 | 2010-04-07 18:03:34 +0200 | [diff] [blame] | 694 | } else |
Denys Vlasenko | 8d9ac30 | 2011-09-26 02:56:08 +0200 | [diff] [blame] | 695 | # endif |
| 696 | if (opts & OPT_l) { |
| 697 | psscan_flags = PSSCAN_STATE | PSSCAN_UIDGID | PSSCAN_PID | PSSCAN_PPID |
| 698 | | PSSCAN_TTY | PSSCAN_STIME | PSSCAN_UTIME | PSSCAN_COMM |
| 699 | | PSSCAN_VSZ | PSSCAN_RSS; |
| 700 | /* http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ps.html |
| 701 | * mandates for -l: |
Flemming Madsen | b64bd16 | 2011-09-27 15:31:25 +0200 | [diff] [blame] | 702 | * -F Flags (?) |
| 703 | * S State |
Denys Vlasenko | 8d9ac30 | 2011-09-26 02:56:08 +0200 | [diff] [blame] | 704 | * UID,PID,PPID |
Flemming Madsen | b64bd16 | 2011-09-27 15:31:25 +0200 | [diff] [blame] | 705 | * -C CPU usage |
Denys Vlasenko | 8d9ac30 | 2011-09-26 02:56:08 +0200 | [diff] [blame] | 706 | * -PRI The priority of the process; higher numbers mean lower priority |
Flemming Madsen | b64bd16 | 2011-09-27 15:31:25 +0200 | [diff] [blame] | 707 | * -NI Nice value |
| 708 | * -ADDR The address of the process (?) |
| 709 | * SZ The size in blocks of the core image |
Denys Vlasenko | 8d9ac30 | 2011-09-26 02:56:08 +0200 | [diff] [blame] | 710 | * -WCHAN The event for which the process is waiting or sleeping |
| 711 | * TTY |
Flemming Madsen | b64bd16 | 2011-09-27 15:31:25 +0200 | [diff] [blame] | 712 | * TIME The cumulative execution time |
Denys Vlasenko | 8d9ac30 | 2011-09-26 02:56:08 +0200 | [diff] [blame] | 713 | * CMD |
Flemming Madsen | b64bd16 | 2011-09-27 15:31:25 +0200 | [diff] [blame] | 714 | * We don't show fields marked with '-'. |
| 715 | * We show VSZ and RSS instead of SZ. |
| 716 | * We also show STIME (standard says that -f shows it, -l doesn't). |
Denys Vlasenko | 8d9ac30 | 2011-09-26 02:56:08 +0200 | [diff] [blame] | 717 | */ |
Flemming Madsen | b64bd16 | 2011-09-27 15:31:25 +0200 | [diff] [blame] | 718 | puts("S UID PID PPID VSZ RSS TTY STIME TIME CMD"); |
Denys Vlasenko | 42be921 | 2012-09-25 12:48:10 +0200 | [diff] [blame] | 719 | # if ENABLE_FEATURE_PS_LONG |
Denys Vlasenko | 8d9ac30 | 2011-09-26 02:56:08 +0200 | [diff] [blame] | 720 | now = time(NULL); |
Michael Tokarev | ac42e3d | 2012-06-13 02:51:56 +0200 | [diff] [blame] | 721 | uptime = get_uptime(); |
Denys Vlasenko | 42be921 | 2012-09-25 12:48:10 +0200 | [diff] [blame] | 722 | # endif |
Denys Vlasenko | 8d9ac30 | 2011-09-26 02:56:08 +0200 | [diff] [blame] | 723 | } |
| 724 | else { |
Denis Vlasenko | 56ea65c | 2008-01-06 03:26:53 +0000 | [diff] [blame] | 725 | puts(" PID USER VSZ STAT COMMAND"); |
Denys Vlasenko | 3960184 | 2010-04-07 18:03:34 +0200 | [diff] [blame] | 726 | } |
| 727 | if (opts & OPT_T) { |
| 728 | psscan_flags |= PSSCAN_TASKS; |
| 729 | } |
Denys Vlasenko | 8d9ac30 | 2011-09-26 02:56:08 +0200 | [diff] [blame] | 730 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 731 | |
Denys Vlasenko | d75295c | 2009-09-21 23:58:43 +0200 | [diff] [blame] | 732 | p = NULL; |
| 733 | while ((p = procps_scan(p, psscan_flags)) != NULL) { |
Denys Vlasenko | 3960184 | 2010-04-07 18:03:34 +0200 | [diff] [blame] | 734 | int len; |
Bernhard Reutner-Fischer | 6d6a40c | 2005-10-04 14:31:18 +0000 | [diff] [blame] | 735 | #if ENABLE_SELINUX |
Denys Vlasenko | d75295c | 2009-09-21 23:58:43 +0200 | [diff] [blame] | 736 | if (psscan_flags & PSSCAN_CONTEXT) { |
Denis Vlasenko | 766f659 | 2008-01-07 16:41:41 +0000 | [diff] [blame] | 737 | len = printf("%5u %-32.32s %s ", |
Denis Vlasenko | 0696b8a | 2007-06-06 07:40:16 +0000 | [diff] [blame] | 738 | p->pid, |
| 739 | p->context ? p->context : "unknown", |
| 740 | p->state); |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 741 | } else |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 742 | #endif |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 743 | { |
Denys Vlasenko | 8d9ac30 | 2011-09-26 02:56:08 +0200 | [diff] [blame] | 744 | char buf6[6]; |
Denys Vlasenko | a407cf7 | 2013-09-06 12:53:14 +0200 | [diff] [blame] | 745 | smart_ulltoa5(p->vsz, buf6, " mgtpezy")[0] = '\0'; |
Denys Vlasenko | 8d9ac30 | 2011-09-26 02:56:08 +0200 | [diff] [blame] | 746 | #if ENABLE_FEATURE_PS_LONG |
| 747 | if (opts & OPT_l) { |
Flemming Madsen | b64bd16 | 2011-09-27 15:31:25 +0200 | [diff] [blame] | 748 | char bufr[6], stime_str[6]; |
Denys Vlasenko | 8d9ac30 | 2011-09-26 02:56:08 +0200 | [diff] [blame] | 749 | char tty[2 * sizeof(int)*3 + 2]; |
| 750 | char *endp; |
| 751 | unsigned sut = (p->stime + p->utime) / 100; |
Michael Tokarev | ac42e3d | 2012-06-13 02:51:56 +0200 | [diff] [blame] | 752 | unsigned elapsed = uptime - (p->start_time / 100); |
Denys Vlasenko | 8d9ac30 | 2011-09-26 02:56:08 +0200 | [diff] [blame] | 753 | time_t start = now - elapsed; |
| 754 | struct tm *tm = localtime(&start); |
| 755 | |
Denys Vlasenko | a407cf7 | 2013-09-06 12:53:14 +0200 | [diff] [blame] | 756 | smart_ulltoa5(p->rss, bufr, " mgtpezy")[0] = '\0'; |
Denys Vlasenko | 8d9ac30 | 2011-09-26 02:56:08 +0200 | [diff] [blame] | 757 | |
| 758 | if (p->tty_major == 136) |
Flemming Madsen | b64bd16 | 2011-09-27 15:31:25 +0200 | [diff] [blame] | 759 | /* It should be pts/N, not ptsN, but N > 9 |
| 760 | * will overflow field width... |
| 761 | */ |
| 762 | endp = stpcpy(tty, "pts"); |
Denys Vlasenko | 8d9ac30 | 2011-09-26 02:56:08 +0200 | [diff] [blame] | 763 | else |
| 764 | if (p->tty_major == 4) { |
| 765 | endp = stpcpy(tty, "tty"); |
| 766 | if (p->tty_minor >= 64) { |
| 767 | p->tty_minor -= 64; |
| 768 | *endp++ = 'S'; |
| 769 | } |
| 770 | } |
| 771 | else |
| 772 | endp = tty + sprintf(tty, "%d:", p->tty_major); |
| 773 | strcpy(endp, utoa(p->tty_minor)); |
| 774 | |
Flemming Madsen | b64bd16 | 2011-09-27 15:31:25 +0200 | [diff] [blame] | 775 | strftime(stime_str, 6, (elapsed >= (24 * 60 * 60)) ? "%b%d" : "%H:%M", tm); |
| 776 | stime_str[5] = '\0'; |
| 777 | // S UID PID PPID VSZ RSS TTY STIME TIME CMD |
| 778 | len = printf("%c %5u %5u %5u %5s %5s %-5s %s %02u:%02u:%02u ", |
Denys Vlasenko | 8d9ac30 | 2011-09-26 02:56:08 +0200 | [diff] [blame] | 779 | p->state[0], p->uid, p->pid, p->ppid, buf6, bufr, tty, |
Flemming Madsen | b64bd16 | 2011-09-27 15:31:25 +0200 | [diff] [blame] | 780 | stime_str, sut / 3600, (sut % 3600) / 60, sut % 60); |
Denys Vlasenko | 8d9ac30 | 2011-09-26 02:56:08 +0200 | [diff] [blame] | 781 | } else |
| 782 | #endif |
Denis Vlasenko | 56ea65c | 2008-01-06 03:26:53 +0000 | [diff] [blame] | 783 | { |
Denys Vlasenko | 8d9ac30 | 2011-09-26 02:56:08 +0200 | [diff] [blame] | 784 | const char *user = get_cached_username(p->uid); |
Denis Vlasenko | 766f659 | 2008-01-07 16:41:41 +0000 | [diff] [blame] | 785 | len = printf("%5u %-8.8s %s %s ", |
Denis Vlasenko | 56ea65c | 2008-01-06 03:26:53 +0000 | [diff] [blame] | 786 | p->pid, user, buf6, p->state); |
| 787 | } |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 788 | } |
Bernhard Reutner-Fischer | 6d6a40c | 2005-10-04 14:31:18 +0000 | [diff] [blame] | 789 | |
Denis Vlasenko | 98ebab8 | 2007-06-30 14:47:41 +0000 | [diff] [blame] | 790 | { |
Denis Vlasenko | dc7a5ea | 2007-07-01 14:53:06 +0000 | [diff] [blame] | 791 | int sz = terminal_width - len; |
Denys Vlasenko | c9091d8 | 2015-07-01 19:07:24 +0200 | [diff] [blame] | 792 | if (sz >= 0) { |
| 793 | char buf[sz + 1]; |
| 794 | read_cmdline(buf, sz, p->pid, p->comm); |
| 795 | puts(buf); |
| 796 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 797 | } |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 798 | } |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 799 | if (ENABLE_FEATURE_CLEAN_UP) |
| 800 | clear_username_cache(); |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 801 | return EXIT_SUCCESS; |
Eric Andersen | ef8b6c7 | 1999-10-20 08:05:35 +0000 | [diff] [blame] | 802 | } |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 803 | |
Denys Vlasenko | 3960184 | 2010-04-07 18:03:34 +0200 | [diff] [blame] | 804 | #endif /* !ENABLE_DESKTOP */ |