1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * Copyright (C) 2003-2004 Greg Kroah-Hartman <greg@kroah.com>
4 * Copyright (C) 2004-2008 Kay Sievers <kay@vrfy.org>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include <sys/signalfd.h>
29 #include "string-util.h"
30 #include "udev-util.h"
32 #include "udevadm-util.h"
34 static void help(void) {
36 printf("%s test [OPTIONS] DEVPATH\n\n"
37 "Test an event run.\n\n"
38 " -h --help Show this help\n"
39 " -V --version Show package version\n"
40 " -a --action=ACTION Set action string\n"
41 " -N --resolve-names=early|late|never When to resolve names\n"
42 , program_invocation_short_name
);
45 static int adm_test(struct udev
*udev
, int argc
, char *argv
[]) {
46 int resolve_names
= 1;
47 char filename
[UTIL_PATH_SIZE
];
48 const char *action
= "add";
49 const char *syspath
= NULL
;
50 struct udev_list_entry
*entry
;
51 _cleanup_(udev_rules_unrefp
) struct udev_rules
*rules
= NULL
;
52 _cleanup_(udev_device_unrefp
) struct udev_device
*dev
= NULL
;
53 _cleanup_(udev_event_unrefp
) struct udev_event
*event
= NULL
;
54 sigset_t mask
, sigmask_orig
;
57 static const struct option options
[] = {
58 { "action", required_argument
, NULL
, 'a' },
59 { "resolve-names", required_argument
, NULL
, 'N' },
60 { "version", no_argument
, NULL
, 'V' },
61 { "help", no_argument
, NULL
, 'h' },
65 log_debug("version %s", PACKAGE_VERSION
);
67 while ((c
= getopt_long(argc
, argv
, "a:N:Vh", options
, NULL
)) >= 0)
73 if (streq (optarg
, "early")) {
75 } else if (streq (optarg
, "late")) {
77 } else if (streq (optarg
, "never")) {
80 fprintf(stderr
, "resolve-names must be early, late or never\n");
81 log_error("resolve-names must be early, late or never");
94 assert_not_reached("Unknown option");
97 syspath
= argv
[optind
];
98 if (syspath
== NULL
) {
99 fprintf(stderr
, "syspath parameter missing\n");
104 printf("This program is for debugging only, it does not run any program\n"
105 "specified by a RUN key. It may show incorrect results, because\n"
106 "some values may be different, or not available at a simulation run.\n"
109 sigprocmask(SIG_SETMASK
, NULL
, &sigmask_orig
);
111 udev_builtin_init(udev
);
113 rules
= udev_rules_new(udev
, resolve_names
);
115 fprintf(stderr
, "error reading rules\n");
120 /* add /sys if needed */
121 if (!startswith(syspath
, "/sys"))
122 strscpyl(filename
, sizeof(filename
), "/sys", syspath
, NULL
);
124 strscpy(filename
, sizeof(filename
), syspath
);
125 delete_trailing_chars(filename
, "/");
127 dev
= udev_device_new_from_synthetic_event(udev
, filename
, action
);
129 fprintf(stderr
, "unable to open device '%s'\n", filename
);
134 /* don't read info from the db */
135 udev_device_set_info_loaded(dev
);
137 event
= udev_event_new(dev
);
140 sigprocmask(SIG_SETMASK
, &mask
, &sigmask_orig
);
142 udev_event_execute_rules(event
,
143 60 * USEC_PER_SEC
, 20 * USEC_PER_SEC
,
147 udev_list_entry_foreach(entry
, udev_device_get_properties_list_entry(dev
))
148 printf("%s=%s\n", udev_list_entry_get_name(entry
), udev_list_entry_get_value(entry
));
150 udev_list_entry_foreach(entry
, udev_list_get_entry(&event
->run_list
)) {
151 char program
[UTIL_PATH_SIZE
];
153 udev_event_apply_format(event
, udev_list_entry_get_name(entry
), program
, sizeof(program
), false);
154 printf("run: '%s'\n", program
);
157 udev_builtin_exit(udev
);
161 const struct udevadm_cmd udevadm_test
= {
164 .help
= "Test an event run",