You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

sig_handler.cc 2.2 kB

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