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 | |
Glenn L McGrath | 7b1eca2 | 2002-11-24 01:32:56 +0000 | [diff] [blame] | 11 | #include "libbb.h" |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 12 | |
Rob Landley | b280455 | 2006-02-13 22:04:27 +0000 | [diff] [blame] | 13 | |
Denis Vlasenko | c50f370 | 2006-11-24 14:57:31 +0000 | [diff] [blame] | 14 | typedef struct unsigned_to_name_map_t { |
| 15 | unsigned id; |
Denis Vlasenko | 2405ad6 | 2007-01-19 21:24:17 +0000 | [diff] [blame] | 16 | char name[USERNAME_MAX_SIZE]; |
Denis Vlasenko | c50f370 | 2006-11-24 14:57:31 +0000 | [diff] [blame] | 17 | } unsigned_to_name_map_t; |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 18 | |
Denis Vlasenko | c50f370 | 2006-11-24 14:57:31 +0000 | [diff] [blame] | 19 | typedef struct cache_t { |
| 20 | unsigned_to_name_map_t *cache; |
| 21 | int size; |
| 22 | } cache_t; |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 23 | |
Denis Vlasenko | c50f370 | 2006-11-24 14:57:31 +0000 | [diff] [blame] | 24 | static cache_t username, groupname; |
| 25 | |
| 26 | static void clear_cache(cache_t *cp) |
| 27 | { |
| 28 | free(cp->cache); |
| 29 | cp->cache = NULL; |
| 30 | cp->size = 0; |
| 31 | } |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 32 | void clear_username_cache(void) |
| 33 | { |
Denis Vlasenko | c50f370 | 2006-11-24 14:57:31 +0000 | [diff] [blame] | 34 | clear_cache(&username); |
| 35 | clear_cache(&groupname); |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 36 | } |
| 37 | |
Denis Vlasenko | b8a8e60 | 2006-11-24 14:59:45 +0000 | [diff] [blame] | 38 | #if 0 /* more generic, but we don't need that yet */ |
Denis Vlasenko | c50f370 | 2006-11-24 14:57:31 +0000 | [diff] [blame] | 39 | /* Returns -N-1 if not found. */ |
| 40 | /* cp->cache[N] is allocated and must be filled in this case */ |
| 41 | static int get_cached(cache_t *cp, unsigned id) |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 42 | { |
| 43 | int i; |
Denis Vlasenko | c50f370 | 2006-11-24 14:57:31 +0000 | [diff] [blame] | 44 | for (i = 0; i < cp->size; i++) |
| 45 | if (cp->cache[i].id == id) |
| 46 | return i; |
| 47 | i = cp->size++; |
| 48 | cp->cache = xrealloc(cp->cache, cp->size * sizeof(*cp->cache)); |
| 49 | cp->cache[i++].id = id; |
| 50 | return -i; |
| 51 | } |
Denis Vlasenko | b8a8e60 | 2006-11-24 14:59:45 +0000 | [diff] [blame] | 52 | #endif |
| 53 | |
| 54 | typedef char* ug_func(char *name, long uid, int bufsize); |
| 55 | static char* get_cached(cache_t *cp, unsigned id, ug_func* fp) |
| 56 | { |
| 57 | int i; |
| 58 | for (i = 0; i < cp->size; i++) |
| 59 | if (cp->cache[i].id == id) |
| 60 | return cp->cache[i].name; |
| 61 | i = cp->size++; |
| 62 | cp->cache = xrealloc(cp->cache, cp->size * sizeof(*cp->cache)); |
| 63 | cp->cache[i].id = id; |
| 64 | fp(cp->cache[i].name, id, sizeof(cp->cache[i].name)); |
| 65 | return cp->cache[i].name; |
| 66 | } |
Denis Vlasenko | c50f370 | 2006-11-24 14:57:31 +0000 | [diff] [blame] | 67 | const char* get_cached_username(uid_t uid) |
| 68 | { |
Denis Vlasenko | b8a8e60 | 2006-11-24 14:59:45 +0000 | [diff] [blame] | 69 | return get_cached(&username, uid, bb_getpwuid); |
Denis Vlasenko | c50f370 | 2006-11-24 14:57:31 +0000 | [diff] [blame] | 70 | } |
Denis Vlasenko | b8a8e60 | 2006-11-24 14:59:45 +0000 | [diff] [blame] | 71 | const char* get_cached_groupname(gid_t gid) |
Denis Vlasenko | c50f370 | 2006-11-24 14:57:31 +0000 | [diff] [blame] | 72 | { |
Denis Vlasenko | b8a8e60 | 2006-11-24 14:59:45 +0000 | [diff] [blame] | 73 | return get_cached(&groupname, gid, bb_getgrgid); |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | |
"Vladimir N. Oleynik" | 465300c | 2006-02-14 10:17:09 +0000 | [diff] [blame] | 77 | #define PROCPS_BUFSIZE 1024 |
| 78 | |
| 79 | static int read_to_buf(const char *filename, void *buf) |
Rob Landley | b280455 | 2006-02-13 22:04:27 +0000 | [diff] [blame] | 80 | { |
"Vladimir N. Oleynik" | 465300c | 2006-02-14 10:17:09 +0000 | [diff] [blame] | 81 | ssize_t ret; |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 82 | ret = open_read_close(filename, buf, PROCPS_BUFSIZE-1); |
| 83 | ((char *)buf)[ret > 0 ? ret : 0] = '\0'; |
"Vladimir N. Oleynik" | 465300c | 2006-02-14 10:17:09 +0000 | [diff] [blame] | 84 | return ret; |
Rob Landley | b280455 | 2006-02-13 22:04:27 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 87 | procps_status_t* alloc_procps_scan(int flags) |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 88 | { |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 89 | procps_status_t* sp = xzalloc(sizeof(procps_status_t)); |
| 90 | sp->dir = xopendir("/proc"); |
| 91 | return sp; |
| 92 | } |
Denis Vlasenko | 35fb512 | 2006-11-01 09:16:49 +0000 | [diff] [blame] | 93 | |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 94 | void free_procps_scan(procps_status_t* sp) |
| 95 | { |
| 96 | closedir(sp->dir); |
| 97 | free(sp->cmd); |
| 98 | free(sp); |
| 99 | } |
| 100 | |
| 101 | void BUG_comm_size(void); |
| 102 | procps_status_t* procps_scan(procps_status_t* sp, int flags) |
| 103 | { |
Denis Vlasenko | 35fb512 | 2006-11-01 09:16:49 +0000 | [diff] [blame] | 104 | struct dirent *entry; |
"Vladimir N. Oleynik" | 465300c | 2006-02-14 10:17:09 +0000 | [diff] [blame] | 105 | char buf[PROCPS_BUFSIZE]; |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 106 | char filename[sizeof("/proc//cmdline") + sizeof(int)*3]; |
| 107 | char *filename_tail; |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 108 | long tasknice; |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 109 | unsigned pid; |
Denis Vlasenko | dd539f7 | 2006-11-01 20:20:37 +0000 | [diff] [blame] | 110 | int n; |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 111 | struct stat sb; |
| 112 | |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 113 | if (!sp) |
| 114 | sp = alloc_procps_scan(flags); |
| 115 | |
Mike Frysinger | 58dda84 | 2006-07-12 20:04:00 +0000 | [diff] [blame] | 116 | for (;;) { |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 117 | entry = readdir(sp->dir); |
Denis Vlasenko | a6dbb08 | 2006-10-12 19:29:44 +0000 | [diff] [blame] | 118 | if (entry == NULL) { |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 119 | free_procps_scan(sp); |
| 120 | return NULL; |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 121 | } |
Denis Vlasenko | d686a04 | 2006-11-27 14:43:21 +0000 | [diff] [blame] | 122 | pid = bb_strtou(entry->d_name, NULL, 10); |
| 123 | if (errno) |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 124 | continue; |
| 125 | |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 126 | /* After this point we have to break, not continue |
| 127 | * ("continue" would mean that current /proc/NNN |
| 128 | * is not a valid process info) */ |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 129 | |
Mike Frysinger | 0aa6ba5 | 2007-02-08 08:21:58 +0000 | [diff] [blame] | 130 | memset(&sp->vsz, 0, sizeof(*sp) - offsetof(procps_status_t, vsz)); |
Eric Andersen | 242ab83 | 2004-01-27 20:17:39 +0000 | [diff] [blame] | 131 | |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 132 | sp->pid = pid; |
| 133 | if (!(flags & ~PSSCAN_PID)) break; |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 134 | |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 135 | filename_tail = filename + sprintf(filename, "/proc/%d", pid); |
| 136 | |
| 137 | if (flags & PSSCAN_UIDGID) { |
| 138 | if (stat(filename, &sb)) |
| 139 | break; |
Denis Vlasenko | 2405ad6 | 2007-01-19 21:24:17 +0000 | [diff] [blame] | 140 | /* Need comment - is this effective or real UID/GID? */ |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 141 | sp->uid = sb.st_uid; |
| 142 | sp->gid = sb.st_gid; |
| 143 | } |
Denis Vlasenko | f7996f3 | 2007-01-11 17:20:00 +0000 | [diff] [blame] | 144 | |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 145 | if (flags & PSSCAN_STAT) { |
| 146 | char *cp; |
Denis Vlasenko | 516a0ca | 2007-04-19 14:46:14 +0000 | [diff] [blame] | 147 | unsigned long vsz, rss; |
| 148 | int tty; |
| 149 | |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 150 | /* see proc(5) for some details on this */ |
| 151 | strcpy(filename_tail, "/stat"); |
| 152 | n = read_to_buf(filename, buf); |
| 153 | if (n < 0) |
| 154 | break; |
| 155 | cp = strrchr(buf, ')'); /* split into "PID (cmd" and "<rest>" */ |
| 156 | if (!cp || cp[1] != ' ') |
| 157 | break; |
| 158 | cp[0] = '\0'; |
| 159 | if (sizeof(sp->comm) < 16) |
| 160 | BUG_comm_size(); |
| 161 | sscanf(buf, "%*s (%15c", sp->comm); |
| 162 | n = sscanf(cp+2, |
| 163 | "%c %u " /* state, ppid */ |
Denis Vlasenko | 516a0ca | 2007-04-19 14:46:14 +0000 | [diff] [blame] | 164 | "%u %u %d %*s " /* pgid, sid, tty, tpgid */ |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 165 | "%*s %*s %*s %*s %*s " /* flags, min_flt, cmin_flt, maj_flt, cmaj_flt */ |
| 166 | "%lu %lu " /* utime, stime */ |
| 167 | "%*s %*s %*s " /* cutime, cstime, priority */ |
| 168 | "%ld " /* nice */ |
| 169 | "%*s %*s %*s " /* timeout, it_real_value, start_time */ |
Denis Vlasenko | 516a0ca | 2007-04-19 14:46:14 +0000 | [diff] [blame] | 170 | "%lu " /* vsize */ |
| 171 | "%lu " /* rss */ |
| 172 | /* "%lu %lu %lu %lu %lu %lu " rss_rlim, start_code, end_code, start_stack, kstk_esp, kstk_eip */ |
| 173 | /* "%u %u %u %u " signal, blocked, sigignore, sigcatch */ |
| 174 | /* "%lu %lu %lu" wchan, nswap, cnswap */ |
| 175 | , |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 176 | sp->state, &sp->ppid, |
Denis Vlasenko | 516a0ca | 2007-04-19 14:46:14 +0000 | [diff] [blame] | 177 | &sp->pgid, &sp->sid, &tty, |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 178 | &sp->utime, &sp->stime, |
| 179 | &tasknice, |
Denis Vlasenko | 516a0ca | 2007-04-19 14:46:14 +0000 | [diff] [blame] | 180 | &vsz, |
| 181 | &rss); |
| 182 | if (n != 10) |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 183 | break; |
| 184 | |
Denis Vlasenko | 516a0ca | 2007-04-19 14:46:14 +0000 | [diff] [blame] | 185 | sp->tty_str[0] = '?'; |
| 186 | /* sp->tty_str[1] = '\0'; - done by memset */ |
Denis Vlasenko | 8581863 | 2007-04-19 14:47:11 +0000 | [diff] [blame^] | 187 | if (tty) /* tty field of "0" means "no tty" */ |
Denis Vlasenko | 516a0ca | 2007-04-19 14:46:14 +0000 | [diff] [blame] | 188 | snprintf(sp->tty_str, sizeof(sp->tty_str), "%u,%u", |
| 189 | (tty >> 8) & 0xfff, /* major */ |
| 190 | (tty & 0xff) | ((tty >> 12) & 0xfff00)); |
Mike Frysinger | 0aa6ba5 | 2007-02-08 08:21:58 +0000 | [diff] [blame] | 191 | if (sp->vsz == 0 && sp->state[0] != 'Z') |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 192 | sp->state[1] = 'W'; |
| 193 | else |
| 194 | sp->state[1] = ' '; |
| 195 | if (tasknice < 0) |
| 196 | sp->state[2] = '<'; |
Denis Vlasenko | 516a0ca | 2007-04-19 14:46:14 +0000 | [diff] [blame] | 197 | else if (tasknice) /* > 0 */ |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 198 | sp->state[2] = 'N'; |
| 199 | else |
| 200 | sp->state[2] = ' '; |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 201 | |
Denis Vlasenko | 516a0ca | 2007-04-19 14:46:14 +0000 | [diff] [blame] | 202 | sp->vsz = vsz >> 10; /* vsize is in bytes and we want kb */ |
| 203 | sp->rss = rss >> 10; |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 204 | } |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 205 | |
| 206 | if (flags & PSSCAN_CMD) { |
| 207 | free(sp->cmd); |
| 208 | sp->cmd = NULL; |
| 209 | strcpy(filename_tail, "/cmdline"); |
| 210 | n = read_to_buf(filename, buf); |
| 211 | if (n <= 0) |
| 212 | break; |
| 213 | if (buf[n-1] == '\n') { |
| 214 | if (!--n) |
| 215 | break; |
| 216 | buf[n] = '\0'; |
| 217 | } |
| 218 | do { |
| 219 | n--; |
| 220 | if ((unsigned char)(buf[n]) < ' ') |
| 221 | buf[n] = ' '; |
| 222 | } while (n); |
Denis Vlasenko | 2405ad6 | 2007-01-19 21:24:17 +0000 | [diff] [blame] | 223 | sp->cmd = xstrdup(buf); |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 224 | } |
| 225 | break; |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 226 | } |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 227 | return sp; |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 228 | } |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 229 | /* from kernel: |
| 230 | // pid comm S ppid pgid sid tty_nr tty_pgrp flg |
Denis Vlasenko | d031ffa | 2006-11-24 21:54:44 +0000 | [diff] [blame] | 231 | sprintf(buffer,"%d (%s) %c %d %d %d %d %d %lu %lu \ |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 232 | %lu %lu %lu %lu %lu %ld %ld %ld %ld %d 0 %llu %lu %ld %lu %lu %lu %lu %lu \ |
| 233 | %lu %lu %lu %lu %lu %lu %lu %lu %d %d %lu %lu %llu\n", |
Denis Vlasenko | d031ffa | 2006-11-24 21:54:44 +0000 | [diff] [blame] | 234 | task->pid, |
| 235 | tcomm, |
| 236 | state, |
| 237 | ppid, |
| 238 | pgid, |
| 239 | sid, |
| 240 | tty_nr, |
| 241 | tty_pgrp, |
| 242 | task->flags, |
| 243 | min_flt, |
Denis Vlasenko | d031ffa | 2006-11-24 21:54:44 +0000 | [diff] [blame] | 244 | cmin_flt, |
| 245 | maj_flt, |
| 246 | cmaj_flt, |
| 247 | cputime_to_clock_t(utime), |
| 248 | cputime_to_clock_t(stime), |
| 249 | cputime_to_clock_t(cutime), |
| 250 | cputime_to_clock_t(cstime), |
| 251 | priority, |
| 252 | nice, |
| 253 | num_threads, |
| 254 | // 0, |
| 255 | start_time, |
| 256 | vsize, |
| 257 | mm ? get_mm_rss(mm) : 0, |
| 258 | rsslim, |
| 259 | mm ? mm->start_code : 0, |
| 260 | mm ? mm->end_code : 0, |
| 261 | mm ? mm->start_stack : 0, |
| 262 | esp, |
| 263 | eip, |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 264 | the rest is some obsolete cruft |
| 265 | */ |