chiark / gitweb /
2c4954761ee9f940c045c0859d106e137235e332
[elogind.git] / udev / udevadm-trigger.c
1 /*
2  * Copyright (C) 2008 Kay Sievers <kayi.sievers@vrfy.org>
3  *
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.
8  *
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/>.
16  */
17
18 #include <stdlib.h>
19 #include <stddef.h>
20 #include <string.h>
21 #include <stdio.h>
22 #include <unistd.h>
23 #include <getopt.h>
24 #include <errno.h>
25 #include <dirent.h>
26 #include <fcntl.h>
27 #include <syslog.h>
28 #include <fnmatch.h>
29 #include <sys/stat.h>
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <sys/un.h>
33
34 #include "udev.h"
35
36 static int verbose;
37 static int dry_run;
38
39 static void exec_list(struct udev_enumerate *udev_enumerate, const char *action)
40 {
41         struct udev *udev = udev_enumerate_get_udev(udev_enumerate);
42         struct udev_list_entry *entry;
43
44         udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(udev_enumerate)) {
45                 char filename[UTIL_PATH_SIZE];
46                 int fd;
47
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;
58                 }
59                 if (write(fd, action, strlen(action)) < 0)
60                         info(udev, "error writing '%s' to '%s': %m\n", action, filename);
61                 close(fd);
62         }
63 }
64
65 static int scan_failed(struct udev_enumerate *udev_enumerate)
66 {
67         struct udev *udev = udev_enumerate_get_udev(udev_enumerate);
68         struct udev_queue *udev_queue;
69         struct udev_list_entry *list_entry;
70
71         udev_queue = udev_queue_new(udev);
72         if (udev_queue == NULL)
73                 return -1;
74         udev_list_entry_foreach(list_entry, udev_queue_get_failed_list_entry(udev_queue)) {
75                 struct udev_device *device;
76
77                 device = udev_device_new_from_syspath(udev, udev_list_entry_get_name(list_entry));
78                 if (device == NULL)
79                         continue;
80                 info(udev, "add '%s'\n", udev_device_get_syspath(device));
81                 udev_enumerate_add_device(udev_enumerate, device);
82                 udev_device_unref(device);
83         }
84         return 0;
85 }
86
87 int udevadm_trigger(struct udev *udev, int argc, char *argv[])
88 {
89         static const struct option options[] = {
90                 { "verbose", 0, NULL, 'v' },
91                 { "dry-run", 0, NULL, 'n' },
92                 { "type", 1, NULL, 't' },
93                 { "retry-failed", 0, NULL, 'F' },
94                 { "action", 1, NULL, 'c' },
95                 { "subsystem-match", 1, NULL, 's' },
96                 { "subsystem-nomatch", 1, NULL, 'S' },
97                 { "attr-match", 1, NULL, 'a' },
98                 { "attr-nomatch", 1, NULL, 'A' },
99                 { "help", 0, NULL, 'h' },
100                 {}
101         };
102         enum {
103                 TYPE_DEVICES,
104                 TYPE_SUBSYSTEMS,
105                 TYPE_FAILED,
106         } device_type = TYPE_DEVICES;
107         const char *action = "add";
108         struct udev_enumerate *udev_enumerate;
109         int rc = 0;
110
111         dbg(udev, "version %s\n", VERSION);
112         udev_enumerate = udev_enumerate_new(udev);
113         if (udev_enumerate == NULL) {
114                 rc = 1;
115                 goto exit;
116         }
117
118         while (1) {
119                 int option;
120                 char attr[UTIL_PATH_SIZE];
121                 char *val;
122
123                 option = getopt_long(argc, argv, "vnFo:t:hce::s:S:a:A:", options, NULL);
124                 if (option == -1)
125                         break;
126
127                 switch (option) {
128                 case 'v':
129                         verbose = 1;
130                         break;
131                 case 'n':
132                         dry_run = 1;
133                         break;
134                 case 't':
135                         if (strcmp(optarg, "devices") == 0) {
136                                 device_type = TYPE_DEVICES;
137                         } else if (strcmp(optarg, "subsystems") == 0) {
138                                 device_type = TYPE_SUBSYSTEMS;
139                         } else if (strcmp(optarg, "failed") == 0) {
140                                 device_type = TYPE_FAILED;
141                         } else {
142                                 fprintf(stderr, "unknown type --type=%s\n", optarg);
143                                 err(udev, "unknown type --type=%s\n", optarg);
144                                 rc = 2;
145                                 goto exit;
146                         }
147                         break;
148                 case 'F':
149                         device_type = TYPE_FAILED;
150                         break;
151                 case 'c':
152                         action = optarg;
153                         break;
154                 case 's':
155                         udev_enumerate_add_match_subsystem(udev_enumerate, optarg);
156                         break;
157                 case 'S':
158                         udev_enumerate_add_nomatch_subsystem(udev_enumerate, optarg);
159                         break;
160                 case 'a':
161                         util_strlcpy(attr, optarg, sizeof(attr));
162                         val = strchr(attr, '=');
163                         if (val != NULL) {
164                                 val[0] = 0;
165                                 val = &val[1];
166                         }
167                         udev_enumerate_add_match_attr(udev_enumerate, attr, val);
168                         break;
169                 case 'A':
170                         util_strlcpy(attr, optarg, sizeof(attr));
171                         val = strchr(attr, '=');
172                         if (val != NULL) {
173                                 val[0] = 0;
174                                 val = &val[1];
175                         }
176                         udev_enumerate_add_nomatch_attr(udev_enumerate, attr, val);
177                         break;
178                 case 'h':
179                         printf("Usage: udevadm trigger OPTIONS\n"
180                                "  --verbose                       print the list of devices while running\n"
181                                "  --dry-run                       do not actually trigger the events\n"
182                                "  --type=                         type of events to trigger\n"
183                                "      devices                       sys devices\n"
184                                "      subsystems                    sys subsystems and drivers\n"
185                                "      failed                        trigger only the events which have been\n"
186                                "                                    marked as failed during a previous run\n"
187                                "  --subsystem-match=<subsystem>   trigger devices from a matching subystem\n"
188                                "  --subsystem-nomatch=<subsystem> exclude devices from a matching subystem\n"
189                                "  --attr-match=<file[=<value>]>   trigger devices with a matching attribute\n"
190                                "  --attr-nomatch=<file[=<value>]> exclude devices with a matching attribute\n"
191                                "  --help                          print this text\n"
192                                "\n");
193                         goto exit;
194                 default:
195                         goto exit;
196                 }
197         }
198
199         switch (device_type) {
200         case TYPE_FAILED:
201                 scan_failed(udev_enumerate);
202                 exec_list(udev_enumerate, action);
203                 goto exit;
204         case TYPE_SUBSYSTEMS:
205                 udev_enumerate_scan_subsystems(udev_enumerate);
206                 exec_list(udev_enumerate, action);
207                 goto exit;
208         case TYPE_DEVICES:
209                 udev_enumerate_scan_devices(udev_enumerate);
210                 exec_list(udev_enumerate, action);
211                 goto exit;
212         default:
213                 goto exit;
214         }
215 exit:
216         udev_enumerate_unref(udev_enumerate);
217         return rc;
218 }