chiark / gitweb /
udev: fix two trivial memleaks in error path
[elogind.git] / src / udev / udevadm-trigger.c
1 /*
2  * Copyright (C) 2008-2009 Kay Sievers <kay@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_list_entry *entry;
42
43         udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(udev_enumerate)) {
44                 char filename[UTIL_PATH_SIZE];
45                 int fd;
46
47                 if (verbose)
48                         printf("%s\n", udev_list_entry_get_name(entry));
49                 if (dry_run)
50                         continue;
51                 strscpyl(filename, sizeof(filename), udev_list_entry_get_name(entry), "/uevent", NULL);
52                 fd = open(filename, O_WRONLY);
53                 if (fd < 0)
54                         continue;
55                 if (write(fd, action, strlen(action)) < 0)
56                         log_debug("error writing '%s' to '%s': %m\n", action, filename);
57                 close(fd);
58         }
59 }
60
61 static const char *keyval(const char *str, const char **val, char *buf, size_t size)
62 {
63         char *pos;
64
65         strscpy(buf, size,str);
66         pos = strchr(buf, '=');
67         if (pos != NULL) {
68                 pos[0] = 0;
69                 pos++;
70         }
71         *val = pos;
72         return buf;
73 }
74
75 static int adm_trigger(struct udev *udev, int argc, char *argv[])
76 {
77         static const struct option options[] = {
78                 { "verbose", no_argument, NULL, 'v' },
79                 { "dry-run", no_argument, NULL, 'n' },
80                 { "type", required_argument, NULL, 't' },
81                 { "action", required_argument, NULL, 'c' },
82                 { "subsystem-match", required_argument, NULL, 's' },
83                 { "subsystem-nomatch", required_argument, NULL, 'S' },
84                 { "attr-match", required_argument, NULL, 'a' },
85                 { "attr-nomatch", required_argument, NULL, 'A' },
86                 { "property-match", required_argument, NULL, 'p' },
87                 { "tag-match", required_argument, NULL, 'g' },
88                 { "sysname-match", required_argument, NULL, 'y' },
89                 { "parent-match", required_argument, NULL, 'b' },
90                 { "help", no_argument, NULL, 'h' },
91                 {}
92         };
93         enum {
94                 TYPE_DEVICES,
95                 TYPE_SUBSYSTEMS,
96         } device_type = TYPE_DEVICES;
97         const char *action = "change";
98         struct udev_enumerate *udev_enumerate;
99         int rc = 0;
100
101         udev_enumerate = udev_enumerate_new(udev);
102         if (udev_enumerate == NULL) {
103                 rc = 1;
104                 goto exit;
105         }
106
107         for (;;) {
108                 int option;
109                 const char *key;
110                 const char *val;
111                 char buf[UTIL_PATH_SIZE];
112
113                 option = getopt_long(argc, argv, "vng:o:t:hc:p:s:S:a:A:y:b:", options, NULL);
114                 if (option == -1)
115                         break;
116
117                 switch (option) {
118                 case 'v':
119                         verbose = 1;
120                         break;
121                 case 'n':
122                         dry_run = 1;
123                         break;
124                 case 't':
125                         if (streq(optarg, "devices")) {
126                                 device_type = TYPE_DEVICES;
127                         } else if (streq(optarg, "subsystems")) {
128                                 device_type = TYPE_SUBSYSTEMS;
129                         } else {
130                                 log_error("unknown type --type=%s\n", optarg);
131                                 rc = 2;
132                                 goto exit;
133                         }
134                         break;
135                 case 'c':
136                         action = optarg;
137                         break;
138                 case 's':
139                         udev_enumerate_add_match_subsystem(udev_enumerate, optarg);
140                         break;
141                 case 'S':
142                         udev_enumerate_add_nomatch_subsystem(udev_enumerate, optarg);
143                         break;
144                 case 'a':
145                         key = keyval(optarg, &val, buf, sizeof(buf));
146                         udev_enumerate_add_match_sysattr(udev_enumerate, key, val);
147                         break;
148                 case 'A':
149                         key = keyval(optarg, &val, buf, sizeof(buf));
150                         udev_enumerate_add_nomatch_sysattr(udev_enumerate, key, val);
151                         break;
152                 case 'p':
153                         key = keyval(optarg, &val, buf, sizeof(buf));
154                         udev_enumerate_add_match_property(udev_enumerate, key, val);
155                         break;
156                 case 'g':
157                         udev_enumerate_add_match_tag(udev_enumerate, optarg);
158                         break;
159                 case 'y':
160                         udev_enumerate_add_match_sysname(udev_enumerate, optarg);
161                         break;
162                 case 'b': {
163                         char path[UTIL_PATH_SIZE];
164                         struct udev_device *dev;
165
166                         /* add sys dir if needed */
167                         if (!startswith(optarg, "/sys"))
168                                 strscpyl(path, sizeof(path), "/sys", optarg, NULL);
169                         else
170                                 strscpy(path, sizeof(path), optarg);
171                         util_remove_trailing_chars(path, '/');
172                         dev = udev_device_new_from_syspath(udev, path);
173                         if (dev == NULL) {
174                                 log_error("unable to open the device '%s'\n", optarg);
175                                 rc = 2;
176                                 goto exit;
177                         }
178                         udev_enumerate_add_match_parent(udev_enumerate, dev);
179                         /* drop reference immediately, enumerate pins the device as long as needed */
180                         udev_device_unref(dev);
181                         break;
182                 }
183                 case 'h':
184                         printf("Usage: udevadm trigger OPTIONS\n"
185                                "  --verbose                       print the list of devices while running\n"
186                                "  --dry-run                       do not actually trigger the events\n"
187                                "  --type=                         type of events to trigger\n"
188                                "      devices                       sys devices (default)\n"
189                                "      subsystems                    sys subsystems and drivers\n"
190                                "  --action=<action>               event action value, default is \"change\"\n"
191                                "  --subsystem-match=<subsystem>   trigger devices from a matching subsystem\n"
192                                "  --subsystem-nomatch=<subsystem> exclude devices from a matching subsystem\n"
193                                "  --attr-match=<file[=<value>]>   trigger devices with a matching attribute\n"
194                                "  --attr-nomatch=<file[=<value>]> exclude devices with a matching attribute\n"
195                                "  --property-match=<key>=<value>  trigger devices with a matching property\n"
196                                "  --tag-match=<key>=<value>       trigger devices with a matching property\n"
197                                "  --sysname-match=<name>          trigger devices with a matching name\n"
198                                "  --parent-match=<name>           trigger devices with that parent device\n"
199                                "  --help\n\n");
200                         goto exit;
201                 default:
202                         rc = 1;
203                         goto exit;
204                 }
205         }
206
207         switch (device_type) {
208         case TYPE_SUBSYSTEMS:
209                 udev_enumerate_scan_subsystems(udev_enumerate);
210                 exec_list(udev_enumerate, action);
211                 goto exit;
212         case TYPE_DEVICES:
213                 udev_enumerate_scan_devices(udev_enumerate);
214                 exec_list(udev_enumerate, action);
215                 goto exit;
216         default:
217                 assert_not_reached("device_type");
218         }
219 exit:
220         udev_enumerate_unref(udev_enumerate);
221         return rc;
222 }
223
224 const struct udevadm_cmd udevadm_trigger = {
225         .name = "trigger",
226         .cmd = adm_trigger,
227         .help = "request events from the kernel",
228 };