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() */ |
| 20 | //#include <sys/sysinfo.h> /* for sysinfo() */ |
| 21 | #ifndef AT_CLKTCK |
| 22 | #define AT_CLKTCK 17 |
| 23 | #endif |
| 24 | |
| 25 | |
| 26 | #if ENABLE_SELINUX |
Denis Vlasenko | 83cea0e | 2008-07-16 21:11:09 +0000 | [diff] [blame] | 27 | #define SELINUX_O_PREFIX "label," |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 28 | #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] | 29 | #else |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 30 | #define DEFAULT_O_STR ("pid,user" IF_FEATURE_PS_TIME(",time") ",args") |
Denis Vlasenko | 5fee2e1 | 2008-01-05 03:26:41 +0000 | [diff] [blame] | 31 | #endif |
| 32 | |
| 33 | typedef struct { |
| 34 | uint16_t width; |
| 35 | char name[6]; |
| 36 | const char *header; |
| 37 | void (*f)(char *buf, int size, const procps_status_t *ps); |
| 38 | int ps_flags; |
| 39 | } ps_out_t; |
| 40 | |
| 41 | struct globals { |
| 42 | ps_out_t* out; |
| 43 | int out_cnt; |
| 44 | int print_header; |
| 45 | int need_flags; |
| 46 | char *buffer; |
| 47 | unsigned terminal_width; |
| 48 | #if ENABLE_FEATURE_PS_TIME |
| 49 | unsigned kernel_HZ; |
| 50 | unsigned long long seconds_since_boot; |
| 51 | #endif |
| 52 | char default_o[sizeof(DEFAULT_O_STR)]; |
| 53 | }; |
| 54 | #define G (*(struct globals*)&bb_common_bufsiz1) |
| 55 | #define out (G.out ) |
| 56 | #define out_cnt (G.out_cnt ) |
| 57 | #define print_header (G.print_header ) |
| 58 | #define need_flags (G.need_flags ) |
| 59 | #define buffer (G.buffer ) |
| 60 | #define terminal_width (G.terminal_width ) |
| 61 | #define kernel_HZ (G.kernel_HZ ) |
| 62 | #define seconds_since_boot (G.seconds_since_boot) |
| 63 | #define default_o (G.default_o ) |
| 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 | |
| 145 | //if (open_read_close("/proc/uptime", buf, sizeof(buf) <= 0) |
| 146 | // bb_perror_msg_and_die("cannot read %s", "/proc/uptime"); |
| 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 | |
Denis Vlasenko | 769d1e0 | 2007-01-22 23:04:27 +0000 | [diff] [blame] | 177 | 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] | 178 | { |
| 179 | safe_strncpy(buf, ps->comm, size+1); |
| 180 | } |
| 181 | |
Denis Vlasenko | 769d1e0 | 2007-01-22 23:04:27 +0000 | [diff] [blame] | 182 | 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] | 183 | { |
Denis Vlasenko | 98ebab8 | 2007-06-30 14:47:41 +0000 | [diff] [blame] | 184 | read_cmdline(buf, size, ps->pid, ps->comm); |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Denis Vlasenko | 769d1e0 | 2007-01-22 23:04:27 +0000 | [diff] [blame] | 187 | 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] | 188 | { |
Denis Vlasenko | 516a0ca | 2007-04-19 14:46:14 +0000 | [diff] [blame] | 189 | sprintf(buf, "%*u", size, ps->pid); |
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_ppid(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->ppid); |
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_pgid(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->pgid); |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Denis Vlasenko | fc1e995 | 2007-08-29 18:23:36 +0000 | [diff] [blame] | 202 | static void put_lu(char *buf, int size, unsigned long u) |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 203 | { |
Denis Vlasenko | 9b4c072 | 2008-01-07 16:51:25 +0000 | [diff] [blame] | 204 | char buf4[5]; |
Denis Vlasenko | 56ea65c | 2008-01-06 03:26:53 +0000 | [diff] [blame] | 205 | |
| 206 | /* see http://en.wikipedia.org/wiki/Tera */ |
Denis Vlasenko | 9b4c072 | 2008-01-07 16:51:25 +0000 | [diff] [blame] | 207 | smart_ulltoa4(u, buf4, " mgtpezy"); |
| 208 | buf4[4] = '\0'; |
| 209 | sprintf(buf, "%.*s", size, buf4); |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 212 | static void func_vsz(char *buf, int size, const procps_status_t *ps) |
| 213 | { |
Denis Vlasenko | fc1e995 | 2007-08-29 18:23:36 +0000 | [diff] [blame] | 214 | put_lu(buf, size, ps->vsz); |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Denis Vlasenko | 516a0ca | 2007-04-19 14:46:14 +0000 | [diff] [blame] | 217 | static void func_rss(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->rss); |
Denis Vlasenko | 516a0ca | 2007-04-19 14:46:14 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | static void func_tty(char *buf, int size, const procps_status_t *ps) |
| 223 | { |
Denis Vlasenko | 31789a8 | 2007-06-19 14:43:17 +0000 | [diff] [blame] | 224 | buf[0] = '?'; |
| 225 | buf[1] = '\0'; |
| 226 | if (ps->tty_major) /* tty field of "0" means "no tty" */ |
| 227 | snprintf(buf, size+1, "%u,%u", ps->tty_major, ps->tty_minor); |
Denis Vlasenko | 516a0ca | 2007-04-19 14:46:14 +0000 | [diff] [blame] | 228 | } |
Denis Vlasenko | 0696b8a | 2007-06-06 07:40:16 +0000 | [diff] [blame] | 229 | |
Denis Vlasenko | 5fee2e1 | 2008-01-05 03:26:41 +0000 | [diff] [blame] | 230 | #if ENABLE_FEATURE_PS_TIME |
| 231 | static void func_etime(char *buf, int size, const procps_status_t *ps) |
| 232 | { |
| 233 | /* elapsed time [[dd-]hh:]mm:ss; here only mm:ss */ |
| 234 | unsigned long mm; |
| 235 | unsigned ss; |
| 236 | |
| 237 | mm = ps->start_time / get_kernel_HZ(); |
| 238 | /* must be after get_kernel_HZ()! */ |
| 239 | mm = seconds_since_boot - mm; |
| 240 | ss = mm % 60; |
| 241 | mm /= 60; |
| 242 | snprintf(buf, size+1, "%3lu:%02u", mm, ss); |
| 243 | } |
| 244 | |
| 245 | static void func_time(char *buf, int size, const procps_status_t *ps) |
| 246 | { |
| 247 | /* cumulative time [[dd-]hh:]mm:ss; here only mm:ss */ |
| 248 | unsigned long mm; |
| 249 | unsigned ss; |
| 250 | |
| 251 | mm = (ps->utime + ps->stime) / get_kernel_HZ(); |
| 252 | ss = mm % 60; |
| 253 | mm /= 60; |
| 254 | snprintf(buf, size+1, "%3lu:%02u", mm, ss); |
| 255 | } |
| 256 | #endif |
| 257 | |
Denis Vlasenko | 0696b8a | 2007-06-06 07:40:16 +0000 | [diff] [blame] | 258 | #if ENABLE_SELINUX |
| 259 | static void func_label(char *buf, int size, const procps_status_t *ps) |
| 260 | { |
| 261 | safe_strncpy(buf, ps->context ? ps->context : "unknown", size+1); |
| 262 | } |
| 263 | #endif |
| 264 | |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 265 | /* |
Denis Vlasenko | 516a0ca | 2007-04-19 14:46:14 +0000 | [diff] [blame] | 266 | 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] | 267 | { |
| 268 | ps->??? |
| 269 | } |
| 270 | |
Denis Vlasenko | 516a0ca | 2007-04-19 14:46:14 +0000 | [diff] [blame] | 271 | 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] | 272 | { |
| 273 | } |
| 274 | */ |
| 275 | |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 276 | static const ps_out_t out_spec[] = { |
| 277 | // Mandated by POSIX: |
Denis Vlasenko | 98ebab8 | 2007-06-30 14:47:41 +0000 | [diff] [blame] | 278 | { 8 , "user" ,"USER" ,func_user ,PSSCAN_UIDGID }, |
| 279 | { 16 , "comm" ,"COMMAND",func_comm ,PSSCAN_COMM }, |
| 280 | { 256 , "args" ,"COMMAND",func_args ,PSSCAN_COMM }, |
| 281 | { 5 , "pid" ,"PID" ,func_pid ,PSSCAN_PID }, |
| 282 | { 5 , "ppid" ,"PPID" ,func_ppid ,PSSCAN_PPID }, |
| 283 | { 5 , "pgid" ,"PGID" ,func_pgid ,PSSCAN_PGID }, |
Denis Vlasenko | 5fee2e1 | 2008-01-05 03:26:41 +0000 | [diff] [blame] | 284 | #if ENABLE_FEATURE_PS_TIME |
| 285 | { sizeof("ELAPSED")-1, "etime" ,"ELAPSED",func_etime ,PSSCAN_START_TIME }, |
| 286 | #endif |
Denis Vlasenko | 98ebab8 | 2007-06-30 14:47:41 +0000 | [diff] [blame] | 287 | // { sizeof("GROUP" )-1, "group" ,"GROUP" ,func_group ,PSSCAN_UIDGID }, |
| 288 | // { sizeof("NI" )-1, "nice" ,"NI" ,func_nice ,PSSCAN_ }, |
| 289 | // { sizeof("%CPU" )-1, "pcpu" ,"%CPU" ,func_pcpu ,PSSCAN_ }, |
| 290 | // { sizeof("RGROUP" )-1, "rgroup","RGROUP" ,func_rgroup,PSSCAN_UIDGID }, |
| 291 | // { sizeof("RUSER" )-1, "ruser" ,"RUSER" ,func_ruser ,PSSCAN_UIDGID }, |
Denis Vlasenko | 5fee2e1 | 2008-01-05 03:26:41 +0000 | [diff] [blame] | 292 | #if ENABLE_FEATURE_PS_TIME |
| 293 | { 6 , "time" ,"TIME" ,func_time ,PSSCAN_STIME | PSSCAN_UTIME }, |
| 294 | #endif |
Denis Vlasenko | 98ebab8 | 2007-06-30 14:47:41 +0000 | [diff] [blame] | 295 | { 6 , "tty" ,"TT" ,func_tty ,PSSCAN_TTY }, |
| 296 | { 4 , "vsz" ,"VSZ" ,func_vsz ,PSSCAN_VSZ }, |
Denis Vlasenko | 516a0ca | 2007-04-19 14:46:14 +0000 | [diff] [blame] | 297 | // Not mandated by POSIX, but useful: |
Denis Vlasenko | 98ebab8 | 2007-06-30 14:47:41 +0000 | [diff] [blame] | 298 | { 4 , "rss" ,"RSS" ,func_rss ,PSSCAN_RSS }, |
Denis Vlasenko | 0696b8a | 2007-06-06 07:40:16 +0000 | [diff] [blame] | 299 | #if ENABLE_SELINUX |
Denis Vlasenko | 98ebab8 | 2007-06-30 14:47:41 +0000 | [diff] [blame] | 300 | { 35 , "label" ,"LABEL" ,func_label ,PSSCAN_CONTEXT }, |
Denis Vlasenko | 0696b8a | 2007-06-06 07:40:16 +0000 | [diff] [blame] | 301 | #endif |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 302 | }; |
| 303 | |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 304 | static ps_out_t* new_out_t(void) |
| 305 | { |
Denis Vlasenko | deeed59 | 2008-07-08 05:14:36 +0000 | [diff] [blame] | 306 | out = xrealloc_vector(out, 2, out_cnt); |
| 307 | return &out[out_cnt++]; |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | static const ps_out_t* find_out_spec(const char *name) |
| 311 | { |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 312 | unsigned i; |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 313 | for (i = 0; i < ARRAY_SIZE(out_spec); i++) { |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 314 | if (!strcmp(name, out_spec[i].name)) |
| 315 | return &out_spec[i]; |
| 316 | } |
| 317 | bb_error_msg_and_die("bad -o argument '%s'", name); |
| 318 | } |
| 319 | |
| 320 | static void parse_o(char* opt) |
| 321 | { |
| 322 | ps_out_t* new; |
| 323 | // POSIX: "-o is blank- or comma-separated list" (FIXME) |
| 324 | char *comma, *equal; |
| 325 | while (1) { |
| 326 | comma = strchr(opt, ','); |
| 327 | equal = strchr(opt, '='); |
| 328 | if (comma && (!equal || equal > comma)) { |
| 329 | *comma = '\0'; |
| 330 | *new_out_t() = *find_out_spec(opt); |
| 331 | *comma = ','; |
| 332 | opt = comma + 1; |
| 333 | continue; |
| 334 | } |
| 335 | break; |
| 336 | } |
Denis Vlasenko | 516a0ca | 2007-04-19 14:46:14 +0000 | [diff] [blame] | 337 | // opt points to last spec in comma separated list. |
| 338 | // This one can have =HEADER part. |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 339 | new = new_out_t(); |
| 340 | if (equal) |
| 341 | *equal = '\0'; |
| 342 | *new = *find_out_spec(opt); |
| 343 | if (equal) { |
| 344 | *equal = '='; |
| 345 | new->header = equal + 1; |
| 346 | // POSIX: the field widths shall be ... at least as wide as |
| 347 | // the header text (default or overridden value). |
| 348 | // If the header text is null, such as -o user=, |
| 349 | // the field width shall be at least as wide as the |
| 350 | // default header text |
| 351 | if (new->header[0]) { |
| 352 | new->width = strlen(new->header); |
| 353 | print_header = 1; |
| 354 | } |
| 355 | } else |
| 356 | print_header = 1; |
| 357 | } |
| 358 | |
| 359 | static void post_process(void) |
| 360 | { |
| 361 | int i; |
| 362 | int width = 0; |
| 363 | for (i = 0; i < out_cnt; i++) { |
Denis Vlasenko | 8581863 | 2007-04-19 14:47:11 +0000 | [diff] [blame] | 364 | need_flags |= out[i].ps_flags; |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 365 | if (out[i].header[0]) { |
| 366 | print_header = 1; |
| 367 | } |
| 368 | width += out[i].width + 1; /* "FIELD " */ |
| 369 | } |
Denis Vlasenko | 0696b8a | 2007-06-06 07:40:16 +0000 | [diff] [blame] | 370 | #if ENABLE_SELINUX |
| 371 | if (!is_selinux_enabled()) |
| 372 | need_flags &= ~PSSCAN_CONTEXT; |
| 373 | #endif |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 374 | buffer = xmalloc(width + 1); /* for trailing \0 */ |
| 375 | } |
| 376 | |
| 377 | static void format_header(void) |
| 378 | { |
| 379 | int i; |
| 380 | ps_out_t* op; |
Denis Vlasenko | 516a0ca | 2007-04-19 14:46:14 +0000 | [diff] [blame] | 381 | char *p; |
| 382 | |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 383 | if (!print_header) |
| 384 | return; |
Denis Vlasenko | 516a0ca | 2007-04-19 14:46:14 +0000 | [diff] [blame] | 385 | p = buffer; |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 386 | i = 0; |
| 387 | if (out_cnt) { |
| 388 | while (1) { |
| 389 | op = &out[i]; |
| 390 | if (++i == out_cnt) /* do not pad last field */ |
| 391 | break; |
| 392 | p += sprintf(p, "%-*s ", op->width, op->header); |
| 393 | } |
| 394 | strcpy(p, op->header); |
| 395 | } |
| 396 | printf("%.*s\n", terminal_width, buffer); |
| 397 | } |
| 398 | |
| 399 | static void format_process(const procps_status_t *ps) |
| 400 | { |
| 401 | int i, len; |
| 402 | char *p = buffer; |
| 403 | i = 0; |
| 404 | if (out_cnt) while (1) { |
| 405 | out[i].f(p, out[i].width, ps); |
| 406 | // POSIX: Any field need not be meaningful in all |
| 407 | // implementations. In such a case a hyphen ( '-' ) |
| 408 | // should be output in place of the field value. |
Denis Vlasenko | 6dca70a | 2006-12-18 14:20:01 +0000 | [diff] [blame] | 409 | if (!p[0]) { |
| 410 | p[0] = '-'; |
| 411 | p[1] = '\0'; |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 412 | } |
| 413 | len = strlen(p); |
| 414 | p += len; |
| 415 | len = out[i].width - len + 1; |
| 416 | if (++i == out_cnt) /* do not pad last field */ |
| 417 | break; |
Denis Vlasenko | 6dca70a | 2006-12-18 14:20:01 +0000 | [diff] [blame] | 418 | p += sprintf(p, "%*s", len, ""); |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 419 | } |
| 420 | printf("%.*s\n", terminal_width, buffer); |
| 421 | } |
| 422 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 423 | int ps_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 424 | int ps_main(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 425 | { |
| 426 | procps_status_t *p; |
| 427 | llist_t* opt_o = NULL; |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 428 | IF_SELINUX(int opt;) |
Denis Vlasenko | 8581863 | 2007-04-19 14:47:11 +0000 | [diff] [blame] | 429 | |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 430 | // POSIX: |
| 431 | // -a Write information for all processes associated with terminals |
| 432 | // Implementations may omit session leaders from this list |
| 433 | // -A Write information for all processes |
| 434 | // -d Write information for all processes, except session leaders |
| 435 | // -e Write information for all processes (equivalent to -A.) |
| 436 | // -f Generate a full listing |
| 437 | // -l Generate a long listing |
| 438 | // -o col1,col2,col3=header |
Denis Vlasenko | 516a0ca | 2007-04-19 14:46:14 +0000 | [diff] [blame] | 439 | // Select which columns to display |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 440 | /* We allow (and ignore) most of the above. FIXME */ |
| 441 | opt_complementary = "o::"; |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 442 | IF_SELINUX(opt =) getopt32(argv, "Zo:aAdefl", &opt_o); |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 443 | if (opt_o) { |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 444 | do { |
Denis Vlasenko | d50dda8 | 2008-06-15 05:40:56 +0000 | [diff] [blame] | 445 | parse_o(llist_pop(&opt_o)); |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 446 | } while (opt_o); |
Denis Vlasenko | 0696b8a | 2007-06-06 07:40:16 +0000 | [diff] [blame] | 447 | } else { |
| 448 | /* Below: parse_o() needs char*, NOT const char*... */ |
| 449 | #if ENABLE_SELINUX |
| 450 | if (!(opt & 1) || !is_selinux_enabled()) { |
| 451 | /* no -Z or no SELinux: do not show LABEL */ |
Denis Vlasenko | 83cea0e | 2008-07-16 21:11:09 +0000 | [diff] [blame] | 452 | strcpy(default_o, DEFAULT_O_STR + sizeof(SELINUX_O_PREFIX)-1); |
Denis Vlasenko | 0696b8a | 2007-06-06 07:40:16 +0000 | [diff] [blame] | 453 | } else |
| 454 | #endif |
| 455 | { |
| 456 | strcpy(default_o, DEFAULT_O_STR); |
| 457 | } |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 458 | parse_o(default_o); |
Denis Vlasenko | 0696b8a | 2007-06-06 07:40:16 +0000 | [diff] [blame] | 459 | } |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 460 | post_process(); |
| 461 | |
Denis Vlasenko | 450196c | 2007-03-28 21:57:12 +0000 | [diff] [blame] | 462 | /* Was INT_MAX, but some libc's go belly up with printf("%.*s") |
| 463 | * and such large widths */ |
Denis Vlasenko | 7ce7594 | 2007-07-25 17:27:58 +0000 | [diff] [blame] | 464 | terminal_width = MAX_WIDTH; |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 465 | if (isatty(1)) { |
Denis Vlasenko | f893da8 | 2007-08-09 08:27:24 +0000 | [diff] [blame] | 466 | get_terminal_width_height(0, &terminal_width, NULL); |
Denis Vlasenko | 7ce7594 | 2007-07-25 17:27:58 +0000 | [diff] [blame] | 467 | if (--terminal_width > MAX_WIDTH) |
| 468 | terminal_width = MAX_WIDTH; |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 469 | } |
| 470 | format_header(); |
| 471 | |
| 472 | p = NULL; |
Denis Vlasenko | 8581863 | 2007-04-19 14:47:11 +0000 | [diff] [blame] | 473 | while ((p = procps_scan(p, need_flags))) { |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 474 | format_process(p); |
| 475 | } |
| 476 | |
| 477 | return EXIT_SUCCESS; |
| 478 | } |
| 479 | |
| 480 | |
| 481 | #else /* !ENABLE_DESKTOP */ |
| 482 | |
| 483 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 484 | int ps_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 485 | int ps_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | ef8b6c7 | 1999-10-20 08:05:35 +0000 | [diff] [blame] | 486 | { |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 487 | procps_status_t *p = NULL; |
Denis Vlasenko | dc7a5ea | 2007-07-01 14:53:06 +0000 | [diff] [blame] | 488 | int len; |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 489 | IF_NOT_SELINUX(const) int use_selinux = 0; |
| 490 | IF_SELINUX(int i;) |
Denis Vlasenko | d3ada32 | 2006-11-01 09:14:52 +0000 | [diff] [blame] | 491 | #if !ENABLE_FEATURE_PS_WIDE |
| 492 | enum { terminal_width = 79 }; |
| 493 | #else |
Denis Vlasenko | 54b90b5 | 2008-05-20 20:44:08 +0000 | [diff] [blame] | 494 | unsigned terminal_width; |
"Vladimir N. Oleynik" | be0ed3d | 2005-10-04 16:48:26 +0000 | [diff] [blame] | 495 | int w_count = 0; |
"Vladimir N. Oleynik" | be0ed3d | 2005-10-04 16:48:26 +0000 | [diff] [blame] | 496 | #endif |
Bernhard Reutner-Fischer | 6d6a40c | 2005-10-04 14:31:18 +0000 | [diff] [blame] | 497 | |
| 498 | #if ENABLE_FEATURE_PS_WIDE || ENABLE_SELINUX |
"Vladimir N. Oleynik" | be0ed3d | 2005-10-04 16:48:26 +0000 | [diff] [blame] | 499 | #if ENABLE_FEATURE_PS_WIDE |
Denis Vlasenko | d3ada32 | 2006-11-01 09:14:52 +0000 | [diff] [blame] | 500 | opt_complementary = "-:ww"; |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 501 | IF_SELINUX(i =) getopt32(argv, IF_SELINUX("Z") "w", &w_count); |
"Vladimir N. Oleynik" | be0ed3d | 2005-10-04 16:48:26 +0000 | [diff] [blame] | 502 | /* if w is given once, GNU ps sets the width to 132, |
| 503 | * if w is given more than once, it is "unlimited" |
| 504 | */ |
Denis Vlasenko | 621204b | 2006-10-27 09:03:24 +0000 | [diff] [blame] | 505 | if (w_count) { |
Denis Vlasenko | 7ce7594 | 2007-07-25 17:27:58 +0000 | [diff] [blame] | 506 | terminal_width = (w_count==1) ? 132 : MAX_WIDTH; |
"Vladimir N. Oleynik" | be0ed3d | 2005-10-04 16:48:26 +0000 | [diff] [blame] | 507 | } else { |
Denis Vlasenko | f893da8 | 2007-08-09 08:27:24 +0000 | [diff] [blame] | 508 | get_terminal_width_height(0, &terminal_width, NULL); |
"Vladimir N. Oleynik" | be0ed3d | 2005-10-04 16:48:26 +0000 | [diff] [blame] | 509 | /* Go one less... */ |
Denis Vlasenko | 7ce7594 | 2007-07-25 17:27:58 +0000 | [diff] [blame] | 510 | if (--terminal_width > MAX_WIDTH) |
| 511 | terminal_width = MAX_WIDTH; |
Bernhard Reutner-Fischer | 6d6a40c | 2005-10-04 14:31:18 +0000 | [diff] [blame] | 512 | } |
Denis Vlasenko | d3ada32 | 2006-11-01 09:14:52 +0000 | [diff] [blame] | 513 | #else /* only ENABLE_SELINUX */ |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 514 | i = getopt32(argv, "Z"); |
Bernhard Reutner-Fischer | 6d6a40c | 2005-10-04 14:31:18 +0000 | [diff] [blame] | 515 | #endif |
"Vladimir N. Oleynik" | be0ed3d | 2005-10-04 16:48:26 +0000 | [diff] [blame] | 516 | #if ENABLE_SELINUX |
Denis Vlasenko | aba7f49 | 2007-07-01 17:25:59 +0000 | [diff] [blame] | 517 | if ((i & 1) && is_selinux_enabled()) |
Denis Vlasenko | 0696b8a | 2007-06-06 07:40:16 +0000 | [diff] [blame] | 518 | use_selinux = PSSCAN_CONTEXT; |
"Vladimir N. Oleynik" | be0ed3d | 2005-10-04 16:48:26 +0000 | [diff] [blame] | 519 | #endif |
Denis Vlasenko | d3ada32 | 2006-11-01 09:14:52 +0000 | [diff] [blame] | 520 | #endif /* ENABLE_FEATURE_PS_WIDE || ENABLE_SELINUX */ |
Bernhard Reutner-Fischer | 6d6a40c | 2005-10-04 14:31:18 +0000 | [diff] [blame] | 521 | |
Rob Landley | 60158cb | 2005-05-03 06:25:50 +0000 | [diff] [blame] | 522 | if (use_selinux) |
Denis Vlasenko | 56ea65c | 2008-01-06 03:26:53 +0000 | [diff] [blame] | 523 | puts(" PID CONTEXT STAT COMMAND"); |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 524 | else |
Denis Vlasenko | 56ea65c | 2008-01-06 03:26:53 +0000 | [diff] [blame] | 525 | puts(" PID USER VSZ STAT COMMAND"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 526 | |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 527 | while ((p = procps_scan(p, 0 |
| 528 | | PSSCAN_PID |
| 529 | | PSSCAN_UIDGID |
| 530 | | PSSCAN_STATE |
Mike Frysinger | 0aa6ba5 | 2007-02-08 08:21:58 +0000 | [diff] [blame] | 531 | | PSSCAN_VSZ |
Denis Vlasenko | 98ebab8 | 2007-06-30 14:47:41 +0000 | [diff] [blame] | 532 | | PSSCAN_COMM |
Denis Vlasenko | 0696b8a | 2007-06-06 07:40:16 +0000 | [diff] [blame] | 533 | | use_selinux |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 534 | ))) { |
Bernhard Reutner-Fischer | 6d6a40c | 2005-10-04 14:31:18 +0000 | [diff] [blame] | 535 | #if ENABLE_SELINUX |
Denis Vlasenko | be905d5 | 2006-09-27 14:17:31 +0000 | [diff] [blame] | 536 | if (use_selinux) { |
Denis Vlasenko | 766f659 | 2008-01-07 16:41:41 +0000 | [diff] [blame] | 537 | len = printf("%5u %-32.32s %s ", |
Denis Vlasenko | 0696b8a | 2007-06-06 07:40:16 +0000 | [diff] [blame] | 538 | p->pid, |
| 539 | p->context ? p->context : "unknown", |
| 540 | p->state); |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 541 | } else |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 542 | #endif |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 543 | { |
| 544 | const char *user = get_cached_username(p->uid); |
Denis Vlasenko | 56ea65c | 2008-01-06 03:26:53 +0000 | [diff] [blame] | 545 | //if (p->vsz == 0) |
| 546 | // len = printf("%5u %-8.8s %s ", |
| 547 | // p->pid, user, p->state); |
| 548 | //else |
| 549 | { |
| 550 | char buf6[6]; |
| 551 | smart_ulltoa5(p->vsz, buf6, " mgtpezy"); |
| 552 | buf6[5] = '\0'; |
Denis Vlasenko | 766f659 | 2008-01-07 16:41:41 +0000 | [diff] [blame] | 553 | len = printf("%5u %-8.8s %s %s ", |
Denis Vlasenko | 56ea65c | 2008-01-06 03:26:53 +0000 | [diff] [blame] | 554 | p->pid, user, buf6, p->state); |
| 555 | } |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 556 | } |
Bernhard Reutner-Fischer | 6d6a40c | 2005-10-04 14:31:18 +0000 | [diff] [blame] | 557 | |
Denis Vlasenko | 98ebab8 | 2007-06-30 14:47:41 +0000 | [diff] [blame] | 558 | { |
Denis Vlasenko | dc7a5ea | 2007-07-01 14:53:06 +0000 | [diff] [blame] | 559 | int sz = terminal_width - len; |
Denis Vlasenko | 98ebab8 | 2007-06-30 14:47:41 +0000 | [diff] [blame] | 560 | char buf[sz + 1]; |
| 561 | read_cmdline(buf, sz, p->pid, p->comm); |
| 562 | puts(buf); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 563 | } |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 564 | } |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 565 | if (ENABLE_FEATURE_CLEAN_UP) |
| 566 | clear_username_cache(); |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 567 | return EXIT_SUCCESS; |
Eric Andersen | ef8b6c7 | 1999-10-20 08:05:35 +0000 | [diff] [blame] | 568 | } |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 569 | |
Denis Vlasenko | 4021516 | 2006-11-05 00:47:38 +0000 | [diff] [blame] | 570 | #endif /* ENABLE_DESKTOP */ |