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 #define SERVICE_ATTR_PERIOD 0x8000    //service period
58 
59 #define SERVICE_ATTR_SETUID 0x10000 //service can setuid with no fixup
60 
61 #define MAX_SERVICE_NAME 32
62 #define MAX_APL_NAME 32
63 #define MAX_ENV_NAME 64
64 #define MAX_JOB_NAME 128
65 #define MAX_WRITEPID_FILES 100
66 #define MAX_ENV_VALUE 128
67 
68 #define FULL_CAP 0xFFFFFFFF
69 // init
70 #define DEFAULT_UMASK_INIT 022
71 
72 #define CAP_NUM 2
73 
74 #define SERVICES_ARR_NAME_IN_JSON "services"
75 
76 #define IsOnDemandService(service) \
77     (((service)->attribute & SERVICE_ATTR_ONDEMAND) == SERVICE_ATTR_ONDEMAND)
78 
79 #define MarkServiceAsOndemand(service) \
80     ((service)->attribute |= SERVICE_ATTR_ONDEMAND)
81 
82 #define UnMarkServiceAsOndemand(service) \
83     ((service)->attribute &= ~SERVICE_ATTR_ONDEMAND)
84 
85 #define IsServiceWithTimerEnabled(service) \
86     (((service)->attribute & SERVICE_ATTR_TIMERSTART) == SERVICE_ATTR_TIMERSTART)
87 
88 #define DisableServiceTimer(service) \
89     ((service)->attribute &= ~SERVICE_ATTR_TIMERSTART)
90 
91 #define EnableServiceTimer(service) \
92     ((service)->attribute |= SERVICE_ATTR_TIMERSTART)
93 
94 #define MarkServiceWithoutSandbox(service) \
95     ((service)->attribute |= SERVICE_ATTR_WITHOUT_SANDBOX)
96 
97 #define MarkServiceWithSandbox(service) \
98     ((service)->attribute &= ~SERVICE_ATTR_WITHOUT_SANDBOX)
99 
100 #pragma pack(4)
101 typedef enum {
102     START_MODE_CONDITION,
103     START_MODE_BOOT,
104     START_MODE_NORMAL,
105 } ServiceStartMode;
106 
107 typedef enum {
108     END_PRE_FORK,
109     END_AFTER_FORK,
110     END_AFTER_EXEC,
111     END_RECV_READY,
112 } ServiceEndMode;
113 
114 typedef struct {
115     uid_t uID;
116     gid_t *gIDArray;
117     int gIDCnt;
118     unsigned int *caps;
119     unsigned int capsCnt;
120 } Perms;
121 
122 typedef struct {
123     int count;
124     char **argv;
125 } ServiceArgs;
126 
127 typedef enum {
128     JOB_ON_BOOT,
129     JOB_PRE_START,
130     JOB_ON_START,
131     JOB_ON_STOP,
132     JOB_ON_RESTART,
133     JOB_ON_MAX
134 } ServiceJobType;
135 
136 typedef struct {
137     char *jobsName[JOB_ON_MAX];
138 } ServiceJobs;
139 
140 typedef struct {
141     char name[MAX_ENV_NAME];
142     char value[MAX_ENV_VALUE];
143 } ServiceEnv;
144 
145 typedef struct Service {
146     char *name;
147     int pid;
148     int crashCnt;
149     uint32_t firstCrashTime;
150     int crashCount;
151     int crashTime;
152     unsigned int attribute;
153     int importance;
154     int startMode : 4; // startCondition/ startBoot / startNormal
155     int endMode : 4; // preFork/ fork / exec / ready
156     int status : 4; // ServiceStatus
157     uint64_t tokenId;
158     char *apl;
159     uint64_t period;
160     ServiceEnv *env;
161     int envCnt;
162     ServiceArgs capsArgs;
163     ServiceArgs permArgs;
164     ServiceArgs permAclsArgs;
165     Perms servPerm;
166     ServiceArgs pathArgs;
167     ServiceArgs writePidArgs;
168     CmdLines *restartArg;
169     ServiceSocket *socketCfg;
170     ServiceFile *fileCfg;
171     int *fds;
172     size_t fdCount;
173     TimerHandle timer;
174     ServiceJobs serviceJobs;
175     cpu_set_t *cpuSet;
176     struct ListNode extDataNode;
177     ConfigContext context;
178     InitErrno lastErrno;
179 } Service;
180 #pragma pack()
181 
182 Service *GetServiceByPid(pid_t pid);
183 Service *GetServiceByName(const char *servName);
184 int ServiceStart(Service *service, ServiceArgs *pathArgs);
185 int ServiceStop(Service *service);
186 void ServiceReap(Service *service);
187 void ReapService(Service *service);
188 
189 void NotifyServiceChange(Service *service, int status);
190 int IsForbidden(const char *fieldStr);
191 int SetImportantValue(Service *curServ, const char *attrName, int value, int flag);
192 int InitServiceCaps(const cJSON *curArrItem, Service *curServ);
193 int ServiceExec(Service *service, const ServiceArgs *pathArgs);
194 void CloseServiceFds(Service *service, bool needFree);
195 int UpdaterServiceFds(Service *service, int *fds, size_t fdCount);
196 int SetAccessToken(const Service *service);
197 void GetAccessToken(void);
198 void ServiceStopTimer(Service *service);
199 void ServiceStartTimer(Service *service, uint64_t timeout);
200 void ServiceReStartTimer(Service *service, uint64_t timeout);
201 void IsEnableSandbox(void);
202 void EnterServiceSandbox(Service *service);
203 int SetServiceEnterSandbox(const Service *service, const char *execPath);
204 
205 int CreateServiceFile(Service *service);
206 void CloseServiceFile(ServiceFile *fileOpt);
207 #ifdef __cplusplus
208 #if __cplusplus
209 }
210 #endif
211 #endif
212 
213 #endif // BASE_STARTUP_INITLITE_SERVICE_H
214