1 /*
2 * Copyright (c) 2022 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
16 #include <errno.h>
17 #include <stdlib.h>
18 #include <stdio.h>
19 #include <unistd.h>
20
21 #include "control_fd.h"
22 #include "init_service.h"
23 #include "init_service_manager.h"
24 #include "init_modulemgr.h"
25 #include "init_utils.h"
26 #include "init_log.h"
27 #include "init_group_manager.h"
28 #include "init_param.h"
29 #include "hookmgr.h"
30 #include "bootstage.h"
31
DumpServiceArgs(const char * info,const ServiceArgs * args)32 static void DumpServiceArgs(const char *info, const ServiceArgs *args)
33 {
34 printf("\tservice %s count %d \n", info, args->count);
35 for (int j = 0; j < args->count; j++) {
36 if (args->argv[j] != NULL) {
37 printf("\t\tinfo [%d] %s \n", j, args->argv[j]);
38 }
39 }
40 }
41
DumpServiceJobs(const Service * service)42 static void DumpServiceJobs(const Service *service)
43 {
44 printf("\tservice job info \n");
45 if (service->serviceJobs.jobsName[JOB_ON_BOOT] != NULL) {
46 printf("\t\tservice boot job %s \n", service->serviceJobs.jobsName[JOB_ON_BOOT]);
47 }
48 if (service->serviceJobs.jobsName[JOB_ON_START] != NULL) {
49 printf("\t\tservice start job %s \n", service->serviceJobs.jobsName[JOB_ON_START]);
50 }
51 if (service->serviceJobs.jobsName[JOB_ON_STOP] != NULL) {
52 printf("\t\tservice stop job %s \n", service->serviceJobs.jobsName[JOB_ON_STOP]);
53 }
54 if (service->serviceJobs.jobsName[JOB_ON_RESTART] != NULL) {
55 printf("\t\tservice restart job %s \n", service->serviceJobs.jobsName[JOB_ON_RESTART]);
56 }
57 }
58
DumpServiceSocket(const Service * service)59 static void DumpServiceSocket(const Service *service)
60 {
61 printf("\tservice socket info \n");
62 ServiceSocket *sockopt = service->socketCfg;
63 while (sockopt != NULL) {
64 printf("\t\tsocket name: %s \n", sockopt->name);
65 printf("\t\tsocket type: %u \n", sockopt->type);
66 printf("\t\tsocket uid: %u \n", sockopt->uid);
67 printf("\t\tsocket gid: %u \n", sockopt->gid);
68 sockopt = sockopt->next;
69 }
70 }
71
DumpServiceHookExecute(const char * name,const char * info)72 void DumpServiceHookExecute(const char *name, const char *info)
73 {
74 SERVICE_INFO_CTX context;
75 context.serviceName = name;
76 context.reserved = info;
77 (void)HookMgrExecute(GetBootStageHookMgr(), INIT_SERVICE_DUMP, (void *)(&context), NULL);
78 }
79
DumpOneService(const Service * service)80 static void DumpOneService(const Service *service)
81 {
82 const InitArgInfo startModeMap[] = {
83 {"condition", START_MODE_CONDITION},
84 {"boot", START_MODE_BOOT},
85 {"normal", START_MODE_NORMAL}
86 };
87
88 const static char *serviceStatusMap[] = {
89 "created", "starting", "running", "ready",
90 "stopping", "stopped", "suspended", "freezed", "disabled", "critical"
91 };
92
93 printf("\tservice name: [%s] \n", service->name);
94 printf("\tservice pid: [%d] \n", service->pid);
95 printf("\tservice context : [%s] \n", (service->context.type == INIT_CONTEXT_CHIPSET) ? "chipset" : "system");
96 printf("\tservice crashCnt: [%d] \n", service->crashCnt);
97 printf("\tservice attribute: [%u] \n", service->attribute);
98 printf("\tservice importance: [%d] \n", service->importance);
99 printf("\tservice startMode: [%s] \n", startModeMap[service->startMode].name);
100 printf("\tservice status: [%s] \n", serviceStatusMap[service->status]);
101 printf("\tservice perms uID [%u] \n", service->servPerm.uID);
102 DumpServiceArgs("path arg", &service->pathArgs);
103 DumpServiceArgs("writepid file", &service->writePidArgs);
104 DumpServiceJobs(service);
105 DumpServiceSocket(service);
106
107 printf("\tservice perms groupId %d \n", service->servPerm.gIDCnt);
108 for (int i = 0; i < service->servPerm.gIDCnt; i++) {
109 printf("\t\tservice perms groupId %u \n", service->servPerm.gIDArray[i]);
110 }
111 printf("\tservice perms capability %u \n", service->servPerm.capsCnt);
112 for (int i = 0; i < (int)service->servPerm.capsCnt; i++) {
113 printf("\t\tservice perms capability %u \n", service->servPerm.caps[i]);
114 }
115
116 DumpServiceHookExecute(service->name, NULL);
117 }
118
PrintBootEventHead(const char * cmd)119 static void PrintBootEventHead(const char *cmd)
120 {
121 if (strcmp(cmd, "bootevent") == 0) {
122 printf("\t%-20.20s\t%-50s\t%-20.20s\t%-20.20s\n",
123 "service-name", "bootevent-name", "fork", "ready");
124 }
125 return;
126 }
127
DumpAllExtData(const char * cmd)128 static void DumpAllExtData(const char *cmd)
129 {
130 PrintBootEventHead(cmd);
131 InitGroupNode *node = GetNextGroupNode(NODE_TYPE_SERVICES, NULL);
132 while (node != NULL) {
133 if (node->data.service == NULL) {
134 node = GetNextGroupNode(NODE_TYPE_SERVICES, node);
135 continue;
136 }
137 DumpServiceHookExecute(node->name, cmd);
138 node = GetNextGroupNode(NODE_TYPE_SERVICES, node);
139 }
140 }
141
DumpAllServices(void)142 static void DumpAllServices(void)
143 {
144 printf("Ready to dump all services: \n");
145 InitGroupNode *node = GetNextGroupNode(NODE_TYPE_SERVICES, NULL);
146 while (node != NULL) {
147 if (node->data.service == NULL) {
148 node = GetNextGroupNode(NODE_TYPE_SERVICES, node);
149 continue;
150 }
151 Service *service = node->data.service;
152 DumpOneService(service);
153 node = GetNextGroupNode(NODE_TYPE_SERVICES, node);
154 }
155 printf("Dump all services finished \n");
156 }
157
ProcessSandboxControlFd(uint16_t type,const char * serviceCmd)158 static void ProcessSandboxControlFd(uint16_t type, const char *serviceCmd)
159 {
160 if ((type != ACTION_SANDBOX) || (serviceCmd == NULL)) {
161 INIT_LOGE("Invalid parameter");
162 return;
163 }
164 Service *service = GetServiceByName(serviceCmd);
165 if (service == NULL) {
166 INIT_LOGE("Failed get service %s", serviceCmd);
167 return;
168 }
169 EnterServiceSandbox(service);
170 return;
171 }
172
ProcessDumpServiceControlFd(uint16_t type,const char * serviceCmd)173 static void ProcessDumpServiceControlFd(uint16_t type, const char *serviceCmd)
174 {
175 if ((type != ACTION_DUMP) || (serviceCmd == NULL)) {
176 return;
177 }
178 char *cmd = strrchr(serviceCmd, '#');
179 if (cmd != NULL) {
180 cmd[0] = '\0';
181 cmd++;
182 }
183
184 if (strcmp(serviceCmd, "all") == 0) {
185 if (cmd != NULL) {
186 DumpAllExtData(cmd);
187 } else {
188 DumpAllServices();
189 }
190 return;
191 }
192 if (strcmp(serviceCmd, "parameter_service") == 0) {
193 if (cmd != NULL && strcmp(cmd, "trigger") == 0) {
194 SystemDumpTriggers(0, printf);
195 }
196 return;
197 }
198 Service *service = GetServiceByName(serviceCmd);
199 if (service != NULL) {
200 if (cmd != NULL) {
201 PrintBootEventHead(cmd);
202 DumpServiceHookExecute(serviceCmd, cmd);
203 } else {
204 DumpOneService(service);
205 }
206 }
207 return;
208 }
209
ProcessModuleMgrControlFd(uint16_t type,const char * serviceCmd)210 static void ProcessModuleMgrControlFd(uint16_t type, const char *serviceCmd)
211 {
212 if ((type != ACTION_MODULEMGR) || (serviceCmd == NULL)) {
213 return;
214 }
215 INIT_LOGE("ProcessModuleMgrControlFd argc [%s] \n", serviceCmd);
216 if (strcmp(serviceCmd, "list") == 0) {
217 InitModuleMgrDump();
218 return;
219 }
220 }
221
ProcessControlFd(uint16_t type,const char * serviceCmd,const void * context)222 void ProcessControlFd(uint16_t type, const char *serviceCmd, const void *context)
223 {
224 if ((type >= ACTION_MAX) || (serviceCmd == NULL)) {
225 return;
226 }
227 switch (type) {
228 case ACTION_SANDBOX :
229 ProcessSandboxControlFd(type, serviceCmd);
230 break;
231 case ACTION_DUMP :
232 ProcessDumpServiceControlFd(type, serviceCmd);
233 break;
234 case ACTION_MODULEMGR :
235 ProcessModuleMgrControlFd(type, serviceCmd);
236 break;
237 default :
238 INIT_LOGW("Unknown control fd type.");
239 break;
240 }
241 }
242
InitControlFd(void)243 void InitControlFd(void)
244 {
245 CmdServiceInit(INIT_CONTROL_FD_SOCKET_PATH, ProcessControlFd, LE_GetDefaultLoop());
246 return;
247 }
248