1 /* vi: set sw=4 ts=4: */
3 * Mini ps implementation(s) for busybox
5 * Copyright (C) 1999,2000 by Lineo, inc. and Erik Andersen
6 * Copyright (C) 1999,2000,2001 by Erik Andersen <andersee@debian.org>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option)
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 * You should have received a copy of the GNU General Public License along with
19 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
20 * Place, Suite 330, Boston, MA 02111-1307 USA
24 * This contains _two_ implementations of ps for Linux. One uses the
25 * traditional /proc virtual filesystem, and the other use the devps kernel
26 * driver (written by Erik Andersen to avoid using /proc thereby saving 100k+).
38 #include <sys/ioctl.h>
41 static const int TERMINAL_WIDTH = 79; /* not 80 in case terminal has linefold bug */
45 #if ! defined CONFIG_FEATURE_USE_DEVPS_PATCH
47 /* The following is the first ps implementation --
48 * the one using the /proc virtual filesystem.
51 typedef struct proc_s {
52 char cmd[16]; /* basename of executable file in call to exec(2) */
53 int ruid; /* real only (sorry) */
54 int pid; /* process id */
55 int ppid; /* pid of parent process */
56 char state; /* single-char code for process state (S=sleeping) */
57 unsigned int vmsize; /* size of process as far as the vm is concerned */
62 static int file2str(char *filename, char *ret, int cap)
66 if ((fd = open(filename, O_RDONLY, 0)) == -1)
68 if ((num_read = read(fd, ret, cap - 1)) <= 0)
76 static void parse_proc_status(char *S, proc_t * P)
80 memset(P->cmd, 0, sizeof P->cmd);
81 sscanf(S, "Name:\t%15c", P->cmd);
82 tmp = strchr(P->cmd, '\n');
85 tmp = strstr(S, "State");
86 sscanf(tmp, "State:\t%c", &P->state);
90 tmp = strstr(S, "Pid:");
92 sscanf(tmp, "Pid:\t%d\n" "PPid:\t%d\n", &P->pid, &P->ppid);
94 error_msg("Internal error!");
96 /* For busybox, ignoring effective, saved, etc. */
98 tmp = strstr(S, "Uid:");
100 sscanf(tmp, "Uid:\t%d", &P->ruid);
102 error_msg("Internal error!");
105 tmp = strstr(S, "VmSize:");
107 sscanf(tmp, "VmSize:\t%d", &P->vmsize);
110 error_msg("Internal error!");
114 extern int ps_main(int argc, char **argv)
119 struct dirent *entry;
120 char path[32], sbuf[512];
123 #ifdef CONFIG_FEATURE_AUTOWIDTH
124 struct winsize win = { 0, 0, 0, 0 };
125 int terminal_width = TERMINAL_WIDTH;
127 #define terminal_width TERMINAL_WIDTH
132 dir = opendir("/proc");
134 error_msg_and_die("Can't open /proc");
136 #ifdef CONFIG_FEATURE_AUTOWIDTH
137 ioctl(fileno(stdout), TIOCGWINSZ, &win);
139 terminal_width = win.ws_col - 1;
142 printf(" PID Uid VmSize Stat Command\n");
143 while ((entry = readdir(dir)) != NULL) {
144 if (!isdigit(*entry->d_name))
146 sprintf(path, "/proc/%s/status", entry->d_name);
147 if ((file2str(path, sbuf, sizeof sbuf)) != -1) {
148 parse_proc_status(sbuf, &p);
151 /* Make some adjustments as needed */
152 my_getpwuid(uidName, p.ruid);
154 sprintf(path, "/proc/%s/cmdline", entry->d_name);
155 file = fopen(path, "r");
160 len = printf("%5d %-8s %c ", p.pid, uidName, p.state);
162 len = printf("%5d %-8s %6d %c ", p.pid, uidName, p.vmsize, p.state);
163 while (((c = getc(file)) != EOF) && (i < (terminal_width-len))) {
171 printf("[%s]", p.cmd);
179 #else /* CONFIG_FEATURE_USE_DEVPS_PATCH */
182 /* The following is the second ps implementation --
183 * this one uses the nifty new devps kernel device.
186 #include <linux/devps.h> /* For Erik's nifty devps device driver */
189 extern int ps_main(int argc, char **argv)
191 char device[] = "/dev/ps";
194 pid_t* pid_array = NULL;
195 struct pid_info info;
197 #ifdef CONFIG_FEATURE_AUTOWIDTH
198 struct winsize win = { 0, 0, 0, 0 };
199 int terminal_width = TERMINAL_WIDTH;
201 #define terminal_width TERMINAL_WIDTH
204 if (argc > 1 && **(argv + 1) == '-')
208 fd = open(device, O_RDONLY);
210 perror_msg_and_die( "open failed for `%s'", device);
212 /* Find out how many processes there are */
213 if (ioctl (fd, DEVPS_GET_NUM_PIDS, &num_pids)<0)
214 perror_msg_and_die( "\nDEVPS_GET_PID_LIST");
216 /* Allocate some memory -- grab a few extras just in case
217 * some new processes start up while we wait. The kernel will
218 * just ignore any extras if we give it too many, and will trunc.
219 * the list if we give it too few. */
220 pid_array = (pid_t*) xcalloc( num_pids+10, sizeof(pid_t));
221 pid_array[0] = num_pids+10;
223 /* Now grab the pid list */
224 if (ioctl (fd, DEVPS_GET_PID_LIST, pid_array)<0)
225 perror_msg_and_die("\nDEVPS_GET_PID_LIST");
227 #ifdef CONFIG_FEATURE_AUTOWIDTH
228 ioctl(fileno(stdout), TIOCGWINSZ, &win);
230 terminal_width = win.ws_col - 1;
233 /* Print up a ps listing */
234 printf(" PID Uid Stat Command\n");
236 for (i=1; i<pid_array[0] ; i++) {
237 info.pid = pid_array[i];
239 if (ioctl (fd, DEVPS_GET_PID_INFO, &info)<0)
240 perror_msg_and_die("\nDEVPS_GET_PID_INFO");
242 /* Make some adjustments as needed */
243 my_getpwuid(uidName, info.euid);
246 len = printf("%5d %-8s %c ", p.pid, uidName, p.state);
248 len = printf("%5d %-8s %6d %c ", p.pid, uidName, p.vmsize, p.state);
249 if (strlen(info.command_line) > 1) {
250 for( j=0; j<(sizeof(info.command_line)-1) && j < (terminal_width-len); j++) {
251 if (*(info.command_line+j) == '\0' && *(info.command_line+j+1) != '\0') {
252 *(info.command_line+j) = ' ';
255 *(info.command_line+j) = '\0';
256 puts(info.command_line);
258 printf("[%s]\n", info.name);
267 perror_msg_and_die("close failed for `%s'", device);
272 #endif /* CONFIG_FEATURE_USE_DEVPS_PATCH */