chiark / gitweb /
update NEWS
[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                 udev_enumerate_add_syspath(udev_enumerate, udev_list_entry_get_name(list_entry));
76         return 0;
77 }
78
79 int udevadm_trigger(struct udev *udev, int argc, char *argv[])
80 {
81         static const struct option options[] = {
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' },
92                 {}
93         };
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;
102
103         dbg(udev, "version %s\n", VERSION);
104         udev_enumerate = udev_enumerate_new(udev);
105         if (udev_enumerate == NULL) {
106                 rc = 1;
107                 goto exit;
108         }
109
110         while (1) {
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);
116                 if (option == -1)
117                         break;
118
119                 switch (option) {
120                 case 'v':
121                         verbose = 1;
122                         break;
123                 case 'n':
124                         dry_run = 1;
125                         break;
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                         }
139                         break;
140                 case 'F':
141                         device_type = TYPE_FAILED;
142                         break;
143                 case 'c':
144                         action = optarg;
145                         break;
146                 case 's':
147                         udev_enumerate_add_match_subsystem(udev_enumerate, optarg);
148                         break;
149                 case 'S':
150                         udev_enumerate_add_nomatch_subsystem(udev_enumerate, optarg);
151                         break;
152                 case 'a':
153                         util_strlcpy(attr, optarg, sizeof(attr));
154                         val = strchr(attr, '=');
155                         if (val != NULL) {
156                                 val[0] = 0;
157                                 val = &val[1];
158                         }
159                         udev_enumerate_add_match_sysattr(udev_enumerate, attr, val);
160                         break;
161                 case 'A':
162                         util_strlcpy(attr, optarg, sizeof(attr));
163                         val = strchr(attr, '=');
164                         if (val != NULL) {
165                                 val[0] = 0;
166                                 val = &val[1];
167                         }
168                         udev_enumerate_add_nomatch_sysattr(udev_enumerate, attr, val);
169                         break;
170                 case 'h':
171                         printf("Usage: udevadm trigger OPTIONS\n"
172                                "  --verbose                       print the list of devices while running\n"
173                                "  --dry-run                       do not actually trigger the events\n"
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"
179                                "  --subsystem-match=<subsystem>   trigger devices from a matching subystem\n"
180                                "  --subsystem-nomatch=<subsystem> exclude devices from a matching subystem\n"
181                                "  --attr-match=<file[=<value>]>   trigger devices with a matching attribute\n"
182                                "  --attr-nomatch=<file[=<value>]> exclude devices with a matching attribute\n"
183                                "  --help                          print this text\n"
184                                "\n");
185                         goto exit;
186                 default:
187                         goto exit;
188                 }
189         }
190
191         switch (device_type) {
192         case TYPE_FAILED:
193                 scan_failed(udev_enumerate);
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:
205                 goto exit;
206         }
207 exit:
208         udev_enumerate_unref(udev_enumerate);
209         return rc;
210 }