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.
39 #include <sys/types.h>
42 #include "udev_rules.h"
44 static const char *udev_run_str;
45 static const char *udev_log_str;
46 static struct udev_rules rules;
49 void log_message(int priority, const char *format, ...)
53 if (priority > udev_log_priority)
56 va_start(args, format);
57 vsyslog(priority, format, args);
63 struct list_head node;
67 /* sort files in lexical order */
68 static int device_list_insert(const char *path, struct list_head *device_list)
70 struct device *loop_device;
71 struct device *new_device;
72 const char *devpath = &path[strlen(sysfs_path)];
74 dbg("insert: '%s'\n", devpath);
76 list_for_each_entry(loop_device, device_list, node) {
77 if (strcmp(loop_device->path, devpath) > 0) {
82 new_device = malloc(sizeof(struct device));
83 if (new_device == NULL) {
88 strlcpy(new_device->path, devpath, sizeof(new_device->path));
89 list_add_tail(&new_device->node, &loop_device->node);
90 dbg("add '%s'" , new_device->path);
94 /* list of devices that we should run last due to any one of a number of reasons */
95 static char *last_list[] = {
96 "/block/dm", /* on here because dm wants to have the block devices around before it */
100 /* list of devices that we should run first due to any one of a number of reasons */
101 static char *first_list[] = {
107 static int add_device(const char *devpath)
109 struct sysfs_device *dev;
110 struct udevice *udev;
113 /* clear and set environment for next event */
115 setenv("ACTION", "add", 1);
116 setenv("UDEV_START", "1", 1);
118 setenv("UDEV_LOG", udev_log_str, 1);
120 setenv("UDEV_RUN", udev_run_str, 1);
122 dev = sysfs_device_get(devpath);
126 udev = udev_device_init();
130 /* override built-in sysfs device */
132 strcpy(udev->action, "add");
134 if (strcmp(udev->dev->subsystem, "net") != 0) {
135 udev->devt = udev_device_get_devt(udev);
136 if (major(udev->devt) == 0)
140 dbg("add '%s'", udev->dev->devpath);
141 setenv("DEVPATH", udev->dev->devpath, 1);
142 setenv("SUBSYSTEM", udev->dev->subsystem, 1);
144 udev_rules_get_name(&rules, udev);
145 if (udev->ignore_device) {
146 dbg("device event will be ignored");
149 if (udev->name[0] != '\0')
150 retval = udev_device_event(&rules, udev);
152 info("device node creation supressed");
154 if (retval == 0 && udev_run) {
155 struct name_entry *name_loop;
157 dbg("executing run list");
158 list_for_each_entry(name_loop, &udev->run_list, node) {
159 if (strncmp(name_loop->name, "socket:", strlen("socket:")) == 0)
160 pass_env_to_socket(&name_loop->name[strlen("socket:")], udev->dev->devpath, "add");
162 char program[PATH_SIZE];
164 strlcpy(program, name_loop->name, sizeof(program));
165 udev_rules_apply_format(udev, program, sizeof(program));
166 run_program(program, udev->dev->subsystem, NULL, 0, NULL, (udev_log_priority >= LOG_INFO));
172 udev_device_cleanup(udev);
176 static void exec_list(struct list_head *device_list)
178 struct device *loop_device;
179 struct device *tmp_device;
182 /* handle the "first" type devices first */
183 list_for_each_entry_safe(loop_device, tmp_device, device_list, node) {
184 for (i = 0; first_list[i] != NULL; i++) {
185 if (strncmp(loop_device->path, first_list[i], strlen(first_list[i])) == 0) {
186 add_device(loop_device->path);
187 list_del(&loop_device->node);
194 /* handle the devices we are allowed to, excluding the "last" type devices */
195 list_for_each_entry_safe(loop_device, tmp_device, device_list, node) {
197 for (i = 0; last_list[i] != NULL; i++) {
198 if (strncmp(loop_device->path, last_list[i], strlen(last_list[i])) == 0) {
206 add_device(loop_device->path);
207 list_del(&loop_device->node);
211 /* handle the rest of the devices left over, if any */
212 list_for_each_entry_safe(loop_device, tmp_device, device_list, node) {
213 add_device(loop_device->path);
214 list_del(&loop_device->node);
219 static int has_devt(const char *path)
221 char filename[PATH_SIZE];
224 snprintf(filename, sizeof(filename), "%s/dev", path);
225 filename[sizeof(filename)-1] = '\0';
227 if (stat(filename, &statbuf) == 0)
233 static void udev_scan_block(struct list_head *device_list)
235 char base[PATH_SIZE];
239 snprintf(base, sizeof(base), "%s/block", sysfs_path);
240 base[sizeof(base)-1] = '\0';
244 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
245 char dirname[PATH_SIZE];
247 struct dirent *dent2;
249 if (dent->d_name[0] == '.')
252 snprintf(dirname, sizeof(dirname), "%s/%s", base, dent->d_name);
253 dirname[sizeof(dirname)-1] = '\0';
254 if (has_devt(dirname))
255 device_list_insert(dirname, device_list);
259 /* look for partitions */
260 dir2 = opendir(dirname);
262 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
263 char dirname2[PATH_SIZE];
265 if (dent2->d_name[0] == '.')
268 snprintf(dirname2, sizeof(dirname2), "%s/%s", dirname, dent2->d_name);
269 dirname2[sizeof(dirname2)-1] = '\0';
271 if (has_devt(dirname2))
272 device_list_insert(dirname2, device_list);
281 static void udev_scan_class(struct list_head *device_list)
283 char base[PATH_SIZE];
287 snprintf(base, sizeof(base), "%s/class", sysfs_path);
288 base[sizeof(base)-1] = '\0';
292 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
293 char dirname[PATH_SIZE];
295 struct dirent *dent2;
297 if (dent->d_name[0] == '.')
300 snprintf(dirname, sizeof(dirname), "%s/%s", base, dent->d_name);
301 dirname[sizeof(dirname)-1] = '\0';
303 dir2 = opendir(dirname);
305 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
306 char dirname2[PATH_SIZE];
308 if (dent2->d_name[0] == '.')
311 snprintf(dirname2, sizeof(dirname2), "%s/%s", dirname, dent2->d_name);
312 dirname2[sizeof(dirname2)-1] = '\0';
314 if (has_devt(dirname2) || strcmp(dent->d_name, "net") == 0)
315 device_list_insert(dirname2, device_list);
324 static void asmlinkage sig_handler(int signum)
335 int main(int argc, char *argv[], char *envp[])
337 LIST_HEAD(device_list);
338 struct sigaction act;
340 logging_init("udevstart");
342 dbg("version %s", UDEV_VERSION);
344 udev_run_str = getenv("UDEV_RUN");
345 udev_log_str = getenv("UDEV_LOG");
347 /* disable all logging if not explicitely requested */
348 if (udev_log_str == NULL)
349 udev_log_priority = 0;
351 /* set signal handlers */
352 memset(&act, 0x00, sizeof(act));
353 act.sa_handler = (void (*) (int))sig_handler;
354 sigemptyset (&act.sa_mask);
356 sigaction(SIGALRM, &act, NULL);
357 sigaction(SIGINT, &act, NULL);
358 sigaction(SIGTERM, &act, NULL);
360 /* trigger timeout to prevent hanging processes */
361 alarm(UDEV_ALARM_TIMEOUT);
364 udev_rules_init(&rules, 1);
366 udev_scan_class(&device_list);
367 udev_scan_block(&device_list);
368 exec_list(&device_list);
370 udev_rules_cleanup(&rules);