X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=libbb%2Fprocps.c;h=eba90705cc8b7bfbb4448caed79273f28f2f4ba1;hb=966ec7c067d7a2df5232a69c8d3d2e777347a62d;hp=10f2ff597e732e0a0c036619f3578dfd56d103a6;hpb=301ad6791257bc061753f30dba0412083c353281;p=oweals%2Fbusybox.git diff --git a/libbb/procps.c b/libbb/procps.c index 10f2ff597..eba90705c 100644 --- a/libbb/procps.c +++ b/libbb/procps.c @@ -22,14 +22,9 @@ static int read_to_buf(const char *filename, void *buf) { - int fd; ssize_t ret; - - fd = open(filename, O_RDONLY); - if(fd < 0) - return -1; - ret = read(fd, buf, PROCPS_BUFSIZE); - close(fd); + ret = open_read_close(filename, buf, PROCPS_BUFSIZE-1); + ((char *)buf)[ret > 0 ? ret : 0] = '\0'; return ret; } @@ -50,10 +45,11 @@ procps_status_t * procps_scan(int save_user_arg0) struct stat sb; if (!dir) { - dir = bb_xopendir("/proc"); + dir = xopendir("/proc"); } - for(;;) { - if((entry = readdir(dir)) == NULL) { + for (;;) { + entry = readdir(dir); + if (entry == NULL) { closedir(dir); dir = 0; return 0; @@ -67,17 +63,17 @@ procps_status_t * procps_scan(int save_user_arg0) curstatus.pid = pid; status_tail = status + sprintf(status, "/proc/%d", pid); - if(stat(status, &sb)) + if (stat(status, &sb)) continue; bb_getpwuid(curstatus.user, sb.st_uid, sizeof(curstatus.user)); /* see proc(5) for some details on this */ strcpy(status_tail, "/stat"); n = read_to_buf(status, buf); - if(n < 0) + if (n < 0) continue; name = strrchr(buf, ')'); /* split into "PID (cmd" and "" */ - if(name == 0 || name[1] != ' ') + if (name == 0 || name[1] != ' ') continue; *name = 0; sscanf(buf, "%*s (%15c", curstatus.short_cmd); @@ -86,15 +82,15 @@ procps_status_t * procps_scan(int save_user_arg0) "%*s %*s %*s %*s " /* pgrp, session, tty, tpgid */ "%*s %*s %*s %*s %*s " /* flags, min_flt, cmin_flt, maj_flt, cmaj_flt */ #ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE - "%lu %lu " + "%lu %lu " /* utime, stime */ #else - "%*s %*s " + "%*s %*s " /* utime, stime */ #endif "%*s %*s %*s " /* cutime, cstime, priority */ - "%ld " + "%ld " /* nice */ "%*s %*s %*s " /* timeout, it_real_value, start_time */ "%*s " /* vsize */ - "%ld", + "%ld", /* rss */ curstatus.state, &curstatus.ppid, #ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE &curstatus.utime, &curstatus.stime, @@ -102,9 +98,9 @@ procps_status_t * procps_scan(int save_user_arg0) &tasknice, &curstatus.rss); #ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE - if(n != 6) + if (n != 6) #else - if(n != 4) + if (n != 4) #endif continue; @@ -125,21 +121,21 @@ procps_status_t * procps_scan(int save_user_arg0) curstatus.rss *= (getpagesize() >> 10); /* 2**10 = 1kb */ #endif - if(save_user_arg0) { + if (save_user_arg0) { strcpy(status_tail, "/cmdline"); n = read_to_buf(status, buf); - if(n > 0) { - if(buf[n-1]=='\n') + if (n > 0) { + if (buf[n-1]=='\n') buf[--n] = 0; name = buf; - while(n) { - if(((unsigned char)*name) < ' ') + while (n) { + if (((unsigned char)*name) < ' ') *name = ' '; name++; n--; } *name = 0; - if(buf[0]) + if (buf[0]) curstatus.cmd = strdup(buf); /* if NULL it work true also */ }