1 /**
2 * Copyright 2019 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 #include "minddata/dataset/util/sig_handler.h"
17 #include <csignal>
18 #if !defined(_WIN32) && !defined(_WIN64) && !defined(__ANDROID__) && !defined(ANDROID) && !defined(__APPLE__)
19 #endif
20 #include "minddata/dataset/util/task_manager.h"
21
22 namespace mindspore {
23 namespace dataset {
24 // Register the custom signal handlers
25 #if !defined(_WIN32) && !defined(_WIN64) && !defined(__ANDROID__) && !defined(ANDROID) && !defined(__APPLE__)
RegisterHandlers()26 void RegisterHandlers() {
27 struct sigaction new_int_action;
28 struct sigaction new_term_action;
29
30 // For the interrupt handler, we do not use SA_RESETHAND so this handler remains in play
31 // permanently, do not use the OS default handler for it.
32 new_int_action.sa_sigaction = [](int num, siginfo_t *info, void *ctx) { IntHandler(num, info, ctx); };
33 (void)sigemptyset(&new_int_action.sa_mask);
34 new_int_action.sa_flags = SA_RESTART | SA_SIGINFO;
35
36 new_term_action.sa_sigaction = [](int num, siginfo_t *info, void *ctx) { IntHandler(num, info, ctx); };
37 (void)sigemptyset(&new_term_action.sa_mask);
38 new_term_action.sa_flags = SA_RESTART | SA_SIGINFO;
39
40 (void)sigaction(SIGINT, &new_int_action, nullptr);
41 (void)sigaction(SIGTERM, &new_term_action, nullptr);
42 }
43
IntHandler(int sig_num,siginfo_t * sig_info,void * context)44 extern void IntHandler(int sig_num, // The signal that was raised
45 siginfo_t *sig_info, // The siginfo structure.
46 void *context) { // context info
47 // Wake up the watchdog which is designed as async-signal-safe.
48 TaskManager::WakeUpWatchDog();
49 }
50 #endif
51 } // namespace dataset
52 } // namespace mindspore
53