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