]>
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.
33 #include <sys/types.h>
42 #define MAX_PATHLEN 1024
43 #define SYSBLOCK "/sys/block"
44 #define SYSCLASS "/sys/class"
47 struct list_head list
;
48 char path
[MAX_PATHLEN
];
49 char subsys
[MAX_PATHLEN
];
52 /* sort files in lexical order */
53 static int device_list_insert(char *path
, char *subsystem
, struct list_head
*device_list
)
55 struct device
*loop_device
;
56 struct device
*new_device
;
58 list_for_each_entry(loop_device
, device_list
, list
) {
59 if (strcmp(loop_device
->path
, path
) > 0) {
64 new_device
= malloc(sizeof(struct device
));
65 if (new_device
== NULL
) {
70 strfieldcpy(new_device
->path
, path
);
71 strfieldcpy(new_device
->subsys
, subsystem
);
72 list_add_tail(&new_device
->list
, &loop_device
->list
);
73 dbg("add '%s' from subsys '%s'", new_device
->path
, new_device
->subsys
);
77 /* list of devices that we should run last due to any one of a number of reasons */
78 static char *last_list
[] = {
79 "/block/dm", /* on here because dm wants to have the block devices around before it */
83 /* list of devices that we should run first due to any one of a number of reasons */
84 static char *first_list
[] = {
85 "/class/mem", /* people tend to like their memory devices around first... */
89 static void add_device(char *path
, char *subsys
, int fake
)
93 /* fake argument vector and environment for callouts and dev.d/ */
99 setenv("DEVPATH", path
, 1);
100 setenv("ACTION", "add", 1);
101 udev_add_device(path
, subsys
, fake
);
104 static void exec_list(struct list_head
*device_list
)
106 struct device
*loop_device
;
107 struct device
*tmp_device
;
110 /* handle the "first" type devices first */
111 list_for_each_entry_safe(loop_device
, tmp_device
, device_list
, list
) {
112 for (i
=0; first_list
[i
] != NULL
; i
++) {
113 if (strncmp(loop_device
->path
, first_list
[i
], strlen(first_list
[i
])) == 0) {
114 add_device(loop_device
->path
, loop_device
->subsys
, NOFAKE
);
115 list_del(&loop_device
->list
);
122 /* handle the devices we are allowed to, excluding the "last" type devices */
123 list_for_each_entry_safe(loop_device
, tmp_device
, device_list
, list
) {
125 for (i
=0; last_list
[i
] != NULL
; i
++) {
126 if (strncmp(loop_device
->path
, last_list
[i
], strlen(last_list
[i
])) == 0) {
134 add_device(loop_device
->path
, loop_device
->subsys
, NOFAKE
);
135 list_del(&loop_device
->list
);
139 /* handle the rest of the devices left over, if any */
140 list_for_each_entry_safe(loop_device
, tmp_device
, device_list
, list
) {
141 add_device(loop_device
->path
, loop_device
->subsys
, NOFAKE
);
142 list_del(&loop_device
->list
);
147 static void udev_scan_block(void)
151 LIST_HEAD(device_list
);
153 dir
= opendir(SYSBLOCK
);
155 for (dent
= readdir(dir
); dent
!= NULL
; dent
= readdir(dir
)) {
156 char dirname
[MAX_PATHLEN
];
158 struct dirent
*dent2
;
160 if ((strcmp(dent
->d_name
, ".") == 0) ||
161 (strcmp(dent
->d_name
, "..") == 0))
164 snprintf(dirname
, MAX_PATHLEN
, "/block/%s", dent
->d_name
);
165 dirname
[MAX_PATHLEN
-1] = '\0';
166 device_list_insert(dirname
, "block", &device_list
);
168 snprintf(dirname
, MAX_PATHLEN
, "%s/%s", SYSBLOCK
, dent
->d_name
);
169 dir2
= opendir(dirname
);
171 for (dent2
= readdir(dir2
); dent2
!= NULL
; dent2
= readdir(dir2
)) {
172 char dirname2
[MAX_PATHLEN
];
174 struct dirent
*dent3
;
176 if ((strcmp(dent2
->d_name
, ".") == 0) ||
177 (strcmp(dent2
->d_name
, "..") == 0))
180 snprintf(dirname2
, MAX_PATHLEN
, "%s/%s", dirname
, dent2
->d_name
);
181 dirname2
[MAX_PATHLEN
-1] = '\0';
183 dir3
= opendir(dirname2
);
185 for (dent3
= readdir(dir3
); dent3
!= NULL
; dent3
= readdir(dir3
)) {
186 char filename
[MAX_PATHLEN
];
188 if (strcmp(dent3
->d_name
, "dev") == 0) {
189 snprintf(filename
, MAX_PATHLEN
, "/block/%s/%s",
190 dent
->d_name
, dent2
->d_name
);
191 filename
[MAX_PATHLEN
-1] = '\0';
192 device_list_insert(filename
, "block", &device_list
);
204 exec_list(&device_list
);
207 static void udev_scan_class(void)
211 LIST_HEAD(device_list
);
213 dir
= opendir(SYSCLASS
);
215 for (dent
= readdir(dir
); dent
!= NULL
; dent
= readdir(dir
)) {
216 char dirname
[MAX_PATHLEN
];
218 struct dirent
*dent2
;
220 if ((strcmp(dent
->d_name
, ".") == 0) ||
221 (strcmp(dent
->d_name
, "..") == 0))
224 snprintf(dirname
, MAX_PATHLEN
, "%s/%s", SYSCLASS
, dent
->d_name
);
225 dirname
[MAX_PATHLEN
-1] = '\0';
226 dir2
= opendir(dirname
);
228 for (dent2
= readdir(dir2
); dent2
!= NULL
; dent2
= readdir(dir2
)) {
229 char dirname2
[MAX_PATHLEN
];
231 struct dirent
*dent3
;
233 if ((strcmp(dent2
->d_name
, ".") == 0) ||
234 (strcmp(dent2
->d_name
, "..") == 0))
237 /* pass the net class as it is */
238 if (strcmp(dent
->d_name
, "net") == 0) {
239 snprintf(dirname2
, MAX_PATHLEN
, "/class/net/%s", dent2
->d_name
);
240 device_list_insert(dirname2
, "net", &device_list
);
244 snprintf(dirname2
, MAX_PATHLEN
, "%s/%s", dirname
, dent2
->d_name
);
245 dirname2
[MAX_PATHLEN
-1] = '\0';
246 dir3
= opendir(dirname2
);
248 for (dent3
= readdir(dir3
); dent3
!= NULL
; dent3
= readdir(dir3
)) {
249 char filename
[MAX_PATHLEN
];
251 /* pass devices with a "dev" file */
252 if (strcmp(dent3
->d_name
, "dev") == 0) {
253 snprintf(filename
, MAX_PATHLEN
, "/class/%s/%s",
254 dent
->d_name
, dent2
->d_name
);
255 filename
[MAX_PATHLEN
-1] = '\0';
256 device_list_insert(filename
, dent
->d_name
, &device_list
);
269 exec_list(&device_list
);