summaryrefslogtreecommitdiffstats
path: root/signal.h
blob: 5aa49c82a5a87826c19b37c4ec12f1f883455231 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/* Copyright 2013-2014
 * Kaz Kylheku <kaz@kylheku.com>
 * Vancouver, Canada
 * All rights reserved.
 *
 * BSD License:
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in
 *      the documentation and/or other materials provided with the
 *      distribution.
 *   3. The name of the author may not be used to endorse or promote
 *      products derived from this software without specific prior
 *      written permission.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 */


#if HAVE_POSIX_SIGS

#define sig_save_enable                         \
  do {                                          \
    int sig_save = async_sig_enabled;           \
    async_sig_enabled = 1;                      \
    if (!sig_save)                              \
      sig_check();                              \
    {                                           \
      typedef void v_o_i_d

#define sig_restore_enable                      \
      do { } while (0);                         \
    }                                           \
    async_sig_enabled = sig_save;               \
  } while(0)

#define sig_save_disable                        \
  do {                                          \
    int sig_save = async_sig_enabled;           \
    async_sig_enabled = 1;                      \
    {                                           \
      typedef void v_o_i_d

#define sig_restore_disable                     \
      do { } while (0);                         \
    }                                           \
    async_sig_enabled = sig_save;               \
    if (sig_save)                               \
      sig_check();                              \
  } while(0)
    
typedef struct {
  jmp_buf jb;
  sig_atomic_t se;
  sigset_t blocked;
  val de;
  int rv;
} extended_jmp_buf;

#define extended_setjmp(EJB)                    \
  (setjmp((EJB).jb)                             \
   ? (async_sig_enabled = (EJB).se,             \
      dyn_env = (EJB).de,                       \
      sig_mask(SIG_SETMASK, &(EJB).blocked, 0), \
      (EJB).rv)                                 \
   : ((EJB).se = async_sig_enabled,             \
      (EJB).de = dyn_env,                       \
      (EJB).blocked = sig_blocked_cache, 0))

#define extended_longjmp(EJB, ARG)              \
  ((EJB).rv = (ARG), longjmp((EJB).jb, 1))

extern sigset_t sig_blocked_cache;
extern volatile sig_atomic_t async_sig_enabled;

#else

#define sig_save_enable do {
#define sig_save_disable do {

#define sig_restore_enable do { } while (0); } while (0)
#define sig_restore_disable do { } while (0); } while (0)

typedef struct {
  jmp_buf jb;
  val de;
  int rv;
} extended_jmp_buf;

#define extended_setjmp(EJB)                    \
  (setjmp((EJB).jb)                             \
   ? (dyn_env = (EJB).de,                       \
      (EJB).rv)                                 \
   : ((EJB).de = dyn_env, 0))

#define extended_longjmp(EJB, ARG)              \
  ((EJB).rv = (ARG), longjmp((EJB).jb, 1))

extern int async_sig_enabled;

#endif

extern val dyn_env; /* eval.c */

extern val sig_hup, sig_int, sig_quit, sig_ill, sig_trap, sig_abrt, sig_bus;
extern val sig_fpe, sig_kill, sig_usr1, sig_segv, sig_usr2, sig_pipe, sig_alrm;
extern val sig_term, sig_chld, sig_cont, sig_stop, sig_tstp, sig_ttin;
extern val sig_ttou, sig_urg, sig_xcpu, sig_xfsz, sigtalrm, sig_prof;
extern val sig_poll, sig_sys, sig_winch, sig_iot, sig_stkflt;
extern val sig_io, sig_lost, sig_pwr;

void sig_init(void);
val set_sig_handler(val signo, val lambda);
val get_sig_handler(val signo);
val sig_check(void);
#if HAVE_POSIX_SIGS
int sig_mask(int how, const sigset_t *set, sigset_t *oldset);
#endif