A collection of basic/generally desirable code I use across multiple C++ projects.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.7 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. #ifndef ULE_SIGNAL_HANDLER_H
  2. #define ULE_SIGNAL_HANDLER_H
  3. #include <signal.h> // for signal() and the SIG macros
  4. #include "config.h"
  5. #include "types.h"
  6. #include "print.h"
  7. // the running process can receive and respond to a variety of platform-dependent 'signals' during runtime from the OS.
  8. // freebsd has something like 30 signals, windows has a subset, just 6. we'll just deal with 6.
  9. static inline void defaultHandler(s32 signal) {
  10. ULE_TYPES_H_FTAG;
  11. switch (signal) {
  12. case SIGSEGV:
  13. case SIGABRT:
  14. print("%serror%s: %d\n", ANSI_RED, ANSI_RESET, signal);
  15. trace();
  16. case SIGINT:
  17. die("Bye! (SIGINT)\n");
  18. case SIGTERM:
  19. die(" We were asked to stop (SIGTERM).\n");
  20. case SIGFPE:
  21. die(" (SIGFPE).\n");
  22. case SIGILL:
  23. die(" (SIGILL).\n");
  24. default:
  25. die(" unknown/non-standard signal raised: %d\n", signal);
  26. break;
  27. }
  28. }
  29. static void setSignalHandlers(void(*handler)(s32 signal) = defaultHandler) {
  30. ULE_TYPES_H_FTAG;
  31. if (signal(SIGSEGV, handler) == SIG_ERR) die("failed to set SIGSEGV handler... zzz...\n");
  32. if (signal(SIGABRT, handler) == SIG_ERR) die("failed to set SIGABRT handler... zzz...\n");
  33. if (signal(SIGFPE, handler) == SIG_ERR) die("failed to set SIGFPE handler... zzz...\n");
  34. if (signal(SIGILL, handler) == SIG_ERR) die("failed to set SIGILL handler... zzz...\n"); // does this fire on using sprintf (on Catalina)?
  35. if (signal(SIGINT, handler) == SIG_ERR) die("failed to set SIGINT handler... zzz...\n");
  36. if (signal(SIGTERM, handler) == SIG_ERR) die("failed to set SIGTERM handler... zzz...\n");
  37. }
  38. #endif