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"
41 #include "udev_utils.h"
46 void log_message(int level, const char *format, ...)
52 struct list_head list;
53 char path[DEVPATH_SIZE];
54 char subsys[SUBSYSTEM_SIZE];
57 /* sort files in lexical order */
58 static int device_list_insert(const char *path, char *subsystem, struct list_head *device_list)
60 struct device *loop_device;
61 struct device *new_device;
63 dbg("insert: '%s'\n", path);
65 list_for_each_entry(loop_device, device_list, list) {
66 if (strcmp(loop_device->path, path) > 0) {
71 new_device = malloc(sizeof(struct device));
72 if (new_device == NULL) {
77 strfieldcpy(new_device->path, path);
78 strfieldcpy(new_device->subsys, subsystem);
79 list_add_tail(&new_device->list, &loop_device->list);
80 dbg("add '%s' from subsys '%s'", new_device->path, new_device->subsys);
84 /* list of devices that we should run last due to any one of a number of reasons */
85 static char *last_list[] = {
86 "/block/dm", /* on here because dm wants to have the block devices around before it */
90 /* list of devices that we should run first due to any one of a number of reasons */
91 static char *first_list[] = {
92 "/class/mem", /* people tend to like their memory devices around first... */
96 static int add_device(const char *path, const char *subsystem)
99 struct sysfs_class_device *class_dev;
102 devpath = &path[strlen(sysfs_path)];
104 /* set environment for callouts and dev.d/ */
105 setenv("DEVPATH", devpath, 1);
106 setenv("SUBSYSTEM", subsystem, 1);
108 dbg("exec: '%s' (%s)\n", devpath, path);
110 class_dev = sysfs_open_class_device_path(path);
111 if (class_dev == NULL) {
112 dbg ("sysfs_open_class_device_path failed");
116 udev_init_device(&udev, devpath, subsystem);
117 udev_add_device(&udev, class_dev);
119 /* run dev.d/ scripts if we created a node or changed a netif name */
120 if (udev_dev_d && udev.devname[0] != '\0') {
121 setenv("DEVNAME", udev.devname, 1);
122 udev_multiplex_directory(&udev, DEVD_DIR, DEVD_SUFFIX);
125 sysfs_close_class_device(class_dev);
126 udev_cleanup_device(&udev);
131 static void exec_list(struct list_head *device_list)
133 struct device *loop_device;
134 struct device *tmp_device;
137 /* handle the "first" type devices first */
138 list_for_each_entry_safe(loop_device, tmp_device, device_list, list) {
139 for (i = 0; first_list[i] != NULL; i++) {
140 if (strncmp(loop_device->path, first_list[i], strlen(first_list[i])) == 0) {
141 add_device(loop_device->path, loop_device->subsys);
142 list_del(&loop_device->list);
149 /* handle the devices we are allowed to, excluding the "last" type devices */
150 list_for_each_entry_safe(loop_device, tmp_device, device_list, list) {
152 for (i = 0; last_list[i] != NULL; i++) {
153 if (strncmp(loop_device->path, last_list[i], strlen(last_list[i])) == 0) {
161 add_device(loop_device->path, loop_device->subsys);
162 list_del(&loop_device->list);
166 /* handle the rest of the devices left over, if any */
167 list_for_each_entry_safe(loop_device, tmp_device, device_list, list) {
168 add_device(loop_device->path, loop_device->subsys);
169 list_del(&loop_device->list);
174 static int has_devt(const char *directory)
176 char filename[NAME_SIZE];
179 snprintf(filename, NAME_SIZE, "%s/dev", directory);
180 filename[NAME_SIZE-1] = '\0';
182 if (stat(filename, &statbuf) == 0)
188 static void udev_scan_block(void)
190 char base[NAME_SIZE];
193 LIST_HEAD(device_list);
195 snprintf(base, DEVPATH_SIZE, "%s/block", sysfs_path);
196 base[DEVPATH_SIZE-1] = '\0';
200 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
201 char dirname[DEVPATH_SIZE];
203 struct dirent *dent2;
205 if (dent->d_name[0] == '.')
208 snprintf(dirname, NAME_SIZE, "%s/%s", base, dent->d_name);
209 dirname[NAME_SIZE-1] = '\0';
210 if (has_devt(dirname))
211 device_list_insert(dirname, "block", &device_list);
215 /* look for partitions */
216 dir2 = opendir(dirname);
218 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
219 char dirname2[DEVPATH_SIZE];
221 if (dent2->d_name[0] == '.')
224 snprintf(dirname2, DEVPATH_SIZE, "%s/%s", dirname, dent2->d_name);
225 dirname2[DEVPATH_SIZE-1] = '\0';
227 if (has_devt(dirname2))
228 device_list_insert(dirname2, "block", &device_list);
235 exec_list(&device_list);
238 static void udev_scan_class(void)
240 char base[DEVPATH_SIZE];
243 LIST_HEAD(device_list);
245 snprintf(base, DEVPATH_SIZE, "%s/class", sysfs_path);
246 base[DEVPATH_SIZE-1] = '\0';
250 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
251 char dirname[DEVPATH_SIZE];
253 struct dirent *dent2;
255 if (dent->d_name[0] == '.')
258 snprintf(dirname, DEVPATH_SIZE, "%s/%s", base, dent->d_name);
259 dirname[DEVPATH_SIZE-1] = '\0';
261 dir2 = opendir(dirname);
263 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
264 char dirname2[DEVPATH_SIZE];
266 if (dent2->d_name[0] == '.')
269 snprintf(dirname2, DEVPATH_SIZE, "%s/%s", dirname, dent2->d_name);
270 dirname2[DEVPATH_SIZE-1] = '\0';
272 /* pass the net class as it is */
273 if (strcmp(dent->d_name, "net") == 0)
274 device_list_insert(dirname2, "net", &device_list);
275 else if (has_devt(dirname2))
276 device_list_insert(dirname2, dent->d_name, &device_list);
283 exec_list(&device_list);
286 static void asmlinkage sig_handler(int signum)
297 int main(int argc, char *argv[], char *envp[])
299 struct sigaction act;
303 /* set signal handlers */
304 memset(&act, 0x00, sizeof(act));
305 act.sa_handler = (void (*) (int))sig_handler;
306 sigemptyset (&act.sa_mask);
308 sigaction(SIGALRM, &act, NULL);
309 sigaction(SIGINT, &act, NULL);
310 sigaction(SIGTERM, &act, NULL);
312 /* trigger timeout to prevent hanging processes */
313 alarm(ALARM_TIMEOUT);
315 /* set environment for executed programs */
316 setenv("ACTION", "add", 1);
317 setenv("UDEV_START", "1", 1);