blob: 4496faa31a4cf9f9fbf669e3c0232c1257481569 [file] [log] [blame]
Eric Andersenef8b6c71999-10-20 08:05:35 +00001/*
2 * Mini ps implementation for busybox
3 *
Eric Andersenc4996011999-10-20 22:08:37 +00004 *
5 * Copyright (C) 1999 by Lineo, inc.
6 * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
Eric Andersenef8b6c71999-10-20 08:05:35 +00007 *
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.
12 *
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.
17 *
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
21 *
22 */
23
24#include "internal.h"
25#include <unistd.h>
26#include <dirent.h>
27#include <stdio.h>
Eric Andersend23f9ba1999-10-20 19:18:15 +000028#include <fcntl.h>
29#include <ctype.h>
30
Eric Andersen0ecb54a1999-12-05 23:24:55 +000031#if ! defined BB_FEATURE_USE_PROCFS
32#error Sorry, I depend on the /proc filesystem right now.
33#endif
Eric Andersend23f9ba1999-10-20 19:18:15 +000034
35typedef struct proc_s {
36 char
37 cmd[16]; /* basename of executable file in call to exec(2) */
38 int
39 ruid, rgid, /* real only (sorry) */
40 pid, /* process id */
41 ppid; /* pid of parent process */
42 char
43 state; /* single-char code for process state (S=sleeping) */
44} proc_t;
45
46
47
48static int file2str(char *filename, char *ret, int cap)
49{
50 int fd, num_read;
51
52 if ( (fd = open(filename, O_RDONLY, 0)) == -1 ) return -1;
53 if ( (num_read = read(fd, ret, cap - 1)) <= 0 ) return -1;
54 ret[num_read] = 0;
55 close(fd);
56 return num_read;
57}
58
59
60static void parse_proc_status(char* S, proc_t* P)
61{
62 char* tmp;
63 memset(P->cmd, 0, sizeof P->cmd);
64 sscanf (S, "Name:\t%15c", P->cmd);
65 tmp = strchr(P->cmd,'\n');
66 if (tmp)
67 *tmp='\0';
68 tmp = strstr (S,"State");
69 sscanf (tmp, "State:\t%c", &P->state);
70
71 tmp = strstr (S,"Pid:");
72 if(tmp) sscanf (tmp,
73 "Pid:\t%d\n"
74 "PPid:\t%d\n",
75 &P->pid,
76 &P->ppid
77 );
78 else fprintf(stderr, "Internal error!\n");
79
Eric Andersen50d63601999-11-09 01:47:36 +000080 /* For busybox, ignoring effective, saved, etc */
Eric Andersend23f9ba1999-10-20 19:18:15 +000081 tmp = strstr (S,"Uid:");
82 if(tmp) sscanf (tmp,
83 "Uid:\t%d", &P->ruid);
84 else fprintf(stderr, "Internal error!\n");
85
86 tmp = strstr (S,"Gid:");
87 if(tmp) sscanf (tmp,
88 "Gid:\t%d", &P->rgid);
89 else fprintf(stderr, "Internal error!\n");
90
91}
Eric Andersenef8b6c71999-10-20 08:05:35 +000092
93
94extern int ps_main(int argc, char **argv)
95{
Eric Andersend23f9ba1999-10-20 19:18:15 +000096 proc_t p;
Eric Andersenef8b6c71999-10-20 08:05:35 +000097 DIR *dir;
98 FILE *file;
99 struct dirent *entry;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000100 char path[32], sbuf[512];
101 char uidName[10]="";
102 char groupName[10]="";
103 int i, c;
Eric Andersenef8b6c71999-10-20 08:05:35 +0000104
105 if ( argc>1 && **(argv+1) == '-' ) {
Eric Andersend73dc5b1999-11-10 23:13:02 +0000106 usage ("ps\n\nReport process status\n\nThis version of ps accepts no options.\n");
Eric Andersenef8b6c71999-10-20 08:05:35 +0000107 }
108
109 dir = opendir("/proc");
110 if (!dir) {
111 perror("Can't open /proc");
112 exit(FALSE);
113 }
114
Eric Andersend23f9ba1999-10-20 19:18:15 +0000115 fprintf(stdout, "%5s %-8s %-3s %5s %s\n", "PID", "Uid", "Gid", "State", "Command");
Eric Andersenef8b6c71999-10-20 08:05:35 +0000116 while ((entry = readdir(dir)) != NULL) {
Eric Andersend23f9ba1999-10-20 19:18:15 +0000117 uidName[0]='\0';
118 groupName[0]='\0';
119
120 if (! isdigit(*entry->d_name))
Eric Andersenef8b6c71999-10-20 08:05:35 +0000121 continue;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000122 sprintf(path, "/proc/%s/status", entry->d_name);
123 if ((file2str(path, sbuf, sizeof sbuf)) != -1 ) {
124 parse_proc_status(sbuf, &p);
Eric Andersenef8b6c71999-10-20 08:05:35 +0000125 }
Eric Andersenef8b6c71999-10-20 08:05:35 +0000126
Eric Andersend23f9ba1999-10-20 19:18:15 +0000127 /* Make some adjustments as needed */
128 my_getpwuid( uidName, p.ruid);
129 my_getgrgid( groupName, p.rgid);
130 if (*uidName == '\0')
131 sprintf( uidName, "%d", p.ruid);
132 if (*groupName == '\0')
133 sprintf( groupName, "%d", p.rgid);
134
135 fprintf(stdout, "%5d %-8s %-8s %c ", p.pid, uidName, groupName, p.state);
136 sprintf(path, "/proc/%s/cmdline", entry->d_name);
137 file = fopen(path, "r");
138 if (file == NULL) {
139 perror(path);
140 exit(FALSE);
141 }
142 i=0;
143 while (((c = getc(file)) != EOF) && (i < 53)) {
144 i++;
145 if (c == '\0')
146 c = ' ';
147 putc(c, stdout);
148 }
149 if (i==0)
150 fprintf(stdout, "%s", p.cmd);
151 fprintf(stdout, "\n");
Eric Andersenef8b6c71999-10-20 08:05:35 +0000152 }
153 closedir(dir);
154 exit(TRUE);
155}