• 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 #define STDERR_HANDLE 2
29 
CloseStdio(void)30 void CloseStdio(void)
31 {
32     int fd = open("/dev/null", O_RDWR | O_CLOEXEC);
33     if (fd < 0) {
34         return;
35     }
36     dup2(fd, 0);
37     dup2(fd, 1);
38     dup2(fd, STDERR_HANDLE);
39     close(fd);
40 }
41 
MountBasicFs(void)42 void MountBasicFs(void)
43 {
44     if (mount("tmpfs", "/dev", "tmpfs", MS_NOSUID, "mode=0755") != 0) {
45         INIT_LOGE("Mount tmpfs failed. %s", strerror(errno));
46     }
47     if (mount("tmpfs", "/mnt", "tmpfs", MS_NOSUID, "mode=0755") != 0) {
48         INIT_LOGE("Mount tmpfs failed. %s", strerror(errno));
49     }
50     if (mkdir("/dev/pts", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) != 0) {
51         INIT_LOGE("mkdir /dev/pts failed. %s", strerror(errno));
52     }
53     if (mount("devpts", "/dev/pts", "devpts", 0, NULL) != 0) {
54         INIT_LOGE("Mount devpts failed. %s", strerror(errno));
55     }
56     if (mount("proc", "/proc", "proc", 0, "hidepid=2") != 0) {
57         INIT_LOGE("Mount procfs failed. %s", strerror(errno));
58     }
59     if (mount("sysfs", "/sys", "sysfs", 0, NULL) != 0) {
60         INIT_LOGE("Mount sysfs failed. %s", strerror(errno));
61     }
62     if (mount("selinuxfs", "/sys/fs/selinux", "selinuxfs", 0, NULL) != 0) {
63         INIT_LOGE("Mount selinuxfs failed. %s", strerror(errno));
64     }
65 }
66 
CreateDeviceNode(void)67 void CreateDeviceNode(void)
68 {
69     if (mknod("/dev/null", S_IFCHR | DEFAULT_RW_MODE, makedev(MEM_MAJOR, DEV_NULL_MINOR)) != 0) {
70         INIT_LOGE("Create /dev/null device node failed. %s", strerror(errno));
71     }
72     if (mknod("/dev/random", S_IFCHR | DEFAULT_RW_MODE, makedev(MEM_MAJOR, DEV_RANDOM_MINOR)) != 0) {
73         INIT_LOGE("Create /dev/random device node failed. %s", strerror(errno));
74     }
75 
76     if (mknod("/dev/urandom", S_IFCHR | DEFAULT_RW_MODE, makedev(MEM_MAJOR, DEV_URANDOM_MINOR)) != 0) {
77         INIT_LOGE("Create /dev/urandom device node failed. %s", strerror(errno));
78     }
79 }
80 
81