Changeset 3232 in MondoRescue for branches/3.2/mindi-busybox/libbb/procps.c


Ignore:
Timestamp:
Jan 1, 2014, 12:47:38 AM (10 years ago)
Author:
Bruno Cornec
Message:
  • Update mindi-busybox to 1.21.1
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/3.2/mindi-busybox/libbb/procps.c

    r2725 r3232  
    1313
    1414
    15 typedef struct unsigned_to_name_map_t {
    16     long id;
     15typedef struct id_to_name_map_t {
     16    uid_t id;
    1717    char name[USERNAME_MAX_SIZE];
    18 } unsigned_to_name_map_t;
     18} id_to_name_map_t;
    1919
    2020typedef struct cache_t {
    21     unsigned_to_name_map_t *cache;
     21    id_to_name_map_t *cache;
    2222    int size;
    2323} cache_t;
     
    4040/* Returns -N-1 if not found. */
    4141/* cp->cache[N] is allocated and must be filled in this case */
    42 static int get_cached(cache_t *cp, unsigned id)
     42static int get_cached(cache_t *cp, uid_t id)
    4343{
    4444    int i;
     
    5353#endif
    5454
    55 static char* get_cached(cache_t *cp, long id,
    56             char* FAST_FUNC x2x_utoa(long id))
     55static char* get_cached(cache_t *cp, uid_t id,
     56            char* FAST_FUNC x2x_utoa(uid_t id))
    5757{
    5858    int i;
     
    128128    unsigned long n = 0;
    129129
    130     while ((c = *str++) != ' ') {
     130    /* Need to stop on both ' ' and '\n' */
     131    while ((c = *str++) > ' ') {
    131132        c = ((c|0x20) - '0');
    132133        if (c > 9)
    133             // c = c + '0' - 'a' + 10:
     134            /* c = c + '0' - 'a' + 10: */
    134135            c = c - ('a' - '0' - 10);
    135136        n = n*16 + c;
     
    144145static unsigned long fast_strtoul_10(char **endptr)
    145146{
    146     char c;
     147    unsigned char c;
    147148    char *str = *endptr;
    148149    unsigned long n = *str - '0';
    149150
    150     while ((c = *++str) != ' ')
     151    /* Need to stop on both ' ' and '\n' */
     152    while ((c = *++str) > ' ')
    151153        n = n*10 + (c - '0');
    152154
     
    179181#if ENABLE_FEATURE_TOPMEM || ENABLE_PMAP
    180182int FAST_FUNC procps_read_smaps(pid_t pid, struct smaprec *total,
    181               void (*cb)(struct smaprec *, void *), void *data)
     183        void (*cb)(struct smaprec *, void *), void *data)
    182184{
    183185    FILE *file;
     
    199201    while (fgets(buf, PROCPS_BUFSIZE, file)) {
    200202        // Each mapping datum has this form:
    201         // f7d29000-f7d39000 rw-s ADR M:m OFS FILE
     203        // f7d29000-f7d39000 rw-s FILEOFS M:m INODE FILENAME
    202204        // Size:                nnn kB
    203205        // Rss:                 nnn kB
     
    224226        if (tp) {
    225227            // We reached next mapping - the line of this form:
    226             // f7d29000-f7d39000 rw-s ADR M:m OFS FILE
     228            // f7d29000-f7d39000 rw-s FILEOFS M:m INODE FILENAME
    227229
    228230            if (cb) {
     
    243245            strncpy(currec.smap_mode, tp, sizeof(currec.smap_mode)-1);
    244246
    245             // skipping "rw-s ADR M:m OFS "
     247            // skipping "rw-s FILEOFS M:m INODE "
    246248            tp = skip_whitespace(skip_fields(tp, 4));
    247249            // filter out /dev/something (something != zero)
     
    285287procps_status_t* FAST_FUNC procps_scan(procps_status_t* sp, int flags)
    286288{
    287     struct dirent *entry;
    288     char buf[PROCPS_BUFSIZE];
    289     char filename[sizeof("/proc//cmdline") + sizeof(int)*3];
    290     char *filename_tail;
    291     long tasknice;
    292     unsigned pid;
    293     int n;
    294     struct stat sb;
    295 
    296289    if (!sp)
    297290        sp = alloc_procps_scan();
    298291
    299292    for (;;) {
     293        struct dirent *entry;
     294        char buf[PROCPS_BUFSIZE];
     295        long tasknice;
     296        unsigned pid;
     297        int n;
     298        char filename[sizeof("/proc/%u/task/%u/cmdline") + sizeof(int)*3 * 2];
     299        char *filename_tail;
     300
    300301#if ENABLE_FEATURE_SHOW_THREADS
    301         if ((flags & PSSCAN_TASKS) && sp->task_dir) {
     302        if (sp->task_dir) {
    302303            entry = readdir(sp->task_dir);
    303304            if (entry)
     
    321322             * there will be /proc/PID/task/PID (same PID!),
    322323             * so just go ahead and dive into /proc/PID/task. */
    323             char task_dir[sizeof("/proc/%u/task") + sizeof(int)*3];
    324             sprintf(task_dir, "/proc/%u/task", pid);
    325             sp->task_dir = xopendir(task_dir);
     324            sprintf(filename, "/proc/%u/task", pid);
     325            /* Note: if opendir fails, we just go to next /proc/XXX */
     326            sp->task_dir = opendir(filename);
     327            sp->main_thread_pid = pid;
    326328            continue;
    327329        }
     
    346348#endif
    347349
    348         filename_tail = filename + sprintf(filename, "/proc/%u/", pid);
     350#if ENABLE_FEATURE_SHOW_THREADS
     351        if (sp->task_dir)
     352            filename_tail = filename + sprintf(filename, "/proc/%u/task/%u/", sp->main_thread_pid, pid);
     353        else
     354#endif
     355            filename_tail = filename + sprintf(filename, "/proc/%u/", pid);
    349356
    350357        if (flags & PSSCAN_UIDGID) {
     358            struct stat sb;
    351359            if (stat(filename, &sb))
    352360                continue; /* process probably exited */
     
    356364        }
    357365
    358         if (flags & PSSCAN_STAT) {
     366        /* These are all retrieved from proc/NN/stat in one go: */
     367        if (flags & (PSSCAN_PPID | PSSCAN_PGID | PSSCAN_SID
     368            | PSSCAN_COMM | PSSCAN_STATE
     369            | PSSCAN_VSZ | PSSCAN_RSS
     370            | PSSCAN_STIME | PSSCAN_UTIME | PSSCAN_START_TIME
     371            | PSSCAN_TTY | PSSCAN_NICE
     372            | PSSCAN_CPU)
     373        ) {
    359374            char *cp, *comm1;
    360375            int tty;
     
    411426                continue; /* bogus data, get next /proc/XXX */
    412427# if ENABLE_FEATURE_TOP_SMP_PROCESS
    413             if (n < 11+15)
     428            if (n == 11)
    414429                sp->last_seen_on_cpu = 0;
    415430# endif
     
    558573{
    559574    int sz;
    560     char filename[sizeof("/proc//cmdline") + sizeof(int)*3];
     575    char filename[sizeof("/proc/%u/cmdline") + sizeof(int)*3];
    561576
    562577    sprintf(filename, "/proc/%u/cmdline", pid);
    563578    sz = open_read_close(filename, buf, col - 1);
    564579    if (sz > 0) {
     580        const char *base;
     581        int comm_len;
     582
    565583        buf[sz] = '\0';
    566584        while (--sz >= 0 && buf[sz] == '\0')
    567585            continue;
    568         do {
     586        /* Prevent basename("process foo/bar") = "bar" */
     587        strchrnul(buf, ' ')[0] = '\0';
     588        base = bb_basename(buf); /* before we replace argv0's NUL with space */
     589        while (sz >= 0) {
    569590            if ((unsigned char)(buf[sz]) < ' ')
    570591                buf[sz] = ' ';
    571         } while (--sz >= 0);
     592            sz--;
     593        }
     594
     595        /* If comm differs from argv0, prepend "{comm} ".
     596         * It allows to see thread names set by prctl(PR_SET_NAME).
     597         */
     598        if (base[0] == '-') /* "-sh" (login shell)? */
     599            base++;
     600        comm_len = strlen(comm);
     601        /* Why compare up to comm_len, not COMM_LEN-1?
     602         * Well, some processes rewrite argv, and use _spaces_ there
     603         * while rewriting. (KDE is observed to do it).
     604         * I prefer to still treat argv0 "process foo bar"
     605         * as 'equal' to comm "process".
     606         */
     607        if (strncmp(base, comm, comm_len) != 0) {
     608            comm_len += 3;
     609            if (col > comm_len)
     610                memmove(buf + comm_len, buf, col - comm_len);
     611            snprintf(buf, col, "{%s}", comm);
     612            if (col <= comm_len)
     613                return;
     614            buf[comm_len - 1] = ' ';
     615            buf[col - 1] = '\0';
     616        }
     617
    572618    } else {
    573619        snprintf(buf, col, "[%s]", comm);
Note: See TracChangeset for help on using the changeset viewer.