123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660 |
- #include "libbb.h"
- typedef struct id_to_name_map_t {
- uid_t id;
- char name[USERNAME_MAX_SIZE];
- } id_to_name_map_t;
- typedef struct cache_t {
- id_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
- static int get_cached(cache_t *cp, uid_t 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, uid_t id,
- char* FAST_FUNC x2x_utoa(uid_t 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;
-
- 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;
-
- 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);
- #if ENABLE_FEATURE_SHOW_THREADS
- if (sp->task_dir)
- closedir(sp->task_dir);
- #endif
- free(sp->argv0);
- free(sp->exe);
- IF_SELINUX(free(sp->context);)
- free(sp);
- }
- #if ENABLE_FEATURE_TOPMEM || ENABLE_PMAP
- 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 - ('a' - '0' - 10);
- n = n*16 + c;
- }
- *endptr = str;
- return n;
- }
- #endif
- #if ENABLE_FEATURE_FAST_TOP || ENABLE_FEATURE_TOPMEM || ENABLE_PMAP
- static unsigned long fast_strtoul_10(char **endptr)
- {
- unsigned char c;
- char *str = *endptr;
- unsigned long n = *str - '0';
-
- while ((c = *++str) > ' ')
- n = n*10 + (c - '0');
- *endptr = str + 1;
- return n;
- }
- # if ENABLE_FEATURE_FAST_TOP
- static long fast_strtol_10(char **endptr)
- {
- if (**endptr != '-')
- return fast_strtoul_10(endptr);
- (*endptr)++;
- return - (long)fast_strtoul_10(endptr);
- }
- # endif
- static char *skip_fields(char *str, int count)
- {
- do {
- while (*str++ != ' ')
- continue;
-
- } while (--count);
- return str;
- }
- #endif
- #if ENABLE_FEATURE_TOPMEM || ENABLE_PMAP
- int FAST_FUNC procps_read_smaps(pid_t pid, struct smaprec *total,
- void (*cb)(struct smaprec *, void *), void *data)
- {
- FILE *file;
- struct smaprec currec;
- char filename[sizeof("/proc/%u/smaps") + sizeof(int)*3];
- char buf[PROCPS_BUFSIZE];
- #if !ENABLE_PMAP
- void (*cb)(struct smaprec *, void *) = NULL;
- void *data = NULL;
- #endif
- sprintf(filename, "/proc/%u/smaps", (int)pid);
- file = fopen_for_read(filename);
- if (!file)
- return 1;
- memset(&currec, 0, sizeof(currec));
- while (fgets(buf, PROCPS_BUFSIZE, file)) {
-
-
-
-
-
- char *tp, *p;
- #define SCAN(S, X) \
- if ((tp = is_prefixed_with(buf, S)) != NULL) { \
- tp = skip_whitespace(tp); \
- total->X += currec.X = fast_strtoul_10(&tp); \
- continue; \
- }
- if (cb) {
- SCAN("Pss:" , smap_pss );
- SCAN("Swap:" , smap_swap );
- }
- SCAN("Private_Dirty:", private_dirty);
- SCAN("Private_Clean:", private_clean);
- SCAN("Shared_Dirty:" , shared_dirty );
- SCAN("Shared_Clean:" , shared_clean );
- #undef SCAN
- tp = strchr(buf, '-');
- if (tp) {
-
-
- if (cb) {
-
- if (currec.smap_size)
- cb(&currec, data);
- free(currec.smap_name);
- }
- memset(&currec, 0, sizeof(currec));
- *tp = ' ';
- tp = buf;
- currec.smap_start = fast_strtoul_16(&tp);
- currec.smap_size = (fast_strtoul_16(&tp) - currec.smap_start) >> 10;
- strncpy(currec.smap_mode, tp, sizeof(currec.smap_mode)-1);
-
- tp = skip_whitespace(skip_fields(tp, 4));
-
- if (!is_prefixed_with(tp, "/dev/") || strcmp(tp, "/dev/zero\n") == 0) {
- if (currec.smap_mode[1] == 'w') {
- currec.mapped_rw = currec.smap_size;
- total->mapped_rw += currec.smap_size;
- } else if (currec.smap_mode[1] == '-') {
- currec.mapped_ro = currec.smap_size;
- total->mapped_ro += currec.smap_size;
- }
- }
- if (strcmp(tp, "[stack]\n") == 0)
- total->stack += currec.smap_size;
- if (cb) {
- p = skip_non_whitespace(tp);
- if (p == tp) {
- currec.smap_name = xstrdup(" [ anon ]");
- } else {
- *p = '\0';
- currec.smap_name = xstrdup(tp);
- }
- }
- total->smap_size += currec.smap_size;
- }
- }
- fclose(file);
- if (cb) {
- if (currec.smap_size)
- cb(&currec, data);
- free(currec.smap_name);
- }
- return 0;
- }
- #endif
- procps_status_t* FAST_FUNC procps_scan(procps_status_t* sp, int flags)
- {
- if (!sp)
- sp = alloc_procps_scan();
- for (;;) {
- struct dirent *entry;
- char buf[PROCPS_BUFSIZE];
- long tasknice;
- unsigned pid;
- int n;
- char filename[sizeof("/proc/%u/task/%u/cmdline") + sizeof(int)*3 * 2];
- char *filename_tail;
- #if ENABLE_FEATURE_SHOW_THREADS
- if (sp->task_dir) {
- entry = readdir(sp->task_dir);
- if (entry)
- goto got_entry;
- closedir(sp->task_dir);
- sp->task_dir = NULL;
- }
- #endif
- entry = readdir(sp->dir);
- if (entry == NULL) {
- free_procps_scan(sp);
- return NULL;
- }
- IF_FEATURE_SHOW_THREADS(got_entry:)
- pid = bb_strtou(entry->d_name, NULL, 10);
- if (errno)
- continue;
- #if ENABLE_FEATURE_SHOW_THREADS
- if ((flags & PSSCAN_TASKS) && !sp->task_dir) {
-
- sprintf(filename, "/proc/%u/task", pid);
-
- sp->task_dir = opendir(filename);
- sp->main_thread_pid = pid;
- continue;
- }
- #endif
-
- 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;
- }
- #endif
- #if ENABLE_FEATURE_SHOW_THREADS
- if (sp->task_dir)
- filename_tail = filename + sprintf(filename, "/proc/%u/task/%u/", sp->main_thread_pid, pid);
- else
- #endif
- filename_tail = filename + sprintf(filename, "/proc/%u/", pid);
- if (flags & PSSCAN_UIDGID) {
- struct stat sb;
- if (stat(filename, &sb))
- continue;
-
- sp->uid = sb.st_uid;
- sp->gid = sb.st_gid;
- }
-
- if (flags & (PSSCAN_PPID | PSSCAN_PGID | PSSCAN_SID
- | PSSCAN_COMM | PSSCAN_STATE
- | PSSCAN_VSZ | PSSCAN_RSS
- | PSSCAN_STIME | PSSCAN_UTIME | PSSCAN_START_TIME
- | PSSCAN_TTY | PSSCAN_NICE
- | PSSCAN_CPU)
- ) {
- int s_idx;
- char *cp, *comm1;
- int tty;
- #if !ENABLE_FEATURE_FAST_TOP
- unsigned long vsz, rss;
- #endif
-
- strcpy(filename_tail, "stat");
- n = read_to_buf(filename, buf);
- if (n < 0)
- continue;
- cp = strrchr(buf, ')');
-
- cp[0] = '\0';
- BUILD_BUG_ON(sizeof(sp->comm) < 16);
- comm1 = strchr(buf, '(');
-
- safe_strncpy(sp->comm, comm1 + 1, sizeof(sp->comm));
- #if !ENABLE_FEATURE_FAST_TOP
- n = sscanf(cp+2,
- "%c %u "
- "%u %u %d %*s "
- "%*s %*s %*s %*s %*s "
- "%lu %lu "
- "%*s %*s %*s "
- "%ld "
- "%*s %*s "
- "%lu "
- "%lu "
- "%lu "
- # if ENABLE_FEATURE_TOP_SMP_PROCESS
- "%*s %*s %*s %*s %*s %*s "
- "%*s %*s %*s %*s "
- "%*s %*s %*s %*s "
- "%d"
- # 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
- );
- if (n < 11)
- continue;
- # if ENABLE_FEATURE_TOP_SMP_PROCESS
- if (n == 11)
- sp->last_seen_on_cpu = 0;
- # endif
-
- sp->vsz = vsz >> 10;
-
- sp->rss = rss << sp->shift_pages_to_kb;
- sp->tty_major = (tty >> 8) & 0xfff;
- sp->tty_minor = (tty & 0xff) | ((tty >> 12) & 0xfff00);
- #else
- 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);
- sp->utime = fast_strtoul_10(&cp);
- sp->stime = fast_strtoul_10(&cp);
- cp = skip_fields(cp, 3);
- tasknice = fast_strtol_10(&cp);
- cp = skip_fields(cp, 2);
- sp->start_time = fast_strtoul_10(&cp);
-
- sp->vsz = fast_strtoul_10(&cp) >> 10;
-
- sp->rss = fast_strtoul_10(&cp) << sp->shift_pages_to_kb;
- # if ENABLE_FEATURE_TOP_SMP_PROCESS
-
-
-
- cp = skip_fields(cp, 14);
- sp->last_seen_on_cpu = fast_strtoul_10(&cp);
- # endif
- #endif
- #if ENABLE_FEATURE_PS_ADDITIONAL_COLUMNS
- sp->niceness = tasknice;
- #endif
- sp->state[1] = ' ';
- sp->state[2] = ' ';
- s_idx = 1;
- if (sp->vsz == 0 && sp->state[0] != 'Z') {
-
- sp->state[1] = 'W';
- s_idx = 2;
- }
- if (tasknice != 0) {
- if (tasknice < 0)
- sp->state[s_idx] = '<';
- else
- sp->state[s_idx] = 'N';
- }
- }
- #if ENABLE_FEATURE_TOPMEM
- if (flags & PSSCAN_SMAPS)
- procps_read_smaps(pid, &sp->smaps, NULL, NULL);
- #endif
- #if ENABLE_FEATURE_PS_ADDITIONAL_COLUMNS
- if (flags & PSSCAN_RUIDGID) {
- FILE *file;
- strcpy(filename_tail, "status");
- file = fopen_for_read(filename);
- if (file) {
- while (fgets(buf, sizeof(buf), file)) {
- char *tp;
- #define SCAN_TWO(str, name, statement) \
- if ((tp = is_prefixed_with(buf, str)) != NULL) { \
- tp = skip_whitespace(tp); \
- sscanf(tp, "%u", &sp->name); \
- statement; \
- }
- SCAN_TWO("Uid:", ruid, continue);
- SCAN_TWO("Gid:", rgid, break);
- #undef SCAN_TWO
- }
- fclose(file);
- }
- }
- #endif
- if (flags & PSSCAN_EXE) {
- strcpy(filename_tail, "exe");
- free(sp->exe);
- sp->exe = xmalloc_readlink(filename);
- }
-
- #if 0
- if (flags & (PSSCAN_CMD|PSSCAN_ARGV0)) {
- free(sp->argv0);
- sp->argv0 = NULL;
- free(sp->cmd);
- sp->cmd = NULL;
- strcpy(filename_tail, "cmdline");
-
- 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 = xmemdup(buf, n + 1);
-
- } else {
- sp->argv_len = 0;
- sp->argv0 = xstrdup(buf);
- }
- }
- #endif
- break;
- }
- return sp;
- }
- void FAST_FUNC read_cmdline(char *buf, int col, unsigned pid, const char *comm)
- {
- int sz;
- char filename[sizeof("/proc/%u/cmdline") + sizeof(int)*3];
- sprintf(filename, "/proc/%u/cmdline", pid);
- sz = open_read_close(filename, buf, col - 1);
- if (sz > 0) {
- const char *base;
- int comm_len;
- buf[sz] = '\0';
- while (--sz >= 0 && buf[sz] == '\0')
- continue;
-
- strchrnul(buf, ' ')[0] = '\0';
- base = bb_basename(buf);
- while (sz >= 0) {
- if ((unsigned char)(buf[sz]) < ' ')
- buf[sz] = ' ';
- sz--;
- }
- if (base[0] == '-')
- base++;
-
- if (!comm)
- return;
- comm_len = strlen(comm);
-
- if (strncmp(base, comm, comm_len) != 0) {
- comm_len += 3;
- if (col > comm_len)
- memmove(buf + comm_len, buf, col - comm_len);
- snprintf(buf, col, "{%s}", comm);
- if (col <= comm_len)
- return;
- buf[comm_len - 1] = ' ';
- buf[col - 1] = '\0';
- }
- } else {
- snprintf(buf, col, "[%s]", comm ? comm : "?");
- }
- }
|