chiark / gitweb /
udevadm: modernization
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 18 Dec 2013 02:49:07 +0000 (21:49 -0500)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 19 Dec 2013 04:58:23 +0000 (23:58 -0500)
src/shared/udev-util.h
src/udev/udevadm-control.c
src/udev/udevadm-info.c
src/udev/udevadm-monitor.c
src/udev/udevadm-settle.c
src/udev/udevadm-test.c
src/udev/udevadm-trigger.c

index 27677af..40f8b77 100644 (file)
@@ -29,9 +29,16 @@ DEFINE_TRIVIAL_CLEANUP_FUNC(struct udev_device*, udev_device_unref);
 DEFINE_TRIVIAL_CLEANUP_FUNC(struct udev_enumerate*, udev_enumerate_unref);
 DEFINE_TRIVIAL_CLEANUP_FUNC(struct udev_event*, udev_event_unref);
 DEFINE_TRIVIAL_CLEANUP_FUNC(struct udev_rules*, udev_rules_unref);
+DEFINE_TRIVIAL_CLEANUP_FUNC(struct udev_ctrl*, udev_ctrl_unref);
+DEFINE_TRIVIAL_CLEANUP_FUNC(struct udev_monitor*, udev_monitor_unref);
+DEFINE_TRIVIAL_CLEANUP_FUNC(struct udev_queue*, udev_queue_unref);
 
 #define _cleanup_udev_unref_ _cleanup_(udev_unrefp)
 #define _cleanup_udev_device_unref_ _cleanup_(udev_device_unrefp)
 #define _cleanup_udev_enumerate_unref_ _cleanup_(udev_enumerate_unrefp)
 #define _cleanup_udev_event_unref_ _cleanup_(udev_event_unrefp)
 #define _cleanup_udev_rules_unref_ _cleanup_(udev_rules_unrefp)
+#define _cleanup_udev_ctrl_unref_ _cleanup_(udev_ctrl_unrefp)
+#define _cleanup_udev_monitor_unref_ _cleanup_(udev_monitor_unrefp)
+#define _cleanup_udev_queue_unref_ _cleanup_(udev_queue_unrefp)
+#define _cleanup_udev_list_cleanup_ _cleanup_(udev_list_cleanup)
index 00a909f..3a6c8ef 100644 (file)
@@ -26,6 +26,7 @@
 #include <sys/un.h>
 
 #include "udev.h"
+#include "udev-util.h"
 
 static void print_help(void)
 {
@@ -43,7 +44,7 @@ static void print_help(void)
 
 static int adm_control(struct udev *udev, int argc, char *argv[])
 {
-        struct udev_ctrl *uctrl = NULL;
+        _cleanup_udev_ctrl_unref_ struct udev_ctrl *uctrl = NULL;
         int timeout = 60;
         int rc = 1, c;
 
@@ -85,7 +86,7 @@ static int adm_control(struct udev *udev, int argc, char *argv[])
                         i = util_log_priority(optarg);
                         if (i < 0) {
                                 fprintf(stderr, "invalid number '%s'\n", optarg);
-                                goto out;
+                                return rc;
                         }
                         if (udev_ctrl_send_set_log_level(uctrl, util_log_priority(optarg), timeout) < 0)
                                 rc = 2;
@@ -114,7 +115,7 @@ static int adm_control(struct udev *udev, int argc, char *argv[])
                 case 'p':
                         if (strchr(optarg, '=') == NULL) {
                                 fprintf(stderr, "expect <KEY>=<value> instead of '%s'\n", optarg);
-                                goto out;
+                                return rc;
                         }
                         if (udev_ctrl_send_set_env(uctrl, optarg, timeout) < 0)
                                 rc = 2;
@@ -128,7 +129,7 @@ static int adm_control(struct udev *udev, int argc, char *argv[])
                         i = strtoul(optarg, &endp, 0);
                         if (endp[0] != '\0' || i < 1) {
                                 fprintf(stderr, "invalid number '%s'\n", optarg);
-                                goto out;
+                                return rc;
                         }
                         if (udev_ctrl_send_set_children_max(uctrl, i, timeout) < 0)
                                 rc = 2;
@@ -156,8 +157,6 @@ static int adm_control(struct udev *udev, int argc, char *argv[])
                 fprintf(stderr, "Extraneous argument: %s\n", argv[optind]);
         else if (optind == 1)
                 fprintf(stderr, "Option missing\n");
-out:
-        udev_ctrl_unref(uctrl);
         return rc;
 }
 
index 2d2cf43..64bb537 100644 (file)
@@ -30,6 +30,7 @@
 #include <sys/types.h>
 
 #include "udev.h"
+#include "udev-util.h"
 
 static bool skip_attribute(const char *name)
 {
@@ -296,13 +297,13 @@ static struct udev_device *find_device(struct udev *udev, const char *id, const
 
 static int uinfo(struct udev *udev, int argc, char *argv[])
 {
-        struct udev_device *device = NULL;
+        _cleanup_udev_device_unref_ struct udev_device *device = NULL;
         bool root = 0;
         bool export = 0;
         const char *export_prefix = NULL;
         char name[UTIL_PATH_SIZE];
         struct udev_list_entry *list_entry;
-        int rc = 0, c;
+        int c;
 
         static const struct option options[] = {
                 { "name",              required_argument, NULL, 'n' },
@@ -360,48 +361,43 @@ static int uinfo(struct udev *udev, int argc, char *argv[])
                 case 'n': {
                         if (device != NULL) {
                                 fprintf(stderr, "device already specified\n");
-                                rc = 2;
-                                goto exit;
+                                return 2;
                         }
 
                         device = find_device(udev, optarg, "/dev/");
                         if (device == NULL) {
                                 fprintf(stderr, "device node not found\n");
-                                rc = 2;
-                                goto exit;
+                                return 2;
                         }
                         break;
                 }
                 case 'p':
                         if (device != NULL) {
                                 fprintf(stderr, "device already specified\n");
-                                rc = 2;
-                                goto exit;
+                                return 2;
                         }
 
                         device = find_device(udev, optarg, "/sys");
                         if (device == NULL) {
                                 fprintf(stderr, "syspath not found\n");
-                                rc = 2;
-                                goto exit;
+                                return 2;
                         }
                         break;
                 case 'q':
                         action = ACTION_QUERY;
-                        if (streq(optarg, "property") || streq(optarg, "env")) {
+                        if (streq(optarg, "property") || streq(optarg, "env"))
                                 query = QUERY_PROPERTY;
-                        } else if (streq(optarg, "name")) {
+                        else if (streq(optarg, "name"))
                                 query = QUERY_NAME;
-                        } else if (streq(optarg, "symlink")) {
+                        else if (streq(optarg, "symlink"))
                                 query = QUERY_SYMLINK;
-                        } else if (streq(optarg, "path")) {
+                        else if (streq(optarg, "path"))
                                 query = QUERY_PATH;
-                        } else if (streq(optarg, "all")) {
+                        else if (streq(optarg, "all"))
                                 query = QUERY_ALL;
-                        else {
+                        else {
                                 fprintf(stderr, "unknown query type\n");
-                                rc = 3;
-                                goto exit;
+                                return 3;
                         }
                         break;
                 case 'r':
@@ -416,10 +412,10 @@ static int uinfo(struct udev *udev, int argc, char *argv[])
                         break;
                 case 'e':
                         export_devices(udev);
-                        goto exit;
+                        return 0;
                 case 'c':
                         cleanup_db(udev);
-                        goto exit;
+                        return 0;
                 case 'x':
                         export = true;
                         break;
@@ -428,13 +424,12 @@ static int uinfo(struct udev *udev, int argc, char *argv[])
                         break;
                 case 'V':
                         printf("%s\n", VERSION);
-                        goto exit;
+                        return 0;
                 case 'h':
                         printf("%s\n", usage);
-                        goto exit;
+                        return 0;
                 default:
-                        rc = 1;
-                        goto exit;
+                        return 1;
                 }
 
         switch (action) {
@@ -442,14 +437,12 @@ static int uinfo(struct udev *udev, int argc, char *argv[])
                 if (!device) {
                         if (!argv[optind]) {
                                 fprintf(stderr, "%s\n", usage);
-                                rc = 2;
-                                goto exit;
+                                return 2;
                         }
                         device = find_device(udev, argv[optind], NULL);
                         if (!device) {
                                 fprintf(stderr, "Unknown device, --name=, --path=, or absolute path in /dev/ or /sys expected.\n");
-                                rc = 4;
-                                goto exit;
+                                return 4;
                         }
                 }
 
@@ -459,8 +452,7 @@ static int uinfo(struct udev *udev, int argc, char *argv[])
 
                         if (node == NULL) {
                                 fprintf(stderr, "no device node found\n");
-                                rc = 5;
-                                goto exit;
+                                return 5;
                         }
 
                         if (root)
@@ -484,7 +476,7 @@ static int uinfo(struct udev *udev, int argc, char *argv[])
                         break;
                 case QUERY_PATH:
                         printf("%s\n", udev_device_get_devpath(device));
-                        goto exit;
+                        return 0;
                 case QUERY_PROPERTY:
                         list_entry = udev_device_get_properties_list_entry(device);
                         while (list_entry != NULL) {
@@ -515,26 +507,22 @@ static int uinfo(struct udev *udev, int argc, char *argv[])
                         device = find_device(udev, argv[optind], NULL);
                         if (!device) {
                                 fprintf(stderr, "Unknown device, absolute path in /dev/ or /sys expected.\n");
-                                rc = 4;
-                                goto exit;
+                                return 4;
                         }
                 }
                 if (!device) {
                         fprintf(stderr, "Unknown device, --name=, --path=, or absolute path in /dev/ or /sys expected.\n");
-                        rc = 4;
-                        goto exit;
+                        return 4;
                 }
                 print_device_chain(device);
                 break;
         case ACTION_DEVICE_ID_FILE:
                 if (stat_device(name, export, export_prefix) != 0)
-                        rc = 1;
+                        return 1;
                 break;
         }
 
-exit:
-        udev_device_unref(device);
-        return rc;
+        return 0;
 }
 
 const struct udevadm_cmd udevadm_info = {
index 2c200b7..94e76da 100644 (file)
@@ -33,6 +33,7 @@
 #include <linux/netlink.h>
 
 #include "udev.h"
+#include "udev-util.h"
 
 static bool udev_exit;
 
@@ -78,18 +79,17 @@ static int adm_monitor(struct udev *udev, int argc, char *argv[])
 {
         struct sigaction act = {};
         sigset_t mask;
-        int option;
         bool prop = false;
         bool print_kernel = false;
         bool print_udev = false;
-        struct udev_list subsystem_match_list;
-        struct udev_list tag_match_list;
-        struct udev_monitor *udev_monitor = NULL;
-        struct udev_monitor *kernel_monitor = NULL;
-        int fd_ep = -1;
+        _cleanup_udev_list_cleanup_ struct udev_list subsystem_match_list;
+        _cleanup_udev_list_cleanup_ struct udev_list tag_match_list;
+        _cleanup_udev_monitor_unref_ struct udev_monitor *udev_monitor = NULL;
+        _cleanup_udev_monitor_unref_ struct udev_monitor *kernel_monitor = NULL;
+        _cleanup_close_ int fd_ep = -1;
         int fd_kernel = -1, fd_udev = -1;
         struct epoll_event ep_kernel, ep_udev;
-        int rc = 0, c;
+        int c;
 
         static const struct option options[] = {
                 { "property",        no_argument,       NULL, 'p' },
@@ -136,10 +136,9 @@ static int adm_monitor(struct udev *udev, int argc, char *argv[])
                         break;
                 case 'h':
                         help();
-                        goto out;
+                        return 0;
                 default:
-                        rc = 1;
-                        goto out;
+                        return 1;
                 }
 
         if (!print_kernel && !print_udev) {
@@ -160,7 +159,7 @@ static int adm_monitor(struct udev *udev, int argc, char *argv[])
         fd_ep = epoll_create1(EPOLL_CLOEXEC);
         if (fd_ep < 0) {
                 log_error("error creating epoll fd: %m\n");
-                goto out;
+                return 1;
         }
 
         printf("monitor will print the received events for:\n");
@@ -170,8 +169,7 @@ static int adm_monitor(struct udev *udev, int argc, char *argv[])
                 udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
                 if (udev_monitor == NULL) {
                         fprintf(stderr, "error: unable to create netlink socket\n");
-                        rc = 1;
-                        goto out;
+                        return 1;
                 }
                 udev_monitor_set_receive_buffer_size(udev_monitor, 128*1024*1024);
                 fd_udev = udev_monitor_get_fd(udev_monitor);
@@ -193,8 +191,7 @@ static int adm_monitor(struct udev *udev, int argc, char *argv[])
 
                 if (udev_monitor_enable_receiving(udev_monitor) < 0) {
                         fprintf(stderr, "error: unable to subscribe to udev events\n");
-                        rc = 2;
-                        goto out;
+                        return 2;
                 }
 
                 memset(&ep_udev, 0, sizeof(struct epoll_event));
@@ -202,7 +199,7 @@ static int adm_monitor(struct udev *udev, int argc, char *argv[])
                 ep_udev.data.fd = fd_udev;
                 if (epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_udev, &ep_udev) < 0) {
                         log_error("fail to add fd to epoll: %m\n");
-                        goto out;
+                        return 2;
                 }
 
                 printf("UDEV - the event which udev sends out after rule processing\n");
@@ -214,8 +211,7 @@ static int adm_monitor(struct udev *udev, int argc, char *argv[])
                 kernel_monitor = udev_monitor_new_from_netlink(udev, "kernel");
                 if (kernel_monitor == NULL) {
                         fprintf(stderr, "error: unable to create netlink socket\n");
-                        rc = 3;
-                        goto out;
+                        return 3;
                 }
                 udev_monitor_set_receive_buffer_size(kernel_monitor, 128*1024*1024);
                 fd_kernel = udev_monitor_get_fd(kernel_monitor);
@@ -229,8 +225,7 @@ static int adm_monitor(struct udev *udev, int argc, char *argv[])
 
                 if (udev_monitor_enable_receiving(kernel_monitor) < 0) {
                         fprintf(stderr, "error: unable to subscribe to kernel events\n");
-                        rc = 4;
-                        goto out;
+                        return 4;
                 }
 
                 memset(&ep_kernel, 0, sizeof(struct epoll_event));
@@ -238,7 +233,7 @@ static int adm_monitor(struct udev *udev, int argc, char *argv[])
                 ep_kernel.data.fd = fd_kernel;
                 if (epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_kernel, &ep_kernel) < 0) {
                         log_error("fail to add fd to epoll: %m\n");
-                        goto out;
+                        return 5;
                 }
 
                 printf("KERNEL - the kernel uevent\n");
@@ -277,14 +272,8 @@ static int adm_monitor(struct udev *udev, int argc, char *argv[])
                         }
                 }
         }
-out:
-        if (fd_ep >= 0)
-                close(fd_ep);
-        udev_monitor_unref(udev_monitor);
-        udev_monitor_unref(kernel_monitor);
-        udev_list_cleanup(&subsystem_match_list);
-        udev_list_cleanup(&tag_match_list);
-        return rc;
+
+        return 0;
 }
 
 const struct udevadm_cmd udevadm_monitor = {
index 362c3b2..5da0be5 100644 (file)
@@ -35,6 +35,7 @@
 #include <sys/types.h>
 
 #include "udev.h"
+#include "udev-util.h"
 #include "util.h"
 
 static void help(void) {
@@ -65,7 +66,7 @@ static int adm_settle(struct udev *udev, int argc, char *argv[])
         const char *exists = NULL;
         unsigned int timeout = 120;
         struct pollfd pfd[1] = { {.fd = -1}, };
-        struct udev_queue *udev_queue = NULL;
+        _cleanup_udev_queue_unref_ struct udev_queue *udev_queue = NULL;
         int rc = EXIT_FAILURE, c;
 
         while ((c = getopt_long(argc, argv, "s:e:t:E:qh", options, NULL)) >= 0)
@@ -229,7 +230,6 @@ static int adm_settle(struct udev *udev, int argc, char *argv[])
 out:
         if (pfd[0].fd >= 0)
                 close(pfd[0].fd);
-        udev_queue_unref(udev_queue);
         return rc;
 }
 
index cd75fad..a9f9caf 100644 (file)
@@ -30,6 +30,7 @@
 #include <sys/signalfd.h>
 
 #include "udev.h"
+#include "udev-util.h"
 
 static int adm_test(struct udev *udev, int argc, char *argv[])
 {
@@ -37,10 +38,10 @@ static int adm_test(struct udev *udev, int argc, char *argv[])
         char filename[UTIL_PATH_SIZE];
         const char *action = "add";
         const char *syspath = NULL;
-        struct udev_event *event = NULL;
-        struct udev_device *dev = NULL;
-        struct udev_rules *rules = NULL;
         struct udev_list_entry *entry;
+        _cleanup_udev_rules_unref_ struct udev_rules *rules = NULL;
+        _cleanup_udev_device_unref_ struct udev_device *dev = NULL;
+        _cleanup_udev_event_unref_ struct udev_event *event = NULL;
         sigset_t mask, sigmask_orig;
         int err;
         int rc = 0, c;
@@ -154,9 +155,6 @@ static int adm_test(struct udev *udev, int argc, char *argv[])
 out:
         if (event != NULL && event->fd_signal >= 0)
                 close(event->fd_signal);
-        udev_event_unref(event);
-        udev_device_unref(dev);
-        udev_rules_unref(rules);
         udev_builtin_exit(udev);
         return rc;
 }
index 3608c35..a898ef9 100644 (file)
@@ -32,6 +32,7 @@
 #include <sys/un.h>
 
 #include "udev.h"
+#include "udev-util.h"
 #include "util.h"
 
 static int verbose;
@@ -115,15 +116,12 @@ static int adm_trigger(struct udev *udev, int argc, char *argv[])
                 TYPE_SUBSYSTEMS,
         } device_type = TYPE_DEVICES;
         const char *action = "change";
-        struct udev_enumerate *udev_enumerate;
-        int rc = 0;
+        _cleanup_udev_enumerate_unref_ struct udev_enumerate *udev_enumerate = NULL;
         int c;
 
         udev_enumerate = udev_enumerate_new(udev);
-        if (udev_enumerate == NULL) {
-                rc = 1;
-                goto exit;
-        }
+        if (udev_enumerate == NULL)
+                return 1;
 
         while ((c = getopt_long(argc, argv, "vno:t:c:s:S:a:A:p:g:y:b:h", options, NULL)) >= 0) {
                 const char *key;
@@ -138,24 +136,22 @@ static int adm_trigger(struct udev *udev, int argc, char *argv[])
                         dry_run = 1;
                         break;
                 case 't':
-                        if (streq(optarg, "devices")) {
+                        if (streq(optarg, "devices"))
                                 device_type = TYPE_DEVICES;
-                        } else if (streq(optarg, "subsystems")) {
+                        else if (streq(optarg, "subsystems"))
                                 device_type = TYPE_SUBSYSTEMS;
-                        else {
+                        else {
                                 log_error("unknown type --type=%s\n", optarg);
-                                rc = 2;
-                                goto exit;
+                                return 2;
                         }
                         break;
                 case 'c':
                         if (!nulstr_contains("add\0" "remove\0" "change\0", optarg)) {
                                 log_error("unknown action '%s'\n", optarg);
-                                rc = 2;
-                                goto exit;
-                        } else {
+                                return 2;
+                        } else
                                 action = optarg;
-                        }
+
                         break;
                 case 's':
                         udev_enumerate_add_match_subsystem(udev_enumerate, optarg);
@@ -194,8 +190,7 @@ static int adm_trigger(struct udev *udev, int argc, char *argv[])
                         dev = udev_device_new_from_syspath(udev, path);
                         if (dev == NULL) {
                                 log_error("unable to open the device '%s'\n", optarg);
-                                rc = 2;
-                                goto exit;
+                                return 2;
                         }
                         udev_enumerate_add_match_parent(udev_enumerate, dev);
                         /* drop reference immediately, enumerate pins the device as long as needed */
@@ -204,10 +199,9 @@ static int adm_trigger(struct udev *udev, int argc, char *argv[])
                 }
                 case 'h':
                         help();
-                        goto exit;
+                        return 0;
                 case '?':
-                        rc = 1;
-                        goto exit;
+                        return 1;
                 default:
                         assert_not_reached("Unknown option");
                 }
@@ -222,17 +216,14 @@ static int adm_trigger(struct udev *udev, int argc, char *argv[])
         case TYPE_SUBSYSTEMS:
                 udev_enumerate_scan_subsystems(udev_enumerate);
                 exec_list(udev_enumerate, action);
-                goto exit;
+                return 0;
         case TYPE_DEVICES:
                 udev_enumerate_scan_devices(udev_enumerate);
                 exec_list(udev_enumerate, action);
-                goto exit;
+                return 0;
         default:
                 assert_not_reached("device_type");
         }
-exit:
-        udev_enumerate_unref(udev_enumerate);
-        return rc;
 }
 
 const struct udevadm_cmd udevadm_trigger = {