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_service_file.h" 26 #include "init_service_socket.h" 27 #include "list.h" 28 #include "loop_event.h" 29 #ifdef __cplusplus 30 #if __cplusplus 31 extern "C" { 32 #endif 33 #endif 34 35 // return value 36 #define SERVICE_FAILURE (-1) 37 #define SERVICE_SUCCESS 0 38 39 // service attributes 40 #define SERVICE_ATTR_INVALID 0x001 // option invalid 41 #define SERVICE_ATTR_ONCE 0x002 // do not restart when it exits 42 #define SERVICE_ATTR_NEED_RESTART 0x004 // will restart in the near future 43 #define SERVICE_ATTR_NEED_STOP 0x008 // will stop in reap 44 #define SERVICE_ATTR_IMPORTANT 0x010 // will reboot if it crash 45 #define SERVICE_ATTR_CRITICAL 0x020 // critical, will reboot if it crash 4 times in 4 minutes 46 #define SERVICE_ATTR_DISABLED 0x040 // disabled 47 #define SERVICE_ATTR_CONSOLE 0x080 // console 48 #define SERVICE_ATTR_ONDEMAND 0x100 // ondemand, manage socket by init 49 #define SERVICE_ATTR_TIMERSTART 0x200 // Mark a service will be started by timer 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 53 #define SERVICE_ATTR_NOTIFY_STATE 0x1000 // service notify state 54 55 #define MAX_SERVICE_NAME 32 56 #define MAX_APL_NAME 32 57 #define MAX_ENV_NAME 64 58 #define MAX_JOB_NAME 128 59 #define MAX_WRITEPID_FILES 100 60 61 #define FULL_CAP 0xFFFFFFFF 62 // init 63 #define DEFAULT_UMASK_INIT 022 64 65 #define CAP_NUM 2 66 67 #define SERVICES_ARR_NAME_IN_JSON "services" 68 69 #define IsOnDemandService(service) \ 70 (((service)->attribute & SERVICE_ATTR_ONDEMAND) == SERVICE_ATTR_ONDEMAND) 71 72 #define MarkServiceAsOndemand(service) \ 73 ((service)->attribute |= SERVICE_ATTR_ONDEMAND) 74 75 #define UnMarkServiceAsOndemand(service) \ 76 ((service)->attribute &= ~SERVICE_ATTR_ONDEMAND) 77 78 #define IsServiceWithTimerEnabled(service) \ 79 (((service)->attribute & SERVICE_ATTR_TIMERSTART) == SERVICE_ATTR_TIMERSTART) 80 81 #define DisableServiceTimer(service) \ 82 ((service)->attribute &= ~SERVICE_ATTR_TIMERSTART) 83 84 #define EnableServiceTimer(service) \ 85 ((service)->attribute |= SERVICE_ATTR_TIMERSTART) 86 87 #define MarkServiceWithoutSandbox(service) \ 88 ((service)->attribute |= SERVICE_ATTR_WITHOUT_SANDBOX) 89 90 #define MarkServiceWithSandbox(service) \ 91 ((service)->attribute &= ~SERVICE_ATTR_WITHOUT_SANDBOX) 92 93 #pragma pack(4) 94 typedef enum { 95 START_MODE_CONDITION, 96 START_MODE_BOOT, 97 START_MODE_NORMAL, 98 } ServiceStartMode; 99 100 typedef enum { 101 END_PRE_FORK, 102 END_AFTER_FORK, 103 END_AFTER_EXEC, 104 END_RECV_READY, 105 } ServiceEndMode; 106 107 typedef struct { 108 uid_t uID; 109 gid_t *gIDArray; 110 int gIDCnt; 111 unsigned int *caps; 112 unsigned int capsCnt; 113 } Perms; 114 115 typedef struct { 116 int count; 117 char **argv; 118 } ServiceArgs; 119 120 typedef enum { 121 JOB_ON_BOOT, 122 JOB_ON_START, 123 JOB_ON_STOP, 124 JOB_ON_RESTART, 125 JOB_ON_MAX 126 } ServiceJobType; 127 128 typedef struct { 129 char *jobsName[JOB_ON_MAX]; 130 } ServiceJobs; 131 132 typedef struct Service_ { 133 char *name; 134 int pid; 135 int crashCnt; 136 time_t firstCrashTime; 137 int crashCount; 138 int crashTime; 139 unsigned int attribute; 140 int importance; 141 int startMode : 4; // startCondition/ startBoot / startNormal 142 int endMode : 4; // preFork/ fork / exec / ready 143 int status : 4; // ServiceStatus 144 uint64_t tokenId; 145 char *apl; 146 ServiceArgs capsArgs; 147 ServiceArgs permArgs; 148 ServiceArgs permAclsArgs; 149 Perms servPerm; 150 ServiceArgs pathArgs; 151 ServiceArgs extraArgs; 152 ServiceArgs writePidArgs; 153 CmdLines *restartArg; 154 ServiceSocket *socketCfg; 155 ServiceFile *fileCfg; 156 int *fds; 157 size_t fdCount; 158 TimerHandle timer; 159 ServiceJobs serviceJobs; 160 cpu_set_t *cpuSet; 161 struct ListNode extDataNode; 162 } Service; 163 #pragma pack() 164 165 Service *GetServiceByPid(pid_t pid); 166 Service *GetServiceByName(const char *servName); 167 int ServiceStart(Service *service); 168 int ServiceStop(Service *service); 169 void ServiceReap(Service *service); 170 void ReapService(Service *service); 171 172 void NotifyServiceChange(Service *service, int status); 173 int IsForbidden(const char *fieldStr); 174 int SetImportantValue(Service *curServ, const char *attrName, int value, int flag); 175 int GetServiceCaps(const cJSON *curArrItem, Service *curServ); 176 int ServiceExec(const Service *service); 177 void CloseServiceFds(Service *service, bool needFree); 178 int UpdaterServiceFds(Service *service, int *fds, size_t fdCount); 179 int SetAccessToken(const Service *service); 180 void GetAccessToken(void); 181 void ServiceStopTimer(Service *service); 182 void ServiceStartTimer(Service *service, uint64_t timeout); 183 void IsEnableSandbox(void); 184 void EnterServiceSandbox(Service *service); 185 void SetServiceEnterSandbox(const char *execPath, unsigned int attribute); 186 #ifdef __cplusplus 187 #if __cplusplus 188 } 189 #endif 190 #endif 191 192 #endif // BASE_STARTUP_INITLITE_SERVICE_H 193