Lots of updates. Finished implementing BB_FEATURE_TRIVIAL_HELP
[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,2000 by Lineo, inc.  Written by Erik Andersen
6  * <andersen@lineo.com>, <andersee@debian.org>
7  *
8  *
9  * This contains _two_ implementations of ps for Linux.  One uses the
10  * traditional /proc virtual filesystem, and the other use the devps kernel
11  * driver (written by Erik Andersen to avoid using /proc thereby saving 100k+).
12  *
13  *
14  *
15  * This program is free software; you can redistribute it and/or modify it
16  * under the terms of the GNU General Public License as published by the Free
17  * Software Foundation; either version 2 of the License, or (at your option)
18  * any later version.
19  *
20  * This program is distributed in the hope that it will be useful, but WITHOUT
21  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
22  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
23  * more details.
24  *
25  * You should have received a copy of the GNU General Public License along with
26  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
27  * Place, Suite 330, Boston, MA 02111-1307 USA
28  *
29  */
30
31 #include "internal.h"
32 #include <stdio.h>
33 #include <unistd.h>
34 #include <dirent.h>
35 #include <errno.h>
36 #include <fcntl.h>
37 #include <ctype.h>
38 #include <sys/ioctl.h>
39 #define BB_DECLARE_EXTERN
40 #define bb_need_help
41 #include "messages.c"
42
43
44 #if ! defined BB_FEATURE_USE_DEVPS_PATCH
45
46 /* The following is the first ps implementation --
47  * the one using the /proc virtual filesystem.
48  */
49
50 #if ! defined BB_FEATURE_USE_PROCFS
51 #error Sorry, I depend on the /proc filesystem right now.
52 #endif
53
54 typedef struct proc_s {
55         char
56          cmd[16];                                       /* basename of executable file in call to exec(2) */
57         int
58          ruid, rgid,                            /* real only (sorry) */
59          pid,                                           /* process id */
60          ppid;                                          /* pid of parent process */
61         char
62          state;                                         /* single-char code for process state (S=sleeping) */
63 } proc_t;
64
65
66
67 static int file2str(char *filename, char *ret, int cap)
68 {
69         int fd, num_read;
70
71         if ((fd = open(filename, O_RDONLY, 0)) == -1)
72                 return -1;
73         if ((num_read = read(fd, ret, cap - 1)) <= 0)
74                 return -1;
75         ret[num_read] = 0;
76         close(fd);
77         return num_read;
78 }
79
80
81 static void parse_proc_status(char *S, proc_t * P)
82 {
83         char *tmp;
84
85         memset(P->cmd, 0, sizeof P->cmd);
86         sscanf(S, "Name:\t%15c", P->cmd);
87         tmp = strchr(P->cmd, '\n');
88         if (tmp)
89                 *tmp = '\0';
90         tmp = strstr(S, "State");
91         sscanf(tmp, "State:\t%c", &P->state);
92
93         tmp = strstr(S, "Pid:");
94         if (tmp)
95                 sscanf(tmp, "Pid:\t%d\n" "PPid:\t%d\n", &P->pid, &P->ppid);
96         else
97                 fprintf(stderr, "Internal error!\n");
98
99         /* For busybox, ignoring effective, saved, etc */
100         tmp = strstr(S, "Uid:");
101         if (tmp)
102                 sscanf(tmp, "Uid:\t%d", &P->ruid);
103         else
104                 fprintf(stderr, "Internal error!\n");
105
106         tmp = strstr(S, "Gid:");
107         if (tmp)
108                 sscanf(tmp, "Gid:\t%d", &P->rgid);
109         else
110                 fprintf(stderr, "Internal error!\n");
111
112 }
113
114
115 extern int ps_main(int argc, char **argv)
116 {
117         proc_t p;
118         DIR *dir;
119         FILE *file;
120         struct dirent *entry;
121         char path[32], sbuf[512];
122         char uidName[10] = "";
123         char groupName[10] = "";
124         int len, i, c;
125 #ifdef BB_FEATURE_AUTOWIDTH
126         struct winsize win = { 0, 0 };
127         int terminal_width = 0;
128 #else
129 #define terminal_width  79
130 #endif
131
132
133
134         if (argc > 1 && strcmp(argv[1], dash_dash_help) == 0) {
135                 usage ("ps\n"
136 #ifndef BB_FEATURE_TRIVIAL_HELP
137                                 "\nReport process status\n"
138                                 "\nThis version of ps accepts no options.\n"
139 #endif
140                                 );
141         }
142
143         dir = opendir("/proc");
144         if (!dir)
145                 fatalError("Can't open /proc");
146
147 #ifdef BB_FEATURE_AUTOWIDTH
148                 ioctl(fileno(stdout), TIOCGWINSZ, &win);
149                 if (win.ws_col > 0)
150                         terminal_width = win.ws_col - 1;
151 #endif
152
153         fprintf(stdout, "%5s  %-8s %-3s %5s %s\n", "PID", "Uid", "Gid",
154                         "State", "Command");
155         while ((entry = readdir(dir)) != NULL) {
156                 uidName[0] = '\0';
157                 groupName[0] = '\0';
158
159                 if (!isdigit(*entry->d_name))
160                         continue;
161                 sprintf(path, "/proc/%s/status", entry->d_name);
162                 if ((file2str(path, sbuf, sizeof sbuf)) != -1) {
163                         parse_proc_status(sbuf, &p);
164                 }
165
166                 /* Make some adjustments as needed */
167                 my_getpwuid(uidName, p.ruid);
168                 if (*uidName == '\0')
169                         sprintf(uidName, "%d", p.ruid);
170                 my_getgrgid(groupName, p.rgid);
171                 if (*groupName == '\0')
172                         sprintf(groupName, "%d", p.rgid);
173
174                 sprintf(path, "/proc/%s/cmdline", entry->d_name);
175                 file = fopen(path, "r");
176                 if (file == NULL)
177                         continue;
178                 i = 0;
179                 len = fprintf(stdout, "%5d %-8s %-8s %c ", p.pid, uidName, groupName,
180                                 p.state);
181                 while (((c = getc(file)) != EOF) && (i < (terminal_width-len))) {
182                         i++;
183                         if (c == '\0')
184                                 c = ' ';
185                         putc(c, stdout);
186                 }
187                 if (i == 0)
188                         fprintf(stdout, "[%s]", p.cmd);
189                 fprintf(stdout, "\n");
190         }
191         closedir(dir);
192         exit(TRUE);
193 }
194
195
196 #else /* BB_FEATURE_USE_DEVPS_PATCH */
197
198
199 /* The following is the second ps implementation --
200  * this one uses the nifty new devps kernel device.
201  */
202
203 #include <linux/devps.h>
204
205
206 extern int ps_main(int argc, char **argv)
207 {
208         char device[] = "/dev/ps";
209         int i, j, len, fd;
210         pid_t num_pids;
211         pid_t* pid_array = NULL;
212         struct pid_info info;
213         char uidName[10] = "";
214         char groupName[10] = "";
215 #ifdef BB_FEATURE_AUTOWIDTH
216         struct winsize win = { 0, 0 };
217         int terminal_width = 0;
218 #else
219 #define terminal_width  79
220 #endif
221
222         if (argc > 1 && **(argv + 1) == '-') 
223                 usage("ps-devps\n\nReport process status\n\nThis version of ps accepts no options.\n\n");
224
225         /* open device */ 
226         fd = open(device, O_RDONLY);
227         if (fd < 0) 
228                 fatalError( "open failed for `%s': %s\n", device, strerror (errno));
229
230         /* Find out how many processes there are */
231         if (ioctl (fd, DEVPS_GET_NUM_PIDS, &num_pids)<0) 
232                 fatalError( "\nDEVPS_GET_PID_LIST: %s\n", strerror (errno));
233         
234         /* Allocate some memory -- grab a few extras just in case 
235          * some new processes start up while we wait. The kernel will
236          * just ignore any extras if we give it too many, and will trunc.
237          * the list if we give it too few.  */
238         pid_array = (pid_t*) calloc( num_pids+10, sizeof(pid_t));
239         pid_array[0] = num_pids+10;
240
241         /* Now grab the pid list */
242         if (ioctl (fd, DEVPS_GET_PID_LIST, pid_array)<0) 
243                 fatalError("\nDEVPS_GET_PID_LIST: %s\n", strerror (errno));
244
245 #ifdef BB_FEATURE_AUTOWIDTH
246                 ioctl(fileno(stdout), TIOCGWINSZ, &win);
247                 if (win.ws_col > 0)
248                         terminal_width = win.ws_col - 1;
249 #endif
250
251         /* Print up a ps listing */
252         fprintf(stdout, "%5s  %-8s %-3s %5s %s\n", "PID", "Uid", "Gid",
253                         "State", "Command");
254
255         for (i=1; i<pid_array[0] ; i++) {
256                 uidName[0] = '\0';
257                 groupName[0] = '\0';
258             info.pid = pid_array[i];
259
260             if (ioctl (fd, DEVPS_GET_PID_INFO, &info)<0)
261                         fatalError("\nDEVPS_GET_PID_INFO: %s\n", strerror (errno));
262             
263                 /* Make some adjustments as needed */
264                 my_getpwuid(uidName, info.euid);
265                 if (*uidName == '\0')
266                         sprintf(uidName, "%ld", info.euid);
267                 my_getgrgid(groupName, info.egid);
268                 if (*groupName == '\0')
269                         sprintf(groupName, "%ld", info.egid);
270
271                 len = fprintf(stdout, "%5d %-8s %-8s %c ", info.pid, uidName, groupName, info.state);
272
273                 if (strlen(info.command_line) > 1) {
274                         for( j=0; j<(sizeof(info.command_line)-1) && j < (terminal_width-len); j++) {
275                                 if (*(info.command_line+j) == '\0' && *(info.command_line+j+1) != '\0') {
276                                         *(info.command_line+j) = ' ';
277                                 }
278                         }
279                         *(info.command_line+j) = '\0';
280                         fprintf(stdout, "%s\n", info.command_line);
281                 } else {
282                         fprintf(stdout, "[%s]\n", info.name);
283                 }
284         }
285
286         /* Free memory */
287         free( pid_array);
288
289         /* close device */
290         if (close (fd) != 0) 
291                 fatalError("close failed for `%s': %s\n", device, strerror (errno));
292  
293         exit (0);
294 }
295
296 #endif /* BB_FEATURE_USE_DEVPS_PATCH */
297