chiark / gitweb /
[PATCH] add support for class/raw/ to wait_for_sysfs
[elogind.git] / wait_for_sysfs.c
1 /*
2  * wait_for_sysfs.c  - small program to delay the execution
3  *                     of /etc/hotplug.d/ programs, until sysfs
4  *                     is fully populated by the kernel. Depending on
5  *                     the type of device, we wait for all expected
6  *                     directories and then just exit.
7  *
8  * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
9  *
10  *      This program is free software; you can redistribute it and/or modify it
11  *      under the terms of the GNU General Public License as published by the
12  *      Free Software Foundation version 2 of the License.
13  * 
14  *      This program is distributed in the hope that it will be useful, but
15  *      WITHOUT ANY WARRANTY; without even the implied warranty of
16  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  *      General Public License for more details.
18  * 
19  *      You should have received a copy of the GNU General Public License along
20  *      with this program; if not, write to the Free Software Foundation, Inc.,
21  *      675 Mass Ave, Cambridge, MA 02139, USA.
22  *
23  */
24
25 #include <stdio.h>
26 #include <stddef.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <string.h>
30 #include <ctype.h>
31 #include <errno.h>
32 #include <sys/stat.h>
33
34 #include "logging.h"
35 #include "udev_version.h"
36 #include "libsysfs/sysfs/libsysfs.h"
37
38 #ifdef LOG
39 unsigned char logname[LOGNAME_SIZE];
40 void log_message(int level, const char *format, ...)
41 {
42         va_list args;
43
44         va_start(args, format);
45         vsyslog(level, format, args);
46         va_end(args);
47 }
48 #endif
49
50 #define WAIT_MAX_SECONDS                5
51 #define WAIT_LOOP_PER_SECOND            20
52
53 /* wait for specific file to show up, normally the "dev"-file */
54 static int wait_for_class_device_attributes(struct sysfs_class_device *class_dev)
55 {
56         static struct class_file {
57                 char *subsystem;
58                 char *file;
59         } class_files[] = {
60                 { .subsystem = "net",           .file = "ifindex" },
61                 { .subsystem = "scsi_host",     .file = "unique_id" },
62                 { .subsystem = "scsi_device",   .file = NULL },
63                 { .subsystem = "pcmcia_socket", .file = "card_type" },
64                 { .subsystem = "usb_host",      .file = NULL },
65                 { .subsystem = "bluetooth",     .file = "address" },
66                 { .subsystem = "i2c-adapter",   .file = NULL },
67                 { .subsystem = "pci_bus",       .file = NULL },
68                 { .subsystem = "ieee1394",      .file = NULL },
69                 { .subsystem = "ieee1394_host", .file = NULL },
70                 { .subsystem = "ieee1394_node", .file = NULL },
71                 { NULL, NULL }
72         };
73         struct class_file *classfile;
74         const char *file = "dev";
75         int loop;
76
77         /* look if we want to look for another file instead of "dev" */
78         for (classfile = class_files; classfile->subsystem != NULL; classfile++) {
79                 if (strcmp(class_dev->classname, classfile->subsystem) == 0) {
80                         if (classfile->file == NULL) {
81                                 dbg("class '%s' has no file to wait for", class_dev->classname);
82                                 return 0;
83                         }
84                         file = classfile->file;
85                         break;
86                 }
87         }
88         dbg("looking at class '%s' for specific file '%s'", class_dev->classname, file);
89
90         loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
91         while (--loop) {
92                 if (sysfs_get_classdev_attr(class_dev, file) != NULL) {
93                         dbg("class '%s' specific file '%s' found", class_dev->classname, file);
94                         return 0;
95                 }
96
97                 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
98         }
99
100         dbg("error: getting class '%s' specific file '%s'", class_dev->classname, file);
101         return -1;
102 }
103
104 /* skip waiting for physical device */
105 static int class_device_expect_no_device_link(struct sysfs_class_device *class_dev)
106 {
107         /* List of devices without a "device" symlink
108          * set .device to NULL to accept all devices in that subsystem */
109         static struct class_device {
110                 char *subsystem;
111                 char *device;
112         } class_device[] = {
113                 { .subsystem = "block",         .device = "double" },
114                 { .subsystem = "block",         .device = "nb" },
115                 { .subsystem = "block",         .device = "ram" },
116                 { .subsystem = "block",         .device = "loop" },
117                 { .subsystem = "block",         .device = "fd" },
118                 { .subsystem = "block",         .device = "md" },
119                 { .subsystem = "block",         .device = "dos_cd" },
120                 { .subsystem = "block",         .device = "rflash" },
121                 { .subsystem = "block",         .device = "rom" },
122                 { .subsystem = "block",         .device = "rrom" },
123                 { .subsystem = "block",         .device = "flash" },
124                 { .subsystem = "block",         .device = "msd" },
125                 { .subsystem = "block",         .device = "sbpcd" },
126                 { .subsystem = "block",         .device = "pcd" },
127                 { .subsystem = "block",         .device = "pf" },
128                 { .subsystem = "block",         .device = "scd" },
129                 { .subsystem = "block",         .device = "ubd" },
130                 { .subsystem = "input",         .device = "event" },
131                 { .subsystem = "input",         .device = "mice" },
132                 { .subsystem = "input",         .device = "mouse" },
133                 { .subsystem = "input",         .device = "ts" },
134                 { .subsystem = "vc",            .device = NULL },
135                 { .subsystem = "tty",           .device = NULL },
136                 { .subsystem = "cpuid",         .device = "cpu" },
137                 { .subsystem = "graphics",      .device = "fb" },
138                 { .subsystem = "mem",           .device = NULL },
139                 { .subsystem = "misc",          .device = NULL },
140                 { .subsystem = "msr",           .device = NULL },
141                 { .subsystem = "netlink",       .device = NULL },
142                 { .subsystem = "net",           .device = "sit" },
143                 { .subsystem = "net",           .device = "ppp" },
144                 { .subsystem = "net",           .device = "lo" },
145                 { .subsystem = "net",           .device = "tap" },
146                 { .subsystem = "sound",         .device = NULL },
147                 { .subsystem = "printer",       .device = "lp" },
148                 { .subsystem = "nvidia",        .device = NULL },
149                 { .subsystem = "video4linux",   .device = "vbi" },
150                 { .subsystem = "lirc",          .device = NULL },
151                 { .subsystem = "firmware",      .device = NULL },
152                 { .subsystem = "drm",           .device = NULL },
153                 { .subsystem = "pci_bus",       .device = NULL },
154                 { .subsystem = "ieee1394",      .device = NULL },
155                 { .subsystem = "ieee1394_host", .device = NULL },
156                 { .subsystem = "ieee1394_node", .device = NULL },
157                 { .subsystem = "raw",           .device = NULL },
158                 { NULL, NULL }
159         };
160         struct class_device *classdevice;
161         int len;
162
163         /* look if we want to look for another file instead of "dev" */
164         for (classdevice = class_device; classdevice->subsystem != NULL; classdevice++) {
165                 if (strcmp(class_dev->classname, classdevice->subsystem) == 0) {
166                         /* if device is NULL, all devices in this class are ok */
167                         if (classdevice->device == NULL)
168                                 return 1;
169
170                         len = strlen(classdevice->device);
171
172                         /* see if device name matches */
173                         if (strncmp(class_dev->name, classdevice->device, len) != 0)
174                                 continue;
175
176                         /* exact match */
177                         if (strlen(class_dev->name) == len)
178                                 return 1;
179
180                         /* instance numbers are matching too */
181                         if (isdigit(class_dev->name[len]))
182                                 return 1;
183                 }
184         }
185
186         return 0;
187 }
188
189 /* skip waiting for the bus */
190 static int class_device_expect_no_bus(struct sysfs_class_device *class_dev)
191 {
192         static char *devices_without_bus[] = {
193                 "scsi_host",
194                 "i2c-adapter",
195                 NULL
196         };
197         char **device;
198
199         for (device = devices_without_bus; *device != NULL; device++) {
200                 int len = strlen(*device);
201
202                 if (strncmp(class_dev->classname, *device, len) == 0)
203                         return 1;
204         }
205
206         return 0;
207 }
208
209 /* wait for the bus and for a bus specific file to show up */
210 static int wait_for_bus_device(struct sysfs_device *device_dev)
211 {
212         static struct bus_file {
213                 char *bus;
214                 char *file;
215         } bus_files[] = {
216                 { .bus = "scsi",        .file = "vendor" },
217                 { .bus = "usb",         .file = "idVendor" },
218                 { .bus = "usb",         .file = "iInterface" },
219                 { .bus = "usb",         .file = "bNumEndpoints" },
220                 { .bus = "usb-serial",  .file = "detach_state" },
221                 { .bus = "ide",         .file = "detach_state" },
222                 { .bus = "pci",         .file = "vendor" },
223                 { .bus = "platform",    .file = "detach_state" },
224                 { .bus = "i2c",         .file = "detach_state" },
225                 { NULL }
226         };
227         struct bus_file *busfile;
228         int loop;
229
230         /* wait for the /bus-device link to the /device-device */
231         loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
232         while (--loop) {
233                 if (sysfs_get_device_bus(device_dev) == 0)
234                         break;
235
236                 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
237         }
238         if (loop == 0) {
239                 dbg("error: getting /bus-device link");
240                 return -1;
241         }
242         dbg("/bus-device link found for bus '%s'", device_dev->bus);
243
244         /* wait for a bus specific file to show up */
245         loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
246         while (--loop) {
247                 int found = 0;
248
249                 for (busfile = bus_files; busfile->bus != NULL; busfile++) {
250                         if (strcmp(device_dev->bus, busfile->bus) == 0) {
251                                 found = 1;
252                                 dbg("looking at bus '%s' for specific file '%s'", device_dev->bus, busfile->file);
253                                 if (sysfs_get_device_attr(device_dev, busfile->file) != NULL) {
254                                         dbg("bus '%s' specific file '%s' found", device_dev->bus, busfile->file);
255                                         return 0;
256                                 }
257                         }
258                 }
259                 if (found == 0) {
260                         info("error: unknown bus, please report to "
261                              "<linux-hotplug-devel@lists.sourceforge.net> '%s'", device_dev->bus);
262                         return -1;
263                 }
264                 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
265         }
266
267         dbg("error: getting bus '%s' specific file '%s'", device_dev->bus, busfile->file);
268         return -1;
269 }
270
271 int main(int argc, char *argv[], char *envp[])
272 {
273         const char *devpath = "";
274         const char *action;
275         const char *subsystem;
276         char sysfs_path[SYSFS_PATH_MAX];
277         char filename[SYSFS_PATH_MAX];
278         struct sysfs_class_device *class_dev;
279         struct sysfs_class_device *class_dev_parent;
280         struct sysfs_device *device_dev = NULL;
281         int loop;
282         int rc = 0;
283
284         init_logging("wait_for_sysfs");
285
286         if (argc != 2) {
287                 dbg("error: subsystem");
288                 return 1;
289         }
290         subsystem = argv[1];
291
292         devpath = getenv ("DEVPATH");
293         if (!devpath) {
294                 dbg("error: no DEVPATH");
295                 return 1;
296         }
297
298         action = getenv ("ACTION");
299         if (!action) {
300                 dbg("error: no ACTION");
301                 return 1;
302         }
303
304         /* we only wait on an add event */
305         if (strcmp(action, "add") != 0)
306                 return 0;
307
308         if (sysfs_get_mnt_path(sysfs_path, SYSFS_PATH_MAX) != 0) {
309                 dbg("error: no sysfs path");
310                 return 2;
311         }
312
313         if ((strncmp(devpath, "/block/", 7) == 0) || (strncmp(devpath, "/class/", 7) == 0)) {
314                 /* open the class device we are called for */
315                 snprintf(filename, SYSFS_PATH_MAX-1, "%s%s", sysfs_path, devpath);
316                 filename[SYSFS_PATH_MAX-1] = '\0';
317
318                 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
319                 while (--loop) {
320                         class_dev = sysfs_open_class_device_path(filename);
321                         if (class_dev)
322                                 break;
323
324                         usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
325                 }
326                 if (class_dev == NULL) {
327                         dbg("error: getting class_device");
328                         rc = 3;
329                         goto exit;
330                 }
331                 dbg("class_device opened '%s'", filename);
332
333                 if (wait_for_class_device_attributes(class_dev) != 0) {
334                         rc = 4;
335                         goto exit_class;
336                 }
337
338                 /* skip devices without /device-link */
339                 if (class_device_expect_no_device_link(class_dev)) {
340                         dbg("no device symlink expected for '%s', ", class_dev->name);
341                         goto exit_class;
342                 }
343
344                 /* the symlink may be on the parent device */
345                 class_dev_parent = sysfs_get_classdev_parent(class_dev);
346                 if (class_dev_parent)
347                         dbg("looking at parent device for device link '%s'", class_dev_parent->path);
348
349                 /* wait for the symlink to the /device-device */
350                 dbg("waiting for symlink to /device-device");
351                 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
352                 while (--loop) {
353                         if (class_dev_parent)
354                                 device_dev = sysfs_get_classdev_device(class_dev_parent);
355                         else
356                                 device_dev = sysfs_get_classdev_device(class_dev);
357
358                         if (device_dev)
359                                 break;
360
361                         usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
362                 }
363                 if (device_dev == NULL) {
364                         dbg("error: getting /device-device");
365                         rc = 5;
366                         goto exit_class;
367                 }
368                 dbg("device symlink found pointing to '%s'", device_dev->path);
369
370                 /* wait for the bus value */
371                 if (class_device_expect_no_bus(class_dev)) {
372                         dbg("no bus device expected for '%s', ", class_dev->classname);
373                 } else {
374                         if (wait_for_bus_device(device_dev) != 0)
375                                 rc = 6;
376                 }
377
378 exit_class:
379                 sysfs_close_class_device(class_dev);
380
381         } else if ((strncmp(devpath, "/devices/", 9) == 0)) {
382                 /* open the path we are called for */
383                 snprintf(filename, SYSFS_PATH_MAX-1, "%s%s", sysfs_path, devpath);
384                 filename[SYSFS_PATH_MAX-1] = '\0';
385
386                 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
387                 while (--loop) {
388                         device_dev = sysfs_open_device_path(filename);
389                         if (device_dev)
390                                 break;
391
392                         usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
393                 }
394                 if (device_dev == NULL) {
395                         dbg("error: getting /device-device");
396                         rc = 7;
397                         goto exit;
398                 }
399                 dbg("device_device opened '%s'", filename);
400
401                 /* wait for the bus value */
402                 if (wait_for_bus_device(device_dev) != 0)
403                         rc = 8;
404
405                 sysfs_close_device(device_dev);
406
407         } else {
408                 dbg("unhandled sysfs path, no need to wait");
409         }
410
411 exit:
412         if (rc == 0)
413                 dbg("result: waiting for sysfs successful '%s'", devpath);
414         else
415                 info("either wait_for_sysfs (udev %s) needs an update to handle the device '%s' "
416                      "properly (%d) or the sysfs-support of your device's driver needs to be fixed, "
417                      "please report to <linux-hotplug-devel@lists.sourceforge.net>",
418                      UDEV_VERSION, devpath, rc);
419
420         return rc;
421 }