]> git.sur5r.net Git - i3/i3status/blobdiff - wmiistatus.c
Add code for getting process status (running/not) and load on NetBSD
[i3/i3status] / wmiistatus.c
index bb65690b1987a700ff5985ffb0b128bfe25a9c2e..5c6630201d993683b66eb12b0178ded18f807c60 100644 (file)
@@ -41,6 +41,7 @@
 #include <stdarg.h>
 #include <unistd.h>
 #include <stdlib.h>
+#include <limits.h>
 #include <ctype.h>
 #include <net/if.h>
 #include <sys/ioctl.h>
 #include <glob.h>
 #include <dirent.h>
 #include <getopt.h>
+
+#ifdef LINUX
 #include <linux/ethtool.h>
 #include <linux/sockios.h>
-
+#else
+/* TODO: correctly check for *BSD */
+#include <sys/param.h>
+#include <sys/sysctl.h>
+#include <sys/resource.h>
+#endif
 
 #define _IS_WMIISTATUS_C
 #include "wmiistatus.h"
@@ -294,7 +302,7 @@ static char *get_wireless_info() {
        memset(part, 0, sizeof(part));
 
        if ((fd = open("/proc/net/wireless", O_RDONLY)) == -1)
-               die("Could not open /proc/net/wireless");
+               die("Could not open /proc/net/wireless\n");
        (void)read(fd, buf, sizeof(buf));
        (void)close(fd);
 
@@ -355,6 +363,7 @@ static char *get_eth_info() {
        int ethspeed = 0;
 
        if (get_ethspeed) {
+#ifdef LINUX
                /* This code path requires root privileges */
                struct ifreq ifr;
                struct ethtool_cmd ecmd;
@@ -366,6 +375,7 @@ static char *get_eth_info() {
                if (ioctl(general_socket, SIOCETHTOOL, &ifr) == 0)
                        ethspeed = (ecmd.speed == USHRT_MAX ? 0 : ecmd.speed);
                else get_ethspeed = false;
+#endif
        }
 
        if (ip_address == NULL)
@@ -392,7 +402,7 @@ static bool process_runs(const char *path) {
        memset(pidbuf, 0, sizeof(pidbuf));
 
        if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0)
-               die("glob() failed");
+               die("glob() failed\n");
        fd = open((globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path), O_RDONLY);
        globfree(&globbuf);
        if (fd < 0)
@@ -400,14 +410,23 @@ static bool process_runs(const char *path) {
        (void)read(fd, pidbuf, sizeof(pidbuf));
        (void)close(fd);
 
+#ifdef LINUX
        (void)snprintf(procbuf, sizeof(procbuf), "/proc/%ld", strtol(pidbuf, NULL, 10));
        return (stat(procbuf, &statbuf) >= 0);
+#else
+       /* TODO: correctly check for NetBSD. Evaluate if this runs on OpenBSD/FreeBSD */
+       struct kinfo_proc info;
+       size_t length = sizeof(struct kinfo_proc);
+       int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, strtol(pidbuf, NULL, 10) };
+       if (sysctl(mib, 4, &info, &length, NULL, 0) < 0)
+               return false;
+       return (length != 0);
+#endif
 }
 
 int main(int argc, char *argv[]) {
        char part[512],
-            pathbuf[512],
-            *end;
+            pathbuf[512];
        unsigned int i;
        int load_avg;
 
@@ -415,12 +434,18 @@ int main(int argc, char *argv[]) {
        int o, option_index = 0;
        struct option long_options[] = {
                {"config", required_argument, 0, 'c'},
+               {"help", no_argument, 0, 'h'},
                {0, 0, 0, 0}
        };
 
-       while ((o = getopt_long(argc, argv, "c:", long_options, &option_index)) != -1)
+       while ((o = getopt_long(argc, argv, "c:h", long_options, &option_index)) != -1)
                if ((char)o == 'c')
                        configfile = optarg;
+               else if ((char)o == 'h') {
+                       printf("wmiistatus (c) 2008-2009 Michael Stapelberg\n"
+                               "Syntax: %s [-c <configfile>]\n", argv[0]);
+                       return 0;
+               }
 
        if (load_configuration(configfile) < 0)
                return EXIT_FAILURE;
@@ -428,7 +453,7 @@ int main(int argc, char *argv[]) {
        setup();
 
        if ((general_socket = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
-               die("Could not create socket");
+               die("Could not create socket\n");
 
        while (1) {
                for (i = 0; i < num_run_watches; i += 2) {
@@ -453,12 +478,25 @@ int main(int argc, char *argv[]) {
                        write_to_statusbar(concat(order[ORDER_BATTERY], "battery"), get_battery_info());
 
                /* Get load */
+#ifdef LINUX
                if ((load_avg = open("/proc/loadavg", O_RDONLY)) == -1)
-                       die("Could not open /proc/loadavg");
+                       die("Could not open /proc/loadavg\n");
                (void)read(load_avg, part, sizeof(part));
                (void)close(load_avg);
-               end = skip_character(part, ' ', 3);
-               *end = '\0';
+               *skip_character(part, ' ', 3) = '\0';
+#else
+               /* TODO: correctly check for NetBSD, check if it works the same on *BSD */
+               struct loadavg load;
+               size_t length = sizeof(struct loadavg);
+               int mib[2] = { CTL_VM, VM_LOADAVG };
+               if (sysctl(mib, 2, &load, &length, NULL, 0) < 0)
+                       die("Could not sysctl({ CTL_VM, VM_LOADAVG })\n");
+               double scale = load.fscale;
+               (void)snprintf(part, sizeof(part), "%.02f %.02f %.02f",
+                               (double)load.ldavg[0] / scale,
+                               (double)load.ldavg[1] / scale,
+                               (double)load.ldavg[2] / scale);
+#endif
                write_to_statusbar(concat(order[ORDER_LOAD], "load"), part);
 
                if (time_format) {