• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2020 Huawei Technologies Co., Ltd
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "utils/signal_util.h"
18 #include <csignal>
19 #include "utils/log_adapter.h"
20 #include "backend/session/kernel_build_client.h"
21 
22 namespace mindspore {
SignalGuard(IntHandlerFunc IntHandler)23 SignalGuard::SignalGuard(IntHandlerFunc IntHandler) { RegisterHandlers(IntHandler); }
24 
~SignalGuard()25 SignalGuard::~SignalGuard() {
26   if (old_handler != nullptr) {
27     int_action.sa_sigaction = old_handler;
28     (void)sigemptyset(&int_action.sa_mask);
29     int_action.sa_flags = SA_RESTART | SA_SIGINFO;
30     (void)sigaction(SIGINT, &int_action, nullptr);
31     old_handler = nullptr;
32   }
33 }
34 
RegisterHandlers(IntHandlerFunc IntHandler)35 void SignalGuard::RegisterHandlers(IntHandlerFunc IntHandler) {
36   struct sigaction old_int_action;
37   (void)sigaction(SIGINT, nullptr, &old_int_action);
38   if (old_int_action.sa_sigaction != nullptr) {
39     MS_LOG(DEBUG) << "The signal has been registered";
40     old_handler = old_int_action.sa_sigaction;
41   }
42   int_action.sa_sigaction = IntHandler;
43   (void)sigemptyset(&int_action.sa_mask);
44   int_action.sa_flags = SA_RESTART | SA_SIGINFO;
45   (void)sigaction(SIGINT, &int_action, nullptr);
46 }
47 }  // namespace mindspore
48