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 extern int ps_main(int argc, char **argv)
51 #ifdef CONFIG_FEATURE_AUTOWIDTH
52 struct winsize win = { 0, 0, 0, 0 };
53 int terminal_width = TERMINAL_WIDTH;
55 #define terminal_width TERMINAL_WIDTH
59 #ifdef CONFIG_FEATURE_AUTOWIDTH
60 ioctl(fileno(stdout), TIOCGWINSZ, &win);
62 terminal_width = win.ws_col - 1;
65 printf(" PID Uid VmSize Stat Command\n");
66 while ((p = procps_scan(1)) != 0) {
67 char *namecmd = p->cmd;
70 len = printf("%5d %-8s %s ", p->pid, p->user, p->state);
72 len = printf("%5d %-8s %6ld %s ", p->pid, p->user, p->rss, p->state);
73 i = terminal_width-len;
75 if(namecmd != 0 && namecmd[0] != 0) {
78 if(strlen(namecmd) > i)
80 printf("%s\n", namecmd);
82 namecmd = p->short_cmd;
85 if(strlen(namecmd) > (i-2))
87 printf("[%s]\n", namecmd);
95 #else /* CONFIG_FEATURE_USE_DEVPS_PATCH */
98 /* The following is the second ps implementation --
99 * this one uses the nifty new devps kernel device.
102 #include <linux/devps.h> /* For Erik's nifty devps device driver */
105 extern int ps_main(int argc, char **argv)
107 char device[] = "/dev/ps";
110 pid_t* pid_array = NULL;
111 struct pid_info info;
113 #ifdef CONFIG_FEATURE_AUTOWIDTH
114 struct winsize win = { 0, 0, 0, 0 };
115 int terminal_width = TERMINAL_WIDTH;
117 #define terminal_width TERMINAL_WIDTH
120 if (argc > 1 && **(argv + 1) == '-')
124 fd = open(device, O_RDONLY);
126 perror_msg_and_die( "open failed for `%s'", device);
128 /* Find out how many processes there are */
129 if (ioctl (fd, DEVPS_GET_NUM_PIDS, &num_pids)<0)
130 perror_msg_and_die( "\nDEVPS_GET_PID_LIST");
132 /* Allocate some memory -- grab a few extras just in case
133 * some new processes start up while we wait. The kernel will
134 * just ignore any extras if we give it too many, and will trunc.
135 * the list if we give it too few. */
136 pid_array = (pid_t*) xcalloc( num_pids+10, sizeof(pid_t));
137 pid_array[0] = num_pids+10;
139 /* Now grab the pid list */
140 if (ioctl (fd, DEVPS_GET_PID_LIST, pid_array)<0)
141 perror_msg_and_die("\nDEVPS_GET_PID_LIST");
143 #ifdef CONFIG_FEATURE_AUTOWIDTH
144 ioctl(fileno(stdout), TIOCGWINSZ, &win);
146 terminal_width = win.ws_col - 1;
149 /* Print up a ps listing */
150 printf(" PID Uid Stat Command\n");
152 for (i=1; i<pid_array[0] ; i++) {
153 info.pid = pid_array[i];
155 if (ioctl (fd, DEVPS_GET_PID_INFO, &info)<0)
156 perror_msg_and_die("\nDEVPS_GET_PID_INFO");
158 /* Make some adjustments as needed */
159 my_getpwuid(uidName, info.euid);
162 len = printf("%5d %-8s %c ", p.pid, uidName, p.state);
164 len = printf("%5d %-8s %6d %c ", p.pid, uidName, p.vmsize, p.state);
165 if (strlen(info.command_line) > 1) {
166 for( j=0; j<(sizeof(info.command_line)-1) && j < (terminal_width-len); j++) {
167 if (*(info.command_line+j) == '\0' && *(info.command_line+j+1) != '\0') {
168 *(info.command_line+j) = ' ';
171 *(info.command_line+j) = '\0';
172 puts(info.command_line);
174 printf("[%s]\n", info.name);
183 perror_msg_and_die("close failed for `%s'", device);
188 #endif /* CONFIG_FEATURE_USE_DEVPS_PATCH */