2 * Copyright (C) 2003-2004 Greg Kroah-Hartman <greg@kroah.com>
3 * Copyright (C) 2004-2008 Kay Sievers <kay@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/>.
26 #include <sys/signalfd.h>
29 #include "udev-util.h"
31 static void help(void) {
33 printf("%s test OPTIONS <syspath>\n\n"
34 "Test an event run.\n"
35 " -h --help Show this help\n"
36 " --version Show package version\n"
37 " -a --action=ACTION Set action string\n"
38 " -N --resolve-names=early|late|never When to resolve names\n"
39 , program_invocation_short_name);
42 static int adm_test(struct udev *udev, int argc, char *argv[]) {
43 int resolve_names = 1;
44 char filename[UTIL_PATH_SIZE];
45 const char *action = "add";
46 const char *syspath = NULL;
47 struct udev_list_entry *entry;
48 _cleanup_udev_rules_unref_ struct udev_rules *rules = NULL;
49 _cleanup_udev_device_unref_ struct udev_device *dev = NULL;
50 _cleanup_udev_event_unref_ struct udev_event *event = NULL;
51 sigset_t mask, sigmask_orig;
54 static const struct option options[] = {
55 { "action", required_argument, NULL, 'a' },
56 { "resolve-names", required_argument, NULL, 'N' },
57 { "help", no_argument, NULL, 'h' },
61 log_debug("version %s", VERSION);
63 while((c = getopt_long(argc, argv, "a:N:h", options, NULL)) >= 0)
69 if (streq (optarg, "early")) {
71 } else if (streq (optarg, "late")) {
73 } else if (streq (optarg, "never")) {
76 fprintf(stderr, "resolve-names must be early, late or never\n");
77 log_error("resolve-names must be early, late or never");
87 assert_not_reached("Unknown option");
90 syspath = argv[optind];
91 if (syspath == NULL) {
92 fprintf(stderr, "syspath parameter missing\n");
97 printf("This program is for debugging only, it does not run any program\n"
98 "specified by a RUN key. It may show incorrect results, because\n"
99 "some values may be different, or not available at a simulation run.\n"
102 sigprocmask(SIG_SETMASK, NULL, &sigmask_orig);
104 udev_builtin_init(udev);
106 rules = udev_rules_new(udev, resolve_names);
108 fprintf(stderr, "error reading rules\n");
113 /* add /sys if needed */
114 if (!startswith(syspath, "/sys"))
115 strscpyl(filename, sizeof(filename), "/sys", syspath, NULL);
117 strscpy(filename, sizeof(filename), syspath);
118 util_remove_trailing_chars(filename, '/');
120 dev = udev_device_new_from_syspath(udev, filename);
122 fprintf(stderr, "unable to open device '%s'\n", filename);
127 /* skip reading of db, but read kernel parameters */
128 udev_device_set_info_loaded(dev);
129 udev_device_read_uevent_file(dev);
131 udev_device_set_action(dev, action);
132 event = udev_event_new(dev);
135 sigprocmask(SIG_SETMASK, &mask, &sigmask_orig);
136 event->fd_signal = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC);
137 if (event->fd_signal < 0) {
138 fprintf(stderr, "error creating signalfd\n");
143 udev_event_execute_rules(event,
144 60 * USEC_PER_SEC, 20 * USEC_PER_SEC,
149 udev_list_entry_foreach(entry, udev_device_get_properties_list_entry(dev))
150 printf("%s=%s\n", udev_list_entry_get_name(entry), udev_list_entry_get_value(entry));
152 udev_list_entry_foreach(entry, udev_list_get_entry(&event->run_list)) {
153 char program[UTIL_PATH_SIZE];
155 udev_event_apply_format(event, udev_list_entry_get_name(entry), program, sizeof(program));
156 printf("run: '%s'\n", program);
159 if (event != NULL && event->fd_signal >= 0)
160 close(event->fd_signal);
161 udev_builtin_exit(udev);
165 const struct udevadm_cmd udevadm_test = {
168 .help = "Test an event run",