blob: b0933ab514347d4c66dbb0b1bff2858e102abde6 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersenef8b6c71999-10-20 08:05:35 +00002/*
Erik Andersen246cc6d2000-03-07 07:41:42 +00003 * 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>
Eric Andersenef8b6c71999-10-20 08:05:35 +00007 *
Eric Andersenc4996011999-10-20 22:08:37 +00008 *
Erik Andersen246cc6d2000-03-07 07:41:42 +00009 * 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+).
Eric Andersenef8b6c71999-10-20 08:05:35 +000012 *
Eric Andersenef8b6c71999-10-20 08:05:35 +000013 *
Eric Andersenef8b6c71999-10-20 08:05:35 +000014 *
Erik Andersen246cc6d2000-03-07 07:41:42 +000015 * 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
Eric Andersenef8b6c71999-10-20 08:05:35 +000028 *
29 */
30
31#include "internal.h"
Erik Andersen246cc6d2000-03-07 07:41:42 +000032#include <stdio.h>
Eric Andersenef8b6c71999-10-20 08:05:35 +000033#include <unistd.h>
34#include <dirent.h>
Erik Andersen246cc6d2000-03-07 07:41:42 +000035#include <errno.h>
Eric Andersend23f9ba1999-10-20 19:18:15 +000036#include <fcntl.h>
37#include <ctype.h>
38
Erik Andersen246cc6d2000-03-07 07:41:42 +000039#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
Eric Andersen0ecb54a1999-12-05 23:24:55 +000045#if ! defined BB_FEATURE_USE_PROCFS
46#error Sorry, I depend on the /proc filesystem right now.
47#endif
Eric Andersend23f9ba1999-10-20 19:18:15 +000048
49typedef struct proc_s {
Erik Andersene49d5ec2000-02-08 19:58:47 +000050 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) */
Eric Andersend23f9ba1999-10-20 19:18:15 +000058} proc_t;
59
60
61
Erik Andersene49d5ec2000-02-08 19:58:47 +000062static int file2str(char *filename, char *ret, int cap)
Eric Andersend23f9ba1999-10-20 19:18:15 +000063{
Erik Andersene49d5ec2000-02-08 19:58:47 +000064 int fd, num_read;
Eric Andersend23f9ba1999-10-20 19:18:15 +000065
Erik Andersene49d5ec2000-02-08 19:58:47 +000066 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;
Eric Andersend23f9ba1999-10-20 19:18:15 +000073}
74
75
Erik Andersene49d5ec2000-02-08 19:58:47 +000076static void parse_proc_status(char *S, proc_t * P)
Eric Andersend23f9ba1999-10-20 19:18:15 +000077{
Erik Andersene49d5ec2000-02-08 19:58:47 +000078 char *tmp;
Eric Andersend23f9ba1999-10-20 19:18:15 +000079
Erik Andersene49d5ec2000-02-08 19:58:47 +000080 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);
Eric Andersend23f9ba1999-10-20 19:18:15 +000087
Erik Andersene49d5ec2000-02-08 19:58:47 +000088 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");
Eric Andersend23f9ba1999-10-20 19:18:15 +000093
Erik Andersene49d5ec2000-02-08 19:58:47 +000094 /* 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");
Eric Andersend23f9ba1999-10-20 19:18:15 +0000106
107}
Eric Andersenef8b6c71999-10-20 08:05:35 +0000108
109
110extern int ps_main(int argc, char **argv)
111{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000112 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;
Eric Andersenef8b6c71999-10-20 08:05:35 +0000120
Erik Andersen246cc6d2000-03-07 07:41:42 +0000121 if (argc > 1 && **(argv + 1) == '-')
122 usage ("ps\n\nReport process status\n\nThis version of ps accepts no options.\n");
Eric Andersenef8b6c71999-10-20 08:05:35 +0000123
Erik Andersene49d5ec2000-02-08 19:58:47 +0000124 dir = opendir("/proc");
Erik Andersen246cc6d2000-03-07 07:41:42 +0000125 if (!dir)
126 fatalError("Can't open /proc");
Eric Andersend23f9ba1999-10-20 19:18:15 +0000127
Erik Andersene49d5ec2000-02-08 19:58:47 +0000128 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);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000143 if (*uidName == '\0')
144 sprintf(uidName, "%d", p.ruid);
Erik Andersen246cc6d2000-03-07 07:41:42 +0000145 my_getgrgid(groupName, p.rgid);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000146 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");
Erik Andersen246cc6d2000-03-07 07:41:42 +0000153 if (file == NULL)
154 fatalError("Can't open %s: %s\n", path, strerror(errno));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000155 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");
Eric Andersend23f9ba1999-10-20 19:18:15 +0000165 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000166 closedir(dir);
167 exit(TRUE);
Eric Andersenef8b6c71999-10-20 08:05:35 +0000168}
Erik Andersen246cc6d2000-03-07 07:41:42 +0000169
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
182extern 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