1 /* vi: set sw=4 ts=4: */
3 * Mini ps implementation for busybox
6 * Copyright (C) 1999 by Lineo, inc.
7 * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 #if ! defined BB_FEATURE_USE_PROCFS
33 #error Sorry, I depend on the /proc filesystem right now.
36 typedef struct proc_s {
38 cmd[16]; /* basename of executable file in call to exec(2) */
40 ruid, rgid, /* real only (sorry) */
42 ppid; /* pid of parent process */
44 state; /* single-char code for process state (S=sleeping) */
49 static int file2str(char *filename, char *ret, int cap)
53 if ((fd = open(filename, O_RDONLY, 0)) == -1)
55 if ((num_read = read(fd, ret, cap - 1)) <= 0)
63 static void parse_proc_status(char *S, proc_t * P)
67 memset(P->cmd, 0, sizeof P->cmd);
68 sscanf(S, "Name:\t%15c", P->cmd);
69 tmp = strchr(P->cmd, '\n');
72 tmp = strstr(S, "State");
73 sscanf(tmp, "State:\t%c", &P->state);
75 tmp = strstr(S, "Pid:");
77 sscanf(tmp, "Pid:\t%d\n" "PPid:\t%d\n", &P->pid, &P->ppid);
79 fprintf(stderr, "Internal error!\n");
81 /* For busybox, ignoring effective, saved, etc */
82 tmp = strstr(S, "Uid:");
84 sscanf(tmp, "Uid:\t%d", &P->ruid);
86 fprintf(stderr, "Internal error!\n");
88 tmp = strstr(S, "Gid:");
90 sscanf(tmp, "Gid:\t%d", &P->rgid);
92 fprintf(stderr, "Internal error!\n");
97 extern int ps_main(int argc, char **argv)
102 struct dirent *entry;
103 char path[32], sbuf[512];
104 char uidName[10] = "";
105 char groupName[10] = "";
108 if (argc > 1 && **(argv + 1) == '-') {
110 ("ps\n\nReport process status\n\nThis version of ps accepts no options.\n");
113 dir = opendir("/proc");
115 perror("Can't open /proc");
119 fprintf(stdout, "%5s %-8s %-3s %5s %s\n", "PID", "Uid", "Gid",
121 while ((entry = readdir(dir)) != NULL) {
125 if (!isdigit(*entry->d_name))
127 sprintf(path, "/proc/%s/status", entry->d_name);
128 if ((file2str(path, sbuf, sizeof sbuf)) != -1) {
129 parse_proc_status(sbuf, &p);
132 /* Make some adjustments as needed */
133 my_getpwuid(uidName, p.ruid);
134 my_getgrgid(groupName, p.rgid);
135 if (*uidName == '\0')
136 sprintf(uidName, "%d", p.ruid);
137 if (*groupName == '\0')
138 sprintf(groupName, "%d", p.rgid);
140 fprintf(stdout, "%5d %-8s %-8s %c ", p.pid, uidName, groupName,
142 sprintf(path, "/proc/%s/cmdline", entry->d_name);
143 file = fopen(path, "r");
149 while (((c = getc(file)) != EOF) && (i < 53)) {
156 fprintf(stdout, "%s", p.cmd);
157 fprintf(stdout, "\n");