From c70005646894869d77fc95b0cee9181a90044506 Mon Sep 17 00:00:00 2001 From: marschap Date: Sun, 29 Oct 2006 18:10:31 +0000 Subject: [PATCH] cleanups --- clients/lcdproc/machine_Linux.c | 161 +++++++++++++------------------- 1 file changed, 64 insertions(+), 97 deletions(-) diff --git a/clients/lcdproc/machine_Linux.c b/clients/lcdproc/machine_Linux.c index f3cfb4c..5fb1077 100644 --- a/clients/lcdproc/machine_Linux.c +++ b/clients/lcdproc/machine_Linux.c @@ -45,7 +45,7 @@ #include "shared/LL.h" #define MAX_CPUS 8 -#define DEVFILE "/proc/net/dev" /* file to read network statistics from */ + static int batt_fd; static int load_fd; @@ -65,54 +65,45 @@ int machine_init() loadavg_fd = -1; meminfo_fd = -1; - if (uptime_fd == -1) - { + if (uptime_fd < 0) { uptime_fd = open("/proc/uptime", O_RDONLY); - if (uptime_fd < 0) - { + if (uptime_fd < 0) { perror("open /proc/uptime"); return(FALSE); } } - if (load_fd == -1) - { + if (load_fd < 0) { load_fd = open("/proc/stat", O_RDONLY); - if (load_fd < 0) - { + if (load_fd < 0) { perror("open /proc/stat"); return(FALSE); } } #ifndef USE_GETLOADAVG - if (loadavg_fd == -1) - { + if (loadavg_fd < 0) { loadavg_fd = open("/proc/loadavg", O_RDONLY); - if (loadavg_fd < 0) - { + if (loadavg_fd < 0) { perror("open /proc/loadavg"); return(FALSE); } } #endif - if (meminfo_fd == -1) - { + if (meminfo_fd < 0) { meminfo_fd = open("/proc/meminfo", O_RDONLY); - if (meminfo_fd < 0) - { + if (meminfo_fd < 0) { perror("open /proc/meminfo"); return(FALSE); } } - if (batt_fd == -1) - { + if (batt_fd < 0) { batt_fd = open("/proc/apm", O_RDONLY); - if (batt_fd < 0) - { + if (batt_fd < 0) { //perror("open /proc/apm"); + /* allow opening /proc/apm to fail */ batt_fd = -1; } } @@ -122,25 +113,25 @@ int machine_init() int machine_close() { - if (batt_fd != -1) + if (batt_fd >= 0) close(batt_fd); batt_fd = -1; - if (load_fd != -1) + if (load_fd >= 0) close(load_fd); load_fd = -1; #ifndef USE_GETLOADAVG - if (loadavg_fd != -1) + if (loadavg_fd >= 0) close(loadavg_fd); loadavg_fd = -1; #endif - if (meminfo_fd != -1) + if (meminfo_fd >= 0) close(meminfo_fd); meminfo_fd = -1; - if (uptime_fd != -1) + if (uptime_fd >= 0) close(uptime_fd); uptime_fd = -1; @@ -150,10 +141,10 @@ int machine_close() static void reread (int f, char *errmsg) { - if (lseek (f, 0L, 0) == 0 && read (f, procbuf, sizeof(procbuf) - 1) > 0) + if (lseek(f, 0L, 0) == 0 && read(f, procbuf, sizeof(procbuf) - 1) > 0) return; - perror (errmsg); - exit (1); + perror(errmsg); + exit(1); } int @@ -162,17 +153,17 @@ getentry (const char *tag, const char *bufptr) char *tail; int retval, len = strlen (tag); - while (bufptr) { + while (bufptr != NULL) { if (*bufptr == '\n') bufptr++; - if (!strncmp (tag, bufptr, len)) { - retval = strtol (bufptr + len, &tail, 10); + if (!strncmp(tag, bufptr, len)) { + retval = strtol(bufptr + len, &tail, 10); if (tail == bufptr + len) return -1; else return retval; } - bufptr = strchr (bufptr, '\n'); + bufptr = strchr(bufptr, '\n'); } return -1; } @@ -182,8 +173,8 @@ int machine_get_battstat(int *acstat, int *battflag, int *percent) char str[64]; int battstat; - if (batt_fd == -1) - { + /* no battery status available: fake one ;-) */ + if (batt_fd < 0) { *acstat = LCDP_AC_ON; *battflag = LCDP_BATT_ABSENT; *percent = 100; @@ -201,8 +192,7 @@ int machine_get_battstat(int *acstat, int *battflag, int *percent) if (*battflag == 0xff) *battflag = LCDP_BATT_UNKNOWN; - else - { + else { if (*battflag & 1) *battflag = LCDP_BATT_HIGH; if (*battflag & 2) @@ -215,8 +205,7 @@ int machine_get_battstat(int *acstat, int *battflag, int *percent) *battflag = LCDP_BATT_ABSENT; } - switch(*acstat) - { + switch(*acstat) { case 0: *acstat = LCDP_AC_OFF; break; @@ -247,17 +236,15 @@ int machine_get_fs(mounts_type fs[], int *cnt) #ifdef MTAB_FILE mtab_fd = fopen(MTAB_FILE, "r"); #else -#error "Can't find your mounted filesystem table file." +# error "Can't find your mounted filesystem table file." #endif // Get rid of old, unmounted filesystems... memset(fs, 0, sizeof(mounts_type) * 256); - while (x < 256) - { - if (fgets(line, 256, mtab_fd) == NULL) - { - fclose (mtab_fd); + while (x < 256) { + if (fgets(line, 256, mtab_fd) == NULL) { + fclose(mtab_fd); *cnt = x; return(FALSE); } @@ -285,8 +272,7 @@ int machine_get_fs(mounts_type fs[], int *cnt) #endif fs[x].blocks = fsinfo.f_blocks; - if (fs[x].blocks > 0) - { + if (fs[x].blocks > 0) { fs[x].bsize = fsinfo.f_bsize; fs[x].bfree = fsinfo.f_bfree; fs[x].files = fsinfo.f_files; @@ -325,9 +311,8 @@ int machine_get_loadavg(double *load) #ifdef USE_GETLOADAVG double loadavg[LOADAVG_NSTATS]; - if (getloadavg(loadavg, LOADAVG_NSTATS) < 0) - { - perror("getloadavg"); + if (getloadavg(loadavg, LOADAVG_NSTATS) < 0) { + perror("getloadavg"); /* ToDo: correct error reporting */ *load = 1.; return(FALSE); } @@ -356,11 +341,9 @@ int machine_get_meminfo(meminfo_type *result) int machine_get_procs(LinkedList *procs) { // Much of this code was ripped from "gmemusage" - char buf[128]; DIR *proc; FILE *StatusFile; struct dirent *procdir; - procinfo_type *p; char procName[16]; int procSize, procRSS, procData, procStk, procExe; @@ -372,7 +355,7 @@ int machine_get_procs(LinkedList *procs) *VmStkLine = "VmStk", *VmExeLine = "VmExe"; const int - NameLineLen = strlen(NameLine), + NameLineLen = strlen(NameLine), VmSizeLineLen = strlen(VmSizeLine), VmDataLineLen = strlen(VmDataLine), VmStkLineLen = strlen(VmStkLine), @@ -380,81 +363,67 @@ int machine_get_procs(LinkedList *procs) VmRSSLineLen = strlen(VmRSSLine); int threshold = 400, unique; - if ((proc = opendir("/proc")) == NULL) - { - perror("mem_top_screen: unable to open /proc"); + if ((proc = opendir("/proc")) == NULL) { + perror("mem_top_screen: unable to open /proc"); /* ToDo: correct error reporting */ return(FALSE); } - while ((procdir = readdir(proc))) - { + while ((procdir = readdir(proc))) { + char buf[128]; + + /* ignore everything in proc except process ids */ if (!index("1234567890", procdir->d_name[0])) continue; sprintf(buf, "/proc/%s/status", procdir->d_name); - if ((StatusFile = fopen(buf, "r")) == NULL) - { + if ((StatusFile = fopen(buf, "r")) == NULL) { // Not a serious error; process has finished before we could // examine it: continue; } procRSS = procSize = procData = procStk = procExe = 0; - while (fgets(buf, sizeof(buf), StatusFile)) - { - if (!strncmp(buf, NameLine, NameLineLen)) - { + while (fgets(buf, sizeof(buf), StatusFile)) { + if (!strncmp(buf, NameLine, NameLineLen)) { /* Name: procName */ sscanf(buf, "%*s %s", procName); - } else if (!strncmp(buf, VmSizeLine, VmSizeLineLen)) - { + } else if (!strncmp(buf, VmSizeLine, VmSizeLineLen)) { /* VmSize: procSize kB */ sscanf(buf, "%*s %d", &procSize); - } else if (!strncmp (buf, VmRSSLine, VmRSSLineLen)) - { + } else if (!strncmp (buf, VmRSSLine, VmRSSLineLen)) { /* VmRSS: procRSS kB */ sscanf(buf, "%*s %d", &procRSS); - } else if (!strncmp(buf, VmDataLine, VmDataLineLen)) - { + } else if (!strncmp(buf, VmDataLine, VmDataLineLen)) { /* VmData: procData kB */ sscanf(buf, "%*s %d", &procData); - } else if (!strncmp(buf, VmStkLine, VmStkLineLen)) - { + } else if (!strncmp(buf, VmStkLine, VmStkLineLen)) { /* VmStk: procStk kB */ sscanf(buf, "%*s %d", &procStk); - } else if (!strncmp(buf, VmExeLine, VmExeLineLen)) - { + } else if (!strncmp(buf, VmExeLine, VmExeLineLen)) { /* VmExe: procExe kB */ sscanf(buf, "%*s %d", &procExe); } } fclose(StatusFile); - if (procSize > threshold) - { + if (procSize > threshold) { // Figure out if it's sharing any memory... unique = 1; LL_Rewind(procs); - do - { - p = LL_Get (procs); - if (p) - { - if (0 == strcmp(p->name, procName)) - { - unique = 0; - p->number++; - p->totl += procData + procStk + procExe; - } + do { + procinfo_type *p = LL_Get(procs); + + if ((p != NULL) && (0 == strcmp(p->name, procName))) { + unique = 0; + p->number++; + p->totl += procData + procStk + procExe; } } while (LL_Next(procs) == 0); // If this is the first one by this name... - if (unique) - { - p = malloc(sizeof(procinfo_type)); - if (!p) - { + if (unique) { + procinfo_type *p = malloc(sizeof(procinfo_type)); + if (p == NULL) { perror("mem_top_screen: Error allocating process entry"); break; } @@ -483,10 +452,8 @@ int machine_get_smpload(load_type *result, int *numcpus) // Look for lines starting with "cpu0", "cpu1", etc. token = strtok(procbuf, "\n"); - while (token) - { - if ((strlen(token) > 3) && (!strncmp(token, "cpu", 3)) && isdigit(token[3])) - { + while (token) { + if ((strlen(token) > 3) && (!strncmp(token, "cpu", 3)) && isdigit(token[3])) { sscanf(token, "%*s %lu %lu %lu %lu", &curr_load[*numcpus].user, &curr_load[*numcpus].nice, &curr_load[*numcpus].system, &curr_load[*numcpus].idle); curr_load[*numcpus].total = curr_load[*numcpus].user + curr_load[*numcpus].nice + curr_load[*numcpus].system + curr_load[*numcpus].idle; @@ -540,7 +507,7 @@ int machine_get_iface_stats (IfaceInfo *interface) /* Open the file in read-only mode and parse */ - if ((file = fopen(DEVFILE, "r")) != NULL) { + if ((file = fopen("/proc/net/dev", "r")) != NULL) { /* Skip first 2 header lines of file */ fgets(buffer, sizeof(buffer), file); fgets(buffer, sizeof(buffer), file);