2 * Copyright (C) 2003-2004 Greg Kroah-Hartman <greg@kroah.com>
3 * Copyright (C) 2004-2008 Kay Sievers <kay.sievers@vrfy.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 #include "udev_rules.h"
34 static int import_uevent_var(struct udev *udev, const char *devpath)
36 char path[UTIL_PATH_SIZE];
37 static char value[4096]; /* must stay, used with putenv */
44 /* read uevent file */
45 util_strlcpy(path, udev_get_sys_path(udev), sizeof(path));
46 util_strlcat(path, devpath, sizeof(path));
47 util_strlcat(path, "/uevent", sizeof(path));
48 fd = open(path, O_RDONLY);
51 size = read(fd, value, sizeof(value));
57 /* import keys into environment */
59 while (key[0] != '\0') {
60 next = strchr(key, '\n');
64 info(udev, "import into environment: '%s'\n", key);
73 int udevadm_test(struct udev *udev, int argc, char *argv[])
76 const char *action = "add";
77 const char *subsystem = NULL;
78 const char *devpath = NULL;
79 struct udevice *udevice;
80 struct sysfs_device *dev;
81 struct udev_rules rules = {};
85 static const struct option options[] = {
86 { "action", required_argument, NULL, 'a' },
87 { "subsystem", required_argument, NULL, 's' },
88 { "force", no_argument, NULL, 'f' },
89 { "help", no_argument, NULL, 'h' },
93 info(udev, "version %s\n", VERSION);
95 /* export log priority to executed programs */
96 if (udev_get_log_priority(udev) > 0) {
99 sprintf(priority, "%i", udev_get_log_priority(udev));
100 setenv("UDEV_LOG", priority, 1);
106 option = getopt_long(argc, argv, "a:s:fh", options, NULL);
110 dbg(udev, "option '%c'\n", option);
122 printf("Usage: udevadm test OPTIONS <devpath>\n"
123 " --action=<string> set action string\n"
124 " --subsystem=<string> set subsystem string\n"
125 " --force don't skip node/link creation\n"
126 " --help print this help text\n\n");
132 devpath = argv[optind];
134 if (devpath == NULL) {
135 fprintf(stderr, "devpath parameter missing\n");
140 printf("This program is for debugging only, it does not run any program,\n"
141 "specified by a RUN key. It may show incorrect results, because\n"
142 "some values may be different, or not available at a simulation run.\n"
145 udev_rules_init(udev, &rules, 0);
147 /* remove /sys if given */
148 if (strncmp(devpath, udev_get_sys_path(udev), strlen(udev_get_sys_path(udev))) == 0)
149 devpath = &devpath[strlen(udev_get_sys_path(udev))];
151 dev = sysfs_device_get(udev, devpath);
153 fprintf(stderr, "unable to open device '%s'\n", devpath);
158 udevice = udev_device_init(udev);
159 if (udevice == NULL) {
160 fprintf(stderr, "error initializing device\n");
165 if (subsystem != NULL)
166 util_strlcpy(dev->subsystem, subsystem, sizeof(dev->subsystem));
168 /* override built-in sysfs device */
170 util_strlcpy(udevice->action, action, sizeof(udevice->action));
171 udevice->devt = udev_device_get_devt(udevice);
173 /* simulate node creation with test flag */
175 udevice->test_run = 1;
177 setenv("DEVPATH", udevice->dev->devpath, 1);
178 setenv("SUBSYSTEM", udevice->dev->subsystem, 1);
179 setenv("ACTION", udevice->action, 1);
180 import_uevent_var(udev, udevice->dev->devpath);
182 info(udev, "looking at device '%s' from subsystem '%s'\n", udevice->dev->devpath, udevice->dev->subsystem);
183 retval = udev_device_event(&rules, udevice);
185 if (udevice->event_timeout >= 0)
186 info(udev, "custom event timeout: %i\n", udevice->event_timeout);
188 if (retval == 0 && !udevice->ignore_device && udev_get_run(udev)) {
189 struct name_entry *name_loop;
191 list_for_each_entry(name_loop, &udevice->run_list, node) {
192 char program[UTIL_PATH_SIZE];
194 util_strlcpy(program, name_loop->name, sizeof(program));
195 udev_rules_apply_format(udevice, program, sizeof(program));
196 info(udev, "run: '%s'\n", program);
199 udev_device_cleanup(udevice);
202 udev_rules_cleanup(&rules);