| 1 | #include "mycpp/gc_iolib.h"
|
| 2 |
|
| 3 | #include <errno.h>
|
| 4 |
|
| 5 | namespace iolib {
|
| 6 |
|
| 7 | SignalSafe* gSignalSafe = nullptr;
|
| 8 |
|
| 9 | SignalSafe* InitSignalSafe() {
|
| 10 | gSignalSafe = Alloc<SignalSafe>();
|
| 11 | gHeap.RootGlobalVar(gSignalSafe);
|
| 12 |
|
| 13 | RegisterSignalInterest(SIGINT); // for KeyboardInterrupt checks
|
| 14 |
|
| 15 | return gSignalSafe;
|
| 16 | }
|
| 17 |
|
| 18 | static void OurSignalHandler(int sig_num) {
|
| 19 | assert(gSignalSafe != nullptr);
|
| 20 | gSignalSafe->UpdateFromSignalHandler(sig_num);
|
| 21 | }
|
| 22 |
|
| 23 | void RegisterSignalInterest(int sig_num) {
|
| 24 | struct sigaction act = {};
|
| 25 | act.sa_handler = OurSignalHandler;
|
| 26 | if (sigaction(sig_num, &act, nullptr) != 0) {
|
| 27 | throw Alloc<OSError>(errno);
|
| 28 | }
|
| 29 | }
|
| 30 |
|
| 31 | // Note that the Python implementation of pyos.sigaction() calls
|
| 32 | // signal.signal(), which calls PyOS_setsig(), which calls sigaction() #ifdef
|
| 33 | // HAVE_SIGACTION.
|
| 34 | void sigaction(int sig_num, void (*handler)(int)) {
|
| 35 | // SIGINT and SIGWINCH must be registered through SignalSafe
|
| 36 | DCHECK(sig_num != SIGINT);
|
| 37 | DCHECK(sig_num != SIGWINCH);
|
| 38 |
|
| 39 | struct sigaction act = {};
|
| 40 | act.sa_handler = handler;
|
| 41 | if (sigaction(sig_num, &act, nullptr) != 0) {
|
| 42 | throw Alloc<OSError>(errno);
|
| 43 | }
|
| 44 | }
|
| 45 |
|
| 46 | } // namespace iolib
|