Improve curl code.
[monky] / src / netbsd.c
index 5a335f0..d1236bb 100644 (file)
@@ -1,36 +1,35 @@
-/* NetBSD port */
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <fcntl.h>
-#include <time.h>
-#include <unistd.h>
-#include <err.h>
-#include <limits.h>
-#include <paths.h>
-
-#include <kvm.h>
-#include <nlist.h>
-
-#include <sys/time.h>
-#include <sys/param.h>
-#include <sys/sysctl.h>
-#include <sys/types.h>
-#include <sys/user.h>
-#include <sys/socket.h>
-#include <sys/swap.h>
-#include <sys/sched.h>
-#include <sys/envsys.h>
-
-#include <net/if.h>
-
-#include <uvm/uvm_extern.h>
-
-#include <machine/param.h>
-
-#include "conky.h"
-
+/* -*- 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
+ *
+ * All code written since the fork of torsmo is licensed under the GPL
+ *
+ * Please see COPYING for details
+ *
+ * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
+ * 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
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * 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 <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include "netbsd.h"
+#include "net_stat.h"
 
 static kvm_t *kd = NULL;
 int kd_init = 0, nkd_init = 0;
@@ -39,12 +38,13 @@ char errbuf[_POSIX2_LINE_MAX];
 
 static int init_kvm(void)
 {
-       if (kd_init)
+       if (kd_init) {
                return 0;
+       }
 
        kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf);
        if (kd == NULL) {
-               (void) warnx("cannot kvm_openfiles: %s", errbuf);
+               warnx("cannot kvm_openfiles: %s", errbuf);
                return -1;
        }
        kd_init = 1;
@@ -62,25 +62,24 @@ static int swapmode(int *retavail, int *retfree)
        n = swapctl(SWAP_NSWAP, 0, 0);
 
        if (n < 1) {
-               (void) warn("could not get swap information");
+               warn("could not get swap information");
                return 0;
        }
 
        sep = (struct swapent *) malloc(n * (sizeof(*sep)));
 
        if (sep == NULL) {
-               (void) warn("memory allocation failed");
+               warn("memory allocation failed");
                return 0;
        }
 
        if (swapctl(SWAP_STATS, (void *) sep, n) < n) {
-               (void) warn("could not get swap stats");
+               warn("could not get swap stats");
                return 0;
        }
        for (; n > 0; n--) {
                *retavail += (int) dbtob(sep[n - 1].se_nblks);
-               *retfree +=
-                   (int) dbtob(sep[n - 1].se_nblks - sep[n - 1].se_inuse);
+               *retfree += (int) dbtob(sep[n - 1].se_nblks - sep[n - 1].se_inuse);
        }
        *retavail = (int) (*retavail / 1024);
        *retfree = (int) (*retfree / 1024);
@@ -88,7 +87,6 @@ static int swapmode(int *retavail, int *retfree)
        return 1;
 }
 
-
 void prepare_update()
 {
 }
@@ -101,15 +99,20 @@ void update_uptime()
        int size = sizeof(boottime);
 
        if ((sysctl(mib, 2, &boottime, &size, NULL, 0) != -1)
-           && (boottime.tv_sec != 0)) {
-               (void) time(&now);
+                       && (boottime.tv_sec != 0)) {
+               time(&now);
                info.uptime = now - boottime.tv_sec;
        } else {
-               (void) warn("could not get uptime");
+               warn("could not get uptime");
                info.uptime = 0;
        }
 }
 
+int check_mount(char *s)
+{
+       /* stub */
+       return 0;
+}
 
 void update_meminfo()
 {
@@ -120,10 +123,6 @@ void update_meminfo()
        struct uvmexp_sysctl uvmexp;
        size_t size = sizeof(uvmexp);
 
-       info.memmax = info.mem = 0;
-       info.swapmax = info.swap = 0;
-
-
        if (sysctl(mib, 2, &uvmexp, &size, NULL, 0) < 0) {
                warn("could not get memory info");
                return;
@@ -134,12 +133,13 @@ void update_meminfo()
        inactive_pages = uvmexp.inactive;
 
        info.memmax = (total_pages * pagesize) >> 10;
-       info.mem =
-           ((total_pages - free_pages - inactive_pages) * pagesize) >> 10;
+       info.mem = ((total_pages - free_pages - inactive_pages) * pagesize) >> 10;
+       info.memeasyfree = info.memfree = info.memmax - info.mem;
 
        if (swapmode(&swap_avail, &swap_free) >= 0) {
                info.swapmax = swap_avail;
                info.swap = (swap_avail - swap_free);
+               info.swapfree = swap_free;
        }
 }
 
@@ -151,67 +151,65 @@ void update_net_stats()
        struct ifnet_head ifhead;       /* interfaces are in a tail queue */
        u_long ifnetaddr;
        static struct nlist namelist[] = {
-               {"_ifnet"},
-               {NULL},
+               { "_ifnet" },
+               { NULL }
        };
        static kvm_t *nkd;
 
        if (!nkd_init) {
                nkd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
                if (nkd == NULL) {
-                       (void) warnx("cannot kvm_openfiles: %s", errbuf);
-                       (void)
-                           warnx
-                           ("maybe you need to setgid kmem this program?");
+                       warnx("cannot kvm_openfiles: %s", errbuf);
+                       warnx("maybe you need to setgid kmem this program?");
                        return;
                } else if (kvm_nlist(nkd, namelist) != 0) {
-                       (void) warn("cannot kvm_nlist");
+                       warn("cannot kvm_nlist");
                        return;
-               } else
+               } else {
                        nkd_init = 1;
+               }
        }
 
        if (kvm_read(nkd, (u_long) namelist[0].n_value, (void *) &ifhead,
-                    sizeof(ifhead)) < 0) {
-               (void) warn("cannot kvm_read");
+                       sizeof(ifhead)) < 0) {
+               warn("cannot kvm_read");
                return;
        }
 
        /* get delta */
        delta = current_update_time - last_update_time;
-       if (delta <= 0.0001)
+       if (delta <= 0.0001) {
                return;
+       }
 
        for (i = 0, ifnetaddr = (u_long) ifhead.tqh_first;
-            ifnet.if_list.tqe_next && i < 16;
-            ifnetaddr = (u_long) ifnet.if_list.tqe_next, i++) {
+                       ifnet.if_list.tqe_next && i < 16;
+                       ifnetaddr = (u_long) ifnet.if_list.tqe_next, i++) {
 
                struct net_stat *ns;
                long long last_recv, last_trans;
 
-               (void) kvm_read(nkd, (u_long) ifnetaddr, (void *) &ifnet,
-                               sizeof(ifnet));
-               ns = get_net_stat(ifnet.if_xname);
+               kvm_read(nkd, (u_long) ifnetaddr, (void *) &ifnet, sizeof(ifnet));
+               ns = get_net_stat(ifnet.if_xname, NULL, NULL);
                ns->up = 1;
                last_recv = ns->recv;
                last_trans = ns->trans;
 
-               if (ifnet.if_ibytes < ns->last_read_recv)
-                       ns->recv +=
-                           ((long long) 4294967295U -
-                            ns->last_read_recv) + ifnet.if_ibytes;
-               else
+               if (ifnet.if_ibytes < ns->last_read_recv) {
+                       ns->recv += ((long long) 4294967295U - ns->last_read_recv) +
+                               ifnet.if_ibytes;
+               } else {
                        ns->recv += (ifnet.if_ibytes - ns->last_read_recv);
+               }
 
                ns->last_read_recv = ifnet.if_ibytes;
 
-               if (ifnet.if_obytes < ns->last_read_trans)
-                       ns->trans +=
-                           ((long long) 4294967295U -
-                            ns->last_read_trans) + ifnet.if_obytes;
-               else
-                       ns->trans +=
-                           (ifnet.if_obytes - ns->last_read_trans);
+               if (ifnet.if_obytes < ns->last_read_trans) {
+                       ns->trans += ((long long) 4294967295U - ns->last_read_trans) +
+                               ifnet.if_obytes;
+               } else {
+                       ns->trans += (ifnet.if_obytes - ns->last_read_trans);
+               }
 
                ns->last_read_trans = ifnet.if_obytes;
 
@@ -233,11 +231,12 @@ void update_total_processes()
 
        info.procs = 0;
 
-       if (init_kvm() < 0)
+       if (init_kvm() < 0) {
                return;
-       else
-               kvm_getproc2(kd, KERN_PROC_ALL, 0,
-                            sizeof(struct kinfo_proc2), &n_processes);
+       } else {
+               kvm_getproc2(kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc2),
+                       &n_processes);
+       }
 
        info.procs = n_processes;
 }
@@ -250,15 +249,17 @@ void update_running_processes()
 
        info.run_procs = 0;
 
-       if (init_kvm() < 0)
+       if (init_kvm() < 0) {
                return;
-       else {
-               p = kvm_getproc2(kd, KERN_PROC_ALL, 0,
-                                sizeof(struct kinfo_proc2), &n_processes);
-               for (i = 0; i < n_processes; i++)
-                       if (p[i].p_stat == LSRUN || p[i].p_stat == LSIDL ||
-                           p[i].p_stat == LSONPROC)
+       } else {
+               p = kvm_getproc2(kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc2),
+                       &n_processes);
+               for (i = 0; i < n_processes; i++) {
+                       if (p[i].p_stat == LSRUN || p[i].p_stat == LSIDL
+                                       || p[i].p_stat == LSONPROC) {
                                cnt++;
+                       }
+               }
        }
 
        info.run_procs = cnt;
@@ -282,9 +283,9 @@ void update_cpu_usage()
 
        info.cpu_usage = 0;
 
-       if (sysctlbyname("kern.cp_time", &cp_time, &len, NULL, 0) < 0)
-               (void) warn("cannot get kern.cp_time");
-
+       if (sysctlbyname("kern.cp_time", &cp_time, &len, NULL, 0) < 0) {
+               warn("cannot get kern.cp_time");
+       }
 
        fresh.load[0] = cp_time[CP_USER];
        fresh.load[1] = cp_time[CP_NICE];
@@ -293,29 +294,23 @@ void 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];
-
-       if ((total - oldtotal) != 0)
-               info.cpu_usage =
-                   ((double) (used - oldused)) / (double) (total -
-                                                           oldtotal);
-       else
+       total = fresh.load[0] + fresh.load[1] + fresh.load[2] + fresh.load[3];
+
+       if ((total - oldtotal) != 0) {
+               info.cpu_usage = ((double) (used - oldused)) /
+                       (double) (total - oldtotal);
+       } else {
                info.cpu_usage = 0;
+       }
 
        oldused = used;
        oldtotal = total;
-
-}
-
-double get_i2c_info(int *fd, int div, char *devtype)
-{
-       return -1;
 }
 
 void update_load_average()
 {
        double v[3];
+
        getloadavg(v, 3);
 
        info.loadavg[0] = (float) v[0];
@@ -328,41 +323,44 @@ double get_acpi_temperature(int fd)
        return -1;
 }
 
-void get_battery_stuff(char *buf, unsigned int n, const char *bat)
+void get_battery_stuff(char *buf, unsigned int n, const char *bat, int item)
 {
 }
 
-int
-open_i2c_sensor(const char *dev, const char *type, int n, int *div,
-               char *devtype)
-{
-       return -1;
-}
-
 int open_acpi_temperature(const char *name)
 {
        return -1;
 }
 
-void get_acpi_ac_adapter( char * p_client_buffer, size_t client_buffer_size )
+void get_acpi_ac_adapter(char *p_client_buffer, size_t client_buffer_size, const char *adapter)
 {
-       if ( !p_client_buffer || client_buffer_size <= 0 )
+       (void) adapter; // only linux uses this
+
+       if (!p_client_buffer || client_buffer_size <= 0) {
                return;
+       }
 
        /* not implemented */
-       memset(p_client_buffer,0,client_buffer_size);
-
-       return;
+       memset(p_client_buffer, 0, client_buffer_size);
 }
 
-/*char *get_acpi_fan()*/
-void get_acpi_fan( char * p_client_buffer, size_t client_buffer_size )
+/* char *get_acpi_fan() */
+void get_acpi_fan(char *p_client_buffer, size_t client_buffer_size)
 {
-       if ( !p_client_buffer || client_buffer_size <= 0 )
+       if (!p_client_buffer || client_buffer_size <= 0) {
                return;
+       }
 
        /* not implemented */
-       memset(p_client_buffer,0,client_buffer_size);
+       memset(p_client_buffer, 0, client_buffer_size);
+}
 
-       return;
+int get_entropy_avail(unsigned int *val)
+{
+       return 1;
+}
+
+int get_entropy_poolsize(unsigned int *val)
+{
+       return 1;
 }