chiark / gitweb /
udevadm,scsi_id: add short options to help strings and to the man page
[elogind.git] / src / udev / udevadm-monitor.c
index 5997dd8e18ae70185575ac4ae445056a77eab299..2c200b70683f5a727c4bbef2d5ffe2f5f4fe2b28 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2004-2010 Kay Sievers <kay.sievers@vrfy.org>
+ * Copyright (C) 2004-2010 Kay Sievers <kay@vrfy.org>
  *
  * 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
@@ -64,9 +64,19 @@ static void print_device(struct udev_device *device, const char *source, int pro
         }
 }
 
+static void help(void) {
+        printf("Usage: udevadm monitor [--property] [--kernel] [--udev] [--help]\n"
+               "  -p,--property                            print the event properties\n"
+               "  -k,--kernel                              print kernel uevents\n"
+               "  -u,--udev                                print udev events\n"
+               "  -s,--subsystem-match=SUBSYSTEM[/DEVTYPE] filter events by subsystem\n"
+               "  -t,--tag-match=TAG                       filter events by tag\n"
+               "  -h,--help\n\n");
+}
+
 static int adm_monitor(struct udev *udev, int argc, char *argv[])
 {
-        struct sigaction act;
+        struct sigaction act = {};
         sigset_t mask;
         int option;
         bool prop = false;
@@ -79,28 +89,24 @@ static int adm_monitor(struct udev *udev, int argc, char *argv[])
         int fd_ep = -1;
         int fd_kernel = -1, fd_udev = -1;
         struct epoll_event ep_kernel, ep_udev;
-        int rc = 0;
+        int rc = 0, c;
 
         static const struct option options[] = {
-                { "property", no_argument, NULL, 'p' },
-                { "environment", no_argument, NULL, 'e' },
-                { "kernel", no_argument, NULL, 'k' },
-                { "udev", no_argument, NULL, 'u' },
+                { "property",        no_argument,       NULL, 'p' },
+                { "environment",     no_argument,       NULL, 'e' }, /* alias for -p */
+                { "kernel",          no_argument,       NULL, 'k' },
+                { "udev",            no_argument,       NULL, 'u' },
                 { "subsystem-match", required_argument, NULL, 's' },
-                { "tag-match", required_argument, NULL, 't' },
-                { "help", no_argument, NULL, 'h' },
+                { "tag-match",       required_argument, NULL, 't' },
+                { "help",            no_argument,       NULL, 'h' },
                 {}
         };
 
         udev_list_init(udev, &subsystem_match_list, true);
         udev_list_init(udev, &tag_match_list, true);
 
-        for (;;) {
-                option = getopt_long(argc, argv, "pekus:t:h", options, NULL);
-                if (option == -1)
-                        break;
-
-                switch (option) {
+        while((c = getopt_long(argc, argv, "pekus:t:h", options, NULL)) >= 0)
+                switch (c) {
                 case 'p':
                 case 'e':
                         prop = true;
@@ -116,7 +122,7 @@ static int adm_monitor(struct udev *udev, int argc, char *argv[])
                                 char subsys[UTIL_NAME_SIZE];
                                 char *devtype;
 
-                                util_strscpy(subsys, sizeof(subsys), optarg);
+                                strscpy(subsys, sizeof(subsys), optarg);
                                 devtype = strchr(subsys, '/');
                                 if (devtype != NULL) {
                                         devtype[0] = '\0';
@@ -129,19 +135,12 @@ static int adm_monitor(struct udev *udev, int argc, char *argv[])
                         udev_list_entry_add(&tag_match_list, optarg, NULL);
                         break;
                 case 'h':
-                        printf("Usage: udevadm monitor [--property] [--kernel] [--udev] [--help]\n"
-                               "  --property                              print the event properties\n"
-                               "  --kernel                                print kernel uevents\n"
-                               "  --udev                                  print udev events\n"
-                               "  --subsystem-match=<subsystem[/devtype]> filter events by subsystem\n"
-                               "  --tag-match=<tag>                       filter events by tag\n"
-                               "  --help\n\n");
+                        help();
                         goto out;
                 default:
                         rc = 1;
                         goto out;
                 }
-        }
 
         if (!print_kernel && !print_udev) {
                 print_kernel = true;
@@ -149,9 +148,7 @@ static int adm_monitor(struct udev *udev, int argc, char *argv[])
         }
 
         /* set signal handlers */
-        memset(&act, 0x00, sizeof(struct sigaction));
         act.sa_handler = sig_handler;
-        sigemptyset(&act.sa_mask);
         act.sa_flags = SA_RESTART;
         sigaction(SIGINT, &act, NULL);
         sigaction(SIGTERM, &act, NULL);
@@ -162,7 +159,7 @@ static int adm_monitor(struct udev *udev, int argc, char *argv[])
 
         fd_ep = epoll_create1(EPOLL_CLOEXEC);
         if (fd_ep < 0) {
-                err(udev, "error creating epoll fd: %m\n");
+                log_error("error creating epoll fd: %m\n");
                 goto out;
         }
 
@@ -204,7 +201,7 @@ static int adm_monitor(struct udev *udev, int argc, char *argv[])
                 ep_udev.events = EPOLLIN;
                 ep_udev.data.fd = fd_udev;
                 if (epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_udev, &ep_udev) < 0) {
-                        err(udev, "fail to add fd to epoll: %m\n");
+                        log_error("fail to add fd to epoll: %m\n");
                         goto out;
                 }
 
@@ -240,7 +237,7 @@ static int adm_monitor(struct udev *udev, int argc, char *argv[])
                 ep_kernel.events = EPOLLIN;
                 ep_kernel.data.fd = fd_kernel;
                 if (epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_kernel, &ep_kernel) < 0) {
-                        err(udev, "fail to add fd to epoll: %m\n");
+                        log_error("fail to add fd to epoll: %m\n");
                         goto out;
                 }
 
@@ -253,7 +250,7 @@ static int adm_monitor(struct udev *udev, int argc, char *argv[])
                 struct epoll_event ev[4];
                 int i;
 
-                fdcount = epoll_wait(fd_ep, ev, ARRAY_SIZE(ev), -1);
+                fdcount = epoll_wait(fd_ep, ev, ELEMENTSOF(ev), -1);
                 if (fdcount < 0) {
                         if (errno != EINTR)
                                 fprintf(stderr, "error receiving uevent message: %m\n");