aboutsummaryrefslogtreecommitdiff
path: root/procps/ps.c
diff options
context:
space:
mode:
Diffstat (limited to 'procps/ps.c')
-rw-r--r--procps/ps.c68
1 files changed, 37 insertions, 31 deletions
diff --git a/procps/ps.c b/procps/ps.c
index 18a6db36f..a610bb218 100644
--- a/procps/ps.c
+++ b/procps/ps.c
@@ -4,19 +4,7 @@
4 * 4 *
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> 5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6 * 6 *
7 * This program is free software; you can redistribute it and/or modify it 7 * Licensed under the GPL v2, see the file LICENSE in this tarball.
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 * Place, Suite 330, Boston, MA 02111-1307 USA
20 */ 8 */
21 9
22#include <stdio.h> 10#include <stdio.h>
@@ -30,32 +18,47 @@
30#include <termios.h> 18#include <termios.h>
31#include <sys/ioctl.h> 19#include <sys/ioctl.h>
32#include "busybox.h" 20#include "busybox.h"
33#ifdef CONFIG_SELINUX 21#if ENABLE_SELINUX
34#include <selinux/selinux.h> /* for is_selinux_enabled() */ 22#include <selinux/selinux.h> /* for is_selinux_enabled() */
35#endif 23#endif
36 24
37static const int TERMINAL_WIDTH = 79; /* not 80 in case terminal has linefold bug */ 25#define TERMINAL_WIDTH 80
38
39
40 26
41extern int ps_main(int argc, char **argv) 27extern int ps_main(int argc, char **argv)
42{ 28{
43 procps_status_t * p; 29 procps_status_t * p;
44 int i, len; 30 int i, len, terminal_width;
45 int terminal_width = TERMINAL_WIDTH; 31#if ENABLE_SELINUX
46
47#ifdef CONFIG_SELINUX
48 int use_selinux = 0; 32 int use_selinux = 0;
49 security_context_t sid=NULL; 33 security_context_t sid=NULL;
50 if(is_selinux_enabled() && argv[1] && !strcmp(argv[1], "-c") )
51 use_selinux = 1;
52#endif 34#endif
53 35
54 get_terminal_width_height(0, &terminal_width, NULL); 36 get_terminal_width_height(0, &terminal_width, NULL);
37
38#if ENABLE_FEATURE_PS_WIDE || ENABLE_SELINUX
39 /* handle arguments */
40 /* bb_getopt_ulflags(argc, argv,) would force a leading dash */
41 for (len = 1; len < argc; len++) {
42 char *c = argv[len];
43 while (*c) {
44 if (ENABLE_FEATURE_PS_WIDE && *c == 'w')
45 /* if w is given once, GNU ps sets the width to 132,
46 * if w is given more than once, it is "unlimited"
47 */
48 terminal_width =
49 (terminal_width==TERMINAL_WIDTH) ? 132 : INT_MAX;
50#if ENABLE_SELINUX
51 if (*c == 'c' && is_selinux_enabled())
52 use_selinux = 1;
53#endif
54 c++;
55 }
56 }
57#endif
58
55 /* Go one less... */ 59 /* Go one less... */
56 terminal_width--; 60 terminal_width--;
57 61#if ENABLE_SELINUX
58#ifdef CONFIG_SELINUX
59 if (use_selinux) 62 if (use_selinux)
60 printf(" PID Context Stat Command\n"); 63 printf(" PID Context Stat Command\n");
61 else 64 else
@@ -64,8 +67,8 @@ extern int ps_main(int argc, char **argv)
64 67
65 while ((p = procps_scan(1)) != 0) { 68 while ((p = procps_scan(1)) != 0) {
66 char *namecmd = p->cmd; 69 char *namecmd = p->cmd;
67#ifdef CONFIG_SELINUX 70#if ENABLE_SELINUX
68 if ( use_selinux ) 71 if (use_selinux )
69 { 72 {
70 char sbuf[128]; 73 char sbuf[128];
71 len = sizeof(sbuf); 74 len = sizeof(sbuf);
@@ -85,18 +88,19 @@ extern int ps_main(int argc, char **argv)
85 safe_strncpy(sbuf, "unknown",7); 88 safe_strncpy(sbuf, "unknown",7);
86 } 89 }
87 len = printf("%5d %-32s %s ", p->pid, sbuf, p->state); 90 len = printf("%5d %-32s %s ", p->pid, sbuf, p->state);
88 } 91 }
89 else 92 else
90#endif 93#endif
91 if(p->rss == 0) 94 if(p->rss == 0)
92 len = printf("%5d %-8s %s ", p->pid, p->user, p->state); 95 len = printf("%5d %-8s %s ", p->pid, p->user, p->state);
93 else 96 else
94 len = printf("%5d %-8s %6ld %s ", p->pid, p->user, p->rss, p->state); 97 len = printf("%5d %-8s %6ld %s ", p->pid, p->user, p->rss, p->state);
98
95 i = terminal_width-len; 99 i = terminal_width-len;
96 100
97 if(namecmd != 0 && namecmd[0] != 0) { 101 if(namecmd && namecmd[0]) {
98 if(i < 0) 102 if(i < 0)
99 i = 0; 103 i = 0;
100 if(strlen(namecmd) > i) 104 if(strlen(namecmd) > i)
101 namecmd[i] = 0; 105 namecmd[i] = 0;
102 printf("%s\n", namecmd); 106 printf("%s\n", namecmd);
@@ -108,7 +112,9 @@ extern int ps_main(int argc, char **argv)
108 namecmd[i-2] = 0; 112 namecmd[i-2] = 0;
109 printf("[%s]\n", namecmd); 113 printf("[%s]\n", namecmd);
110 } 114 }
111 free(p->cmd); 115 /* no check needed, but to make valgrind happy.. */
116 if (ENABLE_FEATURE_CLEAN_UP && p->cmd)
117 free(p->cmd);
112 } 118 }
113 return EXIT_SUCCESS; 119 return EXIT_SUCCESS;
114} 120}