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