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 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 12 | int ps_main(int argc, char **argv) |
Eric Andersen | ef8b6c7 | 1999-10-20 08:05:35 +0000 | [diff] [blame] | 13 | { |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 14 | procps_status_t * p; |
"Vladimir N. Oleynik" | be0ed3d | 2005-10-04 16:48:26 +0000 | [diff] [blame] | 15 | int i, len; |
"Vladimir N. Oleynik" | 35939d9 | 2005-10-05 10:52:47 +0000 | [diff] [blame] | 16 | |
Bernhard Reutner-Fischer | 6d6a40c | 2005-10-04 14:31:18 +0000 | [diff] [blame] | 17 | #if ENABLE_SELINUX |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 18 | int use_selinux = 0; |
Denis Vlasenko | 266bc17 | 2006-09-29 17:16:39 +0000 | [diff] [blame] | 19 | security_context_t sid = NULL; |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 20 | #endif |
"Vladimir N. Oleynik" | 35939d9 | 2005-10-05 10:52:47 +0000 | [diff] [blame] | 21 | |
"Vladimir N. Oleynik" | be0ed3d | 2005-10-04 16:48:26 +0000 | [diff] [blame] | 22 | #if ENABLE_FEATURE_PS_WIDE |
| 23 | int terminal_width; |
| 24 | int w_count = 0; |
"Vladimir N. Oleynik" | 35939d9 | 2005-10-05 10:52:47 +0000 | [diff] [blame] | 25 | |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 26 | opt_complementary = "-:ww"; |
"Vladimir N. Oleynik" | be0ed3d | 2005-10-04 16:48:26 +0000 | [diff] [blame] | 27 | #else |
"Vladimir N. Oleynik" | 35939d9 | 2005-10-05 10:52:47 +0000 | [diff] [blame] | 28 | # define terminal_width 79 |
"Vladimir N. Oleynik" | be0ed3d | 2005-10-04 16:48:26 +0000 | [diff] [blame] | 29 | #endif |
Bernhard Reutner-Fischer | 6d6a40c | 2005-10-04 14:31:18 +0000 | [diff] [blame] | 30 | |
| 31 | #if ENABLE_FEATURE_PS_WIDE || ENABLE_SELINUX |
| 32 | /* handle arguments */ |
"Vladimir N. Oleynik" | be0ed3d | 2005-10-04 16:48:26 +0000 | [diff] [blame] | 33 | #if ENABLE_FEATURE_PS_WIDE && ENABLE_SELINUX |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 34 | i = getopt32(argc, argv, "wc", &w_count); |
"Vladimir N. Oleynik" | be0ed3d | 2005-10-04 16:48:26 +0000 | [diff] [blame] | 35 | #elif ENABLE_FEATURE_PS_WIDE && !ENABLE_SELINUX |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 36 | getopt32(argc, argv, "w", &w_count); |
"Vladimir N. Oleynik" | 9c44dce | 2005-10-04 17:09:50 +0000 | [diff] [blame] | 37 | #else /* !ENABLE_FEATURE_PS_WIDE && ENABLE_SELINUX */ |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 38 | i = getopt32(argc, argv, "c"); |
Bernhard Reutner-Fischer | 6d6a40c | 2005-10-04 14:31:18 +0000 | [diff] [blame] | 39 | #endif |
"Vladimir N. Oleynik" | be0ed3d | 2005-10-04 16:48:26 +0000 | [diff] [blame] | 40 | #if ENABLE_FEATURE_PS_WIDE |
| 41 | /* if w is given once, GNU ps sets the width to 132, |
| 42 | * if w is given more than once, it is "unlimited" |
| 43 | */ |
"Vladimir N. Oleynik" | 35939d9 | 2005-10-05 10:52:47 +0000 | [diff] [blame] | 44 | if(w_count) { |
"Vladimir N. Oleynik" | be0ed3d | 2005-10-04 16:48:26 +0000 | [diff] [blame] | 45 | terminal_width = (w_count==1) ? 132 : INT_MAX; |
| 46 | } else { |
"Vladimir N. Oleynik" | 35939d9 | 2005-10-05 10:52:47 +0000 | [diff] [blame] | 47 | get_terminal_width_height(1, &terminal_width, NULL); |
"Vladimir N. Oleynik" | be0ed3d | 2005-10-04 16:48:26 +0000 | [diff] [blame] | 48 | /* Go one less... */ |
| 49 | terminal_width--; |
Bernhard Reutner-Fischer | 6d6a40c | 2005-10-04 14:31:18 +0000 | [diff] [blame] | 50 | } |
| 51 | #endif |
"Vladimir N. Oleynik" | be0ed3d | 2005-10-04 16:48:26 +0000 | [diff] [blame] | 52 | #if ENABLE_SELINUX |
"Vladimir N. Oleynik" | 9c44dce | 2005-10-04 17:09:50 +0000 | [diff] [blame] | 53 | if ((i & (1+ENABLE_FEATURE_PS_WIDE)) && is_selinux_enabled()) |
"Vladimir N. Oleynik" | be0ed3d | 2005-10-04 16:48:26 +0000 | [diff] [blame] | 54 | use_selinux = 1; |
| 55 | #endif |
| 56 | #endif /* ENABLE_FEATURE_PS_WIDE || ENABLE_SELINUX */ |
Bernhard Reutner-Fischer | 6d6a40c | 2005-10-04 14:31:18 +0000 | [diff] [blame] | 57 | |
Bernhard Reutner-Fischer | 6d6a40c | 2005-10-04 14:31:18 +0000 | [diff] [blame] | 58 | #if ENABLE_SELINUX |
Rob Landley | 60158cb | 2005-05-03 06:25:50 +0000 | [diff] [blame] | 59 | if (use_selinux) |
Denis Vlasenko | be905d5 | 2006-09-27 14:17:31 +0000 | [diff] [blame] | 60 | puts(" PID Context Stat Command"); |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 61 | else |
| 62 | #endif |
Denis Vlasenko | be905d5 | 2006-09-27 14:17:31 +0000 | [diff] [blame] | 63 | puts(" PID Uid VmSize Stat Command"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 64 | |
Rob Landley | 60158cb | 2005-05-03 06:25:50 +0000 | [diff] [blame] | 65 | while ((p = procps_scan(1)) != 0) { |
| 66 | char *namecmd = p->cmd; |
Bernhard Reutner-Fischer | 6d6a40c | 2005-10-04 14:31:18 +0000 | [diff] [blame] | 67 | #if ENABLE_SELINUX |
Denis Vlasenko | be905d5 | 2006-09-27 14:17:31 +0000 | [diff] [blame] | 68 | if (use_selinux) { |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 69 | char sbuf[128]; |
| 70 | len = sizeof(sbuf); |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 71 | |
Rob Landley | 60158cb | 2005-05-03 06:25:50 +0000 | [diff] [blame] | 72 | if (is_selinux_enabled()) { |
Denis Vlasenko | be905d5 | 2006-09-27 14:17:31 +0000 | [diff] [blame] | 73 | if (getpidcon(p->pid,&sid) < 0) |
| 74 | sid = NULL; |
Rob Landley | 60158cb | 2005-05-03 06:25:50 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | if (sid) { |
Denis Vlasenko | be905d5 | 2006-09-27 14:17:31 +0000 | [diff] [blame] | 78 | /* I assume sid initilized with NULL */ |
| 79 | len = strlen(sid)+1; |
| 80 | safe_strncpy(sbuf, sid, len); |
| 81 | freecon(sid); |
| 82 | sid = NULL; |
| 83 | } else { |
| 84 | safe_strncpy(sbuf, "unknown", 7); |
Rob Landley | 60158cb | 2005-05-03 06:25:50 +0000 | [diff] [blame] | 85 | } |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 86 | len = printf("%5d %-32s %s ", p->pid, sbuf, p->state); |
Bernhard Reutner-Fischer | 6d6a40c | 2005-10-04 14:31:18 +0000 | [diff] [blame] | 87 | } |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 88 | else |
| 89 | #endif |
Denis Vlasenko | be905d5 | 2006-09-27 14:17:31 +0000 | [diff] [blame] | 90 | if(p->rss == 0) |
| 91 | len = printf("%5d %-8s %s ", p->pid, p->user, p->state); |
| 92 | else |
| 93 | len = printf("%5d %-8s %6ld %s ", p->pid, p->user, p->rss, p->state); |
Bernhard Reutner-Fischer | 6d6a40c | 2005-10-04 14:31:18 +0000 | [diff] [blame] | 94 | |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 95 | i = terminal_width-len; |
| 96 | |
Bernhard Reutner-Fischer | 6d6a40c | 2005-10-04 14:31:18 +0000 | [diff] [blame] | 97 | if(namecmd && namecmd[0]) { |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 98 | if(i < 0) |
Bernhard Reutner-Fischer | 6d6a40c | 2005-10-04 14:31:18 +0000 | [diff] [blame] | 99 | i = 0; |
"Vladimir N. Oleynik" | 2d60458 | 2006-01-30 14:02:06 +0000 | [diff] [blame] | 100 | if(strlen(namecmd) > (size_t)i) |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 101 | namecmd[i] = 0; |
| 102 | printf("%s\n", namecmd); |
| 103 | } else { |
| 104 | namecmd = p->short_cmd; |
| 105 | if(i < 2) |
| 106 | i = 2; |
"Vladimir N. Oleynik" | 2d60458 | 2006-01-30 14:02:06 +0000 | [diff] [blame] | 107 | if(strlen(namecmd) > ((size_t)i-2)) |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 108 | namecmd[i-2] = 0; |
| 109 | printf("[%s]\n", namecmd); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 110 | } |
Bernhard Reutner-Fischer | 6d6a40c | 2005-10-04 14:31:18 +0000 | [diff] [blame] | 111 | /* no check needed, but to make valgrind happy.. */ |
| 112 | if (ENABLE_FEATURE_CLEAN_UP && p->cmd) |
| 113 | free(p->cmd); |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 114 | } |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 115 | return EXIT_SUCCESS; |
Eric Andersen | ef8b6c7 | 1999-10-20 08:05:35 +0000 | [diff] [blame] | 116 | } |