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