Eric Andersen | ef8b6c7 | 1999-10-20 08:05:35 +0000 | [diff] [blame] | 1 | /* |
| 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 Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 26 | #include <fcntl.h> |
| 27 | #include <ctype.h> |
| 28 | |
| 29 | |
| 30 | typedef 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 | |
| 43 | static 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 | |
| 55 | static 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 Andersen | ef8b6c7 | 1999-10-20 08:05:35 +0000 | [diff] [blame] | 87 | |
| 88 | |
| 89 | extern int ps_main(int argc, char **argv) |
| 90 | { |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 91 | proc_t p; |
Eric Andersen | ef8b6c7 | 1999-10-20 08:05:35 +0000 | [diff] [blame] | 92 | DIR *dir; |
| 93 | FILE *file; |
| 94 | struct dirent *entry; |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 95 | char path[32], sbuf[512]; |
| 96 | char uidName[10]=""; |
| 97 | char groupName[10]=""; |
| 98 | int i, c; |
Eric Andersen | ef8b6c7 | 1999-10-20 08:05:35 +0000 | [diff] [blame] | 99 | |
| 100 | if ( argc>1 && **(argv+1) == '-' ) { |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 101 | usage ("ps - report process status\nThis version of ps accepts no options.\n"); |
Eric Andersen | ef8b6c7 | 1999-10-20 08:05:35 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | dir = opendir("/proc"); |
| 105 | if (!dir) { |
| 106 | perror("Can't open /proc"); |
| 107 | exit(FALSE); |
| 108 | } |
| 109 | |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 110 | fprintf(stdout, "%5s %-8s %-3s %5s %s\n", "PID", "Uid", "Gid", "State", "Command"); |
Eric Andersen | ef8b6c7 | 1999-10-20 08:05:35 +0000 | [diff] [blame] | 111 | while ((entry = readdir(dir)) != NULL) { |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 112 | uidName[0]='\0'; |
| 113 | groupName[0]='\0'; |
| 114 | |
| 115 | if (! isdigit(*entry->d_name)) |
Eric Andersen | ef8b6c7 | 1999-10-20 08:05:35 +0000 | [diff] [blame] | 116 | continue; |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 117 | sprintf(path, "/proc/%s/status", entry->d_name); |
| 118 | if ((file2str(path, sbuf, sizeof sbuf)) != -1 ) { |
| 119 | parse_proc_status(sbuf, &p); |
Eric Andersen | ef8b6c7 | 1999-10-20 08:05:35 +0000 | [diff] [blame] | 120 | } |
Eric Andersen | ef8b6c7 | 1999-10-20 08:05:35 +0000 | [diff] [blame] | 121 | |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 122 | /* 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 Andersen | ef8b6c7 | 1999-10-20 08:05:35 +0000 | [diff] [blame] | 147 | } |
| 148 | closedir(dir); |
| 149 | exit(TRUE); |
| 150 | } |