Changeset 3232 in MondoRescue for branches/3.2/mindi-busybox/libbb/procps.c
- Timestamp:
- Jan 1, 2014, 12:47:38 AM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3.2/mindi-busybox/libbb/procps.c
r2725 r3232 13 13 14 14 15 typedef struct unsigned_to_name_map_t {16 longid;15 typedef struct id_to_name_map_t { 16 uid_t id; 17 17 char name[USERNAME_MAX_SIZE]; 18 } unsigned_to_name_map_t;18 } id_to_name_map_t; 19 19 20 20 typedef struct cache_t { 21 unsigned_to_name_map_t *cache;21 id_to_name_map_t *cache; 22 22 int size; 23 23 } cache_t; … … 40 40 /* Returns -N-1 if not found. */ 41 41 /* cp->cache[N] is allocated and must be filled in this case */ 42 static int get_cached(cache_t *cp, u nsignedid)42 static int get_cached(cache_t *cp, uid_t id) 43 43 { 44 44 int i; … … 53 53 #endif 54 54 55 static char* get_cached(cache_t *cp, longid,56 char* FAST_FUNC x2x_utoa( longid))55 static char* get_cached(cache_t *cp, uid_t id, 56 char* FAST_FUNC x2x_utoa(uid_t id)) 57 57 { 58 58 int i; … … 128 128 unsigned long n = 0; 129 129 130 while ((c = *str++) != ' ') { 130 /* Need to stop on both ' ' and '\n' */ 131 while ((c = *str++) > ' ') { 131 132 c = ((c|0x20) - '0'); 132 133 if (c > 9) 133 / / c = c + '0' - 'a' + 10:134 /* c = c + '0' - 'a' + 10: */ 134 135 c = c - ('a' - '0' - 10); 135 136 n = n*16 + c; … … 144 145 static unsigned long fast_strtoul_10(char **endptr) 145 146 { 146 char c;147 unsigned char c; 147 148 char *str = *endptr; 148 149 unsigned long n = *str - '0'; 149 150 150 while ((c = *++str) != ' ') 151 /* Need to stop on both ' ' and '\n' */ 152 while ((c = *++str) > ' ') 151 153 n = n*10 + (c - '0'); 152 154 … … 179 181 #if ENABLE_FEATURE_TOPMEM || ENABLE_PMAP 180 182 int FAST_FUNC procps_read_smaps(pid_t pid, struct smaprec *total, 181 183 void (*cb)(struct smaprec *, void *), void *data) 182 184 { 183 185 FILE *file; … … 199 201 while (fgets(buf, PROCPS_BUFSIZE, file)) { 200 202 // Each mapping datum has this form: 201 // f7d29000-f7d39000 rw-s ADR M:m OFS FILE203 // f7d29000-f7d39000 rw-s FILEOFS M:m INODE FILENAME 202 204 // Size: nnn kB 203 205 // Rss: nnn kB … … 224 226 if (tp) { 225 227 // We reached next mapping - the line of this form: 226 // f7d29000-f7d39000 rw-s ADR M:m OFS FILE228 // f7d29000-f7d39000 rw-s FILEOFS M:m INODE FILENAME 227 229 228 230 if (cb) { … … 243 245 strncpy(currec.smap_mode, tp, sizeof(currec.smap_mode)-1); 244 246 245 // skipping "rw-s ADR M:m OFS"247 // skipping "rw-s FILEOFS M:m INODE " 246 248 tp = skip_whitespace(skip_fields(tp, 4)); 247 249 // filter out /dev/something (something != zero) … … 285 287 procps_status_t* FAST_FUNC procps_scan(procps_status_t* sp, int flags) 286 288 { 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 296 289 if (!sp) 297 290 sp = alloc_procps_scan(); 298 291 299 292 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 300 301 #if ENABLE_FEATURE_SHOW_THREADS 301 if ( (flags & PSSCAN_TASKS) &&sp->task_dir) {302 if (sp->task_dir) { 302 303 entry = readdir(sp->task_dir); 303 304 if (entry) … … 321 322 * there will be /proc/PID/task/PID (same PID!), 322 323 * 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; 326 328 continue; 327 329 } … … 346 348 #endif 347 349 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); 349 356 350 357 if (flags & PSSCAN_UIDGID) { 358 struct stat sb; 351 359 if (stat(filename, &sb)) 352 360 continue; /* process probably exited */ … … 356 364 } 357 365 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 ) { 359 374 char *cp, *comm1; 360 375 int tty; … … 411 426 continue; /* bogus data, get next /proc/XXX */ 412 427 # if ENABLE_FEATURE_TOP_SMP_PROCESS 413 if (n < 11+15)428 if (n == 11) 414 429 sp->last_seen_on_cpu = 0; 415 430 # endif … … 558 573 { 559 574 int sz; 560 char filename[sizeof("/proc/ /cmdline") + sizeof(int)*3];575 char filename[sizeof("/proc/%u/cmdline") + sizeof(int)*3]; 561 576 562 577 sprintf(filename, "/proc/%u/cmdline", pid); 563 578 sz = open_read_close(filename, buf, col - 1); 564 579 if (sz > 0) { 580 const char *base; 581 int comm_len; 582 565 583 buf[sz] = '\0'; 566 584 while (--sz >= 0 && buf[sz] == '\0') 567 585 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) { 569 590 if ((unsigned char)(buf[sz]) < ' ') 570 591 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 572 618 } else { 573 619 snprintf(buf, col, "[%s]", comm);
Note:
See TracChangeset
for help on using the changeset viewer.