207cdaa02726197ad31e34fac985c5dc02906a14
[oweals/busybox.git] / ps.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini ps implementation for busybox
4  *
5  *
6  * Copyright (C) 1999 by Lineo, inc.
7  * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
8  *
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.
13  *
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.
18  *
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
22  *
23  */
24
25 #include "internal.h"
26 #include <unistd.h>
27 #include <dirent.h>
28 #include <stdio.h>
29 #include <fcntl.h>
30 #include <ctype.h>
31
32 #if ! defined BB_FEATURE_USE_PROCFS
33 #error Sorry, I depend on the /proc filesystem right now.
34 #endif
35
36 typedef struct proc_s {
37         char
38          cmd[16];                                       /* basename of executable file in call to exec(2) */
39         int
40          ruid, rgid,                            /* real only (sorry) */
41          pid,                                           /* process id */
42          ppid;                                          /* pid of parent process */
43         char
44          state;                                         /* single-char code for process state (S=sleeping) */
45 } proc_t;
46
47
48
49 static int file2str(char *filename, char *ret, int cap)
50 {
51         int fd, num_read;
52
53         if ((fd = open(filename, O_RDONLY, 0)) == -1)
54                 return -1;
55         if ((num_read = read(fd, ret, cap - 1)) <= 0)
56                 return -1;
57         ret[num_read] = 0;
58         close(fd);
59         return num_read;
60 }
61
62
63 static void parse_proc_status(char *S, proc_t * P)
64 {
65         char *tmp;
66
67         memset(P->cmd, 0, sizeof P->cmd);
68         sscanf(S, "Name:\t%15c", P->cmd);
69         tmp = strchr(P->cmd, '\n');
70         if (tmp)
71                 *tmp = '\0';
72         tmp = strstr(S, "State");
73         sscanf(tmp, "State:\t%c", &P->state);
74
75         tmp = strstr(S, "Pid:");
76         if (tmp)
77                 sscanf(tmp, "Pid:\t%d\n" "PPid:\t%d\n", &P->pid, &P->ppid);
78         else
79                 fprintf(stderr, "Internal error!\n");
80
81         /* For busybox, ignoring effective, saved, etc */
82         tmp = strstr(S, "Uid:");
83         if (tmp)
84                 sscanf(tmp, "Uid:\t%d", &P->ruid);
85         else
86                 fprintf(stderr, "Internal error!\n");
87
88         tmp = strstr(S, "Gid:");
89         if (tmp)
90                 sscanf(tmp, "Gid:\t%d", &P->rgid);
91         else
92                 fprintf(stderr, "Internal error!\n");
93
94 }
95
96
97 extern int ps_main(int argc, char **argv)
98 {
99         proc_t p;
100         DIR *dir;
101         FILE *file;
102         struct dirent *entry;
103         char path[32], sbuf[512];
104         char uidName[10] = "";
105         char groupName[10] = "";
106         int i, c;
107
108         if (argc > 1 && **(argv + 1) == '-') {
109                 usage
110                         ("ps\n\nReport process status\n\nThis version of ps accepts no options.\n");
111         }
112
113         dir = opendir("/proc");
114         if (!dir) {
115                 perror("Can't open /proc");
116                 exit(FALSE);
117         }
118
119         fprintf(stdout, "%5s  %-8s %-3s %5s %s\n", "PID", "Uid", "Gid",
120                         "State", "Command");
121         while ((entry = readdir(dir)) != NULL) {
122                 uidName[0] = '\0';
123                 groupName[0] = '\0';
124
125                 if (!isdigit(*entry->d_name))
126                         continue;
127                 sprintf(path, "/proc/%s/status", entry->d_name);
128                 if ((file2str(path, sbuf, sizeof sbuf)) != -1) {
129                         parse_proc_status(sbuf, &p);
130                 }
131
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);
139
140                 fprintf(stdout, "%5d %-8s %-8s %c ", p.pid, uidName, groupName,
141                                 p.state);
142                 sprintf(path, "/proc/%s/cmdline", entry->d_name);
143                 file = fopen(path, "r");
144                 if (file == NULL) {
145                         perror(path);
146                         exit(FALSE);
147                 }
148                 i = 0;
149                 while (((c = getc(file)) != EOF) && (i < 53)) {
150                         i++;
151                         if (c == '\0')
152                                 c = ' ';
153                         putc(c, stdout);
154                 }
155                 if (i == 0)
156                         fprintf(stdout, "%s", p.cmd);
157                 fprintf(stdout, "\n");
158         }
159         closedir(dir);
160         exit(TRUE);
161 }