b0933ab514347d4c66dbb0b1bff2858e102abde6
[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 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
39 #if ! defined BB_FEATURE_USE_DEVPS_N_DEVMTAB
40
41 /* The following is the first ps implementation --
42  * the one using the /proc virtual filesystem.
43  */
44
45 #if ! defined BB_FEATURE_USE_PROCFS
46 #error Sorry, I depend on the /proc filesystem right now.
47 #endif
48
49 typedef struct proc_s {
50         char
51          cmd[16];                                       /* basename of executable file in call to exec(2) */
52         int
53          ruid, rgid,                            /* real only (sorry) */
54          pid,                                           /* process id */
55          ppid;                                          /* pid of parent process */
56         char
57          state;                                         /* single-char code for process state (S=sleeping) */
58 } proc_t;
59
60
61
62 static int file2str(char *filename, char *ret, int cap)
63 {
64         int fd, num_read;
65
66         if ((fd = open(filename, O_RDONLY, 0)) == -1)
67                 return -1;
68         if ((num_read = read(fd, ret, cap - 1)) <= 0)
69                 return -1;
70         ret[num_read] = 0;
71         close(fd);
72         return num_read;
73 }
74
75
76 static void parse_proc_status(char *S, proc_t * P)
77 {
78         char *tmp;
79
80         memset(P->cmd, 0, sizeof P->cmd);
81         sscanf(S, "Name:\t%15c", P->cmd);
82         tmp = strchr(P->cmd, '\n');
83         if (tmp)
84                 *tmp = '\0';
85         tmp = strstr(S, "State");
86         sscanf(tmp, "State:\t%c", &P->state);
87
88         tmp = strstr(S, "Pid:");
89         if (tmp)
90                 sscanf(tmp, "Pid:\t%d\n" "PPid:\t%d\n", &P->pid, &P->ppid);
91         else
92                 fprintf(stderr, "Internal error!\n");
93
94         /* For busybox, ignoring effective, saved, etc */
95         tmp = strstr(S, "Uid:");
96         if (tmp)
97                 sscanf(tmp, "Uid:\t%d", &P->ruid);
98         else
99                 fprintf(stderr, "Internal error!\n");
100
101         tmp = strstr(S, "Gid:");
102         if (tmp)
103                 sscanf(tmp, "Gid:\t%d", &P->rgid);
104         else
105                 fprintf(stderr, "Internal error!\n");
106
107 }
108
109
110 extern int ps_main(int argc, char **argv)
111 {
112         proc_t p;
113         DIR *dir;
114         FILE *file;
115         struct dirent *entry;
116         char path[32], sbuf[512];
117         char uidName[10] = "";
118         char groupName[10] = "";
119         int i, c;
120
121         if (argc > 1 && **(argv + 1) == '-')
122                 usage ("ps\n\nReport process status\n\nThis version of ps accepts no options.\n");
123
124         dir = opendir("/proc");
125         if (!dir)
126                 fatalError("Can't open /proc");
127
128         fprintf(stdout, "%5s  %-8s %-3s %5s %s\n", "PID", "Uid", "Gid",
129                         "State", "Command");
130         while ((entry = readdir(dir)) != NULL) {
131                 uidName[0] = '\0';
132                 groupName[0] = '\0';
133
134                 if (!isdigit(*entry->d_name))
135                         continue;
136                 sprintf(path, "/proc/%s/status", entry->d_name);
137                 if ((file2str(path, sbuf, sizeof sbuf)) != -1) {
138                         parse_proc_status(sbuf, &p);
139                 }
140
141                 /* Make some adjustments as needed */
142                 my_getpwuid(uidName, p.ruid);
143                 if (*uidName == '\0')
144                         sprintf(uidName, "%d", p.ruid);
145                 my_getgrgid(groupName, p.rgid);
146                 if (*groupName == '\0')
147                         sprintf(groupName, "%d", p.rgid);
148
149                 fprintf(stdout, "%5d %-8s %-8s %c ", p.pid, uidName, groupName,
150                                 p.state);
151                 sprintf(path, "/proc/%s/cmdline", entry->d_name);
152                 file = fopen(path, "r");
153                 if (file == NULL)
154                         fatalError("Can't open %s: %s\n", path, strerror(errno));
155                 i = 0;
156                 while (((c = getc(file)) != EOF) && (i < 53)) {
157                         i++;
158                         if (c == '\0')
159                                 c = ' ';
160                         putc(c, stdout);
161                 }
162                 if (i == 0)
163                         fprintf(stdout, "%s", p.cmd);
164                 fprintf(stdout, "\n");
165         }
166         closedir(dir);
167         exit(TRUE);
168 }
169
170
171 #else /* BB_FEATURE_USE_DEVPS_N_DEVMTAB */
172
173
174 /* The following is the second ps implementation --
175  * this one uses the nifty new devps kernel device.
176  */
177
178 #include <sys/ioctl.h>
179 #include <linux/devps.h>
180
181
182 extern int ps_main(int argc, char **argv)
183 {
184         char device[] = "/dev/ps";
185         int i, fd;
186         pid_t num_pids;
187         pid_t* pid_array = NULL;
188         struct pid_info info;
189         char uidName[10] = "";
190         char groupName[10] = "";
191
192         if (argc > 1 && **(argv + 1) == '-') 
193                 usage("ps-devps\n\nReport process status\n\nThis version of ps accepts no options.\n\n");
194
195         /* open device */ 
196         fd = open(device, O_RDONLY);
197         if (fd < 0) 
198                 fatalError( "open failed for `%s': %s\n", device, strerror (errno));
199
200         /* Find out how many processes there are */
201         if (ioctl (fd, DEVPS_GET_NUM_PIDS, &num_pids)<0) 
202                 fatalError( "\nDEVPS_GET_PID_LIST: %s\n", strerror (errno));
203         
204         /* Allocate some memory -- grab a few extras just in case 
205          * some new processes start up while we wait. The kernel will
206          * just ignore any extras if we give it too many, and will trunc.
207          * the list if we give it too few.  */
208         pid_array = (pid_t*) calloc( num_pids+10, sizeof(pid_t));
209         pid_array[0] = num_pids+10;
210
211         /* Now grab the pid list */
212         if (ioctl (fd, DEVPS_GET_PID_LIST, pid_array)<0) 
213                 fatalError("\nDEVPS_GET_PID_LIST: %s\n", strerror (errno));
214
215         /* Print up a ps listing */
216         fprintf(stdout, "%5s  %-8s %-3s %5s %s\n", "PID", "Uid", "Gid",
217                         "State", "Command");
218
219         for (i=1; i<pid_array[0] ; i++) {
220                 uidName[0] = '\0';
221                 groupName[0] = '\0';
222             info.pid = pid_array[i];
223
224             if (ioctl (fd, DEVPS_GET_PID_INFO, &info)<0)
225                         fatalError("\nDEVPS_GET_PID_INFO: %s\n", strerror (errno));
226             
227                 /* Make some adjustments as needed */
228                 my_getpwuid(uidName, info.euid);
229                 if (*uidName == '\0')
230                         sprintf(uidName, "%ld", info.euid);
231                 my_getgrgid(groupName, info.egid);
232                 if (*groupName == '\0')
233                         sprintf(groupName, "%ld", info.egid);
234
235                 fprintf(stdout, "%5d %-8s %-8s %c ", info.pid, uidName, groupName, info.state);
236
237                 if (strlen(info.command_line) > 1)
238                         fprintf(stdout, "%s\n", info.command_line);
239                 else
240                         fprintf(stdout, "[%s]\n", info.name);
241
242         }
243
244         /* Free memory */
245         free( pid_array);
246
247         /* close device */
248         if (close (fd) != 0) 
249                 fatalError("close failed for `%s': %s\n", device, strerror (errno));
250  
251         exit (0);
252 }
253
254 #endif /* BB_FEATURE_USE_DEVPS_N_DEVMTAB */
255