169570cc8Sopenharmony_ci/* 269570cc8Sopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 369570cc8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 469570cc8Sopenharmony_ci * you may not use this file except in compliance with the License. 569570cc8Sopenharmony_ci * You may obtain a copy of the License at 669570cc8Sopenharmony_ci * 769570cc8Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 869570cc8Sopenharmony_ci * 969570cc8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1069570cc8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1169570cc8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1269570cc8Sopenharmony_ci * See the License for the specific language governing permissions and 1369570cc8Sopenharmony_ci * limitations under the License. 1469570cc8Sopenharmony_ci */ 1569570cc8Sopenharmony_ci#include <errno.h> 1669570cc8Sopenharmony_ci#include <signal.h> 1769570cc8Sopenharmony_ci#include <sys/types.h> 1869570cc8Sopenharmony_ci#include <sys/wait.h> 1969570cc8Sopenharmony_ci#include <unistd.h> 2069570cc8Sopenharmony_ci 2169570cc8Sopenharmony_ci#include "samgr_lite.h" 2269570cc8Sopenharmony_ci#include "appspawn_server.h" 2369570cc8Sopenharmony_ci#include "appspawn_service.h" 2469570cc8Sopenharmony_ci 2569570cc8Sopenharmony_civoid __attribute__((weak)) HOS_SystemInit(void) 2669570cc8Sopenharmony_ci{ 2769570cc8Sopenharmony_ci SAMGR_Bootstrap(); 2869570cc8Sopenharmony_ci APPSPAWN_LOGI("[appspawn] HOS_SystemInit is called!"); 2969570cc8Sopenharmony_ci} 3069570cc8Sopenharmony_ci 3169570cc8Sopenharmony_cistatic void SignalHandler(int sig) 3269570cc8Sopenharmony_ci{ 3369570cc8Sopenharmony_ci switch (sig) { 3469570cc8Sopenharmony_ci case SIGCHLD: { 3569570cc8Sopenharmony_ci pid_t sigPID; 3669570cc8Sopenharmony_ci int procStat = 0; 3769570cc8Sopenharmony_ci while (1) { 3869570cc8Sopenharmony_ci sigPID = waitpid(-1, &procStat, WNOHANG); 3969570cc8Sopenharmony_ci if (sigPID <= 0) { 4069570cc8Sopenharmony_ci break; 4169570cc8Sopenharmony_ci } 4269570cc8Sopenharmony_ci APPSPAWN_LOGE("SignalHandler sigPID %d.", sigPID); 4369570cc8Sopenharmony_ci } 4469570cc8Sopenharmony_ci break; 4569570cc8Sopenharmony_ci } 4669570cc8Sopenharmony_ci default: 4769570cc8Sopenharmony_ci break; 4869570cc8Sopenharmony_ci } 4969570cc8Sopenharmony_ci} 5069570cc8Sopenharmony_ci 5169570cc8Sopenharmony_civoid SignalRegist(void) 5269570cc8Sopenharmony_ci{ 5369570cc8Sopenharmony_ci struct sigaction act; 5469570cc8Sopenharmony_ci act.sa_handler = SignalHandler; 5569570cc8Sopenharmony_ci act.sa_flags = SA_RESTART; 5669570cc8Sopenharmony_ci if (sigfillset(&act.sa_mask) != 0) { 5769570cc8Sopenharmony_ci APPSPAWN_LOGE("[appspawn] sigfillset failed! err %d.", errno); 5869570cc8Sopenharmony_ci } 5969570cc8Sopenharmony_ci 6069570cc8Sopenharmony_ci if (sigaction(SIGCHLD, &act, NULL) != 0) { 6169570cc8Sopenharmony_ci APPSPAWN_LOGE("[appspawn] sigaction failed! err %d.", errno); 6269570cc8Sopenharmony_ci } 6369570cc8Sopenharmony_ci} 6469570cc8Sopenharmony_ci 6569570cc8Sopenharmony_ciint main(int argc, char * const argv[]) 6669570cc8Sopenharmony_ci{ 6769570cc8Sopenharmony_ci sleep(1); 6869570cc8Sopenharmony_ci APPSPAWN_LOGI("[appspawn] main, enter."); 6969570cc8Sopenharmony_ci 7069570cc8Sopenharmony_ci AppSpawnContent *content = AppSpawnCreateContent(APPSPAWN_SERVICE_NAME, NULL, 0, 0); 7169570cc8Sopenharmony_ci if (content == NULL) { 7269570cc8Sopenharmony_ci APPSPAWN_LOGE("Failed to create content for appspawn"); 7369570cc8Sopenharmony_ci return -1; 7469570cc8Sopenharmony_ci } 7569570cc8Sopenharmony_ci SetContentFunction(content); 7669570cc8Sopenharmony_ci // 1. ipc module init 7769570cc8Sopenharmony_ci HOS_SystemInit(); 7869570cc8Sopenharmony_ci 7969570cc8Sopenharmony_ci // 2. register signal for SIGCHLD 8069570cc8Sopenharmony_ci SignalRegist(); 8169570cc8Sopenharmony_ci 8269570cc8Sopenharmony_ci // 3. keep process alive 8369570cc8Sopenharmony_ci APPSPAWN_LOGI("[appspawn] main, entering wait."); 8469570cc8Sopenharmony_ci while (1) { 8569570cc8Sopenharmony_ci // pause only returns when a signal was caught and the signal-catching function returned. 8669570cc8Sopenharmony_ci // pause only returns -1, no need to process the return value. 8769570cc8Sopenharmony_ci (void)pause(); 8869570cc8Sopenharmony_ci } 8969570cc8Sopenharmony_ci} 90