]> git.ipfire.org Git - thirdparty/systemd.git/blame - udevstart.c
[PATCH] replace strncpy()/strncat() by strlcpy()/strlcat()
[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>
56a8a624 29#include <unistd.h>
82b9a637
GKH
30#include <errno.h>
31#include <ctype.h>
82b9a637 32#include <dirent.h>
56a8a624 33#include <signal.h>
82b9a637 34#include <sys/wait.h>
f22e9686 35#include <sys/stat.h>
f27125f9 36#include <sys/types.h>
82b9a637 37
7a947ce5 38#include "libsysfs/sysfs/libsysfs.h"
63f61c5c
KS
39#include "udev_libc_wrapper.h"
40#include "udev.h"
82b9a637 41#include "logging.h"
56a8a624 42#include "namedev.h"
9af5bb2f 43#include "udev_utils.h"
0e306d08 44#include "list.h"
82b9a637 45
56a8a624
KS
46#ifdef USE_LOG
47void log_message(int level, const char *format, ...)
48{
49}
50#endif
82b9a637 51
0e306d08
GKH
52struct device {
53 struct list_head list;
63f61c5c
KS
54 char path[PATH_SIZE];
55 char subsys[NAME_SIZE];
0e306d08
GKH
56};
57
58/* sort files in lexical order */
f22e9686 59static int device_list_insert(const char *path, char *subsystem, struct list_head *device_list)
0e306d08
GKH
60{
61 struct device *loop_device;
62 struct device *new_device;
63
f22e9686
KS
64 dbg("insert: '%s'\n", path);
65
0e306d08
GKH
66 list_for_each_entry(loop_device, device_list, list) {
67 if (strcmp(loop_device->path, path) > 0) {
68 break;
69 }
70 }
71
72 new_device = malloc(sizeof(struct device));
73 if (new_device == NULL) {
74 dbg("error malloc");
75 return -ENOMEM;
76 }
77
63f61c5c
KS
78 strlcpy(new_device->path, path, sizeof(new_device->path));
79 strlcpy(new_device->subsys, subsystem, sizeof(new_device->subsys));
0e306d08 80 list_add_tail(&new_device->list, &loop_device->list);
aee380b6 81 dbg("add '%s' from subsys '%s'", new_device->path, new_device->subsys);
0e306d08
GKH
82 return 0;
83}
84
0e306d08
GKH
85/* list of devices that we should run last due to any one of a number of reasons */
86static char *last_list[] = {
87 "/block/dm", /* on here because dm wants to have the block devices around before it */
88 NULL,
89};
90
70f630f6
GKH
91/* list of devices that we should run first due to any one of a number of reasons */
92static char *first_list[] = {
93 "/class/mem", /* people tend to like their memory devices around first... */
94 NULL,
95};
96
f22e9686 97static int add_device(const char *path, const char *subsystem)
f608f8ac 98{
7a947ce5 99 struct udevice udev;
7a947ce5 100 struct sysfs_class_device *class_dev;
f22e9686
KS
101 const char *devpath;
102
103 devpath = &path[strlen(sysfs_path)];
f608f8ac 104
af4b05d4 105 /* set environment for callouts and dev.d/ */
7a947ce5 106 setenv("DEVPATH", devpath, 1);
af4b05d4 107 setenv("SUBSYSTEM", subsystem, 1);
7a947ce5 108
56a8a624 109 dbg("exec: '%s' (%s)\n", devpath, path);
f22e9686 110
7a947ce5
KS
111 class_dev = sysfs_open_class_device_path(path);
112 if (class_dev == NULL) {
113 dbg ("sysfs_open_class_device_path failed");
114 return -ENODEV;
115 }
116
45a7b668 117 udev_init_device(&udev, devpath, subsystem);
5d24c6ca 118 udev_add_device(&udev, class_dev);
7a947ce5 119
8673dcb8 120 /* run dev.d/ scripts if we created a node or changed a netif name */
7757db1f 121 if (udev_dev_d && udev.devname[0] != '\0') {
8673dcb8 122 setenv("DEVNAME", udev.devname, 1);
9af5bb2f 123 udev_multiplex_directory(&udev, DEVD_DIR, DEVD_SUFFIX);
8673dcb8 124 }
5d24c6ca
KS
125
126 sysfs_close_class_device(class_dev);
56a8a624 127 udev_cleanup_device(&udev);
5d24c6ca
KS
128
129 return 0;
f608f8ac
KS
130}
131
0e306d08
GKH
132static void exec_list(struct list_head *device_list)
133{
134 struct device *loop_device;
135 struct device *tmp_device;
70f630f6
GKH
136 int i;
137
138 /* handle the "first" type devices first */
139 list_for_each_entry_safe(loop_device, tmp_device, device_list, list) {
f22e9686 140 for (i = 0; first_list[i] != NULL; i++) {
70f630f6 141 if (strncmp(loop_device->path, first_list[i], strlen(first_list[i])) == 0) {
7a947ce5 142 add_device(loop_device->path, loop_device->subsys);
70f630f6
GKH
143 list_del(&loop_device->list);
144 free(loop_device);
145 break;
146 }
147 }
148 }
0e306d08
GKH
149
150 /* handle the devices we are allowed to, excluding the "last" type devices */
151 list_for_each_entry_safe(loop_device, tmp_device, device_list, list) {
152 int found = 0;
f22e9686 153 for (i = 0; last_list[i] != NULL; i++) {
0e306d08
GKH
154 if (strncmp(loop_device->path, last_list[i], strlen(last_list[i])) == 0) {
155 found = 1;
156 break;
157 }
158 }
159 if (found)
160 continue;
161
7a947ce5 162 add_device(loop_device->path, loop_device->subsys);
0e306d08
GKH
163 list_del(&loop_device->list);
164 free(loop_device);
165 }
166
167 /* handle the rest of the devices left over, if any */
168 list_for_each_entry_safe(loop_device, tmp_device, device_list, list) {
7a947ce5 169 add_device(loop_device->path, loop_device->subsys);
0e306d08
GKH
170 list_del(&loop_device->list);
171 free(loop_device);
172 }
173}
174
f22e9686
KS
175static int has_devt(const char *directory)
176{
63f61c5c 177 char filename[PATH_SIZE];
f22e9686
KS
178 struct stat statbuf;
179
63f61c5c
KS
180 snprintf(filename, sizeof(filename), "%s/dev", directory);
181 filename[sizeof(filename)-1] = '\0';
f22e9686
KS
182
183 if (stat(filename, &statbuf) == 0)
184 return 1;
185
186 return 0;
187}
188
0e306d08 189static void udev_scan_block(void)
82b9a637 190{
63f61c5c 191 char base[PATH_SIZE];
e4dc0e11
KS
192 DIR *dir;
193 struct dirent *dent;
0e306d08 194 LIST_HEAD(device_list);
82b9a637 195
63f61c5c
KS
196 snprintf(base, sizeof(base), "%s/block", sysfs_path);
197 base[sizeof(base)-1] = '\0';
f22e9686
KS
198
199 dir = opendir(base);
e4dc0e11
KS
200 if (dir != NULL) {
201 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
63f61c5c 202 char dirname[PATH_SIZE];
e4dc0e11
KS
203 DIR *dir2;
204 struct dirent *dent2;
205
f22e9686 206 if (dent->d_name[0] == '.')
82b9a637
GKH
207 continue;
208
63f61c5c
KS
209 snprintf(dirname, sizeof(dirname), "%s/%s", base, dent->d_name);
210 dirname[sizeof(dirname)-1] = '\0';
f22e9686
KS
211 if (has_devt(dirname))
212 device_list_insert(dirname, "block", &device_list);
213 else
214 continue;
0e306d08 215
56a8a624 216 /* look for partitions */
82b9a637 217 dir2 = opendir(dirname);
e4dc0e11
KS
218 if (dir2 != NULL) {
219 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
63f61c5c 220 char dirname2[PATH_SIZE];
82b9a637 221
f22e9686 222 if (dent2->d_name[0] == '.')
82b9a637
GKH
223 continue;
224
63f61c5c
KS
225 snprintf(dirname2, sizeof(dirname2), "%s/%s", dirname, dent2->d_name);
226 dirname2[sizeof(dirname2)-1] = '\0';
f22e9686
KS
227
228 if (has_devt(dirname2))
229 device_list_insert(dirname2, "block", &device_list);
82b9a637 230 }
e13fa599 231 closedir(dir2);
82b9a637
GKH
232 }
233 }
e13fa599 234 closedir(dir);
82b9a637 235 }
0e306d08
GKH
236 exec_list(&device_list);
237}
238
239static void udev_scan_class(void)
240{
63f61c5c 241 char base[PATH_SIZE];
0e306d08
GKH
242 DIR *dir;
243 struct dirent *dent;
244 LIST_HEAD(device_list);
245
63f61c5c
KS
246 snprintf(base, sizeof(base), "%s/class", sysfs_path);
247 base[sizeof(base)-1] = '\0';
f22e9686
KS
248
249 dir = opendir(base);
e4dc0e11
KS
250 if (dir != NULL) {
251 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
63f61c5c 252 char dirname[PATH_SIZE];
e4dc0e11
KS
253 DIR *dir2;
254 struct dirent *dent2;
255
f22e9686 256 if (dent->d_name[0] == '.')
82b9a637
GKH
257 continue;
258
63f61c5c
KS
259 snprintf(dirname, sizeof(dirname), "%s/%s", base, dent->d_name);
260 dirname[sizeof(dirname)-1] = '\0';
56a8a624 261
82b9a637 262 dir2 = opendir(dirname);
e4dc0e11
KS
263 if (dir2 != NULL) {
264 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
63f61c5c 265 char dirname2[PATH_SIZE];
82b9a637 266
f22e9686 267 if (dent2->d_name[0] == '.')
82b9a637
GKH
268 continue;
269
63f61c5c
KS
270 snprintf(dirname2, sizeof(dirname2), "%s/%s", dirname, dent2->d_name);
271 dirname2[sizeof(dirname2)-1] = '\0';
f22e9686 272
aee380b6 273 /* pass the net class as it is */
f22e9686 274 if (strcmp(dent->d_name, "net") == 0)
aee380b6 275 device_list_insert(dirname2, "net", &device_list);
f22e9686
KS
276 else if (has_devt(dirname2))
277 device_list_insert(dirname2, dent->d_name, &device_list);
82b9a637 278 }
e13fa599 279 closedir(dir2);
82b9a637
GKH
280 }
281 }
e13fa599 282 closedir(dir);
82b9a637 283 }
0e306d08 284 exec_list(&device_list);
82b9a637
GKH
285}
286
56a8a624 287static void asmlinkage sig_handler(int signum)
82b9a637 288{
56a8a624
KS
289 switch (signum) {
290 case SIGALRM:
291 exit(1);
292 case SIGINT:
293 case SIGTERM:
294 exit(20 + signum);
295 }
296}
297
298int main(int argc, char *argv[], char *envp[])
299{
300 struct sigaction act;
301
302 udev_init_config();
303
304 /* set signal handlers */
305 memset(&act, 0x00, sizeof(act));
306 act.sa_handler = (void (*) (int))sig_handler;
307 sigemptyset (&act.sa_mask);
308 act.sa_flags = 0;
309 sigaction(SIGALRM, &act, NULL);
310 sigaction(SIGINT, &act, NULL);
311 sigaction(SIGTERM, &act, NULL);
312
313 /* trigger timeout to prevent hanging processes */
314 alarm(ALARM_TIMEOUT);
315
316 /* set environment for executed programs */
af4b05d4 317 setenv("ACTION", "add", 1);
69d2dbd6 318 setenv("UDEV_START", "1", 1);
af4b05d4 319
56a8a624
KS
320 namedev_init();
321
0e306d08 322 udev_scan_block();
56a8a624 323 udev_scan_class();
af4b05d4 324
3f20eac0 325 return 0;
82b9a637 326}