Forgot some debugging noise.
[oweals/busybox.git] / ps.c
1 /*
2  * Mini ps implementation for busybox
3  *
4  *
5  * Copyright (C) 1999 by Lineo, inc.
6  * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
7  *
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>
28 #include <fcntl.h>
29 #include <ctype.h>
30
31 #if ! defined BB_FEATURE_USE_PROCFS
32 #error Sorry, I depend on the /proc filesystem right now.
33 #endif
34
35 typedef 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
48 static 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
60 static 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
80     /* For busybox, ignoring effective, saved, etc */
81     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 }
92
93
94 extern int ps_main(int argc, char **argv)
95 {
96     proc_t p;
97     DIR *dir;
98     FILE *file;
99     struct dirent *entry;
100     char path[32], sbuf[512];
101     char uidName[10]="";
102     char groupName[10]="";
103     int i, c;
104
105     if ( argc>1 && **(argv+1) == '-' ) {
106         usage ("ps\n\nReport process status\n\nThis version of ps accepts no options.\n");
107     }
108     
109     dir = opendir("/proc");
110     if (!dir) {
111         perror("Can't open /proc");
112         exit(FALSE);
113     }
114
115     fprintf(stdout, "%5s  %-8s %-3s %5s %s\n", "PID", "Uid", "Gid", "State", "Command");
116     while ((entry = readdir(dir)) != NULL) {
117         uidName[0]='\0';
118         groupName[0]='\0';
119                 
120         if (! isdigit(*entry->d_name))
121             continue;
122         sprintf(path, "/proc/%s/status", entry->d_name);
123         if ((file2str(path, sbuf, sizeof sbuf)) != -1 ) {
124             parse_proc_status(sbuf, &p);
125         }
126
127         /* 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");
152     }
153     closedir(dir);
154     exit(TRUE);
155 }