tar: support -T - and -X -
[oweals/busybox.git] / libbb / procps.c
index 48e60a7929872801139b0997e3c477f1744f1ee7..fb4c320015b3ca010f743fa1fea2f62628d0d0de 100644 (file)
@@ -6,7 +6,7 @@
  * 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.
+ * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  */
 
 #include "libbb.h"
@@ -120,7 +120,7 @@ void FAST_FUNC free_procps_scan(procps_status_t* sp)
        free(sp);
 }
 
-#if ENABLE_FEATURE_TOPMEM
+#if ENABLE_FEATURE_TOPMEM || ENABLE_PMAP
 static unsigned long fast_strtoul_16(char **endptr)
 {
        unsigned char c;
@@ -137,12 +137,9 @@ static unsigned long fast_strtoul_16(char **endptr)
        *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
+#if ENABLE_FEATURE_FAST_TOP || ENABLE_FEATURE_TOPMEM || ENABLE_PMAP
 /* We cut a lot of corners here for speed */
 static unsigned long fast_strtoul_10(char **endptr)
 {
@@ -157,6 +154,7 @@ static unsigned long fast_strtoul_10(char **endptr)
        return n;
 }
 
+# if ENABLE_FEATURE_FAST_TOP
 static long fast_strtol_10(char **endptr)
 {
        if (**endptr != '-')
@@ -165,6 +163,7 @@ static long fast_strtol_10(char **endptr)
        (*endptr)++;
        return - (long)fast_strtoul_10(endptr);
 }
+# endif
 
 static char *skip_fields(char *str, int count)
 {
@@ -177,6 +176,111 @@ static char *skip_fields(char *str, int count)
 }
 #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)) {
+               // Each mapping datum has this form:
+               // f7d29000-f7d39000 rw-s ADR M:m OFS FILE
+               // Size:                nnn kB
+               // Rss:                 nnn kB
+               // .....
+
+               char *tp = buf, *p;
+
+#define SCAN(S, X) \
+               if (strncmp(tp, S, sizeof(S)-1) == 0) {              \
+                       tp = skip_whitespace(tp + sizeof(S)-1);      \
+                       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) {
+                       // We reached next mapping - the line of this form:
+                       // f7d29000-f7d39000 rw-s ADR M:m OFS FILE
+
+                       if (cb) {
+                               /* If we have a previous record, there's nothing more
+                                * for it, call the callback and clear currec
+                                */
+                               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);
+
+                       // 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 (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
+
 void BUG_comm_size(void);
 procps_status_t* FAST_FUNC procps_scan(procps_status_t* sp, int flags)
 {
@@ -200,6 +304,7 @@ procps_status_t* FAST_FUNC procps_scan(procps_status_t* sp, int flags)
                                goto got_entry;
                        closedir(sp->task_dir);
                        sp->task_dir = NULL;
+                       sp->main_thread_pid = 0;
                }
 #endif
                entry = readdir(sp->dir);
@@ -219,6 +324,7 @@ procps_status_t* FAST_FUNC procps_scan(procps_status_t* sp, int flags)
                        char task_dir[sizeof("/proc/%u/task") + sizeof(int)*3];
                        sprintf(task_dir, "/proc/%u/task", pid);
                        sp->task_dir = xopendir(task_dir);
+                       sp->main_thread_pid = pid;
                        continue;
                }
 #endif
@@ -346,7 +452,7 @@ procps_status_t* FAST_FUNC procps_scan(procps_status_t* sp, int flags)
 //FIXME: is it safe to assume this field exists?
                        sp->last_seen_on_cpu = fast_strtoul_10(&cp);
 # endif
-#endif /* end of !ENABLE_FEATURE_TOP_SMP_PROCESS */
+#endif /* FEATURE_FAST_TOP */
 
 #if ENABLE_FEATURE_PS_ADDITIONAL_COLUMNS
                        sp->niceness = tasknice;
@@ -365,54 +471,8 @@ procps_status_t* FAST_FUNC procps_scan(procps_status_t* sp, int flags)
                }
 
 #if ENABLE_FEATURE_TOPMEM
-               if (flags & (PSSCAN_SMAPS)) {
-                       FILE *file;
-
-                       strcpy(filename_tail, "smaps");
-                       file = fopen_for_read(filename);
-                       if (file) {
-                               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);
-                       }
-               }
+               if (flags & PSSCAN_SMAPS)
+                       procps_read_smaps(pid, &sp->smaps, NULL, NULL);
 #endif /* TOPMEM */
 #if ENABLE_FEATURE_PS_ADDITIONAL_COLUMNS
                if (flags & PSSCAN_RUIDGID) {