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> |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 7 | * |
Bernhard Reutner-Fischer | b1629b1 | 2006-05-19 19:29:19 +0000 | [diff] [blame] | 8 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 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> |
Rob Landley | 7818a42 | 2006-04-25 18:42:23 +0000 | [diff] [blame] | 14 | #include <sys/param.h> |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 15 | #include <unistd.h> |
Rob Landley | b280455 | 2006-02-13 22:04:27 +0000 | [diff] [blame] | 16 | #include <fcntl.h> |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 17 | |
Glenn L McGrath | 7b1eca2 | 2002-11-24 01:32:56 +0000 | [diff] [blame] | 18 | #include "libbb.h" |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 19 | |
Rob Landley | b280455 | 2006-02-13 22:04:27 +0000 | [diff] [blame] | 20 | |
"Vladimir N. Oleynik" | 465300c | 2006-02-14 10:17:09 +0000 | [diff] [blame] | 21 | #define PROCPS_BUFSIZE 1024 |
| 22 | |
| 23 | static int read_to_buf(const char *filename, void *buf) |
Rob Landley | b280455 | 2006-02-13 22:04:27 +0000 | [diff] [blame] | 24 | { |
| 25 | int fd; |
"Vladimir N. Oleynik" | 465300c | 2006-02-14 10:17:09 +0000 | [diff] [blame] | 26 | ssize_t ret; |
Rob Landley | b280455 | 2006-02-13 22:04:27 +0000 | [diff] [blame] | 27 | |
| 28 | fd = open(filename, O_RDONLY); |
Mike Frysinger | 58dda84 | 2006-07-12 20:04:00 +0000 | [diff] [blame] | 29 | if (fd < 0) |
Rob Landley | b280455 | 2006-02-13 22:04:27 +0000 | [diff] [blame] | 30 | return -1; |
Rob Landley | 22f383e | 2006-06-27 18:14:12 +0000 | [diff] [blame] | 31 | ret = read(fd, buf, PROCPS_BUFSIZE-1); |
| 32 | ((char *)buf)[ret > 0 ? ret : 0] = 0; |
Rob Landley | b280455 | 2006-02-13 22:04:27 +0000 | [diff] [blame] | 33 | close(fd); |
"Vladimir N. Oleynik" | 465300c | 2006-02-14 10:17:09 +0000 | [diff] [blame] | 34 | return ret; |
Rob Landley | b280455 | 2006-02-13 22:04:27 +0000 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 38 | procps_status_t * procps_scan(int save_user_arg0) |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 39 | { |
| 40 | static DIR *dir; |
| 41 | struct dirent *entry; |
| 42 | static procps_status_t ret_status; |
| 43 | char *name; |
| 44 | int n; |
| 45 | char status[32]; |
Rob Landley | b280455 | 2006-02-13 22:04:27 +0000 | [diff] [blame] | 46 | char *status_tail; |
"Vladimir N. Oleynik" | 465300c | 2006-02-14 10:17:09 +0000 | [diff] [blame] | 47 | char buf[PROCPS_BUFSIZE]; |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 48 | procps_status_t curstatus; |
| 49 | int pid; |
| 50 | long tasknice; |
| 51 | struct stat sb; |
| 52 | |
| 53 | if (!dir) { |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 54 | dir = xopendir("/proc"); |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 55 | } |
Mike Frysinger | 58dda84 | 2006-07-12 20:04:00 +0000 | [diff] [blame] | 56 | for (;;) { |
Denis Vlasenko | a6dbb08 | 2006-10-12 19:29:44 +0000 | [diff] [blame^] | 57 | entry = readdir(dir); |
| 58 | if (entry == NULL) { |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 59 | closedir(dir); |
| 60 | dir = 0; |
| 61 | return 0; |
| 62 | } |
| 63 | name = entry->d_name; |
| 64 | if (!(*name >= '0' && *name <= '9')) |
| 65 | continue; |
| 66 | |
| 67 | memset(&curstatus, 0, sizeof(procps_status_t)); |
| 68 | pid = atoi(name); |
| 69 | curstatus.pid = pid; |
| 70 | |
Rob Landley | b280455 | 2006-02-13 22:04:27 +0000 | [diff] [blame] | 71 | status_tail = status + sprintf(status, "/proc/%d", pid); |
Mike Frysinger | 58dda84 | 2006-07-12 20:04:00 +0000 | [diff] [blame] | 72 | if (stat(status, &sb)) |
Eric Andersen | 242ab83 | 2004-01-27 20:17:39 +0000 | [diff] [blame] | 73 | continue; |
Bernhard Reutner-Fischer | d5bd137 | 2005-09-20 21:06:17 +0000 | [diff] [blame] | 74 | bb_getpwuid(curstatus.user, sb.st_uid, sizeof(curstatus.user)); |
Eric Andersen | 242ab83 | 2004-01-27 20:17:39 +0000 | [diff] [blame] | 75 | |
Mike Frysinger | 301ad67 | 2006-06-07 20:24:34 +0000 | [diff] [blame] | 76 | /* see proc(5) for some details on this */ |
Rob Landley | b280455 | 2006-02-13 22:04:27 +0000 | [diff] [blame] | 77 | strcpy(status_tail, "/stat"); |
"Vladimir N. Oleynik" | 465300c | 2006-02-14 10:17:09 +0000 | [diff] [blame] | 78 | n = read_to_buf(status, buf); |
Mike Frysinger | 58dda84 | 2006-07-12 20:04:00 +0000 | [diff] [blame] | 79 | if (n < 0) |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 80 | continue; |
| 81 | name = strrchr(buf, ')'); /* split into "PID (cmd" and "<rest>" */ |
Mike Frysinger | 58dda84 | 2006-07-12 20:04:00 +0000 | [diff] [blame] | 82 | if (name == 0 || name[1] != ' ') |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 83 | continue; |
| 84 | *name = 0; |
| 85 | sscanf(buf, "%*s (%15c", curstatus.short_cmd); |
| 86 | n = sscanf(name+2, |
| 87 | "%c %d " |
| 88 | "%*s %*s %*s %*s " /* pgrp, session, tty, tpgid */ |
| 89 | "%*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] | 90 | #ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE |
Mike Frysinger | 373af43 | 2006-06-07 21:37:59 +0000 | [diff] [blame] | 91 | "%lu %lu " /* utime, stime */ |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 92 | #else |
Mike Frysinger | 373af43 | 2006-06-07 21:37:59 +0000 | [diff] [blame] | 93 | "%*s %*s " /* utime, stime */ |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 94 | #endif |
| 95 | "%*s %*s %*s " /* cutime, cstime, priority */ |
Mike Frysinger | 373af43 | 2006-06-07 21:37:59 +0000 | [diff] [blame] | 96 | "%ld " /* nice */ |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 97 | "%*s %*s %*s " /* timeout, it_real_value, start_time */ |
| 98 | "%*s " /* vsize */ |
Mike Frysinger | 373af43 | 2006-06-07 21:37:59 +0000 | [diff] [blame] | 99 | "%ld", /* rss */ |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 100 | curstatus.state, &curstatus.ppid, |
"Vladimir N. Oleynik" | f246dc7 | 2005-09-16 12:55:29 +0000 | [diff] [blame] | 101 | #ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 102 | &curstatus.utime, &curstatus.stime, |
| 103 | #endif |
| 104 | &tasknice, |
| 105 | &curstatus.rss); |
"Vladimir N. Oleynik" | f246dc7 | 2005-09-16 12:55:29 +0000 | [diff] [blame] | 106 | #ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE |
Mike Frysinger | 58dda84 | 2006-07-12 20:04:00 +0000 | [diff] [blame] | 107 | if (n != 6) |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 108 | #else |
Mike Frysinger | 58dda84 | 2006-07-12 20:04:00 +0000 | [diff] [blame] | 109 | if (n != 4) |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 110 | #endif |
| 111 | continue; |
| 112 | |
| 113 | if (curstatus.rss == 0 && curstatus.state[0] != 'Z') |
| 114 | curstatus.state[1] = 'W'; |
| 115 | else |
| 116 | curstatus.state[1] = ' '; |
| 117 | if (tasknice < 0) |
| 118 | curstatus.state[2] = '<'; |
| 119 | else if (tasknice > 0) |
| 120 | curstatus.state[2] = 'N'; |
| 121 | else |
| 122 | curstatus.state[2] = ' '; |
| 123 | |
Glenn L McGrath | a1e4a0e | 2004-01-21 11:36:44 +0000 | [diff] [blame] | 124 | #ifdef PAGE_SHIFT |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 125 | curstatus.rss <<= (PAGE_SHIFT - 10); /* 2**10 = 1kb */ |
Glenn L McGrath | a1e4a0e | 2004-01-21 11:36:44 +0000 | [diff] [blame] | 126 | #else |
| 127 | curstatus.rss *= (getpagesize() >> 10); /* 2**10 = 1kb */ |
| 128 | #endif |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 129 | |
Mike Frysinger | 58dda84 | 2006-07-12 20:04:00 +0000 | [diff] [blame] | 130 | if (save_user_arg0) { |
Rob Landley | b280455 | 2006-02-13 22:04:27 +0000 | [diff] [blame] | 131 | strcpy(status_tail, "/cmdline"); |
"Vladimir N. Oleynik" | 465300c | 2006-02-14 10:17:09 +0000 | [diff] [blame] | 132 | n = read_to_buf(status, buf); |
Mike Frysinger | 58dda84 | 2006-07-12 20:04:00 +0000 | [diff] [blame] | 133 | if (n > 0) { |
| 134 | if (buf[n-1]=='\n') |
Glenn L McGrath | 393ad1a | 2002-11-25 22:31:37 +0000 | [diff] [blame] | 135 | buf[--n] = 0; |
| 136 | name = buf; |
Mike Frysinger | 58dda84 | 2006-07-12 20:04:00 +0000 | [diff] [blame] | 137 | while (n) { |
| 138 | if (((unsigned char)*name) < ' ') |
Glenn L McGrath | 393ad1a | 2002-11-25 22:31:37 +0000 | [diff] [blame] | 139 | *name = ' '; |
| 140 | name++; |
| 141 | n--; |
| 142 | } |
| 143 | *name = 0; |
Mike Frysinger | 58dda84 | 2006-07-12 20:04:00 +0000 | [diff] [blame] | 144 | if (buf[0]) |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 145 | curstatus.cmd = strdup(buf); |
| 146 | /* if NULL it work true also */ |
| 147 | } |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 148 | } |
| 149 | return memcpy(&ret_status, &curstatus, sizeof(procps_status_t)); |
| 150 | } |
| 151 | } |