init.c 9.4 KB
Newer Older
1
/*
M
Mupceet 已提交
2
 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
 * 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.
 */
#include "init.h"

#include <errno.h>
X
xionglei6 已提交
18
#include <poll.h>
19 20
#include <stdlib.h>
#include <signal.h>
21
#include <time.h>
22 23 24
#include <sys/sysmacros.h>
#include <sys/stat.h>
#include <sys/types.h>
X
xionglei6 已提交
25
#include <sys/socket.h>
4
411148299@qq.com 已提交
26
#include <linux/major.h>
Z
zhr758 已提交
27 28

#include "config_policy_utils.h"
29
#include "device.h"
X
xionglei6 已提交
30
#include "fd_holder_service.h"
M
Mupceet 已提交
31
#include "init_control_fd_service.h"
32 33
#include "init_log.h"
#include "init_mount.h"
X
xionglei6 已提交
34
#include "init_group_manager.h"
35
#include "init_param.h"
X
xionglei6 已提交
36 37
#include "init_service.h"
#include "init_service_manager.h"
38 39
#include "init_utils.h"
#include "securec.h"
X
xionglei6 已提交
40
#include "fd_holder_internal.h"
X
xionglei6 已提交
41 42
#include "sandbox.h"
#include "sandbox_namespace.h"
Q
Qin Fandong 已提交
43
#ifdef WITH_SELINUX
X
xionglei6 已提交
44
#include <policycoreutils.h>
R
renwei 已提交
45
#include <selinux/selinux.h>
Q
Qin Fandong 已提交
46
#endif // WITH_SELINUX
47
#include "bootstage.h"
48

X
xionglei6 已提交
49 50
static bool g_enableSandbox;

X
xionglei6 已提交
51 52 53 54
static int FdHolderSockInit(void)
{
    int sock = -1;
    int on = 1;
X
xionglei6 已提交
55 56
    int fdHolderBufferSize = FD_HOLDER_BUFFER_SIZE; // 4KiB
    sock = socket(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
X
xionglei6 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69
    if (sock < 0) {
        INIT_LOGE("Failed to create fd holder socket, err = %d", errno);
        return -1;
    }

    setsockopt(sock, SOL_SOCKET, SO_RCVBUFFORCE, &fdHolderBufferSize, sizeof(fdHolderBufferSize));
    setsockopt(sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));

    if (access(INIT_HOLDER_SOCKET_PATH, F_OK) == 0) {
        INIT_LOGI("%s exist, remove it", INIT_HOLDER_SOCKET_PATH);
        unlink(INIT_HOLDER_SOCKET_PATH);
    }
    struct sockaddr_un addr;
X
xionglei6 已提交
70
    addr.sun_family = AF_UNIX;
X
xionglei6 已提交
71 72 73 74 75 76 77 78
    if (strncpy_s(addr.sun_path, sizeof(addr.sun_path),
        INIT_HOLDER_SOCKET_PATH, strlen(INIT_HOLDER_SOCKET_PATH)) != 0) {
        INIT_LOGE("Faild to copy fd hoder socket path");
        close(sock);
        return -1;
    }
    socklen_t len = (socklen_t)(offsetof(struct sockaddr_un, sun_path) + strlen(addr.sun_path) + 1);
    if (bind(sock, (struct sockaddr *)&addr, len) < 0) {
X
xionglei6 已提交
79
        INIT_LOGE("Failed to binder fd folder socket %d", errno);
X
xionglei6 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
        close(sock);
        return -1;
    }

    // Owned by root
    if (lchown(addr.sun_path, 0, 0)) {
        INIT_LOGW("Failed to change owner of fd holder socket, err = %d", errno);
    }
    mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
    if (fchmodat(AT_FDCWD, addr.sun_path, mode, AT_SYMLINK_NOFOLLOW)) {
        INIT_LOGW("Failed to change mode of fd holder socket, err = %d", errno);
    }
    INIT_LOGI("Init fd holder socket done");
    return sock;
}

96 97 98
void SystemInit(void)
{
    SignalInit();
X
xionglei6 已提交
99 100
    // umask call always succeeds and return the previous mask value which is not needed here
    (void)umask(DEFAULT_UMASK_INIT);
101
    MakeDirRecursive("/dev/unix/socket", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
X
xionglei6 已提交
102 103 104 105
    int sock = FdHolderSockInit();
    if (sock >= 0) {
        RegisterFdHoldWatcher(sock);
    }
M
Mupceet 已提交
106
    InitControlFd();
X
xionglei6 已提交
107 108
}

109 110 111 112 113 114 115 116 117 118 119
void LogInit(void)
{
    int ret = mknod("/dev/kmsg", S_IFCHR | S_IWUSR | S_IRUSR,
        makedev(MEM_MAJOR, DEV_KMSG_MINOR));
    if (ret == 0) {
        OpenLogDevice();
    }
}

void SystemPrepare(void)
{
120
    // Second stage, nothing to prepare
121 122
}

Q
Qin Fandong 已提交
123 124 125 126
void SystemLoadSelinux(void)
{
#ifdef WITH_SELINUX
    // load selinux policy and context
R
renwei 已提交
127
    if (LoadPolicy() < 0) {
Q
Qin Fandong 已提交
128 129 130 131
        INIT_LOGE("main, load_policy failed.");
    } else {
        INIT_LOGI("main, load_policy success.");
    }
R
renwei 已提交
132 133

    setcon("u:r:init:s0");
R
renwei 已提交
134
    (void)RestoreconRecurse("/dev");
Q
Qin Fandong 已提交
135 136 137
#endif // WITH_SELINUX
}

X
xionglei6 已提交
138 139 140 141
static void BootStateChange(const char *content)
{
    INIT_LOGI("boot start %s finish.", content);
    if (strcmp("init", content) == 0) {
X
xionglei6 已提交
142
        StartAllServices(START_MODE_BOOT);
X
xionglei6 已提交
143 144 145 146
        return;
    }
    if (strcmp("post-init", content) == 0) {
        StartAllServices(START_MODE_NARMAL);
M
Mupceet 已提交
147 148
        // Destroy all hooks
        HookMgrDestroy(NULL);
X
xionglei6 已提交
149 150 151 152 153
        return;
    }
}

#if defined(OHOS_SERVICE_DUMP)
X
xionglei6 已提交
154
static int SystemDump(int id, const char *name, int argc, const char **argv)
X
xionglei6 已提交
155
{
X
xionglei6 已提交
156
    INIT_ERROR_CHECK(argv != NULL && argc >= 1, return 0, "Invalid install parameter");
X
xionglei6 已提交
157
    INIT_LOGI("Dump system info %s", argv[0]);
M
Mupceet 已提交
158 159
    SystemDumpParameters(1);
    SystemDumpTriggers(1);
X
xionglei6 已提交
160
    return 0;
X
xionglei6 已提交
161 162 163
}
#endif

X
xionglei6 已提交
164 165 166 167 168 169 170 171 172 173
static void IsEnableSandbox(void)
{
    const char *name = "const.sandbox";
    char value[MAX_BUFFER_LEN] = {0};
    unsigned int len = MAX_BUFFER_LEN;
    if (SystemReadParam(name, value, &len) != 0) {
        INIT_LOGE("Failed read param.");
        g_enableSandbox = false;
    }
    if (strcmp(value, "enable") == 0) {
X
xionglei6 已提交
174
        INIT_LOGI("Enable sandbox.");
X
xionglei6 已提交
175 176
        g_enableSandbox = true;
    } else {
X
xionglei6 已提交
177
        INIT_LOGI("Disable sandbox.");
X
xionglei6 已提交
178 179 180 181
        g_enableSandbox = false;
    }
}

Z
zhr758 已提交
182 183
static void InitLoadParamFiles(void)
{
Y
yichengzhao 已提交
184 185 186 187 188 189
    if (InUpdaterMode() != 0) {
        LoadDefaultParams("/etc/param/ohos_const", LOAD_PARAM_NORMAL);
        LoadDefaultParams("/etc/param", LOAD_PARAM_ONLY_ADD);
        return;
    }

Z
zhr758 已提交
190 191 192 193 194 195 196 197 198 199 200
    // Load const params, these can't be override!
    LoadDefaultParams("/system/etc/param/ohos_const", LOAD_PARAM_NORMAL);
    CfgFiles *files = GetCfgFiles("etc/param");
    for (int i = MAX_CFG_POLICY_DIRS_CNT - 1; files && i >= 0; i--) {
        if (files->paths[i]) {
            LoadDefaultParams(files->paths[i], LOAD_PARAM_ONLY_ADD);
        }
    }
    FreeCfgFiles(files);
}

201 202 203 204 205 206 207 208 209 210 211 212 213 214
typedef struct HOOK_TIMING_STAT {
    struct timespec startTime;
    struct timespec endTime;
} HOOK_TIMING_STAT;

static void InitPreHook(const HOOK_INFO *hookInfo)
{
    HOOK_TIMING_STAT *stat = (HOOK_TIMING_STAT *)hookInfo->cookie;
    clock_gettime(CLOCK_MONOTONIC, &(stat->startTime));
}

static void InitPostHook(const HOOK_INFO *hookInfo)
{
    long long diff;
M
Mupceet 已提交
215
    const long long baseTime = 1000;
216 217 218
    HOOK_TIMING_STAT *stat = (HOOK_TIMING_STAT *)hookInfo->cookie;
    clock_gettime(CLOCK_MONOTONIC, &(stat->endTime));

M
Mupceet 已提交
219
    diff = (long long)((stat->endTime.tv_sec - stat->startTime.tv_sec) / baseTime);
220
    if (stat->endTime.tv_nsec > stat->startTime.tv_nsec) {
M
Mupceet 已提交
221
        diff += (stat->endTime.tv_nsec - stat->startTime.tv_nsec) * baseTime;
222
    } else {
M
Mupceet 已提交
223
        diff -= (stat->endTime.tv_nsec - stat->startTime.tv_nsec) * baseTime;
224 225
    }

M
Mupceet 已提交
226
    INIT_LOGV("Executing hook [%d:%d:%p] cost [%lld]ms, return %d.",
M
Mupceet 已提交
227
        hookInfo->stage, hookInfo->prio, hookInfo->hook, diff, hookInfo->retVal);
228 229
}

230 231
void SystemConfig(void)
{
232 233 234 235 236 237 238 239 240
    HOOK_TIMING_STAT timingStat;
    HOOK_EXEC_ARGS args;

    args.flags = 0;
    args.cookie = (void *)&timingStat;
    args.preHook = InitPreHook;
    args.postHook = InitPostHook;

    HookMgrExecute(NULL, INIT_GLOBAL_INIT, (void *)&args);
X
xionglei6 已提交
241
    InitServiceSpace();
242 243

    HookMgrExecute(NULL, INIT_PRE_PARAM_SERVICE, (void *)&args);
Z
zhr758 已提交
244
    InitParamService();
X
xionglei6 已提交
245 246 247
    InitParseGroupCfg();
    RegisterBootStateChange(BootStateChange);

R
renwei 已提交
248 249 250
    // load SELinux context and policy
    // Do not move position!
    SystemLoadSelinux();
251
    // parse parameters
252
    HookMgrExecute(NULL, INIT_PRE_PARAM_LOAD, (void *)&args);
Z
zhr758 已提交
253
    InitLoadParamFiles();
254
    // read config
255
    HookMgrExecute(NULL, INIT_PRE_CFG_LOAD, (void *)&args);
256 257
    ReadConfig();
    INIT_LOGI("Parse init config file done.");
258 259
    HookMgrExecute(NULL, INIT_POST_CFG_LOAD, (void *)&args);

260
    // dump config
X
xionglei6 已提交
261 262 263
#if defined(OHOS_SERVICE_DUMP)
    AddCmdExecutor("display", SystemDump);
    (void)AddCompleteJob("param:ohos.servicectrl.display", "ohos.servicectrl.display=*", "display system");
264
#endif
X
xionglei6 已提交
265
    IsEnableSandbox();
266 267 268 269 270 271 272 273 274 275
    // execute init
    PostTrigger(EVENT_TRIGGER_BOOT, "pre-init", strlen("pre-init"));
    PostTrigger(EVENT_TRIGGER_BOOT, "init", strlen("init"));
    PostTrigger(EVENT_TRIGGER_BOOT, "post-init", strlen("post-init"));
}

void SystemRun(void)
{
    StartParamService();
}
X
xionglei6 已提交
276

X
xionglei6 已提交
277
void SetServiceEnterSandbox(const char *execPath, unsigned int attribute)
X
xionglei6 已提交
278 279
{
    if (g_enableSandbox == false) {
X
xionglei6 已提交
280
        return;
X
xionglei6 已提交
281
    }
X
xionglei6 已提交
282 283 284 285 286 287 288 289 290
    if ((attribute & SERVICE_ATTR_SANDBOX) != SERVICE_ATTR_SANDBOX) {
        return;
    }
    INIT_ERROR_CHECK(execPath != NULL, return, "Service path is null.");
    if (strncmp(execPath, "/system/bin/", strlen("/system/bin/")) == 0) {
        if (strcmp(execPath, "/system/bin/appspawn") == 0) {
            INIT_LOGI("Appspawn skip enter sandbox.");
        } else if (strcmp(execPath, "/system/bin/hilogd") == 0) {
            INIT_LOGI("Hilogd skip enter sandbox.");
X
xionglei6 已提交
291
        } else {
M
Mupceet 已提交
292 293
            INIT_INFO_CHECK(EnterSandbox("system") == 0, return,
                "Service %s skip enter sandbox system.", execPath);
X
xionglei6 已提交
294
        }
X
xionglei6 已提交
295 296
    } else if (strncmp(execPath, "/vendor/bin/", strlen("/vendor/bin/")) == 0) {
        // chipset sandbox will be implemented later.
M
Mupceet 已提交
297
        INIT_INFO_CHECK(EnterSandbox("chipset") == 0, return,
M
Mupceet 已提交
298
            "Service %s skip enter sandbox system.", execPath);
X
xionglei6 已提交
299
    } else {
M
Mupceet 已提交
300
        INIT_LOGI("Service %s does not enter sandbox", execPath);
X
xionglei6 已提交
301
    }
X
xionglei6 已提交
302
    return;
X
xionglei6 已提交
303
}