• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     if (mkdir("/dev/pts", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) != 0) {
59         INIT_LOGE("mkdir /dev/pts failed. %s", strerror(errno));
60     }
61     if (mount("devpts", "/dev/pts", "devpts", 0, NULL) != 0) {
62         INIT_LOGE("Mount devpts failed. %s", strerror(errno));
63     }
64     if (mount("proc", "/proc", "proc", 0, "gid=3009,hidepid=2") != 0) {
65         INIT_LOGE("Mount procfs failed. %s", strerror(errno));
66     }
67     if (mount("sysfs", "/sys", "sysfs", 0, NULL) != 0) {
68         INIT_LOGE("Mount sysfs failed. %s", strerror(errno));
69     }
70     if (mount("selinuxfs", "/sys/fs/selinux", "selinuxfs", 0, NULL) != 0) {
71         INIT_LOGE("Mount selinuxfs failed. %s", strerror(errno));
72     }
73 }
74 
CreateDeviceNode(void)75 static void CreateDeviceNode(void)
76 {
77     if (mknod("/dev/null", S_IFCHR | DEFAULT_RW_MODE, makedev(MEM_MAJOR, DEV_NULL_MINOR)) != 0) {
78         INIT_LOGE("Create /dev/null device node failed. %s", strerror(errno));
79     }
80     if (mknod("/dev/random", S_IFCHR | DEFAULT_RW_MODE, makedev(MEM_MAJOR, DEV_RANDOM_MINOR)) != 0) {
81         INIT_LOGE("Create /dev/random device node failed. %s", strerror(errno));
82     }
83 
84     if (mknod("/dev/urandom", S_IFCHR | DEFAULT_RW_MODE, makedev(MEM_MAJOR, DEV_URANDOM_MINOR)) != 0) {
85         INIT_LOGE("Create /dev/urandom device node failed. %s", strerror(errno));
86     }
87 }
88 
EnableDevKmsg(void)89 static void EnableDevKmsg(void)
90 {
91     /* printk_devkmsg default value is ratelimit, We need to set "on" and remove the restrictions */
92     int fd = open("/proc/sys/kernel/printk_devkmsg", O_WRONLY | O_CLOEXEC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
93     if (fd < 0) {
94         return;
95     }
96     char *kmsgStatus = "on";
97     write(fd, kmsgStatus, strlen(kmsgStatus) + 1);
98     close(fd);
99     fd = -1;
100     return;
101 }
102 
CreateFsAndDeviceNode(void)103 void CreateFsAndDeviceNode(void)
104 {
105     MountBasicFs();
106     CreateDeviceNode();
107     EnableDevKmsg();
108 }
109