Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | ef8b6c7 | 1999-10-20 08:05:35 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Mini ps implementation for busybox |
| 4 | * |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 5 | * |
| 6 | * Copyright (C) 1999 by Lineo, inc. |
| 7 | * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org> |
Eric Andersen | ef8b6c7 | 1999-10-20 08:05:35 +0000 | [diff] [blame] | 8 | * |
| 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 Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 29 | #include <fcntl.h> |
| 30 | #include <ctype.h> |
| 31 | |
Eric Andersen | 0ecb54a | 1999-12-05 23:24:55 +0000 | [diff] [blame] | 32 | #if ! defined BB_FEATURE_USE_PROCFS |
| 33 | #error Sorry, I depend on the /proc filesystem right now. |
| 34 | #endif |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 35 | |
| 36 | typedef struct proc_s { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 37 | 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 Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 45 | } proc_t; |
| 46 | |
| 47 | |
| 48 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 49 | static int file2str(char *filename, char *ret, int cap) |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 50 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 51 | int fd, num_read; |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 52 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 53 | 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 Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 63 | static void parse_proc_status(char *S, proc_t * P) |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 64 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 65 | char *tmp; |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 66 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 67 | 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 Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 74 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 75 | 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 Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 80 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 81 | /* 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 Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 93 | |
| 94 | } |
Eric Andersen | ef8b6c7 | 1999-10-20 08:05:35 +0000 | [diff] [blame] | 95 | |
| 96 | |
| 97 | extern int ps_main(int argc, char **argv) |
| 98 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 99 | 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 Andersen | ef8b6c7 | 1999-10-20 08:05:35 +0000 | [diff] [blame] | 107 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 108 | if (argc > 1 && **(argv + 1) == '-') { |
| 109 | usage |
| 110 | ("ps\n\nReport process status\n\nThis version of ps accepts no options.\n"); |
Eric Andersen | ef8b6c7 | 1999-10-20 08:05:35 +0000 | [diff] [blame] | 111 | } |
Eric Andersen | ef8b6c7 | 1999-10-20 08:05:35 +0000 | [diff] [blame] | 112 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 113 | dir = opendir("/proc"); |
| 114 | if (!dir) { |
| 115 | perror("Can't open /proc"); |
| 116 | exit(FALSE); |
| 117 | } |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 118 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 119 | 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 Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 158 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 159 | closedir(dir); |
| 160 | exit(TRUE); |
Eric Andersen | ef8b6c7 | 1999-10-20 08:05:35 +0000 | [diff] [blame] | 161 | } |