d29dc24e8271065014908eb5b8d60a58013938d1
[bcm963xx.git] / userapps / opensource / busybox / procps / ps.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini ps implementation(s) for busybox
4  *
5  * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6  *
7  * This program is free software; you can redistribute it and/or modify it
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  */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <dirent.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <ctype.h>
29 #include <string.h>
30 #include <termios.h>
31 #include <sys/ioctl.h>
32 #include "busybox.h"
33 #ifdef CONFIG_SELINUX
34 #include <fs_secure.h>
35 #include <ss.h>
36 #include <flask_util.h>          /* for is_flask_enabled() */
37 #endif
38
39 static const int TERMINAL_WIDTH = 79;      /* not 80 in case terminal has linefold bug */
40 //brcm
41 extern void bcmHidePassword(char *command);
42
43 extern int ps_main(int argc, char **argv)
44 {
45         procps_status_t * p;
46         int i, len;
47         int terminal_width = TERMINAL_WIDTH;
48
49 #ifdef CONFIG_SELINUX
50         int use_selinux = 0;
51         security_id_t sid;
52         if(is_flask_enabled() && argv[1] && !strcmp(argv[1], "-c") )
53                 use_selinux = 1;
54 #endif
55
56         get_terminal_width_height(0, &terminal_width, NULL);
57         /* Go one less... */
58         terminal_width--;
59
60 #ifdef CONFIG_SELINUX
61         if(use_selinux)
62                 printf("  PID Context                          Stat Command\n");
63         else
64 #endif
65         printf("  PID  Uid     VmSize Stat Command\n");
66 #ifdef CONFIG_SELINUX
67         while ((p = procps_scan(1, use_selinux, &sid)) != 0) {
68 #else
69         while ((p = procps_scan(1)) != 0) {
70 #endif
71                 char *namecmd = p->cmd;
72
73 #ifdef CONFIG_SELINUX
74                 if(use_selinux)
75                 {
76                         char sbuf[128];
77                         len = sizeof(sbuf);
78                         if(security_sid_to_context(sid, (security_context_t)&sbuf, &len))
79                                 strcpy(sbuf, "unknown");
80
81                         len = printf("%5d %-32s %s ", p->pid, sbuf, p->state);
82                 }
83                 else
84 #endif
85                 if(p->rss == 0)
86                         len = printf("%5d %-8s        %s ", p->pid, p->user, p->state);
87                 else
88                         len = printf("%5d %-8s %6ld %s ", p->pid, p->user, p->rss, p->state);
89                 i = terminal_width-len;
90
91                 // brcm begin
92                 if ( strncmp(p->short_cmd,"telnetd",7)==0 || 
93                          strncmp(p->short_cmd,"httpd",5)==0 ||
94                          strncmp(p->short_cmd,"sshd",4)==0 || 
95                          strncmp(p->short_cmd,"tr69c",5)==0 || 
96                          strncmp(p->short_cmd,"snmp",4)==0) {
97                                 printf("%s\n",p->short_cmd);
98                 } else {
99                         if(namecmd != 0 && namecmd[0] != 0) {
100                                 if(i < 0)
101                                         i = 0;
102                                 if(strlen(namecmd) > i)
103                                         namecmd[i] = 0;
104                                 //brcm begin
105                                 if ( strncmp(p->short_cmd,"pppd",4)==0) 
106                                   bcmHidePassword(namecmd);
107                                 //brcm end
108                                 printf("%s\n", namecmd);
109                         } else {
110                                 namecmd = p->short_cmd;
111                                 if(i < 2)
112                                         i = 2;
113                                 if(strlen(namecmd) > (i-2))
114                                         namecmd[i-2] = 0;
115                                 printf("[%s]\n", namecmd);
116                         }
117                 }
118                 // brcm end
119                 free(p->cmd);
120         }
121         return EXIT_SUCCESS;
122 }
123