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