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 | |
Rob Landley | 60158cb | 2005-05-03 06:25:50 +0000 | [diff] [blame] | 19 | extern procps_status_t * procps_scan(int save_user_arg0) |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 20 | { |
| 21 | static DIR *dir; |
| 22 | struct dirent *entry; |
| 23 | static procps_status_t ret_status; |
| 24 | char *name; |
| 25 | int n; |
| 26 | char status[32]; |
| 27 | char buf[1024]; |
| 28 | FILE *fp; |
| 29 | procps_status_t curstatus; |
| 30 | int pid; |
| 31 | long tasknice; |
| 32 | struct stat sb; |
| 33 | |
| 34 | if (!dir) { |
| 35 | dir = opendir("/proc"); |
| 36 | if(!dir) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 37 | bb_error_msg_and_die("Can't open /proc"); |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 38 | } |
| 39 | for(;;) { |
| 40 | if((entry = readdir(dir)) == NULL) { |
| 41 | closedir(dir); |
| 42 | dir = 0; |
| 43 | return 0; |
| 44 | } |
| 45 | name = entry->d_name; |
| 46 | if (!(*name >= '0' && *name <= '9')) |
| 47 | continue; |
| 48 | |
| 49 | memset(&curstatus, 0, sizeof(procps_status_t)); |
| 50 | pid = atoi(name); |
| 51 | curstatus.pid = pid; |
| 52 | |
Eric Andersen | 242ab83 | 2004-01-27 20:17:39 +0000 | [diff] [blame] | 53 | sprintf(status, "/proc/%d", pid); |
| 54 | if(stat(status, &sb)) |
| 55 | continue; |
Bernhard Reutner-Fischer | d5bd137 | 2005-09-20 21:06:17 +0000 | [diff] [blame] | 56 | bb_getpwuid(curstatus.user, sb.st_uid, sizeof(curstatus.user)); |
Eric Andersen | 242ab83 | 2004-01-27 20:17:39 +0000 | [diff] [blame] | 57 | |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 58 | sprintf(status, "/proc/%d/stat", pid); |
Rob Landley | 60158cb | 2005-05-03 06:25:50 +0000 | [diff] [blame] | 59 | |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 60 | if((fp = fopen(status, "r")) == NULL) |
| 61 | continue; |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 62 | name = fgets(buf, sizeof(buf), fp); |
| 63 | fclose(fp); |
| 64 | if(name == NULL) |
| 65 | continue; |
| 66 | name = strrchr(buf, ')'); /* split into "PID (cmd" and "<rest>" */ |
| 67 | if(name == 0 || name[1] != ' ') |
| 68 | continue; |
| 69 | *name = 0; |
| 70 | sscanf(buf, "%*s (%15c", curstatus.short_cmd); |
| 71 | n = sscanf(name+2, |
| 72 | "%c %d " |
| 73 | "%*s %*s %*s %*s " /* pgrp, session, tty, tpgid */ |
| 74 | "%*s %*s %*s %*s %*s " /* flags, min_flt, cmin_flt, maj_flt, cmaj_flt */ |
"Vladimir N. Oleynik" | f246dc7 | 2005-09-16 12:55:29 +0000 | [diff] [blame] | 75 | #ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 76 | "%lu %lu " |
| 77 | #else |
| 78 | "%*s %*s " |
| 79 | #endif |
| 80 | "%*s %*s %*s " /* cutime, cstime, priority */ |
| 81 | "%ld " |
| 82 | "%*s %*s %*s " /* timeout, it_real_value, start_time */ |
| 83 | "%*s " /* vsize */ |
| 84 | "%ld", |
| 85 | curstatus.state, &curstatus.ppid, |
"Vladimir N. Oleynik" | f246dc7 | 2005-09-16 12:55:29 +0000 | [diff] [blame] | 86 | #ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 87 | &curstatus.utime, &curstatus.stime, |
| 88 | #endif |
| 89 | &tasknice, |
| 90 | &curstatus.rss); |
"Vladimir N. Oleynik" | f246dc7 | 2005-09-16 12:55:29 +0000 | [diff] [blame] | 91 | #ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 92 | if(n != 6) |
| 93 | #else |
| 94 | if(n != 4) |
| 95 | #endif |
| 96 | continue; |
| 97 | |
| 98 | if (curstatus.rss == 0 && curstatus.state[0] != 'Z') |
| 99 | curstatus.state[1] = 'W'; |
| 100 | else |
| 101 | curstatus.state[1] = ' '; |
| 102 | if (tasknice < 0) |
| 103 | curstatus.state[2] = '<'; |
| 104 | else if (tasknice > 0) |
| 105 | curstatus.state[2] = 'N'; |
| 106 | else |
| 107 | curstatus.state[2] = ' '; |
| 108 | |
Glenn L McGrath | a1e4a0e | 2004-01-21 11:36:44 +0000 | [diff] [blame] | 109 | #ifdef PAGE_SHIFT |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 110 | curstatus.rss <<= (PAGE_SHIFT - 10); /* 2**10 = 1kb */ |
Glenn L McGrath | a1e4a0e | 2004-01-21 11:36:44 +0000 | [diff] [blame] | 111 | #else |
| 112 | curstatus.rss *= (getpagesize() >> 10); /* 2**10 = 1kb */ |
| 113 | #endif |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 114 | |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 115 | if(save_user_arg0) { |
Eric Andersen | fab3e12 | 2003-05-26 18:07:30 +0000 | [diff] [blame] | 116 | sprintf(status, "/proc/%d/cmdline", pid); |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 117 | if((fp = fopen(status, "r")) == NULL) |
| 118 | continue; |
Glenn L McGrath | 393ad1a | 2002-11-25 22:31:37 +0000 | [diff] [blame] | 119 | if((n=fread(buf, 1, sizeof(buf)-1, fp)) > 0) { |
| 120 | if(buf[n-1]=='\n') |
| 121 | buf[--n] = 0; |
| 122 | name = buf; |
| 123 | while(n) { |
Glenn L McGrath | 09adaca | 2002-12-02 21:18:10 +0000 | [diff] [blame] | 124 | if(((unsigned char)*name) < ' ') |
Glenn L McGrath | 393ad1a | 2002-11-25 22:31:37 +0000 | [diff] [blame] | 125 | *name = ' '; |
| 126 | name++; |
| 127 | n--; |
| 128 | } |
| 129 | *name = 0; |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 130 | if(buf[0]) |
| 131 | curstatus.cmd = strdup(buf); |
| 132 | /* if NULL it work true also */ |
| 133 | } |
| 134 | fclose(fp); |
| 135 | } |
| 136 | return memcpy(&ret_status, &curstatus, sizeof(procps_status_t)); |
| 137 | } |
| 138 | } |
| 139 | |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 140 | /* END CODE */ |
| 141 | /* |
| 142 | Local Variables: |
| 143 | c-file-style: "linux" |
| 144 | c-basic-offset: 4 |
| 145 | tab-width: 4 |
| 146 | End: |
| 147 | */ |