chiark / gitweb /
*_id: fix zero length in set_str()
[elogind.git] / udevmonitor.c
index 3a71d002f562377aad741d514dfd57c22f6640fa..2775b295ceac9bc18e709d77e6660dce23655612 100644 (file)
@@ -33,6 +33,7 @@
 
 #include "udev.h"
 #include "udevd.h"
+#include "udev_utils.h"
 #include "udev_libc_wrapper.h"
 
 static int uevent_netlink_sock;
@@ -102,42 +103,59 @@ static int init_uevent_netlink_sock(void)
 int main(int argc, char *argv[])
 {
        int env = 0;
-       int maxsockplus;
        fd_set readfds;
-       int retval;
+       int i;
+       int retval = 0;
+
+       for (i = 1 ; i < argc; i++) {
+               char *arg = argv[i];
+               if (strcmp(arg, "--env") == 0 || strcmp(arg, "-e") == 0) {
+                       env = 1;
+               }
+               else if (strcmp(arg, "--help") == 0  || strcmp(arg, "-h") == 0){
+                       printf("Usage: udevmonitor [--env]\n"
+                               "  --env    print the whole event environment\n"
+                               "  --help   print this help text\n\n");
+                       exit(0);
+               } else {
+                       fprintf(stderr, "unknown option\n\n");
+                       exit(1);
+               }
+       }
 
        if (getuid() != 0) {
-               printf("need to be root, exit\n");
+               fprintf(stderr, "need to be root, exit\n\n");
                exit(1);
        }
 
-       if (argc == 2 && strstr(argv[1], "--env"))
-               env = 1;
-
+       retval = init_udev_monitor_socket();
+       if (retval)
+               goto out;
        init_uevent_netlink_sock();
-       init_udev_monitor_socket();
 
-       FD_ZERO(&readfds);
-       FD_SET(uevent_netlink_sock, &readfds);
-       FD_SET(udev_monitor_sock, &readfds);
-       maxsockplus = udev_monitor_sock+1;
+       printf("udevmonitor prints the received event from the kernel [UEVENT]\n"
+              "and the event which udev sends out after rule processing [UDEV]\n\n");
 
        while (1) {
                static char buf[UEVENT_BUFFER_SIZE*2];
                ssize_t buflen;
-               fd_set workreadfds;
+               int fdcount;
 
                buflen = 0;
-               workreadfds = readfds;
-
-               retval = select(maxsockplus, &workreadfds, NULL, NULL, NULL);
-               if (retval < 0) {
+               FD_ZERO(&readfds);
+               if (uevent_netlink_sock > 0)
+                       FD_SET(uevent_netlink_sock, &readfds);
+               if (udev_monitor_sock > 0)
+                       FD_SET(udev_monitor_sock, &readfds);
+
+               fdcount = select(UDEV_MAX(uevent_netlink_sock, udev_monitor_sock)+1, &readfds, NULL, NULL, NULL);
+               if (fdcount < 0) {
                        if (errno != EINTR)
                                fprintf(stderr, "error receiving uevent message\n");
                        continue;
                }
 
-               if (FD_ISSET(uevent_netlink_sock, &workreadfds)) {
+               if ((uevent_netlink_sock > 0) && FD_ISSET(uevent_netlink_sock, &readfds)) {
                        buflen = recv(uevent_netlink_sock, &buf, sizeof(buf), 0);
                        if (buflen <=  0) {
                                fprintf(stderr, "error receiving uevent message\n");
@@ -146,7 +164,7 @@ int main(int argc, char *argv[])
                        printf("UEVENT[%i] %s\n", time(NULL), buf);
                }
 
-               if (FD_ISSET(udev_monitor_sock, &workreadfds)) {
+               if ((udev_monitor_sock > 0) && FD_ISSET(udev_monitor_sock, &readfds)) {
                        buflen = recv(udev_monitor_sock, &buf, sizeof(buf), 0);
                        if (buflen <=  0) {
                                fprintf(stderr, "error receiving udev message\n");
@@ -180,7 +198,11 @@ int main(int argc, char *argv[])
                }
        }
 
-       close(uevent_netlink_sock);
-       close(udev_monitor_sock);
-       exit(1);
+out:
+       if (uevent_netlink_sock > 0)
+               close(uevent_netlink_sock);
+       if (udev_monitor_sock > 0)
+               close(udev_monitor_sock);
+
+       return retval;
 }