5fcb9a333200d5d32fad5f7f98d8359f05c4fc6b
[project/procd.git] / plug / 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 "../libc-compat.h"
23
24 #include "hotplug.h"
25
26 static struct uloop_process udevtrigger;
27
28 static void coldplug_complete(struct uloop_timeout *t)
29 {
30 DEBUG(4, "Coldplug complete\n");
31 hotplug_last_event(NULL);
32 procd_state_next();
33 }
34
35 static void udevtrigger_complete(struct uloop_process *proc, int ret)
36 {
37 DEBUG(4, "Finished udevtrigger\n");
38 hotplug_last_event(coldplug_complete);
39 }
40
41 void procd_coldplug(void)
42 {
43 char *argv[] = { "udevtrigger", NULL };
44 unsigned int oldumask = umask(0);
45
46 umount2("/dev/pts", MNT_DETACH);
47 umount2("/dev/", MNT_DETACH);
48 mount("tmpfs", "/dev", "tmpfs", MS_NOSUID, "mode=0755,size=512K");
49 ignore(symlink("/tmp/shm", "/dev/shm"));
50 mkdir("/dev/pts", 0755);
51 umask(oldumask);
52 mount("devpts", "/dev/pts", "devpts", MS_NOEXEC | MS_NOSUID, 0);
53 udevtrigger.cb = udevtrigger_complete;
54 udevtrigger.pid = fork();
55 if (!udevtrigger.pid) {
56 execvp(argv[0], argv);
57 ERROR("Failed to start coldplug\n");
58 exit(-1);
59 }
60
61 if (udevtrigger.pid <= 0) {
62 ERROR("Failed to start new coldplug instance\n");
63 return;
64 }
65
66 uloop_process_add(&udevtrigger);
67
68 DEBUG(4, "Launched coldplug instance, pid=%d\n", (int) udevtrigger.pid);
69 }