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