2 * Mini ps implementation for busybox
5 * Copyright (C) 1999 by Lineo, inc.
6 * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #if ! defined BB_FEATURE_USE_PROCFS
32 #error Sorry, I depend on the /proc filesystem right now.
35 typedef struct proc_s {
37 cmd[16]; /* basename of executable file in call to exec(2) */
39 ruid, rgid, /* real only (sorry) */
41 ppid; /* pid of parent process */
43 state; /* single-char code for process state (S=sleeping) */
48 static int file2str(char *filename, char *ret, int cap)
52 if ( (fd = open(filename, O_RDONLY, 0)) == -1 ) return -1;
53 if ( (num_read = read(fd, ret, cap - 1)) <= 0 ) return -1;
60 static void parse_proc_status(char* S, proc_t* P)
63 memset(P->cmd, 0, sizeof P->cmd);
64 sscanf (S, "Name:\t%15c", P->cmd);
65 tmp = strchr(P->cmd,'\n');
68 tmp = strstr (S,"State");
69 sscanf (tmp, "State:\t%c", &P->state);
71 tmp = strstr (S,"Pid:");
78 else fprintf(stderr, "Internal error!\n");
80 /* For busybox, ignoring effective, saved, etc */
81 tmp = strstr (S,"Uid:");
83 "Uid:\t%d", &P->ruid);
84 else fprintf(stderr, "Internal error!\n");
86 tmp = strstr (S,"Gid:");
88 "Gid:\t%d", &P->rgid);
89 else fprintf(stderr, "Internal error!\n");
94 extern int ps_main(int argc, char **argv)
100 char path[32], sbuf[512];
102 char groupName[10]="";
105 if ( argc>1 && **(argv+1) == '-' ) {
106 usage ("ps\n\nReport process status\n\nThis version of ps accepts no options.\n");
109 dir = opendir("/proc");
111 perror("Can't open /proc");
115 fprintf(stdout, "%5s %-8s %-3s %5s %s\n", "PID", "Uid", "Gid", "State", "Command");
116 while ((entry = readdir(dir)) != NULL) {
120 if (! isdigit(*entry->d_name))
122 sprintf(path, "/proc/%s/status", entry->d_name);
123 if ((file2str(path, sbuf, sizeof sbuf)) != -1 ) {
124 parse_proc_status(sbuf, &p);
127 /* Make some adjustments as needed */
128 my_getpwuid( uidName, p.ruid);
129 my_getgrgid( groupName, p.rgid);
130 if (*uidName == '\0')
131 sprintf( uidName, "%d", p.ruid);
132 if (*groupName == '\0')
133 sprintf( groupName, "%d", p.rgid);
135 fprintf(stdout, "%5d %-8s %-8s %c ", p.pid, uidName, groupName, p.state);
136 sprintf(path, "/proc/%s/cmdline", entry->d_name);
137 file = fopen(path, "r");
143 while (((c = getc(file)) != EOF) && (i < 53)) {
150 fprintf(stdout, "%s", p.cmd);
151 fprintf(stdout, "\n");