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