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 5
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 = "firmware", .file = "data" },
67 { .subsystem = "i2c-adapter", .file = NULL },
68 { .subsystem = "pci_bus", .file = NULL },
69 { .subsystem = "ieee1394", .file = NULL },
70 { .subsystem = "ieee1394_host", .file = NULL },
71 { .subsystem = "ieee1394_node", .file = NULL },
74 struct class_file *classfile;
75 const char *file = "dev";
78 /* look if we want to look for another file instead of "dev" */
79 for (classfile = class_files; classfile->subsystem != NULL; classfile++) {
80 if (strcmp(class_dev->classname, classfile->subsystem) == 0) {
81 if (classfile->file == NULL) {
82 dbg("class '%s' has no file to wait for", class_dev->classname);
85 file = classfile->file;
90 dbg("looking at class '%s' for specific file '%s'", class_dev->classname, class_dev->path);
92 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
96 if (stat(class_dev->path, &stats) == 0) {
97 dbg("class '%s' specific file '%s' found", class_dev->classname, file);
101 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
104 dbg("error: getting class '%s' specific file '%s'", class_dev->classname, file);
108 /* check if we need to wait for a physical device */
109 static int class_device_expect_no_device_link(struct sysfs_class_device *class_dev)
111 /* list of devices without a "device" symlink to the physical device
112 * if device is set to NULL, no devices in that subsystem has a link */
113 static struct class_device {
117 { .subsystem = "block", .device = "double" },
118 { .subsystem = "block", .device = "nb" },
119 { .subsystem = "block", .device = "ram" },
120 { .subsystem = "block", .device = "loop" },
121 { .subsystem = "block", .device = "fd" },
122 { .subsystem = "block", .device = "md" },
123 { .subsystem = "block", .device = "dos_cd" },
124 { .subsystem = "block", .device = "rflash" },
125 { .subsystem = "block", .device = "rom" },
126 { .subsystem = "block", .device = "rrom" },
127 { .subsystem = "block", .device = "flash" },
128 { .subsystem = "block", .device = "msd" },
129 { .subsystem = "block", .device = "sbpcd" },
130 { .subsystem = "block", .device = "pcd" },
131 { .subsystem = "block", .device = "pf" },
132 { .subsystem = "block", .device = "scd" },
133 { .subsystem = "block", .device = "ubd" },
134 { .subsystem = "input", .device = "event" },
135 { .subsystem = "input", .device = "mice" },
136 { .subsystem = "input", .device = "mouse" },
137 { .subsystem = "input", .device = "ts" },
138 { .subsystem = "vc", .device = NULL },
139 { .subsystem = "tty", .device = NULL },
140 { .subsystem = "cpuid", .device = "cpu" },
141 { .subsystem = "graphics", .device = "fb" },
142 { .subsystem = "mem", .device = NULL },
143 { .subsystem = "misc", .device = NULL },
144 { .subsystem = "msr", .device = NULL },
145 { .subsystem = "netlink", .device = NULL },
146 { .subsystem = "net", .device = "sit" },
147 { .subsystem = "net", .device = "ppp" },
148 { .subsystem = "net", .device = "lo" },
149 { .subsystem = "net", .device = "tap" },
150 { .subsystem = "net", .device = "ipsec" },
151 { .subsystem = "net", .device = "irda" },
152 { .subsystem = "sound", .device = NULL },
153 { .subsystem = "printer", .device = "lp" },
154 { .subsystem = "nvidia", .device = NULL },
155 { .subsystem = "video4linux", .device = "vbi" },
156 { .subsystem = "lirc", .device = NULL },
157 { .subsystem = "firmware", .device = NULL },
158 { .subsystem = "drm", .device = NULL },
159 { .subsystem = "pci_bus", .device = NULL },
160 { .subsystem = "ieee1394", .device = NULL },
161 { .subsystem = "ieee1394_host", .device = NULL },
162 { .subsystem = "ieee1394_node", .device = NULL },
163 { .subsystem = "raw", .device = NULL },
166 struct class_device *classdevice;
169 for (classdevice = class_device; classdevice->subsystem != NULL; classdevice++) {
170 if (strcmp(class_dev->classname, classdevice->subsystem) == 0) {
171 /* see if no device in this class is expected to have a device-link */
172 if (classdevice->device == NULL)
175 len = strlen(classdevice->device);
177 /* see if device name matches */
178 if (strncmp(class_dev->name, classdevice->device, len) != 0)
181 /* exact name match */
182 if (strlen(class_dev->name) == len)
185 /* name match with instance number */
186 if (isdigit(class_dev->name[len]))
194 /* skip waiting for the bus */
195 static int class_device_expect_no_bus(struct sysfs_class_device *class_dev)
197 static char *devices_without_bus[] = {
204 for (device = devices_without_bus; *device != NULL; device++) {
205 int len = strlen(*device);
207 if (strncmp(class_dev->classname, *device, len) == 0)
214 /* wait for the bus and for a bus specific file to show up */
215 static int wait_for_bus_device(struct sysfs_device *device_dev)
217 static struct bus_file {
221 { .bus = "scsi", .file = "vendor" },
222 { .bus = "usb", .file = "idVendor" },
223 { .bus = "usb", .file = "iInterface" },
224 { .bus = "usb", .file = "bNumEndpoints" },
225 { .bus = "usb-serial", .file = "detach_state" },
226 { .bus = "ide", .file = "detach_state" },
227 { .bus = "pci", .file = "vendor" },
228 { .bus = "platform", .file = "detach_state" },
229 { .bus = "i2c", .file = "detach_state" },
232 struct bus_file *busfile;
235 /* wait for the /bus-device link to the /device-device */
236 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
238 if (sysfs_get_device_bus(device_dev) == 0)
241 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
244 dbg("error: getting /bus-device link");
247 dbg("/bus-device link found for bus '%s'", device_dev->bus);
249 /* wait for a bus specific file to show up */
250 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
254 for (busfile = bus_files; busfile->bus != NULL; busfile++) {
255 if (strcmp(device_dev->bus, busfile->bus) == 0) {
257 dbg("looking at bus '%s' for specific file '%s'", device_dev->bus, busfile->file);
258 if (sysfs_get_device_attr(device_dev, busfile->file) != NULL) {
259 dbg("bus '%s' specific file '%s' found", device_dev->bus, busfile->file);
265 info("error: unknown bus, please report to "
266 "<linux-hotplug-devel@lists.sourceforge.net> '%s'", device_dev->bus);
269 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
272 dbg("error: getting bus '%s' specific file '%s'", device_dev->bus, busfile->file);
276 int main(int argc, char *argv[], char *envp[])
278 const char *devpath = "";
280 const char *subsystem;
281 char sysfs_path[SYSFS_PATH_MAX];
282 char filename[SYSFS_PATH_MAX];
283 struct sysfs_class_device *class_dev;
284 struct sysfs_class_device *class_dev_parent;
285 struct sysfs_device *device_dev = NULL;
289 init_logging("wait_for_sysfs");
292 dbg("error: subsystem");
297 devpath = getenv ("DEVPATH");
299 dbg("error: no DEVPATH");
303 action = getenv ("ACTION");
305 dbg("error: no ACTION");
309 /* we only wait on an add event */
310 if (strcmp(action, "add") != 0)
313 if (sysfs_get_mnt_path(sysfs_path, SYSFS_PATH_MAX) != 0) {
314 dbg("error: no sysfs path");
318 if ((strncmp(devpath, "/block/", 7) == 0) || (strncmp(devpath, "/class/", 7) == 0)) {
319 /* open the class device we are called for */
320 snprintf(filename, SYSFS_PATH_MAX-1, "%s%s", sysfs_path, devpath);
321 filename[SYSFS_PATH_MAX-1] = '\0';
323 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
325 class_dev = sysfs_open_class_device_path(filename);
329 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
331 if (class_dev == NULL) {
332 dbg("error: getting class_device");
336 dbg("class_device opened '%s'", filename);
338 if (wait_for_class_device_attributes(class_dev) != 0) {
343 /* skip devices without /device-link */
344 if (class_device_expect_no_device_link(class_dev)) {
345 dbg("no device symlink expected for '%s', ", class_dev->name);
349 /* the symlink may be on the parent device */
350 class_dev_parent = sysfs_get_classdev_parent(class_dev);
351 if (class_dev_parent)
352 dbg("looking at parent device for device link '%s'", class_dev_parent->path);
354 /* wait for the symlink to the /device-device */
355 dbg("waiting for symlink to /device-device");
356 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
358 if (class_dev_parent)
359 device_dev = sysfs_get_classdev_device(class_dev_parent);
361 device_dev = sysfs_get_classdev_device(class_dev);
366 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
368 if (device_dev == NULL) {
369 dbg("error: getting /device-device");
373 dbg("device symlink found pointing to '%s'", device_dev->path);
375 /* wait for the bus value */
376 if (class_device_expect_no_bus(class_dev)) {
377 dbg("no bus device expected for '%s', ", class_dev->classname);
379 if (wait_for_bus_device(device_dev) != 0)
384 sysfs_close_class_device(class_dev);
386 } else if ((strncmp(devpath, "/devices/", 9) == 0)) {
387 /* open the path we are called for */
388 snprintf(filename, SYSFS_PATH_MAX-1, "%s%s", sysfs_path, devpath);
389 filename[SYSFS_PATH_MAX-1] = '\0';
391 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
393 device_dev = sysfs_open_device_path(filename);
397 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
399 if (device_dev == NULL) {
400 dbg("error: getting /device-device");
404 dbg("device_device opened '%s'", filename);
406 /* wait for the bus value */
407 if (wait_for_bus_device(device_dev) != 0)
410 sysfs_close_device(device_dev);
413 dbg("unhandled sysfs path, no need to wait");
418 dbg("result: waiting for sysfs successful '%s'", devpath);
420 info("either wait_for_sysfs (udev %s) needs an update to handle the device '%s' "
421 "properly (%d) or the sysfs-support of your device's driver needs to be fixed, "
422 "please report to <linux-hotplug-devel@lists.sourceforge.net>",
423 UDEV_VERSION, devpath, rc);