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 "libsysfs/sysfs/libsysfs.h"
38 unsigned char logname[LOGNAME_SIZE];
39 void log_message(int level, const char *format, ...)
43 va_start(args, format);
44 vsyslog(level, format, args);
49 #define WAIT_MAX_SECONDS 5
50 #define WAIT_LOOP_PER_SECOND 20
52 /* wait for specific file to show up, normally the "dev"-file */
53 static int wait_for_class_device_attributes(struct sysfs_class_device *class_dev)
55 static struct class_file {
59 { .subsystem = "net", .file = "ifindex" },
60 { .subsystem = "scsi_host", .file = "unique_id" },
61 { .subsystem = "scsi_device", .file = NULL },
62 { .subsystem = "pcmcia_socket", .file = "card_type" },
63 { .subsystem = "usb_host", .file = NULL },
64 { .subsystem = "bluetooth", .file = "address" },
65 { .subsystem = "i2c-adapter", .file = NULL },
66 { .subsystem = "pci_bus", .file = NULL },
67 { .subsystem = "ieee1394", .file = NULL },
68 { .subsystem = "ieee1394_host", .file = NULL },
69 { .subsystem = "ieee1394_node", .file = NULL },
72 struct class_file *classfile;
73 const char *file = "dev";
76 /* look if we want to look for another file instead of "dev" */
77 for (classfile = class_files; classfile->subsystem != NULL; classfile++) {
78 if (strcmp(class_dev->classname, classfile->subsystem) == 0) {
79 if (classfile->file == NULL) {
80 dbg("class '%s' has no file to wait for", class_dev->classname);
83 file = classfile->file;
87 dbg("looking at class '%s' for specific file '%s'", class_dev->classname, file);
89 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
91 if (sysfs_get_classdev_attr(class_dev, file) != NULL) {
92 dbg("class '%s' specific file '%s' found", class_dev->classname, file);
96 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
99 dbg("error: getting class '%s' specific file '%s'", class_dev->classname, file);
103 /* skip waiting for physical device */
104 static int class_device_expect_no_device_link(struct sysfs_class_device *class_dev)
106 /* List of devices without a "device" symlink
107 * set .device to NULL to accept all devices in that subsystem */
108 static struct class_device {
112 { .subsystem = "block", .device = "double" },
113 { .subsystem = "block", .device = "nb" },
114 { .subsystem = "block", .device = "ram" },
115 { .subsystem = "block", .device = "loop" },
116 { .subsystem = "block", .device = "fd" },
117 { .subsystem = "block", .device = "md" },
118 { .subsystem = "block", .device = "dos_cd" },
119 { .subsystem = "block", .device = "rflash" },
120 { .subsystem = "block", .device = "rom" },
121 { .subsystem = "block", .device = "rrom" },
122 { .subsystem = "block", .device = "flash" },
123 { .subsystem = "block", .device = "msd" },
124 { .subsystem = "block", .device = "sbpcd" },
125 { .subsystem = "block", .device = "pcd" },
126 { .subsystem = "block", .device = "pf" },
127 { .subsystem = "block", .device = "scd" },
128 { .subsystem = "block", .device = "ubd" },
129 { .subsystem = "input", .device = "event" },
130 { .subsystem = "input", .device = "mice" },
131 { .subsystem = "input", .device = "mouse" },
132 { .subsystem = "input", .device = "ts" },
133 { .subsystem = "vc", .device = NULL },
134 { .subsystem = "tty", .device = NULL },
135 { .subsystem = "cpuid", .device = "cpu" },
136 { .subsystem = "graphics", .device = "fb" },
137 { .subsystem = "mem", .device = NULL },
138 { .subsystem = "misc", .device = NULL },
139 { .subsystem = "msr", .device = NULL },
140 { .subsystem = "netlink", .device = NULL },
141 { .subsystem = "net", .device = NULL },
142 { .subsystem = "sound", .device = NULL },
143 { .subsystem = "printer", .device = "lp" },
144 { .subsystem = "nvidia", .device = NULL },
145 { .subsystem = "video4linux", .device = NULL },
146 { .subsystem = "lirc", .device = NULL },
147 { .subsystem = "firmware", .device = NULL },
148 { .subsystem = "drm", .device = NULL },
149 { .subsystem = "pci_bus", .device = NULL },
150 { .subsystem = "ieee1394", .device = NULL },
151 { .subsystem = "ieee1394_host", .device = NULL },
152 { .subsystem = "ieee1394_node", .device = NULL },
155 struct class_device *classdevice;
158 /* look if we want to look for another file instead of "dev" */
159 for (classdevice = class_device; classdevice->subsystem != NULL; classdevice++) {
160 if (strcmp(class_dev->classname, classdevice->subsystem) == 0) {
161 /* if device is NULL, all devices in this class are ok */
162 if (classdevice->device == NULL)
165 len = strlen(classdevice->device);
167 /* see if device name matches */
168 if (strncmp(class_dev->name, classdevice->device, len) != 0)
172 if (strlen(class_dev->name) == len)
175 /* instance numbers are matching too */
176 if (isdigit(class_dev->name[len]))
184 /* skip waiting for the bus */
185 static int class_device_expect_no_bus(struct sysfs_class_device *class_dev)
187 static char *devices_without_bus[] = {
194 for (device = devices_without_bus; *device != NULL; device++) {
195 int len = strlen(*device);
197 if (strncmp(class_dev->classname, *device, len) == 0)
204 /* wait for the bus and for a bus specific file to show up */
205 static int wait_for_bus_device(struct sysfs_device *device_dev)
207 static struct bus_file {
211 { .bus = "scsi", .file = "vendor" },
212 { .bus = "usb", .file = "idVendor" },
213 { .bus = "usb", .file = "iInterface" },
214 { .bus = "usb", .file = "bNumEndpoints" },
215 { .bus = "usb-serial", .file = "detach_state" },
216 { .bus = "ide", .file = "detach_state" },
217 { .bus = "pci", .file = "vendor" },
218 { .bus = "platform", .file = "detach_state" },
219 { .bus = "i2c", .file = "detach_state" },
222 struct bus_file *busfile;
225 /* wait for the /bus-device link to the /device-device */
226 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
228 if (sysfs_get_device_bus(device_dev) == 0)
231 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
234 dbg("error: getting /bus-device link");
237 dbg("/bus-device link found for bus '%s'", device_dev->bus);
239 /* wait for a bus specific file to show up */
240 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
244 for (busfile = bus_files; busfile->bus != NULL; busfile++) {
245 if (strcmp(device_dev->bus, busfile->bus) == 0) {
247 dbg("looking at bus '%s' for specific file '%s'", device_dev->bus, busfile->file);
248 if (sysfs_get_device_attr(device_dev, busfile->file) != NULL) {
249 dbg("bus '%s' specific file '%s' found", device_dev->bus, busfile->file);
255 info("error: unknown bus, please report to "
256 "<linux-hotplug-devel@lists.sourceforge.net> '%s'", device_dev->bus);
259 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
262 dbg("error: getting bus '%s' specific file '%s'", device_dev->bus, busfile->file);
266 int main(int argc, char *argv[], char *envp[])
268 const char *devpath = "";
270 const char *subsystem;
271 char sysfs_path[SYSFS_PATH_MAX];
272 char filename[SYSFS_PATH_MAX];
273 struct sysfs_class_device *class_dev;
274 struct sysfs_class_device *class_dev_parent;
275 struct sysfs_device *device_dev = NULL;
279 init_logging("wait_for_sysfs");
282 dbg("error: subsystem");
287 devpath = getenv ("DEVPATH");
289 dbg("error: no DEVPATH");
293 action = getenv ("ACTION");
295 dbg("error: no ACTION");
299 /* we only wait on an add event */
300 if (strcmp(action, "add") != 0)
303 if (sysfs_get_mnt_path(sysfs_path, SYSFS_PATH_MAX) != 0) {
304 dbg("error: no sysfs path");
308 if ((strncmp(devpath, "/block/", 7) == 0) || (strncmp(devpath, "/class/", 7) == 0)) {
309 /* open the class device we are called for */
310 snprintf(filename, SYSFS_PATH_MAX-1, "%s%s", sysfs_path, devpath);
311 filename[SYSFS_PATH_MAX-1] = '\0';
313 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
315 class_dev = sysfs_open_class_device_path(filename);
319 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
321 if (class_dev == NULL) {
322 dbg("error: getting class_device");
326 dbg("class_device opened '%s'", filename);
328 if (wait_for_class_device_attributes(class_dev) != 0) {
333 /* skip devices without /device-link */
334 if (class_device_expect_no_device_link(class_dev)) {
335 dbg("no device symlink expected for '%s', ", class_dev->name);
339 /* the symlink may be on the parent device */
340 class_dev_parent = sysfs_get_classdev_parent(class_dev);
341 if (class_dev_parent)
342 dbg("looking at parent device for device link '%s'", class_dev_parent->path);
344 /* wait for the symlink to the /device-device */
345 dbg("waiting for symlink to /device-device");
346 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
348 if (class_dev_parent)
349 device_dev = sysfs_get_classdev_device(class_dev_parent);
351 device_dev = sysfs_get_classdev_device(class_dev);
356 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
358 if (device_dev == NULL) {
359 dbg("error: getting /device-device");
363 dbg("device symlink found pointing to '%s'", device_dev->path);
365 /* wait for the bus value */
366 if (class_device_expect_no_bus(class_dev)) {
367 dbg("no bus device expected for '%s', ", class_dev->classname);
369 if (wait_for_bus_device(device_dev) != 0)
374 sysfs_close_class_device(class_dev);
376 } else if ((strncmp(devpath, "/devices/", 9) == 0)) {
377 /* open the path we are called for */
378 snprintf(filename, SYSFS_PATH_MAX-1, "%s%s", sysfs_path, devpath);
379 filename[SYSFS_PATH_MAX-1] = '\0';
381 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
383 device_dev = sysfs_open_device_path(filename);
387 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
389 if (device_dev == NULL) {
390 dbg("error: getting /device-device");
394 dbg("device_device opened '%s'", filename);
396 /* wait for the bus value */
397 if (wait_for_bus_device(device_dev) != 0)
400 sysfs_close_device(device_dev);
403 dbg("unhandled sysfs path, no need to wait");
408 dbg("result: waiting for sysfs successful '%s'", devpath);
410 info("error: wait_for_sysfs needs an update to handle the device '%s' "
411 "properly, please report to <linux-hotplug-devel@lists.sourceforge.net>",