691f490c73ef24d592227b13794ec198f5b96032
[platform/upstream/busybox.git] / procps / ps.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini ps implementation(s) for busybox
4  *
5  * Copyright (C) 1999,2000 by Lineo, inc. and Erik Andersen  
6  * Copyright (C) 1999,2000,2001 by Erik Andersen <andersee@debian.org>
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by the Free
10  * Software Foundation; either version 2 of the License, or (at your option)
11  * any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16  * more details.
17  *
18  * You should have received a copy of the GNU General Public License along with
19  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
20  * Place, Suite 330, Boston, MA 02111-1307 USA
21  */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <dirent.h>
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <ctype.h>
30 #include <string.h>
31 #include <termios.h>
32 #include <sys/ioctl.h>
33 #include "busybox.h"
34 #ifdef CONFIG_SELINUX
35 #include <fs_secure.h>
36 #include <ss.h>
37 #include <flask_util.h>          /* for is_flask_enabled() */
38 #endif
39
40 static const int TERMINAL_WIDTH = 79;      /* not 80 in case terminal has linefold bug */
41
42
43
44 extern int ps_main(int argc, char **argv)
45 {
46         procps_status_t * p;
47         int i, len;
48 #ifdef CONFIG_FEATURE_AUTOWIDTH
49         struct winsize win = { 0, 0, 0, 0 };
50         int terminal_width = TERMINAL_WIDTH;
51 #else
52 #define terminal_width  TERMINAL_WIDTH
53 #endif
54
55 #ifdef CONFIG_SELINUX
56         int use_selinux = 0;
57         security_id_t sid;
58         if(is_flask_enabled() && argv[1] && !strcmp(argv[1], "-c") )
59                 use_selinux = 1;
60 #endif
61
62
63 #ifdef CONFIG_FEATURE_AUTOWIDTH
64                 ioctl(fileno(stdout), TIOCGWINSZ, &win);
65                 if (win.ws_col > 0)
66                         terminal_width = win.ws_col - 1;
67 #endif
68
69 #ifdef CONFIG_SELINUX
70         if(use_selinux)
71                 printf("  PID Context                          Stat Command\n");
72         else
73 #endif
74         printf("  PID  Uid     VmSize Stat Command\n");
75 #ifdef CONFIG_SELINUX
76         while ((p = procps_scan(1, use_selinux, &sid)) != 0) {
77 #else
78         while ((p = procps_scan(1)) != 0) {
79 #endif
80                 char *namecmd = p->cmd;
81
82 #ifdef CONFIG_SELINUX
83                 if(use_selinux)
84                 {
85                         char sbuf[128];
86                         len = sizeof(sbuf);
87                         if(security_sid_to_context(sid, (security_context_t)&sbuf, &len))
88                                 strcpy(sbuf, "unknown");
89
90                         len = printf("%5d %-32s %s ", p->pid, sbuf, p->state);
91                 }
92                 else
93 #endif
94                 if(p->rss == 0)
95                         len = printf("%5d %-8s        %s ", p->pid, p->user, p->state);
96                 else
97                         len = printf("%5d %-8s %6ld %s ", p->pid, p->user, p->rss, p->state);
98                 i = terminal_width-len;
99
100                 if(namecmd != 0 && namecmd[0] != 0) {
101                         if(i < 0)
102                 i = 0;
103                         if(strlen(namecmd) > i)
104                                 namecmd[i] = 0;
105                         printf("%s\n", namecmd);
106                 } else {
107                         namecmd = p->short_cmd;
108                         if(i < 2)
109                                 i = 2;
110                         if(strlen(namecmd) > (i-2))
111                                 namecmd[i-2] = 0;
112                         printf("[%s]\n", namecmd);
113                 }
114                 free(p->cmd);
115         }
116         return EXIT_SUCCESS;
117 }
118