]> git.ipfire.org Git - thirdparty/systemd.git/blame - udevstart.c
[PATCH] cleanup netif handling and netif-dev.d/ events
[thirdparty/systemd.git] / udevstart.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
KS
95 char *argv[3];
96
97 /* fake argument vector and environment for callouts and dev.d/ */
98 argv[0] = "udev";
7a947ce5 99 argv[1] = subsystem;
f608f8ac
KS
100 argv[2] = NULL;
101
102 main_argv = argv;
7a947ce5 103 setenv("DEVPATH", devpath, 1);
f608f8ac 104 setenv("ACTION", "add", 1);
7a947ce5 105
5d24c6ca 106 snprintf(path, SYSFS_PATH_MAX-1, "%s%s", sysfs_path, devpath);
7a947ce5
KS
107 class_dev = sysfs_open_class_device_path(path);
108 if (class_dev == NULL) {
109 dbg ("sysfs_open_class_device_path failed");
110 return -ENODEV;
111 }
112
113 udev_set_values(&udev, devpath, subsystem);
5d24c6ca 114 udev_add_device(&udev, class_dev);
7a947ce5 115
5d24c6ca
KS
116 /* run scripts */
117 dev_d_execute(&udev);
118
119 sysfs_close_class_device(class_dev);
120
121 return 0;
f608f8ac
KS
122}
123
0e306d08
GKH
124static void exec_list(struct list_head *device_list)
125{
126 struct device *loop_device;
127 struct device *tmp_device;
70f630f6
GKH
128 int i;
129
130 /* handle the "first" type devices first */
131 list_for_each_entry_safe(loop_device, tmp_device, device_list, list) {
132 for (i=0; first_list[i] != NULL; i++) {
133 if (strncmp(loop_device->path, first_list[i], strlen(first_list[i])) == 0) {
7a947ce5 134 add_device(loop_device->path, loop_device->subsys);
70f630f6
GKH
135 list_del(&loop_device->list);
136 free(loop_device);
137 break;
138 }
139 }
140 }
0e306d08
GKH
141
142 /* handle the devices we are allowed to, excluding the "last" type devices */
143 list_for_each_entry_safe(loop_device, tmp_device, device_list, list) {
144 int found = 0;
0e306d08
GKH
145 for (i=0; last_list[i] != NULL; i++) {
146 if (strncmp(loop_device->path, last_list[i], strlen(last_list[i])) == 0) {
147 found = 1;
148 break;
149 }
150 }
151 if (found)
152 continue;
153
7a947ce5 154 add_device(loop_device->path, loop_device->subsys);
0e306d08
GKH
155 list_del(&loop_device->list);
156 free(loop_device);
157 }
158
159 /* handle the rest of the devices left over, if any */
160 list_for_each_entry_safe(loop_device, tmp_device, device_list, list) {
7a947ce5 161 add_device(loop_device->path, loop_device->subsys);
0e306d08
GKH
162 list_del(&loop_device->list);
163 free(loop_device);
164 }
165}
166
167static void udev_scan_block(void)
82b9a637 168{
e4dc0e11
KS
169 DIR *dir;
170 struct dirent *dent;
0e306d08 171 LIST_HEAD(device_list);
82b9a637 172
0e306d08 173 dir = opendir(SYSBLOCK);
e4dc0e11
KS
174 if (dir != NULL) {
175 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
176 char dirname[MAX_PATHLEN];
177 DIR *dir2;
178 struct dirent *dent2;
179
180 if ((strcmp(dent->d_name, ".") == 0) ||
181 (strcmp(dent->d_name, "..") == 0))
82b9a637
GKH
182 continue;
183
0e306d08
GKH
184 snprintf(dirname, MAX_PATHLEN, "/block/%s", dent->d_name);
185 dirname[MAX_PATHLEN-1] = '\0';
186 device_list_insert(dirname, "block", &device_list);
187
188 snprintf(dirname, MAX_PATHLEN, "%s/%s", SYSBLOCK, dent->d_name);
82b9a637 189 dir2 = opendir(dirname);
e4dc0e11
KS
190 if (dir2 != NULL) {
191 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
0e306d08 192 char dirname2[MAX_PATHLEN];
e4dc0e11
KS
193 DIR *dir3;
194 struct dirent *dent3;
82b9a637
GKH
195
196 if ((strcmp(dent2->d_name, ".") == 0) ||
197 (strcmp(dent2->d_name, "..") == 0))
198 continue;
199
200 snprintf(dirname2, MAX_PATHLEN, "%s/%s", dirname, dent2->d_name);
e4dc0e11 201 dirname2[MAX_PATHLEN-1] = '\0';
82b9a637
GKH
202
203 dir3 = opendir(dirname2);
e4dc0e11
KS
204 if (dir3 != NULL) {
205 for (dent3 = readdir(dir3); dent3 != NULL; dent3 = readdir(dir3)) {
82b9a637
GKH
206 char filename[MAX_PATHLEN];
207
208 if (strcmp(dent3->d_name, "dev") == 0) {
0e306d08 209 snprintf(filename, MAX_PATHLEN, "/block/%s/%s",
e4dc0e11
KS
210 dent->d_name, dent2->d_name);
211 filename[MAX_PATHLEN-1] = '\0';
0e306d08 212 device_list_insert(filename, "block", &device_list);
82b9a637
GKH
213 }
214 }
e13fa599 215 closedir(dir3);
82b9a637
GKH
216 }
217 }
e13fa599 218 closedir(dir2);
82b9a637
GKH
219 }
220 }
e13fa599 221 closedir(dir);
82b9a637
GKH
222 }
223
0e306d08
GKH
224 exec_list(&device_list);
225}
226
227static void udev_scan_class(void)
228{
229 DIR *dir;
230 struct dirent *dent;
231 LIST_HEAD(device_list);
232
233 dir = opendir(SYSCLASS);
e4dc0e11
KS
234 if (dir != NULL) {
235 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
236 char dirname[MAX_PATHLEN];
237 DIR *dir2;
238 struct dirent *dent2;
239
240 if ((strcmp(dent->d_name, ".") == 0) ||
241 (strcmp(dent->d_name, "..") == 0))
82b9a637
GKH
242 continue;
243
0e306d08 244 snprintf(dirname, MAX_PATHLEN, "%s/%s", SYSCLASS, dent->d_name);
462be028 245 dirname[MAX_PATHLEN-1] = '\0';
82b9a637 246 dir2 = opendir(dirname);
e4dc0e11
KS
247 if (dir2 != NULL) {
248 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
a551c7b0 249 char dirname2[MAX_PATHLEN];
e4dc0e11
KS
250 DIR *dir3;
251 struct dirent *dent3;
82b9a637 252
e4dc0e11
KS
253 if ((strcmp(dent2->d_name, ".") == 0) ||
254 (strcmp(dent2->d_name, "..") == 0))
82b9a637
GKH
255 continue;
256
aee380b6
KS
257 /* pass the net class as it is */
258 if (strcmp(dent->d_name, "net") == 0) {
259 snprintf(dirname2, MAX_PATHLEN, "/class/net/%s", dent2->d_name);
260 device_list_insert(dirname2, "net", &device_list);
261 continue;
262 }
263
82b9a637 264 snprintf(dirname2, MAX_PATHLEN, "%s/%s", dirname, dent2->d_name);
e4dc0e11 265 dirname2[MAX_PATHLEN-1] = '\0';
82b9a637 266 dir3 = opendir(dirname2);
e4dc0e11
KS
267 if (dir3 != NULL) {
268 for (dent3 = readdir(dir3); dent3 != NULL; dent3 = readdir(dir3)) {
269 char filename[MAX_PATHLEN];
82b9a637 270
aee380b6 271 /* pass devices with a "dev" file */
82b9a637 272 if (strcmp(dent3->d_name, "dev") == 0) {
0e306d08 273 snprintf(filename, MAX_PATHLEN, "/class/%s/%s",
e4dc0e11
KS
274 dent->d_name, dent2->d_name);
275 filename[MAX_PATHLEN-1] = '\0';
0e306d08 276 device_list_insert(filename, dent->d_name, &device_list);
aee380b6 277 break;
82b9a637
GKH
278 }
279 }
e13fa599 280 closedir(dir3);
82b9a637
GKH
281 }
282 }
e13fa599 283 closedir(dir2);
82b9a637
GKH
284 }
285 }
e13fa599 286 closedir(dir);
82b9a637 287 }
0e306d08
GKH
288
289 exec_list(&device_list);
82b9a637
GKH
290}
291
eb6c7cd0 292int udev_start(void)
82b9a637 293{
0e306d08
GKH
294 udev_scan_class();
295 udev_scan_block();
3f20eac0 296 return 0;
82b9a637 297}