- be C99 friendly. Anonymous unions are a GNU extension. This change is
[oweals/busybox.git] / libbb / procps.c
index e04ff34ff20a95cda224899c20377c8be80dede3..015ad80efa7c0f2e368c08d009dacc3645d57ce3 100644 (file)
@@ -5,7 +5,7 @@
  * Copyright 1998 by Albert Cahalan; all rights reserved.
  * Copyright (C) 2002 by Vladimir Oleynik <dzo@simtreas.ru>
  * SELinux support: (c) 2007 by Yuichi Nakamura <ynakam@hitachisoft.jp>
- * 
+ *
  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  */
 
@@ -52,7 +52,7 @@ static int get_cached(cache_t *cp, unsigned id)
 }
 #endif
 
-typedef char* ug_func(char *name, long uid, int bufsize);
+typedef char* ug_func(char *name, int bufsize, long uid);
 static char* get_cached(cache_t *cp, unsigned id, ug_func* fp)
 {
        int i;
@@ -62,7 +62,8 @@ static char* get_cached(cache_t *cp, unsigned id, ug_func* fp)
        i = cp->size++;
        cp->cache = xrealloc(cp->cache, cp->size * sizeof(*cp->cache));
        cp->cache[i].id = id;
-       fp(cp->cache[i].name, id, sizeof(cp->cache[i].name));
+       /* Never fails. Generates numeric string if name isn't found */
+       fp(cp->cache[i].name, sizeof(cp->cache[i].name), id);
        return cp->cache[i].name;
 }
 const char* get_cached_username(uid_t uid)
@@ -92,26 +93,56 @@ static int read_to_buf(const char *filename, void *buf)
        return ret;
 }
 
-procps_status_talloc_procps_scan(int flags)
+procps_status_t *alloc_procps_scan(int flags)
 {
+       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 free_procps_scan(procps_status_t* sp)
 {
        closedir(sp->dir);
-       free(sp->cmd);
+       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 *str, char **endptr)
+static unsigned long fast_strtoul_10(char **endptr)
 {
        char c;
+       char *str = *endptr;
        unsigned long n = *str - '0';
 
        while ((c = *++str) != ' ')
@@ -123,14 +154,16 @@ static unsigned long fast_strtoul_10(char *str, char **endptr)
 static char *skip_fields(char *str, int count)
 {
        do {
-               str = skip_non_whitespace(str); str++;
+               while (*str++ != ' ')
+                       continue;
+               /* we found a space char, str points after it */
        } while (--count);
        return str;
 }
 #endif
 
 void BUG_comm_size(void);
-procps_status_tprocps_scan(procps_status_t* sp, int flags)
+procps_status_t *procps_scan(procps_status_t* sp, int flags)
 {
        struct dirent *entry;
        char buf[PROCPS_BUFSIZE];
@@ -167,8 +200,8 @@ procps_status_t* procps_scan(procps_status_t* sp, int flags)
                if (flags & PSSCAN_CONTEXT) {
                        if (getpidcon(sp->pid, &sp->context) < 0)
                                sp->context = NULL;
-               }       
-#endif 
+               }
+#endif
 
                filename_tail = filename + sprintf(filename, "/proc/%d", pid);
 
@@ -181,11 +214,11 @@ procps_status_t* procps_scan(procps_status_t* sp, int flags)
                }
 
                if (flags & PSSCAN_STAT) {
-                       char *cp;
+                       char *cp, *comm1;
+                       int tty;
 #if !ENABLE_FEATURE_FAST_TOP
                        unsigned long vsz, rss;
 #endif
-                       int tty;
 
                        /* see proc(5) for some details on this */
                        strcpy(filename_tail, "/stat");
@@ -193,12 +226,14 @@ procps_status_t* procps_scan(procps_status_t* sp, int flags)
                        if (n < 0)
                                break;
                        cp = strrchr(buf, ')'); /* split into "PID (cmd" and "<rest>" */
-                       if (!cp || cp[1] != ' ')
-                               break;
+                       /*if (!cp || cp[1] != ' ')
+                               break;*/
                        cp[0] = '\0';
                        if (sizeof(sp->comm) < 16)
                                BUG_comm_size();
-                       sscanf(buf, "%*s (%15c", sp->comm);
+                       comm1 = strchr(buf, '(');
+                       /*if (comm1)*/
+                               safe_strncpy(sp->comm, comm1 + 1, sizeof(sp->comm));
 
 #if !ENABLE_FEATURE_FAST_TOP
                        n = sscanf(cp+2,
@@ -208,7 +243,8 @@ procps_status_t* procps_scan(procps_status_t* sp, int flags)
                                "%lu %lu "             /* utime, stime */
                                "%*s %*s %*s "         /* cutime, cstime, priority */
                                "%ld "                 /* nice */
-                               "%*s %*s %*s "         /* timeout, it_real_value, start_time */
+                               "%*s %*s "             /* timeout, it_real_value */
+                               "%lu "                 /* start_time */
                                "%lu "                 /* vsize */
                                "%lu "                 /* rss */
                        /*      "%lu %lu %lu %lu %lu %lu " rss_rlim, start_code, end_code, start_stack, kstk_esp, kstk_eip */
@@ -219,44 +255,39 @@ procps_status_t* procps_scan(procps_status_t* sp, int flags)
                                &sp->pgid, &sp->sid, &tty,
                                &sp->utime, &sp->stime,
                                &tasknice,
+                               &sp->start_time,
                                &vsz,
                                &rss);
                        if (n != 10)
                                break;
-                       sp->vsz = vsz >> 10; /* vsize is in bytes and we want kb */
-                       sp->rss = rss >> 10;
-
-                       sp->tty_str[0] = '?';
-                       /* sp->tty_str[1] = '\0'; - done by memset */
-                       if (tty) /* tty field of "0" means "no tty" */
-                               snprintf(sp->tty_str, sizeof(sp->tty_str), "%u,%u",
-                                       (tty >> 8) & 0xfff, /* major */
-                                       (tty & 0xff) | ((tty >> 12) & 0xfff00));
+                       /* 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
 /* This costs ~100 bytes more but makes top faster by 20%
  * If you run 10000 processes, this may be important for you */
-                       cp += 2;
-                       sp->state[0] = *cp++; cp++;
-                       sp->ppid = fast_strtoul_10(cp, &cp);
-                       sp->pgid = fast_strtoul_10(cp, &cp);
-                       sp->sid = fast_strtoul_10(cp, &cp);
-                       sp->tty_str[0] = '?';
-                       /* sp->tty_str[1] = '\0'; - done by memset */
-                       tty = fast_strtoul_10(cp, &cp);
-                       if (tty && (flags & PSSCAN_TTY)) {
-                               /* tty field of "0" means "no tty" */
-                               snprintf(sp->tty_str, sizeof(sp->tty_str), "%u,%u",
-                                       (tty >> 8) & 0xfff, /* major */
-                                       (tty & 0xff) | ((tty >> 12) & 0xfff00));
-                       }
+                       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, &cp);
-                       sp->stime = fast_strtoul_10(cp, &cp);
+                       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);
-                       cp = skip_fields(cp, 3); /* timeout, it_real_value, start_time */
-                       sp->vsz = fast_strtoul_10(cp, &cp) >> 10; /* vsize is in bytes and we want kb */
-                       sp->rss = fast_strtoul_10(cp, &cp) >> 10;
+                       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;
 #endif
 
                        if (sp->vsz == 0 && sp->state[0] != 'Z')
@@ -272,29 +303,122 @@ procps_status_t* procps_scan(procps_status_t* sp, int flags)
 
                }
 
-               if (flags & PSSCAN_CMD) {
+#if ENABLE_FEATURE_TOPMEM
+               if (flags & (PSSCAN_SMAPS)) {
+                       FILE *file;
+
+                       strcpy(filename_tail, "/smaps");
+                       file = fopen(filename, "r");
+                       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;
+                               }
+                       }
+                       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 (buf[n-1] == '\n') {
-                               if (!--n)
-                                       break;
-                               buf[n] = '\0';
+#if ENABLE_PGREP || ENABLE_PKILL
+                       if (flags & PSSCAN_ARGVN) {
+                               do {
+                                       n--;
+                                       if (buf[n] == '\0')
+                                               buf[n] = ' ';
+                               } while (n);
                        }
-                       do {
-                               n--;
-                               if ((unsigned char)(buf[n]) < ' ')
-                                       buf[n] = ' ';
-                       } while (n);
-                       sp->cmd = xstrdup(buf);
+#endif
+                       sp->argv0 = xstrdup(buf);
                }
+#endif
                break;
        }
        return sp;
 }
+
+void 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 \