chiark / gitweb /
udevadm: split out find_device helper
[elogind.git] / src / udev / udevadm-trigger.c
index 0ee27bb40873d3569f5d3c8a195bf2fdb4139087..49e256289c717229da094b8f92f4b91a1ad2a6f2 100644 (file)
@@ -24,7 +24,6 @@
 #include <errno.h>
 #include <dirent.h>
 #include <fcntl.h>
-#include <syslog.h>
 #include <fnmatch.h>
 #include <sys/stat.h>
 #include <sys/types.h>
 
 #include "udev.h"
 #include "udev-util.h"
+#include "udevadm-util.h"
 #include "util.h"
 
 static int verbose;
 static int dry_run;
 
-static void exec_list(struct udev_enumerate *udev_enumerate, const char *action)
-{
+static void exec_list(struct udev_enumerate *udev_enumerate, const char *action) {
         struct udev_list_entry *entry;
 
         udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(udev_enumerate)) {
@@ -60,8 +59,7 @@ static void exec_list(struct udev_enumerate *udev_enumerate, const char *action)
         }
 }
 
-static const char *keyval(const char *str, const char **val, char *buf, size_t size)
-{
+static const char *keyval(const char *str, const char **val, char *buf, size_t size) {
         char *pos;
 
         strscpy(buf, size,str);
@@ -93,8 +91,7 @@ static void help(void) {
                "  -h,--help\n\n");
 }
 
-static int adm_trigger(struct udev *udev, int argc, char *argv[])
-{
+static int adm_trigger(struct udev *udev, int argc, char *argv[]) {
         static const struct option options[] = {
                 { "verbose",           no_argument,       NULL, 'v' },
                 { "dry-run",           no_argument,       NULL, 'n' },
@@ -178,25 +175,18 @@ static int adm_trigger(struct udev *udev, int argc, char *argv[])
                         udev_enumerate_add_match_sysname(udev_enumerate, optarg);
                         break;
                 case 'b': {
-                        char path[UTIL_PATH_SIZE];
-                        struct udev_device *dev;
-
-                        /* add sys dir if needed */
-                        if (!startswith(optarg, "/sys"))
-                                strscpyl(path, sizeof(path), "/sys", optarg, NULL);
-                        else
-                                strscpy(path, sizeof(path), optarg);
-                        util_remove_trailing_chars(path, '/');
-                        dev = udev_device_new_from_syspath(udev, path);
+                        _cleanup_udev_device_unref_ struct udev_device *dev;
+
+                        dev = find_device(udev, optarg, "/sys");
                         if (dev == NULL) {
                                 log_error("unable to open the device '%s'", optarg);
                                 return 2;
                         }
+
                         udev_enumerate_add_match_parent(udev_enumerate, dev);
-                        /* drop reference immediately, enumerate pins the device as long as needed */
-                        udev_device_unref(dev);
                         break;
                 }
+
                 case 'h':
                         help();
                         return 0;