]>
Commit | Line | Data |
---|---|---|
bb051f66 | 1 | /* |
e8d569b4 | 2 | * Copyright (C) 2003-2004 Greg Kroah-Hartman <greg@kroah.com> |
55e9959b | 3 | * Copyright (C) 2004-2008 Kay Sievers <kay.sievers@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 | ||
19 | #include <stdlib.h> | |
20 | #include <string.h> | |
21 | #include <stdio.h> | |
1aa1e248 KS |
22 | #include <stddef.h> |
23 | #include <unistd.h> | |
bb051f66 GKH |
24 | #include <errno.h> |
25 | #include <ctype.h> | |
68d4af11 | 26 | #include <fcntl.h> |
bb051f66 | 27 | #include <signal.h> |
6b493a20 | 28 | #include <syslog.h> |
eff4a673 | 29 | #include <getopt.h> |
2181d30a | 30 | #include <sys/signalfd.h> |
bb051f66 GKH |
31 | |
32 | #include "udev.h" | |
bb051f66 | 33 | |
7d563a17 | 34 | int udevadm_test(struct udev *udev, int argc, char *argv[]) |
bb051f66 | 35 | { |
2181d30a | 36 | int resolve_names = 1; |
aa8734ff | 37 | char filename[UTIL_PATH_SIZE]; |
285e2a24 | 38 | const char *action = "add"; |
aa8734ff | 39 | const char *syspath = NULL; |
2181d30a KS |
40 | struct udev_event *event = NULL; |
41 | struct udev_device *dev = NULL; | |
d7ddce18 | 42 | struct udev_rules *rules = NULL; |
456719b6 | 43 | struct udev_list_entry *entry; |
2181d30a | 44 | sigset_t mask, sigmask_orig; |
aa8734ff | 45 | int err; |
1aa1e248 | 46 | int rc = 0; |
8a0acf85 | 47 | |
eff4a673 | 48 | static const struct option options[] = { |
033e9f8c | 49 | { "action", required_argument, NULL, 'a' }, |
2181d30a | 50 | { "resolve-names", required_argument, NULL, 'N' }, |
033e9f8c | 51 | { "help", no_argument, NULL, 'h' }, |
eff4a673 KS |
52 | {} |
53 | }; | |
54 | ||
7d563a17 KS |
55 | info(udev, "version %s\n", VERSION); |
56 | ||
88cbfb09 | 57 | for (;;) { |
eff4a673 KS |
58 | int option; |
59 | ||
2181d30a | 60 | option = getopt_long(argc, argv, "a:s:N:fh", options, NULL); |
eff4a673 KS |
61 | if (option == -1) |
62 | break; | |
63 | ||
7d563a17 | 64 | dbg(udev, "option '%c'\n", option); |
eff4a673 KS |
65 | switch (option) { |
66 | case 'a': | |
67 | action = optarg; | |
68 | break; | |
2181d30a KS |
69 | case 'N': |
70 | if (strcmp (optarg, "early") == 0) { | |
71 | resolve_names = 1; | |
72 | } else if (strcmp (optarg, "late") == 0) { | |
73 | resolve_names = 0; | |
74 | } else if (strcmp (optarg, "never") == 0) { | |
75 | resolve_names = -1; | |
76 | } else { | |
77 | fprintf(stderr, "resolve-names must be early, late or never\n"); | |
78 | err(udev, "resolve-names must be early, late or never\n"); | |
79 | exit(EXIT_FAILURE); | |
80 | } | |
81 | break; | |
eff4a673 | 82 | case 'h': |
aa8734ff | 83 | printf("Usage: udevadm test OPTIONS <syspath>\n" |
df97701e | 84 | " --action=<string> set action string\n" |
f1e7e360 | 85 | " --help\n\n"); |
2181d30a | 86 | exit(EXIT_SUCCESS); |
eff4a673 | 87 | default: |
2181d30a | 88 | exit(EXIT_FAILURE); |
eff4a673 | 89 | } |
e3396a2d | 90 | } |
aa8734ff | 91 | syspath = argv[optind]; |
e3396a2d | 92 | |
aa8734ff KS |
93 | if (syspath == NULL) { |
94 | fprintf(stderr, "syspath parameter missing\n"); | |
2181d30a KS |
95 | rc = 2; |
96 | goto out; | |
e03a196a KS |
97 | } |
98 | ||
86fd6b4f KS |
99 | printf("This program is for debugging only, it does not run any program,\n" |
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 | ||
2181d30a KS |
104 | sigprocmask(SIG_SETMASK, NULL, &sigmask_orig); |
105 | ||
106 | rules = udev_rules_new(udev, resolve_names); | |
d7ddce18 KS |
107 | if (rules == NULL) { |
108 | fprintf(stderr, "error reading rules\n"); | |
2181d30a KS |
109 | rc = 3; |
110 | goto out; | |
d7ddce18 | 111 | } |
2362eea6 | 112 | |
aa8734ff | 113 | /* add /sys if needed */ |
065db052 KS |
114 | if (strncmp(syspath, udev_get_sys_path(udev), strlen(udev_get_sys_path(udev))) != 0) |
115 | util_strscpyl(filename, sizeof(filename), udev_get_sys_path(udev), syspath, NULL); | |
116 | else | |
117 | util_strscpy(filename, sizeof(filename), syspath); | |
f454f670 | 118 | util_remove_trailing_chars(filename, '/'); |
bb051f66 | 119 | |
f454f670 | 120 | dev = udev_device_new_from_syspath(udev, filename); |
1aa1e248 | 121 | if (dev == NULL) { |
f454f670 | 122 | fprintf(stderr, "unable to open device '%s'\n", filename); |
2181d30a KS |
123 | rc = 4; |
124 | goto out; | |
1aa1e248 | 125 | } |
7a947ce5 | 126 | |
aa8734ff KS |
127 | /* skip reading of db, but read kernel parameters */ |
128 | udev_device_set_info_loaded(dev); | |
129 | udev_device_read_uevent_file(dev); | |
7a947ce5 | 130 | |
aa8734ff KS |
131 | udev_device_set_action(dev, action); |
132 | event = udev_event_new(dev); | |
bf50425b | 133 | |
2181d30a KS |
134 | sigfillset(&mask); |
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"); | |
139 | rc = 5; | |
140 | goto out; | |
141 | } | |
142 | ||
143 | err = udev_event_execute_rules(event, rules, &sigmask_orig); | |
bf50425b | 144 | |
456719b6 | 145 | udev_list_entry_foreach(entry, udev_device_get_properties_list_entry(dev)) |
372b9bff | 146 | printf("%s=%s\n", udev_list_entry_get_name(entry), udev_list_entry_get_value(entry)); |
1aa1e248 | 147 | |
2181d30a | 148 | if (err == 0) { |
aa8734ff | 149 | udev_list_entry_foreach(entry, udev_list_get_entry(&event->run_list)) { |
17fcfb59 | 150 | char program[UTIL_PATH_SIZE]; |
ad27f5b3 | 151 | |
065db052 | 152 | udev_event_apply_format(event, udev_list_entry_get_name(entry), program, sizeof(program)); |
372b9bff | 153 | printf("run: '%s'\n", program); |
ad27f5b3 | 154 | } |
2181d30a KS |
155 | } |
156 | out: | |
157 | if (event != NULL && event->fd_signal >= 0) | |
158 | close(event->fd_signal); | |
aa8734ff KS |
159 | udev_event_unref(event); |
160 | udev_device_unref(dev); | |
d7ddce18 | 161 | udev_rules_unref(rules); |
1aa1e248 | 162 | return rc; |
bb051f66 | 163 | } |