]>
Commit | Line | Data |
---|---|---|
e7145211 | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
bb051f66 | 2 | /* |
e8d569b4 | 3 | * Copyright (C) 2003-2004 Greg Kroah-Hartman <greg@kroah.com> |
1298001e | 4 | * Copyright (C) 2004-2008 Kay Sievers <kay@vrfy.org> |
bb051f66 | 5 | * |
55e9959b KS |
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. | |
bb051f66 | 10 | * |
55e9959b KS |
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. | |
15 | * | |
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/>. | |
bb051f66 GKH |
18 | */ |
19 | ||
bb051f66 | 20 | #include <errno.h> |
eff4a673 | 21 | #include <getopt.h> |
07630cea LP |
22 | #include <signal.h> |
23 | #include <stddef.h> | |
24 | #include <stdio.h> | |
25 | #include <stdlib.h> | |
2181d30a | 26 | #include <sys/signalfd.h> |
07630cea | 27 | #include <unistd.h> |
bb051f66 | 28 | |
07630cea | 29 | #include "string-util.h" |
44433ebd | 30 | #include "udev-util.h" |
07630cea | 31 | #include "udev.h" |
5639df9a | 32 | #include "udevadm-util.h" |
bb051f66 | 33 | |
5ac0162c LP |
34 | static void help(void) { |
35 | ||
5639df9a YW |
36 | printf("%s test [OPTIONS] DEVPATH\n\n" |
37 | "Test an event run.\n\n" | |
5ac0162c | 38 | " -h --help Show this help\n" |
5639df9a | 39 | " -V --version Show package version\n" |
5ac0162c LP |
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); | |
43 | } | |
44 | ||
9ec6e95b | 45 | static int adm_test(struct udev *udev, int argc, char *argv[]) { |
912541b0 KS |
46 | int resolve_names = 1; |
47 | char filename[UTIL_PATH_SIZE]; | |
48 | const char *action = "add"; | |
49 | const char *syspath = NULL; | |
912541b0 | 50 | struct udev_list_entry *entry; |
44433ebd ZJS |
51 | _cleanup_udev_rules_unref_ struct udev_rules *rules = NULL; |
52 | _cleanup_udev_device_unref_ struct udev_device *dev = NULL; | |
53 | _cleanup_udev_event_unref_ struct udev_event *event = NULL; | |
912541b0 | 54 | sigset_t mask, sigmask_orig; |
7643ac9a | 55 | int rc = 0, c; |
912541b0 KS |
56 | |
57 | static const struct option options[] = { | |
5639df9a | 58 | { "action", required_argument, NULL, 'a' }, |
912541b0 | 59 | { "resolve-names", required_argument, NULL, 'N' }, |
5639df9a YW |
60 | { "version", no_argument, NULL, 'V' }, |
61 | { "help", no_argument, NULL, 'h' }, | |
912541b0 KS |
62 | {} |
63 | }; | |
64 | ||
948aaa7c | 65 | log_debug("version %s", PACKAGE_VERSION); |
912541b0 | 66 | |
5639df9a | 67 | while ((c = getopt_long(argc, argv, "a:N:Vh", options, NULL)) >= 0) |
7643ac9a | 68 | switch (c) { |
912541b0 KS |
69 | case 'a': |
70 | action = optarg; | |
71 | break; | |
72 | case 'N': | |
090be865 | 73 | if (streq (optarg, "early")) { |
912541b0 | 74 | resolve_names = 1; |
090be865 | 75 | } else if (streq (optarg, "late")) { |
912541b0 | 76 | resolve_names = 0; |
090be865 | 77 | } else if (streq (optarg, "never")) { |
912541b0 KS |
78 | resolve_names = -1; |
79 | } else { | |
80 | fprintf(stderr, "resolve-names must be early, late or never\n"); | |
9f6445e3 | 81 | log_error("resolve-names must be early, late or never"); |
912541b0 KS |
82 | exit(EXIT_FAILURE); |
83 | } | |
84 | break; | |
5639df9a YW |
85 | case 'V': |
86 | print_version(); | |
87 | exit(EXIT_SUCCESS); | |
912541b0 | 88 | case 'h': |
5ac0162c | 89 | help(); |
912541b0 | 90 | exit(EXIT_SUCCESS); |
7643ac9a | 91 | case '?': |
912541b0 | 92 | exit(EXIT_FAILURE); |
7643ac9a ZJS |
93 | default: |
94 | assert_not_reached("Unknown option"); | |
912541b0 | 95 | } |
912541b0 | 96 | |
7643ac9a | 97 | syspath = argv[optind]; |
912541b0 KS |
98 | if (syspath == NULL) { |
99 | fprintf(stderr, "syspath parameter missing\n"); | |
100 | rc = 2; | |
101 | goto out; | |
102 | } | |
103 | ||
baa30fbc | 104 | printf("This program is for debugging only, it does not run any program\n" |
912541b0 KS |
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" | |
107 | "\n"); | |
108 | ||
109 | sigprocmask(SIG_SETMASK, NULL, &sigmask_orig); | |
110 | ||
111 | udev_builtin_init(udev); | |
112 | ||
113 | rules = udev_rules_new(udev, resolve_names); | |
114 | if (rules == NULL) { | |
115 | fprintf(stderr, "error reading rules\n"); | |
116 | rc = 3; | |
117 | goto out; | |
118 | } | |
119 | ||
120 | /* add /sys if needed */ | |
33502ffe | 121 | if (!startswith(syspath, "/sys")) |
d5a89d7d | 122 | strscpyl(filename, sizeof(filename), "/sys", syspath, NULL); |
912541b0 | 123 | else |
d5a89d7d | 124 | strscpy(filename, sizeof(filename), syspath); |
7546145e | 125 | delete_trailing_chars(filename, "/"); |
912541b0 | 126 | |
bf0e00ec | 127 | dev = udev_device_new_from_synthetic_event(udev, filename, action); |
912541b0 KS |
128 | if (dev == NULL) { |
129 | fprintf(stderr, "unable to open device '%s'\n", filename); | |
130 | rc = 4; | |
131 | goto out; | |
132 | } | |
133 | ||
bf0e00ec | 134 | /* don't read info from the db */ |
912541b0 | 135 | udev_device_set_info_loaded(dev); |
912541b0 | 136 | |
912541b0 KS |
137 | event = udev_event_new(dev); |
138 | ||
139 | sigfillset(&mask); | |
140 | sigprocmask(SIG_SETMASK, &mask, &sigmask_orig); | |
912541b0 | 141 | |
adeba500 KS |
142 | udev_event_execute_rules(event, |
143 | 60 * USEC_PER_SEC, 20 * USEC_PER_SEC, | |
144 | NULL, | |
8314de1d | 145 | rules); |
912541b0 KS |
146 | |
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)); | |
149 | ||
1ea97217 KS |
150 | udev_list_entry_foreach(entry, udev_list_get_entry(&event->run_list)) { |
151 | char program[UTIL_PATH_SIZE]; | |
912541b0 | 152 | |
e20a9171 | 153 | udev_event_apply_format(event, udev_list_entry_get_name(entry), program, sizeof(program), false); |
1ea97217 | 154 | printf("run: '%s'\n", program); |
912541b0 | 155 | } |
2181d30a | 156 | out: |
912541b0 KS |
157 | udev_builtin_exit(udev); |
158 | return rc; | |
bb051f66 | 159 | } |
1985c76e KS |
160 | |
161 | const struct udevadm_cmd udevadm_test = { | |
912541b0 KS |
162 | .name = "test", |
163 | .cmd = adm_test, | |
5ac0162c | 164 | .help = "Test an event run", |
912541b0 | 165 | .debug = true, |
1985c76e | 166 | }; |