1 /*
2 * Copyright (c) 2021-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 #include <errno.h>
16 #include <signal.h>
17 #include <sys/types.h>
18 #include <sys/wait.h>
19 #include <unistd.h>
20
21 #include "samgr_lite.h"
22 #include "appspawn_server.h"
23 #include "appspawn_service.h"
24
HOS_SystemInit(void)25 void __attribute__((weak)) HOS_SystemInit(void)
26 {
27 SAMGR_Bootstrap();
28 APPSPAWN_LOGI("[appspawn] HOS_SystemInit is called!");
29 }
30
SignalHandler(int sig)31 static void SignalHandler(int sig)
32 {
33 switch (sig) {
34 case SIGCHLD: {
35 pid_t sigPID;
36 int procStat = 0;
37 while (1) {
38 sigPID = waitpid(-1, &procStat, WNOHANG);
39 if (sigPID <= 0) {
40 break;
41 }
42 APPSPAWN_LOGE("SignalHandler sigPID %d.", sigPID);
43 }
44 break;
45 }
46 default:
47 break;
48 }
49 }
50
SignalRegist(void)51 void SignalRegist(void)
52 {
53 struct sigaction act;
54 act.sa_handler = SignalHandler;
55 act.sa_flags = SA_RESTART;
56 if (sigfillset(&act.sa_mask) != 0) {
57 APPSPAWN_LOGE("[appspawn] sigfillset failed! err %d.", errno);
58 }
59
60 if (sigaction(SIGCHLD, &act, NULL) != 0) {
61 APPSPAWN_LOGE("[appspawn] sigaction failed! err %d.", errno);
62 }
63 }
64
main(int argc,char * const argv[])65 int main(int argc, char * const argv[])
66 {
67 sleep(1);
68 APPSPAWN_LOGI("[appspawn] main, enter.");
69
70 AppSpawnContent *content = AppSpawnCreateContent(APPSPAWN_SERVICE_NAME, NULL, 0, 0);
71 if (content == NULL) {
72 APPSPAWN_LOGE("Failed to create content for appspawn");
73 return -1;
74 }
75 SetContentFunction(content);
76 // 1. ipc module init
77 HOS_SystemInit();
78
79 // 2. register signal for SIGCHLD
80 SignalRegist();
81
82 // 3. keep process alive
83 APPSPAWN_LOGI("[appspawn] main, entering wait.");
84 while (1) {
85 // pause only returns when a signal was caught and the signal-catching function returned.
86 // pause only returns -1, no need to process the return value.
87 (void)pause();
88 }
89 }
90