init_service.h 5.7 KB
Newer Older
1
/*
2
 * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3 4 5 6 7 8 9 10 11 12 13 14 15 16
 * 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.
 */
#ifndef BASE_STARTUP_INITLITE_SERVICE_H
#define BASE_STARTUP_INITLITE_SERVICE_H
17
#include <stdbool.h>
18
#include <stdint.h>
19
#include <sys/types.h>
X
xlei1030 已提交
20 21
#include <sched.h>
#include <stdio.h>
22 23 24

#include "cJSON.h"
#include "init_cmds.h"
X
xionglei6 已提交
25
#include "init_service_file.h"
26 27
#include "init_service_socket.h"
#include "list.h"
X
xionglei6 已提交
28
#include "loop_event.h"
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif
#endif

// return value
#define SERVICE_FAILURE (-1)
#define SERVICE_SUCCESS 0

// service attributes
#define SERVICE_ATTR_INVALID 0x001      // option invalid
#define SERVICE_ATTR_ONCE 0x002         // do not restart when it exits
#define SERVICE_ATTR_NEED_RESTART 0x004 // will restart in the near future
#define SERVICE_ATTR_NEED_STOP 0x008    // will stop in reap
#define SERVICE_ATTR_IMPORTANT 0x010    // will reboot if it crash
#define SERVICE_ATTR_CRITICAL 0x020     // critical, will reboot if it crash 4 times in 4 minutes
#define SERVICE_ATTR_DISABLED 0x040     // disabled
#define SERVICE_ATTR_CONSOLE 0x080      // console
X
xionglei6 已提交
48 49
#define SERVICE_ATTR_ONDEMAND 0x100     // ondemand, manage socket by init
#define SERVICE_ATTR_TIMERSTART 0x200   // Mark a service will be started by timer
X
xionglei6 已提交
50
#define SERVICE_ATTR_NEEDWAIT 0x400     // service should execute waitpid while stopping
51
#define SERVICE_ATTR_WITHOUT_SANDBOX 0x800     // make service not enter sandbox
52

C
cheng_jinsong 已提交
53
#define SERVICE_ATTR_NOTIFY_STATE 0x1000     // service notify state
54
#define SERVICE_ATTR_MODULE_UPDATE 0x2000     // module update
C
cheng_jinsong 已提交
55

56
#define MAX_SERVICE_NAME 32
X
xionglei6 已提交
57
#define MAX_APL_NAME 32
58
#define MAX_ENV_NAME 64
X
xionglei6 已提交
59
#define MAX_JOB_NAME 128
60 61 62 63 64 65 66 67 68 69
#define MAX_WRITEPID_FILES 100

#define FULL_CAP 0xFFFFFFFF
// init
#define DEFAULT_UMASK_INIT 022

#define CAP_NUM 2

#define SERVICES_ARR_NAME_IN_JSON "services"

X
xionglei6 已提交
70 71 72
#define IsOnDemandService(service) \
    (((service)->attribute & SERVICE_ATTR_ONDEMAND) == SERVICE_ATTR_ONDEMAND)

C
cheng_jinsong 已提交
73 74 75 76 77 78
#define MarkServiceAsOndemand(service) \
    ((service)->attribute |= SERVICE_ATTR_ONDEMAND)

#define UnMarkServiceAsOndemand(service) \
    ((service)->attribute &= ~SERVICE_ATTR_ONDEMAND)

X
xionglei6 已提交
79 80 81 82 83 84 85 86 87
#define IsServiceWithTimerEnabled(service) \
    (((service)->attribute & SERVICE_ATTR_TIMERSTART) == SERVICE_ATTR_TIMERSTART)

#define DisableServiceTimer(service) \
    ((service)->attribute &= ~SERVICE_ATTR_TIMERSTART)

#define EnableServiceTimer(service) \
    ((service)->attribute |= SERVICE_ATTR_TIMERSTART)

88 89
#define MarkServiceWithoutSandbox(service) \
    ((service)->attribute |= SERVICE_ATTR_WITHOUT_SANDBOX)
X
xionglei6 已提交
90

91 92
#define MarkServiceWithSandbox(service) \
    ((service)->attribute &= ~SERVICE_ATTR_WITHOUT_SANDBOX)
X
xionglei6 已提交
93

94
#pragma pack(4)
X
xionglei6 已提交
95 96 97
typedef enum {
    START_MODE_CONDITION,
    START_MODE_BOOT,
M
Mupceet 已提交
98
    START_MODE_NORMAL,
X
xionglei6 已提交
99 100 101 102 103 104 105 106 107
} ServiceStartMode;

typedef enum {
    END_PRE_FORK,
    END_AFTER_FORK,
    END_AFTER_EXEC,
    END_RECV_READY,
} ServiceEndMode;

108 109 110 111 112 113 114 115 116 117 118 119 120
typedef struct {
    uid_t uID;
    gid_t *gIDArray;
    int gIDCnt;
    unsigned int *caps;
    unsigned int capsCnt;
} Perms;

typedef struct {
    int count;
    char **argv;
} ServiceArgs;

X
xionglei6 已提交
121 122 123 124 125 126 127 128
typedef enum {
    JOB_ON_BOOT,
    JOB_ON_START,
    JOB_ON_STOP,
    JOB_ON_RESTART,
    JOB_ON_MAX
} ServiceJobType;

129
typedef struct {
X
xionglei6 已提交
130 131 132 133 134
    char *jobsName[JOB_ON_MAX];
} ServiceJobs;

typedef struct Service_ {
    char *name;
135 136 137
    int pid;
    int crashCnt;
    time_t firstCrashTime;
X
xionglei6 已提交
138 139
    int crashCount;
    int crashTime;
140 141
    unsigned int attribute;
    int importance;
X
xionglei6 已提交
142 143
    int startMode : 4; // startCondition/ startBoot / startNormal
    int endMode : 4; // preFork/ fork / exec / ready
X
xionglei6 已提交
144
    int status : 4; // ServiceStatus
145
    uint64_t tokenId;
146
    char *apl;
147
    ServiceArgs capsArgs;
Y
yichengzhao 已提交
148
    ServiceArgs permArgs;
Y
yichengzhao 已提交
149
    ServiceArgs permAclsArgs;
150 151
    Perms servPerm;
    ServiceArgs pathArgs;
X
xionglei6 已提交
152
    ServiceArgs extraArgs;
153 154 155
    ServiceArgs writePidArgs;
    CmdLines *restartArg;
    ServiceSocket *socketCfg;
X
xionglei6 已提交
156
    ServiceFile *fileCfg;
X
xionglei6 已提交
157 158
    int *fds;
    size_t fdCount;
X
xionglei6 已提交
159
    TimerHandle timer;
X
xionglei6 已提交
160
    ServiceJobs serviceJobs;
C
cheng_jinsong 已提交
161
    cpu_set_t *cpuSet;
M
Mupceet 已提交
162
    struct ListNode extDataNode;
163
} Service;
164
#pragma pack()
165

M
Mupceet 已提交
166 167
Service *GetServiceByPid(pid_t pid);
Service *GetServiceByName(const char *servName);
168 169 170 171 172
int ServiceStart(Service *service);
int ServiceStop(Service *service);
void ServiceReap(Service *service);
void ReapService(Service *service);

X
xionglei6 已提交
173
void NotifyServiceChange(Service *service, int status);
174 175
int IsForbidden(const char *fieldStr);
int SetImportantValue(Service *curServ, const char *attrName, int value, int flag);
C
cheng_jinsong 已提交
176
int InitServiceCaps(const cJSON *curArrItem, Service *curServ);
X
xionglei6 已提交
177
int ServiceExec(const Service *service);
X
xionglei6 已提交
178 179
void CloseServiceFds(Service *service, bool needFree);
int UpdaterServiceFds(Service *service, int *fds, size_t fdCount);
180 181
int SetAccessToken(const Service *service);
void GetAccessToken(void);
X
xionglei6 已提交
182 183
void ServiceStopTimer(Service *service);
void ServiceStartTimer(Service *service, uint64_t timeout);
C
cheng_jinsong 已提交
184
void IsEnableSandbox(void);
M
Mupceet 已提交
185
void EnterServiceSandbox(Service *service);
C
cheng_jinsong 已提交
186
void SetServiceEnterSandbox(const char *execPath, unsigned int attribute);
187 188 189 190 191 192
#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif

193
#endif // BASE_STARTUP_INITLITE_SERVICE_H