init.c 9.2 KB
Newer Older
1
/*
X
xionglei6 已提交
2
 * Copyright (c) 2021 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 21 22 23
#include <stdlib.h>
#include <signal.h>
#include <sys/sysmacros.h>
#include <sys/stat.h>
#include <sys/types.h>
X
xionglei6 已提交
24
#include <sys/socket.h>
4
411148299@qq.com 已提交
25
#include <linux/major.h>
26
#include "device.h"
X
xionglei6 已提交
27
#include "fd_holder_service.h"
X
xionglei6 已提交
28
#include "fs_manager/fs_manager.h"
29 30
#include "init_log.h"
#include "init_mount.h"
X
xionglei6 已提交
31
#include "init_group_manager.h"
32
#include "init_param.h"
X
xionglei6 已提交
33 34
#include "init_service.h"
#include "init_service_manager.h"
35 36 37
#include "init_utils.h"
#include "securec.h"
#include "switch_root.h"
X
xionglei6 已提交
38 39
#include "ueventd.h"
#include "ueventd_socket.h"
X
xionglei6 已提交
40
#include "fd_holder_internal.h"
Q
Qin Fandong 已提交
41
#ifdef WITH_SELINUX
X
xionglei6 已提交
42
#include <policycoreutils.h>
R
renwei 已提交
43
#include <selinux/selinux.h>
Q
Qin Fandong 已提交
44
#endif // WITH_SELINUX
45

X
xionglei6 已提交
46 47 48 49
static int FdHolderSockInit(void)
{
    int sock = -1;
    int on = 1;
X
xionglei6 已提交
50 51
    int fdHolderBufferSize = FD_HOLDER_BUFFER_SIZE; // 4KiB
    sock = socket(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
X
xionglei6 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
    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;
    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) {
        INIT_LOGE("Failed to binder fd folder socket");
        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;
}

90 91 92 93
void SystemInit(void)
{
    SignalInit();
    MakeDirRecursive("/dev/unix/socket", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
X
xionglei6 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
    int sock = FdHolderSockInit();
    if (sock >= 0) {
        RegisterFdHoldWatcher(sock);
    }
}

static void EnableDevKmsg(void)
{
    /* printk_devkmsg default value is ratelimit, We need to set "on" and remove the restrictions */
    int fd = open("/proc/sys/kernel/printk_devkmsg", O_WRONLY | O_CLOEXEC, S_IRUSR | S_IWUSR | S_IRGRP | S_IRGRP);
    if (fd < 0) {
        return;
    }
    char *kmsgStatus = "on";
    write(fd, kmsgStatus, strlen(kmsgStatus) + 1);
    close(fd);
    fd = -1;
    return;
112 113 114 115 116 117 118 119 120 121 122 123
}

void LogInit(void)
{
    CloseStdio();
    int ret = mknod("/dev/kmsg", S_IFCHR | S_IWUSR | S_IRUSR,
        makedev(MEM_MAJOR, DEV_KMSG_MINOR));
    if (ret == 0) {
        OpenLogDevice();
    }
}

X
xionglei6 已提交
124
static char **GetRequiredDevices(Fstab fstab, int *requiredNum)
125
{
X
xionglei6 已提交
126 127 128 129 130 131 132
    int num = 0;
    FstabItem *item = fstab.head;
    while (item != NULL) {
        if (FM_MANAGER_REQUIRED_ENABLED(item->fsManagerFlags)) {
            num++;
        }
        item = item->next;
133
    }
X
xionglei6 已提交
134 135 136 137 138 139 140 141 142 143 144
    char **devices = (char **)calloc(num, sizeof(char *));
    INIT_ERROR_CHECK(devices != NULL, return NULL, "Failed calloc err=%d", errno);

    int i = 0;
    item = fstab.head;
    while (item != NULL) {
        if (FM_MANAGER_REQUIRED_ENABLED(item->fsManagerFlags)) {
            devices[i] = strdup(item->deviceName);
            INIT_ERROR_CHECK(devices[i] != NULL, FreeStringVector(devices, num); return NULL,
                "Failed strdup err=%d", errno);
            i++;
145
        }
X
xionglei6 已提交
146
        item = item->next;
147
    }
X
xionglei6 已提交
148 149
    *requiredNum = num;
    return devices;
150 151
}

X
xionglei6 已提交
152
static int StartUeventd(char **requiredDevices, int num)
153
{
X
xionglei6 已提交
154 155 156 157 158
    INIT_ERROR_CHECK(requiredDevices != NULL && num > 0, return -1, "Failed parameters");
    int ueventSockFd = UeventdSocketInit();
    if (ueventSockFd < 0) {
        INIT_LOGE("Failed to create uevent socket");
        return -1;
159
    }
X
xionglei6 已提交
160 161 162 163 164 165 166 167
    RetriggerUevent(ueventSockFd, requiredDevices, num);
    return 0;
}

static void StartInitSecondStage(void)
{
    const char *fstabFile = "/etc/fstab.required";
    Fstab *fstab = NULL;
X
xionglei6 已提交
168 169 170
    if (access(fstabFile, F_OK) != 0) {
        fstabFile = "/system/etc/fstab.required";
    }
X
xionglei6 已提交
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
    INIT_ERROR_CHECK(access(fstabFile, F_OK) == 0, abort(), "Failed get fstab.required");
    fstab = ReadFstabFromFile(fstabFile, false);
    INIT_ERROR_CHECK(fstab != NULL, abort(), "Read fstab file \" %s \" failed\n", fstabFile);

    int requiredNum = 0;
    char **devices = GetRequiredDevices(*fstab, &requiredNum);
    if (devices != NULL && requiredNum > 0) {
        int ret = StartUeventd(devices, requiredNum);
        if (ret == 0) {
            ret = MountRequriedPartitions(fstab);
        }
        FreeStringVector(devices, requiredNum);
        devices = NULL;
        ReleaseFstab(fstab);
        fstab = NULL;
        if (ret < 0) {
            // If mount required partitions failure.
            // There is no necessary to continue.
            // Just abort
X
xionglei6 已提交
190 191 192
            INIT_LOGE("Mount requried partitions failed; please check fstab file");
            // Execute sh for debugging
            execv("/bin/sh", NULL);
X
xionglei6 已提交
193 194
            abort();
        }
195
    }
X
xionglei6 已提交
196
#ifndef DISABLE_INIT_TWO_STAGES
197 198
    SwitchRoot("/usr");
    // Execute init second stage
X
xionglei6 已提交
199
    char * const args[] = {
200 201
        "/bin/init",
        "--second-stage",
S
sun_fan 已提交
202
        NULL,
203 204 205 206 207
    };
    if (execv("/bin/init", args) != 0) {
        INIT_LOGE("Failed to exec \"/bin/init\", err = %d", errno);
        exit(-1);
    }
S
sun_fan 已提交
208
#endif
X
xionglei6 已提交
209
}
210 211 212 213

void SystemPrepare(void)
{
    MountBasicFs();
X
xionglei6 已提交
214
    LogInit();
215 216 217 218 219 220
    // Make sure init log always output to /dev/kmsg.
    EnableDevKmsg();
    CreateDeviceNode();
    // Only ohos normal system support
    // two stages of init.
    // If we are in updater mode, only one stage of init,
S
sun_fan 已提交
221
    INIT_LOGI("DISABLE_INIT_TWO_STAGES not defined");
222 223 224 225 226
    if (InUpdaterMode() == 0) {
        StartInitSecondStage();
    }
}

Q
Qin Fandong 已提交
227 228 229 230 231 232 233 234 235
void SystemLoadSelinux(void)
{
#ifdef WITH_SELINUX
    // load selinux policy and context
    if (load_policy() < 0) {
        INIT_LOGE("main, load_policy failed.");
    } else {
        INIT_LOGI("main, load_policy success.");
    }
R
renwei 已提交
236 237

    setcon("u:r:init:s0");
Q
Qin Fandong 已提交
238 239 240
#endif // WITH_SELINUX
}

X
xionglei6 已提交
241 242 243 244
static void BootStateChange(const char *content)
{
    INIT_LOGI("boot start %s finish.", content);
    if (strcmp("init", content) == 0) {
X
xionglei6 已提交
245 246 247 248 249 250
        static const char *bootServiceNames[] = {
            "hdf_devmgr", "samgr", "appspawn", "faultloggerd"
        };
        for (int i = 0; i < ARRAY_LENGTH(bootServiceNames); i++) {
            StartServiceByName(bootServiceNames[i], 0);
        }
X
xionglei6 已提交
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
        return;
    }
    if (strcmp("post-init", content) == 0) {
        StartAllServices(START_MODE_NARMAL);
        return;
    }
}

#if defined(OHOS_SERVICE_DUMP)
static void SystemDump(int id, const char *name, int argc, const char **argv)
{
    INIT_ERROR_CHECK(argv != NULL && argc >= 1, return, "Invalid install parameter");
    INIT_LOGI("Dump system info %s", argv[0]);
    DumpAllServices();
    DumpParametersAndTriggers();
}
#endif

269 270
void SystemConfig(void)
{
X
xionglei6 已提交
271 272 273
    InitServiceSpace();
    InitParseGroupCfg();
    PluginManagerInit();
R
renwei 已提交
274

275
    InitParamService();
X
xionglei6 已提交
276 277
    RegisterBootStateChange(BootStateChange);

278 279 280 281 282 283 284 285 286
    // parse parameters
    LoadDefaultParams("/system/etc/param/ohos_const", LOAD_PARAM_NORMAL);
    LoadDefaultParams("/vendor/etc/param", LOAD_PARAM_NORMAL);
    LoadDefaultParams("/system/etc/param", LOAD_PARAM_ONLY_ADD);
    // read config
    ReadConfig();
    INIT_LOGI("Parse init config file done.");

    // dump config
X
xionglei6 已提交
287 288 289
#if defined(OHOS_SERVICE_DUMP)
    AddCmdExecutor("display", SystemDump);
    (void)AddCompleteJob("param:ohos.servicectrl.display", "ohos.servicectrl.display=*", "display system");
290 291 292 293 294 295
#endif

    // 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"));
X
xionglei6 已提交
296 297
    // load SELinux context and policy
    SystemLoadSelinux();
298 299 300 301 302 303
}

void SystemRun(void)
{
    StartParamService();
}