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