Better burst and memory handling. - rfkilld - An rfkill daemon, which runs scripts according to rfkill events.
       
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) LICENSE
       ---
 (DIR) commit e74a86dd3c2964d55aface077c27eb09165d8ca3
 (DIR) parent de4759d9b5515141c7097f4a118da29e2d86c7ae
 (HTM) Author: Christoph Lohmann <20h@r-36.net>
       Date:   Fri,  8 Apr 2011 20:09:59 +0200
       
       Better burst and memory handling.
       
       Diffstat:
         config.mk                           |       6 +++---
         rfkilld.c                           |      43 ++++++++++++++++++++++++++-----
       
       2 files changed, 39 insertions(+), 10 deletions(-)
       ---
 (DIR) diff --git a/config.mk b/config.mk
       @@ -1,11 +1,11 @@
        # rfkilld metadata
        NAME = rfkilld
       -VERSION = 0.2
       +VERSION = 0.3
        
        # Customize below to fit your system
        
        # paths
       -PREFIX ?= /usr
       +PREFIX ?= /usr/local
        MANPREFIX = ${PREFIX}/share/man
        
        # includes and libs
       @@ -14,7 +14,7 @@ LIBS = -L/usr/lib -lc -ludev
        
        # flags
        CPPFLAGS = -DVERSION=\"${VERSION}\"
       -CFLAGS = -g -std=c99 -pedantic -Wall -O0 ${INCS} ${CPPFLAGS}
       +CFLAGS = -g -std=gnu99 -pedantic -Wall -O0 ${INCS} ${CPPFLAGS}
        LDFLAGS = -g ${LIBS}
        #LDFLAGS = -s ${LIBS}
        
 (DIR) diff --git a/rfkilld.c b/rfkilld.c
       @@ -3,7 +3,6 @@
         * by 20h
         */
        
       -#define _XOPEN_SOURCE
        #include <unistd.h>
        #include <stdio.h>
        #include <stdlib.h>
       @@ -24,6 +23,9 @@
        
        char *argv0;
        char *etcdir = "/etc/rfkilld";
       +char *lastname = NULL;
       +char *lasttype = NULL;
       +char *laststate = NULL;
        int running = 1;
        int dolog = 0;
        
       @@ -58,7 +60,7 @@ runifexecutable(char *file, char *oname, char *ostate)
                                if (!fork())
                                        if(execl(cmd, name, state, NULL) < 0)
                                                perror("execl");
       -                        exit(EXIT_SUCCESS);
       +                        exit(0);
                        }
                        waitpid(pid, NULL, 0);
                }
       @@ -75,12 +77,30 @@ runscripts(struct udev_device *dev)
                name = (char *)udev_device_get_property_value(dev, "RFKILL_TYPE");
                state = (char *)udev_device_get_property_value(dev, "RFKILL_STATE");
        
       +        if (lasttype != NULL && lastname != NULL && laststate != NULL) {
       +                if (!strcmp(lasttype, type) && !strcmp(lastname, name)
       +                                && !strcmp(laststate, state))
       +                        goto runscriptshandlecleanup;
       +        }
       +
                if (dolog)
                        syslog(LOG_NOTICE, "name: %s; type: %s; state: %s;\n",
                                        name, type, state);
        
                runifexecutable(name, type, state);
                runifexecutable(type, name, state);
       +
       +runscriptshandlecleanup:
       +        if (lasttype != NULL)
       +                free(lasttype);
       +        if (lastname != NULL)
       +                free(lastname);
       +        if (laststate != NULL)
       +                free(laststate);
       +
       +        lasttype = strdup(type);
       +        lastname = strdup(name);
       +        laststate = strdup(state);
        }
        
        void
       @@ -119,9 +139,10 @@ initsignals(void)
        void
        usage(void)
        {
       -        fprintf(stderr, "usage: %s [-hbl] [-e etcdir]\n", argv0);
       +        fprintf(stderr, "usage: %s [-hbl] [-e etcdir]\n",
       +                        argv0);
                fflush(stderr);
       -        exit(EXIT_FAILURE);
       +        exit(1);
        }
        
        int
       @@ -160,7 +181,7 @@ main(int argc, char *argv[])
                udev = udev_new();
                if (!udev) {
                        perror("udev_new");
       -                exit(EXIT_FAILURE);
       +                exit(1);
                }
        
                mon = udev_monitor_new_from_netlink(udev, "kernel");
       @@ -171,7 +192,7 @@ main(int argc, char *argv[])
                fds[0].events = POLLIN|POLLPRI;
                if (setnonblocking(fds[0].fd)) {
                        perror("setnonblocking");
       -                exit(EXIT_FAILURE);
       +                exit(1);
                }
                while(running) {
                        ret = poll(fds, 1, -1);
       @@ -187,8 +208,16 @@ main(int argc, char *argv[])
                        }
                }
        
       +        udev_monitor_unref(mon);
       +        udev_unref(udev);
                if (dolog)
                        closelog();
       -        exit(EXIT_SUCCESS);
       +        if (lasttype != NULL)
       +                free(lasttype);
       +        if (lastname != NULL)
       +                free(lastname);
       +        if (laststate != NULL)
       +                free(laststate);
       +        exit(0);
        }