procd: Don't use syslog before its initialization
[project/procd.git] / initd / early.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/mount.h>
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <sys/sysmacros.h>
19
20 #include <stdio.h>
21 #include <fcntl.h>
22 #include <unistd.h>
23 #include <stdlib.h>
24
25 #include "../utils/utils.h"
26 #include "init.h"
27 #include "../libc-compat.h"
28
29 static void
30 early_dev(void)
31 {
32         mkdev("*", 0600);
33         mknod("/dev/null", 0666, makedev(1, 3));
34 }
35
36 static void
37 early_console(const char *dev)
38 {
39         struct stat s;
40
41         if (stat(dev, &s)) {
42                 ERROR("Failed to stat %s\n", dev);
43                 return;
44         }
45
46         if (patch_stdio(dev)) {
47                 ERROR("Failed to setup i/o redirection\n");
48                 return;
49         }
50
51         fcntl(STDERR_FILENO, F_SETFL, fcntl(STDERR_FILENO, F_GETFL) | O_NONBLOCK);
52 }
53
54 static void
55 early_mounts(void)
56 {
57         unsigned int oldumask = umask(0);
58
59         mount("proc", "/proc", "proc", MS_NOATIME | MS_NODEV | MS_NOEXEC | MS_NOSUID, 0);
60         mount("sysfs", "/sys", "sysfs", MS_NOATIME | MS_NODEV | MS_NOEXEC | MS_NOSUID, 0);
61         mount("cgroup", "/sys/fs/cgroup", "cgroup",  MS_NODEV | MS_NOEXEC | MS_NOSUID, 0);
62         mount("tmpfs", "/dev", "tmpfs", MS_NOATIME | MS_NOSUID, "mode=0755,size=512K");
63         ignore(symlink("/tmp/shm", "/dev/shm"));
64         mkdir("/dev/pts", 0755);
65         mount("devpts", "/dev/pts", "devpts", MS_NOATIME | MS_NOEXEC | MS_NOSUID, "mode=600");
66         early_dev();
67
68         early_console("/dev/console");
69         if (mount_zram_on_tmp()) {
70                 mount("tmpfs", "/tmp", "tmpfs", MS_NOSUID | MS_NODEV | MS_NOATIME, 0);
71                 mkdir("/tmp/shm", 01777);
72         } else {
73                 mkdir("/tmp/shm", 01777);
74                 mount("tmpfs", "/tmp/shm", "tmpfs", MS_NOSUID | MS_NODEV | MS_NOATIME,
75                                 "mode=01777");
76         }
77         mkdir("/tmp/run", 0755);
78         mkdir("/tmp/lock", 0755);
79         mkdir("/tmp/state", 0755);
80         umask(oldumask);
81 }
82
83 static void
84 early_env(void)
85 {
86         setenv("PATH", EARLY_PATH, 1);
87 }
88
89 void
90 early(void)
91 {
92         if (getpid() != 1)
93                 return;
94
95         early_mounts();
96         early_env();
97
98         LOG("Console is alive\n");
99 }