mkfs.vfat: fix a problem with over-estimating FAT size
[oweals/busybox.git] / libbb / procps.c
index 015ad80efa7c0f2e368c08d009dacc3645d57ce3..c5e40bf885712681e7b8b3b1850bc27e3bba3c78 100644 (file)
@@ -13,7 +13,7 @@
 
 
 typedef struct unsigned_to_name_map_t {
-       unsigned id;
+       long id;
        char name[USERNAME_MAX_SIZE];
 } unsigned_to_name_map_t;
 
@@ -30,7 +30,7 @@ static void clear_cache(cache_t *cp)
        cp->cache = NULL;
        cp->size = 0;
 }
-void clear_username_cache(void)
+void FAST_FUNC clear_username_cache(void)
 {
        clear_cache(&username);
        clear_cache(&groupname);
@@ -46,33 +46,33 @@ static int get_cached(cache_t *cp, unsigned id)
                if (cp->cache[i].id == id)
                        return i;
        i = cp->size++;
-       cp->cache = xrealloc(cp->cache, cp->size * sizeof(*cp->cache));
+       cp->cache = xrealloc_vector(cp->cache, 2, i);
        cp->cache[i++].id = id;
        return -i;
 }
 #endif
 
-typedef char* ug_func(char *name, int bufsize, long uid);
-static char* get_cached(cache_t *cp, unsigned id, ug_func* fp)
+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(cp->cache, cp->size * sizeof(*cp->cache));
+       cp->cache = xrealloc_vector(cp->cache, 2, i);
        cp->cache[i].id = id;
        /* Never fails. Generates numeric string if name isn't found */
-       fp(cp->cache[i].name, sizeof(cp->cache[i].name), id);
+       safe_strncpy(cp->cache[i].name, x2x_utoa(id), sizeof(cp->cache[i].name));
        return cp->cache[i].name;
 }
-const char* get_cached_username(uid_t uid)
+const char* FAST_FUNC get_cached_username(uid_t uid)
 {
-       return get_cached(&username, uid, bb_getpwuid);
+       return get_cached(&username, uid, uid2uname_utoa);
 }
-const char* get_cached_groupname(gid_t gid)
+const char* FAST_FUNC get_cached_groupname(gid_t gid)
 {
-       return get_cached(&groupname, gid, bb_getgrgid);
+       return get_cached(&groupname, gid, gid2group_utoa);
 }
 
 
@@ -93,7 +93,7 @@ static int read_to_buf(const char *filename, void *buf)
        return ret;
 }
 
-procps_status_t *alloc_procps_scan(int flags)
+static procps_status_t* FAST_FUNC alloc_procps_scan(void)
 {
        unsigned n = getpagesize();
        procps_status_t* sp = xzalloc(sizeof(procps_status_t));
@@ -107,7 +107,7 @@ procps_status_t *alloc_procps_scan(int flags)
        return sp;
 }
 
-void free_procps_scan(procps_status_t* sp)
+void FAST_FUNC free_procps_scan(procps_status_t* sp)
 {
        closedir(sp->dir);
        free(sp->argv0);
@@ -163,7 +163,7 @@ static char *skip_fields(char *str, int count)
 #endif
 
 void BUG_comm_size(void);
-procps_status_t *procps_scan(procps_status_t* sp, int flags)
+procps_status_t* FAST_FUNC procps_scan(procps_status_t* sp, int flags)
 {
        struct dirent *entry;
        char buf[PROCPS_BUFSIZE];
@@ -175,7 +175,7 @@ procps_status_t *procps_scan(procps_status_t* sp, int flags)
        struct stat sb;
 
        if (!sp)
-               sp = alloc_procps_scan(flags);
+               sp = alloc_procps_scan();
 
        for (;;) {
                entry = readdir(sp->dir);
@@ -219,7 +219,6 @@ procps_status_t *procps_scan(procps_status_t* sp, int flags)
 #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);
@@ -247,9 +246,12 @@ procps_status_t *procps_scan(procps_status_t* sp, int flags)
                                "%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 */
-                       /*      "%u %u %u %u "         signal, blocked, sigignore, sigcatch */
-                       /*      "%lu %lu %lu"          wchan, nswap, cnswap */
+#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,
@@ -257,9 +259,19 @@ procps_status_t *procps_scan(procps_status_t* sp, int flags)
                                &tasknice,
                                &sp->start_time,
                                &vsz,
-                               &rss);
-                       if (n != 10)
+                               &rss
+#if ENABLE_FEATURE_TOP_SMP_PROCESS
+                               , &sp->last_seen_on_cpu
+#endif
+                               );
+
+                       if (n < 11)
                                break;
+#if ENABLE_FEATURE_TOP_SMP_PROCESS
+                       if (n < 11+15)
+                               sp->last_seen_on_cpu = 0;
+#endif
+
                        /* 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? */
@@ -288,7 +300,15 @@ procps_status_t *procps_scan(procps_status_t* sp, int flags)
                        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
+#endif /* end of !ENABLE_FEATURE_TOP_SMP_PROCESS */
 
                        if (sp->vsz == 0 && sp->state[0] != 'Z')
                                sp->state[1] = 'W';
@@ -300,7 +320,6 @@ procps_status_t *procps_scan(procps_status_t* sp, int flags)
                                sp->state[2] = 'N';
                        else
                                sp->state[2] = ' ';
-
                }
 
 #if ENABLE_FEATURE_TOPMEM
@@ -308,7 +327,7 @@ procps_status_t *procps_scan(procps_status_t* sp, int flags)
                        FILE *file;
 
                        strcpy(filename_tail, "/smaps");
-                       file = fopen(filename, "r");
+                       file = fopen_for_read(filename);
                        if (!file)
                                break;
                        while (fgets(buf, sizeof(buf), file)) {
@@ -385,16 +404,15 @@ procps_status_t *procps_scan(procps_status_t* sp, int flags)
                        n = read_to_buf(filename, buf);
                        if (n <= 0)
                                break;
-#if ENABLE_PGREP || ENABLE_PKILL
                        if (flags & PSSCAN_ARGVN) {
-                               do {
-                                       n--;
-                                       if (buf[n] == '\0')
-                                               buf[n] = ' ';
-                               } while (n);
+                               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);
                        }
-#endif
-                       sp->argv0 = xstrdup(buf);
                }
 #endif
                break;
@@ -402,7 +420,7 @@ procps_status_t *procps_scan(procps_status_t* sp, int flags)
        return sp;
 }
 
-void read_cmdline(char *buf, int col, unsigned pid, const char *comm)
+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];