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 },
68 struct class_file *classfile;
69 const char *file = "dev";
72 /* look if we want to look for another file instead of "dev" */
73 for (classfile = class_files; classfile->subsystem != NULL; classfile++) {
74 if (strcmp(class_dev->classname, classfile->subsystem) == 0) {
75 if (classfile->file == NULL) {
76 dbg("class '%s' has no file to wait for", class_dev->classname);
79 file = classfile->file;
83 dbg("looking at class '%s' for specific file '%s'", class_dev->classname, file);
85 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
87 if (sysfs_get_classdev_attr(class_dev, file) != NULL) {
88 dbg("class '%s' specific file '%s' found", class_dev->classname, file);
92 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
95 dbg("error: getting class '%s' specific file '%s'", class_dev->classname, file);
99 /* skip waiting for physical device */
100 static int class_device_expect_no_device_link(struct sysfs_class_device *class_dev)
102 static char *devices_without_link[] = {
131 for (device = devices_without_link; *device != NULL; device++) {
132 int len = strlen(*device);
134 /* look if name matches */
135 if (strncmp(class_dev->name, *device, len) != 0)
139 if (strlen(class_dev->name) == len)
142 /* instance numbers are matching too */
143 if (isdigit(class_dev->name[len]))
150 /* skip waiting for the bus */
151 static int class_device_expect_no_bus(struct sysfs_class_device *class_dev)
153 static char *devices_without_bus[] = {
160 for (device = devices_without_bus; *device != NULL; device++) {
161 int len = strlen(*device);
163 if (strncmp(class_dev->classname, *device, len) == 0)
170 /* wait for the bus and for a bus specific file to show up */
171 static int wait_for_bus_device(struct sysfs_device *device_dev)
173 static struct bus_file {
177 { .bus = "scsi", .file = "vendor" },
178 { .bus = "usb", .file = "idVendor" },
179 { .bus = "usb", .file = "iInterface" },
180 { .bus = "usb", .file = "bNumEndpoints" },
181 { .bus = "usb-serial", .file = "detach_state" },
182 { .bus = "ide", .file = "detach_state" },
183 { .bus = "pci", .file = "vendor" },
184 { .bus = "platform", .file = "detach_state" },
187 struct bus_file *busfile;
190 /* wait for the /bus-device link to the /device-device */
191 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
193 if (sysfs_get_device_bus(device_dev) == 0)
196 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
199 dbg("error: getting /bus-device link");
202 dbg("/bus-device link found for bus '%s'", device_dev->bus);
204 /* wait for a bus specific file to show up */
205 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
209 for (busfile = bus_files; busfile->bus != NULL; busfile++) {
210 if (strcmp(device_dev->bus, busfile->bus) == 0) {
212 dbg("looking at bus '%s' for specific file '%s'", device_dev->bus, busfile->file);
213 if (sysfs_get_device_attr(device_dev, busfile->file) != NULL) {
214 dbg("bus '%s' specific file '%s' found", device_dev->bus, busfile->file);
220 info("error: unknown bus, please report to "
221 "<linux-hotplug-devel@lists.sourceforge.net> '%s'", device_dev->bus);
224 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
227 dbg("error: getting bus '%s' specific file '%s'", device_dev->bus, busfile->file);
231 int main(int argc, char *argv[], char *envp[])
233 const char *devpath = "";
235 const char *subsystem;
236 char sysfs_path[SYSFS_PATH_MAX];
237 char filename[SYSFS_PATH_MAX];
238 struct sysfs_class_device *class_dev;
239 struct sysfs_class_device *class_dev_parent;
240 struct sysfs_device *device_dev = NULL;
244 init_logging("wait_for_sysfs");
247 dbg("error: subsystem");
252 devpath = getenv ("DEVPATH");
254 dbg("error: no DEVPATH");
258 action = getenv ("ACTION");
260 dbg("error: no ACTION");
264 /* we only wait on an add event */
265 if (strcmp(action, "add") != 0)
268 if (sysfs_get_mnt_path(sysfs_path, SYSFS_PATH_MAX) != 0) {
269 dbg("error: no sysfs path");
273 if ((strncmp(devpath, "/block/", 7) == 0) || (strncmp(devpath, "/class/", 7) == 0)) {
274 /* open the class device we are called for */
275 snprintf(filename, SYSFS_PATH_MAX-1, "%s%s", sysfs_path, devpath);
276 filename[SYSFS_PATH_MAX-1] = '\0';
278 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
280 class_dev = sysfs_open_class_device_path(filename);
284 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
286 if (class_dev == NULL) {
287 dbg("error: getting class_device");
291 dbg("class_device opened '%s'", filename);
293 if (wait_for_class_device_attributes(class_dev) != 0) {
298 /* skip devices without /device-link */
299 if (class_device_expect_no_device_link(class_dev)) {
300 dbg("no device symlink expected for '%s', ", class_dev->name);
304 /* the symlink may be on the parent device */
305 class_dev_parent = sysfs_get_classdev_parent(class_dev);
306 if (class_dev_parent)
307 dbg("looking at parent device for device link '%s'", class_dev_parent->path);
309 /* wait for the symlink to the /device-device */
310 dbg("waiting for symlink to /device-device");
311 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
313 if (class_dev_parent)
314 device_dev = sysfs_get_classdev_device(class_dev_parent);
316 device_dev = sysfs_get_classdev_device(class_dev);
321 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
323 if (device_dev == NULL) {
324 dbg("error: getting /device-device");
328 dbg("device symlink found pointing to '%s'", device_dev->path);
330 /* wait for the bus value */
331 if (class_device_expect_no_bus(class_dev)) {
332 dbg("no bus device expected for '%s', ", class_dev->classname);
334 if (wait_for_bus_device(device_dev) != 0)
339 sysfs_close_class_device(class_dev);
341 } else if ((strncmp(devpath, "/devices/", 9) == 0)) {
342 /* open the path we are called for */
343 snprintf(filename, SYSFS_PATH_MAX-1, "%s%s", sysfs_path, devpath);
344 filename[SYSFS_PATH_MAX-1] = '\0';
346 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
348 device_dev = sysfs_open_device_path(filename);
352 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
354 if (device_dev == NULL) {
355 dbg("error: getting /device-device");
359 dbg("device_device opened '%s'", filename);
361 /* wait for the bus value */
362 if (wait_for_bus_device(device_dev) != 0)
365 sysfs_close_device(device_dev);
368 dbg("unhandled sysfs path, no need to wait");
373 dbg("result: waiting for sysfs successful '%s'", devpath);
375 info("error: wait_for_sysfs needs an update to handle the device '%s' "
376 "properly, please report to <linux-hotplug-devel@lists.sourceforge.net>",