chiark / gitweb /
rule-generator: net - whitelist NICs that violate MAC local scheme
[elogind.git] / udev / udevadm-test.c
index d06249c6c1d1f3e01d2b7e087932909e80aef672..c53c27329556e8a8e659262f6d9c36ab7979b183 100644 (file)
@@ -1,20 +1,19 @@
 /*
  * Copyright (C) 2003-2004 Greg Kroah-Hartman <greg@kroah.com>
- * Copyright (C) 2004-2006 Kay Sievers <kay.sievers@vrfy.org>
+ * Copyright (C) 2004-2008 Kay Sievers <kay.sievers@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 the
- *     Free Software Foundation version 2 of the License.
- * 
- *     This program is distributed in the hope that it will be useful, but
- *     WITHOUT ANY WARRANTY; without even the implied warranty of
- *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- *     General Public License for more details.
- * 
- *     You should have received a copy of the GNU General Public License along
- *     with this program; if not, write to the Free Software Foundation, Inc.,
- *     51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ * 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
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
  *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
 #include <stdlib.h>
 #include <getopt.h>
 
 #include "udev.h"
-#include "udev_rules.h"
-
-static int import_uevent_var(const char *devpath)
-{
-       char path[PATH_SIZE];
-       static char value[4096]; /* must stay, used with putenv */
-       ssize_t size;
-       int fd;
-       char *key;
-       char *next;
-       int rc = -1;
-
-       /* read uevent file */
-       strlcpy(path, sysfs_path, sizeof(path));
-       strlcat(path, devpath, sizeof(path));
-       strlcat(path, "/uevent", sizeof(path));
-       fd = open(path, O_RDONLY);
-       if (fd < 0)
-               goto out;
-       size = read(fd, value, sizeof(value));
-       close(fd);
-       if (size < 0)
-               goto out;
-       value[size] = '\0';
-
-       /* import keys into environment */
-       key = value;
-       while (key[0] != '\0') {
-               next = strchr(key, '\n');
-               if (next == NULL)
-                       goto out;
-               next[0] = '\0';
-               info("import into environment: '%s'\n", key);
-               putenv(key);
-               key = &next[1];
-       }
-       rc = 0;
-out:
-       return rc;
-}
 
-int udevadm_test(int argc, char *argv[])
+int udevadm_test(struct udev *udev, int argc, char *argv[])
 {
-       int force = 0;
+       char filename[UTIL_PATH_SIZE];
        const char *action = "add";
-       const char *subsystem = NULL;
-       const char *devpath = NULL;
-       struct udevice *udev;
-       struct sysfs_device *dev;
-       struct udev_rules rules = {};
-       int retval;
+       const char *syspath = NULL;
+       struct udev_event *event;
+       struct udev_device *dev;
+       struct udev_rules *rules = NULL;
+       int err;
        int rc = 0;
 
        static const struct option options[] = {
-               { "action", 1, NULL, 'a' },
-               { "subsystem", 1, NULL, 's' },
-               { "force", 0, NULL, 'f' },
-               { "help", 0, NULL, 'h' },
+               { "action", required_argument, NULL, 'a' },
+               { "help", no_argument, NULL, 'h' },
                {}
        };
 
-       info("version %s\n", VERSION);
-       if (udev_log_priority < LOG_INFO) {
-               char priority[32];
-
-               udev_log_priority = LOG_INFO;
-               sprintf(priority, "%i", udev_log_priority);
-               setenv("UDEV_LOG", priority, 1);
-       }
+       info(udev, "version %s\n", VERSION);
 
        while (1) {
                int option;
@@ -107,32 +56,24 @@ int udevadm_test(int argc, char *argv[])
                if (option == -1)
                        break;
 
-               dbg("option '%c'\n", option);
+               dbg(udev, "option '%c'\n", option);
                switch (option) {
                case 'a':
                        action = optarg;
                        break;
-               case 's':
-                       subsystem = optarg;
-                       break;
-               case 'f':
-                       force = 1;
-                       break;
                case 'h':
-                       printf("Usage: udevadm test OPTIONS <devpath>\n"
+                       printf("Usage: udevadm test OPTIONS <syspath>\n"
                               "  --action=<string>     set action string\n"
-                              "  --subsystem=<string>  set subsystem string\n"
-                              "  --force               don't skip node/link creation\n"
-                              "  --help                print this help text\n\n");
+                              "  --help\n\n");
                        exit(0);
                default:
                        exit(1);
                }
        }
-       devpath = argv[optind];
+       syspath = argv[optind];
 
-       if (devpath == NULL) {
-               fprintf(stderr, "devpath parameter missing\n");
+       if (syspath == NULL) {
+               fprintf(stderr, "syspath parameter missing\n");
                rc = 1;
                goto exit;
        }
@@ -142,63 +83,54 @@ int udevadm_test(int argc, char *argv[])
               "some values may be different, or not available at a simulation run.\n"
               "\n");
 
-       udev_rules_init(&rules, 0);
+       rules = udev_rules_new(udev, 1);
+       if (rules == NULL) {
+               fprintf(stderr, "error reading rules\n");
+               rc = 1;
+               goto exit;
+       }
 
-       /* remove /sys if given */
-       if (strncmp(devpath, sysfs_path, strlen(sysfs_path)) == 0)
-               devpath = &devpath[strlen(sysfs_path)];
+       /* add /sys if needed */
+       if (strncmp(syspath, udev_get_sys_path(udev), strlen(udev_get_sys_path(udev))) != 0) {
+               util_strlcpy(filename, udev_get_sys_path(udev), sizeof(filename));
+               util_strlcat(filename, syspath, sizeof(filename));
+       } else {
+               util_strlcpy(filename, syspath, sizeof(filename));
+       }
+       util_remove_trailing_chars(filename, '/');
 
-       dev = sysfs_device_get(devpath);
+       dev = udev_device_new_from_syspath(udev, filename);
        if (dev == NULL) {
-               fprintf(stderr, "unable to open device '%s'\n", devpath);
+               fprintf(stderr, "unable to open device '%s'\n", filename);
                rc = 2;
                goto exit;
        }
 
-       udev = udev_device_init();
-       if (udev == NULL) {
-               fprintf(stderr, "error initializing device\n");
-               rc = 3;
-               goto exit;
-       }
-
-       if (subsystem != NULL)
-               strlcpy(dev->subsystem, subsystem, sizeof(dev->subsystem));
-
-       /* override built-in sysfs device */
-       udev->dev = dev;
-       strlcpy(udev->action, action, sizeof(udev->action));
-       udev->devt = udev_device_get_devt(udev);
-
-       /* simulate node creation with test flag */
-       if (!force)
-               udev->test_run = 1;
+       /* skip reading of db, but read kernel parameters */
+       udev_device_set_info_loaded(dev);
+       udev_device_read_uevent_file(dev);
 
-       setenv("DEVPATH", udev->dev->devpath, 1);
-       setenv("SUBSYSTEM", udev->dev->subsystem, 1);
-       setenv("ACTION", udev->action, 1);
-       import_uevent_var(udev->dev->devpath);
+       udev_device_set_action(dev, action);
+       event = udev_event_new(dev);
+       err = udev_event_execute_rules(event, rules);
 
-       info("looking at device '%s' from subsystem '%s'\n", udev->dev->devpath, udev->dev->subsystem);
-       retval = udev_device_event(&rules, udev);
+       if (udev_device_get_event_timeout(dev) >= 0)
+               info(udev, "custom event timeout: %i\n", udev_device_get_event_timeout(dev));
 
-       if (udev->event_timeout >= 0)
-               info("custom event timeout: %i\n", udev->event_timeout);
+       if (err == 0 && !event->ignore_device && udev_get_run(udev)) {
+               struct udev_list_entry *entry;
 
-       if (retval == 0 && !udev->ignore_device && udev_run) {
-               struct name_entry *name_loop;
+               udev_list_entry_foreach(entry, udev_list_get_entry(&event->run_list)) {
+                       char program[UTIL_PATH_SIZE];
 
-               list_for_each_entry(name_loop, &udev->run_list, node) {
-                       char program[PATH_SIZE];
-
-                       strlcpy(program, name_loop->name, sizeof(program));
-                       udev_rules_apply_format(udev, program, sizeof(program));
-                       info("run: '%s'\n", program);
+                       util_strlcpy(program, udev_list_entry_get_name(entry), sizeof(program));
+                       udev_event_apply_format(event, program, sizeof(program));
+                       info(udev, "run: '%s'\n", program);
                }
        }
-       udev_device_cleanup(udev);
-
+       udev_event_unref(event);
+       udev_device_unref(dev);
 exit:
-       udev_rules_cleanup(&rules);
+       udev_rules_unref(rules);
        return rc;
 }