• 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 #include <signal.h>
16 #include <sys/wait.h>
17 
18 #include "init_adapter.h"
19 #include "init_service_manager.h"
20 
ReapService(Service * service)21 void ReapService(Service *service)
22 {
23     if (service == NULL) {
24         return;
25     }
26     if (service->attribute & SERVICE_ATTR_IMPORTANT) {
27         // important process exit, need to reboot system
28         service->pid = -1;
29         StopAllServices(0, NULL, 0, NULL);
30         RebootSystem();
31     }
32     ServiceReap(service);
33 }
34 
SigHandler(int sig)35 static void SigHandler(int sig)
36 {
37     switch (sig) {
38         case SIGCHLD: {
39             pid_t sigPID;
40             int procStat = 0;
41             while (1) {
42                 sigPID = waitpid(-1, &procStat, WNOHANG);
43                 if (sigPID <= 0) {
44                     break;
45                 }
46                 CheckWaitPid(sigPID);
47                 ReapService(GetServiceByPid(sigPID));
48             }
49             break;
50         }
51         case SIGTERM: {
52             StopAllServices(0, NULL, 0, NULL);
53             break;
54         }
55         default:
56             break;
57     }
58 }
59 
SignalInit(void)60 void SignalInit(void)
61 {
62     struct sigaction act;
63     act.sa_handler = SigHandler;
64     act.sa_flags = SA_RESTART;
65     (void)sigfillset(&act.sa_mask);
66 
67     sigaction(SIGCHLD, &act, NULL);
68     sigaction(SIGTERM, &act, NULL);
69 }