chiark / gitweb /
1d7044c61f30e2adb73a71c70d375e669263ab78
[elogind.git] / wait_for_sysfs.c
1 /*
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.
7  *
8  * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
9  * Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com>
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"
36 #include "udev_version.h"
37 #include "libsysfs/sysfs/libsysfs.h"
38
39 #ifndef FILENAME_MAX
40 #define FILENAME_MAX    4096
41 #endif
42
43 #ifdef LOG
44 unsigned char logname[LOGNAME_SIZE];
45 void 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
55 #define WAIT_MAX_SECONDS                5
56 #define WAIT_LOOP_PER_SECOND            20
57
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,
60                                             const char **error)
61 {
62         static struct class_file {
63                 char *subsystem;
64                 char *file;
65         } class_files[] = {
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 },
78                 { NULL, NULL }
79         };
80         struct class_file *classfile;
81         char *file = "dev";
82         char filename[FILENAME_MAX];
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         }
96
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);
101
102         loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
103         while (--loop) {
104                 struct stat stats;
105
106                 if (stat(class_dev->path, &stats) == -1) {
107                         dbg("'%s' now disappeared (probably remove has beaten us)", class_dev->path);
108                         return -ENODEV;
109                 }
110
111                 if (stat(filename, &stats) == 0) {
112                         dbg("class '%s' specific file '%s' found", class_dev->classname, file);
113                         return 0;
114                 }
115
116                 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
117         }
118
119         dbg("error: getting class '%s' specific file '%s'", class_dev->classname, file);
120         *error = "class specific file unavailable";
121         return -ENOENT;
122 }
123
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)
126 {
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 {
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" },
150                 { .subsystem = "input",         .device = "event" },
151                 { .subsystem = "input",         .device = "mice" },
152                 { .subsystem = "input",         .device = "mouse" },
153                 { .subsystem = "input",         .device = "ts" },
154                 { .subsystem = "vc",            .device = NULL },
155                 { .subsystem = "tty",           .device = NULL },
156                 { .subsystem = "cpuid",         .device = "cpu" },
157                 { .subsystem = "graphics",      .device = "fb" },
158                 { .subsystem = "mem",           .device = NULL },
159                 { .subsystem = "misc",          .device = NULL },
160                 { .subsystem = "msr",           .device = NULL },
161                 { .subsystem = "netlink",       .device = NULL },
162                 { .subsystem = "net",           .device = "sit" },
163                 { .subsystem = "net",           .device = "lo" },
164                 { .subsystem = "net",           .device = "tap" },
165                 { .subsystem = "net",           .device = "ipsec" },
166                 { .subsystem = "net",           .device = "irda" },
167                 { .subsystem = "net",           .device = "ppp" },
168                 { .subsystem = "ppp",           .device = NULL },
169                 { .subsystem = "sound",         .device = NULL },
170                 { .subsystem = "printer",       .device = "lp" },
171                 { .subsystem = "nvidia",        .device = NULL },
172                 { .subsystem = "video4linux",   .device = "vbi" },
173                 { .subsystem = "lirc",          .device = NULL },
174                 { .subsystem = "firmware",      .device = NULL },
175                 { .subsystem = "drm",           .device = NULL },
176                 { .subsystem = "pci_bus",       .device = NULL },
177                 { .subsystem = "ieee1394",      .device = NULL },
178                 { .subsystem = "ieee1394_host", .device = NULL },
179                 { .subsystem = "ieee1394_node", .device = NULL },
180                 { .subsystem = "raw",           .device = NULL },
181                 { NULL, NULL }
182         };
183         struct class_device *classdevice;
184         int len;
185
186         for (classdevice = class_device; classdevice->subsystem != NULL; classdevice++) {
187                 if (strcmp(class_dev->classname, classdevice->subsystem) == 0) {
188                         /* see if no device in this class is expected to have a device-link */
189                         if (classdevice->device == NULL)
190                                 return 1;
191
192                         len = strlen(classdevice->device);
193
194                         /* see if device name matches */
195                         if (strncmp(class_dev->name, classdevice->device, len) != 0)
196                                 continue;
197
198                         /* exact name match */
199                         if (strlen(class_dev->name) == len)
200                                 return 1;
201
202                         /* name match with instance number */
203                         if (isdigit(class_dev->name[len]))
204                                 return 1;
205                 }
206         }
207
208         return 0;
209 }
210
211 /* skip waiting for the bus */
212 static int class_device_expect_no_bus(struct sysfs_class_device *class_dev)
213 {
214         static char *devices_without_bus[] = {
215                 "scsi_host",
216                 "i2c-adapter",
217                 NULL
218         };
219         char **device;
220
221         for (device = devices_without_bus; *device != NULL; device++) {
222                 int len = strlen(*device);
223
224                 if (strncmp(class_dev->classname, *device, len) == 0)
225                         return 1;
226         }
227
228         return 0;
229 }
230
231 /* wait for the bus and for a bus specific file to show up */
232 static int wait_for_bus_device(struct sysfs_device *devices_dev,
233                                const char **error)
234 {
235         static struct bus_file {
236                 char *bus;
237                 char *file;
238         } bus_files[] = {
239                 { .bus = "scsi",        .file = "vendor" },
240                 { .bus = "usb",         .file = "idVendor" },
241                 { .bus = "usb",         .file = "iInterface" },
242                 { .bus = "usb",         .file = "bNumEndpoints" },
243                 { .bus = "usb-serial",  .file = "detach_state" },
244                 { .bus = "ide",         .file = "detach_state" },
245                 { .bus = "pci",         .file = "vendor" },
246                 { .bus = "platform",    .file = "detach_state" },
247                 { .bus = "i2c",         .file = "detach_state" },
248                 { NULL }
249         };
250         struct bus_file *busfile;
251         int loop;
252
253         /* wait for the bus device link to the devices device */
254         loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
255         while (--loop) {
256                 if (sysfs_get_device_bus(devices_dev) == 0)
257                         break;
258
259                 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
260         }
261         if (loop == 0) {
262                 dbg("error: getting bus device link");
263                 *error = "no bus device link";
264                 return -1;
265         }
266         dbg("bus device link found for bus '%s'", devices_dev->bus);
267
268         /* wait for a bus specific file to show up */
269         loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
270         while (--loop) {
271                 int found = 0;
272
273                 for (busfile = bus_files; busfile->bus != NULL; busfile++) {
274                         if (strcmp(devices_dev->bus, busfile->bus) == 0) {
275                                 found = 1;
276                                 dbg("looking at bus '%s' for specific file '%s'", devices_dev->bus, busfile->file);
277                                 if (sysfs_get_device_attr(devices_dev, busfile->file) != NULL) {
278                                         dbg("bus '%s' specific file '%s' found", devices_dev->bus, busfile->file);
279                                         return 0;
280                                 }
281                         }
282                 }
283                 if (found == 0) {
284                         *error = "unknown bus";
285                         info("error: unknown bus, please report to "
286                              "<linux-hotplug-devel@lists.sourceforge.net> '%s'", devices_dev->bus);
287                         return -1;
288                 }
289                 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
290         }
291
292         dbg("error: getting bus '%s' specific file '%s'", devices_dev->bus, busfile->file);
293         *error = "bus specific file unavailable";
294         return -1;
295 }
296
297
298 static struct sysfs_class_device *open_class_device(const char *path)
299 {
300         struct sysfs_class_device *class_dev;
301         int loop;
302
303         loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
304         while (--loop) {
305                 class_dev = sysfs_open_class_device_path(path);
306                 if (class_dev)
307                         break;
308
309                 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
310         }
311
312         return (class_dev);
313 }
314
315 static int wait_for_class_device(struct sysfs_class_device *class_dev,
316                                  const char **error)
317 {
318         struct sysfs_class_device *class_dev_parent;
319         struct sysfs_device *devices_dev = NULL;
320         int loop;
321
322         if (wait_for_class_device_attributes(class_dev, error) != 0)
323                 return -ENOENT;
324
325         /* skip devices without devices-link */
326         if (class_device_expect_no_device_link(class_dev)) {
327                 dbg("no device symlink expected for '%s', ", class_dev->name);
328                 return -ENODEV;
329         }
330
331         /* the symlink may be on the parent device */
332         class_dev_parent = sysfs_get_classdev_parent(class_dev);
333         if (class_dev_parent)
334                 dbg("looking at parent device for device link '%s'", class_dev_parent->path);
335
336         /* wait for the symlink to the devices device */
337         dbg("waiting for symlink to devices device");
338         loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
339         while (--loop) {
340                 if (class_dev_parent)
341                         devices_dev = sysfs_get_classdev_device(class_dev_parent);
342                 else
343                         devices_dev = sysfs_get_classdev_device(class_dev);
344
345                 if (devices_dev)
346                         break;
347
348                 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
349         }
350         if (!devices_dev) {
351                 dbg(" error: no devices device symlink found");
352                 *error = "no device symlink";
353                 return -ENODEV;
354         }
355         dbg("device symlink found pointing to '%s'", devices_dev->path);
356
357         /* wait for the bus value */
358         if (class_device_expect_no_bus(class_dev)) {
359                 dbg("no bus device expected for '%s', ", class_dev->classname);
360                 return 0;
361         } else {
362                 return wait_for_bus_device(devices_dev, error);
363         }
364 }
365
366 static struct sysfs_device *open_devices_device(const char *path)
367 {
368         struct sysfs_device *devices_dev;
369         int loop;
370
371         loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
372         while (--loop) {
373                 devices_dev = sysfs_open_device_path(path);
374                 if (devices_dev)
375                         break;
376
377                 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
378         }
379
380         return(devices_dev);
381 }
382
383 int main(int argc, char *argv[], char *envp[])
384 {
385         const char *devpath = "";
386         const char *action;
387         const char *subsystem;
388         char sysfs_path[SYSFS_PATH_MAX];
389         char filename[SYSFS_PATH_MAX];
390         struct sysfs_class_device *class_dev;
391         struct sysfs_device *devices_dev;
392         int rc = 0;
393         const char *error = NULL;
394
395         init_logging("wait_for_sysfs");
396
397         if (argc != 2) {
398                 dbg("error: subsystem");
399                 return 1;
400         }
401         subsystem = argv[1];
402
403         devpath = getenv ("DEVPATH");
404         if (!devpath) {
405                 dbg("error: no DEVPATH");
406                 rc = 1;
407                 goto exit;
408         }
409
410         action = getenv ("ACTION");
411         if (!action) {
412                 dbg("error: no ACTION");
413                 rc = 1;
414                 goto exit;
415         }
416
417         /* we only wait on an add event */
418         if (strcmp(action, "add") != 0) {
419                 dbg("no add ACTION");
420                 goto exit;
421         }
422
423         if (sysfs_get_mnt_path(sysfs_path, SYSFS_PATH_MAX) != 0) {
424                 dbg("error: no sysfs path");
425                 rc = 2;
426                 goto exit;
427         }
428
429         if ((strncmp(devpath, "/block/", 7) == 0) || (strncmp(devpath, "/class/", 7) == 0)) {
430                 snprintf(filename, SYSFS_PATH_MAX-1, "%s%s", sysfs_path, devpath);
431                 filename[SYSFS_PATH_MAX-1] = '\0';
432
433                 /* open the class device we are called for */
434                 class_dev = open_class_device(filename);
435                 if (!class_dev) {
436                         dbg("error: class device unavailable (probably remove has beaten us)");
437                         goto exit;
438                 }
439                 dbg("class device opened '%s'", filename);
440
441                 /* wait for the class device with possible physical device and bus */
442                 wait_for_class_device(class_dev, &error);
443
444                 sysfs_close_class_device(class_dev);
445
446         } else if ((strncmp(devpath, "/devices/", 9) == 0)) {
447                 snprintf(filename, SYSFS_PATH_MAX-1, "%s%s", sysfs_path, devpath);
448                 filename[SYSFS_PATH_MAX-1] = '\0';
449
450                 /* open the path we are called for */
451                 devices_dev = open_devices_device(filename);
452                 if (!devices_dev) {
453                         dbg("error: devices device unavailable (probably remove has beaten us)");
454                         goto exit;
455                 }
456                 dbg("devices device opened '%s'", filename);
457
458                 /* wait for the bus value */
459                 wait_for_bus_device(devices_dev, &error);
460
461                 sysfs_close_device(devices_dev);
462
463         } else {
464                 dbg("unhandled sysfs path, no need to wait");
465         }
466
467 exit:
468         if (error) {
469                 info("either wait_for_sysfs (udev %s) needs an update to handle the device '%s' "
470                      "properly (%s) or the sysfs-support of your device's driver needs to be fixed, "
471                      "please report to <linux-hotplug-devel@lists.sourceforge.net>",
472                      UDEV_VERSION, devpath, error);
473                 rc =3;
474         } else {
475                 dbg("result: waiting for sysfs successful '%s'", devpath);
476         }
477
478         exit(rc);
479 }