OILS / mycpp / gc_iolib.cc View on Github | oilshell.org

46 lines, 24 significant
1#include "mycpp/gc_iolib.h"
2
3#include <errno.h>
4
5namespace iolib {
6
7SignalSafe* gSignalSafe = nullptr;
8
9SignalSafe* InitSignalSafe() {
10 gSignalSafe = Alloc<SignalSafe>();
11 gHeap.RootGlobalVar(gSignalSafe);
12
13 RegisterSignalInterest(SIGINT); // for KeyboardInterrupt checks
14
15 return gSignalSafe;
16}
17
18static void OurSignalHandler(int sig_num) {
19 assert(gSignalSafe != nullptr);
20 gSignalSafe->UpdateFromSignalHandler(sig_num);
21}
22
23void 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.
34void 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