]>
Commit | Line | Data |
---|---|---|
0d5be398 | 1 | /* |
f5001d24 | 2 | * Copyright (C) 2008 Kay Sievers <kayi.sievers@vrfy.org> |
0d5be398 | 3 | * |
55e9959b KS |
4 | * This program is free software: you can redistribute it and/or modify |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation, either version 2 of the License, or | |
7 | * (at your option) any later version. | |
0d5be398 | 8 | * |
55e9959b KS |
9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
0d5be398 KS |
16 | */ |
17 | ||
18 | #include <stdlib.h> | |
19 | #include <stddef.h> | |
20 | #include <string.h> | |
21 | #include <stdio.h> | |
22 | #include <unistd.h> | |
fc89fe7e | 23 | #include <getopt.h> |
0d5be398 KS |
24 | #include <errno.h> |
25 | #include <dirent.h> | |
26 | #include <fcntl.h> | |
27 | #include <syslog.h> | |
fc89fe7e | 28 | #include <fnmatch.h> |
0d5be398 KS |
29 | #include <sys/stat.h> |
30 | #include <sys/types.h> | |
ab815cae KS |
31 | #include <sys/socket.h> |
32 | #include <sys/un.h> | |
0d5be398 KS |
33 | |
34 | #include "udev.h" | |
35 | ||
c48622cc KS |
36 | static int verbose; |
37 | static int dry_run; | |
c48622cc | 38 | |
f5001d24 | 39 | static void exec_list(struct udev_enumerate *udev_enumerate, const char *action) |
c48622cc | 40 | { |
f5001d24 KS |
41 | struct udev *udev = udev_enumerate_get_udev(udev_enumerate); |
42 | struct udev_list_entry *entry; | |
ab815cae | 43 | |
f5001d24 KS |
44 | udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(udev_enumerate)) { |
45 | char filename[UTIL_PATH_SIZE]; | |
fc89fe7e | 46 | int fd; |
fc89fe7e | 47 | |
f5001d24 KS |
48 | if (verbose) |
49 | printf("%s\n", udev_list_entry_get_name(entry)); | |
50 | if (dry_run) | |
51 | continue; | |
52 | util_strlcpy(filename, udev_list_entry_get_name(entry), sizeof(filename)); | |
53 | util_strlcat(filename, "/uevent", sizeof(filename)); | |
54 | fd = open(filename, O_WRONLY); | |
55 | if (fd < 0) { | |
56 | dbg(udev, "error on opening %s: %m\n", filename); | |
57 | continue; | |
0d5be398 | 58 | } |
f5001d24 KS |
59 | if (write(fd, action, strlen(action)) < 0) |
60 | info(udev, "error writing '%s' to '%s': %m\n", action, filename); | |
61 | close(fd); | |
0d5be398 KS |
62 | } |
63 | } | |
64 | ||
8a1946ae | 65 | static int scan_failed(struct udev_enumerate *udev_enumerate) |
04cc3a81 | 66 | { |
f5001d24 | 67 | struct udev *udev = udev_enumerate_get_udev(udev_enumerate); |
8a1946ae KS |
68 | struct udev_queue *udev_queue; |
69 | struct udev_list_entry *list_entry; | |
04cc3a81 | 70 | |
8a1946ae KS |
71 | udev_queue = udev_queue_new(udev); |
72 | if (udev_queue == NULL) | |
73 | return -1; | |
13ddea81 KS |
74 | udev_list_entry_foreach(list_entry, udev_queue_get_failed_list_entry(udev_queue)) |
75 | udev_enumerate_add_syspath(udev_enumerate, udev_list_entry_get_name(list_entry)); | |
f5001d24 | 76 | return 0; |
04cc3a81 KS |
77 | } |
78 | ||
7d563a17 | 79 | int udevadm_trigger(struct udev *udev, int argc, char *argv[]) |
0d5be398 | 80 | { |
e97717ba | 81 | static const struct option options[] = { |
033e9f8c KS |
82 | { "verbose", no_argument, NULL, 'v' }, |
83 | { "dry-run", no_argument, NULL, 'n' }, | |
84 | { "type", required_argument, NULL, 't' }, | |
85 | { "retry-failed", no_argument, NULL, 'F' }, | |
86 | { "action", required_argument, NULL, 'c' }, | |
87 | { "subsystem-match", required_argument, NULL, 's' }, | |
88 | { "subsystem-nomatch", required_argument, NULL, 'S' }, | |
89 | { "attr-match", required_argument, NULL, 'a' }, | |
90 | { "attr-nomatch", required_argument, NULL, 'A' }, | |
91 | { "help", no_argument, NULL, 'h' }, | |
fc89fe7e KS |
92 | {} |
93 | }; | |
f5001d24 KS |
94 | enum { |
95 | TYPE_DEVICES, | |
96 | TYPE_SUBSYSTEMS, | |
97 | TYPE_FAILED, | |
98 | } device_type = TYPE_DEVICES; | |
99 | const char *action = "add"; | |
100 | struct udev_enumerate *udev_enumerate; | |
101 | int rc = 0; | |
0d5be398 | 102 | |
7d563a17 | 103 | dbg(udev, "version %s\n", VERSION); |
f5001d24 KS |
104 | udev_enumerate = udev_enumerate_new(udev); |
105 | if (udev_enumerate == NULL) { | |
106 | rc = 1; | |
107 | goto exit; | |
108 | } | |
0d5be398 | 109 | |
fc89fe7e | 110 | while (1) { |
f5001d24 KS |
111 | int option; |
112 | char attr[UTIL_PATH_SIZE]; | |
113 | char *val; | |
114 | ||
115 | option = getopt_long(argc, argv, "vnFo:t:hce::s:S:a:A:", options, NULL); | |
fc89fe7e KS |
116 | if (option == -1) |
117 | break; | |
0d5be398 | 118 | |
fc89fe7e KS |
119 | switch (option) { |
120 | case 'v': | |
0d5be398 | 121 | verbose = 1; |
fc89fe7e KS |
122 | break; |
123 | case 'n': | |
0d5be398 | 124 | dry_run = 1; |
fc89fe7e | 125 | break; |
f5001d24 KS |
126 | case 't': |
127 | if (strcmp(optarg, "devices") == 0) { | |
128 | device_type = TYPE_DEVICES; | |
129 | } else if (strcmp(optarg, "subsystems") == 0) { | |
130 | device_type = TYPE_SUBSYSTEMS; | |
131 | } else if (strcmp(optarg, "failed") == 0) { | |
132 | device_type = TYPE_FAILED; | |
133 | } else { | |
134 | fprintf(stderr, "unknown type --type=%s\n", optarg); | |
135 | err(udev, "unknown type --type=%s\n", optarg); | |
136 | rc = 2; | |
137 | goto exit; | |
138 | } | |
fc89fe7e | 139 | break; |
f5001d24 KS |
140 | case 'F': |
141 | device_type = TYPE_FAILED; | |
ab815cae | 142 | break; |
285e2a24 KS |
143 | case 'c': |
144 | action = optarg; | |
145 | break; | |
fc89fe7e | 146 | case 's': |
f5001d24 | 147 | udev_enumerate_add_match_subsystem(udev_enumerate, optarg); |
fc89fe7e KS |
148 | break; |
149 | case 'S': | |
f5001d24 | 150 | udev_enumerate_add_nomatch_subsystem(udev_enumerate, optarg); |
fc89fe7e KS |
151 | break; |
152 | case 'a': | |
f5001d24 KS |
153 | util_strlcpy(attr, optarg, sizeof(attr)); |
154 | val = strchr(attr, '='); | |
155 | if (val != NULL) { | |
156 | val[0] = 0; | |
157 | val = &val[1]; | |
158 | } | |
69239210 | 159 | udev_enumerate_add_match_sysattr(udev_enumerate, attr, val); |
fc89fe7e KS |
160 | break; |
161 | case 'A': | |
f5001d24 KS |
162 | util_strlcpy(attr, optarg, sizeof(attr)); |
163 | val = strchr(attr, '='); | |
164 | if (val != NULL) { | |
165 | val[0] = 0; | |
166 | val = &val[1]; | |
167 | } | |
69239210 | 168 | udev_enumerate_add_nomatch_sysattr(udev_enumerate, attr, val); |
fc89fe7e KS |
169 | break; |
170 | case 'h': | |
225cb03b | 171 | printf("Usage: udevadm trigger OPTIONS\n" |
7c27c752 KS |
172 | " --verbose print the list of devices while running\n" |
173 | " --dry-run do not actually trigger the events\n" | |
f5001d24 KS |
174 | " --type= type of events to trigger\n" |
175 | " devices sys devices\n" | |
176 | " subsystems sys subsystems and drivers\n" | |
177 | " failed trigger only the events which have been\n" | |
178 | " marked as failed during a previous run\n" | |
7c27c752 KS |
179 | " --subsystem-match=<subsystem> trigger devices from a matching subystem\n" |
180 | " --subsystem-nomatch=<subsystem> exclude devices from a matching subystem\n" | |
f5001d24 KS |
181 | " --attr-match=<file[=<value>]> trigger devices with a matching attribute\n" |
182 | " --attr-nomatch=<file[=<value>]> exclude devices with a matching attribute\n" | |
7c27c752 | 183 | " --help print this text\n" |
fc89fe7e KS |
184 | "\n"); |
185 | goto exit; | |
186 | default: | |
04cc3a81 | 187 | goto exit; |
0d5be398 KS |
188 | } |
189 | } | |
190 | ||
f5001d24 KS |
191 | switch (device_type) { |
192 | case TYPE_FAILED: | |
8a1946ae | 193 | scan_failed(udev_enumerate); |
f5001d24 KS |
194 | exec_list(udev_enumerate, action); |
195 | goto exit; | |
196 | case TYPE_SUBSYSTEMS: | |
197 | udev_enumerate_scan_subsystems(udev_enumerate); | |
198 | exec_list(udev_enumerate, action); | |
199 | goto exit; | |
200 | case TYPE_DEVICES: | |
201 | udev_enumerate_scan_devices(udev_enumerate); | |
202 | exec_list(udev_enumerate, action); | |
203 | goto exit; | |
204 | default: | |
7b2aad33 | 205 | goto exit; |
ab815cae | 206 | } |
0d5be398 | 207 | exit: |
f5001d24 KS |
208 | udev_enumerate_unref(udev_enumerate); |
209 | return rc; | |
0d5be398 | 210 | } |