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