1 /* 2 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #ifndef BASE_STARTUP_INITLITE_SERVICE_H 16 #define BASE_STARTUP_INITLITE_SERVICE_H 17 #include <stdbool.h> 18 #include <stdint.h> 19 #include <sys/types.h> 20 #include <sched.h> 21 #include <stdio.h> 22 23 #include "cJSON.h" 24 #include "init_cmds.h" 25 #include "init_error.h" 26 #include "init_service_file.h" 27 #include "init_service_socket.h" 28 #include "list.h" 29 #include "loop_event.h" 30 #ifdef __cplusplus 31 #if __cplusplus 32 extern "C" { 33 #endif 34 #endif 35 36 // return value 37 #define SERVICE_FAILURE (-1) 38 #define SERVICE_SUCCESS 0 39 40 // service attributes 41 #define SERVICE_ATTR_INVALID 0x001 // option invalid 42 #define SERVICE_ATTR_ONCE 0x002 // do not restart when it exits 43 #define SERVICE_ATTR_NEED_RESTART 0x004 // will restart in the near future 44 #define SERVICE_ATTR_NEED_STOP 0x008 // will stop in reap 45 #define SERVICE_ATTR_IMPORTANT 0x010 // will reboot if it crash 46 #define SERVICE_ATTR_CRITICAL 0x020 // critical, will reboot if it crash 4 times in 4 minutes 47 #define SERVICE_ATTR_DISABLED 0x040 // disabled 48 #define SERVICE_ATTR_CONSOLE 0x080 // console 49 #define SERVICE_ATTR_ONDEMAND 0x100 // ondemand, manage socket by init 50 #define SERVICE_ATTR_TIMERSTART 0x200 // Mark a service will be started by timer 51 #define SERVICE_ATTR_NEEDWAIT 0x400 // service should execute waitpid while stopping 52 #define SERVICE_ATTR_WITHOUT_SANDBOX 0x800 // make service not enter sandbox 53 54 #define SERVICE_ATTR_NOTIFY_STATE 0x1000 // service notify state 55 #define SERVICE_ATTR_MODULE_UPDATE 0x2000 // module update 56 #define SERVICE_ATTR_KMSG 0x4000 // kmsg 57 58 #define MAX_SERVICE_NAME 32 59 #define MAX_APL_NAME 32 60 #define MAX_ENV_NAME 64 61 #define MAX_JOB_NAME 128 62 #define MAX_WRITEPID_FILES 100 63 64 #define FULL_CAP 0xFFFFFFFF 65 // init 66 #define DEFAULT_UMASK_INIT 022 67 68 #define CAP_NUM 2 69 70 #define SERVICES_ARR_NAME_IN_JSON "services" 71 72 #define IsOnDemandService(service) \ 73 (((service)->attribute & SERVICE_ATTR_ONDEMAND) == SERVICE_ATTR_ONDEMAND) 74 75 #define MarkServiceAsOndemand(service) \ 76 ((service)->attribute |= SERVICE_ATTR_ONDEMAND) 77 78 #define UnMarkServiceAsOndemand(service) \ 79 ((service)->attribute &= ~SERVICE_ATTR_ONDEMAND) 80 81 #define IsServiceWithTimerEnabled(service) \ 82 (((service)->attribute & SERVICE_ATTR_TIMERSTART) == SERVICE_ATTR_TIMERSTART) 83 84 #define DisableServiceTimer(service) \ 85 ((service)->attribute &= ~SERVICE_ATTR_TIMERSTART) 86 87 #define EnableServiceTimer(service) \ 88 ((service)->attribute |= SERVICE_ATTR_TIMERSTART) 89 90 #define MarkServiceWithoutSandbox(service) \ 91 ((service)->attribute |= SERVICE_ATTR_WITHOUT_SANDBOX) 92 93 #define MarkServiceWithSandbox(service) \ 94 ((service)->attribute &= ~SERVICE_ATTR_WITHOUT_SANDBOX) 95 96 #pragma pack(4) 97 typedef enum { 98 START_MODE_CONDITION, 99 START_MODE_BOOT, 100 START_MODE_NORMAL, 101 } ServiceStartMode; 102 103 typedef enum { 104 END_PRE_FORK, 105 END_AFTER_FORK, 106 END_AFTER_EXEC, 107 END_RECV_READY, 108 } ServiceEndMode; 109 110 typedef struct { 111 uid_t uID; 112 gid_t *gIDArray; 113 int gIDCnt; 114 unsigned int *caps; 115 unsigned int capsCnt; 116 } Perms; 117 118 typedef struct { 119 int count; 120 char **argv; 121 } ServiceArgs; 122 123 typedef enum { 124 JOB_ON_BOOT, 125 JOB_ON_START, 126 JOB_ON_STOP, 127 JOB_ON_RESTART, 128 JOB_ON_MAX 129 } ServiceJobType; 130 131 typedef struct { 132 char *jobsName[JOB_ON_MAX]; 133 } ServiceJobs; 134 135 typedef struct Service_ { 136 char *name; 137 int pid; 138 int crashCnt; 139 uint32_t firstCrashTime; 140 int crashCount; 141 int crashTime; 142 unsigned int attribute; 143 int importance; 144 int startMode : 4; // startCondition/ startBoot / startNormal 145 int endMode : 4; // preFork/ fork / exec / ready 146 int status : 4; // ServiceStatus 147 uint64_t tokenId; 148 char *apl; 149 ServiceArgs capsArgs; 150 ServiceArgs permArgs; 151 ServiceArgs permAclsArgs; 152 Perms servPerm; 153 ServiceArgs pathArgs; 154 ServiceArgs writePidArgs; 155 CmdLines *restartArg; 156 ServiceSocket *socketCfg; 157 ServiceFile *fileCfg; 158 int *fds; 159 size_t fdCount; 160 TimerHandle timer; 161 ServiceJobs serviceJobs; 162 cpu_set_t *cpuSet; 163 struct ListNode extDataNode; 164 ConfigContext context; 165 InitErrno lastErrno; 166 } Service; 167 #pragma pack() 168 169 Service *GetServiceByPid(pid_t pid); 170 Service *GetServiceByName(const char *servName); 171 int ServiceStart(Service *service, ServiceArgs *pathArgs); 172 int ServiceStop(Service *service); 173 void ServiceReap(Service *service); 174 void ReapService(Service *service); 175 176 void NotifyServiceChange(Service *service, int status); 177 int IsForbidden(const char *fieldStr); 178 int SetImportantValue(Service *curServ, const char *attrName, int value, int flag); 179 int InitServiceCaps(const cJSON *curArrItem, Service *curServ); 180 int ServiceExec(Service *service, const ServiceArgs *pathArgs); 181 void CloseServiceFds(Service *service, bool needFree); 182 int UpdaterServiceFds(Service *service, int *fds, size_t fdCount); 183 int SetAccessToken(const Service *service); 184 void GetAccessToken(void); 185 void ServiceStopTimer(Service *service); 186 void ServiceStartTimer(Service *service, uint64_t timeout); 187 void IsEnableSandbox(void); 188 void EnterServiceSandbox(Service *service); 189 int SetServiceEnterSandbox(const Service *service, const char *execPath); 190 191 int CreateServiceFile(Service *service); 192 void CloseServiceFile(ServiceFile *fileOpt); 193 #ifdef __cplusplus 194 #if __cplusplus 195 } 196 #endif 197 #endif 198 199 #endif // BASE_STARTUP_INITLITE_SERVICE_H 200