move /dev/shm to /tmp/shm
[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
19 #include <stdio.h>
20 #include <fcntl.h>
21 #include <unistd.h>
22 #include <stdlib.h>
23
24 #include "init.h"
25
26 static void
27 early_dev(void)
28 {
29 mkdev("*", 0600);
30 mknod("/dev/null", 0666, makedev(1, 3));
31 }
32
33 static void
34 early_console(const char *dev)
35 {
36 struct stat s;
37 int dd;
38
39 if (stat(dev, &s)) {
40 ERROR("Failed to stat %s\n", dev);
41 return;
42 }
43
44 dd = open(dev, O_RDWR);
45 if (dd < 0)
46 dd = open("/dev/null", O_RDWR);
47
48 dup2(dd, STDIN_FILENO);
49 dup2(dd, STDOUT_FILENO);
50 dup2(dd, STDERR_FILENO);
51
52 if (dd != STDIN_FILENO &&
53 dd != STDOUT_FILENO &&
54 dd != STDERR_FILENO)
55 close(dd);
56
57 fcntl(STDERR_FILENO, F_SETFL, fcntl(STDERR_FILENO, F_GETFL) | O_NONBLOCK);
58 }
59
60 static void
61 early_mounts(void)
62 {
63 unsigned int oldumask = umask(0);
64
65 mount("proc", "/proc", "proc", MS_NOATIME, 0);
66 mount("sysfs", "/sys", "sysfs", MS_NOATIME, 0);
67 mount("none", "/sys/fs/cgroup", "cgroup", 0, 0);
68 mount("tmpfs", "/dev", "tmpfs", MS_NOATIME, "mode=0755,size=512K");
69 symlink("/tmp/shm", "/dev/shm");
70 mkdir("/dev/pts", 0755);
71 mount("devpts", "/dev/pts", "devpts", MS_NOATIME, "mode=600");
72 early_dev();
73
74 early_console("/dev/console");
75 if (mount_zram_on_tmp()) {
76 mount("tmpfs", "/tmp", "tmpfs", MS_NOSUID | MS_NODEV | MS_NOATIME, NULL);
77 mkdir("/tmp/shm", 01777);
78 } else {
79 mkdir("/tmp/shm", 01777);
80 mount("tmpfs", "/tmp/shm", "tmpfs", MS_NOSUID | MS_NODEV | MS_NOATIME,
81 "mode=01777");
82 }
83 mkdir("/tmp/run", 0777);
84 mkdir("/tmp/lock", 0777);
85 mkdir("/tmp/state", 0777);
86 umask(oldumask);
87 }
88
89 static void
90 early_env(void)
91 {
92 setenv("PATH", "/bin:/sbin:/usr/bin:/usr/sbin", 1);
93 }
94
95 void
96 early(void)
97 {
98 if (getpid() != 1)
99 return;
100
101 early_mounts();
102 early_env();
103
104 LOG("Console is alive\n");
105 }