From 4aca87515a5083ae0e31ce3177189fd43b6d05ac Mon Sep 17 00:00:00 2001 From: Andreas Baumann Date: Sat, 3 Jan 2015 13:58:15 +0100 Subject: patch to Vanilla Tomato 1.28 --- release/src/router/busybox/libbb/procps.c | 558 +++++++++++++++++++++++------- 1 file changed, 441 insertions(+), 117 deletions(-) (limited to 'release/src/router/busybox/libbb/procps.c') diff --git a/release/src/router/busybox/libbb/procps.c b/release/src/router/busybox/libbb/procps.c index 44103fae..c5e40bf8 100644 --- a/release/src/router/busybox/libbb/procps.c +++ b/release/src/router/busybox/libbb/procps.c @@ -4,149 +4,473 @@ * * Copyright 1998 by Albert Cahalan; all rights reserved. * Copyright (C) 2002 by Vladimir Oleynik - * GNU Library General Public License Version 2, or any later version + * SELinux support: (c) 2007 by Yuichi Nakamura * + * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. */ -#include -#include -#include -#include -#include - #include "libbb.h" -extern procps_status_t * procps_scan(int save_user_arg0 -#ifdef CONFIG_SELINUX - , int use_selinux , security_id_t *sid + +typedef struct unsigned_to_name_map_t { + long id; + char name[USERNAME_MAX_SIZE]; +} unsigned_to_name_map_t; + +typedef struct cache_t { + unsigned_to_name_map_t *cache; + int size; +} cache_t; + +static cache_t username, groupname; + +static void clear_cache(cache_t *cp) +{ + free(cp->cache); + cp->cache = NULL; + cp->size = 0; +} +void FAST_FUNC clear_username_cache(void) +{ + clear_cache(&username); + clear_cache(&groupname); +} + +#if 0 /* more generic, but we don't need that yet */ +/* Returns -N-1 if not found. */ +/* cp->cache[N] is allocated and must be filled in this case */ +static int get_cached(cache_t *cp, unsigned id) +{ + int i; + for (i = 0; i < cp->size; i++) + if (cp->cache[i].id == id) + return i; + i = cp->size++; + cp->cache = xrealloc_vector(cp->cache, 2, i); + cp->cache[i++].id = id; + return -i; +} +#endif + +static char* get_cached(cache_t *cp, long id, + char* FAST_FUNC x2x_utoa(long id)) +{ + int i; + for (i = 0; i < cp->size; i++) + if (cp->cache[i].id == id) + return cp->cache[i].name; + i = cp->size++; + cp->cache = xrealloc_vector(cp->cache, 2, i); + cp->cache[i].id = id; + /* Never fails. Generates numeric string if name isn't found */ + safe_strncpy(cp->cache[i].name, x2x_utoa(id), sizeof(cp->cache[i].name)); + return cp->cache[i].name; +} +const char* FAST_FUNC get_cached_username(uid_t uid) +{ + return get_cached(&username, uid, uid2uname_utoa); +} +const char* FAST_FUNC get_cached_groupname(gid_t gid) +{ + return get_cached(&groupname, gid, gid2group_utoa); +} + + +#define PROCPS_BUFSIZE 1024 + +static int read_to_buf(const char *filename, void *buf) +{ + int fd; + /* open_read_close() would do two reads, checking for EOF. + * When you have 10000 /proc/$NUM/stat to read, it isn't desirable */ + ssize_t ret = -1; + fd = open(filename, O_RDONLY); + if (fd >= 0) { + ret = read(fd, buf, PROCPS_BUFSIZE-1); + close(fd); + } + ((char *)buf)[ret > 0 ? ret : 0] = '\0'; + return ret; +} + +static procps_status_t* FAST_FUNC alloc_procps_scan(void) +{ + unsigned n = getpagesize(); + procps_status_t* sp = xzalloc(sizeof(procps_status_t)); + sp->dir = xopendir("/proc"); + while (1) { + n >>= 1; + if (!n) break; + sp->shift_pages_to_bytes++; + } + sp->shift_pages_to_kb = sp->shift_pages_to_bytes - 10; + return sp; +} + +void FAST_FUNC free_procps_scan(procps_status_t* sp) +{ + closedir(sp->dir); + free(sp->argv0); + USE_SELINUX(free(sp->context);) + free(sp); +} + +#if ENABLE_FEATURE_TOPMEM +static unsigned long fast_strtoul_16(char **endptr) +{ + unsigned char c; + char *str = *endptr; + unsigned long n = 0; + + while ((c = *str++) != ' ') { + c = ((c|0x20) - '0'); + if (c > 9) + // c = c + '0' - 'a' + 10: + c = c - ('a' - '0' - 10); + n = n*16 + c; + } + *endptr = str; /* We skip trailing space! */ + return n; +} +/* TOPMEM uses fast_strtoul_10, so... */ +#undef ENABLE_FEATURE_FAST_TOP +#define ENABLE_FEATURE_FAST_TOP 1 +#endif + +#if ENABLE_FEATURE_FAST_TOP +/* We cut a lot of corners here for speed */ +static unsigned long fast_strtoul_10(char **endptr) +{ + char c; + char *str = *endptr; + unsigned long n = *str - '0'; + + while ((c = *++str) != ' ') + n = n*10 + (c - '0'); + + *endptr = str + 1; /* We skip trailing space! */ + return n; +} +static char *skip_fields(char *str, int count) +{ + do { + while (*str++ != ' ') + continue; + /* we found a space char, str points after it */ + } while (--count); + return str; +} #endif - ) + +void BUG_comm_size(void); +procps_status_t* FAST_FUNC procps_scan(procps_status_t* sp, int flags) { - static DIR *dir; struct dirent *entry; - static procps_status_t ret_status; - char *name; - int n; - char status[32]; - char buf[1024]; - FILE *fp; - procps_status_t curstatus; - int pid; + char buf[PROCPS_BUFSIZE]; + char filename[sizeof("/proc//cmdline") + sizeof(int)*3]; + char *filename_tail; long tasknice; + unsigned pid; + int n; struct stat sb; - if (!dir) { - dir = opendir("/proc"); - if(!dir) - bb_error_msg_and_die("Can't open /proc"); - } - for(;;) { - if((entry = readdir(dir)) == NULL) { - closedir(dir); - dir = 0; - return 0; + if (!sp) + sp = alloc_procps_scan(); + + for (;;) { + entry = readdir(sp->dir); + if (entry == NULL) { + free_procps_scan(sp); + return NULL; } - name = entry->d_name; - if (!(*name >= '0' && *name <= '9')) + pid = bb_strtou(entry->d_name, NULL, 10); + if (errno) continue; - memset(&curstatus, 0, sizeof(procps_status_t)); - pid = atoi(name); - curstatus.pid = pid; + /* After this point we have to break, not continue + * ("continue" would mean that current /proc/NNN + * is not a valid process info) */ - sprintf(status, "/proc/%d/stat", pid); - if((fp = fopen(status, "r")) == NULL) - continue; -#ifdef CONFIG_SELINUX - if(use_selinux) - { - if(fstat_secure(fileno(fp), &sb, sid)) - continue; + memset(&sp->vsz, 0, sizeof(*sp) - offsetof(procps_status_t, vsz)); + + sp->pid = pid; + if (!(flags & ~PSSCAN_PID)) break; + +#if ENABLE_SELINUX + if (flags & PSSCAN_CONTEXT) { + if (getpidcon(sp->pid, &sp->context) < 0) + sp->context = NULL; } - else #endif - if(fstat(fileno(fp), &sb)) - continue; - my_getpwuid(curstatus.user, sb.st_uid); - name = fgets(buf, sizeof(buf), fp); - fclose(fp); - if(name == NULL) - continue; - name = strrchr(buf, ')'); /* split into "PID (cmd" and "" */ - if(name == 0 || name[1] != ' ') - continue; - *name = 0; - sscanf(buf, "%*s (%15c", curstatus.short_cmd); - n = sscanf(name+2, - "%c %d " - "%*s %*s %*s %*s " /* pgrp, session, tty, tpgid */ - "%*s %*s %*s %*s %*s " /* flags, min_flt, cmin_flt, maj_flt, cmaj_flt */ -#ifdef FEATURE_CPU_USAGE_PERCENTAGE - "%lu %lu " -#else - "%*s %*s " + + filename_tail = filename + sprintf(filename, "/proc/%d", pid); + + if (flags & PSSCAN_UIDGID) { + if (stat(filename, &sb)) + break; + /* Need comment - is this effective or real UID/GID? */ + sp->uid = sb.st_uid; + sp->gid = sb.st_gid; + } + + if (flags & PSSCAN_STAT) { + char *cp, *comm1; + int tty; +#if !ENABLE_FEATURE_FAST_TOP + unsigned long vsz, rss; +#endif + /* see proc(5) for some details on this */ + strcpy(filename_tail, "/stat"); + n = read_to_buf(filename, buf); + if (n < 0) + break; + cp = strrchr(buf, ')'); /* split into "PID (cmd" and "" */ + /*if (!cp || cp[1] != ' ') + break;*/ + cp[0] = '\0'; + if (sizeof(sp->comm) < 16) + BUG_comm_size(); + comm1 = strchr(buf, '('); + /*if (comm1)*/ + safe_strncpy(sp->comm, comm1 + 1, sizeof(sp->comm)); + +#if !ENABLE_FEATURE_FAST_TOP + n = sscanf(cp+2, + "%c %u " /* state, ppid */ + "%u %u %d %*s " /* pgid, sid, tty, tpgid */ + "%*s %*s %*s %*s %*s " /* flags, min_flt, cmin_flt, maj_flt, cmaj_flt */ + "%lu %lu " /* utime, stime */ + "%*s %*s %*s " /* cutime, cstime, priority */ + "%ld " /* nice */ + "%*s %*s " /* timeout, it_real_value */ + "%lu " /* start_time */ + "%lu " /* vsize */ + "%lu " /* rss */ +#if ENABLE_FEATURE_TOP_SMP_PROCESS + "%*s %*s %*s %*s %*s %*s " /*rss_rlim, start_code, end_code, start_stack, kstk_esp, kstk_eip */ + "%*s %*s %*s %*s " /*signal, blocked, sigignore, sigcatch */ + "%*s %*s %*s %*s " /*wchan, nswap, cnswap, exit_signal */ + "%d" /*cpu last seen on*/ +#endif + , + sp->state, &sp->ppid, + &sp->pgid, &sp->sid, &tty, + &sp->utime, &sp->stime, + &tasknice, + &sp->start_time, + &vsz, + &rss +#if ENABLE_FEATURE_TOP_SMP_PROCESS + , &sp->last_seen_on_cpu #endif - "%*s %*s %*s " /* cutime, cstime, priority */ - "%ld " - "%*s %*s %*s " /* timeout, it_real_value, start_time */ - "%*s " /* vsize */ - "%ld", - curstatus.state, &curstatus.ppid, -#ifdef FEATURE_CPU_USAGE_PERCENTAGE - &curstatus.utime, &curstatus.stime, + ); + + if (n < 11) + break; +#if ENABLE_FEATURE_TOP_SMP_PROCESS + if (n < 11+15) + sp->last_seen_on_cpu = 0; #endif - &tasknice, - &curstatus.rss); -#ifdef FEATURE_CPU_USAGE_PERCENTAGE - if(n != 6) + + /* vsz is in bytes and we want kb */ + sp->vsz = vsz >> 10; + /* vsz is in bytes but rss is in *PAGES*! Can you believe that? */ + sp->rss = rss << sp->shift_pages_to_kb; + sp->tty_major = (tty >> 8) & 0xfff; + sp->tty_minor = (tty & 0xff) | ((tty >> 12) & 0xfff00); #else - if(n != 4) +/* This costs ~100 bytes more but makes top faster by 20% + * If you run 10000 processes, this may be important for you */ + sp->state[0] = cp[2]; + cp += 4; + sp->ppid = fast_strtoul_10(&cp); + sp->pgid = fast_strtoul_10(&cp); + sp->sid = fast_strtoul_10(&cp); + tty = fast_strtoul_10(&cp); + sp->tty_major = (tty >> 8) & 0xfff; + sp->tty_minor = (tty & 0xff) | ((tty >> 12) & 0xfff00); + cp = skip_fields(cp, 6); /* tpgid, flags, min_flt, cmin_flt, maj_flt, cmaj_flt */ + sp->utime = fast_strtoul_10(&cp); + sp->stime = fast_strtoul_10(&cp); + cp = skip_fields(cp, 3); /* cutime, cstime, priority */ + tasknice = fast_strtoul_10(&cp); + cp = skip_fields(cp, 2); /* timeout, it_real_value */ + sp->start_time = fast_strtoul_10(&cp); + /* vsz is in bytes and we want kb */ + sp->vsz = fast_strtoul_10(&cp) >> 10; + /* vsz is in bytes but rss is in *PAGES*! Can you believe that? */ + sp->rss = fast_strtoul_10(&cp) << sp->shift_pages_to_kb; +#if ENABLE_FEATURE_TOP_SMP_PROCESS + /* (6): rss_rlim, start_code, end_code, start_stack, kstk_esp, kstk_eip */ + /* (4): signal, blocked, sigignore, sigcatch */ + /* (4): wchan, nswap, cnswap, exit_signal */ + cp = skip_fields(cp, 14); +//FIXME: is it safe to assume this field exists? + sp->last_seen_on_cpu = fast_strtoul_10(&cp); #endif - continue; +#endif /* end of !ENABLE_FEATURE_TOP_SMP_PROCESS */ - if (curstatus.rss == 0 && curstatus.state[0] != 'Z') - curstatus.state[1] = 'W'; - else - curstatus.state[1] = ' '; - if (tasknice < 0) - curstatus.state[2] = '<'; - else if (tasknice > 0) - curstatus.state[2] = 'N'; - else - curstatus.state[2] = ' '; - - curstatus.rss <<= (PAGE_SHIFT - 10); /* 2**10 = 1kb */ - - if(save_user_arg0) { - sprintf(status, "/proc/%d/cmdline", pid); - if((fp = fopen(status, "r")) == NULL) - continue; - if((n=fread(buf, 1, sizeof(buf)-1, fp)) > 0) { - if(buf[n-1]=='\n') - buf[--n] = 0; - name = buf; - while(n) { - if(((unsigned char)*name) < ' ') - *name = ' '; - name++; - n--; + if (sp->vsz == 0 && sp->state[0] != 'Z') + sp->state[1] = 'W'; + else + sp->state[1] = ' '; + if (tasknice < 0) + sp->state[2] = '<'; + else if (tasknice) /* > 0 */ + sp->state[2] = 'N'; + else + sp->state[2] = ' '; + } + +#if ENABLE_FEATURE_TOPMEM + if (flags & (PSSCAN_SMAPS)) { + FILE *file; + + strcpy(filename_tail, "/smaps"); + file = fopen_for_read(filename); + if (!file) + break; + while (fgets(buf, sizeof(buf), file)) { + unsigned long sz; + char *tp; + char w; +#define SCAN(str, name) \ + if (strncmp(buf, str, sizeof(str)-1) == 0) { \ + tp = skip_whitespace(buf + sizeof(str)-1); \ + sp->name += fast_strtoul_10(&tp); \ + continue; \ + } + SCAN("Shared_Clean:" , shared_clean ); + SCAN("Shared_Dirty:" , shared_dirty ); + SCAN("Private_Clean:", private_clean); + SCAN("Private_Dirty:", private_dirty); +#undef SCAN + // f7d29000-f7d39000 rw-s ADR M:m OFS FILE + tp = strchr(buf, '-'); + if (tp) { + *tp = ' '; + tp = buf; + sz = fast_strtoul_16(&tp); /* start */ + sz = (fast_strtoul_16(&tp) - sz) >> 10; /* end - start */ + // tp -> "rw-s" string + w = tp[1]; + // skipping "rw-s ADR M:m OFS " + tp = skip_whitespace(skip_fields(tp, 4)); + // filter out /dev/something (something != zero) + if (strncmp(tp, "/dev/", 5) != 0 || strcmp(tp, "/dev/zero\n") == 0) { + if (w == 'w') { + sp->mapped_rw += sz; + } else if (w == '-') { + sp->mapped_ro += sz; + } + } +//else printf("DROPPING %s (%s)\n", buf, tp); + if (strcmp(tp, "[stack]\n") == 0) + sp->stack += sz; } - *name = 0; - if(buf[0]) - curstatus.cmd = strdup(buf); - /* if NULL it work true also */ } - fclose(fp); + fclose(file); + } +#endif /* TOPMEM */ + +#if 0 /* PSSCAN_CMD is not used */ + if (flags & (PSSCAN_CMD|PSSCAN_ARGV0)) { + free(sp->argv0); + sp->argv0 = NULL; + free(sp->cmd); + sp->cmd = NULL; + strcpy(filename_tail, "/cmdline"); + /* TODO: to get rid of size limits, read into malloc buf, + * then realloc it down to real size. */ + n = read_to_buf(filename, buf); + if (n <= 0) + break; + if (flags & PSSCAN_ARGV0) + sp->argv0 = xstrdup(buf); + if (flags & PSSCAN_CMD) { + do { + n--; + if ((unsigned char)(buf[n]) < ' ') + buf[n] = ' '; + } while (n); + sp->cmd = xstrdup(buf); + } + } +#else + if (flags & (PSSCAN_ARGV0|PSSCAN_ARGVN)) { + free(sp->argv0); + sp->argv0 = NULL; + strcpy(filename_tail, "/cmdline"); + n = read_to_buf(filename, buf); + if (n <= 0) + break; + if (flags & PSSCAN_ARGVN) { + sp->argv_len = n; + sp->argv0 = xmalloc(n + 1); + memcpy(sp->argv0, buf, n + 1); + /* sp->argv0[n] = '\0'; - buf has it */ + } else { + sp->argv_len = 0; + sp->argv0 = xstrdup(buf); + } } - return memcpy(&ret_status, &curstatus, sizeof(procps_status_t)); +#endif + break; } + return sp; } -/* END CODE */ -/* -Local Variables: -c-file-style: "linux" -c-basic-offset: 4 -tab-width: 4 -End: +void FAST_FUNC read_cmdline(char *buf, int col, unsigned pid, const char *comm) +{ + ssize_t sz; + char filename[sizeof("/proc//cmdline") + sizeof(int)*3]; + + sprintf(filename, "/proc/%u/cmdline", pid); + sz = open_read_close(filename, buf, col); + if (sz > 0) { + buf[sz] = '\0'; + while (--sz >= 0) + if ((unsigned char)(buf[sz]) < ' ') + buf[sz] = ' '; + } else { + snprintf(buf, col, "[%s]", comm); + } +} + +/* from kernel: + // pid comm S ppid pgid sid tty_nr tty_pgrp flg + sprintf(buffer,"%d (%s) %c %d %d %d %d %d %lu %lu \ +%lu %lu %lu %lu %lu %ld %ld %ld %ld %d 0 %llu %lu %ld %lu %lu %lu %lu %lu \ +%lu %lu %lu %lu %lu %lu %lu %lu %d %d %lu %lu %llu\n", + task->pid, + tcomm, + state, + ppid, + pgid, + sid, + tty_nr, + tty_pgrp, + task->flags, + min_flt, + cmin_flt, + maj_flt, + cmaj_flt, + cputime_to_clock_t(utime), + cputime_to_clock_t(stime), + cputime_to_clock_t(cutime), + cputime_to_clock_t(cstime), + priority, + nice, + num_threads, + // 0, + start_time, + vsize, + mm ? get_mm_rss(mm) : 0, + rsslim, + mm ? mm->start_code : 0, + mm ? mm->end_code : 0, + mm ? mm->start_stack : 0, + esp, + eip, +the rest is some obsolete cruft */ -- cgit v1.2.3-54-g00ecf