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