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
32 typedef struct proc_s {
34 cmd[16]; /* basename of executable file in call to exec(2) */
36 ruid, rgid, /* real only (sorry) */
38 ppid; /* pid of parent process */
40 state; /* single-char code for process state (S=sleeping) */
45 static int file2str(char *filename, char *ret, int cap)
49 if ( (fd = open(filename, O_RDONLY, 0)) == -1 ) return -1;
50 if ( (num_read = read(fd, ret, cap - 1)) <= 0 ) return -1;
57 static void parse_proc_status(char* S, proc_t* P)
60 memset(P->cmd, 0, sizeof P->cmd);
61 sscanf (S, "Name:\t%15c", P->cmd);
62 tmp = strchr(P->cmd,'\n');
65 tmp = strstr (S,"State");
66 sscanf (tmp, "State:\t%c", &P->state);
68 tmp = strstr (S,"Pid:");
75 else fprintf(stderr, "Internal error!\n");
77 /* For busybox, ignoring effecting, saved, etc */
78 tmp = strstr (S,"Uid:");
80 "Uid:\t%d", &P->ruid);
81 else fprintf(stderr, "Internal error!\n");
83 tmp = strstr (S,"Gid:");
85 "Gid:\t%d", &P->rgid);
86 else fprintf(stderr, "Internal error!\n");
91 extern int ps_main(int argc, char **argv)
97 char path[32], sbuf[512];
99 char groupName[10]="";
102 if ( argc>1 && **(argv+1) == '-' ) {
103 usage ("ps - report process status\nThis version of ps accepts no options.\n");
106 dir = opendir("/proc");
108 perror("Can't open /proc");
112 fprintf(stdout, "%5s %-8s %-3s %5s %s\n", "PID", "Uid", "Gid", "State", "Command");
113 while ((entry = readdir(dir)) != NULL) {
117 if (! isdigit(*entry->d_name))
119 sprintf(path, "/proc/%s/status", entry->d_name);
120 if ((file2str(path, sbuf, sizeof sbuf)) != -1 ) {
121 parse_proc_status(sbuf, &p);
124 /* Make some adjustments as needed */
125 my_getpwuid( uidName, p.ruid);
126 my_getgrgid( groupName, p.rgid);
127 if (*uidName == '\0')
128 sprintf( uidName, "%d", p.ruid);
129 if (*groupName == '\0')
130 sprintf( groupName, "%d", p.rgid);
132 fprintf(stdout, "%5d %-8s %-8s %c ", p.pid, uidName, groupName, p.state);
133 sprintf(path, "/proc/%s/cmdline", entry->d_name);
134 file = fopen(path, "r");
140 while (((c = getc(file)) != EOF) && (i < 53)) {
147 fprintf(stdout, "%s", p.cmd);
148 fprintf(stdout, "\n");