2 * Copyright (C) 2003-2004 Greg Kroah-Hartman <greg@kroah.com>
3 * Copyright (C) 2004-2006 Kay Sievers <kay.sievers@vrfy.org>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation version 2 of the License.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
33 #include "udev_rules.h"
35 static int import_uevent_var(const char *devpath)
38 static char value[4096]; /* must stay, used with putenv */
45 /* read uevent file */
46 strlcpy(path, sysfs_path, sizeof(path));
47 strlcat(path, devpath, sizeof(path));
48 strlcat(path, "/uevent", sizeof(path));
49 fd = open(path, O_RDONLY);
52 size = read(fd, value, sizeof(value));
58 /* import keys into environment */
60 while (key[0] != '\0') {
61 next = strchr(key, '\n');
65 info("import into environment: '%s'\n", key);
74 int udevtest(int argc, char *argv[], char *envp[])
77 const char *action = "add";
78 const char *subsystem = NULL;
79 const char *devpath = NULL;
81 struct sysfs_device *dev;
82 struct udev_rules rules = {};
86 static const struct option options[] = {
87 { "action", 1, NULL, 'a' },
88 { "subsystem", 1, NULL, 's' },
89 { "force", 0, NULL, 'f' },
90 { "help", 0, NULL, 'h' },
94 info("version %s\n", VERSION);
96 if (udev_log_priority < LOG_INFO) {
99 udev_log_priority = LOG_INFO;
100 sprintf(priority, "%i", udev_log_priority);
101 setenv("UDEV_LOG", priority, 1);
107 option = getopt_long(argc, argv, "a:s:fh", options, NULL);
111 dbg("option '%c'\n", option);
123 printf("Usage: udevadm test OPTIONS <devpath>\n"
124 " --action=<string> set action string\n"
125 " --subsystem=<string> set subsystem string\n"
126 " --force don't skip node/link creation\n"
127 " --help print this help text\n\n");
133 devpath = argv[optind];
135 if (devpath == NULL) {
136 fprintf(stderr, "devpath parameter missing\n");
141 printf("This program is for debugging only, it does not run any program,\n"
142 "specified by a RUN key. It may show incorrect results, because\n"
143 "some values may be different, or not available at a simulation run.\n"
147 udev_rules_init(&rules, 0);
149 /* remove /sys if given */
150 if (strncmp(devpath, sysfs_path, strlen(sysfs_path)) == 0)
151 devpath = &devpath[strlen(sysfs_path)];
153 dev = sysfs_device_get(devpath);
155 fprintf(stderr, "unable to open device '%s'\n", devpath);
160 udev = udev_device_init(NULL);
162 fprintf(stderr, "error initializing device\n");
167 if (subsystem != NULL)
168 strlcpy(dev->subsystem, subsystem, sizeof(dev->subsystem));
170 /* override built-in sysfs device */
172 strlcpy(udev->action, action, sizeof(udev->action));
173 udev->devt = udev_device_get_devt(udev);
175 /* simulate node creation with test flag */
179 setenv("DEVPATH", udev->dev->devpath, 1);
180 setenv("SUBSYSTEM", udev->dev->subsystem, 1);
181 setenv("ACTION", udev->action, 1);
182 import_uevent_var(udev->dev->devpath);
184 info("looking at device '%s' from subsystem '%s'\n", udev->dev->devpath, udev->dev->subsystem);
185 retval = udev_device_event(&rules, udev);
187 if (udev->event_timeout >= 0)
188 info("custom event timeout: %i\n", udev->event_timeout);
190 if (retval == 0 && !udev->ignore_device && udev_run) {
191 struct name_entry *name_loop;
193 list_for_each_entry(name_loop, &udev->run_list, node) {
194 char program[PATH_SIZE];
196 strlcpy(program, name_loop->name, sizeof(program));
197 udev_rules_apply_format(udev, program, sizeof(program));
198 info("run: '%s'\n", program);
201 udev_device_cleanup(udev);
204 udev_rules_cleanup(&rules);