• 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 #ifdef WITH_SELINUX
28 #   include "init_selinux_param.h"
29 #endif // WITH_SELINUX
30 #include "list.h"
31 #include "loop_event.h"
32 #ifdef __cplusplus
33 #if __cplusplus
34 extern "C" {
35 #endif
36 #endif
37 
38 // return value
39 #define SERVICE_FAILURE (-1)
40 #define SERVICE_SUCCESS 0
41 
42 // service attributes
43 #define SERVICE_ATTR_INVALID 0x001      // option invalid
44 #define SERVICE_ATTR_ONCE 0x002         // do not restart when it exits
45 #define SERVICE_ATTR_NEED_RESTART 0x004 // will restart in the near future
46 #define SERVICE_ATTR_NEED_STOP 0x008    // will stop in reap
47 #define SERVICE_ATTR_IMPORTANT 0x010    // will reboot if it crash
48 #define SERVICE_ATTR_CRITICAL 0x020     // critical, will reboot if it crash 4 times in 4 minutes
49 #define SERVICE_ATTR_DISABLED 0x040     // disabled
50 #define SERVICE_ATTR_CONSOLE 0x080      // console
51 #define SERVICE_ATTR_DYNAMIC 0x100      // dynamic service
52 #define SERVICE_ATTR_ONDEMAND 0x200     // ondemand, manage socket by init
53 #define SERVICE_ATTR_TIMERSTART 0x400   // Mark a service will be started by timer
54 #define SERVICE_ATTR_NEEDWAIT 0x800     // Mark a service will be started by timer
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 IsServiceWithTimerEnabled(service) \
74     (((service)->attribute & SERVICE_ATTR_TIMERSTART) == SERVICE_ATTR_TIMERSTART)
75 
76 #define DisableServiceTimer(service) \
77     ((service)->attribute &= ~SERVICE_ATTR_TIMERSTART)
78 
79 #define EnableServiceTimer(service) \
80     ((service)->attribute |= SERVICE_ATTR_TIMERSTART)
81 
82 typedef enum {
83     START_MODE_CONDITION,
84     START_MODE_BOOT,
85     START_MODE_NARMAL,
86 } ServiceStartMode;
87 
88 typedef enum {
89     END_PRE_FORK,
90     END_AFTER_FORK,
91     END_AFTER_EXEC,
92     END_RECV_READY,
93 } ServiceEndMode;
94 
95 typedef struct {
96     uid_t uID;
97     gid_t *gIDArray;
98     int gIDCnt;
99     unsigned int *caps;
100     unsigned int capsCnt;
101 } Perms;
102 
103 typedef struct {
104     int count;
105     char **argv;
106 } ServiceArgs;
107 
108 typedef enum {
109     JOB_ON_BOOT,
110     JOB_ON_START,
111     JOB_ON_STOP,
112     JOB_ON_RESTART,
113     JOB_ON_MAX
114 } ServiceJobType;
115 
116 typedef struct {
117     char *jobsName[JOB_ON_MAX];
118 } ServiceJobs;
119 
120 typedef struct Service_ {
121     char *name;
122 #ifdef WITH_SELINUX
123     char secon[MAX_SECON_LEN];
124 #endif // WITH_SELINUX
125     int pid;
126     int crashCnt;
127     time_t firstCrashTime;
128     int crashCount;
129     int crashTime;
130     unsigned int attribute;
131     int importance;
132     int startMode : 4; // startCondition/ startBoot / startNormal
133     int endMode : 4; // preFork/ fork / exec / ready
134     int status : 4; // ServiceStatus
135     uint64_t tokenId;
136     char apl[MAX_APL_NAME + 1];
137     ServiceArgs capsArgs;
138     Perms servPerm;
139     ServiceArgs pathArgs;
140     ServiceArgs extraArgs;
141     ServiceArgs writePidArgs;
142     CmdLines *restartArg;
143     ServiceSocket *socketCfg;
144     ServiceFile *fileCfg;
145     int *fds;
146     size_t fdCount;
147     TimerHandle timer;
148     ServiceJobs serviceJobs;
149     cpu_set_t cpuSet;
150 } Service;
151 
152 int ServiceStart(Service *service);
153 int ServiceStop(Service *service);
154 void ServiceReap(Service *service);
155 void ReapService(Service *service);
156 
157 void NotifyServiceChange(Service *service, int status);
158 int IsForbidden(const char *fieldStr);
159 int SetImportantValue(Service *curServ, const char *attrName, int value, int flag);
160 int GetServiceCaps(const cJSON *curArrItem, Service *curServ);
161 int ServiceExec(const Service *service);
162 void CloseServiceFds(Service *service, bool needFree);
163 int UpdaterServiceFds(Service *service, int *fds, size_t fdCount);
164 int SetAccessToken(const Service *service);
165 void GetAccessToken(void);
166 void ServiceStopTimer(Service *service);
167 void ServiceStartTimer(Service *service, uint64_t timeout);
168 
169 #ifdef __cplusplus
170 #if __cplusplus
171 }
172 #endif
173 #endif
174 
175 #endif // BASE_STARTUP_INITLITE_SERVICE_H
176