add service_validator support
[project/procd.git] / coldplug.c
1 /*
2  * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
3  * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License version 2.1
7  * as published by the Free Software Foundation
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 #include <sys/stat.h>
16 #include <sys/types.h>
17 #include <sys/mount.h>
18
19 #include <unistd.h>
20
21 #include "procd.h"
22 #include "hotplug.h"
23
24 static struct uloop_process udevtrigger;
25
26 static void coldplug_complete(struct uloop_timeout *t)
27 {
28         DEBUG(2, "Coldplug complete\n");
29         hotplug_last_event(NULL);
30         procd_state_next();
31 }
32
33 static void udevtrigger_complete(struct uloop_process *proc, int ret)
34 {
35         DEBUG(2, "Finished udevtrigger\n");
36         hotplug_last_event(coldplug_complete);
37 }
38
39 void procd_coldplug(void)
40 {
41         char *argv[] = { "udevtrigger", NULL };
42
43         umount2("/dev/pts", MNT_DETACH);
44         umount2("/dev/", MNT_DETACH);
45         mount("tmpfs", "/dev", "tmpfs", 0, "mode=0755,size=512K");
46         mkdir("/dev/shm", 0755);
47         mkdir("/dev/pts", 0755);
48         mount("devpts", "/dev/pts", "devpts", 0, 0);
49         udevtrigger.cb = udevtrigger_complete;
50         udevtrigger.pid = fork();
51         if (!udevtrigger.pid) {
52                 execvp(argv[0], argv);
53                 ERROR("Failed to start coldplug\n");
54                 exit(-1);
55         }
56
57         if (udevtrigger.pid <= 0) {
58                 ERROR("Failed to start new coldplug instance\n");
59                 return;
60         }
61
62         uloop_process_add(&udevtrigger);
63
64         DEBUG(2, "Launched coldplug instance, pid=%d\n", (int) udevtrigger.pid);
65 }