chiark / gitweb /
udevadm-trigger: add parameters checking
[elogind.git] / src / udev / udevadm-trigger.c
index 3cce23dfb220149b928df5021ec62b1c491b7fa9..d10ca59cca82333d0142e20be4f565f101510e16 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2008-2009 Kay Sievers <kay.sievers@vrfy.org>
+ * Copyright (C) 2008-2009 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
 #include <sys/un.h>
 
 #include "udev.h"
+#include "util.h"
 
 static int verbose;
 static int dry_run;
 
 static void exec_list(struct udev_enumerate *udev_enumerate, const char *action)
 {
-        struct udev *udev = udev_enumerate_get_udev(udev_enumerate);
         struct udev_list_entry *entry;
 
         udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(udev_enumerate)) {
@@ -49,14 +49,12 @@ static void exec_list(struct udev_enumerate *udev_enumerate, const char *action)
                         printf("%s\n", udev_list_entry_get_name(entry));
                 if (dry_run)
                         continue;
-                util_strscpyl(filename, sizeof(filename), udev_list_entry_get_name(entry), "/uevent", NULL);
+                strscpyl(filename, sizeof(filename), udev_list_entry_get_name(entry), "/uevent", NULL);
                 fd = open(filename, O_WRONLY);
-                if (fd < 0) {
-                        dbg(udev, "error on opening %s: %m\n", filename);
+                if (fd < 0)
                         continue;
-                }
                 if (write(fd, action, strlen(action)) < 0)
-                        info(udev, "error writing '%s' to '%s': %m\n", action, filename);
+                        log_debug("error writing '%s' to '%s': %m\n", action, filename);
                 close(fd);
         }
 }
@@ -65,7 +63,7 @@ static const char *keyval(const char *str, const char **val, char *buf, size_t s
 {
         char *pos;
 
-        util_strscpy(buf, size,str);
+        strscpy(buf, size,str);
         pos = strchr(buf, '=');
         if (pos != NULL) {
                 pos[0] = 0;
@@ -101,7 +99,6 @@ static int adm_trigger(struct udev *udev, int argc, char *argv[])
         struct udev_enumerate *udev_enumerate;
         int rc = 0;
 
-        dbg(udev, "version %s\n", VERSION);
         udev_enumerate = udev_enumerate_new(udev);
         if (udev_enumerate == NULL) {
                 rc = 1;
@@ -115,8 +112,14 @@ static int adm_trigger(struct udev *udev, int argc, char *argv[])
                 char buf[UTIL_PATH_SIZE];
 
                 option = getopt_long(argc, argv, "vng:o:t:hc:p:s:S:a:A:y:b:", options, NULL);
-                if (option == -1)
+                if (option == -1) {
+                        if (optind < argc) {
+                                fprintf(stderr, "Extraneous argument: '%s'\n", argv[optind]);
+                                rc = 1;
+                                goto exit;
+                        }
                         break;
+                }
 
                 switch (option) {
                 case 'v':
@@ -126,18 +129,24 @@ static int adm_trigger(struct udev *udev, int argc, char *argv[])
                         dry_run = 1;
                         break;
                 case 't':
-                        if (strcmp(optarg, "devices") == 0) {
+                        if (streq(optarg, "devices")) {
                                 device_type = TYPE_DEVICES;
-                        } else if (strcmp(optarg, "subsystems") == 0) {
+                        } else if (streq(optarg, "subsystems")) {
                                 device_type = TYPE_SUBSYSTEMS;
                         } else {
-                                err(udev, "unknown type --type=%s\n", optarg);
+                                log_error("unknown type --type=%s\n", optarg);
                                 rc = 2;
                                 goto exit;
                         }
                         break;
                 case 'c':
-                        action = optarg;
+                        if (!nulstr_contains("add\0" "remove\0" "change\0", optarg)) {
+                                log_error("unknown action '%s'\n", optarg);
+                                rc = 2;
+                                goto exit;
+                        } else {
+                                action = optarg;
+                        }
                         break;
                 case 's':
                         udev_enumerate_add_match_subsystem(udev_enumerate, optarg);
@@ -168,14 +177,14 @@ static int adm_trigger(struct udev *udev, int argc, char *argv[])
                         struct udev_device *dev;
 
                         /* add sys dir if needed */
-                        if (strncmp(optarg, udev_get_sys_path(udev), strlen(udev_get_sys_path(udev))) != 0)
-                                util_strscpyl(path, sizeof(path), udev_get_sys_path(udev), optarg, NULL);
+                        if (!startswith(optarg, "/sys"))
+                                strscpyl(path, sizeof(path), "/sys", optarg, NULL);
                         else
-                                util_strscpy(path, sizeof(path), optarg);
+                                strscpy(path, sizeof(path), optarg);
                         util_remove_trailing_chars(path, '/');
                         dev = udev_device_new_from_syspath(udev, path);
                         if (dev == NULL) {
-                                err(udev, "unable to open the device '%s'\n", optarg);
+                                log_error("unable to open the device '%s'\n", optarg);
                                 rc = 2;
                                 goto exit;
                         }
@@ -218,7 +227,7 @@ static int adm_trigger(struct udev *udev, int argc, char *argv[])
                 exec_list(udev_enumerate, action);
                 goto exit;
         default:
-                goto exit;
+                assert_not_reached("device_type");
         }
 exit:
         udev_enumerate_unref(udev_enumerate);