Finished port of lcdproc to Solaris.

This commit is contained in:
cjdebenh
2000-12-20 00:39:16 +00:00
parent a1be5b7039
commit b227f3cad9
6 changed files with 222 additions and 9 deletions
+16 -2
View File
@@ -12,6 +12,10 @@
#include "mode.h"
#include "chrono.h"
#ifdef SOLARIS
#include <utmpx.h>
#endif
int TwentyFourHour = 1;
int uptime_fd = 0;
@@ -25,8 +29,18 @@ get_uptime (double *up, double *idle)
{
double uptime;
#ifndef SOLARIS
reread (uptime_fd, "get_uptime:");
sscanf (buffer, "%lf %lf", &uptime, idle);
#else
struct utmpx *u, id;
id.ut_type = BOOT_TIME;
u = getutxid(&id);
uptime=time(0) - u->ut_xtime;
#endif
*up = uptime;
@@ -91,14 +105,14 @@ chrono_init ()
unamebuf = (struct utsname *) malloc (sizeof (struct utsname));
uptime_fd = open ("/proc/uptime", O_RDONLY);
#if 0
#ifdef 0
kversion_fd = open ("/proc/sys/kernel/osrelease", O_RDONLY);
reread (kversion_fd, "main:");
sscanf (buffer, "%s", kver);
close (kversion_fd);
# endif
#endif
/* Get OS name and version from uname() */
/* Changed to check if eq -1 instead of non-zero */
+50
View File
@@ -4,6 +4,7 @@
#include <fcntl.h>
#include <unistd.h>
#include "../../shared/sockets.h"
#include "../../shared/debug.h"
@@ -11,10 +12,24 @@
#include "mode.h"
#include "cpu.h"
#ifdef SOLARIS
#include <sys/types.h>
#include <sys/param.h>
#include <sys/cpuvar.h>
#endif
#ifdef HAVE_LIBKSTAT
#include <kstat.h>
#endif
struct load {
unsigned long total, user, system, nice, idle;
};
#ifdef HAVE_LIBKSTAT
kstat_ctl_t *kc;
#endif
int load_fd = 0;
static void get_load (struct load *result);
@@ -24,8 +39,31 @@ get_load (struct load *result)
static struct load last_load = { 0, 0, 0, 0, 0 };
struct load curr_load;
#ifndef HAVE_LIBKSTAT
reread (load_fd, "get_load:");
sscanf (buffer, "%*s %lu %lu %lu %lu\n", &curr_load.user, &curr_load.nice, &curr_load.system, &curr_load.idle);
#else
kstat_t *k_space;
void *val_ptr;
k_space = kstat_lookup(kc, "cpu_stat", 0, "cpu_stat0");
if (k_space == NULL) {
printf("\nkstat lookup error");
exit(1);
} else if (kstat_read(kc, k_space, NULL) == -1) {
printf("\nkstat read error");
exit(1);
}
k_space=kstat_lookup(kc, "cpu_stat", -1, "cpu_stat0");
{
struct cpu_stat cinfo;
kstat_read(kc,k_space,&cinfo);
curr_load.idle=cinfo.cpu_sysinfo.cpu[CPU_IDLE];
curr_load.user=cinfo.cpu_sysinfo.cpu[CPU_USER];
curr_load.system=cinfo.cpu_sysinfo.cpu[CPU_KERNEL];
curr_load.nice=cinfo.cpu_sysinfo.cpu[CPU_WAIT];
}
curr_load.nice=0;
#endif
curr_load.total = curr_load.user + curr_load.nice + curr_load.system + curr_load.idle;
result->total = curr_load.total - last_load.total;
result->user = curr_load.user - last_load.user;
@@ -42,9 +80,17 @@ get_load (struct load *result)
int
cpu_init ()
{
#ifndef HAVE_LIBKSTAT
if (!load_fd) {
load_fd = open ("/proc/stat", O_RDONLY);
}
#else
kc = kstat_open();
if (kc == NULL) {
exit(1);
}
#endif
return 0;
}
@@ -52,8 +98,12 @@ cpu_init ()
int
cpu_close ()
{
#ifndef HAVE_LIBKSTAT
if (load_fd)
close (load_fd);
#else
kstat_close(kc);
#endif
load_fd = 0;
+62
View File
@@ -56,12 +56,25 @@
#include "mode.h"
#include "cpu_smp.h"
#ifdef SOLARIS
#include <sys/types.h>
#include <sys/param.h>
#include <sys/cpuvar.h>
#endif
#ifdef HAVE_LIBKSTAT
#include <kstat.h>
#endif
#define MAX_CPUS 8
struct load {
unsigned long total, user, system, nice, idle;
};
#ifdef HAVE_LIBKSTAT
kstat_ctl_t *kc;
#endif
static int load_fd = 0;
static char *numnames[MAX_CPUS] = { "one", "two", "three", "four", "five", "six", "seven", "eight" };
@@ -73,6 +86,8 @@ get_load (struct load *result, int *numcpus)
static struct load last_load[MAX_CPUS];
struct load curr_load[MAX_CPUS];
#ifndef HAVE_LIBKSTAT
*numcpus = 0;
reread (load_fd, "get_load");
@@ -104,14 +119,57 @@ get_load (struct load *result, int *numcpus)
}
token = strtok (NULL, "\n");
}
#else
kstat_t *k_space;
void *val_ptr;
int ncpu=0,count;
char buffer[10];
*numcpus=sysconf(_SC_NPROCESSORS_CONF);
for (count = 0; count < MAX_CPUS;count++) {
sprintf (buffer, "cpu_stat%d",count);
k_space = kstat_lookup(kc, "cpu_stat", count, buffer);
if (k_space == NULL) {
} else if (kstat_read(kc, k_space, NULL) == -1) {
} else {
struct cpu_stat cinfo;
k_space=kstat_lookup(kc, "cpu_stat", -1, buffer);
kstat_read(kc,k_space,&cinfo);
curr_load[ncpu].idle=cinfo.cpu_sysinfo.cpu[CPU_IDLE];
curr_load[ncpu].user=cinfo.cpu_sysinfo.cpu[CPU_USER];
curr_load[ncpu].system=cinfo.cpu_sysinfo.cpu[CPU_KERNEL];
curr_load[ncpu].nice=cinfo.cpu_sysinfo.cpu[CPU_WAIT];
curr_load[ncpu].nice=0;
curr_load[ncpu].total = curr_load[ncpu].user + curr_load[ncpu].nice + curr_load[ncpu].system + curr_load[ncpu].idle;
result[ncpu].total = curr_load[ncpu].total - last_load[ncpu].total;
result[ncpu].user = curr_load[ncpu].user - last_load[ncpu].user;
result[ncpu].nice = curr_load[ncpu].nice - last_load[ncpu].nice;
result[ncpu].system = curr_load[ncpu].system - last_load[ncpu].system;
result[ncpu].idle = curr_load[ncpu].idle - last_load[ncpu].idle;
last_load[ncpu].total = curr_load[ncpu].total;
last_load[ncpu].user = curr_load[ncpu].user;
last_load[ncpu].nice = curr_load[ncpu].nice;
last_load[ncpu].system = curr_load[ncpu].system;
last_load[ncpu].idle = curr_load[ncpu].idle;
ncpu=ncpu++;
}
}
#endif
}
int
cpu_smp_init ()
{
#ifndef HAVE_LIBKSTAT
if (!load_fd) {
load_fd = open ("/proc/stat", O_RDONLY);
}
#else
kc = kstat_open();
if (kc == NULL) {
exit(1);
}
#endif
return 0;
}
@@ -119,8 +177,12 @@ cpu_smp_init ()
int
cpu_smp_close ()
{
#ifndef HAVE_LIBKSTAT
if (load_fd)
close (load_fd);
#else
kstat_close(kc);
#endif
load_fd = 0;
+4
View File
@@ -51,7 +51,11 @@ get_fs (mounts fs[])
char line[256];
int x = 0, y;
#ifdef SOLARIS
mtab_fd = fopen ("/etc/mnttab", "r");
#else
mtab_fd = fopen ("/etc/mtab", "r");
#endif
// Get rid of old, unmounted filesystems...
memset (fs, 0, sizeof (mounts) * 256);
+10 -1
View File
@@ -10,6 +10,10 @@
#include "mode.h"
#include "load.h"
#ifdef HAVE_GETLOADAVG
#include <sys/loadavg.h>
#endif
int loadavg_fd = 0;
static double get_loadavg (void);
@@ -18,9 +22,15 @@ static double
get_loadavg (void)
{
double load;
double loadavg[LOADAVG_NSTATS];
#ifndef HAVE_GETLOADAVG
reread (loadavg_fd, "get_load:");
sscanf (buffer, "%lf", &load);
#else
getloadavg(loadavg,LOADAVG_NSTATS);
load = loadavg[LOADAVG_1MIN];
#endif
return load;
}
@@ -30,7 +40,6 @@ load_init ()
#ifndef SOLARIS
if (!loadavg_fd)
loadavg_fd = open ("/proc/loadavg", O_RDONLY);
#endif
return 0;
}
+80 -6
View File
@@ -17,6 +17,9 @@
#ifdef SOLARIS
#include <strings.h>
#include <sys/stat.h>
#include <sys/swap.h>
#include <procfs.h>
#endif
@@ -32,7 +35,7 @@ static void
get_mem_info (struct meminfo *result)
{
// int i, res; char *bufptr;
int pagesize,page,i;
int pagesize,page;
#ifndef SOLARIS
reread (meminfo_fd, "get_meminfo:");
@@ -44,13 +47,61 @@ get_mem_info (struct meminfo *result)
result[1].total = getentry ("SwapTotal:", buffer);
result[1].free = getentry ("SwapFree:", buffer);
#else
result[0].total = 0;
result[0].free = 0;
#define MAXSTRSIZE 80
swaptbl_t *s=NULL;
int i, n, num;
char *strtab; /* string table for path names */
result[0].total = sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE) / 1024;
result[0].free = sysconf(_SC_AVPHYS_PAGES) * sysconf(_SC_PAGESIZE) / 1024;
result[0].shared = 0;
result[0].buffers = 0;
result[0].cache = 0;
again:
if ((num = swapctl(SC_GETNSWP, 0)) == -1) {
perror("swapctl: GETNSWP");
exit(1);
}
if (num == 0) {
fprintf(stderr, "No Swap Devices Configured\n");
exit(2);
}
/* allocate swaptable for num+1 entries */
if ((s = (swaptbl_t *)
malloc(num * sizeof(swapent_t) +
sizeof(struct swaptable))) ==
(void *) 0) {
fprintf(stderr, "Malloc Failed\n");
exit(3);
}
/* allocate num+1 string holders */
if ((strtab = (char *)
malloc((num + 1) * MAXSTRSIZE)) == (void *) 0) {
fprintf(stderr, "Malloc Failed\n");
exit(3);
}
/* initialize string pointers */
for (i = 0; i < (num + 1); i++) {
s->swt_ent[i].ste_path = strtab + (i * MAXSTRSIZE);
}
s->swt_n = num + 1;
if ((n = swapctl(SC_LIST, s)) < 0) {
perror("swapctl");
exit(1);
}
if (n > num) { /* more were added */
free(s);
free(strtab);
goto again;
}
result[1].total = 0;
result[1].free = 0;
for (i = 0; i < n; i++) {
result[1].total = result[1].total + s->swt_ent[i].ste_pages * sysconf(_SC_PAGESIZE) / 1024;
result[1].free = result[1].free + s->swt_ent[i].ste_free * sysconf(_SC_PAGESIZE) / 1024;
}
#endif
}
@@ -285,7 +336,6 @@ mem_top_screen (int rep, int display)
if (first) {
first = 0;
sock_send_string (sock, "screen_add S\n");
sprintf (buffer, "screen_set S -name {Top Memory Use: %s}\n", host);
sock_send_string (sock, buffer);
@@ -320,6 +370,7 @@ mem_top_screen (int rep, int display)
if (!index ("1234567890", procdir->d_name[0])) {
continue;
}
#ifndef SOLARIS
sprintf (buf, "/proc/%s/status", procdir->d_name);
if ((StatusFile = fopen (buf, "r")) == NULL) {
// Not a serious error; process has finished before we could
@@ -351,6 +402,29 @@ mem_top_screen (int rep, int display)
sscanf (buf, "%*s %d", &procExe);
}
}
#else
sprintf (buf, "/proc/%s/psinfo", procdir->d_name);
if ((StatusFile = fopen (buf, "r")) == NULL) {
// Not a serious error; process has finished before we could
// examine it:
//fprintf ( stderr , "mem_top_screen: cannot open %s for reading" ,
// buf ) ;
//perror ( "" ) ;
continue;
}
{
psinfo_t psinfo;
fread(&psinfo,sizeof(psinfo),1,StatusFile);
procRSS = procSize = procData = procStk = procExe = 0;
strcpy(procName,psinfo.pr_fname);
procSize=psinfo.pr_size;
procRSS=psinfo.pr_rssize;
// Following values not accurate, not sure what needs to be set to
procData=psinfo.pr_size;
procStk=0;
procExe=0;
}
#endif
fclose (StatusFile);
if (procSize > threshold) {
// Figure out if it's sharing any memory...
@@ -385,7 +459,6 @@ mem_top_screen (int rep, int display)
}
closedir (proc);
// Now, print some info...
LL_Rewind (procs);
LL_Sort (procs, sort_procs);
@@ -402,7 +475,8 @@ mem_top_screen (int rep, int display)
sock_send_string (sock, buffer);
} else {
//printf("Mem hog: none?\n");
sprintf (buffer, "widget_set S %i 1 %i {}\n", i, i);
//sprintf (buffer, "widget_set S %i 1 %i {}\n", i, i);
sprintf (buffer, "widget_set S %i 1 %i \" \"\n", i, i);
if (display)
sock_send_string (sock, buffer);
}