]> git.ipfire.org Git - thirdparty/systemd.git/blame - udev_start.c
[PATCH] add a bunch of busses to the list of what to wait for
[thirdparty/systemd.git] / udev_start.c
CommitLineData
82b9a637
GKH
1/*
2 * udevstart.c
3 *
4 * Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com>
5 *
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>
9 *
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.
13 *
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.
18 *
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.
22 *
23 */
24
25#include <stdlib.h>
0e306d08 26#include <stddef.h>
82b9a637
GKH
27#include <string.h>
28#include <stdio.h>
29#include <errno.h>
30#include <ctype.h>
82b9a637
GKH
31#include <dirent.h>
32#include <sys/wait.h>
f27125f9 33#include <sys/types.h>
34#include <unistd.h>
82b9a637 35
7a947ce5 36#include "libsysfs/sysfs/libsysfs.h"
82b9a637 37#include "logging.h"
9b28a52a 38#include "udev_lib.h"
0e306d08 39#include "list.h"
eb6c7cd0 40#include "udev.h"
82b9a637
GKH
41
42
e4dc0e11
KS
43#define MAX_PATHLEN 1024
44#define SYSBLOCK "/sys/block"
45#define SYSCLASS "/sys/class"
82b9a637 46
0e306d08
GKH
47struct device {
48 struct list_head list;
49 char path[MAX_PATHLEN];
50 char subsys[MAX_PATHLEN];
51};
52
53/* sort files in lexical order */
54static int device_list_insert(char *path, char *subsystem, struct list_head *device_list)
55{
56 struct device *loop_device;
57 struct device *new_device;
58
59 list_for_each_entry(loop_device, device_list, list) {
60 if (strcmp(loop_device->path, path) > 0) {
61 break;
62 }
63 }
64
65 new_device = malloc(sizeof(struct device));
66 if (new_device == NULL) {
67 dbg("error malloc");
68 return -ENOMEM;
69 }
70
71 strfieldcpy(new_device->path, path);
72 strfieldcpy(new_device->subsys, subsystem);
73 list_add_tail(&new_device->list, &loop_device->list);
aee380b6 74 dbg("add '%s' from subsys '%s'", new_device->path, new_device->subsys);
0e306d08
GKH
75 return 0;
76}
77
0e306d08
GKH
78/* list of devices that we should run last due to any one of a number of reasons */
79static char *last_list[] = {
80 "/block/dm", /* on here because dm wants to have the block devices around before it */
81 NULL,
82};
83
70f630f6
GKH
84/* list of devices that we should run first due to any one of a number of reasons */
85static char *first_list[] = {
86 "/class/mem", /* people tend to like their memory devices around first... */
87 NULL,
88};
89
7a947ce5 90static int add_device(char *devpath, char *subsystem)
f608f8ac 91{
7a947ce5
KS
92 struct udevice udev;
93 char path[SYSFS_PATH_MAX];
94 struct sysfs_class_device *class_dev;
f608f8ac 95
af4b05d4 96 /* set environment for callouts and dev.d/ */
7a947ce5 97 setenv("DEVPATH", devpath, 1);
af4b05d4 98 setenv("SUBSYSTEM", subsystem, 1);
7a947ce5 99
8673dcb8
KS
100 snprintf(path, SYSFS_PATH_MAX, "%s%s", sysfs_path, devpath);
101 path[SYSFS_PATH_MAX-1] = '\0';
7a947ce5
KS
102 class_dev = sysfs_open_class_device_path(path);
103 if (class_dev == NULL) {
104 dbg ("sysfs_open_class_device_path failed");
105 return -ENODEV;
106 }
107
c6478ec1 108 udev_set_values(&udev, devpath, subsystem, "add");
5d24c6ca 109 udev_add_device(&udev, class_dev);
7a947ce5 110
8673dcb8
KS
111 /* run dev.d/ scripts if we created a node or changed a netif name */
112 if (udev.devname[0] != '\0') {
113 setenv("DEVNAME", udev.devname, 1);
114 dev_d_execute(&udev, DEVD_DIR, DEVD_SUFFIX);
115 }
5d24c6ca
KS
116
117 sysfs_close_class_device(class_dev);
118
119 return 0;
f608f8ac
KS
120}
121
0e306d08
GKH
122static void exec_list(struct list_head *device_list)
123{
124 struct device *loop_device;
125 struct device *tmp_device;
70f630f6
GKH
126 int i;
127
128 /* handle the "first" type devices first */
129 list_for_each_entry_safe(loop_device, tmp_device, device_list, list) {
130 for (i=0; first_list[i] != NULL; i++) {
131 if (strncmp(loop_device->path, first_list[i], strlen(first_list[i])) == 0) {
7a947ce5 132 add_device(loop_device->path, loop_device->subsys);
70f630f6
GKH
133 list_del(&loop_device->list);
134 free(loop_device);
135 break;
136 }
137 }
138 }
0e306d08
GKH
139
140 /* handle the devices we are allowed to, excluding the "last" type devices */
141 list_for_each_entry_safe(loop_device, tmp_device, device_list, list) {
142 int found = 0;
0e306d08
GKH
143 for (i=0; last_list[i] != NULL; i++) {
144 if (strncmp(loop_device->path, last_list[i], strlen(last_list[i])) == 0) {
145 found = 1;
146 break;
147 }
148 }
149 if (found)
150 continue;
151
7a947ce5 152 add_device(loop_device->path, loop_device->subsys);
0e306d08
GKH
153 list_del(&loop_device->list);
154 free(loop_device);
155 }
156
157 /* handle the rest of the devices left over, if any */
158 list_for_each_entry_safe(loop_device, tmp_device, device_list, list) {
7a947ce5 159 add_device(loop_device->path, loop_device->subsys);
0e306d08
GKH
160 list_del(&loop_device->list);
161 free(loop_device);
162 }
163}
164
165static void udev_scan_block(void)
82b9a637 166{
e4dc0e11
KS
167 DIR *dir;
168 struct dirent *dent;
0e306d08 169 LIST_HEAD(device_list);
82b9a637 170
0e306d08 171 dir = opendir(SYSBLOCK);
e4dc0e11
KS
172 if (dir != NULL) {
173 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
174 char dirname[MAX_PATHLEN];
175 DIR *dir2;
176 struct dirent *dent2;
177
178 if ((strcmp(dent->d_name, ".") == 0) ||
179 (strcmp(dent->d_name, "..") == 0))
82b9a637
GKH
180 continue;
181
0e306d08
GKH
182 snprintf(dirname, MAX_PATHLEN, "/block/%s", dent->d_name);
183 dirname[MAX_PATHLEN-1] = '\0';
184 device_list_insert(dirname, "block", &device_list);
185
186 snprintf(dirname, MAX_PATHLEN, "%s/%s", SYSBLOCK, dent->d_name);
82b9a637 187 dir2 = opendir(dirname);
e4dc0e11
KS
188 if (dir2 != NULL) {
189 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
0e306d08 190 char dirname2[MAX_PATHLEN];
e4dc0e11
KS
191 DIR *dir3;
192 struct dirent *dent3;
82b9a637
GKH
193
194 if ((strcmp(dent2->d_name, ".") == 0) ||
195 (strcmp(dent2->d_name, "..") == 0))
196 continue;
197
198 snprintf(dirname2, MAX_PATHLEN, "%s/%s", dirname, dent2->d_name);
e4dc0e11 199 dirname2[MAX_PATHLEN-1] = '\0';
82b9a637
GKH
200
201 dir3 = opendir(dirname2);
e4dc0e11
KS
202 if (dir3 != NULL) {
203 for (dent3 = readdir(dir3); dent3 != NULL; dent3 = readdir(dir3)) {
82b9a637
GKH
204 char filename[MAX_PATHLEN];
205
206 if (strcmp(dent3->d_name, "dev") == 0) {
0e306d08 207 snprintf(filename, MAX_PATHLEN, "/block/%s/%s",
e4dc0e11
KS
208 dent->d_name, dent2->d_name);
209 filename[MAX_PATHLEN-1] = '\0';
0e306d08 210 device_list_insert(filename, "block", &device_list);
82b9a637
GKH
211 }
212 }
e13fa599 213 closedir(dir3);
82b9a637
GKH
214 }
215 }
e13fa599 216 closedir(dir2);
82b9a637
GKH
217 }
218 }
e13fa599 219 closedir(dir);
82b9a637
GKH
220 }
221
0e306d08
GKH
222 exec_list(&device_list);
223}
224
225static void udev_scan_class(void)
226{
227 DIR *dir;
228 struct dirent *dent;
229 LIST_HEAD(device_list);
230
231 dir = opendir(SYSCLASS);
e4dc0e11
KS
232 if (dir != NULL) {
233 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
234 char dirname[MAX_PATHLEN];
235 DIR *dir2;
236 struct dirent *dent2;
237
238 if ((strcmp(dent->d_name, ".") == 0) ||
239 (strcmp(dent->d_name, "..") == 0))
82b9a637
GKH
240 continue;
241
0e306d08 242 snprintf(dirname, MAX_PATHLEN, "%s/%s", SYSCLASS, dent->d_name);
462be028 243 dirname[MAX_PATHLEN-1] = '\0';
82b9a637 244 dir2 = opendir(dirname);
e4dc0e11
KS
245 if (dir2 != NULL) {
246 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
a551c7b0 247 char dirname2[MAX_PATHLEN];
e4dc0e11
KS
248 DIR *dir3;
249 struct dirent *dent3;
82b9a637 250
e4dc0e11
KS
251 if ((strcmp(dent2->d_name, ".") == 0) ||
252 (strcmp(dent2->d_name, "..") == 0))
82b9a637
GKH
253 continue;
254
aee380b6
KS
255 /* pass the net class as it is */
256 if (strcmp(dent->d_name, "net") == 0) {
257 snprintf(dirname2, MAX_PATHLEN, "/class/net/%s", dent2->d_name);
258 device_list_insert(dirname2, "net", &device_list);
259 continue;
260 }
261
82b9a637 262 snprintf(dirname2, MAX_PATHLEN, "%s/%s", dirname, dent2->d_name);
e4dc0e11 263 dirname2[MAX_PATHLEN-1] = '\0';
82b9a637 264 dir3 = opendir(dirname2);
e4dc0e11
KS
265 if (dir3 != NULL) {
266 for (dent3 = readdir(dir3); dent3 != NULL; dent3 = readdir(dir3)) {
267 char filename[MAX_PATHLEN];
82b9a637 268
aee380b6 269 /* pass devices with a "dev" file */
82b9a637 270 if (strcmp(dent3->d_name, "dev") == 0) {
0e306d08 271 snprintf(filename, MAX_PATHLEN, "/class/%s/%s",
e4dc0e11
KS
272 dent->d_name, dent2->d_name);
273 filename[MAX_PATHLEN-1] = '\0';
0e306d08 274 device_list_insert(filename, dent->d_name, &device_list);
aee380b6 275 break;
82b9a637
GKH
276 }
277 }
e13fa599 278 closedir(dir3);
82b9a637
GKH
279 }
280 }
e13fa599 281 closedir(dir2);
82b9a637
GKH
282 }
283 }
e13fa599 284 closedir(dir);
82b9a637 285 }
0e306d08
GKH
286
287 exec_list(&device_list);
82b9a637
GKH
288}
289
eb6c7cd0 290int udev_start(void)
82b9a637 291{
af4b05d4
KS
292 /* set environment for callouts and dev.d/ */
293 setenv("ACTION", "add", 1);
294 setenv("UDEVSTART", "1", 1);
295
0e306d08
GKH
296 udev_scan_class();
297 udev_scan_block();
af4b05d4 298
3f20eac0 299 return 0;
82b9a637 300}