]> git.sur5r.net Git - i3/i3status/commitdiff
Implement run_watches
authorMichael Stapelberg <michael+x200@stapelberg.de>
Sat, 4 Oct 2008 17:16:41 +0000 (19:16 +0200)
committerMichael Stapelberg <michael+x200@stapelberg.de>
Sat, 4 Oct 2008 17:16:41 +0000 (19:16 +0200)
config.h
wmiistatus.c
wmiistatus.h

index b8d30aa0b8c82857ef823ed8572ce7ffaea83117..3f55af846cfd3bb9af70994942f71b466c89eea2 100644 (file)
--- a/config.h
+++ b/config.h
@@ -1,9 +1,5 @@
 const char *wlan_interface = "wlan0";
 const char *eth_interface = "eth0";
-
 const char *wmii_path = "/mnt/wmii/rbar/status";
-
-// TODO: run-watches, z.B.
-// "/var/run/dhcp*.pid" --> checkt ob's das file gibt, ob der prozess läuft und zeigt dann yes/no an
-// "/var/run/vpnc*.pid"
-
+const char *run_watches[] = {"DHCP", "/var/run/dhclient*.pid",
+                            "VPN", "/var/run/vpnc*.pid"};
index 269f68135a9d008154532809de906b929e544812..f516849c5cfd8f56b61c0bc9a427672696c8c929 100644 (file)
@@ -46,8 +46,9 @@
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
-#include "wmiistatus.h"
+#include <glob.h>
 
+#include "wmiistatus.h"
 #include "config.h"
 
 static void write_to_statusbar(const char *message) {
@@ -232,15 +233,43 @@ static char *get_eth_info() {
        return part;
 }
 
+/*
+ * Checks if the PID in path is still valid by checking if /proc/<pid> exists
+ *
+ */
+bool process_runs(const char *path) {
+       char pidbuf[512],
+            procbuf[512];
+       static glob_t globbuf;
+       struct stat statbuf;
+
+       if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0)
+               die("glob() failed");
+       const char *real_path = (globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path);
+       int fd = open(real_path, O_RDONLY);
+       globfree(&globbuf);
+       if (fd < 0)
+               return false;
+       read(fd, pidbuf, sizeof(pidbuf));
+       close(fd);
+       sprintf(procbuf, "/proc/%s", pidbuf);
+       return (stat(procbuf, &statbuf) >= 0);
+}
 
 int main(void) {
        char part[512],
             *end;
+       unsigned int i;
 
        while (1) {
                memset(output, '\0', sizeof(output));
                first_push = true;
 
+               for (i = 0; i < sizeof(run_watches) / sizeof(char*); i += 2) {
+                       sprintf(part, "%s: %s", run_watches[i], (process_runs(run_watches[i+1]) ? "yes" : "no"));
+                       push_part(part, strlen(part));
+               }
+
                char *wireless_info = get_wireless_info();
                push_part(wireless_info, strlen(wireless_info));
 
@@ -250,9 +279,10 @@ int main(void) {
                char *battery_info = get_battery_info();
                push_part(battery_info, strlen(battery_info));
 
-               
                /* Get load */
                int load_avg = open("/proc/loadavg", O_RDONLY);
+               if (load_avg == -1)
+                       die("Could not open /proc/loadavg");
                read(load_avg, part, sizeof(part));
                close(load_avg);
                end = skip_character(part, ' ', 3);
@@ -264,8 +294,6 @@ int main(void) {
                strftime(part, sizeof(part), "%d.%m.%Y %H:%M:%S", current_tm);
                push_part(part, strlen(part));
 
-               printf("output = %s\n", output);
-
                int fd = open("/mnt/wmii/rbar/status", O_RDWR);
                write(fd, output, strlen(output));
                close(fd);
index de876ad0067289a02be2f8bc2bd18fc33c84b688..13ec2c773dbded0a84ac9a70bcc3f940cf26a20a 100644 (file)
@@ -13,3 +13,4 @@ static char *get_battery_info(void);
 static char *get_wireless_info(void);
 static char *get_ip_address(const char *interface);
 static char *get_eth_info(void);
+bool process_runs(const char *path);