chiark / gitweb /
[PATCH] added ieee1394 support to wait_for_sysfs
[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  *
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.
13  * 
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.
18  * 
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.
22  *
23  */
24
25 #include <stdio.h>
26 #include <stddef.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <string.h>
30 #include <ctype.h>
31 #include <errno.h>
32 #include <sys/stat.h>
33
34 #include "logging.h"
35 #include "libsysfs/sysfs/libsysfs.h"
36
37 #ifdef LOG
38 unsigned char logname[LOGNAME_SIZE];
39 void log_message(int level, const char *format, ...)
40 {
41         va_list args;
42
43         va_start(args, format);
44         vsyslog(level, format, args);
45         va_end(args);
46 }
47 #endif
48
49 #define WAIT_MAX_SECONDS                5
50 #define WAIT_LOOP_PER_SECOND            20
51
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)
54 {
55         static struct class_file {
56                 char *subsystem;
57                 char *file;
58         } class_files[] = {
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 },
66                 { .subsystem = "pci_bus",       .file = NULL },
67                 { .subsystem = "ieee1394",      .file = NULL },
68                 { .subsystem = "ieee1394_host", .file = NULL },
69                 { .subsystem = "ieee1394_node", .file = NULL },
70                 { NULL, NULL }
71         };
72         struct class_file *classfile;
73         const char *file = "dev";
74         int loop;
75
76         /* look if we want to look for another file instead of "dev" */
77         for (classfile = class_files; classfile->subsystem != NULL; classfile++) {
78                 if (strcmp(class_dev->classname, classfile->subsystem) == 0) {
79                         if (classfile->file == NULL) {
80                                 dbg("class '%s' has no file to wait for", class_dev->classname);
81                                 return 0;
82                         }
83                         file = classfile->file;
84                         break;
85                 }
86         }
87         dbg("looking at class '%s' for specific file '%s'", class_dev->classname, file);
88
89         loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
90         while (--loop) {
91                 if (sysfs_get_classdev_attr(class_dev, file) != NULL) {
92                         dbg("class '%s' specific file '%s' found", class_dev->classname, file);
93                         return 0;
94                 }
95
96                 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
97         }
98
99         dbg("error: getting class '%s' specific file '%s'", class_dev->classname, file);
100         return -1;
101 }
102
103 /* skip waiting for physical device */
104 static int class_device_expect_no_device_link(struct sysfs_class_device *class_dev)
105 {
106         /* List of devices without a "device" symlink
107          * set .device to NULL to accept all devices in that subsystem */
108         static struct class_device {
109                 char *subsystem;
110                 char *device;
111         } class_device[] = {
112                 { .subsystem = "block",         .device = "double" },
113                 { .subsystem = "block",         .device = "nb" },
114                 { .subsystem = "block",         .device = "ram" },
115                 { .subsystem = "block",         .device = "loop" },
116                 { .subsystem = "block",         .device = "fd" },
117                 { .subsystem = "block",         .device = "md" },
118                 { .subsystem = "block",         .device = "dos_cd" },
119                 { .subsystem = "block",         .device = "rflash" },
120                 { .subsystem = "block",         .device = "rom" },
121                 { .subsystem = "block",         .device = "rrom" },
122                 { .subsystem = "block",         .device = "flash" },
123                 { .subsystem = "block",         .device = "msd" },
124                 { .subsystem = "block",         .device = "sbpcd" },
125                 { .subsystem = "block",         .device = "pcd" },
126                 { .subsystem = "block",         .device = "pf" },
127                 { .subsystem = "block",         .device = "scd" },
128                 { .subsystem = "block",         .device = "ubd" },
129                 { .subsystem = "input",         .device = "event" },
130                 { .subsystem = "input",         .device = "mice" },
131                 { .subsystem = "input",         .device = "mouse" },
132                 { .subsystem = "input",         .device = "ts" },
133                 { .subsystem = "vc",            .device = NULL },
134                 { .subsystem = "tty",           .device = NULL },
135                 { .subsystem = "cpuid",         .device = "cpu" },
136                 { .subsystem = "graphics",      .device = "fb" },
137                 { .subsystem = "mem",           .device = NULL },
138                 { .subsystem = "misc",          .device = NULL },
139                 { .subsystem = "msr",           .device = NULL },
140                 { .subsystem = "netlink",       .device = NULL },
141                 { .subsystem = "net",           .device = NULL },
142                 { .subsystem = "sound",         .device = NULL },
143                 { .subsystem = "printer",       .device = "lp" },
144                 { .subsystem = "nvidia",        .device = NULL },
145                 { .subsystem = "video4linux",   .device = NULL },
146                 { .subsystem = "lirc",          .device = NULL },
147                 { .subsystem = "firmware",      .device = NULL },
148                 { .subsystem = "drm",           .device = NULL },
149                 { .subsystem = "pci_bus",       .device = NULL },
150                 { .subsystem = "ieee1394",      .device = NULL },
151                 { .subsystem = "ieee1394_host", .device = NULL },
152                 { .subsystem = "ieee1394_node", .device = NULL },
153                 { NULL, NULL }
154         };
155         struct class_device *classdevice;
156         int len;
157
158         /* look if we want to look for another file instead of "dev" */
159         for (classdevice = class_device; classdevice->subsystem != NULL; classdevice++) {
160                 if (strcmp(class_dev->classname, classdevice->subsystem) == 0) {
161                         /* if device is NULL, all devices in this class are ok */
162                         if (classdevice->device == NULL)
163                                 return 1;
164
165                         len = strlen(classdevice->device);
166
167                         /* see if device name matches */
168                         if (strncmp(class_dev->name, classdevice->device, len) != 0)
169                                 continue;
170
171                         /* exact match */
172                         if (strlen(class_dev->name) == len)
173                                 return 1;
174
175                         /* instance numbers are matching too */
176                         if (isdigit(class_dev->name[len]))
177                                 return 1;
178                 }
179         }
180
181         return 0;
182 }
183
184 /* skip waiting for the bus */
185 static int class_device_expect_no_bus(struct sysfs_class_device *class_dev)
186 {
187         static char *devices_without_bus[] = {
188                 "scsi_host",
189                 "i2c-adapter",
190                 NULL
191         };
192         char **device;
193
194         for (device = devices_without_bus; *device != NULL; device++) {
195                 int len = strlen(*device);
196
197                 if (strncmp(class_dev->classname, *device, len) == 0)
198                         return 1;
199         }
200
201         return 0;
202 }
203
204 /* wait for the bus and for a bus specific file to show up */
205 static int wait_for_bus_device(struct sysfs_device *device_dev)
206 {
207         static struct bus_file {
208                 char *bus;
209                 char *file;
210         } bus_files[] = {
211                 { .bus = "scsi",        .file = "vendor" },
212                 { .bus = "usb",         .file = "idVendor" },
213                 { .bus = "usb",         .file = "iInterface" },
214                 { .bus = "usb",         .file = "bNumEndpoints" },
215                 { .bus = "usb-serial",  .file = "detach_state" },
216                 { .bus = "ide",         .file = "detach_state" },
217                 { .bus = "pci",         .file = "vendor" },
218                 { .bus = "platform",    .file = "detach_state" },
219                 { .bus = "i2c",         .file = "detach_state" },
220                 { NULL }
221         };
222         struct bus_file *busfile;
223         int loop;
224
225         /* wait for the /bus-device link to the /device-device */
226         loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
227         while (--loop) {
228                 if (sysfs_get_device_bus(device_dev) == 0)
229                         break;
230
231                 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
232         }
233         if (loop == 0) {
234                 dbg("error: getting /bus-device link");
235                 return -1;
236         }
237         dbg("/bus-device link found for bus '%s'", device_dev->bus);
238
239         /* wait for a bus specific file to show up */
240         loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
241         while (--loop) {
242                 int found = 0;
243
244                 for (busfile = bus_files; busfile->bus != NULL; busfile++) {
245                         if (strcmp(device_dev->bus, busfile->bus) == 0) {
246                                 found = 1;
247                                 dbg("looking at bus '%s' for specific file '%s'", device_dev->bus, busfile->file);
248                                 if (sysfs_get_device_attr(device_dev, busfile->file) != NULL) {
249                                         dbg("bus '%s' specific file '%s' found", device_dev->bus, busfile->file);
250                                         return 0;
251                                 }
252                         }
253                 }
254                 if (found == 0) {
255                         info("error: unknown bus, please report to "
256                              "<linux-hotplug-devel@lists.sourceforge.net> '%s'", device_dev->bus);
257                         return -1;
258                 }
259                 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
260         }
261
262         dbg("error: getting bus '%s' specific file '%s'", device_dev->bus, busfile->file);
263         return -1;
264 }
265
266 int main(int argc, char *argv[], char *envp[])
267 {
268         const char *devpath = "";
269         const char *action;
270         const char *subsystem;
271         char sysfs_path[SYSFS_PATH_MAX];
272         char filename[SYSFS_PATH_MAX];
273         struct sysfs_class_device *class_dev;
274         struct sysfs_class_device *class_dev_parent;
275         struct sysfs_device *device_dev = NULL;
276         int loop;
277         int rc = 0;
278
279         init_logging("wait_for_sysfs");
280
281         if (argc != 2) {
282                 dbg("error: subsystem");
283                 return 1;
284         }
285         subsystem = argv[1];
286
287         devpath = getenv ("DEVPATH");
288         if (!devpath) {
289                 dbg("error: no DEVPATH");
290                 return 1;
291         }
292
293         action = getenv ("ACTION");
294         if (!action) {
295                 dbg("error: no ACTION");
296                 return 1;
297         }
298
299         /* we only wait on an add event */
300         if (strcmp(action, "add") != 0)
301                 return 0;
302
303         if (sysfs_get_mnt_path(sysfs_path, SYSFS_PATH_MAX) != 0) {
304                 dbg("error: no sysfs path");
305                 return 2;
306         }
307
308         if ((strncmp(devpath, "/block/", 7) == 0) || (strncmp(devpath, "/class/", 7) == 0)) {
309                 /* open the class device we are called for */
310                 snprintf(filename, SYSFS_PATH_MAX-1, "%s%s", sysfs_path, devpath);
311                 filename[SYSFS_PATH_MAX-1] = '\0';
312
313                 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
314                 while (--loop) {
315                         class_dev = sysfs_open_class_device_path(filename);
316                         if (class_dev)
317                                 break;
318
319                         usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
320                 }
321                 if (class_dev == NULL) {
322                         dbg("error: getting class_device");
323                         rc = 3;
324                         goto exit;
325                 }
326                 dbg("class_device opened '%s'", filename);
327
328                 if (wait_for_class_device_attributes(class_dev) != 0) {
329                         rc = 4;
330                         goto exit_class;
331                 }
332
333                 /* skip devices without /device-link */
334                 if (class_device_expect_no_device_link(class_dev)) {
335                         dbg("no device symlink expected for '%s', ", class_dev->name);
336                         goto exit_class;
337                 }
338
339                 /* the symlink may be on the parent device */
340                 class_dev_parent = sysfs_get_classdev_parent(class_dev);
341                 if (class_dev_parent)
342                         dbg("looking at parent device for device link '%s'", class_dev_parent->path);
343
344                 /* wait for the symlink to the /device-device */
345                 dbg("waiting for symlink to /device-device");
346                 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
347                 while (--loop) {
348                         if (class_dev_parent)
349                                 device_dev = sysfs_get_classdev_device(class_dev_parent);
350                         else
351                                 device_dev = sysfs_get_classdev_device(class_dev);
352
353                         if (device_dev)
354                                 break;
355
356                         usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
357                 }
358                 if (device_dev == NULL) {
359                         dbg("error: getting /device-device");
360                         rc = 5;
361                         goto exit_class;
362                 }
363                 dbg("device symlink found pointing to '%s'", device_dev->path);
364
365                 /* wait for the bus value */
366                 if (class_device_expect_no_bus(class_dev)) {
367                         dbg("no bus device expected for '%s', ", class_dev->classname);
368                 } else {
369                         if (wait_for_bus_device(device_dev) != 0)
370                                 rc = 6;
371                 }
372
373 exit_class:
374                 sysfs_close_class_device(class_dev);
375
376         } else if ((strncmp(devpath, "/devices/", 9) == 0)) {
377                 /* open the path we are called for */
378                 snprintf(filename, SYSFS_PATH_MAX-1, "%s%s", sysfs_path, devpath);
379                 filename[SYSFS_PATH_MAX-1] = '\0';
380
381                 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
382                 while (--loop) {
383                         device_dev = sysfs_open_device_path(filename);
384                         if (device_dev)
385                                 break;
386
387                         usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
388                 }
389                 if (device_dev == NULL) {
390                         dbg("error: getting /device-device");
391                         rc = 7;
392                         goto exit;
393                 }
394                 dbg("device_device opened '%s'", filename);
395
396                 /* wait for the bus value */
397                 if (wait_for_bus_device(device_dev) != 0)
398                         rc = 8;
399
400                 sysfs_close_device(device_dev);
401
402         } else {
403                 dbg("unhandled sysfs path, no need to wait");
404         }
405
406 exit:
407         if (rc == 0)
408                 dbg("result: waiting for sysfs successful '%s'", devpath);
409         else
410                 info("error: wait_for_sysfs needs an update to handle the device '%s' "
411                      "properly, please report to <linux-hotplug-devel@lists.sourceforge.net>",
412                      devpath);
413
414         return rc;
415 }