]>
git.ipfire.org Git - thirdparty/systemd.git/blob - udevstart.c
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"
41 #include "udev_version.h"
43 #include "udev_rules.h"
44 #include "udev_utils.h"
48 void log_message(int priority
, const char *format
, ...)
52 if (priority
> udev_log_priority
)
55 va_start(args
, format
);
56 vsyslog(priority
, format
, args
);
62 struct list_head node
;
64 char subsys
[NAME_SIZE
];
67 /* sort files in lexical order */
68 static int device_list_insert(const char *path
, char *subsystem
, struct list_head
*device_list
)
70 struct device
*loop_device
;
71 struct device
*new_device
;
73 dbg("insert: '%s'\n", path
);
75 list_for_each_entry(loop_device
, device_list
, node
) {
76 if (strcmp(loop_device
->path
, path
) > 0) {
81 new_device
= malloc(sizeof(struct device
));
82 if (new_device
== NULL
) {
87 strlcpy(new_device
->path
, path
, sizeof(new_device
->path
));
88 strlcpy(new_device
->subsys
, subsystem
, sizeof(new_device
->subsys
));
89 list_add_tail(&new_device
->node
, &loop_device
->node
);
90 dbg("add '%s' from subsys '%s'", new_device
->path
, new_device
->subsys
);
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
[] = {
102 "/class/mem", /* people tend to like their memory devices around first... */
106 static int add_device(const char *path
, const char *subsystem
)
109 struct sysfs_class_device
*class_dev
;
112 devpath
= &path
[strlen(sysfs_path
)];
114 /* set environment for callouts and dev.d/ */
115 setenv("DEVPATH", devpath
, 1);
116 setenv("SUBSYSTEM", subsystem
, 1);
118 dbg("exec: '%s' (%s)\n", devpath
, path
);
120 class_dev
= sysfs_open_class_device_path(path
);
121 if (class_dev
== NULL
) {
122 dbg ("sysfs_open_class_device_path failed");
126 udev_init_device(&udev
, devpath
, subsystem
, "add");
127 udev_add_device(&udev
, class_dev
);
129 if (udev
.devname
[0] != '\0')
130 setenv("DEVNAME", udev
.devname
, 1);
132 if (udev_run
&& !list_empty(&udev
.run_list
)) {
133 struct name_entry
*name_loop
;
135 dbg("executing run list");
136 list_for_each_entry(name_loop
, &udev
.run_list
, node
)
137 execute_command(name_loop
->name
, udev
.subsystem
);
140 /* run dev.d/ scripts if we created a node or changed a netif name */
141 if (udev_dev_d
&& udev
.devname
[0] != '\0')
142 udev_multiplex_directory(&udev
, DEVD_DIR
, DEVD_SUFFIX
);
144 sysfs_close_class_device(class_dev
);
145 udev_cleanup_device(&udev
);
150 static void exec_list(struct list_head
*device_list
)
152 struct device
*loop_device
;
153 struct device
*tmp_device
;
156 /* handle the "first" type devices first */
157 list_for_each_entry_safe(loop_device
, tmp_device
, device_list
, node
) {
158 for (i
= 0; first_list
[i
] != NULL
; i
++) {
159 if (strncmp(loop_device
->path
, first_list
[i
], strlen(first_list
[i
])) == 0) {
160 add_device(loop_device
->path
, loop_device
->subsys
);
161 list_del(&loop_device
->node
);
168 /* handle the devices we are allowed to, excluding the "last" type devices */
169 list_for_each_entry_safe(loop_device
, tmp_device
, device_list
, node
) {
171 for (i
= 0; last_list
[i
] != NULL
; i
++) {
172 if (strncmp(loop_device
->path
, last_list
[i
], strlen(last_list
[i
])) == 0) {
180 add_device(loop_device
->path
, loop_device
->subsys
);
181 list_del(&loop_device
->node
);
185 /* handle the rest of the devices left over, if any */
186 list_for_each_entry_safe(loop_device
, tmp_device
, device_list
, node
) {
187 add_device(loop_device
->path
, loop_device
->subsys
);
188 list_del(&loop_device
->node
);
193 static int has_devt(const char *directory
)
195 char filename
[PATH_SIZE
];
198 snprintf(filename
, sizeof(filename
), "%s/dev", directory
);
199 filename
[sizeof(filename
)-1] = '\0';
201 if (stat(filename
, &statbuf
) == 0)
207 static void udev_scan_block(void)
209 char base
[PATH_SIZE
];
212 LIST_HEAD(device_list
);
214 snprintf(base
, sizeof(base
), "%s/block", sysfs_path
);
215 base
[sizeof(base
)-1] = '\0';
219 for (dent
= readdir(dir
); dent
!= NULL
; dent
= readdir(dir
)) {
220 char dirname
[PATH_SIZE
];
222 struct dirent
*dent2
;
224 if (dent
->d_name
[0] == '.')
227 snprintf(dirname
, sizeof(dirname
), "%s/%s", base
, dent
->d_name
);
228 dirname
[sizeof(dirname
)-1] = '\0';
229 if (has_devt(dirname
))
230 device_list_insert(dirname
, "block", &device_list
);
234 /* look for partitions */
235 dir2
= opendir(dirname
);
237 for (dent2
= readdir(dir2
); dent2
!= NULL
; dent2
= readdir(dir2
)) {
238 char dirname2
[PATH_SIZE
];
240 if (dent2
->d_name
[0] == '.')
243 snprintf(dirname2
, sizeof(dirname2
), "%s/%s", dirname
, dent2
->d_name
);
244 dirname2
[sizeof(dirname2
)-1] = '\0';
246 if (has_devt(dirname2
))
247 device_list_insert(dirname2
, "block", &device_list
);
254 exec_list(&device_list
);
257 static void udev_scan_class(void)
259 char base
[PATH_SIZE
];
262 LIST_HEAD(device_list
);
264 snprintf(base
, sizeof(base
), "%s/class", sysfs_path
);
265 base
[sizeof(base
)-1] = '\0';
269 for (dent
= readdir(dir
); dent
!= NULL
; dent
= readdir(dir
)) {
270 char dirname
[PATH_SIZE
];
272 struct dirent
*dent2
;
274 if (dent
->d_name
[0] == '.')
277 snprintf(dirname
, sizeof(dirname
), "%s/%s", base
, dent
->d_name
);
278 dirname
[sizeof(dirname
)-1] = '\0';
280 dir2
= opendir(dirname
);
282 for (dent2
= readdir(dir2
); dent2
!= NULL
; dent2
= readdir(dir2
)) {
283 char dirname2
[PATH_SIZE
];
285 if (dent2
->d_name
[0] == '.')
288 snprintf(dirname2
, sizeof(dirname2
), "%s/%s", dirname
, dent2
->d_name
);
289 dirname2
[sizeof(dirname2
)-1] = '\0';
291 /* pass the net class as it is */
292 if (strcmp(dent
->d_name
, "net") == 0)
293 device_list_insert(dirname2
, "net", &device_list
);
294 else if (has_devt(dirname2
))
295 device_list_insert(dirname2
, dent
->d_name
, &device_list
);
302 exec_list(&device_list
);
305 static void asmlinkage
sig_handler(int signum
)
316 int main(int argc
, char *argv
[], char *envp
[])
318 struct sigaction act
;
320 logging_init("udev");
322 /* disable all logging if not explicitely requested */
323 if (getenv("UDEV_LOG") == NULL
)
324 udev_log_priority
= 0;
325 dbg("version %s", UDEV_VERSION
);
327 /* set signal handlers */
328 memset(&act
, 0x00, sizeof(act
));
329 act
.sa_handler
= (void (*) (int))sig_handler
;
330 sigemptyset (&act
.sa_mask
);
332 sigaction(SIGALRM
, &act
, NULL
);
333 sigaction(SIGINT
, &act
, NULL
);
334 sigaction(SIGTERM
, &act
, NULL
);
336 /* trigger timeout to prevent hanging processes */
337 alarm(ALARM_TIMEOUT
);
339 /* set environment for executed programs */
340 setenv("ACTION", "add", 1);
341 setenv("UDEV_START", "1", 1);