9dc45d35d45940caa68b1545d08794241923d77c
[oweals/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-2003 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
41
42
43 extern int ps_main(int argc, char **argv)
44 {
45         procps_status_t * p;
46         int i, len;
47 #ifdef CONFIG_FEATURE_AUTOWIDTH
48         struct winsize win = { 0, 0, 0, 0 };
49         int terminal_width = TERMINAL_WIDTH;
50 #else
51 #define terminal_width  TERMINAL_WIDTH
52 #endif
53
54 #ifdef CONFIG_SELINUX
55         int use_selinux = 0;
56         security_id_t sid;
57         if(is_flask_enabled() && argv[1] && !strcmp(argv[1], "-c") )
58                 use_selinux = 1;
59 #endif
60
61
62 #ifdef CONFIG_FEATURE_AUTOWIDTH
63                 ioctl(fileno(stdout), TIOCGWINSZ, &win);
64                 if (win.ws_col > 0)
65                         terminal_width = win.ws_col - 1;
66 #endif
67
68 #ifdef CONFIG_SELINUX
69         if(use_selinux)
70                 printf("  PID Context                          Stat Command\n");
71         else
72 #endif
73         printf("  PID  Uid     VmSize Stat Command\n");
74 #ifdef CONFIG_SELINUX
75         while ((p = procps_scan(1, use_selinux, &sid)) != 0) {
76 #else
77         while ((p = procps_scan(1)) != 0) {
78 #endif
79                 char *namecmd = p->cmd;
80
81 #ifdef CONFIG_SELINUX
82                 if(use_selinux)
83                 {
84                         char sbuf[128];
85                         len = sizeof(sbuf);
86                         if(security_sid_to_context(sid, (security_context_t)&sbuf, &len))
87                                 strcpy(sbuf, "unknown");
88
89                         len = printf("%5d %-32s %s ", p->pid, sbuf, p->state);
90                 }
91                 else
92 #endif
93                 if(p->rss == 0)
94                         len = printf("%5d %-8s        %s ", p->pid, p->user, p->state);
95                 else
96                         len = printf("%5d %-8s %6ld %s ", p->pid, p->user, p->rss, p->state);
97                 i = terminal_width-len;
98
99                 if(namecmd != 0 && namecmd[0] != 0) {
100                         if(i < 0)
101                 i = 0;
102                         if(strlen(namecmd) > i)
103                                 namecmd[i] = 0;
104                         printf("%s\n", namecmd);
105                 } else {
106                         namecmd = p->short_cmd;
107                         if(i < 2)
108                                 i = 2;
109                         if(strlen(namecmd) > (i-2))
110                                 namecmd[i-2] = 0;
111                         printf("[%s]\n", namecmd);
112                 }
113                 free(p->cmd);
114         }
115         return EXIT_SUCCESS;
116 }
117