NetBSD port by Thomas Runge.

This commit is contained in:
gfk
2003-08-12 02:35:01 +00:00
parent d5556d1388
commit c828ddc45b
9 changed files with 346 additions and 25 deletions
+69
View File
@@ -7,6 +7,14 @@
#include "shared/sockets.h"
#ifdef __NetBSD__
#include <errno.h>
#include <sys/fcntl.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <machine/apmvar.h>
#endif
#include "main.h"
#include "mode.h"
#include "batt.h"
@@ -18,6 +26,44 @@ static int get_batt_stat (int *acstat, int *battstat, int *battflag, int *percen
static int
get_batt_stat (int *acstat, int *battstat, int *battflag, int *percent)
{
#ifdef __NetBSD__
struct apm_power_info apmi;
int apmd;
if((apmd = open("/dev/apm", O_RDONLY)) == -1)
return -1;
memset(&apmi, 0, sizeof(apmi));
if(ioctl(apmd, APM_IOC_GETPOWER, &apmi) == -1)
{
fprintf(stderr, "APM_IOC_GETPOWER failed in get_batt_stat(): %s\n",
strerror(errno));
return -1;
}
/* don't know, what the Linux counterparts exactly mean */
switch(apmi.ac_state)
{
case APM_AC_OFF:
*acstat = 0;
break;
case APM_AC_ON:
*acstat = 1;
break;
case APM_AC_BACKUP:
*acstat = 2;
break;
default:
*acstat = apmi.ac_state;
break;
}
*battstat = 0;
*battflag = apmi.battery_state;
*percent = apmi.battery_life;
close(apmd);
#else
char str[64];
if (!batt_fd)
@@ -34,6 +80,7 @@ get_batt_stat (int *acstat, int *battstat, int *battflag, int *percent)
if (3 > sscanf (str + 13, "0x%x 0x%x 0x%x %d", acstat, battstat, battflag, percent))
return -1;
#endif
return 0;
}
@@ -133,6 +180,27 @@ battery_screen (int rep, int display)
sock_send_string (sock, tmp);
sprintf (tmp, "widget_set B two 1 3 {");
#ifdef __NetBSD__
switch(battflag)
{
case APM_BATT_HIGH:
sprintf (tmp + strlen (tmp), "Batt: High");
break;
case APM_BATT_LOW:
sprintf (tmp + strlen (tmp), "Batt: Low");
break;
case APM_BATT_CRITICAL:
sprintf (tmp + strlen (tmp), "Batt: Critical");
break;
case APM_BATT_CHARGING:
sprintf (tmp + strlen (tmp), "Batt: Charging");
break;
case APM_BATT_UNKNOWN:
default:
sprintf (tmp + strlen (tmp), "Batt: Unknown");
break;
}
#else
if (battflag == 0xff) {
sprintf (tmp + strlen (tmp), "Battery Stat Unknown");
} else {
@@ -148,6 +216,7 @@ battery_screen (int rep, int display)
if (battflag & 128)
sprintf (tmp + strlen (tmp), " (NONE)");
}
#endif
sprintf (tmp + strlen (tmp), "}\n");
if (display)
sock_send_string (sock, tmp);
+3 -3
View File
@@ -100,7 +100,7 @@ int uptime_fd = 0;
#if FREEBSD
static double freebsd_get_uptime(double *up, double *idle);
#define get_uptime freebsd_get_uptime
#elif (NETBSD || OPENBSD)
#elif (__NetBSD__ || OPENBSD)
static double opennetbsd_get_uptime(double *up, double *idle);
#define get_uptime opennetbsd_get_uptime
#elif LINUX
@@ -196,7 +196,7 @@ static long get_openbsd_idle_time() {
}
#endif
#if (NETBSD || OPENBSD)
#if (__NetBSD__ || OPENBSD)
static double opennetbsd_get_uptime(double *up, double *idle) {
double uptime = 0;
struct timeval boottime;
@@ -204,7 +204,7 @@ static double opennetbsd_get_uptime(double *up, double *idle) {
time_t now, uuptime;
size_t size;
int mib[2];
#if NETBSD
#if __NetBSD__
u_int64_t cp_time[CPUSTATES];
#endif
+3 -3
View File
@@ -88,7 +88,7 @@ kstat_ctl_t *kc;
int load_fd = 0;
#ifdef NETBSD
#ifdef __NetBSD__
static void netbsd_get_load (struct load * result, struct load * last_load );
#define get_load netbsd_get_load
#elif defined OPENBSD || FREEBSD
@@ -141,7 +141,7 @@ static void linuxsolaris_get_load (struct load *result, struct load * last_load
}
#endif
#ifdef NETBSD
#ifdef __NetBSD__
static void netbsd_get_load(struct load * result, struct load * last_load) {
struct load curr_load;
u_int64_t cp_time[CPUSTATES];
@@ -153,7 +153,7 @@ static void netbsd_get_load(struct load * result, struct load * last_load) {
size = sizeof(cp_time);
if(sysctl(mib, 2, cp_time, &size, NULL, 0) < 0) {
//fprintf(stderr, "sysctl kern.cp_time failed: %s\n", strerror(errno));
fprintf(stderr, "sysctl kern.cp_time failed: %s\n", strerror(errno));
curr_load.user = 1;
curr_load.nice = 1;
curr_load.system = 1;
+63 -1
View File
@@ -64,6 +64,12 @@
#ifdef HAVE_LIBKSTAT
#include <kstat.h>
#endif
#ifdef __NetBSD__
#include <sys/param.h>
#include <sys/sysctl.h>
#include <sys/sched.h>
#include <errno.h>
#endif
#define MAX_CPUS 8
@@ -79,14 +85,65 @@ static int load_fd = 0;
static char *numnames[MAX_CPUS] = { "one", "two", "three", "four", "five", "six", "seven", "eight" };
#ifdef __NetBSD__
static void
get_netbsd_load (struct load *result, int *numcpus)
{
int mib[2], i, num;
size_t size;
struct load curr_load;
u_int64_t cp_time[CPUSTATES];
mib[0] = CTL_HW;
mib[1] = HW_NCPU;
size = sizeof(int);
if(sysctl(mib, 2, &num, &size, NULL, 0) < 0)
{
fprintf(stderr, "sysctl kern.boottime failed: %s\n", strerror(errno));
}
mib[0] = CTL_KERN;
mib[1] = KERN_CP_TIME;
size = sizeof(cp_time);
if(sysctl(mib, 2, cp_time, &size, NULL, 0) < 0)
{
fprintf(stderr, "sysctl kern.cp_time failed: %s\n", strerror(errno));
curr_load.user = 1;
curr_load.nice = 1;
curr_load.system = 1;
curr_load.idle = 1;
}
else
{
curr_load.user = cp_time[CP_USER];
curr_load.nice = cp_time[CP_NICE];
curr_load.system = cp_time[CP_SYS] + cp_time[CP_INTR];
curr_load.idle = cp_time[CP_IDLE];
}
curr_load.total = curr_load.user + curr_load.nice + curr_load.system + curr_load.idle;
*numcpus = num;
num = num > 8 ? 8 : num;
/* we can not get per-cpu-load values */
for(i = 0; i < num; i++)
{
result[i] = curr_load;
}
}
#endif /* __NetBSD__ */
static void
get_load (struct load *result, int *numcpus)
{
#ifdef __NetBSD__
get_netbsd_load(result, numcpus);
#else
char *token;
static struct load last_load[MAX_CPUS];
struct load curr_load[MAX_CPUS];
#ifndef HAVE_LIBKSTAT
*numcpus = 0;
@@ -155,11 +212,13 @@ get_load (struct load *result, int *numcpus)
}
}
#endif
#endif /* __NetBSD__ */
}
int
cpu_smp_init ()
{
#ifndef __NetBSD__
#ifndef HAVE_LIBKSTAT
if (!load_fd) {
load_fd = open ("/proc/stat", O_RDONLY);
@@ -170,6 +229,7 @@ cpu_smp_init ()
exit(1);
}
#endif
#endif /* __NetBSD__ */
return 0;
}
@@ -177,6 +237,7 @@ cpu_smp_init ()
int
cpu_smp_close ()
{
#ifndef __NetBSD__
#ifndef HAVE_LIBKSTAT
if (load_fd)
close (load_fd);
@@ -186,6 +247,7 @@ cpu_smp_close ()
load_fd = 0;
#endif /* __NetBSD__ */
return 0;
}
+60 -1
View File
@@ -33,7 +33,13 @@
#include "mode.h"
#include "disk.h"
#ifdef __NetBSD__
#include <sys/param.h>
#include <sys/ucred.h>
#include <sys/mount.h>
#else
FILE *mtab_fd;
#endif
typedef struct mounts {
char dev[256], type[64], mpoint[256];
@@ -43,6 +49,58 @@ typedef struct mounts {
static int
get_fs (mounts fs[])
{
#ifdef __NetBSD__
struct statfs *mntbuf;
struct statfs *pp;
int statcnt, cnt, i;
cnt = getmntinfo(&mntbuf, MNT_WAIT);
if(cnt == 0)
{
perror("getmntinfo");
return(0);
}
for(statcnt = 0, pp = mntbuf, i = 0; i < cnt; pp++, i++)
{
if(strcmp(pp->f_fstypename, "procfs")
&& strcmp(pp->f_fstypename, "kernfs")
&& strcmp(pp->f_fstypename, "linprocfs")
#ifndef STAT_NFS
&& strcmp(pp->f_fstypename, "nfs")
#endif
#ifndef STAT_SMBFS
&& strcmp(pp->f_fstypename, "smbfs")
#endif
)
{
/*
printf("mount point: %s\n", pp->f_mntonname);
printf("fs name : %s\n", pp->f_mntfromname);
printf("fs type : %s\n", pp->f_fstypename);
*/
snprintf(fs[statcnt].dev, 255, "%s", pp->f_mntfromname);
snprintf(fs[statcnt].mpoint, 255, "%s", pp->f_mntonname);
snprintf(fs[statcnt].type, 255, "%s", pp->f_fstypename);
fs[statcnt].blocks = pp->f_blocks;
if(fs[statcnt].blocks > 0)
{
fs[statcnt].bsize = pp->f_bsize;
fs[statcnt].bfree = pp->f_bfree;
fs[statcnt].files = pp->f_files;
fs[statcnt].ffree = pp->f_ffree;
/*
printf("bsize : %ld\n", pp->f_bsize);
printf("blocks: %ld\n", pp->f_blocks);
printf("free : %ld\n", pp->f_bfree);
*/
}
statcnt++;
}
}
return(statcnt);
#else
#ifdef STAT_STATVFS
struct statvfs fsinfo;
#else
@@ -99,6 +157,7 @@ get_fs (mounts fs[])
fclose (mtab_fd);
return x;
#endif
}
int
@@ -178,7 +237,7 @@ disk_screen (int rep, int display)
sprintf (table[i].dev, "%s", mnt[i].mpoint);
}
//table[i].full = (lcd.cellwid * 4)
//table[i].full = (lcd_cellwid * 4);
table[i].full = (huge) (lcd_cellwid * 4)
* (huge) (mnt[i].blocks - mnt[i].bfree)
/ (huge) mnt[i].blocks;
+5
View File
@@ -23,6 +23,11 @@
# include <sys/loadavg.h>
#endif
#ifdef __NetBSD__
#define USE_GETLOADAVG
#define LOADAVG_NSTATS 1
#define LOADAVG_1MIN 1
#endif
int loadavg_fd = 0;
+134 -15
View File
@@ -26,6 +26,17 @@
# include <sys/procfs.h>
#endif
#endif
#ifdef __NetBSD__
#include <sys/param.h>
#include <sys/sysctl.h>
#include <uvm/uvm_extern.h>
#include <errno.h>
#include <kvm.h>
static int pageshift;
#define pagetok(size) ((size) << pageshift)
#define PROCSIZE(pp) ((pp)->p_vm_tsize + (pp)->p_vm_dsize + (pp)->p_vm_ssize)
static void netbsd_get_procs(LinkedList *procs);
#endif
struct meminfo {
@@ -40,16 +51,7 @@ static void
get_mem_info (struct meminfo *result)
{
#ifndef SOLARIS
reread (meminfo_fd, "get_meminfo:");
result[0].total = getentry ("MemTotal:", buffer);
result[0].free = getentry ("MemFree:", buffer);
result[0].shared = getentry ("MemShared:", buffer);
result[0].buffers = getentry ("Buffers:", buffer);
result[0].cache = getentry ("Cached:", buffer);
result[1].total = getentry ("SwapTotal:", buffer);
result[1].free = getentry ("SwapFree:", buffer);
#else
#ifdef SOLARIS
#define MAXSTRSIZE 80
swaptbl_t *s=NULL;
int i, n, num;
@@ -105,13 +107,65 @@ again:
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;
}
#else
#ifdef __NetBSD__
size_t size;
int mib[2];
struct uvmexp_sysctl uvmexp;
mib[0] = CTL_VM;
mib[1] = VM_UVMEXP2;
size = sizeof(uvmexp);
if(sysctl(mib, 2, &uvmexp, &size, NULL, 0) < 0)
{
fprintf(stderr, "sysctl vm.uvmexp2 failed: %s\n",
strerror(errno));
exit(EXIT_FAILURE);
}
/* memory */
result[0].total = pagetok(uvmexp.npages);
result[0].free = pagetok(uvmexp.free);
/* not really */
result[0].shared = pagetok(uvmexp.wired);
result[0].buffers = pagetok(uvmexp.execpages);
result[0].cache = pagetok(uvmexp.filepages);
/* swap */
result[1].total = uvmexp.pagesize * uvmexp.swpages;
result[1].free = uvmexp.pagesize * uvmexp.swpginuse;
result[1].free = result[1].total - result[1].free;
#else
reread (meminfo_fd, "get_meminfo:");
result[0].total = getentry ("MemTotal:", buffer);
result[0].free = getentry ("MemFree:", buffer);
result[0].shared = getentry ("MemShared:", buffer);
result[0].buffers = getentry ("Buffers:", buffer);
result[0].cache = getentry ("Cached:", buffer);
result[1].total = getentry ("SwapTotal:", buffer);
result[1].free = getentry ("SwapFree:", buffer);
#endif
#endif
}
int
mem_init ()
{
#ifndef SOLARIS
#ifdef SOLARIS
#else
#ifdef __NetBSD__
/* get the page size with "getpagesize" and calculate pageshift from it */
int pagesize = getpagesize();
pageshift = 0;
while(pagesize > 1)
{
pageshift++;
pagesize >>= 1;
}
/* we only need the amount of log(2)1024 for our conversion */
pageshift -= 10;
#else
if (!meminfo_fd) {
meminfo_fd = open ("/proc/meminfo", O_RDONLY);
if (meminfo_fd < 0) {
@@ -119,7 +173,7 @@ mem_init ()
exit (1);
}
}
#endif
#endif
return 0;
}
@@ -127,12 +181,16 @@ mem_init ()
int
mem_close ()
{
#ifndef SOLARIS
#ifdef SOLARIS
#else
#ifdef __NetBSD__
#else
if (meminfo_fd)
meminfo_fd = open ("/proc/meminfo", O_RDONLY);
meminfo_fd = 0;
#endif
#endif
return 0;
}
@@ -330,6 +388,7 @@ sort_procs (void *a, void *b)
int
mem_top_screen (int rep, int display)
{
#ifndef __NetBSD__
// Much of this code was ripped from "gmemusage"
char buf[128];
@@ -344,8 +403,9 @@ mem_top_screen (int rep, int display)
*NameLine = "Name:", *VmSizeLine = "VmSize:", *VmRSSLine = "VmRSS", *VmDataLine = "VmData", *VmStkLine = "VmStk", *VmExeLine = "VmExe";
const int
NameLineLen = strlen (NameLine), VmSizeLineLen = strlen (VmSizeLine), VmDataLineLen = strlen (VmDataLine), VmStkLineLen = strlen (VmStkLine), VmExeLineLen = strlen (VmExeLine), VmRSSLineLen = strlen (VmRSSLine);
int threshold = 400, unique;
#endif /* __NetBSD__ */
int i;
proc_mem_info *p;
LinkedList *procs;
@@ -377,6 +437,9 @@ mem_top_screen (int rep, int display)
return -1;
}
#ifdef __NetBSD__
netbsd_get_procs(procs);
#else
if ((proc = opendir ("/proc")) == NULL) {
fprintf (stderr, "mem_top_screen: unable to open /proc");
perror ("");
@@ -476,6 +539,8 @@ mem_top_screen (int rep, int display)
}
closedir (proc);
#endif /* __NetBSD__ */
// Now, print some info...
LL_Rewind (procs);
LL_Sort (procs, sort_procs);
@@ -501,7 +566,9 @@ mem_top_screen (int rep, int display)
LL_Next (procs);
}
#ifndef __NetBSD__
end: // Ack! I hate using labels!
#endif
// Now clean it all up...
LL_Rewind (procs);
do {
@@ -515,4 +582,56 @@ mem_top_screen (int rep, int display)
return 0;
} // End mem_top_screen()
}
#ifdef __NetBSD__
static void netbsd_get_procs(LinkedList *procs)
{
kvm_t *kvmd = NULL;
struct kinfo_proc2 *kprocs = NULL;
struct kinfo_proc2 *pp;
int nproc, i;
proc_mem_info *p;
if((kvmd = kvm_open(NULL, NULL, NULL, KVM_NO_FILES, "kvm_open")) == NULL
)
{
fprintf(stderr, "kvm_openfiles\n");
exit(EXIT_FAILURE);
}
kprocs = kvm_getproc2(kvmd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc2), &nproc);
if(kprocs == NULL)
{
fprintf(stderr, "kvm_getproc2\n");
exit(EXIT_FAILURE);
}
for(pp = kprocs, i = 0; i < nproc; pp++, i++)
{
/*
printf("proc: %s (%ld) size: %ld K\n", pp->p_comm,
(long) (pp->p_pid),
(long) (PROCSIZE(pp) << pageshift));
*/
p = malloc(sizeof(proc_mem_info));
if(!p)
{
fprintf(stderr, "mem_top_screen: Error allocating process entry\n");
kvm_close(kvmd);
exit(EXIT_FAILURE);
}
strncpy(p->name, pp->p_comm, 15);
p->name[15] = '\0';
p->totl = (PROCSIZE(pp) << pageshift);
p->number = pp->p_pid;
/* TODO: Check for errors here? */
LL_Push(procs, (void *)p);
kprocs++;
}
kvm_close(kvmd);
}
#endif /* __NetBSD__ */
+9 -1
View File
@@ -280,12 +280,20 @@ credit_screen (int rep, int display)
sock_send_string (sock, "widget_add A one string\n");
sock_send_string (sock, "widget_add A two string\n");
sock_send_string (sock, "widget_add A three string\n");
sock_send_string (sock, "widget_set A one 1 2 { for Linux }\n");
#ifdef __NetBSD__
sock_send_string (sock, "widget_set A one 1 2 { for Linux & NetBSD}\n");
#else
sock_send_string (sock, "widget_set A one 1 2 { for Linux }\n");
#endif
sock_send_string (sock, "widget_set A two 1 3 { by William Ferrell}\n");
sock_send_string (sock, "widget_set A three 1 4 { and Scott Scriven}\n");
} else {
sock_send_string (sock, "widget_add A text scroller\n");
#ifdef __NetBSD__
sock_send_string (sock, "widget_set A text 1 2 20 2 v 8 { for Linux & NetBSD by William Ferrell and Scott Scriven}\n");
#else
sock_send_string (sock, "widget_set A text 1 2 20 2 v 8 { for Linux by William Ferrell and Scott Scriven}\n");
#endif
}
}
-1
View File
@@ -290,7 +290,6 @@ process_command_line (int argc, char **argv)
debug( RPT_DEBUG, "%s( argc=%d, argv=...)", __FUNCTION__, argc );
/* Reset getopt */
optind = 0;
opterr = 0; /* Prevent some messages to stderr */
/* Analyze options here.. (please try to keep list of options the