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