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