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