device.c 3.0 KB
Newer Older
Z
zhong_ning 已提交
1
/*
2
 * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
Z
zhong_ning 已提交
3 4 5 6 7 8 9 10 11 12 13 14
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
Z
zhong_ning 已提交
15
#include "device.h"
16

L
lanxueyuan 已提交
17
#include <errno.h>
Z
zhong_ning 已提交
18
#include <fcntl.h>
L
lanxueyuan 已提交
19 20 21
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
22

4
411148299@qq.com 已提交
23
#include <linux/major.h>
Z
zhong_ning 已提交
24
#include "init_log.h"
Z
zhong_ning 已提交
25

Z
zhong_ning 已提交
26 27
#define DEFAULT_RW_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)
#define DEFAULT_NO_AUTHORITY_MODE (S_IWUSR | S_IRUSR)
Z
zhong_ning 已提交
28
#define STDERR_HANDLE 2
L
leon 已提交
29

Z
zhong_ning 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42
void CloseStdio(void)
{
    int fd = open("/dev/null", O_RDWR | O_CLOEXEC);
    if (fd < 0) {
        return;
    }
    dup2(fd, 0);
    dup2(fd, 1);
    dup2(fd, STDERR_HANDLE);
    close(fd);
}

void MountBasicFs(void)
Z
zhong_ning 已提交
43 44
{
    if (mount("tmpfs", "/dev", "tmpfs", MS_NOSUID, "mode=0755") != 0) {
Z
zhong_ning 已提交
45
        INIT_LOGE("Mount tmpfs failed. %s", strerror(errno));
Z
zhong_ning 已提交
46
    }
47 48 49
    if (mount("tmpfs", "/mnt", "tmpfs", MS_NOSUID, "mode=0755") != 0) {
        INIT_LOGE("Mount tmpfs failed. %s", strerror(errno));
    }
X
xionglei6 已提交
50 51 52 53 54 55
    if (mount("tmpfs", "/storage", "tmpfs", MS_NOEXEC | MS_NODEV| MS_NOSUID, "mode=0755") != 0) {
        INIT_LOGE("Mount storage failed. %s", strerror(errno));
    }
    if (mount("none", "/config", "configfs", MS_NOEXEC | MS_NODEV| MS_NOSUID, "mode=0755") != 0) {
        INIT_LOGE("Mount  configfs failed. %s", strerror(errno));
    }
Z
zhong_ning 已提交
56
    if (mkdir("/dev/pts", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) != 0) {
Z
zhong_ning 已提交
57
        INIT_LOGE("mkdir /dev/pts failed. %s", strerror(errno));
Z
zhong_ning 已提交
58 59
    }
    if (mount("devpts", "/dev/pts", "devpts", 0, NULL) != 0) {
Z
zhong_ning 已提交
60
        INIT_LOGE("Mount devpts failed. %s", strerror(errno));
Z
zhong_ning 已提交
61
    }
Z
zhong_ning 已提交
62
    if (mount("proc", "/proc", "proc", 0, "hidepid=2") != 0) {
Z
zhong_ning 已提交
63
        INIT_LOGE("Mount procfs failed. %s", strerror(errno));
Z
zhong_ning 已提交
64 65
    }
    if (mount("sysfs", "/sys", "sysfs", 0, NULL) != 0) {
Z
zhong_ning 已提交
66
        INIT_LOGE("Mount sysfs failed. %s", strerror(errno));
Z
zhong_ning 已提交
67
    }
Z
zhong_ning 已提交
68
    if (mount("selinuxfs", "/sys/fs/selinux", "selinuxfs", 0, NULL) != 0) {
Z
zhong_ning 已提交
69
        INIT_LOGE("Mount selinuxfs failed. %s", strerror(errno));
Z
zhong_ning 已提交
70
    }
Z
zhong_ning 已提交
71 72
}

Z
zhong_ning 已提交
73
void CreateDeviceNode(void)
Z
zhong_ning 已提交
74
{
Z
zhong_ning 已提交
75
    if (mknod("/dev/null", S_IFCHR | DEFAULT_RW_MODE, makedev(MEM_MAJOR, DEV_NULL_MINOR)) != 0) {
Z
zhong_ning 已提交
76
        INIT_LOGE("Create /dev/null device node failed. %s", strerror(errno));
Z
zhong_ning 已提交
77
    }
Z
zhong_ning 已提交
78
    if (mknod("/dev/random", S_IFCHR | DEFAULT_RW_MODE, makedev(MEM_MAJOR, DEV_RANDOM_MINOR)) != 0) {
Z
zhong_ning 已提交
79
        INIT_LOGE("Create /dev/random device node failed. %s", strerror(errno));
Z
zhong_ning 已提交
80 81
    }

Z
zhong_ning 已提交
82
    if (mknod("/dev/urandom", S_IFCHR | DEFAULT_RW_MODE, makedev(MEM_MAJOR, DEV_URANDOM_MINOR)) != 0) {
Z
zhong_ning 已提交
83
        INIT_LOGE("Create /dev/urandom device node failed. %s", strerror(errno));
Z
zhong_ning 已提交
84 85
    }
}
Z
zhong_ning 已提交
86