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.
31 #include "udev_rules.h"
35 void log_message (int priority, const char *format, ...)
39 if (priority > udev_log_priority)
42 va_start(args, format);
43 vprintf(format, args);
45 if (format[strlen(format)-1] != '\n')
50 int main(int argc, char *argv[], char *envp[])
52 struct udev_rules rules = {};
55 struct sysfs_device *dev;
60 info("version %s", UDEV_VERSION);
62 if (udev_log_priority < LOG_INFO) {
65 udev_log_priority = LOG_INFO;
66 sprintf(priority, "%i", udev_log_priority);
67 setenv("UDEV_LOG", priority, 1);
70 for (i = 1 ; i < argc; i++) {
73 if (strcmp(arg, "--help") == 0 || strcmp(arg, "-h") == 0) {
74 printf("Usage: udevtest [--help] <devpath>\n");
80 if (devpath == NULL) {
81 fprintf(stderr, "devpath parameter missing\n");
87 udev_rules_init(&rules, 0);
89 /* remove /sys if given */
90 if (strncmp(devpath, sysfs_path, strlen(sysfs_path)) == 0)
91 devpath = &devpath[strlen(sysfs_path)];
93 dev = sysfs_device_get(devpath);
95 fprintf(stderr, "unable to open device '%s'\n", devpath);
100 udev = udev_device_init();
102 fprintf(stderr, "error initializing device\n");
107 /* override built-in sysfs device */
109 strcpy(udev->action, "add");
110 udev->devt = udev_device_get_devt(udev);
112 /* simulate node creation with test flag */
115 setenv("DEVPATH", udev->dev->devpath, 1);
116 setenv("SUBSYSTEM", udev->dev->subsystem, 1);
117 setenv("ACTION", "add", 1);
119 printf("This program is for debugging only, it does not create any node,\n"
120 "or run any program specified by a RUN key. It may show incorrect results,\n"
121 "if rules match against subsystem specfic kernel event variables.\n"
124 info("looking at device '%s' from subsystem '%s'", udev->dev->devpath, udev->dev->subsystem);
125 retval = udev_device_event(&rules, udev);
126 if (retval == 0 && !udev->ignore_device && udev_run) {
127 struct name_entry *name_loop;
129 list_for_each_entry(name_loop, &udev->run_list, node) {
130 char program[PATH_SIZE];
132 strlcpy(program, name_loop->name, sizeof(program));
133 udev_rules_apply_format(udev, program, sizeof(program));
134 info("run: '%s'", program);
139 udev_rules_cleanup(&rules);