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