]> git.ipfire.org Git - thirdparty/systemd.git/blame - wait_for_sysfs.c
[PATCH] 040 release
[thirdparty/systemd.git] / wait_for_sysfs.c
CommitLineData
a8b5267a
KS
1/*
2 * wait_for_sysfs.c - small program to delay the execution
3 * of /etc/hotplug.d/ programs, until sysfs
e629ca3f 4 * is fully populated by the kernel. Depending on
a8b5267a
KS
5 * the type of device, we wait for all expected
6 * directories and then just exit.
7 *
8 * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
a8f2703a 9 * Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com>
a8b5267a
KS
10 *
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.
14 *
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.
19 *
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.
23 *
24 */
25
26#include <stdio.h>
27#include <stddef.h>
28#include <stdlib.h>
29#include <unistd.h>
30#include <string.h>
31#include <ctype.h>
32#include <errno.h>
33#include <sys/stat.h>
34
35#include "logging.h"
cdd95f9a 36#include "udev_version.h"
a8b5267a
KS
37#include "libsysfs/sysfs/libsysfs.h"
38
a8f2703a
GKH
39#ifndef FILENAME_MAX
40#define FILENAME_MAX 4096
41#endif
42
a8b5267a
KS
43#ifdef LOG
44unsigned char logname[LOGNAME_SIZE];
45void log_message(int level, const char *format, ...)
46{
47 va_list args;
48
49 va_start(args, format);
50 vsyslog(level, format, args);
51 va_end(args);
52}
53#endif
54
1946fec4 55#define WAIT_MAX_SECONDS 5
a8b5267a
KS
56#define WAIT_LOOP_PER_SECOND 20
57
e629ca3f 58/* wait for specific file to show up, normally the "dev"-file */
dcb9c852
KS
59static int wait_for_class_device_attributes(struct sysfs_class_device *class_dev,
60 const char **error)
a8b5267a
KS
61{
62 static struct class_file {
63 char *subsystem;
64 char *file;
65 } class_files[] = {
66 { .subsystem = "net", .file = "ifindex" },
e629ca3f
KS
67 { .subsystem = "scsi_host", .file = "unique_id" },
68 { .subsystem = "scsi_device", .file = NULL },
1946fec4 69 { .subsystem = "pcmcia_socket", .file = "card_type" },
a8b5267a 70 { .subsystem = "usb_host", .file = NULL },
90852564 71 { .subsystem = "bluetooth", .file = "address" },
139087e8 72 { .subsystem = "firmware", .file = "data" },
1a13fcb3 73 { .subsystem = "i2c-adapter", .file = NULL },
10eb67ec 74 { .subsystem = "pci_bus", .file = NULL },
83729077
GKH
75 { .subsystem = "ieee1394", .file = NULL },
76 { .subsystem = "ieee1394_host", .file = NULL },
77 { .subsystem = "ieee1394_node", .file = NULL },
a8b5267a
KS
78 { NULL, NULL }
79 };
80 struct class_file *classfile;
dcb9c852 81 char *file = "dev";
a8f2703a 82 char filename[FILENAME_MAX];
a8b5267a
KS
83 int loop;
84
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);
90 return 0;
91 }
92 file = classfile->file;
93 break;
94 }
95 }
1946fec4 96
a8f2703a
GKH
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);
a8b5267a
KS
101
102 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
103 while (--loop) {
1946fec4
KS
104 struct stat stats;
105
a8f2703a 106 if (stat(class_dev->path, &stats) == -1) {
dcb9c852 107 dbg("'%s' now disappeared (probably remove has beaten us)", class_dev->path);
a8f2703a
GKH
108 return -ENODEV;
109 }
110
dcb9c852 111 if (stat(filename, &stats) == 0) {
a8b5267a
KS
112 dbg("class '%s' specific file '%s' found", class_dev->classname, file);
113 return 0;
114 }
e629ca3f
KS
115
116 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
a8b5267a
KS
117 }
118
e629ca3f 119 dbg("error: getting class '%s' specific file '%s'", class_dev->classname, file);
dcb9c852 120 *error = "class specific file unavailable";
a8f2703a 121 return -ENOENT;
a8b5267a
KS
122}
123
7e89a569 124/* check if we need to wait for a physical device */
a8b5267a
KS
125static int class_device_expect_no_device_link(struct sysfs_class_device *class_dev)
126{
7e89a569
KS
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 */
b9b659ae
GKH
129 static struct class_device {
130 char *subsystem;
131 char *device;
132 } 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" },
3b2382de 150 { .subsystem = "block", .device = "dm-" },
b9b659ae
GKH
151 { .subsystem = "input", .device = "event" },
152 { .subsystem = "input", .device = "mice" },
153 { .subsystem = "input", .device = "mouse" },
154 { .subsystem = "input", .device = "ts" },
10eb67ec 155 { .subsystem = "vc", .device = NULL },
b9b659ae
GKH
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 },
cdd95f9a 163 { .subsystem = "net", .device = "sit" },
cdd95f9a
KS
164 { .subsystem = "net", .device = "lo" },
165 { .subsystem = "net", .device = "tap" },
d38c486a 166 { .subsystem = "net", .device = "ipsec" },
74d2a1d5 167 { .subsystem = "net", .device = "dummy" },
139087e8 168 { .subsystem = "net", .device = "irda" },
0f634c33
KS
169 { .subsystem = "net", .device = "ppp" },
170 { .subsystem = "ppp", .device = NULL },
b9b659ae 171 { .subsystem = "sound", .device = NULL },
b9b659ae 172 { .subsystem = "printer", .device = "lp" },
10eb67ec 173 { .subsystem = "nvidia", .device = NULL },
cdd95f9a 174 { .subsystem = "video4linux", .device = "vbi" },
10eb67ec
GKH
175 { .subsystem = "lirc", .device = NULL },
176 { .subsystem = "firmware", .device = NULL },
177 { .subsystem = "drm", .device = NULL },
178 { .subsystem = "pci_bus", .device = NULL },
83729077
GKH
179 { .subsystem = "ieee1394", .device = NULL },
180 { .subsystem = "ieee1394_host", .device = NULL },
181 { .subsystem = "ieee1394_node", .device = NULL },
2b29bb4f 182 { .subsystem = "raw", .device = NULL },
b9b659ae 183 { NULL, NULL }
a8b5267a 184 };
b9b659ae
GKH
185 struct class_device *classdevice;
186 int len;
a8b5267a 187
b9b659ae
GKH
188 for (classdevice = class_device; classdevice->subsystem != NULL; classdevice++) {
189 if (strcmp(class_dev->classname, classdevice->subsystem) == 0) {
7e89a569 190 /* see if no device in this class is expected to have a device-link */
b9b659ae
GKH
191 if (classdevice->device == NULL)
192 return 1;
a8b5267a 193
b9b659ae 194 len = strlen(classdevice->device);
a8b5267a 195
b9b659ae
GKH
196 /* see if device name matches */
197 if (strncmp(class_dev->name, classdevice->device, len) != 0)
198 continue;
a8b5267a 199
7e89a569 200 /* exact name match */
b9b659ae
GKH
201 if (strlen(class_dev->name) == len)
202 return 1;
203
7e89a569 204 /* name match with instance number */
b9b659ae
GKH
205 if (isdigit(class_dev->name[len]))
206 return 1;
207 }
a8b5267a
KS
208 }
209
210 return 0;
211}
212
e629ca3f
KS
213/* skip waiting for the bus */
214static int class_device_expect_no_bus(struct sysfs_class_device *class_dev)
215{
216 static char *devices_without_bus[] = {
217 "scsi_host",
1a13fcb3 218 "i2c-adapter",
e629ca3f
KS
219 NULL
220 };
221 char **device;
222
223 for (device = devices_without_bus; *device != NULL; device++) {
224 int len = strlen(*device);
225
226 if (strncmp(class_dev->classname, *device, len) == 0)
227 return 1;
228 }
229
230 return 0;
231}
232
233/* wait for the bus and for a bus specific file to show up */
dcb9c852
KS
234static int wait_for_bus_device(struct sysfs_device *devices_dev,
235 const char **error)
a8b5267a
KS
236{
237 static struct bus_file {
238 char *bus;
239 char *file;
240 } bus_files[] = {
241 { .bus = "scsi", .file = "vendor" },
242 { .bus = "usb", .file = "idVendor" },
243 { .bus = "usb", .file = "iInterface" },
e284ecba 244 { .bus = "usb", .file = "bNumEndpoints" },
a8b5267a
KS
245 { .bus = "usb-serial", .file = "detach_state" },
246 { .bus = "ide", .file = "detach_state" },
247 { .bus = "pci", .file = "vendor" },
e629ca3f 248 { .bus = "platform", .file = "detach_state" },
606397db 249 { .bus = "i2c", .file = "detach_state" },
a8b5267a
KS
250 { NULL }
251 };
252 struct bus_file *busfile;
253 int loop;
254
dcb9c852 255 /* wait for the bus device link to the devices device */
a8b5267a
KS
256 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
257 while (--loop) {
dcb9c852 258 if (sysfs_get_device_bus(devices_dev) == 0)
a8b5267a
KS
259 break;
260
261 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
262 }
263 if (loop == 0) {
dcb9c852
KS
264 dbg("error: getting bus device link");
265 *error = "no bus device link";
a8b5267a
KS
266 return -1;
267 }
dcb9c852 268 dbg("bus device link found for bus '%s'", devices_dev->bus);
a8b5267a
KS
269
270 /* wait for a bus specific file to show up */
271 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
272 while (--loop) {
e629ca3f
KS
273 int found = 0;
274
a8b5267a 275 for (busfile = bus_files; busfile->bus != NULL; busfile++) {
dcb9c852 276 if (strcmp(devices_dev->bus, busfile->bus) == 0) {
e629ca3f 277 found = 1;
dcb9c852
KS
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);
a8b5267a
KS
281 return 0;
282 }
a8b5267a
KS
283 }
284 }
e629ca3f 285 if (found == 0) {
dcb9c852 286 *error = "unknown bus";
e629ca3f 287 info("error: unknown bus, please report to "
dcb9c852 288 "<linux-hotplug-devel@lists.sourceforge.net> '%s'", devices_dev->bus);
e629ca3f
KS
289 return -1;
290 }
291 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
a8b5267a
KS
292 }
293
dcb9c852
KS
294 dbg("error: getting bus '%s' specific file '%s'", devices_dev->bus, busfile->file);
295 *error = "bus specific file unavailable";
a8b5267a
KS
296 return -1;
297}
298
dcb9c852
KS
299
300static struct sysfs_class_device *open_class_device(const char *path)
301{
302 struct sysfs_class_device *class_dev;
303 int loop;
304
305 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
306 while (--loop) {
307 class_dev = sysfs_open_class_device_path(path);
308 if (class_dev)
309 break;
310
311 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
312 }
313
314 return (class_dev);
315}
316
317static int wait_for_class_device(struct sysfs_class_device *class_dev,
318 const char **error)
319{
320 struct sysfs_class_device *class_dev_parent;
321 struct sysfs_device *devices_dev = NULL;
322 int loop;
323
324 if (wait_for_class_device_attributes(class_dev, error) != 0)
325 return -ENOENT;
326
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);
330 return -ENODEV;
331 }
332
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);
337
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;
341 while (--loop) {
342 if (class_dev_parent)
343 devices_dev = sysfs_get_classdev_device(class_dev_parent);
344 else
345 devices_dev = sysfs_get_classdev_device(class_dev);
346
347 if (devices_dev)
348 break;
349
350 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
351 }
352 if (!devices_dev) {
353 dbg(" error: no devices device symlink found");
354 *error = "no device symlink";
355 return -ENODEV;
356 }
357 dbg("device symlink found pointing to '%s'", devices_dev->path);
358
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);
362 return 0;
363 } else {
364 return wait_for_bus_device(devices_dev, error);
365 }
366}
367
368static struct sysfs_device *open_devices_device(const char *path)
369{
370 struct sysfs_device *devices_dev;
371 int loop;
372
373 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
374 while (--loop) {
375 devices_dev = sysfs_open_device_path(path);
376 if (devices_dev)
377 break;
378
379 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
380 }
381
382 return(devices_dev);
383}
384
a8b5267a
KS
385int main(int argc, char *argv[], char *envp[])
386{
387 const char *devpath = "";
388 const char *action;
389 const char *subsystem;
ff694eca 390 char sysfs_mnt_path[SYSFS_PATH_MAX];
a8b5267a
KS
391 char filename[SYSFS_PATH_MAX];
392 struct sysfs_class_device *class_dev;
dcb9c852 393 struct sysfs_device *devices_dev;
a8b5267a 394 int rc = 0;
dcb9c852 395 const char *error = NULL;
a8b5267a 396
7257cb18 397 logging_init("wait_for_sysfs");
e629ca3f 398
a8b5267a
KS
399 if (argc != 2) {
400 dbg("error: subsystem");
401 return 1;
402 }
403 subsystem = argv[1];
404
405 devpath = getenv ("DEVPATH");
406 if (!devpath) {
407 dbg("error: no DEVPATH");
dcb9c852
KS
408 rc = 1;
409 goto exit;
a8b5267a
KS
410 }
411
412 action = getenv ("ACTION");
413 if (!action) {
414 dbg("error: no ACTION");
dcb9c852
KS
415 rc = 1;
416 goto exit;
a8b5267a
KS
417 }
418
e629ca3f 419 /* we only wait on an add event */
dcb9c852
KS
420 if (strcmp(action, "add") != 0) {
421 dbg("no add ACTION");
422 goto exit;
423 }
a8b5267a 424
ff694eca 425 if (sysfs_get_mnt_path(sysfs_mnt_path, SYSFS_PATH_MAX) != 0) {
a8b5267a 426 dbg("error: no sysfs path");
dcb9c852
KS
427 rc = 2;
428 goto exit;
a8b5267a
KS
429 }
430
431 if ((strncmp(devpath, "/block/", 7) == 0) || (strncmp(devpath, "/class/", 7) == 0)) {
ff694eca 432 snprintf(filename, SYSFS_PATH_MAX-1, "%s%s", sysfs_mnt_path, devpath);
a8b5267a
KS
433 filename[SYSFS_PATH_MAX-1] = '\0';
434
dcb9c852
KS
435 /* open the class device we are called for */
436 class_dev = open_class_device(filename);
437 if (!class_dev) {
438 dbg("error: class device unavailable (probably remove has beaten us)");
a8b5267a
KS
439 goto exit;
440 }
dcb9c852 441 dbg("class device opened '%s'", filename);
a8b5267a 442
dcb9c852
KS
443 /* wait for the class device with possible physical device and bus */
444 wait_for_class_device(class_dev, &error);
a8b5267a 445
e629ca3f 446 sysfs_close_class_device(class_dev);
a8b5267a
KS
447
448 } else if ((strncmp(devpath, "/devices/", 9) == 0)) {
ff694eca 449 snprintf(filename, SYSFS_PATH_MAX-1, "%s%s", sysfs_mnt_path, devpath);
a8b5267a
KS
450 filename[SYSFS_PATH_MAX-1] = '\0';
451
dcb9c852
KS
452 /* open the path we are called for */
453 devices_dev = open_devices_device(filename);
454 if (!devices_dev) {
455 dbg("error: devices device unavailable (probably remove has beaten us)");
a8b5267a
KS
456 goto exit;
457 }
dcb9c852 458 dbg("devices device opened '%s'", filename);
a8b5267a
KS
459
460 /* wait for the bus value */
dcb9c852 461 wait_for_bus_device(devices_dev, &error);
a8b5267a 462
dcb9c852 463 sysfs_close_device(devices_dev);
a8b5267a 464
a8b5267a
KS
465 } else {
466 dbg("unhandled sysfs path, no need to wait");
467 }
468
469exit:
dcb9c852 470 if (error) {
cdd95f9a 471 info("either wait_for_sysfs (udev %s) needs an update to handle the device '%s' "
dcb9c852 472 "properly (%s) or the sysfs-support of your device's driver needs to be fixed, "
cdd95f9a 473 "please report to <linux-hotplug-devel@lists.sourceforge.net>",
dcb9c852
KS
474 UDEV_VERSION, devpath, error);
475 rc =3;
476 } else {
477 dbg("result: waiting for sysfs successful '%s'", devpath);
478 }
a8b5267a 479
7257cb18 480 logging_close();
dcb9c852 481 exit(rc);
a8b5267a 482}