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"
37 void log_message (int priority, const char *format, ...)
41 if (priority > udev_log_priority)
44 va_start(args, format);
45 vprintf(format, args);
47 if (format[strlen(format)-1] != '\n')
52 static int import_uevent_var(const char *devpath)
55 static char value[4096]; /* must stay, used with putenv */
62 /* read uevent file */
63 strlcpy(path, sysfs_path, sizeof(path));
64 strlcat(path, devpath, sizeof(path));
65 strlcat(path, "/uevent", sizeof(path));
66 fd = open(path, O_RDONLY);
69 size = read(fd, value, sizeof(value));
75 /* import keys into environment */
77 while (key[0] != '\0') {
78 next = strchr(key, '\n');
82 info("import into environment: '%s'", key);
91 int main(int argc, char *argv[], char *envp[])
95 struct udev_rules rules = {};
98 struct sysfs_device *dev;
102 static const struct option options[] = {
103 { "action", 1, NULL, 'a' },
104 { "force", 0, NULL, 'f' },
105 { "help", 0, NULL, 'h' },
109 info("version %s", UDEV_VERSION);
111 if (udev_log_priority < LOG_INFO) {
114 udev_log_priority = LOG_INFO;
115 sprintf(priority, "%i", udev_log_priority);
116 setenv("UDEV_LOG", priority, 1);
122 option = getopt_long(argc, argv, "a:fh", options, NULL);
126 dbg("option '%c'", option);
135 printf("Usage: udevtest [--action=<string>] [--force] [--help] <devpath>\n"
136 " --action=<string> set action string\n"
137 " --force don't skip node/link creation\n"
138 " --help print this help text\n\n");
144 devpath = argv[optind];
146 if (devpath == NULL) {
147 fprintf(stderr, "devpath parameter missing\n");
153 udev_rules_init(&rules, 0);
155 /* remove /sys if given */
156 if (strncmp(devpath, sysfs_path, strlen(sysfs_path)) == 0)
157 devpath = &devpath[strlen(sysfs_path)];
159 dev = sysfs_device_get(devpath);
161 fprintf(stderr, "unable to open device '%s'\n", devpath);
166 udev = udev_device_init(NULL);
168 fprintf(stderr, "error initializing device\n");
173 /* override built-in sysfs device */
175 strcpy(udev->action, action);
176 udev->devt = udev_device_get_devt(udev);
178 /* simulate node creation with test flag */
182 setenv("DEVPATH", udev->dev->devpath, 1);
183 setenv("SUBSYSTEM", udev->dev->subsystem, 1);
184 setenv("ACTION", udev->action, 1);
185 import_uevent_var(udev->dev->devpath);
187 printf("This program is for debugging only, it does not run any program,\n"
188 "specified by a RUN key. It may show incorrect results, because\n"
189 "some values may be different, or not available at a simulation run.\n"
192 info("looking at device '%s' from subsystem '%s'", udev->dev->devpath, udev->dev->subsystem);
193 retval = udev_device_event(&rules, udev);
194 if (retval == 0 && !udev->ignore_device && udev_run) {
195 struct name_entry *name_loop;
197 list_for_each_entry(name_loop, &udev->run_list, node) {
198 char program[PATH_SIZE];
200 strlcpy(program, name_loop->name, sizeof(program));
201 udev_rules_apply_format(udev, program, sizeof(program));
202 info("run: '%s'", program);
207 udev_rules_cleanup(&rules);