]>
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" |
bb051f66 | 32 | |
5ac0162c LP |
33 | static void help(void) { |
34 | ||
35 | printf("%s test OPTIONS <syspath>\n\n" | |
36 | "Test an event run.\n" | |
37 | " -h --help Show this help\n" | |
38 | " --version Show package version\n" | |
39 | " -a --action=ACTION Set action string\n" | |
40 | " -N --resolve-names=early|late|never When to resolve names\n" | |
41 | , program_invocation_short_name); | |
42 | } | |
43 | ||
9ec6e95b | 44 | static int adm_test(struct udev *udev, int argc, char *argv[]) { |
912541b0 KS |
45 | int resolve_names = 1; |
46 | char filename[UTIL_PATH_SIZE]; | |
47 | const char *action = "add"; | |
48 | const char *syspath = NULL; | |
912541b0 | 49 | struct udev_list_entry *entry; |
44433ebd ZJS |
50 | _cleanup_udev_rules_unref_ struct udev_rules *rules = NULL; |
51 | _cleanup_udev_device_unref_ struct udev_device *dev = NULL; | |
52 | _cleanup_udev_event_unref_ struct udev_event *event = NULL; | |
912541b0 | 53 | sigset_t mask, sigmask_orig; |
7643ac9a | 54 | int rc = 0, c; |
912541b0 KS |
55 | |
56 | static const struct option options[] = { | |
57 | { "action", required_argument, NULL, 'a' }, | |
58 | { "resolve-names", required_argument, NULL, 'N' }, | |
59 | { "help", no_argument, NULL, 'h' }, | |
60 | {} | |
61 | }; | |
62 | ||
948aaa7c | 63 | log_debug("version %s", PACKAGE_VERSION); |
912541b0 | 64 | |
9ed794a3 | 65 | while ((c = getopt_long(argc, argv, "a:N:h", options, NULL)) >= 0) |
7643ac9a | 66 | switch (c) { |
912541b0 KS |
67 | case 'a': |
68 | action = optarg; | |
69 | break; | |
70 | case 'N': | |
090be865 | 71 | if (streq (optarg, "early")) { |
912541b0 | 72 | resolve_names = 1; |
090be865 | 73 | } else if (streq (optarg, "late")) { |
912541b0 | 74 | resolve_names = 0; |
090be865 | 75 | } else if (streq (optarg, "never")) { |
912541b0 KS |
76 | resolve_names = -1; |
77 | } else { | |
78 | fprintf(stderr, "resolve-names must be early, late or never\n"); | |
9f6445e3 | 79 | log_error("resolve-names must be early, late or never"); |
912541b0 KS |
80 | exit(EXIT_FAILURE); |
81 | } | |
82 | break; | |
83 | case 'h': | |
5ac0162c | 84 | help(); |
912541b0 | 85 | exit(EXIT_SUCCESS); |
7643ac9a | 86 | case '?': |
912541b0 | 87 | exit(EXIT_FAILURE); |
7643ac9a ZJS |
88 | default: |
89 | assert_not_reached("Unknown option"); | |
912541b0 | 90 | } |
912541b0 | 91 | |
7643ac9a | 92 | syspath = argv[optind]; |
912541b0 KS |
93 | if (syspath == NULL) { |
94 | fprintf(stderr, "syspath parameter missing\n"); | |
95 | rc = 2; | |
96 | goto out; | |
97 | } | |
98 | ||
baa30fbc | 99 | printf("This program is for debugging only, it does not run any program\n" |
912541b0 KS |
100 | "specified by a RUN key. It may show incorrect results, because\n" |
101 | "some values may be different, or not available at a simulation run.\n" | |
102 | "\n"); | |
103 | ||
104 | sigprocmask(SIG_SETMASK, NULL, &sigmask_orig); | |
105 | ||
106 | udev_builtin_init(udev); | |
107 | ||
108 | rules = udev_rules_new(udev, resolve_names); | |
109 | if (rules == NULL) { | |
110 | fprintf(stderr, "error reading rules\n"); | |
111 | rc = 3; | |
112 | goto out; | |
113 | } | |
114 | ||
115 | /* add /sys if needed */ | |
33502ffe | 116 | if (!startswith(syspath, "/sys")) |
d5a89d7d | 117 | strscpyl(filename, sizeof(filename), "/sys", syspath, NULL); |
912541b0 | 118 | else |
d5a89d7d | 119 | strscpy(filename, sizeof(filename), syspath); |
7546145e | 120 | delete_trailing_chars(filename, "/"); |
912541b0 | 121 | |
bf0e00ec | 122 | dev = udev_device_new_from_synthetic_event(udev, filename, action); |
912541b0 KS |
123 | if (dev == NULL) { |
124 | fprintf(stderr, "unable to open device '%s'\n", filename); | |
125 | rc = 4; | |
126 | goto out; | |
127 | } | |
128 | ||
bf0e00ec | 129 | /* don't read info from the db */ |
912541b0 | 130 | udev_device_set_info_loaded(dev); |
912541b0 | 131 | |
912541b0 KS |
132 | event = udev_event_new(dev); |
133 | ||
134 | sigfillset(&mask); | |
135 | sigprocmask(SIG_SETMASK, &mask, &sigmask_orig); | |
912541b0 | 136 | |
adeba500 KS |
137 | udev_event_execute_rules(event, |
138 | 60 * USEC_PER_SEC, 20 * USEC_PER_SEC, | |
139 | NULL, | |
8314de1d | 140 | rules); |
912541b0 KS |
141 | |
142 | udev_list_entry_foreach(entry, udev_device_get_properties_list_entry(dev)) | |
143 | printf("%s=%s\n", udev_list_entry_get_name(entry), udev_list_entry_get_value(entry)); | |
144 | ||
1ea97217 KS |
145 | udev_list_entry_foreach(entry, udev_list_get_entry(&event->run_list)) { |
146 | char program[UTIL_PATH_SIZE]; | |
912541b0 | 147 | |
e20a9171 | 148 | udev_event_apply_format(event, udev_list_entry_get_name(entry), program, sizeof(program), false); |
1ea97217 | 149 | printf("run: '%s'\n", program); |
912541b0 | 150 | } |
2181d30a | 151 | out: |
912541b0 KS |
152 | udev_builtin_exit(udev); |
153 | return rc; | |
bb051f66 | 154 | } |
1985c76e KS |
155 | |
156 | const struct udevadm_cmd udevadm_test = { | |
912541b0 KS |
157 | .name = "test", |
158 | .cmd = adm_test, | |
5ac0162c | 159 | .help = "Test an event run", |
912541b0 | 160 | .debug = true, |
1985c76e | 161 | }; |