a610bb2188b96a2bb600f45259928f5fe5256fb7
[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-2004 by Erik Andersen <andersen@codepoet.org>
6  *
7  * Licensed under the GPL v2, see the file LICENSE in this tarball.
8  */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <unistd.h>
13 #include <dirent.h>
14 #include <errno.h>
15 #include <fcntl.h>
16 #include <ctype.h>
17 #include <string.h>
18 #include <termios.h>
19 #include <sys/ioctl.h>
20 #include "busybox.h"
21 #if ENABLE_SELINUX
22 #include <selinux/selinux.h>  /* for is_selinux_enabled()  */
23 #endif
24
25 #define TERMINAL_WIDTH 80
26
27 extern int ps_main(int argc, char **argv)
28 {
29         procps_status_t * p;
30         int i, len, terminal_width;
31 #if ENABLE_SELINUX
32         int use_selinux = 0;
33         security_context_t sid=NULL;
34 #endif
35
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
59         /* Go one less... */
60         terminal_width--;
61 #if ENABLE_SELINUX
62         if (use_selinux)
63           printf("  PID Context                          Stat Command\n");
64         else
65 #endif
66           printf("  PID  Uid     VmSize Stat Command\n");
67
68         while ((p = procps_scan(1)) != 0)  {
69                 char *namecmd = p->cmd;
70 #if ENABLE_SELINUX
71                 if (use_selinux )
72                   {
73                         char sbuf[128];
74                         len = sizeof(sbuf);
75
76                         if (is_selinux_enabled()) {
77                           if (getpidcon(p->pid,&sid)<0)
78                             sid=NULL;
79                         }
80
81                         if (sid) {
82                           /*  I assume sid initilized with NULL  */
83                           len = strlen(sid)+1;
84                           safe_strncpy(sbuf, sid, len);
85                           freecon(sid);
86                           sid=NULL;
87                         }else {
88                           safe_strncpy(sbuf, "unknown",7);
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
99                 i = terminal_width-len;
100
101                 if(namecmd && namecmd[0]) {
102                         if(i < 0)
103                                 i = 0;
104                         if(strlen(namecmd) > i)
105                                 namecmd[i] = 0;
106                         printf("%s\n", namecmd);
107                 } else {
108                         namecmd = p->short_cmd;
109                         if(i < 2)
110                                 i = 2;
111                         if(strlen(namecmd) > (i-2))
112                                 namecmd[i-2] = 0;
113                         printf("[%s]\n", namecmd);
114                 }
115                 /* no check needed, but to make valgrind happy..  */
116                 if (ENABLE_FEATURE_CLEAN_UP && p->cmd)
117                         free(p->cmd);
118         }
119         return EXIT_SUCCESS;
120 }
121