init_service.c 6.9 KB
Newer Older
W
wenjun 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Copyright (c) 2020 Huawei Device Co., Ltd.
 * 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_service.h"
17

W
wenjun 已提交
18 19 20
#include <errno.h>
#include <signal.h>
#include <stdio.h>
M
mamingshuai 已提交
21
#include <string.h>
W
wenjun 已提交
22 23 24
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
25

W
wenjun 已提交
26 27 28 29 30 31 32 33 34 35
#include "init_adapter.h"
#include "init_perms.h"

#define CAP_NUM 2

// 240 seconds, 4 minutes
static const int CRASH_TIME_LIMIT  = 240;
// maximum number of crashes within time CRASH_TIME_LIMIT for one service
static const int CRASH_COUNT_LIMIT = 4;

M
mamingshuai 已提交
36 37 38 39 40 41 42 43 44 45
static int SetAllAmbientCapability()
{
    for (int i = 0; i <= CAP_LAST_CAP; ++i) {
        if (SetAmbientCapability(i) != 0) {
            return SERVICE_FAILURE;
        }
    }
    return SERVICE_SUCCESS;
}

W
wenjun 已提交
46 47 48 49 50 51 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
static int SetPerms(const Service *service)
{
    if (KeepCapability() != 0) {
        return SERVICE_FAILURE;
    }

    if (setgid(service->servPerm.gID) != 0) {
        return SERVICE_FAILURE;
    }

    if (setuid(service->servPerm.uID) != 0) {
        return SERVICE_FAILURE;
    }

    // umask call always succeeds and return the previous mask value which is not needed here
    (void)umask(DEFAULT_UMASK_INIT);

    struct __user_cap_header_struct capHeader;
    capHeader.version = _LINUX_CAPABILITY_VERSION_3;
    capHeader.pid = 0;

    struct __user_cap_data_struct capData[CAP_NUM] = {0};
    for (unsigned int i = 0; i < service->servPerm.capsCnt; ++i) {
        if (service->servPerm.caps[i] == FULL_CAP) {
            for (int i = 0; i < CAP_NUM; ++i) {
                capData[i].effective = FULL_CAP;
                capData[i].permitted = FULL_CAP;
                capData[i].inheritable = FULL_CAP;
            }
            break;
        }
        capData[CAP_TO_INDEX(service->servPerm.caps[i])].effective |= CAP_TO_MASK(service->servPerm.caps[i]);
        capData[CAP_TO_INDEX(service->servPerm.caps[i])].permitted |= CAP_TO_MASK(service->servPerm.caps[i]);
        capData[CAP_TO_INDEX(service->servPerm.caps[i])].inheritable |= CAP_TO_MASK(service->servPerm.caps[i]);
    }

    if (capset(&capHeader, capData) != 0) {
        return SERVICE_FAILURE;
    }
M
mamingshuai 已提交
85 86 87 88 89 90 91 92
    for (unsigned int i = 0; i < service->servPerm.capsCnt; ++i) {
        if (service->servPerm.caps[i] == FULL_CAP) {
            return SetAllAmbientCapability();
        }
        if (SetAmbientCapability(service->servPerm.caps[i]) != 0) {
            return SERVICE_FAILURE;
        }
    }
W
wenjun 已提交
93 94 95 96 97
    return SERVICE_SUCCESS;
}

int ServiceStart(Service *service)
{
M
mamingshuai 已提交
98 99 100 101 102
    if (service == NULL) {
        printf("[Init] start service failed! null ptr.\n");
        return SERVICE_FAILURE;
    }

W
wenjun 已提交
103 104 105 106 107 108 109
    if (service->attribute & SERVICE_ATTR_INVALID) {
        printf("[Init] start service %s invalid.\n", service->name);
        return SERVICE_FAILURE;
    }

    struct stat pathStat = {0};
    service->attribute &= (~(SERVICE_ATTR_NEED_RESTART | SERVICE_ATTR_NEED_STOP));
M
mamingshuai 已提交
110
    if (stat(service->pathArgs[0], &pathStat) != 0) {
W
wenjun 已提交
111
        service->attribute |= SERVICE_ATTR_INVALID;
M
mamingshuai 已提交
112 113
        printf("[Init] start service %s invalid, please check %s.\n",\
            service->name, service->pathArgs[0]);
W
wenjun 已提交
114 115 116 117 118 119 120 121 122 123 124
        return SERVICE_FAILURE;
    }

    int pid = fork();
    if (pid == 0) {
        // permissions
        if (SetPerms(service) != SERVICE_SUCCESS) {
            printf("[Init] service %s exit! set perms failed! err %d.\n", service->name, errno);
            _exit(0x7f); // 0x7f: user specified
        }

C
changcheng-weng 已提交
125
        char* env[] = {"LD_LIBRARY_PATH=/storage/app/libs", NULL};
M
mamingshuai 已提交
126
        if (execve(service->pathArgs[0], service->pathArgs, env) != 0) {
W
wenjun 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
            printf("[Init] service %s execve failed! err %d.\n", service->name, errno);
        }
        _exit(0x7f); // 0x7f: user specified
    } else if (pid < 0) {
        printf("[Init] start service %s fork failed!\n", service->name);
        return SERVICE_FAILURE;
    }

    service->pid = pid;
    printf("[Init] start service %s succeed, pid %d.\n", service->name, service->pid);
    return SERVICE_SUCCESS;
}

int ServiceStop(Service *service)
{
M
mamingshuai 已提交
142 143 144 145 146
    if (service == NULL) {
        printf("[Init] stop service failed! null ptr.\n");
        return SERVICE_FAILURE;
    }

W
wenjun 已提交
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
    service->attribute &= ~SERVICE_ATTR_NEED_RESTART;
    service->attribute |= SERVICE_ATTR_NEED_STOP;
    if (service->pid <= 0) {
        return SERVICE_SUCCESS;
    }

    if (kill(service->pid, SIGKILL) != 0) {
        printf("[Init] stop service %s pid %d failed! err %d.\n", service->name, service->pid, errno);
        return SERVICE_FAILURE;
    }

    printf("[Init] stop service %s, pid %d.\n", service->name, service->pid);
    return SERVICE_SUCCESS;
}

void ServiceReap(Service *service)
{
M
mamingshuai 已提交
164 165 166 167 168
    if (service == NULL) {
        printf("[Init] reap service failed! null ptr.\n");
        return;
    }

W
wenjun 已提交
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
    service->pid = -1;

    // stopped by system-init itself, no need to restart even if it is not one-shot service
    if (service->attribute & SERVICE_ATTR_NEED_STOP) {
        service->attribute &= (~SERVICE_ATTR_NEED_STOP);
        service->crashCnt = 0;
        return;
    }

    // for one-shot service
    if (service->attribute & SERVICE_ATTR_ONCE) {
        // no need to restart
        if (!(service->attribute & SERVICE_ATTR_NEED_RESTART)) {
            service->attribute &= (~SERVICE_ATTR_NEED_STOP);
            return;
        }
        // the service could be restart even if it is one-shot service
    }

    // the service that does not need to be restarted restarts, indicating that it has crashed
    if (!(service->attribute & SERVICE_ATTR_NEED_RESTART)) {
        // crash time and count check
        time_t curTime = time(NULL);
        if (service->crashCnt == 0) {
            service->firstCrashTime = curTime;
            ++service->crashCnt;
        } else if (difftime(curTime, service->firstCrashTime) > CRASH_TIME_LIMIT) {
            service->firstCrashTime = curTime;
            service->crashCnt = 1;
        } else {
            ++service->crashCnt;
            if (service->crashCnt > CRASH_COUNT_LIMIT) {
                printf("[Init] reap service %s, crash too many times!\n", service->name);
                return;
            }
        }
    }

    int ret = ServiceStart(service);
    if (ret != SERVICE_SUCCESS) {
        printf("[Init] reap service %s start failed!\n", service->name);
    }

    service->attribute &= (~SERVICE_ATTR_NEED_RESTART);
}
M
mamingshuai 已提交
214