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 = "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 },
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 = "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 },
160 struct class_device *classdevice;
163 for (classdevice = class_device; classdevice->subsystem != NULL; classdevice++) {
164 if (strcmp(class_dev->classname, classdevice->subsystem) == 0) {
165 /* see if no device in this class is expected to have a device-link */
166 if (classdevice->device == NULL)
169 len = strlen(classdevice->device);
171 /* see if device name matches */
172 if (strncmp(class_dev->name, classdevice->device, len) != 0)
175 /* exact name match */
176 if (strlen(class_dev->name) == len)
179 /* name match with instance number */
180 if (isdigit(class_dev->name[len]))
188 /* skip waiting for the bus */
189 static int class_device_expect_no_bus(struct sysfs_class_device *class_dev)
191 static char *devices_without_bus[] = {
198 for (device = devices_without_bus; *device != NULL; device++) {
199 int len = strlen(*device);
201 if (strncmp(class_dev->classname, *device, len) == 0)
208 /* wait for the bus and for a bus specific file to show up */
209 static int wait_for_bus_device(struct sysfs_device *device_dev)
211 static struct bus_file {
215 { .bus = "scsi", .file = "vendor" },
216 { .bus = "usb", .file = "idVendor" },
217 { .bus = "usb", .file = "iInterface" },
218 { .bus = "usb", .file = "bNumEndpoints" },
219 { .bus = "usb-serial", .file = "detach_state" },
220 { .bus = "ide", .file = "detach_state" },
221 { .bus = "pci", .file = "vendor" },
222 { .bus = "platform", .file = "detach_state" },
223 { .bus = "i2c", .file = "detach_state" },
226 struct bus_file *busfile;
229 /* wait for the /bus-device link to the /device-device */
230 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
232 if (sysfs_get_device_bus(device_dev) == 0)
235 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
238 dbg("error: getting /bus-device link");
241 dbg("/bus-device link found for bus '%s'", device_dev->bus);
243 /* wait for a bus specific file to show up */
244 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
248 for (busfile = bus_files; busfile->bus != NULL; busfile++) {
249 if (strcmp(device_dev->bus, busfile->bus) == 0) {
251 dbg("looking at bus '%s' for specific file '%s'", device_dev->bus, busfile->file);
252 if (sysfs_get_device_attr(device_dev, busfile->file) != NULL) {
253 dbg("bus '%s' specific file '%s' found", device_dev->bus, busfile->file);
259 info("error: unknown bus, please report to "
260 "<linux-hotplug-devel@lists.sourceforge.net> '%s'", device_dev->bus);
263 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
266 dbg("error: getting bus '%s' specific file '%s'", device_dev->bus, busfile->file);
270 int main(int argc, char *argv[], char *envp[])
272 const char *devpath = "";
274 const char *subsystem;
275 char sysfs_path[SYSFS_PATH_MAX];
276 char filename[SYSFS_PATH_MAX];
277 struct sysfs_class_device *class_dev;
278 struct sysfs_class_device *class_dev_parent;
279 struct sysfs_device *device_dev = NULL;
283 init_logging("wait_for_sysfs");
286 dbg("error: subsystem");
291 devpath = getenv ("DEVPATH");
293 dbg("error: no DEVPATH");
297 action = getenv ("ACTION");
299 dbg("error: no ACTION");
303 /* we only wait on an add event */
304 if (strcmp(action, "add") != 0)
307 if (sysfs_get_mnt_path(sysfs_path, SYSFS_PATH_MAX) != 0) {
308 dbg("error: no sysfs path");
312 if ((strncmp(devpath, "/block/", 7) == 0) || (strncmp(devpath, "/class/", 7) == 0)) {
313 /* open the class device we are called for */
314 snprintf(filename, SYSFS_PATH_MAX-1, "%s%s", sysfs_path, devpath);
315 filename[SYSFS_PATH_MAX-1] = '\0';
317 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
319 class_dev = sysfs_open_class_device_path(filename);
323 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
325 if (class_dev == NULL) {
326 dbg("error: getting class_device");
330 dbg("class_device opened '%s'", filename);
332 if (wait_for_class_device_attributes(class_dev) != 0) {
337 /* skip devices without /device-link */
338 if (class_device_expect_no_device_link(class_dev)) {
339 dbg("no device symlink expected for '%s', ", class_dev->name);
343 /* the symlink may be on the parent device */
344 class_dev_parent = sysfs_get_classdev_parent(class_dev);
345 if (class_dev_parent)
346 dbg("looking at parent device for device link '%s'", class_dev_parent->path);
348 /* wait for the symlink to the /device-device */
349 dbg("waiting for symlink to /device-device");
350 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
352 if (class_dev_parent)
353 device_dev = sysfs_get_classdev_device(class_dev_parent);
355 device_dev = sysfs_get_classdev_device(class_dev);
360 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
362 if (device_dev == NULL) {
363 dbg("error: getting /device-device");
367 dbg("device symlink found pointing to '%s'", device_dev->path);
369 /* wait for the bus value */
370 if (class_device_expect_no_bus(class_dev)) {
371 dbg("no bus device expected for '%s', ", class_dev->classname);
373 if (wait_for_bus_device(device_dev) != 0)
378 sysfs_close_class_device(class_dev);
380 } else if ((strncmp(devpath, "/devices/", 9) == 0)) {
381 /* open the path we are called for */
382 snprintf(filename, SYSFS_PATH_MAX-1, "%s%s", sysfs_path, devpath);
383 filename[SYSFS_PATH_MAX-1] = '\0';
385 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
387 device_dev = sysfs_open_device_path(filename);
391 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
393 if (device_dev == NULL) {
394 dbg("error: getting /device-device");
398 dbg("device_device opened '%s'", filename);
400 /* wait for the bus value */
401 if (wait_for_bus_device(device_dev) != 0)
404 sysfs_close_device(device_dev);
407 dbg("unhandled sysfs path, no need to wait");
412 dbg("result: waiting for sysfs successful '%s'", devpath);
414 info("either wait_for_sysfs (udev %s) needs an update to handle the device '%s' "
415 "properly (%d) or the sysfs-support of your device's driver needs to be fixed, "
416 "please report to <linux-hotplug-devel@lists.sourceforge.net>",
417 UDEV_VERSION, devpath, rc);