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.
8 * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
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.
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.
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.
35 #include "udev_version.h"
36 #include "libsysfs/sysfs/libsysfs.h"
39 unsigned char logname[LOGNAME_SIZE];
40 void log_message(int level, const char *format, ...)
44 va_start(args, format);
45 vsyslog(level, format, args);
50 #define WAIT_MAX_SECONDS 10
51 #define WAIT_LOOP_PER_SECOND 20
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)
56 static struct class_file {
60 { .subsystem = "net", .file = "ifindex" },
61 { .subsystem = "scsi_host", .file = "unique_id" },
62 { .subsystem = "scsi_device", .file = NULL },
63 { .subsystem = "pcmcia_socket", .file = NULL }, /* all files are unreadable in empty slot :( */
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 },
73 struct class_file *classfile;
74 const char *file = "dev";
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);
84 file = classfile->file;
88 dbg("looking at class '%s' for specific file '%s'", class_dev->classname, file);
90 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
92 if (sysfs_get_classdev_attr(class_dev, file) != NULL) {
93 dbg("class '%s' specific file '%s' found", class_dev->classname, file);
97 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
100 dbg("error: getting class '%s' specific file '%s'", class_dev->classname, file);
104 /* check if we need to wait for a physical device */
105 static int class_device_expect_no_device_link(struct sysfs_class_device *class_dev)
107 /* list of devices without a "device" symlink to the physical device
108 * if device is set to NULL, no devices in that subsystem has a link */
109 static struct 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 = "net", .device = "ipsec" },
147 { .subsystem = "sound", .device = NULL },
148 { .subsystem = "printer", .device = "lp" },
149 { .subsystem = "nvidia", .device = NULL },
150 { .subsystem = "video4linux", .device = "vbi" },
151 { .subsystem = "lirc", .device = NULL },
152 { .subsystem = "firmware", .device = NULL },
153 { .subsystem = "drm", .device = NULL },
154 { .subsystem = "pci_bus", .device = NULL },
155 { .subsystem = "ieee1394", .device = NULL },
156 { .subsystem = "ieee1394_host", .device = NULL },
157 { .subsystem = "ieee1394_node", .device = NULL },
158 { .subsystem = "raw", .device = NULL },
161 struct class_device *classdevice;
164 for (classdevice = class_device; classdevice->subsystem != NULL; classdevice++) {
165 if (strcmp(class_dev->classname, classdevice->subsystem) == 0) {
166 /* see if no device in this class is expected to have a device-link */
167 if (classdevice->device == NULL)
170 len = strlen(classdevice->device);
172 /* see if device name matches */
173 if (strncmp(class_dev->name, classdevice->device, len) != 0)
176 /* exact name match */
177 if (strlen(class_dev->name) == len)
180 /* name match with instance number */
181 if (isdigit(class_dev->name[len]))
189 /* skip waiting for the bus */
190 static int class_device_expect_no_bus(struct sysfs_class_device *class_dev)
192 static char *devices_without_bus[] = {
199 for (device = devices_without_bus; *device != NULL; device++) {
200 int len = strlen(*device);
202 if (strncmp(class_dev->classname, *device, len) == 0)
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)
212 static struct bus_file {
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" },
227 struct bus_file *busfile;
230 /* wait for the /bus-device link to the /device-device */
231 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
233 if (sysfs_get_device_bus(device_dev) == 0)
236 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
239 dbg("error: getting /bus-device link");
242 dbg("/bus-device link found for bus '%s'", device_dev->bus);
244 /* wait for a bus specific file to show up */
245 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
249 for (busfile = bus_files; busfile->bus != NULL; busfile++) {
250 if (strcmp(device_dev->bus, busfile->bus) == 0) {
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);
260 info("error: unknown bus, please report to "
261 "<linux-hotplug-devel@lists.sourceforge.net> '%s'", device_dev->bus);
264 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
267 dbg("error: getting bus '%s' specific file '%s'", device_dev->bus, busfile->file);
271 int main(int argc, char *argv[], char *envp[])
273 const char *devpath = "";
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;
284 init_logging("wait_for_sysfs");
287 dbg("error: subsystem");
292 devpath = getenv ("DEVPATH");
294 dbg("error: no DEVPATH");
298 action = getenv ("ACTION");
300 dbg("error: no ACTION");
304 /* we only wait on an add event */
305 if (strcmp(action, "add") != 0)
308 if (sysfs_get_mnt_path(sysfs_path, SYSFS_PATH_MAX) != 0) {
309 dbg("error: no sysfs path");
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';
318 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
320 class_dev = sysfs_open_class_device_path(filename);
324 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
326 if (class_dev == NULL) {
327 dbg("error: getting class_device");
331 dbg("class_device opened '%s'", filename);
333 if (wait_for_class_device_attributes(class_dev) != 0) {
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);
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);
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;
353 if (class_dev_parent)
354 device_dev = sysfs_get_classdev_device(class_dev_parent);
356 device_dev = sysfs_get_classdev_device(class_dev);
361 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
363 if (device_dev == NULL) {
364 dbg("error: getting /device-device");
368 dbg("device symlink found pointing to '%s'", device_dev->path);
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);
374 if (wait_for_bus_device(device_dev) != 0)
379 sysfs_close_class_device(class_dev);
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';
386 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
388 device_dev = sysfs_open_device_path(filename);
392 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
394 if (device_dev == NULL) {
395 dbg("error: getting /device-device");
399 dbg("device_device opened '%s'", filename);
401 /* wait for the bus value */
402 if (wait_for_bus_device(device_dev) != 0)
405 sysfs_close_device(device_dev);
408 dbg("unhandled sysfs path, no need to wait");
413 dbg("result: waiting for sysfs successful '%s'", devpath);
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);