blob: 207cdaa02726197ad31e34fac985c5dc02906a14 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersenef8b6c71999-10-20 08:05:35 +00002/*
3 * Mini ps implementation for busybox
4 *
Eric Andersenc4996011999-10-20 22:08:37 +00005 *
6 * Copyright (C) 1999 by Lineo, inc.
7 * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
Eric Andersenef8b6c71999-10-20 08:05:35 +00008 *
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>
Eric Andersend23f9ba1999-10-20 19:18:15 +000029#include <fcntl.h>
30#include <ctype.h>
31
Eric Andersen0ecb54a1999-12-05 23:24:55 +000032#if ! defined BB_FEATURE_USE_PROCFS
33#error Sorry, I depend on the /proc filesystem right now.
34#endif
Eric Andersend23f9ba1999-10-20 19:18:15 +000035
36typedef struct proc_s {
Erik Andersene49d5ec2000-02-08 19:58:47 +000037 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) */
Eric Andersend23f9ba1999-10-20 19:18:15 +000045} proc_t;
46
47
48
Erik Andersene49d5ec2000-02-08 19:58:47 +000049static int file2str(char *filename, char *ret, int cap)
Eric Andersend23f9ba1999-10-20 19:18:15 +000050{
Erik Andersene49d5ec2000-02-08 19:58:47 +000051 int fd, num_read;
Eric Andersend23f9ba1999-10-20 19:18:15 +000052
Erik Andersene49d5ec2000-02-08 19:58:47 +000053 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;
Eric Andersend23f9ba1999-10-20 19:18:15 +000060}
61
62
Erik Andersene49d5ec2000-02-08 19:58:47 +000063static void parse_proc_status(char *S, proc_t * P)
Eric Andersend23f9ba1999-10-20 19:18:15 +000064{
Erik Andersene49d5ec2000-02-08 19:58:47 +000065 char *tmp;
Eric Andersend23f9ba1999-10-20 19:18:15 +000066
Erik Andersene49d5ec2000-02-08 19:58:47 +000067 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);
Eric Andersend23f9ba1999-10-20 19:18:15 +000074
Erik Andersene49d5ec2000-02-08 19:58:47 +000075 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");
Eric Andersend23f9ba1999-10-20 19:18:15 +000080
Erik Andersene49d5ec2000-02-08 19:58:47 +000081 /* 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");
Eric Andersend23f9ba1999-10-20 19:18:15 +000093
94}
Eric Andersenef8b6c71999-10-20 08:05:35 +000095
96
97extern int ps_main(int argc, char **argv)
98{
Erik Andersene49d5ec2000-02-08 19:58:47 +000099 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;
Eric Andersenef8b6c71999-10-20 08:05:35 +0000107
Erik Andersene49d5ec2000-02-08 19:58:47 +0000108 if (argc > 1 && **(argv + 1) == '-') {
109 usage
110 ("ps\n\nReport process status\n\nThis version of ps accepts no options.\n");
Eric Andersenef8b6c71999-10-20 08:05:35 +0000111 }
Eric Andersenef8b6c71999-10-20 08:05:35 +0000112
Erik Andersene49d5ec2000-02-08 19:58:47 +0000113 dir = opendir("/proc");
114 if (!dir) {
115 perror("Can't open /proc");
116 exit(FALSE);
117 }
Eric Andersend23f9ba1999-10-20 19:18:15 +0000118
Erik Andersene49d5ec2000-02-08 19:58:47 +0000119 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");
Eric Andersend23f9ba1999-10-20 19:18:15 +0000158 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000159 closedir(dir);
160 exit(TRUE);
Eric Andersenef8b6c71999-10-20 08:05:35 +0000161}