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