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