Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Utility routines. |
| 4 | * |
| 5 | * Copyright 1998 by Albert Cahalan; all rights reserved. |
| 6 | * Copyright (C) 2002 by Vladimir Oleynik <dzo@simtreas.ru> |
| 7 | * GNU Library General Public License Version 2, or any later version |
| 8 | * |
| 9 | */ |
| 10 | |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 11 | #include <dirent.h> |
| 12 | #include <string.h> |
| 13 | #include <stdlib.h> |
| 14 | #include <unistd.h> |
| 15 | #include <asm/page.h> |
| 16 | |
Glenn L McGrath | 7b1eca2 | 2002-11-24 01:32:56 +0000 | [diff] [blame] | 17 | #include "libbb.h" |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 18 | |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 19 | extern procps_status_t * procps_scan(int save_user_arg0 |
| 20 | #ifdef CONFIG_SELINUX |
| 21 | , int use_selinux , security_id_t *sid |
| 22 | #endif |
| 23 | ) |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 24 | { |
| 25 | static DIR *dir; |
| 26 | struct dirent *entry; |
| 27 | static procps_status_t ret_status; |
| 28 | char *name; |
| 29 | int n; |
| 30 | char status[32]; |
| 31 | char buf[1024]; |
| 32 | FILE *fp; |
| 33 | procps_status_t curstatus; |
| 34 | int pid; |
| 35 | long tasknice; |
| 36 | struct stat sb; |
| 37 | |
| 38 | if (!dir) { |
| 39 | dir = opendir("/proc"); |
| 40 | if(!dir) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 41 | bb_error_msg_and_die("Can't open /proc"); |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 42 | } |
| 43 | for(;;) { |
| 44 | if((entry = readdir(dir)) == NULL) { |
| 45 | closedir(dir); |
| 46 | dir = 0; |
| 47 | return 0; |
| 48 | } |
| 49 | name = entry->d_name; |
| 50 | if (!(*name >= '0' && *name <= '9')) |
| 51 | continue; |
| 52 | |
| 53 | memset(&curstatus, 0, sizeof(procps_status_t)); |
| 54 | pid = atoi(name); |
| 55 | curstatus.pid = pid; |
| 56 | |
| 57 | sprintf(status, "/proc/%d/stat", pid); |
| 58 | if((fp = fopen(status, "r")) == NULL) |
| 59 | continue; |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 60 | #ifdef CONFIG_SELINUX |
| 61 | if(use_selinux) |
| 62 | { |
| 63 | if(fstat_secure(fileno(fp), &sb, sid)) |
| 64 | continue; |
| 65 | } |
| 66 | else |
| 67 | #endif |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 68 | if(fstat(fileno(fp), &sb)) |
| 69 | continue; |
| 70 | my_getpwuid(curstatus.user, sb.st_uid); |
| 71 | name = fgets(buf, sizeof(buf), fp); |
| 72 | fclose(fp); |
| 73 | if(name == NULL) |
| 74 | continue; |
| 75 | name = strrchr(buf, ')'); /* split into "PID (cmd" and "<rest>" */ |
| 76 | if(name == 0 || name[1] != ' ') |
| 77 | continue; |
| 78 | *name = 0; |
| 79 | sscanf(buf, "%*s (%15c", curstatus.short_cmd); |
| 80 | n = sscanf(name+2, |
| 81 | "%c %d " |
| 82 | "%*s %*s %*s %*s " /* pgrp, session, tty, tpgid */ |
| 83 | "%*s %*s %*s %*s %*s " /* flags, min_flt, cmin_flt, maj_flt, cmaj_flt */ |
| 84 | #ifdef FEATURE_CPU_USAGE_PERCENTAGE |
| 85 | "%lu %lu " |
| 86 | #else |
| 87 | "%*s %*s " |
| 88 | #endif |
| 89 | "%*s %*s %*s " /* cutime, cstime, priority */ |
| 90 | "%ld " |
| 91 | "%*s %*s %*s " /* timeout, it_real_value, start_time */ |
| 92 | "%*s " /* vsize */ |
| 93 | "%ld", |
| 94 | curstatus.state, &curstatus.ppid, |
| 95 | #ifdef FEATURE_CPU_USAGE_PERCENTAGE |
| 96 | &curstatus.utime, &curstatus.stime, |
| 97 | #endif |
| 98 | &tasknice, |
| 99 | &curstatus.rss); |
| 100 | #ifdef FEATURE_CPU_USAGE_PERCENTAGE |
| 101 | if(n != 6) |
| 102 | #else |
| 103 | if(n != 4) |
| 104 | #endif |
| 105 | continue; |
| 106 | |
| 107 | if (curstatus.rss == 0 && curstatus.state[0] != 'Z') |
| 108 | curstatus.state[1] = 'W'; |
| 109 | else |
| 110 | curstatus.state[1] = ' '; |
| 111 | if (tasknice < 0) |
| 112 | curstatus.state[2] = '<'; |
| 113 | else if (tasknice > 0) |
| 114 | curstatus.state[2] = 'N'; |
| 115 | else |
| 116 | curstatus.state[2] = ' '; |
| 117 | |
| 118 | curstatus.rss <<= (PAGE_SHIFT - 10); /* 2**10 = 1kb */ |
| 119 | |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 120 | if(save_user_arg0) { |
Eric Andersen | fab3e12 | 2003-05-26 18:07:30 +0000 | [diff] [blame] | 121 | sprintf(status, "/proc/%d/cmdline", pid); |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 122 | if((fp = fopen(status, "r")) == NULL) |
| 123 | continue; |
Glenn L McGrath | 393ad1a | 2002-11-25 22:31:37 +0000 | [diff] [blame] | 124 | if((n=fread(buf, 1, sizeof(buf)-1, fp)) > 0) { |
| 125 | if(buf[n-1]=='\n') |
| 126 | buf[--n] = 0; |
| 127 | name = buf; |
| 128 | while(n) { |
Glenn L McGrath | 09adaca | 2002-12-02 21:18:10 +0000 | [diff] [blame] | 129 | if(((unsigned char)*name) < ' ') |
Glenn L McGrath | 393ad1a | 2002-11-25 22:31:37 +0000 | [diff] [blame] | 130 | *name = ' '; |
| 131 | name++; |
| 132 | n--; |
| 133 | } |
| 134 | *name = 0; |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 135 | if(buf[0]) |
| 136 | curstatus.cmd = strdup(buf); |
| 137 | /* if NULL it work true also */ |
| 138 | } |
| 139 | fclose(fp); |
| 140 | } |
| 141 | return memcpy(&ret_status, &curstatus, sizeof(procps_status_t)); |
| 142 | } |
| 143 | } |
| 144 | |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 145 | /* END CODE */ |
| 146 | /* |
| 147 | Local Variables: |
| 148 | c-file-style: "linux" |
| 149 | c-basic-offset: 4 |
| 150 | tab-width: 4 |
| 151 | End: |
| 152 | */ |