]>
Commit | Line | Data |
---|---|---|
a8b5267a KS |
1 | /* |
2 | * wait_for_sysfs.c - small program to delay the execution | |
3 | * of /etc/hotplug.d/ programs, until sysfs | |
e629ca3f | 4 | * is fully populated by the kernel. Depending on |
a8b5267a KS |
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> | |
a8f2703a | 9 | * Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com> |
a8b5267a KS |
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" | |
cdd95f9a | 36 | #include "udev_version.h" |
f0713480 | 37 | #include "udev_sysfs.h" |
a8b5267a KS |
38 | #include "libsysfs/sysfs/libsysfs.h" |
39 | ||
40 | #ifdef LOG | |
41 | unsigned char logname[LOGNAME_SIZE]; | |
42 | void log_message(int level, const char *format, ...) | |
43 | { | |
44 | va_list args; | |
45 | ||
46 | va_start(args, format); | |
47 | vsyslog(level, format, args); | |
48 | va_end(args); | |
49 | } | |
50 | #endif | |
51 | ||
a8b5267a KS |
52 | int main(int argc, char *argv[], char *envp[]) |
53 | { | |
54 | const char *devpath = ""; | |
55 | const char *action; | |
56 | const char *subsystem; | |
ff694eca | 57 | char sysfs_mnt_path[SYSFS_PATH_MAX]; |
a8b5267a KS |
58 | char filename[SYSFS_PATH_MAX]; |
59 | struct sysfs_class_device *class_dev; | |
dcb9c852 | 60 | struct sysfs_device *devices_dev; |
a8b5267a | 61 | int rc = 0; |
dcb9c852 | 62 | const char *error = NULL; |
a8b5267a | 63 | |
7257cb18 | 64 | logging_init("wait_for_sysfs"); |
e629ca3f | 65 | |
a8b5267a KS |
66 | if (argc != 2) { |
67 | dbg("error: subsystem"); | |
68 | return 1; | |
69 | } | |
70 | subsystem = argv[1]; | |
71 | ||
72 | devpath = getenv ("DEVPATH"); | |
73 | if (!devpath) { | |
74 | dbg("error: no DEVPATH"); | |
dcb9c852 KS |
75 | rc = 1; |
76 | goto exit; | |
a8b5267a KS |
77 | } |
78 | ||
79 | action = getenv ("ACTION"); | |
80 | if (!action) { | |
81 | dbg("error: no ACTION"); | |
dcb9c852 KS |
82 | rc = 1; |
83 | goto exit; | |
a8b5267a KS |
84 | } |
85 | ||
e629ca3f | 86 | /* we only wait on an add event */ |
dcb9c852 KS |
87 | if (strcmp(action, "add") != 0) { |
88 | dbg("no add ACTION"); | |
89 | goto exit; | |
90 | } | |
a8b5267a | 91 | |
ff694eca | 92 | if (sysfs_get_mnt_path(sysfs_mnt_path, SYSFS_PATH_MAX) != 0) { |
a8b5267a | 93 | dbg("error: no sysfs path"); |
dcb9c852 KS |
94 | rc = 2; |
95 | goto exit; | |
a8b5267a KS |
96 | } |
97 | ||
98 | if ((strncmp(devpath, "/block/", 7) == 0) || (strncmp(devpath, "/class/", 7) == 0)) { | |
ff694eca | 99 | snprintf(filename, SYSFS_PATH_MAX-1, "%s%s", sysfs_mnt_path, devpath); |
a8b5267a KS |
100 | filename[SYSFS_PATH_MAX-1] = '\0'; |
101 | ||
ab510356 KS |
102 | /* skip bad events where we get no device for the class */ |
103 | if (strncmp(devpath, "/class/", 7) == 0 && strchr(&devpath[7], '/') == NULL) { | |
104 | dbg("no device name for '%s', bad event", devpath); | |
105 | goto exit; | |
106 | } | |
107 | ||
dcb9c852 | 108 | /* open the class device we are called for */ |
f0713480 | 109 | class_dev = open_class_device_wait(filename); |
dcb9c852 KS |
110 | if (!class_dev) { |
111 | dbg("error: class device unavailable (probably remove has beaten us)"); | |
a8b5267a KS |
112 | goto exit; |
113 | } | |
dcb9c852 | 114 | dbg("class device opened '%s'", filename); |
a8b5267a | 115 | |
dcb9c852 KS |
116 | /* wait for the class device with possible physical device and bus */ |
117 | wait_for_class_device(class_dev, &error); | |
a8b5267a | 118 | |
52a6ef66 KS |
119 | /* |
120 | * we got too many unfixable class/net errors, kernel later than 2.6.10-rc1 will | |
121 | * solve this by exporting the needed information with the hotplug event | |
122 | * until we use this just don't print any error for net devices, but still | |
123 | * wait for it. | |
124 | */ | |
125 | if (strncmp(devpath, "/class/net/", 11) == 0) | |
126 | error = NULL; | |
127 | ||
e629ca3f | 128 | sysfs_close_class_device(class_dev); |
a8b5267a KS |
129 | |
130 | } else if ((strncmp(devpath, "/devices/", 9) == 0)) { | |
ff694eca | 131 | snprintf(filename, SYSFS_PATH_MAX-1, "%s%s", sysfs_mnt_path, devpath); |
a8b5267a KS |
132 | filename[SYSFS_PATH_MAX-1] = '\0'; |
133 | ||
dcb9c852 | 134 | /* open the path we are called for */ |
f0713480 | 135 | devices_dev = open_devices_device_wait(filename); |
dcb9c852 KS |
136 | if (!devices_dev) { |
137 | dbg("error: devices device unavailable (probably remove has beaten us)"); | |
a8b5267a KS |
138 | goto exit; |
139 | } | |
dcb9c852 | 140 | dbg("devices device opened '%s'", filename); |
a8b5267a KS |
141 | |
142 | /* wait for the bus value */ | |
dcb9c852 | 143 | wait_for_bus_device(devices_dev, &error); |
a8b5267a | 144 | |
dcb9c852 | 145 | sysfs_close_device(devices_dev); |
a8b5267a | 146 | |
a8b5267a KS |
147 | } else { |
148 | dbg("unhandled sysfs path, no need to wait"); | |
149 | } | |
150 | ||
151 | exit: | |
dcb9c852 | 152 | if (error) { |
cdd95f9a | 153 | info("either wait_for_sysfs (udev %s) needs an update to handle the device '%s' " |
dcb9c852 | 154 | "properly (%s) or the sysfs-support of your device's driver needs to be fixed, " |
cdd95f9a | 155 | "please report to <linux-hotplug-devel@lists.sourceforge.net>", |
dcb9c852 KS |
156 | UDEV_VERSION, devpath, error); |
157 | rc =3; | |
158 | } else { | |
159 | dbg("result: waiting for sysfs successful '%s'", devpath); | |
160 | } | |
a8b5267a | 161 | |
7257cb18 | 162 | logging_close(); |
dcb9c852 | 163 | exit(rc); |
a8b5267a | 164 | } |