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