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