4 * Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com>
6 * Quick and dirty way to populate a /dev with udev if your system
7 * does not have access to a shell. Based originally on a patch to udev
8 * from Harald Hoyer <harald@redhat.com>
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.
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.
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.
36 #include <sys/types.h>
38 #include "libsysfs/sysfs/libsysfs.h"
39 #include "udev_libc_wrapper.h"
43 #include "udev_utils.h"
47 void log_message(int level, const char *format, ...)
53 struct list_head node;
55 char subsys[NAME_SIZE];
58 /* sort files in lexical order */
59 static int device_list_insert(const char *path, char *subsystem, struct list_head *device_list)
61 struct device *loop_device;
62 struct device *new_device;
64 dbg("insert: '%s'\n", path);
66 list_for_each_entry(loop_device, device_list, node) {
67 if (strcmp(loop_device->path, path) > 0) {
72 new_device = malloc(sizeof(struct device));
73 if (new_device == NULL) {
78 strlcpy(new_device->path, path, sizeof(new_device->path));
79 strlcpy(new_device->subsys, subsystem, sizeof(new_device->subsys));
80 list_add_tail(&new_device->node, &loop_device->node);
81 dbg("add '%s' from subsys '%s'", new_device->path, new_device->subsys);
85 /* list of devices that we should run last due to any one of a number of reasons */
86 static char *last_list[] = {
87 "/block/dm", /* on here because dm wants to have the block devices around before it */
91 /* list of devices that we should run first due to any one of a number of reasons */
92 static char *first_list[] = {
93 "/class/mem", /* people tend to like their memory devices around first... */
97 static int add_device(const char *path, const char *subsystem)
100 struct sysfs_class_device *class_dev;
103 devpath = &path[strlen(sysfs_path)];
105 /* set environment for callouts and dev.d/ */
106 setenv("DEVPATH", devpath, 1);
107 setenv("SUBSYSTEM", subsystem, 1);
109 dbg("exec: '%s' (%s)\n", devpath, path);
111 class_dev = sysfs_open_class_device_path(path);
112 if (class_dev == NULL) {
113 dbg ("sysfs_open_class_device_path failed");
117 udev_init_device(&udev, devpath, subsystem);
118 udev_add_device(&udev, class_dev);
120 /* run dev.d/ scripts if we created a node or changed a netif name */
121 if (udev_dev_d && udev.devname[0] != '\0') {
122 setenv("DEVNAME", udev.devname, 1);
123 udev_multiplex_directory(&udev, DEVD_DIR, DEVD_SUFFIX);
126 sysfs_close_class_device(class_dev);
127 udev_cleanup_device(&udev);
132 static void exec_list(struct list_head *device_list)
134 struct device *loop_device;
135 struct device *tmp_device;
138 /* handle the "first" type devices first */
139 list_for_each_entry_safe(loop_device, tmp_device, device_list, node) {
140 for (i = 0; first_list[i] != NULL; i++) {
141 if (strncmp(loop_device->path, first_list[i], strlen(first_list[i])) == 0) {
142 add_device(loop_device->path, loop_device->subsys);
143 list_del(&loop_device->node);
150 /* handle the devices we are allowed to, excluding the "last" type devices */
151 list_for_each_entry_safe(loop_device, tmp_device, device_list, node) {
153 for (i = 0; last_list[i] != NULL; i++) {
154 if (strncmp(loop_device->path, last_list[i], strlen(last_list[i])) == 0) {
162 add_device(loop_device->path, loop_device->subsys);
163 list_del(&loop_device->node);
167 /* handle the rest of the devices left over, if any */
168 list_for_each_entry_safe(loop_device, tmp_device, device_list, node) {
169 add_device(loop_device->path, loop_device->subsys);
170 list_del(&loop_device->node);
175 static int has_devt(const char *directory)
177 char filename[PATH_SIZE];
180 snprintf(filename, sizeof(filename), "%s/dev", directory);
181 filename[sizeof(filename)-1] = '\0';
183 if (stat(filename, &statbuf) == 0)
189 static void udev_scan_block(void)
191 char base[PATH_SIZE];
194 LIST_HEAD(device_list);
196 snprintf(base, sizeof(base), "%s/block", sysfs_path);
197 base[sizeof(base)-1] = '\0';
201 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
202 char dirname[PATH_SIZE];
204 struct dirent *dent2;
206 if (dent->d_name[0] == '.')
209 snprintf(dirname, sizeof(dirname), "%s/%s", base, dent->d_name);
210 dirname[sizeof(dirname)-1] = '\0';
211 if (has_devt(dirname))
212 device_list_insert(dirname, "block", &device_list);
216 /* look for partitions */
217 dir2 = opendir(dirname);
219 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
220 char dirname2[PATH_SIZE];
222 if (dent2->d_name[0] == '.')
225 snprintf(dirname2, sizeof(dirname2), "%s/%s", dirname, dent2->d_name);
226 dirname2[sizeof(dirname2)-1] = '\0';
228 if (has_devt(dirname2))
229 device_list_insert(dirname2, "block", &device_list);
236 exec_list(&device_list);
239 static void udev_scan_class(void)
241 char base[PATH_SIZE];
244 LIST_HEAD(device_list);
246 snprintf(base, sizeof(base), "%s/class", sysfs_path);
247 base[sizeof(base)-1] = '\0';
251 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
252 char dirname[PATH_SIZE];
254 struct dirent *dent2;
256 if (dent->d_name[0] == '.')
259 snprintf(dirname, sizeof(dirname), "%s/%s", base, dent->d_name);
260 dirname[sizeof(dirname)-1] = '\0';
262 dir2 = opendir(dirname);
264 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
265 char dirname2[PATH_SIZE];
267 if (dent2->d_name[0] == '.')
270 snprintf(dirname2, sizeof(dirname2), "%s/%s", dirname, dent2->d_name);
271 dirname2[sizeof(dirname2)-1] = '\0';
273 /* pass the net class as it is */
274 if (strcmp(dent->d_name, "net") == 0)
275 device_list_insert(dirname2, "net", &device_list);
276 else if (has_devt(dirname2))
277 device_list_insert(dirname2, dent->d_name, &device_list);
284 exec_list(&device_list);
287 static void asmlinkage sig_handler(int signum)
298 int main(int argc, char *argv[], char *envp[])
300 struct sigaction act;
304 /* set signal handlers */
305 memset(&act, 0x00, sizeof(act));
306 act.sa_handler = (void (*) (int))sig_handler;
307 sigemptyset (&act.sa_mask);
309 sigaction(SIGALRM, &act, NULL);
310 sigaction(SIGINT, &act, NULL);
311 sigaction(SIGTERM, &act, NULL);
313 /* trigger timeout to prevent hanging processes */
314 alarm(ALARM_TIMEOUT);
316 /* set environment for executed programs */
317 setenv("ACTION", "add", 1);
318 setenv("UDEV_START", "1", 1);