4 * Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com>
5 * Copyright (C) 2004 Kay Sievers <kay@vrfy.org>
7 * Quick and dirty way to populate a /dev with udev if your system
8 * does not have access to a shell. Based originally on a patch
10 * Harald Hoyer <harald@redhat.com>
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation version 2 of the License.
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 675 Mass Ave, Cambridge, MA 02139, USA.
38 #include <sys/types.h>
40 #include "libsysfs/sysfs/libsysfs.h"
41 #include "udev_libc_wrapper.h"
42 #include "udev_sysfs.h"
44 #include "udev_version.h"
46 #include "udev_utils.h"
47 #include "udev_rules.h"
50 static const char *udev_run_str;
51 static const char *udev_log_str;
52 static struct udev_rules rules;
55 void log_message(int priority, const char *format, ...)
59 if (priority > udev_log_priority)
62 va_start(args, format);
63 vsyslog(priority, format, args);
69 struct list_head node;
71 char subsys[NAME_SIZE];
74 /* sort files in lexical order */
75 static int device_list_insert(const char *path, char *subsystem, struct list_head *device_list)
77 struct device *loop_device;
78 struct device *new_device;
80 dbg("insert: '%s'\n", path);
82 list_for_each_entry(loop_device, device_list, node) {
83 if (strcmp(loop_device->path, path) > 0) {
88 new_device = malloc(sizeof(struct device));
89 if (new_device == NULL) {
94 strlcpy(new_device->path, path, sizeof(new_device->path));
95 strlcpy(new_device->subsys, subsystem, sizeof(new_device->subsys));
96 list_add_tail(&new_device->node, &loop_device->node);
97 dbg("add '%s' from subsys '%s'", new_device->path, new_device->subsys);
101 /* list of devices that we should run last due to any one of a number of reasons */
102 static char *last_list[] = {
103 "/block/dm", /* on here because dm wants to have the block devices around before it */
107 /* list of devices that we should run first due to any one of a number of reasons */
108 static char *first_list[] = {
109 "/class/mem", /* people tend to like their memory devices around first... */
113 static int add_device(const char *path, const char *subsystem)
116 struct sysfs_class_device *class_dev;
119 devpath = &path[strlen(sysfs_path)];
121 /* clear and set environment for next event */
123 setenv("ACTION", "add", 1);
124 setenv("DEVPATH", devpath, 1);
125 setenv("SUBSYSTEM", subsystem, 1);
126 setenv("UDEV_START", "1", 1);
128 setenv("UDEV_LOG", udev_log_str, 1);
130 setenv("UDEV_RUN", udev_run_str, 1);
131 dbg("exec: '%s' (%s)\n", devpath, path);
133 class_dev = sysfs_open_class_device_path(path);
134 if (class_dev == NULL) {
135 dbg("sysfs_open_class_device_path failed");
139 udev_init_device(&udev, devpath, subsystem, "add");
140 udev.devt = get_devt(class_dev);
141 if (!udev.devt && udev.type != DEV_NET) {
142 dbg("sysfs_open_class_device_path failed");
145 udev_rules_get_name(&rules, &udev, class_dev);
146 if (udev.ignore_device) {
147 dbg("device event will be ignored");
150 if (udev.name[0] == '\0') {
151 dbg("device node creation supressed");
155 udev_add_device(&udev, class_dev);
156 if (udev.devname[0] != '\0')
157 setenv("DEVNAME", udev.devname, 1);
159 if (udev_run && !list_empty(&udev.run_list)) {
160 struct name_entry *name_loop;
162 dbg("executing run list");
163 list_for_each_entry(name_loop, &udev.run_list, node) {
164 if (strncmp(name_loop->name, "socket:", strlen("socket:")) == 0)
165 pass_env_to_socket(&name_loop->name[strlen("socket:")], devpath, "add");
167 execute_program(name_loop->name, udev.subsystem, NULL, 0, NULL);
171 sysfs_close_class_device(class_dev);
172 udev_cleanup_device(&udev);
177 static void exec_list(struct list_head *device_list)
179 struct device *loop_device;
180 struct device *tmp_device;
183 /* handle the "first" type devices first */
184 list_for_each_entry_safe(loop_device, tmp_device, device_list, node) {
185 for (i = 0; first_list[i] != NULL; i++) {
186 if (strncmp(loop_device->path, first_list[i], strlen(first_list[i])) == 0) {
187 add_device(loop_device->path, loop_device->subsys);
188 list_del(&loop_device->node);
195 /* handle the devices we are allowed to, excluding the "last" type devices */
196 list_for_each_entry_safe(loop_device, tmp_device, device_list, node) {
198 for (i = 0; last_list[i] != NULL; i++) {
199 if (strncmp(loop_device->path, last_list[i], strlen(last_list[i])) == 0) {
207 add_device(loop_device->path, loop_device->subsys);
208 list_del(&loop_device->node);
212 /* handle the rest of the devices left over, if any */
213 list_for_each_entry_safe(loop_device, tmp_device, device_list, node) {
214 add_device(loop_device->path, loop_device->subsys);
215 list_del(&loop_device->node);
220 static int has_devt(const char *directory)
222 char filename[PATH_SIZE];
225 snprintf(filename, sizeof(filename), "%s/dev", directory);
226 filename[sizeof(filename)-1] = '\0';
228 if (stat(filename, &statbuf) == 0)
234 static void udev_scan_block(void)
236 char base[PATH_SIZE];
239 LIST_HEAD(device_list);
241 snprintf(base, sizeof(base), "%s/block", sysfs_path);
242 base[sizeof(base)-1] = '\0';
246 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
247 char dirname[PATH_SIZE];
249 struct dirent *dent2;
251 if (dent->d_name[0] == '.')
254 snprintf(dirname, sizeof(dirname), "%s/%s", base, dent->d_name);
255 dirname[sizeof(dirname)-1] = '\0';
256 if (has_devt(dirname))
257 device_list_insert(dirname, "block", &device_list);
261 /* look for partitions */
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 if (has_devt(dirname2))
274 device_list_insert(dirname2, "block", &device_list);
281 exec_list(&device_list);
284 static void udev_scan_class(void)
286 char base[PATH_SIZE];
289 LIST_HEAD(device_list);
291 snprintf(base, sizeof(base), "%s/class", sysfs_path);
292 base[sizeof(base)-1] = '\0';
296 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
297 char dirname[PATH_SIZE];
299 struct dirent *dent2;
301 if (dent->d_name[0] == '.')
304 snprintf(dirname, sizeof(dirname), "%s/%s", base, dent->d_name);
305 dirname[sizeof(dirname)-1] = '\0';
307 dir2 = opendir(dirname);
309 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
310 char dirname2[PATH_SIZE];
312 if (dent2->d_name[0] == '.')
315 snprintf(dirname2, sizeof(dirname2), "%s/%s", dirname, dent2->d_name);
316 dirname2[sizeof(dirname2)-1] = '\0';
318 /* pass the net class as it is */
319 if (strcmp(dent->d_name, "net") == 0)
320 device_list_insert(dirname2, "net", &device_list);
321 else if (has_devt(dirname2))
322 device_list_insert(dirname2, dent->d_name, &device_list);
329 exec_list(&device_list);
332 static void asmlinkage sig_handler(int signum)
343 int main(int argc, char *argv[], char *envp[])
345 struct sigaction act;
347 logging_init("udevstart");
349 dbg("version %s", UDEV_VERSION);
351 udev_run_str = getenv("UDEV_RUN");
352 udev_log_str = getenv("UDEV_LOG");
354 /* disable all logging if not explicitely requested */
355 if (udev_log_str == NULL)
356 udev_log_priority = 0;
358 /* set signal handlers */
359 memset(&act, 0x00, sizeof(act));
360 act.sa_handler = (void (*) (int))sig_handler;
361 sigemptyset (&act.sa_mask);
363 sigaction(SIGALRM, &act, NULL);
364 sigaction(SIGINT, &act, NULL);
365 sigaction(SIGTERM, &act, NULL);
367 /* trigger timeout to prevent hanging processes */
368 alarm(UDEV_ALARM_TIMEOUT);
370 udev_rules_init(&rules, 1);
375 udev_rules_close(&rules);