chiark / gitweb /
[PATCH] 039 release
[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 {
61         static struct class_file {
62                 char *subsystem;
63                 char *file;
64         } class_files[] = {
65                 { .subsystem = "net",           .file = "ifindex" },
66                 { .subsystem = "scsi_host",     .file = "unique_id" },
67                 { .subsystem = "scsi_device",   .file = NULL },
68                 { .subsystem = "pcmcia_socket", .file = "card_type" },
69                 { .subsystem = "usb_host",      .file = NULL },
70                 { .subsystem = "bluetooth",     .file = "address" },
71                 { .subsystem = "firmware",      .file = "data" },
72                 { .subsystem = "i2c-adapter",   .file = NULL },
73                 { .subsystem = "pci_bus",       .file = NULL },
74                 { .subsystem = "ieee1394",      .file = NULL },
75                 { .subsystem = "ieee1394_host", .file = NULL },
76                 { .subsystem = "ieee1394_node", .file = NULL },
77                 { NULL, NULL }
78         };
79         struct class_file *classfile;
80         const char *file = "dev";
81         char filename[FILENAME_MAX];
82         int loop;
83
84         /* look if we want to look for another file instead of "dev" */
85         for (classfile = class_files; classfile->subsystem != NULL; classfile++) {
86                 if (strcmp(class_dev->classname, classfile->subsystem) == 0) {
87                         if (classfile->file == NULL) {
88                                 dbg("class '%s' has no file to wait for", class_dev->classname);
89                                 return 0;
90                         }
91                         file = classfile->file;
92                         break;
93                 }
94         }
95
96         strcpy(filename, class_dev->path);
97         strcat(filename, "/");
98         strcat(filename, file);
99         dbg("looking at class '%s' for specific file '%s' with full name %s", class_dev->classname, class_dev->path, filename);
100
101         loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
102         while (--loop) {
103                 struct stat stats;
104
105                 if (stat(class_dev->path, &stats) == -1) {
106                         dbg("oops, the directory '%s' just disappeared.", class_dev->path);
107                         return -ENODEV;
108                 }
109
110                 if (stat(filename, &stats) == 0) {      
111                         dbg("class '%s' specific file '%s' found", class_dev->classname, file);
112                         return 0;
113                 }
114
115                 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
116         }
117
118         dbg("error: getting class '%s' specific file '%s'", class_dev->classname, file);
119         return -ENOENT;
120 }
121
122 /* check if we need to wait for a physical device */
123 static int class_device_expect_no_device_link(struct sysfs_class_device *class_dev)
124 {
125         /* list of devices without a "device" symlink to the physical device
126          * if device is set to NULL, no devices in that subsystem has a link */
127         static struct class_device {
128                 char *subsystem;
129                 char *device;
130         } class_device[] = {
131                 { .subsystem = "block",         .device = "double" },
132                 { .subsystem = "block",         .device = "nb" },
133                 { .subsystem = "block",         .device = "ram" },
134                 { .subsystem = "block",         .device = "loop" },
135                 { .subsystem = "block",         .device = "fd" },
136                 { .subsystem = "block",         .device = "md" },
137                 { .subsystem = "block",         .device = "dos_cd" },
138                 { .subsystem = "block",         .device = "rflash" },
139                 { .subsystem = "block",         .device = "rom" },
140                 { .subsystem = "block",         .device = "rrom" },
141                 { .subsystem = "block",         .device = "flash" },
142                 { .subsystem = "block",         .device = "msd" },
143                 { .subsystem = "block",         .device = "sbpcd" },
144                 { .subsystem = "block",         .device = "pcd" },
145                 { .subsystem = "block",         .device = "pf" },
146                 { .subsystem = "block",         .device = "scd" },
147                 { .subsystem = "block",         .device = "ubd" },
148                 { .subsystem = "input",         .device = "event" },
149                 { .subsystem = "input",         .device = "mice" },
150                 { .subsystem = "input",         .device = "mouse" },
151                 { .subsystem = "input",         .device = "ts" },
152                 { .subsystem = "vc",            .device = NULL },
153                 { .subsystem = "tty",           .device = NULL },
154                 { .subsystem = "cpuid",         .device = "cpu" },
155                 { .subsystem = "graphics",      .device = "fb" },
156                 { .subsystem = "mem",           .device = NULL },
157                 { .subsystem = "misc",          .device = NULL },
158                 { .subsystem = "msr",           .device = NULL },
159                 { .subsystem = "netlink",       .device = NULL },
160                 { .subsystem = "net",           .device = "sit" },
161                 { .subsystem = "net",           .device = "ppp" },
162                 { .subsystem = "net",           .device = "lo" },
163                 { .subsystem = "net",           .device = "tap" },
164                 { .subsystem = "net",           .device = "ipsec" },
165                 { .subsystem = "net",           .device = "irda" },
166                 { .subsystem = "sound",         .device = NULL },
167                 { .subsystem = "printer",       .device = "lp" },
168                 { .subsystem = "nvidia",        .device = NULL },
169                 { .subsystem = "video4linux",   .device = "vbi" },
170                 { .subsystem = "lirc",          .device = NULL },
171                 { .subsystem = "firmware",      .device = NULL },
172                 { .subsystem = "drm",           .device = NULL },
173                 { .subsystem = "pci_bus",       .device = NULL },
174                 { .subsystem = "ieee1394",      .device = NULL },
175                 { .subsystem = "ieee1394_host", .device = NULL },
176                 { .subsystem = "ieee1394_node", .device = NULL },
177                 { .subsystem = "raw",           .device = NULL },
178                 { NULL, NULL }
179         };
180         struct class_device *classdevice;
181         int len;
182
183         for (classdevice = class_device; classdevice->subsystem != NULL; classdevice++) {
184                 if (strcmp(class_dev->classname, classdevice->subsystem) == 0) {
185                         /* see if no device in this class is expected to have a device-link */
186                         if (classdevice->device == NULL)
187                                 return 1;
188
189                         len = strlen(classdevice->device);
190
191                         /* see if device name matches */
192                         if (strncmp(class_dev->name, classdevice->device, len) != 0)
193                                 continue;
194
195                         /* exact name match */
196                         if (strlen(class_dev->name) == len)
197                                 return 1;
198
199                         /* name match with instance number */
200                         if (isdigit(class_dev->name[len]))
201                                 return 1;
202                 }
203         }
204
205         return 0;
206 }
207
208 /* skip waiting for the bus */
209 static int class_device_expect_no_bus(struct sysfs_class_device *class_dev)
210 {
211         static char *devices_without_bus[] = {
212                 "scsi_host",
213                 "i2c-adapter",
214                 NULL
215         };
216         char **device;
217
218         for (device = devices_without_bus; *device != NULL; device++) {
219                 int len = strlen(*device);
220
221                 if (strncmp(class_dev->classname, *device, len) == 0)
222                         return 1;
223         }
224
225         return 0;
226 }
227
228 /* wait for the bus and for a bus specific file to show up */
229 static int wait_for_bus_device(struct sysfs_device *device_dev)
230 {
231         static struct bus_file {
232                 char *bus;
233                 char *file;
234         } bus_files[] = {
235                 { .bus = "scsi",        .file = "vendor" },
236                 { .bus = "usb",         .file = "idVendor" },
237                 { .bus = "usb",         .file = "iInterface" },
238                 { .bus = "usb",         .file = "bNumEndpoints" },
239                 { .bus = "usb-serial",  .file = "detach_state" },
240                 { .bus = "ide",         .file = "detach_state" },
241                 { .bus = "pci",         .file = "vendor" },
242                 { .bus = "platform",    .file = "detach_state" },
243                 { .bus = "i2c",         .file = "detach_state" },
244                 { NULL }
245         };
246         struct bus_file *busfile;
247         int loop;
248
249         /* wait for the /bus-device link to the /device-device */
250         loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
251         while (--loop) {
252                 if (sysfs_get_device_bus(device_dev) == 0)
253                         break;
254
255                 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
256         }
257         if (loop == 0) {
258                 dbg("error: getting /bus-device link");
259                 return -1;
260         }
261         dbg("/bus-device link found for bus '%s'", device_dev->bus);
262
263         /* wait for a bus specific file to show up */
264         loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
265         while (--loop) {
266                 int found = 0;
267
268                 for (busfile = bus_files; busfile->bus != NULL; busfile++) {
269                         if (strcmp(device_dev->bus, busfile->bus) == 0) {
270                                 found = 1;
271                                 dbg("looking at bus '%s' for specific file '%s'", device_dev->bus, busfile->file);
272                                 if (sysfs_get_device_attr(device_dev, busfile->file) != NULL) {
273                                         dbg("bus '%s' specific file '%s' found", device_dev->bus, busfile->file);
274                                         return 0;
275                                 }
276                         }
277                 }
278                 if (found == 0) {
279                         info("error: unknown bus, please report to "
280                              "<linux-hotplug-devel@lists.sourceforge.net> '%s'", device_dev->bus);
281                         return -1;
282                 }
283                 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
284         }
285
286         dbg("error: getting bus '%s' specific file '%s'", device_dev->bus, busfile->file);
287         return -1;
288 }
289
290 int main(int argc, char *argv[], char *envp[])
291 {
292         const char *devpath = "";
293         const char *action;
294         const char *subsystem;
295         char sysfs_path[SYSFS_PATH_MAX];
296         char filename[SYSFS_PATH_MAX];
297         struct sysfs_class_device *class_dev;
298         struct sysfs_class_device *class_dev_parent;
299         struct sysfs_device *device_dev = NULL;
300         int loop;
301         int retval;
302         int rc = 0;
303
304         init_logging("wait_for_sysfs");
305
306         if (argc != 2) {
307                 dbg("error: subsystem");
308                 return 1;
309         }
310         subsystem = argv[1];
311
312         devpath = getenv ("DEVPATH");
313         if (!devpath) {
314                 dbg("error: no DEVPATH");
315                 return 1;
316         }
317
318         action = getenv ("ACTION");
319         if (!action) {
320                 dbg("error: no ACTION");
321                 return 1;
322         }
323
324         /* we only wait on an add event */
325         if (strcmp(action, "add") != 0)
326                 return 0;
327
328         if (sysfs_get_mnt_path(sysfs_path, SYSFS_PATH_MAX) != 0) {
329                 dbg("error: no sysfs path");
330                 return 2;
331         }
332
333         if ((strncmp(devpath, "/block/", 7) == 0) || (strncmp(devpath, "/class/", 7) == 0)) {
334                 /* open the class device we are called for */
335                 snprintf(filename, SYSFS_PATH_MAX-1, "%s%s", sysfs_path, devpath);
336                 filename[SYSFS_PATH_MAX-1] = '\0';
337
338                 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
339                 while (--loop) {
340                         class_dev = sysfs_open_class_device_path(filename);
341                         if (class_dev)
342                                 break;
343
344                         usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
345                 }
346                 if (class_dev == NULL) {
347                         dbg("error: getting class_device");
348                         rc = 3;
349                         goto exit;
350                 }
351                 dbg("class_device opened '%s'", filename);
352
353                 retval = wait_for_class_device_attributes(class_dev);
354                 if (retval == -ENODEV)
355                         goto exit_class;
356                 if (retval != 0) {
357                         rc = 4;
358                         goto exit_class;
359                 }
360
361                 /* skip devices without /device-link */
362                 if (class_device_expect_no_device_link(class_dev)) {
363                         dbg("no device symlink expected for '%s', ", class_dev->name);
364                         goto exit_class;
365                 }
366
367                 /* the symlink may be on the parent device */
368                 class_dev_parent = sysfs_get_classdev_parent(class_dev);
369                 if (class_dev_parent)
370                         dbg("looking at parent device for device link '%s'", class_dev_parent->path);
371
372                 /* wait for the symlink to the /device-device */
373                 dbg("waiting for symlink to /device-device");
374                 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
375                 while (--loop) {
376                         if (class_dev_parent)
377                                 device_dev = sysfs_get_classdev_device(class_dev_parent);
378                         else
379                                 device_dev = sysfs_get_classdev_device(class_dev);
380
381                         if (device_dev)
382                                 break;
383
384                         usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
385                 }
386                 if (device_dev == NULL) {
387                         dbg("error: getting /device-device");
388                         rc = 5;
389                         goto exit_class;
390                 }
391                 dbg("device symlink found pointing to '%s'", device_dev->path);
392
393                 /* wait for the bus value */
394                 if (class_device_expect_no_bus(class_dev)) {
395                         dbg("no bus device expected for '%s', ", class_dev->classname);
396                 } else {
397                         if (wait_for_bus_device(device_dev) != 0)
398                                 rc = 6;
399                 }
400
401 exit_class:
402                 sysfs_close_class_device(class_dev);
403
404         } else if ((strncmp(devpath, "/devices/", 9) == 0)) {
405                 /* open the path we are called for */
406                 snprintf(filename, SYSFS_PATH_MAX-1, "%s%s", sysfs_path, devpath);
407                 filename[SYSFS_PATH_MAX-1] = '\0';
408
409                 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
410                 while (--loop) {
411                         device_dev = sysfs_open_device_path(filename);
412                         if (device_dev)
413                                 break;
414
415                         usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
416                 }
417                 if (device_dev == NULL) {
418                         dbg("error: getting /device-device");
419                         rc = 7;
420                         goto exit;
421                 }
422                 dbg("device_device opened '%s'", filename);
423
424                 /* wait for the bus value */
425                 if (wait_for_bus_device(device_dev) != 0)
426                         rc = 8;
427
428                 sysfs_close_device(device_dev);
429
430         } else {
431                 dbg("unhandled sysfs path, no need to wait");
432         }
433
434 exit:
435         if (rc == 0)
436                 dbg("result: waiting for sysfs successful '%s'", devpath);
437         else
438                 info("either wait_for_sysfs (udev %s) needs an update to handle the device '%s' "
439                      "properly (%d) or the sysfs-support of your device's driver needs to be fixed, "
440                      "please report to <linux-hotplug-devel@lists.sourceforge.net>",
441                      UDEV_VERSION, devpath, rc);
442
443         return rc;
444 }