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