tizen 2.4 release
[external/systemd.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 <fnmatch.h>
28 #include <sys/stat.h>
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <sys/un.h>
32
33 #include "udev.h"
34 #include "udev-util.h"
35 #include "util.h"
36
37 static int verbose;
38 static int dry_run;
39
40 static void exec_list(struct udev_enumerate *udev_enumerate, const char *action) {
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|O_CLOEXEC);
53                 if (fd < 0)
54                         continue;
55                 if (write(fd, action, strlen(action)) < 0)
56                         log_debug("error writing '%s' to '%s': %m", 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         char *pos;
63
64         strscpy(buf, size,str);
65         pos = strchr(buf, '=');
66         if (pos != NULL) {
67                 pos[0] = 0;
68                 pos++;
69         }
70         *val = pos;
71         return buf;
72 }
73
74 static void help(void) {
75         printf("Usage: udevadm trigger OPTIONS\n"
76                "  -v,--verbose                       print the list of devices while running\n"
77                "  -n,--dry-run                       do not actually trigger the events\n"
78                "  -t,--type=                         type of events to trigger\n"
79                "          devices                       sys devices (default)\n"
80                "          subsystems                    sys subsystems and drivers\n"
81                "  -c,--action=<action>               event action value, default is \"change\"\n"
82                "  -s,--subsystem-match=<subsystem>   trigger devices from a matching subsystem\n"
83                "  -S,--subsystem-nomatch=<subsystem> exclude devices from a matching subsystem\n"
84                "  -a,--attr-match=<file[=<value>]>   trigger devices with a matching attribute\n"
85                "  -A,--attr-nomatch=<file[=<value>]> exclude devices with a matching attribute\n"
86                "  -p,--property-match=<key>=<value>  trigger devices with a matching property\n"
87                "  -g,--tag-match=<key>=<value>       trigger devices with a matching property\n"
88                "  -y,--sysname-match=<name>          trigger devices with a matching name\n"
89                "  -b,--parent-match=<name>           trigger devices with that parent device\n"
90                "  -h,--help\n\n");
91 }
92
93 static int adm_trigger(struct udev *udev, int argc, char *argv[]) {
94         static const struct option options[] = {
95                 { "verbose",           no_argument,       NULL, 'v' },
96                 { "dry-run",           no_argument,       NULL, 'n' },
97                 { "type",              required_argument, NULL, 't' },
98                 { "action",            required_argument, NULL, 'c' },
99                 { "subsystem-match",   required_argument, NULL, 's' },
100                 { "subsystem-nomatch", required_argument, NULL, 'S' },
101                 { "attr-match",        required_argument, NULL, 'a' },
102                 { "attr-nomatch",      required_argument, NULL, 'A' },
103                 { "property-match",    required_argument, NULL, 'p' },
104                 { "tag-match",         required_argument, NULL, 'g' },
105                 { "sysname-match",     required_argument, NULL, 'y' },
106                 { "parent-match",      required_argument, NULL, 'b' },
107                 { "help",              no_argument,       NULL, 'h' },
108                 {}
109         };
110         enum {
111                 TYPE_DEVICES,
112                 TYPE_SUBSYSTEMS,
113         } device_type = TYPE_DEVICES;
114         const char *action = "change";
115         _cleanup_udev_enumerate_unref_ struct udev_enumerate *udev_enumerate = NULL;
116         int c;
117
118         udev_enumerate = udev_enumerate_new(udev);
119         if (udev_enumerate == NULL)
120                 return 1;
121
122         while ((c = getopt_long(argc, argv, "vno:t:c:s:S:a:A:p:g:y:b:h", options, NULL)) >= 0) {
123                 const char *key;
124                 const char *val;
125                 char buf[UTIL_PATH_SIZE];
126
127                 switch (c) {
128                 case 'v':
129                         verbose = 1;
130                         break;
131                 case 'n':
132                         dry_run = 1;
133                         break;
134                 case 't':
135                         if (streq(optarg, "devices"))
136                                 device_type = TYPE_DEVICES;
137                         else if (streq(optarg, "subsystems"))
138                                 device_type = TYPE_SUBSYSTEMS;
139                         else {
140                                 log_error("unknown type --type=%s", optarg);
141                                 return 2;
142                         }
143                         break;
144                 case 'c':
145                         if (!nulstr_contains("add\0" "remove\0" "change\0", optarg)) {
146                                 log_error("unknown action '%s'", optarg);
147                                 return 2;
148                         } else
149                                 action = optarg;
150
151                         break;
152                 case 's':
153                         udev_enumerate_add_match_subsystem(udev_enumerate, optarg);
154                         break;
155                 case 'S':
156                         udev_enumerate_add_nomatch_subsystem(udev_enumerate, optarg);
157                         break;
158                 case 'a':
159                         key = keyval(optarg, &val, buf, sizeof(buf));
160                         udev_enumerate_add_match_sysattr(udev_enumerate, key, val);
161                         break;
162                 case 'A':
163                         key = keyval(optarg, &val, buf, sizeof(buf));
164                         udev_enumerate_add_nomatch_sysattr(udev_enumerate, key, val);
165                         break;
166                 case 'p':
167                         key = keyval(optarg, &val, buf, sizeof(buf));
168                         udev_enumerate_add_match_property(udev_enumerate, key, val);
169                         break;
170                 case 'g':
171                         udev_enumerate_add_match_tag(udev_enumerate, optarg);
172                         break;
173                 case 'y':
174                         udev_enumerate_add_match_sysname(udev_enumerate, optarg);
175                         break;
176                 case 'b': {
177                         char path[UTIL_PATH_SIZE];
178                         struct udev_device *dev;
179
180                         /* add sys dir if needed */
181                         if (!startswith(optarg, "/sys"))
182                                 strscpyl(path, sizeof(path), "/sys", optarg, NULL);
183                         else
184                                 strscpy(path, sizeof(path), optarg);
185                         util_remove_trailing_chars(path, '/');
186                         dev = udev_device_new_from_syspath(udev, path);
187                         if (dev == NULL) {
188                                 log_error("unable to open the device '%s'", optarg);
189                                 return 2;
190                         }
191                         udev_enumerate_add_match_parent(udev_enumerate, dev);
192                         /* drop reference immediately, enumerate pins the device as long as needed */
193                         udev_device_unref(dev);
194                         break;
195                 }
196                 case 'h':
197                         help();
198                         return 0;
199                 case '?':
200                         return 1;
201                 default:
202                         assert_not_reached("Unknown option");
203                 }
204         }
205
206         if (optind < argc) {
207                 fprintf(stderr, "Extraneous argument: '%s'\n", argv[optind]);
208                 return 1;
209         }
210
211         switch (device_type) {
212         case TYPE_SUBSYSTEMS:
213                 udev_enumerate_scan_subsystems(udev_enumerate);
214                 exec_list(udev_enumerate, action);
215                 return 0;
216         case TYPE_DEVICES:
217                 udev_enumerate_scan_devices(udev_enumerate);
218                 exec_list(udev_enumerate, action);
219                 return 0;
220         default:
221                 assert_not_reached("device_type");
222         }
223 }
224
225 const struct udevadm_cmd udevadm_trigger = {
226         .name = "trigger",
227         .cmd = adm_trigger,
228         .help = "request events from the kernel",
229 };