]>
git.ipfire.org Git - thirdparty/systemd.git/blob - wait_for_sysfs.c
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.
8 * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
9 * Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com>
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.
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.
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.
36 #include "udev_version.h"
37 #include "udev_sysfs.h"
38 #include "libsysfs/sysfs/libsysfs.h"
41 unsigned char logname
[LOGNAME_SIZE
];
42 void log_message(int level
, const char *format
, ...)
46 va_start(args
, format
);
47 vsyslog(level
, format
, args
);
52 int main(int argc
, char *argv
[], char *envp
[])
54 const char *devpath
= "";
56 const char *subsystem
;
57 char sysfs_mnt_path
[SYSFS_PATH_MAX
];
58 char filename
[SYSFS_PATH_MAX
];
59 struct sysfs_class_device
*class_dev
;
60 struct sysfs_device
*devices_dev
;
62 const char *error
= NULL
;
64 logging_init("wait_for_sysfs");
67 dbg("error: subsystem");
72 devpath
= getenv ("DEVPATH");
74 dbg("error: no DEVPATH");
79 action
= getenv ("ACTION");
81 dbg("error: no ACTION");
86 /* we only wait on an add event */
87 if (strcmp(action
, "add") != 0) {
92 if (sysfs_get_mnt_path(sysfs_mnt_path
, SYSFS_PATH_MAX
) != 0) {
93 dbg("error: no sysfs path");
98 if ((strncmp(devpath
, "/block/", 7) == 0) || (strncmp(devpath
, "/class/", 7) == 0)) {
99 snprintf(filename
, SYSFS_PATH_MAX
-1, "%s%s", sysfs_mnt_path
, devpath
);
100 filename
[SYSFS_PATH_MAX
-1] = '\0';
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
);
108 /* open the class device we are called for */
109 class_dev
= open_class_device_wait(filename
);
111 dbg("error: class device unavailable (probably remove has beaten us)");
114 dbg("class device opened '%s'", filename
);
116 /* wait for the class device with possible physical device and bus */
117 wait_for_class_device(class_dev
, &error
);
119 sysfs_close_class_device(class_dev
);
121 } else if ((strncmp(devpath
, "/devices/", 9) == 0)) {
122 snprintf(filename
, SYSFS_PATH_MAX
-1, "%s%s", sysfs_mnt_path
, devpath
);
123 filename
[SYSFS_PATH_MAX
-1] = '\0';
125 /* open the path we are called for */
126 devices_dev
= open_devices_device_wait(filename
);
128 dbg("error: devices device unavailable (probably remove has beaten us)");
131 dbg("devices device opened '%s'", filename
);
133 /* wait for the bus value */
134 wait_for_bus_device(devices_dev
, &error
);
136 sysfs_close_device(devices_dev
);
139 dbg("unhandled sysfs path, no need to wait");
144 info("either wait_for_sysfs (udev %s) needs an update to handle the device '%s' "
145 "properly (%s) or the sysfs-support of your device's driver needs to be fixed, "
146 "please report to <linux-hotplug-devel@lists.sourceforge.net>",
147 UDEV_VERSION
, devpath
, error
);
150 dbg("result: waiting for sysfs successful '%s'", devpath
);