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