chiark / gitweb /
use more efficient string copying
[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_strscpyl(filename, sizeof(filename), udev_list_entry_get_name(entry), "/uevent", NULL);
53                 fd = open(filename, O_WRONLY);
54                 if (fd < 0) {
55                         dbg(udev, "error on opening %s: %m\n", filename);
56                         continue;
57                 }
58                 if (write(fd, action, strlen(action)) < 0)
59                         info(udev, "error writing '%s' to '%s': %m\n", action, filename);
60                 close(fd);
61         }
62 }
63
64 static int scan_failed(struct udev_enumerate *udev_enumerate)
65 {
66         struct udev *udev = udev_enumerate_get_udev(udev_enumerate);
67         struct udev_queue *udev_queue;
68         struct udev_list_entry *list_entry;
69
70         udev_queue = udev_queue_new(udev);
71         if (udev_queue == NULL)
72                 return -1;
73         udev_list_entry_foreach(list_entry, udev_queue_get_failed_list_entry(udev_queue))
74                 udev_enumerate_add_syspath(udev_enumerate, udev_list_entry_get_name(list_entry));
75         return 0;
76 }
77
78 int udevadm_trigger(struct udev *udev, int argc, char *argv[])
79 {
80         static const struct option options[] = {
81                 { "verbose", no_argument, NULL, 'v' },
82                 { "dry-run", no_argument, NULL, 'n' },
83                 { "type", required_argument, NULL, 't' },
84                 { "retry-failed", no_argument, NULL, 'F' },
85                 { "action", required_argument, NULL, 'c' },
86                 { "subsystem-match", required_argument, NULL, 's' },
87                 { "subsystem-nomatch", required_argument, NULL, 'S' },
88                 { "attr-match", required_argument, NULL, 'a' },
89                 { "attr-nomatch", required_argument, NULL, 'A' },
90                 { "help", no_argument, NULL, 'h' },
91                 {}
92         };
93         enum {
94                 TYPE_DEVICES,
95                 TYPE_SUBSYSTEMS,
96                 TYPE_FAILED,
97         } device_type = TYPE_DEVICES;
98         const char *action = "add";
99         struct udev_enumerate *udev_enumerate;
100         int rc = 0;
101
102         dbg(udev, "version %s\n", VERSION);
103         udev_enumerate = udev_enumerate_new(udev);
104         if (udev_enumerate == NULL) {
105                 rc = 1;
106                 goto exit;
107         }
108
109         while (1) {
110                 int option;
111                 char attr[UTIL_PATH_SIZE];
112                 char *val;
113
114                 option = getopt_long(argc, argv, "vnFo:t:hce::s:S:a:A:", options, NULL);
115                 if (option == -1)
116                         break;
117
118                 switch (option) {
119                 case 'v':
120                         verbose = 1;
121                         break;
122                 case 'n':
123                         dry_run = 1;
124                         break;
125                 case 't':
126                         if (strcmp(optarg, "devices") == 0) {
127                                 device_type = TYPE_DEVICES;
128                         } else if (strcmp(optarg, "subsystems") == 0) {
129                                 device_type = TYPE_SUBSYSTEMS;
130                         } else if (strcmp(optarg, "failed") == 0) {
131                                 device_type = TYPE_FAILED;
132                         } else {
133                                 fprintf(stderr, "unknown type --type=%s\n", optarg);
134                                 err(udev, "unknown type --type=%s\n", optarg);
135                                 rc = 2;
136                                 goto exit;
137                         }
138                         break;
139                 case 'F':
140                         device_type = TYPE_FAILED;
141                         break;
142                 case 'c':
143                         action = optarg;
144                         break;
145                 case 's':
146                         udev_enumerate_add_match_subsystem(udev_enumerate, optarg);
147                         break;
148                 case 'S':
149                         udev_enumerate_add_nomatch_subsystem(udev_enumerate, optarg);
150                         break;
151                 case 'a':
152                         util_strscpy(attr, sizeof(attr), optarg);
153                         val = strchr(attr, '=');
154                         if (val != NULL) {
155                                 val[0] = 0;
156                                 val = &val[1];
157                         }
158                         udev_enumerate_add_match_sysattr(udev_enumerate, attr, val);
159                         break;
160                 case 'A':
161                         util_strscpy(attr, sizeof(attr), optarg);
162                         val = strchr(attr, '=');
163                         if (val != NULL) {
164                                 val[0] = 0;
165                                 val = &val[1];
166                         }
167                         udev_enumerate_add_nomatch_sysattr(udev_enumerate, attr, val);
168                         break;
169                 case 'h':
170                         printf("Usage: udevadm trigger OPTIONS\n"
171                                "  --verbose                       print the list of devices while running\n"
172                                "  --dry-run                       do not actually trigger the events\n"
173                                "  --type=                         type of events to trigger\n"
174                                "      devices                       sys devices (default)\n"
175                                "      subsystems                    sys subsystems and drivers\n"
176                                "      failed                        trigger only the events which have been\n"
177                                "                                    marked as failed during a previous run\n"
178                                "  --action=<action>               event action value, default is \"add\"\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\n\n");
184                         goto exit;
185                 default:
186                         goto exit;
187                 }
188         }
189
190         switch (device_type) {
191         case TYPE_FAILED:
192                 scan_failed(udev_enumerate);
193                 exec_list(udev_enumerate, action);
194                 goto exit;
195         case TYPE_SUBSYSTEMS:
196                 udev_enumerate_scan_subsystems(udev_enumerate);
197                 exec_list(udev_enumerate, action);
198                 goto exit;
199         case TYPE_DEVICES:
200                 udev_enumerate_scan_devices(udev_enumerate);
201                 exec_list(udev_enumerate, action);
202                 goto exit;
203         default:
204                 goto exit;
205         }
206 exit:
207         udev_enumerate_unref(udev_enumerate);
208         return rc;
209 }