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> |
| 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 | |
| 19 | /* Print value to buf, max size+1 chars (including trailing '\0') */ |
| 20 | |
Denis Vlasenko | 769d1e0 | 2007-01-22 23:04:27 +0000 | [diff] [blame] | 21 | 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] | 22 | { |
| 23 | safe_strncpy(buf, get_cached_username(ps->uid), size+1); |
| 24 | } |
| 25 | |
Denis Vlasenko | 769d1e0 | 2007-01-22 23:04:27 +0000 | [diff] [blame] | 26 | 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] | 27 | { |
| 28 | safe_strncpy(buf, ps->comm, size+1); |
| 29 | } |
| 30 | |
Denis Vlasenko | 769d1e0 | 2007-01-22 23:04:27 +0000 | [diff] [blame] | 31 | 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] | 32 | { |
Denis Vlasenko | 98ebab8 | 2007-06-30 14:47:41 +0000 | [diff] [blame] | 33 | read_cmdline(buf, size, ps->pid, ps->comm); |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 34 | } |
| 35 | |
Denis Vlasenko | 769d1e0 | 2007-01-22 23:04:27 +0000 | [diff] [blame] | 36 | 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] | 37 | { |
Denis Vlasenko | 516a0ca | 2007-04-19 14:46:14 +0000 | [diff] [blame] | 38 | sprintf(buf, "%*u", size, ps->pid); |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Denis Vlasenko | 769d1e0 | 2007-01-22 23:04:27 +0000 | [diff] [blame] | 41 | 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] | 42 | { |
Denis Vlasenko | 516a0ca | 2007-04-19 14:46:14 +0000 | [diff] [blame] | 43 | sprintf(buf, "%*u", size, ps->ppid); |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Denis Vlasenko | 769d1e0 | 2007-01-22 23:04:27 +0000 | [diff] [blame] | 46 | 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] | 47 | { |
Denis Vlasenko | 516a0ca | 2007-04-19 14:46:14 +0000 | [diff] [blame] | 48 | sprintf(buf, "%*u", size, ps->pgid); |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Denis Vlasenko | fc1e995 | 2007-08-29 18:23:36 +0000 | [diff] [blame] | 51 | static void put_lu(char *buf, int size, unsigned long u) |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 52 | { |
| 53 | char buf5[5]; |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 54 | smart_ulltoa5( ((unsigned long long)u) << 10, buf5); |
Denis Vlasenko | 516a0ca | 2007-04-19 14:46:14 +0000 | [diff] [blame] | 55 | sprintf(buf, "%.*s", size, buf5); |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 58 | static void func_vsz(char *buf, int size, const procps_status_t *ps) |
| 59 | { |
Denis Vlasenko | fc1e995 | 2007-08-29 18:23:36 +0000 | [diff] [blame] | 60 | put_lu(buf, size, ps->vsz); |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Denis Vlasenko | 516a0ca | 2007-04-19 14:46:14 +0000 | [diff] [blame] | 63 | static void func_rss(char *buf, int size, const procps_status_t *ps) |
| 64 | { |
Denis Vlasenko | fc1e995 | 2007-08-29 18:23:36 +0000 | [diff] [blame] | 65 | put_lu(buf, size, ps->rss); |
Denis Vlasenko | 516a0ca | 2007-04-19 14:46:14 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | static void func_tty(char *buf, int size, const procps_status_t *ps) |
| 69 | { |
Denis Vlasenko | 31789a8 | 2007-06-19 14:43:17 +0000 | [diff] [blame] | 70 | buf[0] = '?'; |
| 71 | buf[1] = '\0'; |
| 72 | if (ps->tty_major) /* tty field of "0" means "no tty" */ |
| 73 | snprintf(buf, size+1, "%u,%u", ps->tty_major, ps->tty_minor); |
Denis Vlasenko | 516a0ca | 2007-04-19 14:46:14 +0000 | [diff] [blame] | 74 | } |
Denis Vlasenko | 0696b8a | 2007-06-06 07:40:16 +0000 | [diff] [blame] | 75 | |
| 76 | #if ENABLE_SELINUX |
| 77 | static void func_label(char *buf, int size, const procps_status_t *ps) |
| 78 | { |
| 79 | safe_strncpy(buf, ps->context ? ps->context : "unknown", size+1); |
| 80 | } |
| 81 | #endif |
| 82 | |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 83 | /* |
Denis Vlasenko | 516a0ca | 2007-04-19 14:46:14 +0000 | [diff] [blame] | 84 | 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] | 85 | { |
| 86 | ps->??? |
| 87 | } |
| 88 | |
Denis Vlasenko | 516a0ca | 2007-04-19 14:46:14 +0000 | [diff] [blame] | 89 | static void func_etime(char *buf, int size, const procps_status_t *ps) |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 90 | { |
| 91 | elapled time [[dd-]hh:]mm:ss |
| 92 | } |
| 93 | |
Denis Vlasenko | 516a0ca | 2007-04-19 14:46:14 +0000 | [diff] [blame] | 94 | static void func_time(char *buf, int size, const procps_status_t *ps) |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 95 | { |
| 96 | cumulative time [[dd-]hh:]mm:ss |
| 97 | } |
| 98 | |
Denis Vlasenko | 516a0ca | 2007-04-19 14:46:14 +0000 | [diff] [blame] | 99 | 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] | 100 | { |
| 101 | } |
| 102 | */ |
| 103 | |
| 104 | typedef struct { |
Denis Vlasenko | 516a0ca | 2007-04-19 14:46:14 +0000 | [diff] [blame] | 105 | uint16_t width; |
| 106 | char name[6]; |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 107 | const char *header; |
| 108 | void (*f)(char *buf, int size, const procps_status_t *ps); |
| 109 | int ps_flags; |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 110 | } ps_out_t; |
| 111 | |
| 112 | static const ps_out_t out_spec[] = { |
| 113 | // Mandated by POSIX: |
Denis Vlasenko | 98ebab8 | 2007-06-30 14:47:41 +0000 | [diff] [blame] | 114 | { 8 , "user" ,"USER" ,func_user ,PSSCAN_UIDGID }, |
| 115 | { 16 , "comm" ,"COMMAND",func_comm ,PSSCAN_COMM }, |
| 116 | { 256 , "args" ,"COMMAND",func_args ,PSSCAN_COMM }, |
| 117 | { 5 , "pid" ,"PID" ,func_pid ,PSSCAN_PID }, |
| 118 | { 5 , "ppid" ,"PPID" ,func_ppid ,PSSCAN_PPID }, |
| 119 | { 5 , "pgid" ,"PGID" ,func_pgid ,PSSCAN_PGID }, |
| 120 | // { sizeof("ELAPSED")-1, "etime" ,"ELAPSED",func_etime ,PSSCAN_ }, |
| 121 | // { sizeof("GROUP" )-1, "group" ,"GROUP" ,func_group ,PSSCAN_UIDGID }, |
| 122 | // { sizeof("NI" )-1, "nice" ,"NI" ,func_nice ,PSSCAN_ }, |
| 123 | // { sizeof("%CPU" )-1, "pcpu" ,"%CPU" ,func_pcpu ,PSSCAN_ }, |
| 124 | // { sizeof("RGROUP" )-1, "rgroup","RGROUP" ,func_rgroup,PSSCAN_UIDGID }, |
| 125 | // { sizeof("RUSER" )-1, "ruser" ,"RUSER" ,func_ruser ,PSSCAN_UIDGID }, |
| 126 | // { sizeof("TIME" )-1, "time" ,"TIME" ,func_time ,PSSCAN_ }, |
| 127 | { 6 , "tty" ,"TT" ,func_tty ,PSSCAN_TTY }, |
| 128 | { 4 , "vsz" ,"VSZ" ,func_vsz ,PSSCAN_VSZ }, |
Denis Vlasenko | 516a0ca | 2007-04-19 14:46:14 +0000 | [diff] [blame] | 129 | // Not mandated by POSIX, but useful: |
Denis Vlasenko | 98ebab8 | 2007-06-30 14:47:41 +0000 | [diff] [blame] | 130 | { 4 , "rss" ,"RSS" ,func_rss ,PSSCAN_RSS }, |
Denis Vlasenko | 0696b8a | 2007-06-06 07:40:16 +0000 | [diff] [blame] | 131 | #if ENABLE_SELINUX |
Denis Vlasenko | 98ebab8 | 2007-06-30 14:47:41 +0000 | [diff] [blame] | 132 | { 35 , "label" ,"LABEL" ,func_label ,PSSCAN_CONTEXT }, |
Denis Vlasenko | 0696b8a | 2007-06-06 07:40:16 +0000 | [diff] [blame] | 133 | #endif |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 134 | }; |
| 135 | |
Denis Vlasenko | 0696b8a | 2007-06-06 07:40:16 +0000 | [diff] [blame] | 136 | #if ENABLE_SELINUX |
| 137 | #define SELINIX_O_PREFIX "label," |
| 138 | #define DEFAULT_O_STR SELINIX_O_PREFIX "pid,user" /* TODO: ,vsz,stat */ ",args" |
| 139 | #else |
| 140 | #define DEFAULT_O_STR "pid,user" /* TODO: ,vsz,stat */ ",args" |
| 141 | #endif |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 142 | |
Denis Vlasenko | 8581863 | 2007-04-19 14:47:11 +0000 | [diff] [blame] | 143 | struct globals { |
| 144 | ps_out_t* out; |
| 145 | int out_cnt; |
| 146 | int print_header; |
| 147 | int need_flags; |
| 148 | char *buffer; |
| 149 | unsigned terminal_width; |
| 150 | char default_o[sizeof(DEFAULT_O_STR)]; |
| 151 | }; |
| 152 | #define G (*(struct globals*)&bb_common_bufsiz1) |
| 153 | #define out (G.out ) |
| 154 | #define out_cnt (G.out_cnt ) |
| 155 | #define print_header (G.print_header ) |
| 156 | #define need_flags (G.need_flags ) |
| 157 | #define buffer (G.buffer ) |
| 158 | #define terminal_width (G.terminal_width) |
| 159 | #define default_o (G.default_o ) |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 160 | |
| 161 | static ps_out_t* new_out_t(void) |
| 162 | { |
| 163 | int i = out_cnt++; |
| 164 | out = xrealloc(out, out_cnt * sizeof(*out)); |
| 165 | return &out[i]; |
| 166 | } |
| 167 | |
| 168 | static const ps_out_t* find_out_spec(const char *name) |
| 169 | { |
| 170 | int i; |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 171 | for (i = 0; i < ARRAY_SIZE(out_spec); i++) { |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 172 | if (!strcmp(name, out_spec[i].name)) |
| 173 | return &out_spec[i]; |
| 174 | } |
| 175 | bb_error_msg_and_die("bad -o argument '%s'", name); |
| 176 | } |
| 177 | |
| 178 | static void parse_o(char* opt) |
| 179 | { |
| 180 | ps_out_t* new; |
| 181 | // POSIX: "-o is blank- or comma-separated list" (FIXME) |
| 182 | char *comma, *equal; |
| 183 | while (1) { |
| 184 | comma = strchr(opt, ','); |
| 185 | equal = strchr(opt, '='); |
| 186 | if (comma && (!equal || equal > comma)) { |
| 187 | *comma = '\0'; |
| 188 | *new_out_t() = *find_out_spec(opt); |
| 189 | *comma = ','; |
| 190 | opt = comma + 1; |
| 191 | continue; |
| 192 | } |
| 193 | break; |
| 194 | } |
Denis Vlasenko | 516a0ca | 2007-04-19 14:46:14 +0000 | [diff] [blame] | 195 | // opt points to last spec in comma separated list. |
| 196 | // This one can have =HEADER part. |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 197 | new = new_out_t(); |
| 198 | if (equal) |
| 199 | *equal = '\0'; |
| 200 | *new = *find_out_spec(opt); |
| 201 | if (equal) { |
| 202 | *equal = '='; |
| 203 | new->header = equal + 1; |
| 204 | // POSIX: the field widths shall be ... at least as wide as |
| 205 | // the header text (default or overridden value). |
| 206 | // If the header text is null, such as -o user=, |
| 207 | // the field width shall be at least as wide as the |
| 208 | // default header text |
| 209 | if (new->header[0]) { |
| 210 | new->width = strlen(new->header); |
| 211 | print_header = 1; |
| 212 | } |
| 213 | } else |
| 214 | print_header = 1; |
| 215 | } |
| 216 | |
| 217 | static void post_process(void) |
| 218 | { |
| 219 | int i; |
| 220 | int width = 0; |
| 221 | for (i = 0; i < out_cnt; i++) { |
Denis Vlasenko | 8581863 | 2007-04-19 14:47:11 +0000 | [diff] [blame] | 222 | need_flags |= out[i].ps_flags; |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 223 | if (out[i].header[0]) { |
| 224 | print_header = 1; |
| 225 | } |
| 226 | width += out[i].width + 1; /* "FIELD " */ |
| 227 | } |
Denis Vlasenko | 0696b8a | 2007-06-06 07:40:16 +0000 | [diff] [blame] | 228 | #if ENABLE_SELINUX |
| 229 | if (!is_selinux_enabled()) |
| 230 | need_flags &= ~PSSCAN_CONTEXT; |
| 231 | #endif |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 232 | buffer = xmalloc(width + 1); /* for trailing \0 */ |
| 233 | } |
| 234 | |
| 235 | static void format_header(void) |
| 236 | { |
| 237 | int i; |
| 238 | ps_out_t* op; |
Denis Vlasenko | 516a0ca | 2007-04-19 14:46:14 +0000 | [diff] [blame] | 239 | char *p; |
| 240 | |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 241 | if (!print_header) |
| 242 | return; |
Denis Vlasenko | 516a0ca | 2007-04-19 14:46:14 +0000 | [diff] [blame] | 243 | p = buffer; |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 244 | i = 0; |
| 245 | if (out_cnt) { |
| 246 | while (1) { |
| 247 | op = &out[i]; |
| 248 | if (++i == out_cnt) /* do not pad last field */ |
| 249 | break; |
| 250 | p += sprintf(p, "%-*s ", op->width, op->header); |
| 251 | } |
| 252 | strcpy(p, op->header); |
| 253 | } |
| 254 | printf("%.*s\n", terminal_width, buffer); |
| 255 | } |
| 256 | |
| 257 | static void format_process(const procps_status_t *ps) |
| 258 | { |
| 259 | int i, len; |
| 260 | char *p = buffer; |
| 261 | i = 0; |
| 262 | if (out_cnt) while (1) { |
| 263 | out[i].f(p, out[i].width, ps); |
| 264 | // POSIX: Any field need not be meaningful in all |
| 265 | // implementations. In such a case a hyphen ( '-' ) |
| 266 | // should be output in place of the field value. |
Denis Vlasenko | 6dca70a | 2006-12-18 14:20:01 +0000 | [diff] [blame] | 267 | if (!p[0]) { |
| 268 | p[0] = '-'; |
| 269 | p[1] = '\0'; |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 270 | } |
| 271 | len = strlen(p); |
| 272 | p += len; |
| 273 | len = out[i].width - len + 1; |
| 274 | if (++i == out_cnt) /* do not pad last field */ |
| 275 | break; |
Denis Vlasenko | 6dca70a | 2006-12-18 14:20:01 +0000 | [diff] [blame] | 276 | p += sprintf(p, "%*s", len, ""); |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 277 | } |
| 278 | printf("%.*s\n", terminal_width, buffer); |
| 279 | } |
| 280 | |
Denis Vlasenko | 06af216 | 2007-02-03 17:28:39 +0000 | [diff] [blame] | 281 | int ps_main(int argc, char **argv); |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 282 | int ps_main(int argc, char **argv) |
| 283 | { |
| 284 | procps_status_t *p; |
| 285 | llist_t* opt_o = NULL; |
Denis Vlasenko | 0696b8a | 2007-06-06 07:40:16 +0000 | [diff] [blame] | 286 | USE_SELINUX(int opt;) |
Denis Vlasenko | 8581863 | 2007-04-19 14:47:11 +0000 | [diff] [blame] | 287 | |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 288 | // POSIX: |
| 289 | // -a Write information for all processes associated with terminals |
| 290 | // Implementations may omit session leaders from this list |
| 291 | // -A Write information for all processes |
| 292 | // -d Write information for all processes, except session leaders |
| 293 | // -e Write information for all processes (equivalent to -A.) |
| 294 | // -f Generate a full listing |
| 295 | // -l Generate a long listing |
| 296 | // -o col1,col2,col3=header |
Denis Vlasenko | 516a0ca | 2007-04-19 14:46:14 +0000 | [diff] [blame] | 297 | // Select which columns to display |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 298 | /* We allow (and ignore) most of the above. FIXME */ |
| 299 | opt_complementary = "o::"; |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 300 | USE_SELINUX(opt =) getopt32(argv, "Zo:aAdefl", &opt_o); |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 301 | if (opt_o) { |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 302 | do { |
| 303 | parse_o(opt_o->data); |
| 304 | opt_o = opt_o->link; |
| 305 | } while (opt_o); |
Denis Vlasenko | 0696b8a | 2007-06-06 07:40:16 +0000 | [diff] [blame] | 306 | } else { |
| 307 | /* Below: parse_o() needs char*, NOT const char*... */ |
| 308 | #if ENABLE_SELINUX |
| 309 | if (!(opt & 1) || !is_selinux_enabled()) { |
| 310 | /* no -Z or no SELinux: do not show LABEL */ |
| 311 | strcpy(default_o, DEFAULT_O_STR + sizeof(SELINIX_O_PREFIX)-1); |
| 312 | } else |
| 313 | #endif |
| 314 | { |
| 315 | strcpy(default_o, DEFAULT_O_STR); |
| 316 | } |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 317 | parse_o(default_o); |
Denis Vlasenko | 0696b8a | 2007-06-06 07:40:16 +0000 | [diff] [blame] | 318 | } |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 319 | post_process(); |
| 320 | |
Denis Vlasenko | 450196c | 2007-03-28 21:57:12 +0000 | [diff] [blame] | 321 | /* Was INT_MAX, but some libc's go belly up with printf("%.*s") |
| 322 | * and such large widths */ |
Denis Vlasenko | 7ce7594 | 2007-07-25 17:27:58 +0000 | [diff] [blame] | 323 | terminal_width = MAX_WIDTH; |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 324 | if (isatty(1)) { |
Denis Vlasenko | f893da8 | 2007-08-09 08:27:24 +0000 | [diff] [blame] | 325 | get_terminal_width_height(0, &terminal_width, NULL); |
Denis Vlasenko | 7ce7594 | 2007-07-25 17:27:58 +0000 | [diff] [blame] | 326 | if (--terminal_width > MAX_WIDTH) |
| 327 | terminal_width = MAX_WIDTH; |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 328 | } |
| 329 | format_header(); |
| 330 | |
| 331 | p = NULL; |
Denis Vlasenko | 8581863 | 2007-04-19 14:47:11 +0000 | [diff] [blame] | 332 | while ((p = procps_scan(p, need_flags))) { |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 333 | format_process(p); |
| 334 | } |
| 335 | |
| 336 | return EXIT_SUCCESS; |
| 337 | } |
| 338 | |
| 339 | |
| 340 | #else /* !ENABLE_DESKTOP */ |
| 341 | |
| 342 | |
Denis Vlasenko | 06af216 | 2007-02-03 17:28:39 +0000 | [diff] [blame] | 343 | int ps_main(int argc, char **argv); |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 344 | int ps_main(int argc, char **argv) |
Eric Andersen | ef8b6c7 | 1999-10-20 08:05:35 +0000 | [diff] [blame] | 345 | { |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 346 | procps_status_t *p = NULL; |
Denis Vlasenko | dc7a5ea | 2007-07-01 14:53:06 +0000 | [diff] [blame] | 347 | int len; |
Denis Vlasenko | d3ada32 | 2006-11-01 09:14:52 +0000 | [diff] [blame] | 348 | SKIP_SELINUX(const) int use_selinux = 0; |
Denis Vlasenko | dc7a5ea | 2007-07-01 14:53:06 +0000 | [diff] [blame] | 349 | USE_SELINUX(int i;) |
Denis Vlasenko | d3ada32 | 2006-11-01 09:14:52 +0000 | [diff] [blame] | 350 | #if !ENABLE_FEATURE_PS_WIDE |
| 351 | enum { terminal_width = 79 }; |
| 352 | #else |
"Vladimir N. Oleynik" | be0ed3d | 2005-10-04 16:48:26 +0000 | [diff] [blame] | 353 | int terminal_width; |
| 354 | int w_count = 0; |
"Vladimir N. Oleynik" | be0ed3d | 2005-10-04 16:48:26 +0000 | [diff] [blame] | 355 | #endif |
Bernhard Reutner-Fischer | 6d6a40c | 2005-10-04 14:31:18 +0000 | [diff] [blame] | 356 | |
| 357 | #if ENABLE_FEATURE_PS_WIDE || ENABLE_SELINUX |
"Vladimir N. Oleynik" | be0ed3d | 2005-10-04 16:48:26 +0000 | [diff] [blame] | 358 | #if ENABLE_FEATURE_PS_WIDE |
Denis Vlasenko | d3ada32 | 2006-11-01 09:14:52 +0000 | [diff] [blame] | 359 | opt_complementary = "-:ww"; |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 360 | USE_SELINUX(i =) getopt32(argv, USE_SELINUX("Z") "w", &w_count); |
"Vladimir N. Oleynik" | be0ed3d | 2005-10-04 16:48:26 +0000 | [diff] [blame] | 361 | /* if w is given once, GNU ps sets the width to 132, |
| 362 | * if w is given more than once, it is "unlimited" |
| 363 | */ |
Denis Vlasenko | 621204b | 2006-10-27 09:03:24 +0000 | [diff] [blame] | 364 | if (w_count) { |
Denis Vlasenko | 7ce7594 | 2007-07-25 17:27:58 +0000 | [diff] [blame] | 365 | terminal_width = (w_count==1) ? 132 : MAX_WIDTH; |
"Vladimir N. Oleynik" | be0ed3d | 2005-10-04 16:48:26 +0000 | [diff] [blame] | 366 | } else { |
Denis Vlasenko | f893da8 | 2007-08-09 08:27:24 +0000 | [diff] [blame] | 367 | get_terminal_width_height(0, &terminal_width, NULL); |
"Vladimir N. Oleynik" | be0ed3d | 2005-10-04 16:48:26 +0000 | [diff] [blame] | 368 | /* Go one less... */ |
Denis Vlasenko | 7ce7594 | 2007-07-25 17:27:58 +0000 | [diff] [blame] | 369 | if (--terminal_width > MAX_WIDTH) |
| 370 | terminal_width = MAX_WIDTH; |
Bernhard Reutner-Fischer | 6d6a40c | 2005-10-04 14:31:18 +0000 | [diff] [blame] | 371 | } |
Denis Vlasenko | d3ada32 | 2006-11-01 09:14:52 +0000 | [diff] [blame] | 372 | #else /* only ENABLE_SELINUX */ |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 373 | i = getopt32(argv, "Z"); |
Bernhard Reutner-Fischer | 6d6a40c | 2005-10-04 14:31:18 +0000 | [diff] [blame] | 374 | #endif |
"Vladimir N. Oleynik" | be0ed3d | 2005-10-04 16:48:26 +0000 | [diff] [blame] | 375 | #if ENABLE_SELINUX |
Denis Vlasenko | aba7f49 | 2007-07-01 17:25:59 +0000 | [diff] [blame] | 376 | if ((i & 1) && is_selinux_enabled()) |
Denis Vlasenko | 0696b8a | 2007-06-06 07:40:16 +0000 | [diff] [blame] | 377 | use_selinux = PSSCAN_CONTEXT; |
"Vladimir N. Oleynik" | be0ed3d | 2005-10-04 16:48:26 +0000 | [diff] [blame] | 378 | #endif |
Denis Vlasenko | d3ada32 | 2006-11-01 09:14:52 +0000 | [diff] [blame] | 379 | #endif /* ENABLE_FEATURE_PS_WIDE || ENABLE_SELINUX */ |
Bernhard Reutner-Fischer | 6d6a40c | 2005-10-04 14:31:18 +0000 | [diff] [blame] | 380 | |
Rob Landley | 60158cb | 2005-05-03 06:25:50 +0000 | [diff] [blame] | 381 | if (use_selinux) |
Denis Vlasenko | be905d5 | 2006-09-27 14:17:31 +0000 | [diff] [blame] | 382 | puts(" PID Context Stat Command"); |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 383 | else |
Mike Frysinger | 0aa6ba5 | 2007-02-08 08:21:58 +0000 | [diff] [blame] | 384 | puts(" PID Uid VSZ Stat Command"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 385 | |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 386 | while ((p = procps_scan(p, 0 |
| 387 | | PSSCAN_PID |
| 388 | | PSSCAN_UIDGID |
| 389 | | PSSCAN_STATE |
Mike Frysinger | 0aa6ba5 | 2007-02-08 08:21:58 +0000 | [diff] [blame] | 390 | | PSSCAN_VSZ |
Denis Vlasenko | 98ebab8 | 2007-06-30 14:47:41 +0000 | [diff] [blame] | 391 | | PSSCAN_COMM |
Denis Vlasenko | 0696b8a | 2007-06-06 07:40:16 +0000 | [diff] [blame] | 392 | | use_selinux |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 393 | ))) { |
Bernhard Reutner-Fischer | 6d6a40c | 2005-10-04 14:31:18 +0000 | [diff] [blame] | 394 | #if ENABLE_SELINUX |
Denis Vlasenko | be905d5 | 2006-09-27 14:17:31 +0000 | [diff] [blame] | 395 | if (use_selinux) { |
Denis Vlasenko | 0696b8a | 2007-06-06 07:40:16 +0000 | [diff] [blame] | 396 | len = printf("%5u %-32s %s ", |
| 397 | p->pid, |
| 398 | p->context ? p->context : "unknown", |
| 399 | p->state); |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 400 | } else |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 401 | #endif |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 402 | { |
| 403 | const char *user = get_cached_username(p->uid); |
Mike Frysinger | 0aa6ba5 | 2007-02-08 08:21:58 +0000 | [diff] [blame] | 404 | if (p->vsz == 0) |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 405 | len = printf("%5u %-8s %s ", |
| 406 | p->pid, user, p->state); |
Denis Vlasenko | be905d5 | 2006-09-27 14:17:31 +0000 | [diff] [blame] | 407 | else |
Denis Vlasenko | fc1e995 | 2007-08-29 18:23:36 +0000 | [diff] [blame] | 408 | len = printf("%5u %-8s %6lu %s ", |
Mike Frysinger | 0aa6ba5 | 2007-02-08 08:21:58 +0000 | [diff] [blame] | 409 | p->pid, user, p->vsz, p->state); |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 410 | } |
Bernhard Reutner-Fischer | 6d6a40c | 2005-10-04 14:31:18 +0000 | [diff] [blame] | 411 | |
Denis Vlasenko | 98ebab8 | 2007-06-30 14:47:41 +0000 | [diff] [blame] | 412 | { |
Denis Vlasenko | dc7a5ea | 2007-07-01 14:53:06 +0000 | [diff] [blame] | 413 | int sz = terminal_width - len; |
Denis Vlasenko | 98ebab8 | 2007-06-30 14:47:41 +0000 | [diff] [blame] | 414 | char buf[sz + 1]; |
| 415 | read_cmdline(buf, sz, p->pid, p->comm); |
| 416 | puts(buf); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 417 | } |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 418 | } |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 419 | if (ENABLE_FEATURE_CLEAN_UP) |
| 420 | clear_username_cache(); |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 421 | return EXIT_SUCCESS; |
Eric Andersen | ef8b6c7 | 1999-10-20 08:05:35 +0000 | [diff] [blame] | 422 | } |
Denis Vlasenko | 9494919 | 2006-11-05 00:45:47 +0000 | [diff] [blame] | 423 | |
Denis Vlasenko | 4021516 | 2006-11-05 00:47:38 +0000 | [diff] [blame] | 424 | #endif /* ENABLE_DESKTOP */ |