chiark / gitweb /
build argv[] for builtin commands
authorKay Sievers <kay.sievers@vrfy.org>
Fri, 23 Dec 2011 01:02:44 +0000 (02:02 +0100)
committerKay Sievers <kay.sievers@vrfy.org>
Fri, 23 Dec 2011 01:02:44 +0000 (02:02 +0100)
Makefile.am
test/rules-test.sh
udev/udev-builtin-blkid.c
udev/udev-builtin-input_id.c
udev/udev-builtin-kmod.c
udev/udev-builtin-path_id.c
udev/udev-builtin-usb_id.c
udev/udev-builtin.c
udev/udev-event.c
udev/udev.h
udev/udevadm-test-builtin.c

index 3ac85b95cc7125fd417ae270005ee0fa61d2ac46..eb9ee9313503b36f622724622082459d5a603410 100644 (file)
@@ -284,7 +284,8 @@ libudev_test_libudev_LDADD = libudev/libudev.la
 udev_test_udev_SOURCES = \
        $(udev_common_sources) \
        udev/test-udev.c
-udev_test_udev_LDADD = libudev/libudev-private.la
+udev_test_udev_CFLAGS = $(udev_common_CFLAGS)
+udev_test_udev_LDADD = $(udev_common_LDADD)
 
 # ------------------------------------------------------------------------------
 # firmware - firmware loading
index 9d5a07c19ee9d42e5dbc5be7e0e9ebba480a3969..b98b69aba85e6b3e3112ae9976c7b656ad701396 100755 (executable)
@@ -14,4 +14,4 @@ type python >/dev/null 2>&1 || {
     exit 0
 }
 
-$srcdir/test/rule-syntax-check.py `find $srcdir/rules -type f`
+$srcdir/test/rule-syntax-check.py `find $srcdir/rules -name '*.rules'`
index a787bf2f6c3a948e2c860ebbd04828a950a042e0..04ad6a444db8a4cc30bd90004be20b6a7bdbf7f9 100644 (file)
@@ -105,7 +105,7 @@ static int probe_superblocks(blkid_probe pr)
        return blkid_do_safeprobe(pr);
 }
 
-static int builtin_blkid(struct udev_device *dev, const char *command, bool test)
+static int builtin_blkid(struct udev_device *dev, int argc, char *argv[], bool test)
 {
        char *device = "/dev/sda3";
        int64_t offset = 0;
@@ -167,5 +167,5 @@ const struct udev_builtin udev_builtin_blkid = {
        .name = "blkid",
        .cmd = builtin_blkid,
        .help = "filesystem and partition probing",
-       .run_once = false,
+       .run_once = true,
 };
index 420a87266292c50f47e71cc7db5718757fa45a29..4ef060d899925462811e43e9467ff885c73d22cb 100644 (file)
@@ -181,7 +181,7 @@ static void test_key (struct udev_device *dev,
                udev_builtin_add_property(dev, test, "ID_INPUT_KEYBOARD", "1");
 }
 
-static int builtin_input_id(struct udev_device *dev, const char *command, bool test)
+static int builtin_input_id(struct udev_device *dev, int argc, char *argv[], bool test)
 {
        struct udev_device *pdev;
        unsigned long bitmask_ev[NBITS(EV_MAX)];
index 90f9c564ed4c40d800979db33abdd1a5fded2d44..4e8ec653fe90abd99779cb44a66bd5764e5d42e0 100644 (file)
 
 static char *kmod;
 
-static int builtin_kmod(struct udev_device *dev, const char *command, bool test)
+static int builtin_kmod(struct udev_device *dev, int argc, char *argv[], bool test)
 {
-       printf("soon we load a module here: '%s'\n", command);
+       struct udev *udev = udev_device_get_udev(dev);
+
+       if (argc < 3) {
+               err(udev, "missing command + argument\n");
+               return EXIT_FAILURE;
+       }
+
+       printf("soon we '%s' the module '%s' (%i) here\n", argv[1], argv[2], argc);
        printf("test: %s\n", kmod);
        return EXIT_SUCCESS;
 }
 
 static int builtin_kmod_load(struct udev *udev)
 {
-       printf("load module index\n");
-       asprintf(&kmod, "pid: %u\n", getpid());
+       info(udev, "load module index\n");
+       asprintf(&kmod, "pid: %u", getpid());
        return 0;
 }
 
 static int builtin_kmod_unload(struct udev *udev)
 {
-       printf("unload module index\n");
+       info(udev, "unload module index\n");
        free(kmod);
        return 0;
 }
index 97a9ace5c156236d7a893da11bbaf4d0dbabd58a..18af12f29a43dc666a21a8c830c00a489a8979b1 100644 (file)
@@ -390,7 +390,7 @@ out:
        return parent;
 }
 
-static int builtin_path_id(struct udev_device *dev, const char *command, bool test)
+static int builtin_path_id(struct udev_device *dev, int argc, char *argv[], bool test)
 {
        struct udev_device *parent;
        char *path = NULL;
index 9a519179c88bd1f0088ebb354bbb9289bca85d3a..21c3c03d8ace0b67447e422f119a7d7e433a5558 100644 (file)
@@ -239,7 +239,7 @@ out:
  * 6.) If the device supplies a serial number, this number
  *     is concatenated with the identification with an underscore '_'.
  */
-static int builtin_usb_id(struct udev_device *dev, const char *command, bool test)
+static int builtin_usb_id(struct udev_device *dev, int argc, char *argv[], bool test)
 {
        char vendor_str[64];
        char vendor_str_enc[256];
index 9f2172769938be02ac061031c0d0da569fb80df0..f17b04f67e5900c3d4cf61a6a9c0aecdb4863b6b 100644 (file)
@@ -90,7 +90,13 @@ enum udev_builtin_cmd udev_builtin_lookup(const char *command)
 
 int udev_builtin_run(struct udev_device *dev, enum udev_builtin_cmd cmd, const char *command, bool test)
 {
-       return builtins[cmd]->cmd(dev, command, test);
+       char arg[UTIL_PATH_SIZE];
+       int argc;
+       char *argv[128];
+
+       util_strscpy(arg, sizeof(arg), command);
+       udev_build_argv(udev_device_get_udev(dev), arg, &argc, argv);
+       return builtins[cmd]->cmd(dev, argc, argv, test);
 }
 
 int udev_builtin_add_property(struct udev_device *dev, bool test, const char *key, const char *val, ...)
index c978e847b7237fd9660ba41c3f632cece593c776..e10d48e13be467f267ab46bc5710a36073edb9f3 100644 (file)
@@ -698,6 +698,41 @@ out:
        return err;
 }
 
+int udev_build_argv(struct udev *udev, char *cmd, int *argc, char *argv[])
+{
+       int i = 0;
+       char *pos;
+
+       if (strchr(cmd, ' ') == NULL) {
+               argv[i++] = cmd;
+               goto out;
+       }
+
+       pos = cmd;
+       while (pos != NULL && pos[0] != '\0') {
+               if (pos[0] == '\'') {
+                       /* do not separate quotes */
+                       pos++;
+                       argv[i] = strsep(&pos, "\'");
+                       if (pos != NULL)
+                               while (pos[0] == ' ')
+                                       pos++;
+               } else {
+                       argv[i] = strsep(&pos, " ");
+                       if (pos != NULL)
+                               while (pos[0] == ' ')
+                                       pos++;
+               }
+               dbg(udev, "argv[%i] '%s'\n", i, argv[i]);
+               i++;
+       }
+out:
+       argv[i] = NULL;
+       if (argc)
+               *argc = i;
+       return 0;
+}
+
 int udev_event_spawn(struct udev_event *event,
                     const char *cmd, char **envp, const sigset_t *sigmask,
                     char *result, size_t ressize)
@@ -707,39 +742,12 @@ int udev_event_spawn(struct udev_event *event,
        int errpipe[2] = {-1, -1};
        pid_t pid;
        char arg[UTIL_PATH_SIZE];
+       char *argv[128];
        char program[UTIL_PATH_SIZE];
-       char *argv[((sizeof(arg) + 1) / 2) + 1];
-       int i;
        int err = 0;
 
-       /* build argv from command */
        util_strscpy(arg, sizeof(arg), cmd);
-       i = 0;
-       if (strchr(arg, ' ') != NULL) {
-               char *pos = arg;
-
-               while (pos != NULL && pos[0] != '\0') {
-                       if (pos[0] == '\'') {
-                               /* do not separate quotes */
-                               pos++;
-                               argv[i] = strsep(&pos, "\'");
-                               if (pos != NULL)
-                                       while (pos[0] == ' ')
-                                               pos++;
-                       } else {
-                               argv[i] = strsep(&pos, " ");
-                               if (pos != NULL)
-                                       while (pos[0] == ' ')
-                                               pos++;
-                       }
-                       dbg(udev, "arg[%i] '%s'\n", i, argv[i]);
-                       i++;
-               }
-               argv[i] = NULL;
-       } else {
-               argv[0] = arg;
-               argv[1] = NULL;
-       }
+       udev_build_argv(event->udev, arg, NULL, argv);
 
        /* pipes from child to parent */
        if (result != NULL || udev_get_log_priority(udev) >= LOG_INFO) {
index a07c525a89431ec7f12a57273e8023218014b462..d5445e84e6d1dcd171e1d1776711d6163fcdbdb4 100644 (file)
@@ -81,6 +81,7 @@ int udev_event_spawn(struct udev_event *event,
                     char *result, size_t ressize);
 int udev_event_execute_rules(struct udev_event *event, struct udev_rules *rules, const sigset_t *sigset);
 int udev_event_execute_run(struct udev_event *event, const sigset_t *sigset);
+int udev_build_argv(struct udev *udev, char *cmd, int *argc, char *argv[]);
 
 /* udev-watch.c */
 int udev_watch_init(struct udev *udev);
@@ -156,7 +157,7 @@ enum udev_builtin_cmd {
 };
 struct udev_builtin {
        const char *name;
-       int (*cmd)(struct udev_device *dev, const char *command, bool test);
+       int (*cmd)(struct udev_device *dev, int argc, char *argv[], bool test);
        const char *help;
        int (*load)(struct udev *udev);
        int (*unload)(struct udev *udev);
index 18f04bf888f36bf207d1b8d25743e9d860aa7211..ea1bcbcb07d17de17c9a7a51e4a5d6a375c3b036 100644 (file)
@@ -70,6 +70,7 @@ static int adm_builtin(struct udev *udev, int argc, char *argv[])
                        goto out;
                }
        }
+
        command = argv[optind++];
        if (command == NULL) {
                fprintf(stderr, "command missing\n");
@@ -85,6 +86,16 @@ static int adm_builtin(struct udev *udev, int argc, char *argv[])
                goto out;
        }
 
+       udev_builtin_load(udev);
+
+       cmd = udev_builtin_lookup(command);
+       if (cmd >= UDEV_BUILTIN_MAX) {
+               fprintf(stderr, "unknown command '%s'\n", command);
+               help(udev);
+               rc = 5;
+               goto out;
+       }
+
        /* add /sys if needed */
        if (strncmp(syspath, udev_get_sys_path(udev), strlen(udev_get_sys_path(udev))) != 0)
                util_strscpyl(filename, sizeof(filename), udev_get_sys_path(udev), syspath, NULL);
@@ -99,16 +110,6 @@ static int adm_builtin(struct udev *udev, int argc, char *argv[])
                goto out;
        }
 
-       udev_builtin_load(udev);
-
-       cmd = udev_builtin_lookup(command);
-       if (cmd >= UDEV_BUILTIN_MAX) {
-               fprintf(stderr, "unknown command '%s'\n", command);
-               help(udev);
-               rc = 5;
-               goto out;
-       }
-
        if (udev_builtin_run(dev, cmd, command, true) < 0) {
                fprintf(stderr, "error executing '%s'\n\n", command);
                rc = 6;