• 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("tmpfs", "/storage", "tmpfs", MS_NOEXEC | MS_NODEV| MS_NOSUID, "mode=0755") != 0) {
38         INIT_LOGE("Mount storage failed. %s", strerror(errno));
39     }
40     if (mkdir("/dev/pts", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) != 0) {
41         INIT_LOGE("mkdir /dev/pts failed. %s", strerror(errno));
42     }
43     if (mount("devpts", "/dev/pts", "devpts", 0, NULL) != 0) {
44         INIT_LOGE("Mount devpts failed. %s", strerror(errno));
45     }
46     if (mount("proc", "/proc", "proc", 0, "gid=3009,hidepid=2") != 0) {
47         INIT_LOGE("Mount procfs failed. %s", strerror(errno));
48     }
49     if (mount("sysfs", "/sys", "sysfs", 0, NULL) != 0) {
50         INIT_LOGE("Mount sysfs failed. %s", strerror(errno));
51     }
52     if (mount("selinuxfs", "/sys/fs/selinux", "selinuxfs", 0, NULL) != 0) {
53         INIT_LOGE("Mount selinuxfs failed. %s", strerror(errno));
54     }
55 }
56 
CreateDeviceNode(void)57 void CreateDeviceNode(void)
58 {
59     if (mknod("/dev/null", S_IFCHR | DEFAULT_RW_MODE, makedev(MEM_MAJOR, DEV_NULL_MINOR)) != 0) {
60         INIT_LOGE("Create /dev/null device node failed. %s", strerror(errno));
61     }
62     if (mknod("/dev/random", S_IFCHR | DEFAULT_RW_MODE, makedev(MEM_MAJOR, DEV_RANDOM_MINOR)) != 0) {
63         INIT_LOGE("Create /dev/random device node failed. %s", strerror(errno));
64     }
65 
66     if (mknod("/dev/urandom", S_IFCHR | DEFAULT_RW_MODE, makedev(MEM_MAJOR, DEV_URANDOM_MINOR)) != 0) {
67         INIT_LOGE("Create /dev/urandom device node failed. %s", strerror(errno));
68     }
69 }
70 
71