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