2000-02-09 01:28:47 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
1999-10-20 13:35:35 +05:30
|
|
|
/*
|
2000-03-07 13:11:42 +05:30
|
|
|
* Mini ps implementation(s) for busybox
|
1999-10-20 13:35:35 +05:30
|
|
|
*
|
2001-10-24 10:30:29 +05:30
|
|
|
* Copyright (C) 1999,2000 by Lineo, inc. and Erik Andersen
|
|
|
|
* Copyright (C) 1999,2000,2001 by Erik Andersen <andersee@debian.org>
|
2000-03-07 13:11:42 +05:30
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the Free
|
|
|
|
* Software Foundation; either version 2 of the License, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with
|
|
|
|
* this program; if not, write to the Free Software Foundation, Inc., 59 Temple
|
|
|
|
* Place, Suite 330, Boston, MA 02111-1307 USA
|
2001-10-24 10:30:29 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This contains _two_ implementations of ps for Linux. One uses the
|
|
|
|
* traditional /proc virtual filesystem, and the other use the devps kernel
|
|
|
|
* driver (written by Erik Andersen to avoid using /proc thereby saving 100k+).
|
1999-10-20 13:35:35 +05:30
|
|
|
*/
|
|
|
|
|
2000-03-07 13:11:42 +05:30
|
|
|
#include <stdio.h>
|
2001-01-27 13:54:39 +05:30
|
|
|
#include <stdlib.h>
|
1999-10-20 13:35:35 +05:30
|
|
|
#include <unistd.h>
|
|
|
|
#include <dirent.h>
|
2000-03-07 13:11:42 +05:30
|
|
|
#include <errno.h>
|
1999-10-21 00:48:15 +05:30
|
|
|
#include <fcntl.h>
|
|
|
|
#include <ctype.h>
|
2001-01-27 13:54:39 +05:30
|
|
|
#include <string.h>
|
2001-07-23 04:30:15 +05:30
|
|
|
#include <termios.h>
|
2000-03-08 05:02:17 +05:30
|
|
|
#include <sys/ioctl.h>
|
2001-02-20 11:44:08 +05:30
|
|
|
#include "busybox.h"
|
2000-03-08 05:02:17 +05:30
|
|
|
|
2001-01-24 04:00:04 +05:30
|
|
|
static const int TERMINAL_WIDTH = 79; /* not 80 in case terminal has linefold bug */
|
2000-06-02 08:51:42 +05:30
|
|
|
|
|
|
|
|
1999-10-21 00:48:15 +05:30
|
|
|
|
2001-10-24 10:30:29 +05:30
|
|
|
#if ! defined CONFIG_FEATURE_USE_DEVPS_PATCH
|
2000-03-07 13:11:42 +05:30
|
|
|
|
1999-10-20 13:35:35 +05:30
|
|
|
extern int ps_main(int argc, char **argv)
|
|
|
|
{
|
2002-10-22 17:51:15 +05:30
|
|
|
procps_status_t * p;
|
|
|
|
int i, len;
|
2001-10-24 10:30:29 +05:30
|
|
|
#ifdef CONFIG_FEATURE_AUTOWIDTH
|
2000-07-14 12:19:52 +05:30
|
|
|
struct winsize win = { 0, 0, 0, 0 };
|
2000-06-02 08:51:42 +05:30
|
|
|
int terminal_width = TERMINAL_WIDTH;
|
2000-03-08 05:02:17 +05:30
|
|
|
#else
|
2000-06-02 08:51:42 +05:30
|
|
|
#define terminal_width TERMINAL_WIDTH
|
2000-03-08 05:02:17 +05:30
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2001-10-24 10:30:29 +05:30
|
|
|
#ifdef CONFIG_FEATURE_AUTOWIDTH
|
2000-03-08 05:02:17 +05:30
|
|
|
ioctl(fileno(stdout), TIOCGWINSZ, &win);
|
|
|
|
if (win.ws_col > 0)
|
|
|
|
terminal_width = win.ws_col - 1;
|
|
|
|
#endif
|
|
|
|
|
2002-04-27 11:36:11 +05:30
|
|
|
printf(" PID Uid VmSize Stat Command\n");
|
2002-10-22 17:51:15 +05:30
|
|
|
while ((p = procps_scan(1)) != 0) {
|
|
|
|
char *namecmd = p->cmd;
|
2000-02-09 01:28:47 +05:30
|
|
|
|
2002-10-22 17:51:15 +05:30
|
|
|
if(p->rss == 0)
|
|
|
|
len = printf("%5d %-8s %s ", p->pid, p->user, p->state);
|
|
|
|
else
|
|
|
|
len = printf("%5d %-8s %6ld %s ", p->pid, p->user, p->rss, p->state);
|
|
|
|
i = terminal_width-len;
|
2000-02-09 01:28:47 +05:30
|
|
|
|
2002-10-22 17:51:15 +05:30
|
|
|
if(namecmd != 0 && namecmd[0] != 0) {
|
|
|
|
if(i < 0)
|
2000-02-09 01:28:47 +05:30
|
|
|
i = 0;
|
2002-10-22 17:51:15 +05:30
|
|
|
if(strlen(namecmd) > i)
|
|
|
|
namecmd[i] = 0;
|
|
|
|
printf("%s\n", namecmd);
|
|
|
|
} else {
|
|
|
|
namecmd = p->short_cmd;
|
|
|
|
if(i < 2)
|
|
|
|
i = 2;
|
|
|
|
if(strlen(namecmd) > (i-2))
|
|
|
|
namecmd[i-2] = 0;
|
|
|
|
printf("[%s]\n", namecmd);
|
2000-02-09 01:28:47 +05:30
|
|
|
}
|
2002-10-22 17:51:15 +05:30
|
|
|
free(p->cmd);
|
1999-10-21 00:48:15 +05:30
|
|
|
}
|
2000-12-01 08:25:13 +05:30
|
|
|
return EXIT_SUCCESS;
|
1999-10-20 13:35:35 +05:30
|
|
|
}
|
2000-03-07 13:11:42 +05:30
|
|
|
|
|
|
|
|
2001-10-24 10:30:29 +05:30
|
|
|
#else /* CONFIG_FEATURE_USE_DEVPS_PATCH */
|
2000-03-07 13:11:42 +05:30
|
|
|
|
|
|
|
|
|
|
|
/* The following is the second ps implementation --
|
|
|
|
* this one uses the nifty new devps kernel device.
|
|
|
|
*/
|
|
|
|
|
2000-07-11 04:27:14 +05:30
|
|
|
#include <linux/devps.h> /* For Erik's nifty devps device driver */
|
2000-03-07 13:11:42 +05:30
|
|
|
|
|
|
|
|
|
|
|
extern int ps_main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
char device[] = "/dev/ps";
|
2000-03-08 05:02:17 +05:30
|
|
|
int i, j, len, fd;
|
2000-03-07 13:11:42 +05:30
|
|
|
pid_t num_pids;
|
|
|
|
pid_t* pid_array = NULL;
|
|
|
|
struct pid_info info;
|
2000-12-13 07:22:39 +05:30
|
|
|
char uidName[9];
|
2001-10-24 10:30:29 +05:30
|
|
|
#ifdef CONFIG_FEATURE_AUTOWIDTH
|
2000-07-14 21:25:41 +05:30
|
|
|
struct winsize win = { 0, 0, 0, 0 };
|
2000-06-02 08:51:42 +05:30
|
|
|
int terminal_width = TERMINAL_WIDTH;
|
2000-03-08 05:02:17 +05:30
|
|
|
#else
|
2000-06-02 08:51:42 +05:30
|
|
|
#define terminal_width TERMINAL_WIDTH
|
2000-03-08 05:02:17 +05:30
|
|
|
#endif
|
2000-03-07 13:11:42 +05:30
|
|
|
|
|
|
|
if (argc > 1 && **(argv + 1) == '-')
|
2001-02-15 02:53:06 +05:30
|
|
|
show_usage();
|
2000-03-07 13:11:42 +05:30
|
|
|
|
|
|
|
/* open device */
|
|
|
|
fd = open(device, O_RDONLY);
|
|
|
|
if (fd < 0)
|
2000-12-18 09:27:16 +05:30
|
|
|
perror_msg_and_die( "open failed for `%s'", device);
|
2000-03-07 13:11:42 +05:30
|
|
|
|
|
|
|
/* Find out how many processes there are */
|
|
|
|
if (ioctl (fd, DEVPS_GET_NUM_PIDS, &num_pids)<0)
|
2000-12-18 09:27:16 +05:30
|
|
|
perror_msg_and_die( "\nDEVPS_GET_PID_LIST");
|
2000-03-07 13:11:42 +05:30
|
|
|
|
|
|
|
/* Allocate some memory -- grab a few extras just in case
|
|
|
|
* some new processes start up while we wait. The kernel will
|
|
|
|
* just ignore any extras if we give it too many, and will trunc.
|
|
|
|
* the list if we give it too few. */
|
2000-09-13 08:16:14 +05:30
|
|
|
pid_array = (pid_t*) xcalloc( num_pids+10, sizeof(pid_t));
|
2000-03-07 13:11:42 +05:30
|
|
|
pid_array[0] = num_pids+10;
|
|
|
|
|
|
|
|
/* Now grab the pid list */
|
|
|
|
if (ioctl (fd, DEVPS_GET_PID_LIST, pid_array)<0)
|
2000-12-18 09:27:16 +05:30
|
|
|
perror_msg_and_die("\nDEVPS_GET_PID_LIST");
|
2000-03-07 13:11:42 +05:30
|
|
|
|
2001-10-24 10:30:29 +05:30
|
|
|
#ifdef CONFIG_FEATURE_AUTOWIDTH
|
2000-03-08 05:02:17 +05:30
|
|
|
ioctl(fileno(stdout), TIOCGWINSZ, &win);
|
|
|
|
if (win.ws_col > 0)
|
|
|
|
terminal_width = win.ws_col - 1;
|
|
|
|
#endif
|
|
|
|
|
2000-03-07 13:11:42 +05:30
|
|
|
/* Print up a ps listing */
|
2001-06-27 04:41:44 +05:30
|
|
|
printf(" PID Uid Stat Command\n");
|
2000-03-07 13:11:42 +05:30
|
|
|
|
|
|
|
for (i=1; i<pid_array[0] ; i++) {
|
|
|
|
info.pid = pid_array[i];
|
|
|
|
|
|
|
|
if (ioctl (fd, DEVPS_GET_PID_INFO, &info)<0)
|
2000-12-18 09:27:16 +05:30
|
|
|
perror_msg_and_die("\nDEVPS_GET_PID_INFO");
|
2000-03-07 13:11:42 +05:30
|
|
|
|
|
|
|
/* Make some adjustments as needed */
|
|
|
|
my_getpwuid(uidName, info.euid);
|
|
|
|
|
2002-04-27 11:36:11 +05:30
|
|
|
if(p.vmsize == 0)
|
|
|
|
len = printf("%5d %-8s %c ", p.pid, uidName, p.state);
|
|
|
|
else
|
|
|
|
len = printf("%5d %-8s %6d %c ", p.pid, uidName, p.vmsize, p.state);
|
2000-03-08 05:02:17 +05:30
|
|
|
if (strlen(info.command_line) > 1) {
|
|
|
|
for( j=0; j<(sizeof(info.command_line)-1) && j < (terminal_width-len); j++) {
|
|
|
|
if (*(info.command_line+j) == '\0' && *(info.command_line+j+1) != '\0') {
|
|
|
|
*(info.command_line+j) = ' ';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*(info.command_line+j) = '\0';
|
2001-01-18 08:27:08 +05:30
|
|
|
puts(info.command_line);
|
2000-03-08 05:02:17 +05:30
|
|
|
} else {
|
2001-01-18 08:27:08 +05:30
|
|
|
printf("[%s]\n", info.name);
|
2000-03-08 05:02:17 +05:30
|
|
|
}
|
2000-03-07 13:11:42 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* Free memory */
|
|
|
|
free( pid_array);
|
|
|
|
|
|
|
|
/* close device */
|
|
|
|
if (close (fd) != 0)
|
2000-12-18 09:27:16 +05:30
|
|
|
perror_msg_and_die("close failed for `%s'", device);
|
2000-03-07 13:11:42 +05:30
|
|
|
|
|
|
|
exit (0);
|
|
|
|
}
|
|
|
|
|
2001-10-24 10:30:29 +05:30
|
|
|
#endif /* CONFIG_FEATURE_USE_DEVPS_PATCH */
|
2000-03-07 13:11:42 +05:30
|
|
|
|