• 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 <errno.h>
18 #include <fcntl.h>
19 #include <sys/mount.h>
20 #include <sys/stat.h>
21 #include <sys/sysmacros.h>
22 
23 #include <linux/major.h>
24 #include "init_log.h"
25 
26 #define DEFAULT_RW_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)
27 #define DEFAULT_NO_AUTHORITY_MODE (S_IWUSR | S_IRUSR)
28 
MountBasicFs(void)29 void MountBasicFs(void)
30 {
31     if (mount("tmpfs", "/dev", "tmpfs", MS_NOSUID, "mode=0755") != 0) {
32         INIT_LOGE("Mount tmpfs failed. %s", strerror(errno));
33     }
34     if (mount("tmpfs", "/mnt", "tmpfs", MS_NOSUID, "mode=0755") != 0) {
35         INIT_LOGE("Mount tmpfs failed. %s", strerror(errno));
36     }
37     if (mount(NULL, "/mnt", NULL, MS_SLAVE, NULL) != 0) {
38         INIT_LOGE("Mount tmpfs slave failed. %s", strerror(errno));
39     }
40     if (mkdir("/mnt/data", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) != 0) {
41         INIT_LOGE("mkdir /mnt/data failed. %s", strerror(errno));
42     }
43     if (mount("tmpfs", "/mnt/data", "tmpfs", MS_NOSUID, "mode=0755") != 0) {
44         INIT_LOGE("Mount tmpfs failed. %s", strerror(errno));
45     }
46     if (mount(NULL, "/mnt/data", NULL, MS_SHARED, NULL) != 0) {
47         INIT_LOGE("Mount tmpfs shared failed. %s", strerror(errno));
48     }
49     if (mount("tmpfs", "/storage", "tmpfs", MS_NOEXEC | MS_NODEV| MS_NOSUID, "mode=0755") != 0) {
50         INIT_LOGE("Mount storage failed. %s", strerror(errno));
51     }
52     if (mkdir("/dev/pts", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) != 0) {
53         INIT_LOGE("mkdir /dev/pts failed. %s", strerror(errno));
54     }
55     if (mount("devpts", "/dev/pts", "devpts", 0, NULL) != 0) {
56         INIT_LOGE("Mount devpts failed. %s", strerror(errno));
57     }
58     if (mount("proc", "/proc", "proc", 0, "gid=3009,hidepid=2") != 0) {
59         INIT_LOGE("Mount procfs failed. %s", strerror(errno));
60     }
61     if (mount("sysfs", "/sys", "sysfs", 0, NULL) != 0) {
62         INIT_LOGE("Mount sysfs failed. %s", strerror(errno));
63     }
64     if (mount("selinuxfs", "/sys/fs/selinux", "selinuxfs", 0, NULL) != 0) {
65         INIT_LOGE("Mount selinuxfs failed. %s", strerror(errno));
66     }
67 }
68 
CreateDeviceNode(void)69 void CreateDeviceNode(void)
70 {
71     if (mknod("/dev/null", S_IFCHR | DEFAULT_RW_MODE, makedev(MEM_MAJOR, DEV_NULL_MINOR)) != 0) {
72         INIT_LOGE("Create /dev/null device node failed. %s", strerror(errno));
73     }
74     if (mknod("/dev/random", S_IFCHR | DEFAULT_RW_MODE, makedev(MEM_MAJOR, DEV_RANDOM_MINOR)) != 0) {
75         INIT_LOGE("Create /dev/random device node failed. %s", strerror(errno));
76     }
77 
78     if (mknod("/dev/urandom", S_IFCHR | DEFAULT_RW_MODE, makedev(MEM_MAJOR, DEV_URANDOM_MINOR)) != 0) {
79         INIT_LOGE("Create /dev/urandom device node failed. %s", strerror(errno));
80     }
81 }
82 
83