]>
git.ipfire.org Git - thirdparty/systemd.git/blob - wait_for_sysfs.c
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>
9 * Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com>
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation version 2 of the License.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 675 Mass Ave, Cambridge, MA 02139, USA.
36 #include "udev_version.h"
37 #include "libsysfs/sysfs/libsysfs.h"
40 #define FILENAME_MAX 4096
44 unsigned char logname
[LOGNAME_SIZE
];
45 void log_message(int level
, const char *format
, ...)
49 va_start(args
, format
);
50 vsyslog(level
, format
, args
);
55 #define WAIT_MAX_SECONDS 5
56 #define WAIT_LOOP_PER_SECOND 20
58 /* wait for specific file to show up, normally the "dev"-file */
59 static int wait_for_class_device_attributes(struct sysfs_class_device
*class_dev
,
62 static struct class_file
{
66 { .subsystem
= "net", .file
= "ifindex" },
67 { .subsystem
= "scsi_host", .file
= "unique_id" },
68 { .subsystem
= "scsi_device", .file
= NULL
},
69 { .subsystem
= "pcmcia_socket", .file
= "card_type" },
70 { .subsystem
= "usb_host", .file
= NULL
},
71 { .subsystem
= "bluetooth", .file
= "address" },
72 { .subsystem
= "firmware", .file
= "data" },
73 { .subsystem
= "i2c-adapter", .file
= NULL
},
74 { .subsystem
= "pci_bus", .file
= NULL
},
75 { .subsystem
= "ieee1394", .file
= NULL
},
76 { .subsystem
= "ieee1394_host", .file
= NULL
},
77 { .subsystem
= "ieee1394_node", .file
= NULL
},
80 struct class_file
*classfile
;
82 char filename
[FILENAME_MAX
];
85 /* look if we want to look for another file instead of "dev" */
86 for (classfile
= class_files
; classfile
->subsystem
!= NULL
; classfile
++) {
87 if (strcmp(class_dev
->classname
, classfile
->subsystem
) == 0) {
88 if (classfile
->file
== NULL
) {
89 dbg("class '%s' has no file to wait for", class_dev
->classname
);
92 file
= classfile
->file
;
97 strcpy(filename
, class_dev
->path
);
98 strcat(filename
, "/");
99 strcat(filename
, file
);
100 dbg("looking at class '%s' for specific file '%s' with full name %s", class_dev
->classname
, class_dev
->path
, filename
);
102 loop
= WAIT_MAX_SECONDS
* WAIT_LOOP_PER_SECOND
;
106 if (stat(class_dev
->path
, &stats
) == -1) {
107 dbg("'%s' now disappeared (probably remove has beaten us)", class_dev
->path
);
111 if (stat(filename
, &stats
) == 0) {
112 dbg("class '%s' specific file '%s' found", class_dev
->classname
, file
);
116 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND
);
119 dbg("error: getting class '%s' specific file '%s'", class_dev
->classname
, file
);
120 *error
= "class specific file unavailable";
124 /* check if we need to wait for a physical device */
125 static int class_device_expect_no_device_link(struct sysfs_class_device
*class_dev
)
127 /* list of devices without a "device" symlink to the physical device
128 * if device is set to NULL, no devices in that subsystem has a link */
129 static struct class_device
{
133 { .subsystem
= "block", .device
= "double" },
134 { .subsystem
= "block", .device
= "nb" },
135 { .subsystem
= "block", .device
= "ram" },
136 { .subsystem
= "block", .device
= "loop" },
137 { .subsystem
= "block", .device
= "fd" },
138 { .subsystem
= "block", .device
= "md" },
139 { .subsystem
= "block", .device
= "dos_cd" },
140 { .subsystem
= "block", .device
= "rflash" },
141 { .subsystem
= "block", .device
= "rom" },
142 { .subsystem
= "block", .device
= "rrom" },
143 { .subsystem
= "block", .device
= "flash" },
144 { .subsystem
= "block", .device
= "msd" },
145 { .subsystem
= "block", .device
= "sbpcd" },
146 { .subsystem
= "block", .device
= "pcd" },
147 { .subsystem
= "block", .device
= "pf" },
148 { .subsystem
= "block", .device
= "scd" },
149 { .subsystem
= "block", .device
= "ubd" },
150 { .subsystem
= "block", .device
= "dm-" },
151 { .subsystem
= "input", .device
= "event" },
152 { .subsystem
= "input", .device
= "mice" },
153 { .subsystem
= "input", .device
= "mouse" },
154 { .subsystem
= "input", .device
= "ts" },
155 { .subsystem
= "vc", .device
= NULL
},
156 { .subsystem
= "tty", .device
= NULL
},
157 { .subsystem
= "cpuid", .device
= "cpu" },
158 { .subsystem
= "graphics", .device
= "fb" },
159 { .subsystem
= "mem", .device
= NULL
},
160 { .subsystem
= "misc", .device
= NULL
},
161 { .subsystem
= "msr", .device
= NULL
},
162 { .subsystem
= "netlink", .device
= NULL
},
163 { .subsystem
= "net", .device
= "sit" },
164 { .subsystem
= "net", .device
= "lo" },
165 { .subsystem
= "net", .device
= "tap" },
166 { .subsystem
= "net", .device
= "ipsec" },
167 { .subsystem
= "net", .device
= "dummy" },
168 { .subsystem
= "net", .device
= "irda" },
169 { .subsystem
= "net", .device
= "ppp" },
170 { .subsystem
= "ppp", .device
= NULL
},
171 { .subsystem
= "sound", .device
= NULL
},
172 { .subsystem
= "printer", .device
= "lp" },
173 { .subsystem
= "nvidia", .device
= NULL
},
174 { .subsystem
= "video4linux", .device
= "vbi" },
175 { .subsystem
= "lirc", .device
= NULL
},
176 { .subsystem
= "firmware", .device
= NULL
},
177 { .subsystem
= "drm", .device
= NULL
},
178 { .subsystem
= "pci_bus", .device
= NULL
},
179 { .subsystem
= "ieee1394", .device
= NULL
},
180 { .subsystem
= "ieee1394_host", .device
= NULL
},
181 { .subsystem
= "ieee1394_node", .device
= NULL
},
182 { .subsystem
= "raw", .device
= NULL
},
185 struct class_device
*classdevice
;
188 for (classdevice
= class_device
; classdevice
->subsystem
!= NULL
; classdevice
++) {
189 if (strcmp(class_dev
->classname
, classdevice
->subsystem
) == 0) {
190 /* see if no device in this class is expected to have a device-link */
191 if (classdevice
->device
== NULL
)
194 len
= strlen(classdevice
->device
);
196 /* see if device name matches */
197 if (strncmp(class_dev
->name
, classdevice
->device
, len
) != 0)
200 /* exact name match */
201 if (strlen(class_dev
->name
) == len
)
204 /* name match with instance number */
205 if (isdigit(class_dev
->name
[len
]))
213 /* skip waiting for the bus */
214 static int class_device_expect_no_bus(struct sysfs_class_device
*class_dev
)
216 static char *devices_without_bus
[] = {
223 for (device
= devices_without_bus
; *device
!= NULL
; device
++) {
224 int len
= strlen(*device
);
226 if (strncmp(class_dev
->classname
, *device
, len
) == 0)
233 /* wait for the bus and for a bus specific file to show up */
234 static int wait_for_bus_device(struct sysfs_device
*devices_dev
,
237 static struct bus_file
{
241 { .bus
= "scsi", .file
= "vendor" },
242 { .bus
= "usb", .file
= "idVendor" },
243 { .bus
= "usb", .file
= "iInterface" },
244 { .bus
= "usb", .file
= "bNumEndpoints" },
245 { .bus
= "usb-serial", .file
= "detach_state" },
246 { .bus
= "ide", .file
= "detach_state" },
247 { .bus
= "pci", .file
= "vendor" },
248 { .bus
= "platform", .file
= "detach_state" },
249 { .bus
= "i2c", .file
= "detach_state" },
252 struct bus_file
*busfile
;
255 /* wait for the bus device link to the devices device */
256 loop
= WAIT_MAX_SECONDS
* WAIT_LOOP_PER_SECOND
;
258 if (sysfs_get_device_bus(devices_dev
) == 0)
261 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND
);
264 dbg("error: getting bus device link");
265 *error
= "no bus device link";
268 dbg("bus device link found for bus '%s'", devices_dev
->bus
);
270 /* wait for a bus specific file to show up */
271 loop
= WAIT_MAX_SECONDS
* WAIT_LOOP_PER_SECOND
;
275 for (busfile
= bus_files
; busfile
->bus
!= NULL
; busfile
++) {
276 if (strcmp(devices_dev
->bus
, busfile
->bus
) == 0) {
278 dbg("looking at bus '%s' for specific file '%s'", devices_dev
->bus
, busfile
->file
);
279 if (sysfs_get_device_attr(devices_dev
, busfile
->file
) != NULL
) {
280 dbg("bus '%s' specific file '%s' found", devices_dev
->bus
, busfile
->file
);
286 *error
= "unknown bus";
287 info("error: unknown bus, please report to "
288 "<linux-hotplug-devel@lists.sourceforge.net> '%s'", devices_dev
->bus
);
291 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND
);
294 dbg("error: getting bus '%s' specific file '%s'", devices_dev
->bus
, busfile
->file
);
295 *error
= "bus specific file unavailable";
300 static struct sysfs_class_device
*open_class_device(const char *path
)
302 struct sysfs_class_device
*class_dev
;
305 loop
= WAIT_MAX_SECONDS
* WAIT_LOOP_PER_SECOND
;
307 class_dev
= sysfs_open_class_device_path(path
);
311 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND
);
317 static int wait_for_class_device(struct sysfs_class_device
*class_dev
,
320 struct sysfs_class_device
*class_dev_parent
;
321 struct sysfs_device
*devices_dev
= NULL
;
324 if (wait_for_class_device_attributes(class_dev
, error
) != 0)
327 /* skip devices without devices-link */
328 if (class_device_expect_no_device_link(class_dev
)) {
329 dbg("no device symlink expected for '%s', ", class_dev
->name
);
333 /* the symlink may be on the parent device */
334 class_dev_parent
= sysfs_get_classdev_parent(class_dev
);
335 if (class_dev_parent
)
336 dbg("looking at parent device for device link '%s'", class_dev_parent
->path
);
338 /* wait for the symlink to the devices device */
339 dbg("waiting for symlink to devices device");
340 loop
= WAIT_MAX_SECONDS
* WAIT_LOOP_PER_SECOND
;
342 if (class_dev_parent
)
343 devices_dev
= sysfs_get_classdev_device(class_dev_parent
);
345 devices_dev
= sysfs_get_classdev_device(class_dev
);
350 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND
);
353 dbg(" error: no devices device symlink found");
354 *error
= "no device symlink";
357 dbg("device symlink found pointing to '%s'", devices_dev
->path
);
359 /* wait for the bus value */
360 if (class_device_expect_no_bus(class_dev
)) {
361 dbg("no bus device expected for '%s', ", class_dev
->classname
);
364 return wait_for_bus_device(devices_dev
, error
);
368 static struct sysfs_device
*open_devices_device(const char *path
)
370 struct sysfs_device
*devices_dev
;
373 loop
= WAIT_MAX_SECONDS
* WAIT_LOOP_PER_SECOND
;
375 devices_dev
= sysfs_open_device_path(path
);
379 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND
);
385 int main(int argc
, char *argv
[], char *envp
[])
387 const char *devpath
= "";
389 const char *subsystem
;
390 char sysfs_mnt_path
[SYSFS_PATH_MAX
];
391 char filename
[SYSFS_PATH_MAX
];
392 struct sysfs_class_device
*class_dev
;
393 struct sysfs_device
*devices_dev
;
395 const char *error
= NULL
;
397 logging_init("wait_for_sysfs");
400 dbg("error: subsystem");
405 devpath
= getenv ("DEVPATH");
407 dbg("error: no DEVPATH");
412 action
= getenv ("ACTION");
414 dbg("error: no ACTION");
419 /* we only wait on an add event */
420 if (strcmp(action
, "add") != 0) {
421 dbg("no add ACTION");
425 if (sysfs_get_mnt_path(sysfs_mnt_path
, SYSFS_PATH_MAX
) != 0) {
426 dbg("error: no sysfs path");
431 if ((strncmp(devpath
, "/block/", 7) == 0) || (strncmp(devpath
, "/class/", 7) == 0)) {
432 snprintf(filename
, SYSFS_PATH_MAX
-1, "%s%s", sysfs_mnt_path
, devpath
);
433 filename
[SYSFS_PATH_MAX
-1] = '\0';
435 /* open the class device we are called for */
436 class_dev
= open_class_device(filename
);
438 dbg("error: class device unavailable (probably remove has beaten us)");
441 dbg("class device opened '%s'", filename
);
443 /* wait for the class device with possible physical device and bus */
444 wait_for_class_device(class_dev
, &error
);
446 sysfs_close_class_device(class_dev
);
448 } else if ((strncmp(devpath
, "/devices/", 9) == 0)) {
449 snprintf(filename
, SYSFS_PATH_MAX
-1, "%s%s", sysfs_mnt_path
, devpath
);
450 filename
[SYSFS_PATH_MAX
-1] = '\0';
452 /* open the path we are called for */
453 devices_dev
= open_devices_device(filename
);
455 dbg("error: devices device unavailable (probably remove has beaten us)");
458 dbg("devices device opened '%s'", filename
);
460 /* wait for the bus value */
461 wait_for_bus_device(devices_dev
, &error
);
463 sysfs_close_device(devices_dev
);
466 dbg("unhandled sysfs path, no need to wait");
471 info("either wait_for_sysfs (udev %s) needs an update to handle the device '%s' "
472 "properly (%s) or the sysfs-support of your device's driver needs to be fixed, "
473 "please report to <linux-hotplug-devel@lists.sourceforge.net>",
474 UDEV_VERSION
, devpath
, error
);
477 dbg("result: waiting for sysfs successful '%s'", devpath
);