X-Git-Url: http://vcs.maemo.org/git/?a=blobdiff_plain;f=src%2Fopenbsd.c;h=2cd3f0c693b6da65d1f7b75723f6abe4936829c3;hb=72c9a740c77f3e73e2d104add63890ee7df13b43;hp=fe68f5ea790975fd56720d94238e2702edd70cb6;hpb=60f4ea4fb759276eb8ca9797263c1a28ff42426f;p=monky diff --git a/src/openbsd.c b/src/openbsd.c index fe68f5e..2cd3f0c 100644 --- a/src/openbsd.c +++ b/src/openbsd.c @@ -1,4 +1,6 @@ -/* +/* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*- + * vim: ts=4 sw=4 noet ai cindent syntax=c + * * Conky, a system monitor, based on torsmo * * Any original torsmo code is licensed under the BSD license @@ -8,7 +10,8 @@ * Please see COPYING for details * * Copyright (c) 2007 Toni Spets - * Copyright (c) 2005-2007 Brenden Matthews, Philip Kovacs, et. al. (see AUTHORS) + * Copyright (c) 2005-2010 Brenden Matthews, Philip Kovacs, et. al. + * (see AUTHORS) * All rights reserved. * * This program is free software: you can redistribute it and/or modify @@ -21,9 +24,8 @@ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * You should have received a copy of the GNU General Public License - * along with this program. If not, see . + * along with this program. If not, see . * - * $Id$ */ #include @@ -50,9 +52,6 @@ #include #include #include -#include -#include -#include #include #include @@ -60,6 +59,11 @@ #include #include "conky.h" +#include "diskio.h" +#include "logging.h" +#include "net_stat.h" +#include "openbsd.h" +#include "top.h" #define MAXSHOWDEVS 16 @@ -79,39 +83,42 @@ int init_sensors = 0; static int kvm_init() { - if(init_kvm) + if (init_kvm) { return 1; + } kd = kvm_open(NULL, NULL, NULL, KVM_NO_FILES, NULL); - if(kd == NULL) - ERR("error opening kvm"); - else init_kvm = 1; + if (kd == NULL) { + NORM_ERR("error opening kvm"); + } else { + init_kvm = 1; + } return 1; } /* note: swapmode taken from 'top' source */ -/* - * swapmode is rewritten by Tobias Weingartner - * to be based on the new swapctl(2) system call. - */ -static int -swapmode(int *used, int *total) +/* swapmode is rewritten by Tobias Weingartner + * to be based on the new swapctl(2) system call. */ +static int swapmode(int *used, int *total) { struct swapent *swdev; int nswap, rnswap, i; nswap = swapctl(SWAP_NSWAP, 0, 0); - if (nswap == 0) + if (nswap == 0) { return 0; + } swdev = malloc(nswap * sizeof(*swdev)); - if (swdev == NULL) + if (swdev == NULL) { return 0; + } rnswap = swapctl(SWAP_STATS, swdev, nswap); - if (rnswap == -1) + if (rnswap == -1) { return 0; + } /* if rnswap != nswap, then what? */ @@ -133,28 +140,26 @@ int check_mount(char *s) return 0; } -void -update_uptime() +void update_uptime() { int mib[2] = { CTL_KERN, KERN_BOOTTIME }; struct timeval boottime; time_t now; - size_t size = sizeof (boottime); + size_t size = sizeof(boottime); - if ((sysctl(mib, 2, &boottime, &size, NULL, 0) != -1) && - (boottime.tv_sec != 0)) { + if ((sysctl(mib, 2, &boottime, &size, NULL, 0) != -1) + && (boottime.tv_sec != 0)) { time(&now); info.uptime = now - boottime.tv_sec; } else { - ERR("Could not get uptime"); + NORM_ERR("Could not get uptime"); info.uptime = 0; } } -void -update_meminfo() +void update_meminfo() { - static int mib[2] = {CTL_VM, VM_METER}; + static int mib[2] = { CTL_VM, VM_METER }; struct vmtotal vmtotal; size_t size; int pagesize, pageshift, swap_avail, swap_used; @@ -176,20 +181,22 @@ update_meminfo() bzero(&vmtotal, sizeof(vmtotal)); } - info.memmax = pagetok(vmtotal.t_rm) + pagetok(vmtotal.t_free); + info.memmax = pagetok(vmtotal.t_rm) + pagetok(vmtotal.t_free); info.mem = pagetok(vmtotal.t_rm); + info.memeasyfree = info.memfree = info.memmax - info.mem; if ((swapmode(&swap_used, &swap_avail)) >= 0) { info.swapmax = swap_avail; info.swap = swap_used; + info.swapfree = swap_avail - swap_used; } else { info.swapmax = 0; info.swap = 0; + info.swapfree = 0; } } -void -update_net_stats() +void update_net_stats() { struct net_stat *ns; double delta; @@ -197,17 +204,18 @@ update_net_stats() struct ifaddrs *ifap, *ifa; struct if_data *ifd; - /* get delta */ delta = current_update_time - last_update_time; - if (delta <= 0.0001) + if (delta <= 0.0001) { return; + } - if (getifaddrs(&ifap) < 0) + if (getifaddrs(&ifap) < 0) { return; + } for (ifa = ifap; ifa; ifa = ifa->ifa_next) { - ns = get_net_stat((const char *) ifa->ifa_name); + ns = get_net_stat((const char *) ifa->ifa_name, NULL, NULL); if (ifa->ifa_flags & IFF_UP) { struct ifaddrs *iftmp; @@ -216,35 +224,36 @@ update_net_stats() last_recv = ns->recv; last_trans = ns->trans; - if (ifa->ifa_addr->sa_family != AF_LINK) + if (ifa->ifa_addr->sa_family != AF_LINK) { continue; + } - for (iftmp = ifa->ifa_next; iftmp != NULL && - strcmp(ifa->ifa_name, iftmp->ifa_name) == 0; - iftmp = iftmp->ifa_next) - if (iftmp->ifa_addr->sa_family == AF_INET) + for (iftmp = ifa->ifa_next; + iftmp != NULL && strcmp(ifa->ifa_name, iftmp->ifa_name) == 0; + iftmp = iftmp->ifa_next) { + if (iftmp->ifa_addr->sa_family == AF_INET) { memcpy(&(ns->addr), iftmp->ifa_addr, iftmp->ifa_addr->sa_len); + } + } ifd = (struct if_data *) ifa->ifa_data; r = ifd->ifi_ibytes; t = ifd->ifi_obytes; - if (r < ns->last_read_recv) - ns->recv += - ((long long) 4294967295U - - ns->last_read_recv) + r; - else + if (r < ns->last_read_recv) { + ns->recv += ((long long) 4294967295U - ns->last_read_recv) + r; + } else { ns->recv += (r - ns->last_read_recv); + } ns->last_read_recv = r; - if (t < ns->last_read_trans) - ns->trans += - ((long long) 4294967295U - - ns->last_read_trans) + t; - else + if (t < ns->last_read_trans) { + ns->trans += (long long) 4294967295U - ns->last_read_trans + t; + } else { ns->trans += (t - ns->last_read_trans); + } ns->last_read_trans = t; @@ -259,19 +268,17 @@ update_net_stats() freeifaddrs(ifap); } -void -update_total_processes() +void update_total_processes() { int n_processes; kvm_init(); - kvm_getprocs(kd, KERN_PROC_ALL, 0, &n_processes); + kvm_getprocs(kd, KERN_PROC_ALL, 0, &n_processes); info.procs = n_processes; } -void -update_running_processes() +void update_running_processes() { struct kinfo_proc2 *p; int n_processes; @@ -279,50 +286,74 @@ update_running_processes() kvm_init(); int max_size = sizeof(struct kinfo_proc2); + p = kvm_getproc2(kd, KERN_PROC_ALL, 0, max_size, &n_processes); for (i = 0; i < n_processes; i++) { - if (p[i].p_stat == SRUN) + if (p[i].p_stat == SRUN) { cnt++; + } } info.run_procs = cnt; } +/* new SMP code can be enabled by commenting the following line */ +#define OLDCPU + +#ifdef OLDCPU struct cpu_load_struct { unsigned long load[5]; }; struct cpu_load_struct fresh = { {0, 0, 0, 0, 0} }; long cpu_used, oldtotal, oldused; +#else +#include +int64_t *fresh = NULL; + +/* XXX is 8 enough? - What's the constant for MAXCPU? */ +/* allocate this with malloc would be better */ +int64_t oldtotal[8], oldused[8]; +#endif -void -get_cpu_count() +void get_cpu_count() { - /* - * FIXME: is it possible to get per cpu stats with openbsd? - */ -#if 0 - int cpu_count = 0; + int cpu_count = 1; /* default to 1 cpu */ +#ifndef OLDCPU int mib[2] = { CTL_HW, HW_NCPU }; size_t len = sizeof(cpu_count); - if (sysctl(mib, 2, &cpu_count, &len, NULL, 0) == 0) - info.cpu_count = cpu_count; - else /* last resort, 1 cpu */ + + if (sysctl(mib, 2, &cpu_count, &len, NULL, 0) != 0) { + NORM_ERR("error getting cpu count, defaulting to 1"); + } #endif - info.cpu_count = 1; + info.cpu_count = cpu_count; - info.cpu_usage = malloc(info.cpu_count * sizeof (float)); - if (info.cpu_usage == NULL) - CRIT_ERR("malloc"); + info.cpu_usage = malloc(info.cpu_count * sizeof(float)); + if (info.cpu_usage == NULL) { + CRIT_ERR(NULL, NULL, "malloc"); + } + +#ifndef OLDCPU + assert(fresh == NULL); /* XXX Is this leaking memory? */ + /* XXX Where shall I free this? */ + if (NULL == (fresh = calloc(cpu_count, sizeof(int64_t) * CPUSTATES))) { + CRIT_ERR(NULL, NULL, "calloc"); + } +#endif } -void -update_cpu_usage() +void update_cpu_usage() { +#ifdef OLDCPU int mib[2] = { CTL_KERN, KERN_CPTIME }; long used, total; long cp_time[CPUSTATES]; - size_t len = sizeof (cp_time); + size_t len = sizeof(cp_time); +#else + size_t size; + unsigned int i; +#endif /* add check for !info.cpu_usage since that mem is freed on a SIGUSR1 */ if ((cpu_setup == 0) || (!info.cpu_usage)) { @@ -330,8 +361,9 @@ update_cpu_usage() cpu_setup = 1; } +#ifdef OLDCPU if (sysctl(mib, 2, &cp_time, &len, NULL, 0) < 0) { - ERR("Cannot get kern.cp_time"); + NORM_ERR("Cannot get kern.cp_time"); } fresh.load[0] = cp_time[CP_USER]; @@ -341,8 +373,7 @@ update_cpu_usage() fresh.load[4] = cp_time[CP_IDLE]; used = fresh.load[0] + fresh.load[1] + fresh.load[2]; - total = - fresh.load[0] + fresh.load[1] + fresh.load[2] + fresh.load[3]; + total = fresh.load[0] + fresh.load[1] + fresh.load[2] + fresh.load[3]; if ((total - oldtotal) != 0) { info.cpu_usage[0] = ((double) (used - oldused)) / @@ -353,12 +384,55 @@ update_cpu_usage() oldused = used; oldtotal = total; +#else + if (info.cpu_count > 1) { + size = CPUSTATES * sizeof(int64_t); + for (i = 0; i < info.cpu_count; i++) { + int cp_time_mib[] = { CTL_KERN, KERN_CPTIME2, i }; + if (sysctl(cp_time_mib, 3, &(fresh[i * CPUSTATES]), &size, NULL, 0) + < 0) { + NORM_ERR("sysctl kern.cp_time2 failed"); + } + } + } else { + int cp_time_mib[] = { CTL_KERN, KERN_CPTIME }; + long cp_time_tmp[CPUSTATES]; + + size = sizeof(cp_time_tmp); + if (sysctl(cp_time_mib, 2, cp_time_tmp, &size, NULL, 0) < 0) { + NORM_ERR("sysctl kern.cp_time failed"); + } + + for (i = 0; i < CPUSTATES; i++) { + fresh[i] = (int64_t) cp_time_tmp[i]; + } + } + + /* XXX Do sg with this int64_t => long => double ? float hell. */ + for (i = 0; i < info.cpu_count; i++) { + int64_t used, total; + int at = i * CPUSTATES; + + used = fresh[at + CP_USER] + fresh[at + CP_NICE] + fresh[at + CP_SYS]; + total = used + fresh[at + CP_IDLE]; + + if ((total - oldtotal[i]) != 0) { + info.cpu_usage[i] = ((double) (used - oldused[i])) / + (double) (total - oldtotal[i]); + } else { + info.cpu_usage[i] = 0; + } + + oldused[i] = used; + oldtotal[i] = total; + } +#endif } -void -update_load_average() +void update_load_average() { double v[3]; + getloadavg(v, 3); info.loadavg[0] = (float) v[0]; @@ -366,13 +440,21 @@ update_load_average() info.loadavg[2] = (float) v[2]; } +#define OBSD_MAX_SENSORS 256 +static struct obsd_sensors_struct { + int device; + float temp[MAXSENSORDEVICES][OBSD_MAX_SENSORS]; + unsigned int fan[MAXSENSORDEVICES][OBSD_MAX_SENSORS]; + float volt[MAXSENSORDEVICES][OBSD_MAX_SENSORS]; +} obsd_sensors; + /* read sensors from sysctl */ void update_obsd_sensors() { int sensor_cnt, dev, numt, mib[5] = { CTL_HW, HW_SENSORS, 0, 0, 0 }; struct sensor sensor; struct sensordev sensordev; - size_t slen,sdlen; + size_t slen, sdlen; enum sensor_type type; slen = sizeof(sensor); @@ -380,38 +462,42 @@ void update_obsd_sensors() sensor_cnt = 0; - dev = obsd_sensors.device; // FIXME: read more than one device + dev = obsd_sensors.device; // FIXME: read more than one device /* for (dev = 0; dev < MAXSENSORDEVICES; dev++) { */ mib[2] = dev; - if(sysctl(mib, 3, &sensordev, &sdlen, NULL, 0) == -1) { - if (errno != ENOENT) + if (sysctl(mib, 3, &sensordev, &sdlen, NULL, 0) == -1) { + if (errno != ENOENT) { warn("sysctl"); + } return; - //continue; + // continue; } for (type = 0; type < SENSOR_MAX_TYPES; type++) { mib[3] = type; for (numt = 0; numt < sensordev.maxnumt[type]; numt++) { mib[4] = numt; - if (sysctl(mib, 5, &sensor, &slen, NULL, 0) - == -1) { - if (errno != ENOENT) + if (sysctl(mib, 5, &sensor, &slen, NULL, 0) == -1) { + if (errno != ENOENT) { warn("sysctl"); + } continue; } - if (sensor.flags & SENSOR_FINVALID) + if (sensor.flags & SENSOR_FINVALID) { continue; + } switch (type) { case SENSOR_TEMP: - obsd_sensors.temp[dev][sensor.numt] = (sensor.value - 273150000) / 1000000.0; + obsd_sensors.temp[dev][sensor.numt] = + (sensor.value - 273150000) / 1000000.0; break; case SENSOR_FANRPM: obsd_sensors.fan[dev][sensor.numt] = sensor.value; break; case SENSOR_VOLTS_DC: - obsd_sensors.volt[dev][sensor.numt] = sensor.value/1000000.0; + obsd_sensors.volt[dev][sensor.numt] = + sensor.value / 1000000.0; break; default: break; @@ -425,16 +511,53 @@ void update_obsd_sensors() init_sensors = 1; } +void parse_obsd_sensor(struct text_object *obj, const char *arg) +{ + if (!isdigit(arg[0]) || atoi(&arg[0]) < 0 + || atoi(&arg[0]) > OBSD_MAX_SENSORS - 1) { + obj->data.l = 0; + NORM_ERR("Invalid sensor number!"); + } else + obj->data.l = atoi(&arg[0]); +} + +void print_obsd_sensors_temp(struct text_object *obj, char *p, int p_max_size) +{ + obsd_sensors.device = sensor_device; + update_obsd_sensors(); + temp_print(p, p_max_size, + obsd_sensors.temp[obsd_sensors.device][obj->data.l], + TEMP_CELSIUS); +} + +void print_obsd_sensors_fan(struct text_object *obj, char *p, int p_max_size) +{ + obsd_sensors.device = sensor_device; + update_obsd_sensors(); + snprintf(p, p_max_size, "%d", + obsd_sensors.fan[obsd_sensors.device][obj->data.l]); +} + +void print_obsd_sensors_volt(struct text_object *obj, char *p, int p_max_size) +{ + obsd_sensors.device = sensor_device; + update_obsd_sensors(); + snprintf(p, p_max_size, "%.2f", + obsd_sensors.volt[obsd_sensors.device][obj->data.l]); +} + /* chipset vendor */ void get_obsd_vendor(char *buf, size_t client_buffer_size) { int mib[2]; + mib[0] = CTL_HW; mib[1] = HW_VENDOR; char vendor[64]; size_t size = sizeof(vendor); - if(sysctl(mib, 2, vendor, &size, NULL, 0) == -1) { - ERR("error reading vendor"); + + if (sysctl(mib, 2, vendor, &size, NULL, 0) == -1) { + NORM_ERR("error reading vendor"); snprintf(buf, client_buffer_size, "unknown"); } else { snprintf(buf, client_buffer_size, "%s", vendor); @@ -445,107 +568,65 @@ void get_obsd_vendor(char *buf, size_t client_buffer_size) void get_obsd_product(char *buf, size_t client_buffer_size) { int mib[2]; + mib[0] = CTL_HW; mib[1] = HW_PRODUCT; char product[64]; size_t size = sizeof(product); - if(sysctl(mib, 2, product, &size, NULL, 0) == -1) { - ERR("error reading product"); + + if (sysctl(mib, 2, product, &size, NULL, 0) == -1) { + NORM_ERR("error reading product"); snprintf(buf, client_buffer_size, "unknown"); } else { snprintf(buf, client_buffer_size, "%s", product); } } -/* rdtsc() and get_freq_dynamic() copied from linux.c */ - -#if defined(__i386) || defined(__x86_64) -__inline__ unsigned long long int -rdtsc() -{ - unsigned long long int x; - __asm__ volatile(".byte 0x0f, 0x31":"=A" (x)); - return (x); -} -#endif - -/* return system frequency in MHz (use divisor=1) or GHz (use divisor=1000) */ -void -get_freq_dynamic(char *p_client_buffer, size_t client_buffer_size, - char *p_format, int divisor) -{ -#if defined(__i386) || defined(__x86_64) - struct timezone tz; - struct timeval tvstart, tvstop; - unsigned long long cycles[2]; /* gotta be 64 bit */ - unsigned int microseconds; /* total time taken */ - - memset(&tz, 0, sizeof (tz)); - - /* get this function in cached memory */ - gettimeofday(&tvstart, &tz); - cycles[0] = rdtsc(); - gettimeofday(&tvstart, &tz); - - /* we don't trust that this is any specific length of time */ - usleep(100); - cycles[1] = rdtsc(); - gettimeofday(&tvstop, &tz); - microseconds = ((tvstop.tv_sec - tvstart.tv_sec) * 1000000) + - (tvstop.tv_usec - tvstart.tv_usec); - - snprintf(p_client_buffer, client_buffer_size, p_format, - (float)((cycles[1] - cycles[0]) / microseconds) / divisor); -#else - get_freq(p_client_buffer, client_buffer_size, p_format, divisor, 1); -#endif -} - -/*void*/ -char -get_freq(char *p_client_buffer, size_t client_buffer_size, - char *p_format, int divisor, unsigned int cpu) +/* void */ +char get_freq(char *p_client_buffer, size_t client_buffer_size, + const char *p_format, int divisor, unsigned int cpu) { int freq = cpu; int mib[2] = { CTL_HW, HW_CPUSPEED }; - - if (!p_client_buffer || client_buffer_size <= 0 || - !p_format || divisor <= 0) + + if (!p_client_buffer || client_buffer_size <= 0 || !p_format + || divisor <= 0) { return 0; + } size_t size = sizeof(freq); - if(sysctl(mib, 2, &freq, &size, NULL, 0) == 0) - snprintf(p_client_buffer, client_buffer_size, - p_format, (float)freq/divisor); - else + + if (sysctl(mib, 2, &freq, &size, NULL, 0) == 0) { + snprintf(p_client_buffer, client_buffer_size, p_format, + (float) freq / divisor); + } else { snprintf(p_client_buffer, client_buffer_size, p_format, 0.0f); + } return 1; } -void -update_top() +void update_top() { + kvm_init(); proc_find_top(info.cpu, info.memu); } #if 0 /* deprecated, will rewrite this soon in update_net_stats() -hifi */ -void -update_wifi_stats() +void update_wifi_stats() { - struct net_stat * ns; + struct net_stat *ns; struct ifaddrs *ifap, *ifa; struct ifmediareq ifmr; struct ieee80211_nodereq nr; struct ieee80211_bssid bssid; - int s,ibssid; + int s, ibssid; - /* - * Get iface table - */ - if (getifaddrs(&ifap) < 0) + /* Get iface table */ + if (getifaddrs(&ifap) < 0) { return; + } for (ifa = ifap; ifa; ifa = ifa->ifa_next) { ns = get_net_stat((const char *) ifa->ifa_name); @@ -555,15 +636,15 @@ update_wifi_stats() /* Get media type */ bzero(&ifmr, sizeof(ifmr)); strlcpy(ifmr.ifm_name, ifa->ifa_name, IFNAMSIZ); - if (ioctl(s, SIOCGIFMEDIA, (caddr_t) &ifmr) < 0) - goto cleanup; - - /* - * We can monitor only wireless interfaces - * which not in hostap mode - */ - if ((ifmr.ifm_active & IFM_IEEE80211) && - !(ifmr.ifm_active & IFM_IEEE80211_HOSTAP)) { + if (ioctl(s, SIOCGIFMEDIA, (caddr_t) &ifmr) < 0) { + close(s); + return; + } + + /* We can monitor only wireless interfaces + * which are not in hostap mode */ + if ((ifmr.ifm_active & IFM_IEEE80211) + && !(ifmr.ifm_active & IFM_IEEE80211_HOSTAP)) { /* Get wi status */ memset(&bssid, 0, sizeof(bssid)); @@ -574,8 +655,9 @@ update_wifi_stats() bcopy(bssid.i_bssid, &nr.nr_macaddr, sizeof(nr.nr_macaddr)); strlcpy(nr.nr_ifname, ifa->ifa_name, sizeof(nr.nr_ifname)); - if (ioctl(s, SIOCG80211NODE, &nr) == 0 && nr.nr_rssi) + if (ioctl(s, SIOCG80211NODE, &nr) == 0 && nr.nr_rssi) { ns->linkstatus = nr.nr_rssi; + } } cleanup: close(s); @@ -583,42 +665,48 @@ cleanup: } #endif -void -update_diskio() +void clear_diskio_stats() { - return; /* XXX implement? hifi: not sure how */ } -/* - * While topless is obviously better, top is also not bad. - */ +struct diskio_stat *prepare_diskio_stat(const char *s) +{ +} + +void update_diskio() +{ + return; /* XXX: implement? hifi: not sure how */ +} + +/* While topless is obviously better, top is also not bad. */ -int -comparecpu(const void *a, const void *b) +int comparecpu(const void *a, const void *b) { - if (((struct process *)a)->amount > ((struct process *)b)->amount) - return (-1); + if (((struct process *) a)->amount > ((struct process *) b)->amount) { + return -1; + } - if (((struct process *)a)->amount < ((struct process *)b)->amount) - return (1); + if (((struct process *) a)->amount < ((struct process *) b)->amount) { + return 1; + } - return (0); + return 0; } -int -comparemem(const void *a, const void *b) +int comparemem(const void *a, const void *b) { - if (((struct process *)a)->totalmem > ((struct process *)b)->totalmem) - return (-1); + if (((struct process *) a)->rss > ((struct process *) b)->rss) { + return -1; + } - if (((struct process *)a)->totalmem < ((struct process *)b)->totalmem) - return (1); + if (((struct process *) a)->rss < ((struct process *) b)->rss) { + return 1; + } - return (0); + return 0; } -inline void -proc_find_top(struct process **cpu, struct process **mem) +inline void proc_find_top(struct process **cpu, struct process **mem) { struct kinfo_proc2 *p; int n_processes; @@ -626,43 +714,44 @@ proc_find_top(struct process **cpu, struct process **mem) struct process *processes; int mib[2]; - int total_pages; + u_int total_pages; + int64_t usermem; int pagesize = getpagesize(); /* we get total pages count again to be sure it is up to date */ mib[0] = CTL_HW; - mib[1] = HW_USERMEM; - size_t size = sizeof(total_pages); - if(sysctl(mib, 2, &total_pages, &size, NULL, 0) == -1) - ERR("error reading nmempages"); + mib[1] = HW_USERMEM64; + size_t size = sizeof(usermem); + + if (sysctl(mib, 2, &usermem, &size, NULL, 0) == -1) { + NORM_ERR("error reading usermem"); + } + + /* translate bytes into page count */ + total_pages = usermem / pagesize; int max_size = sizeof(struct kinfo_proc2); - p = kvm_getproc2(kd, KERN_PROC_ALL, 0, max_size, &n_processes); - processes = malloc(n_processes * sizeof (struct process)); + p = kvm_getproc2(kd, KERN_PROC_ALL, 0, max_size, &n_processes); + processes = malloc(n_processes * sizeof(struct process)); for (i = 0; i < n_processes; i++) { - if (!((p[i].p_flag & P_SYSTEM)) && - p[i].p_comm != NULL) { + if (!((p[i].p_flag & P_SYSTEM)) && p[i].p_comm != NULL) { processes[j].pid = p[i].p_pid; - processes[j].name = strdup(p[i].p_comm); - processes[j].amount = 100.0 * - p[i].p_pctcpu / FSCALE; - processes[j].totalmem = (float)(p[i].p_vm_rssize * pagesize / - (float)total_pages) * 100.0; + processes[j].name = strndup(p[i].p_comm, text_buffer_size); + processes[j].amount = 100.0 * p[i].p_pctcpu / FSCALE; j++; } } - qsort(processes, j - 1, sizeof (struct process), comparemem); + qsort(processes, j - 1, sizeof(struct process), comparemem); for (i = 0; i < 10; i++) { struct process *tmp, *ttmp; - tmp = malloc(sizeof (struct process)); + tmp = malloc(sizeof(struct process)); tmp->pid = processes[i].pid; tmp->amount = processes[i].amount; - tmp->totalmem = processes[i].totalmem; - tmp->name = strdup(processes[i].name); + tmp->name = strndup(processes[i].name, text_buffer_size); ttmp = mem[i]; mem[i] = tmp; @@ -672,15 +761,14 @@ proc_find_top(struct process **cpu, struct process **mem) } } - qsort(processes, j - 1, sizeof (struct process), comparecpu); + qsort(processes, j - 1, sizeof(struct process), comparecpu); for (i = 0; i < 10; i++) { struct process *tmp, *ttmp; - tmp = malloc(sizeof (struct process)); + tmp = malloc(sizeof(struct process)); tmp->pid = processes[i].pid; tmp->amount = processes[i].amount; - tmp->totalmem = processes[i].totalmem; - tmp->name = strdup(processes[i].name); + tmp->name = strndup(processes[i].name, text_buffer_size); ttmp = cpu[i]; cpu[i] = tmp; @@ -690,101 +778,101 @@ proc_find_top(struct process **cpu, struct process **mem) } } - for (i = 0; i < j; free(processes[i++].name)); + for (i = 0; i < j; i++) { + free(processes[i].name); + } free(processes); } -#if defined(i386) || defined(__i386__) +#if defined(i386) || defined(__i386__) #define APMDEV "/dev/apm" #define APM_UNKNOWN 255 -int -apm_getinfo(int fd, apm_info_t aip) +int apm_getinfo(int fd, apm_info_t aip) { - if (ioctl(fd, APM_IOC_GETPOWER, aip) == -1) - return (-1); + if (ioctl(fd, APM_IOC_GETPOWER, aip) == -1) { + return -1; + } - return (0); + return 0; } -char -*get_apm_adapter() +char *get_apm_adapter() { int fd; struct apm_power_info info; char *out; - out = (char *)calloc(16, sizeof (char)); + out = (char *) calloc(16, sizeof(char)); fd = open(APMDEV, O_RDONLY); if (fd < 0) { strncpy(out, "ERR", 16); - return (out); + return out; } if (apm_getinfo(fd, &info) != 0) { close(fd); strncpy(out, "ERR", 16); - return (out); + return out; } close(fd); switch (info.ac_state) { case APM_AC_OFF: strncpy(out, "off-line", 16); - return (out); + return out; break; case APM_AC_ON: if (info.battery_state == APM_BATT_CHARGING) { strncpy(out, "charging", 16); - return (out); + return out; } else { strncpy(out, "on-line", 16); - return (out); + return out; } break; default: strncpy(out, "unknown", 16); - return (out); + return out; break; } } -char -*get_apm_battery_life() +char *get_apm_battery_life() { int fd; u_int batt_life; struct apm_power_info info; char *out; - out = (char *)calloc(16, sizeof (char)); + out = (char *) calloc(16, sizeof(char)); fd = open(APMDEV, O_RDONLY); if (fd < 0) { strncpy(out, "ERR", 16); - return (out); + return out; } if (apm_getinfo(fd, &info) != 0) { close(fd); strncpy(out, "ERR", 16); - return (out); + return out; } close(fd); batt_life = info.battery_life; if (batt_life <= 100) { snprintf(out, 16, "%d%%", batt_life); - return (out); - } else + return out; + } else { strncpy(out, "ERR", 16); + } - return (out); + return out; } -char -*get_apm_battery_time() +char *get_apm_battery_time() { int fd; int batt_time; @@ -792,50 +880,51 @@ char struct apm_power_info info; char *out; - out = (char *)calloc(16, sizeof (char)); + out = (char *) calloc(16, sizeof(char)); fd = open(APMDEV, O_RDONLY); if (fd < 0) { strncpy(out, "ERR", 16); - return (out); + return out; } if (apm_getinfo(fd, &info) != 0) { close(fd); strncpy(out, "ERR", 16); - return (out); + return out; } close(fd); batt_time = info.minutes_left; - if (batt_time == -1) + if (batt_time == -1) { strncpy(out, "unknown", 16); - else { + } else { h = batt_time / 60; m = batt_time % 60; snprintf(out, 16, "%2d:%02d", h, m); } - return (out); + return out; } #endif /* empty stubs so conky links */ -void -prepare_update() +void prepare_update() { - return; } -void update_entropy (void) +int get_entropy_avail(unsigned int *val) { - return; + return 1; +} + +int get_entropy_poolsize(unsigned int *val) +{ + return 1; } -void -free_all_processes(void) +void free_all_processes(void) { - return; }