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