blob: 97a5d6ba2b1baea83f5fde2c9acda2bb3c3186c7 [file] [log] [blame]
Eric Andersenef8b6c71999-10-20 08:05:35 +00001/*
2 * Mini ps implementation for busybox
3 *
4 * Copyright (C) 1998 by Erik Andersen <andersee@debian.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22#include "internal.h"
23#include <unistd.h>
24#include <dirent.h>
25#include <stdio.h>
Eric Andersend23f9ba1999-10-20 19:18:15 +000026#include <fcntl.h>
27#include <ctype.h>
28
29
30typedef struct proc_s {
31 char
32 cmd[16]; /* basename of executable file in call to exec(2) */
33 int
34 ruid, rgid, /* real only (sorry) */
35 pid, /* process id */
36 ppid; /* pid of parent process */
37 char
38 state; /* single-char code for process state (S=sleeping) */
39} proc_t;
40
41
42
43static int file2str(char *filename, char *ret, int cap)
44{
45 int fd, num_read;
46
47 if ( (fd = open(filename, O_RDONLY, 0)) == -1 ) return -1;
48 if ( (num_read = read(fd, ret, cap - 1)) <= 0 ) return -1;
49 ret[num_read] = 0;
50 close(fd);
51 return num_read;
52}
53
54
55static void parse_proc_status(char* S, proc_t* P)
56{
57 char* tmp;
58 memset(P->cmd, 0, sizeof P->cmd);
59 sscanf (S, "Name:\t%15c", P->cmd);
60 tmp = strchr(P->cmd,'\n');
61 if (tmp)
62 *tmp='\0';
63 tmp = strstr (S,"State");
64 sscanf (tmp, "State:\t%c", &P->state);
65
66 tmp = strstr (S,"Pid:");
67 if(tmp) sscanf (tmp,
68 "Pid:\t%d\n"
69 "PPid:\t%d\n",
70 &P->pid,
71 &P->ppid
72 );
73 else fprintf(stderr, "Internal error!\n");
74
75 /* For busybox, ignoring effecting, saved, etc */
76 tmp = strstr (S,"Uid:");
77 if(tmp) sscanf (tmp,
78 "Uid:\t%d", &P->ruid);
79 else fprintf(stderr, "Internal error!\n");
80
81 tmp = strstr (S,"Gid:");
82 if(tmp) sscanf (tmp,
83 "Gid:\t%d", &P->rgid);
84 else fprintf(stderr, "Internal error!\n");
85
86}
Eric Andersenef8b6c71999-10-20 08:05:35 +000087
88
89extern int ps_main(int argc, char **argv)
90{
Eric Andersend23f9ba1999-10-20 19:18:15 +000091 proc_t p;
Eric Andersenef8b6c71999-10-20 08:05:35 +000092 DIR *dir;
93 FILE *file;
94 struct dirent *entry;
Eric Andersend23f9ba1999-10-20 19:18:15 +000095 char path[32], sbuf[512];
96 char uidName[10]="";
97 char groupName[10]="";
98 int i, c;
Eric Andersenef8b6c71999-10-20 08:05:35 +000099
100 if ( argc>1 && **(argv+1) == '-' ) {
Eric Andersend23f9ba1999-10-20 19:18:15 +0000101 usage ("ps - report process status\nThis version of ps accepts no options.\n");
Eric Andersenef8b6c71999-10-20 08:05:35 +0000102 }
103
104 dir = opendir("/proc");
105 if (!dir) {
106 perror("Can't open /proc");
107 exit(FALSE);
108 }
109
Eric Andersend23f9ba1999-10-20 19:18:15 +0000110 fprintf(stdout, "%5s %-8s %-3s %5s %s\n", "PID", "Uid", "Gid", "State", "Command");
Eric Andersenef8b6c71999-10-20 08:05:35 +0000111 while ((entry = readdir(dir)) != NULL) {
Eric Andersend23f9ba1999-10-20 19:18:15 +0000112 uidName[0]='\0';
113 groupName[0]='\0';
114
115 if (! isdigit(*entry->d_name))
Eric Andersenef8b6c71999-10-20 08:05:35 +0000116 continue;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000117 sprintf(path, "/proc/%s/status", entry->d_name);
118 if ((file2str(path, sbuf, sizeof sbuf)) != -1 ) {
119 parse_proc_status(sbuf, &p);
Eric Andersenef8b6c71999-10-20 08:05:35 +0000120 }
Eric Andersenef8b6c71999-10-20 08:05:35 +0000121
Eric Andersend23f9ba1999-10-20 19:18:15 +0000122 /* Make some adjustments as needed */
123 my_getpwuid( uidName, p.ruid);
124 my_getgrgid( groupName, p.rgid);
125 if (*uidName == '\0')
126 sprintf( uidName, "%d", p.ruid);
127 if (*groupName == '\0')
128 sprintf( groupName, "%d", p.rgid);
129
130 fprintf(stdout, "%5d %-8s %-8s %c ", p.pid, uidName, groupName, p.state);
131 sprintf(path, "/proc/%s/cmdline", entry->d_name);
132 file = fopen(path, "r");
133 if (file == NULL) {
134 perror(path);
135 exit(FALSE);
136 }
137 i=0;
138 while (((c = getc(file)) != EOF) && (i < 53)) {
139 i++;
140 if (c == '\0')
141 c = ' ';
142 putc(c, stdout);
143 }
144 if (i==0)
145 fprintf(stdout, "%s", p.cmd);
146 fprintf(stdout, "\n");
Eric Andersenef8b6c71999-10-20 08:05:35 +0000147 }
148 closedir(dir);
149 exit(TRUE);
150}