udevtrigger: add device_has_attribute helper
[project/procd.git] / udevtrigger.c
1 /*
2 * Copyright (C) 2004-2006 Kay Sievers <kay@vrfy.org>
3 * Copyright (C) 2006 Hannes Reinecke <hare@suse.de>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation version 2 of the License.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 *
18 */
19
20 #include <stdlib.h>
21 #include <stddef.h>
22 #include <stdarg.h>
23 #include <stdio.h>
24 #include <stdbool.h>
25 #include <unistd.h>
26 #include <getopt.h>
27 #include <errno.h>
28 #include <dirent.h>
29 #include <fcntl.h>
30 #include <syslog.h>
31 #include <sys/stat.h>
32 #include <sys/types.h>
33 #include <string.h>
34
35 #define PATH_SIZE 512
36
37 #ifndef strlcpy
38 #define strlcpy(d,s,l) (strncpy(d,s,l), (d)[(l)-1] = '\0')
39 #endif
40
41 #ifndef strlcat
42 #define strlcat(d,s,l) strncat(d,s,(l)-strlen(d)-1)
43 #endif
44
45 static int verbose;
46 static int dry_run;
47
48 static void log_message(int priority, const char *format, ...)
49 {
50 va_list args;
51
52 va_start(args, format);
53 vsyslog(priority, format, args);
54 va_end(args);
55 }
56
57 #undef err
58 #define err(format, arg...) \
59 do { \
60 log_message(LOG_ERR ,"%s: " format ,__FUNCTION__ ,## arg); \
61 } while (0)
62
63 #undef info
64 #define info(format, arg...) \
65 do { \
66 log_message(LOG_INFO ,"%s: " format ,__FUNCTION__ ,## arg); \
67 } while (0)
68
69 #ifdef DEBUG
70 #undef dbg
71 #define dbg(format, arg...) \
72 do { \
73 log_message(LOG_DEBUG ,"%s: " format ,__FUNCTION__ ,## arg); \
74 } while (0)
75 #else
76 #define dbg(...) do {} while(0)
77 #endif
78
79
80 static void trigger_uevent(const char *devpath)
81 {
82 char filename[PATH_SIZE];
83 int fd;
84
85 strlcpy(filename, "/sys", sizeof(filename));
86 strlcat(filename, devpath, sizeof(filename));
87 strlcat(filename, "/uevent", sizeof(filename));
88
89 if (verbose)
90 printf("%s\n", devpath);
91
92 if (dry_run)
93 return;
94
95 fd = open(filename, O_WRONLY);
96 if (fd < 0) {
97 dbg("error on opening %s: %s\n", filename, strerror(errno));
98 return;
99 }
100
101 if (write(fd, "add", 3) < 0)
102 info("error on triggering %s: %s\n", filename, strerror(errno));
103
104 close(fd);
105 }
106
107 static int sysfs_resolve_link(char *devpath, size_t size)
108 {
109 char link_path[PATH_SIZE];
110 char link_target[PATH_SIZE];
111 int len;
112 int i;
113 int back;
114
115 strlcpy(link_path, "/sys", sizeof(link_path));
116 strlcat(link_path, devpath, sizeof(link_path));
117 len = readlink(link_path, link_target, sizeof(link_target));
118 if (len <= 0)
119 return -1;
120 link_target[len] = '\0';
121 dbg("path link '%s' points to '%s'", devpath, link_target);
122
123 for (back = 0; strncmp(&link_target[back * 3], "../", 3) == 0; back++)
124 ;
125 dbg("base '%s', tail '%s', back %i", devpath, &link_target[back * 3], back);
126 for (i = 0; i <= back; i++) {
127 char *pos = strrchr(devpath, '/');
128
129 if (pos == NULL)
130 return -1;
131 pos[0] = '\0';
132 }
133 dbg("after moving back '%s'", devpath);
134 strlcat(devpath, "/", size);
135 strlcat(devpath, &link_target[back * 3], size);
136 return 0;
137 }
138
139 static bool device_has_attribute(const char *path, const char *attr,
140 mode_t mode)
141 {
142 char filename[PATH_SIZE];
143 struct stat statbuf;
144
145 strlcpy(filename, path, sizeof(filename));
146 strlcat(filename, attr, sizeof(filename));
147
148 if (stat(filename, &statbuf) < 0)
149 return false;
150
151 if (!(statbuf.st_mode & mode))
152 return false;
153
154 return true;
155 }
156
157 static int device_list_insert(const char *path)
158 {
159 char devpath[PATH_SIZE];
160 struct stat statbuf;
161
162 dbg("add '%s'" , path);
163
164 /* we only have a device, if we have an uevent file */
165 if (!device_has_attribute(path, "/uevent", S_IWUSR))
166 return -1;
167
168 strlcpy(devpath, &path[4], sizeof(devpath));
169
170 /* resolve possible link to real target */
171 if (lstat(path, &statbuf) < 0)
172 return -1;
173 if (S_ISLNK(statbuf.st_mode))
174 if (sysfs_resolve_link(devpath, sizeof(devpath)) != 0)
175 return -1;
176
177 trigger_uevent(devpath);
178 return 0;
179 }
180
181
182 static void scan_subsystem(const char *subsys)
183 {
184 char base[PATH_SIZE];
185 DIR *dir;
186 struct dirent *dent;
187
188 strlcpy(base, "/sys/", sizeof(base));
189 strlcat(base, subsys, sizeof(base));
190
191 dir = opendir(base);
192 if (dir != NULL) {
193 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
194 char dirname[PATH_SIZE];
195 DIR *dir2;
196 struct dirent *dent2;
197
198 if (dent->d_name[0] == '.')
199 continue;
200
201 strlcpy(dirname, base, sizeof(dirname));
202 strlcat(dirname, "/", sizeof(dirname));
203 strlcat(dirname, dent->d_name, sizeof(dirname));
204 strlcat(dirname, "/devices", sizeof(dirname));
205
206 /* look for devices */
207 dir2 = opendir(dirname);
208 if (dir2 != NULL) {
209 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
210 char dirname2[PATH_SIZE];
211
212 if (dent2->d_name[0] == '.')
213 continue;
214
215 strlcpy(dirname2, dirname, sizeof(dirname2));
216 strlcat(dirname2, "/", sizeof(dirname2));
217 strlcat(dirname2, dent2->d_name, sizeof(dirname2));
218 device_list_insert(dirname2);
219 }
220 closedir(dir2);
221 }
222 }
223 closedir(dir);
224 }
225 }
226
227 static void scan_block(void)
228 {
229 char base[PATH_SIZE];
230 DIR *dir;
231 struct dirent *dent;
232
233 strlcpy(base, "/sys/block", sizeof(base));
234
235 dir = opendir(base);
236 if (dir != NULL) {
237 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
238 char dirname[PATH_SIZE];
239 DIR *dir2;
240 struct dirent *dent2;
241
242 if (dent->d_name[0] == '.')
243 continue;
244
245 strlcpy(dirname, base, sizeof(dirname));
246 strlcat(dirname, "/", sizeof(dirname));
247 strlcat(dirname, dent->d_name, sizeof(dirname));
248 if (device_list_insert(dirname) != 0)
249 continue;
250
251 /* look for partitions */
252 dir2 = opendir(dirname);
253 if (dir2 != NULL) {
254 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
255 char dirname2[PATH_SIZE];
256
257 if (dent2->d_name[0] == '.')
258 continue;
259
260 if (!strcmp(dent2->d_name,"device"))
261 continue;
262
263 strlcpy(dirname2, dirname, sizeof(dirname2));
264 strlcat(dirname2, "/", sizeof(dirname2));
265 strlcat(dirname2, dent2->d_name, sizeof(dirname2));
266 device_list_insert(dirname2);
267 }
268 closedir(dir2);
269 }
270 }
271 closedir(dir);
272 }
273 }
274
275 static void scan_class(void)
276 {
277 char base[PATH_SIZE];
278 DIR *dir;
279 struct dirent *dent;
280
281 strlcpy(base, "/sys/class", sizeof(base));
282
283 dir = opendir(base);
284 if (dir != NULL) {
285 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
286 char dirname[PATH_SIZE];
287 DIR *dir2;
288 struct dirent *dent2;
289
290 if (dent->d_name[0] == '.')
291 continue;
292
293 strlcpy(dirname, base, sizeof(dirname));
294 strlcat(dirname, "/", sizeof(dirname));
295 strlcat(dirname, dent->d_name, sizeof(dirname));
296 dir2 = opendir(dirname);
297 if (dir2 != NULL) {
298 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
299 char dirname2[PATH_SIZE];
300
301 if (dent2->d_name[0] == '.')
302 continue;
303
304 if (!strcmp(dent2->d_name, "device"))
305 continue;
306
307 strlcpy(dirname2, dirname, sizeof(dirname2));
308 strlcat(dirname2, "/", sizeof(dirname2));
309 strlcat(dirname2, dent2->d_name, sizeof(dirname2));
310 device_list_insert(dirname2);
311 }
312 closedir(dir2);
313 }
314 }
315 closedir(dir);
316 }
317 }
318
319 int main(int argc, char *argv[], char *envp[])
320 {
321 char base[PATH_SIZE];
322 struct stat statbuf;
323 int option;
324
325 openlog("udevtrigger", LOG_PID | LOG_CONS, LOG_DAEMON);
326
327 while (1) {
328 option = getopt(argc, argv, "vnh");
329 if (option == -1)
330 break;
331
332 switch (option) {
333 case 'v':
334 verbose = 1;
335 break;
336 case 'n':
337 dry_run = 1;
338 break;
339 case 'h':
340 printf("Usage: udevtrigger OPTIONS\n"
341 " -v print the list of devices while running\n"
342 " -n do not actually trigger the events\n"
343 " -h print this text\n"
344 "\n");
345 goto exit;
346 default:
347 goto exit;
348 }
349 }
350
351
352 /* if we have /sys/subsystem, forget all the old stuff */
353 scan_subsystem("bus");
354 scan_class();
355
356 /* scan "block" if it isn't a "class" */
357 strlcpy(base, "/sys/class/block", sizeof(base));
358 if (stat(base, &statbuf) != 0)
359 scan_block();
360
361 exit:
362
363 closelog();
364 return 0;
365 }