1 /*
2  * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include "device.h"
16 
17 #include <unistd.h>
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <sys/mount.h>
21 #include <sys/stat.h>
22 #include <sys/sysmacros.h>
23 
24 #include <linux/major.h>
25 #include "init_log.h"
26 
27 #define DEFAULT_RW_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)
28 #define DEFAULT_NO_AUTHORITY_MODE (S_IWUSR | S_IRUSR)
29 
MountBasicFs(void)30 static void MountBasicFs(void)
31 {
32     if (access("dev/null", F_OK) != 0) {
33         INIT_LOGI("mount dev tmpfs");
34         if (mount("tmpfs", "/dev", "tmpfs", MS_NOSUID, "mode=0755") != 0) {
35             INIT_LOGE("Mount tmpfs failed. %s", strerror(errno));
36         }
37     } else {
38         INIT_LOGI("dev already mounted");
39     }
40     if (mount("tmpfs", "/mnt", "tmpfs", MS_NOSUID, "mode=0755") != 0) {
41         INIT_LOGE("Mount tmpfs failed. %s", strerror(errno));
42     }
43     if (mount(NULL, "/mnt", NULL, MS_SLAVE, NULL) != 0) {
44         INIT_LOGE("Mount tmpfs slave failed. %s", strerror(errno));
45     }
46     if (mkdir("/mnt/data", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) != 0) {
47         INIT_LOGE("mkdir /mnt/data failed. %s", strerror(errno));
48     }
49     if (mount("tmpfs", "/mnt/data", "tmpfs", MS_NOSUID, "mode=0755") != 0) {
50         INIT_LOGE("Mount tmpfs failed. %s", strerror(errno));
51     }
52     if (mount(NULL, "/mnt/data", NULL, MS_SHARED, NULL) != 0) {
53         INIT_LOGE("Mount tmpfs shared failed. %s", strerror(errno));
54     }
55     if (mount("tmpfs", "/storage", "tmpfs", MS_NOEXEC | MS_NODEV| MS_NOSUID, "mode=0755") != 0) {
56         INIT_LOGE("Mount storage failed. %s", strerror(errno));
57     }
58     struct stat st;
59     if (!(stat("/dev/pts", &st) == 0 && S_ISDIR(st.st_mode))) {
60         if (mkdir("/dev/pts", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) != 0) {
61             INIT_LOGE("mkdir /dev/pts failed. %s", strerror(errno));
62         }
63         if (mount("devpts", "/dev/pts", "devpts", 0, NULL) != 0) {
64             INIT_LOGE("Mount devpts failed. %s", strerror(errno));
65         }
66     }
67     if (mount("proc", "/proc", "proc", 0, "gid=3009,hidepid=2") != 0) {
68         INIT_LOGE("Mount procfs failed. %s", strerror(errno));
69     }
70     if (mount("sysfs", "/sys", "sysfs", 0, NULL) != 0) {
71         INIT_LOGE("Mount sysfs failed. %s", strerror(errno));
72     }
73     if (mount("selinuxfs", "/sys/fs/selinux", "selinuxfs", 0, NULL) != 0) {
74         INIT_LOGE("Mount selinuxfs failed. %s", strerror(errno));
75     }
76 }
77 
CreateDeviceNode(void)78 static void CreateDeviceNode(void)
79 {
80     if (access("/dev/null", F_OK) != 0) {
81         if (mknod("/dev/null", S_IFCHR | DEFAULT_RW_MODE, makedev(MEM_MAJOR, DEV_NULL_MINOR)) != 0) {
82             INIT_LOGE("Create /dev/null device node failed. %s", strerror(errno));
83         }
84     }
85 
86     if (access("/dev/random", F_OK) != 0) {
87         if (mknod("/dev/random", S_IFCHR | DEFAULT_RW_MODE, makedev(MEM_MAJOR, DEV_RANDOM_MINOR)) != 0) {
88             INIT_LOGE("Create /dev/random device node failed. %s", strerror(errno));
89         }
90     }
91 
92     if (access("/dev/urandom", F_OK) != 0) {
93         if (mknod("/dev/urandom", S_IFCHR | DEFAULT_RW_MODE, makedev(MEM_MAJOR, DEV_URANDOM_MINOR)) != 0) {
94             INIT_LOGW("Create /dev/urandom device node failed. %s", strerror(errno));
95         }
96     }
97 }
98 
EnableDevKmsg(void)99 static void EnableDevKmsg(void)
100 {
101     /* printk_devkmsg default value is ratelimit, We need to set "on" and remove the restrictions */
102     int fd = open("/proc/sys/kernel/printk_devkmsg", O_WRONLY | O_CLOEXEC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
103     if (fd < 0) {
104         return;
105     }
106     char *kmsgStatus = "on";
107     write(fd, kmsgStatus, strlen(kmsgStatus) + 1);
108     close(fd);
109     fd = -1;
110     return;
111 }
112 
CreateFsAndDeviceNode(void)113 void CreateFsAndDeviceNode(void)
114 {
115     MountBasicFs();
116     CreateDeviceNode();
117     EnableDevKmsg();
118 }
119