1
|
1 /* |
|
2 |
11523
|
3 Copyright (C) 1993-2011 John W. Eaton |
1
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
7016
|
9 Free Software Foundation; either version 3 of the License, or (at your |
|
10 option) any later version. |
1
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
7016
|
18 along with Octave; see the file COPYING. If not, see |
|
19 <http://www.gnu.org/licenses/>. |
1
|
20 |
|
21 */ |
|
22 |
2939
|
23 /* |
|
24 |
|
25 The signal blocking macros defined below were adapted from similar |
|
26 functions from GNU Bash, the Bourne Again SHell, copyright (C) 1994 |
|
27 Free Software Foundation, Inc. |
|
28 |
|
29 */ |
|
30 |
834
|
31 // This file should always be included after config.h! |
|
32 |
383
|
33 #if !defined (octave_sighandlers_h) |
|
34 #define octave_sighandlers_h 1 |
1
|
35 |
3566
|
36 // Include signal.h, not csignal since the latter might only define |
|
37 // the ANSI standard C signal interface. |
|
38 |
|
39 #include <signal.h> |
2209
|
40 |
|
41 #include "syswait.h" |
3546
|
42 #include "siglist.h" |
2209
|
43 |
5142
|
44 #include "base-list.h" |
3566
|
45 |
290
|
46 // Signal handler return type. |
|
47 #ifndef BADSIG |
10246
|
48 #define BADSIG (void (*)(int))-1 |
290
|
49 #endif |
|
50 |
2693
|
51 #define BLOCK_SIGNAL(sig, nvar, ovar) \ |
|
52 do \ |
|
53 { \ |
10411
|
54 gnulib::sigemptyset (&nvar); \ |
|
55 gnulib::sigaddset (&nvar, sig); \ |
|
56 gnulib::sigemptyset (&ovar); \ |
|
57 gnulib::sigprocmask (SIG_BLOCK, &nvar, &ovar); \ |
2693
|
58 } \ |
|
59 while (0) |
|
60 |
5144
|
61 #if !defined (SIGCHLD) && defined (SIGCLD) |
|
62 #define SIGCHLD SIGCLD |
|
63 #endif |
|
64 |
2693
|
65 #define BLOCK_CHILD(nvar, ovar) BLOCK_SIGNAL (SIGCHLD, nvar, ovar) |
10411
|
66 #define UNBLOCK_CHILD(ovar) gnulib::sigprocmask (SIG_SETMASK, &ovar, 0) |
2693
|
67 |
10246
|
68 typedef void sig_handler (int); |
290
|
69 |
5775
|
70 // FIXME -- the data should probably be private... |
2554
|
71 |
2705
|
72 struct |
|
73 octave_interrupt_handler |
|
74 { |
|
75 #ifdef SIGINT |
|
76 sig_handler *int_handler; |
|
77 #endif |
|
78 |
|
79 #ifdef SIGBREAK |
|
80 sig_handler *brk_handler; |
|
81 #endif |
|
82 }; |
2554
|
83 |
1
|
84 // Nonzero means we have already printed a message for this series of |
|
85 // SIGPIPES. We assume that the writer will eventually give up. |
|
86 extern int pipe_handler_error_count; |
|
87 |
3018
|
88 // TRUE means we can be interrupted. |
6972
|
89 extern OCTINTERP_API bool can_interrupt; |
1
|
90 |
6109
|
91 extern OCTINTERP_API sig_handler *octave_set_signal_handler (int, sig_handler *, |
10313
|
92 bool restart_syscalls = true); |
1443
|
93 |
6109
|
94 extern OCTINTERP_API void install_signal_handlers (void); |
1
|
95 |
6109
|
96 extern OCTINTERP_API void octave_signal_handler (void); |
5142
|
97 |
6109
|
98 extern OCTINTERP_API octave_interrupt_handler octave_catch_interrupts (void); |
2554
|
99 |
6109
|
100 extern OCTINTERP_API octave_interrupt_handler octave_ignore_interrupts (void); |
2554
|
101 |
6109
|
102 extern OCTINTERP_API octave_interrupt_handler |
5770
|
103 octave_set_interrupt_handler (const volatile octave_interrupt_handler&, |
10313
|
104 bool restart_syscalls = true); |
1651
|
105 |
2214
|
106 // extern void ignore_sigchld (void); |
|
107 |
2209
|
108 // Maybe this should be in a separate file? |
|
109 |
|
110 class |
6109
|
111 OCTINTERP_API |
2209
|
112 octave_child |
|
113 { |
|
114 public: |
5142
|
115 |
|
116 // Do whatever to handle event for child with PID (might not |
|
117 // actually be dead, could just be stopped). Return true if |
|
118 // the list element corresponding to PID should be removed from |
|
119 // list. This function should not call any functions that modify |
|
120 // the octave_child_list. |
2209
|
121 |
5142
|
122 typedef bool (*child_event_handler) (pid_t, int); |
|
123 |
|
124 octave_child (pid_t id = -1, child_event_handler f = 0) |
|
125 : pid (id), handler (f), have_status (0), status (0) { } |
2209
|
126 |
|
127 octave_child (const octave_child& oc) |
5142
|
128 : pid (oc.pid), handler (oc.handler), |
|
129 have_status (oc.have_status), status (oc.status) { } |
|
130 |
2209
|
131 octave_child& operator = (const octave_child& oc) |
|
132 { |
|
133 if (&oc != this) |
10313
|
134 { |
|
135 pid = oc.pid; |
|
136 handler = oc.handler; |
|
137 have_status = oc.have_status; |
|
138 status = oc.status; |
|
139 } |
2209
|
140 return *this; |
|
141 } |
|
142 |
|
143 ~octave_child (void) { } |
|
144 |
|
145 // The process id of this child. |
|
146 pid_t pid; |
|
147 |
5142
|
148 // The function we call if an event happens for this child. |
|
149 child_event_handler handler; |
|
150 |
|
151 // Nonzero if this child has stopped or terminated. |
|
152 sig_atomic_t have_status; |
|
153 |
|
154 // The status of this child; 0 if running, otherwise a status value |
|
155 // from waitpid. |
|
156 int status; |
2209
|
157 }; |
|
158 |
|
159 class |
6109
|
160 OCTINTERP_API |
2209
|
161 octave_child_list |
|
162 { |
|
163 protected: |
|
164 |
5142
|
165 octave_child_list (void) { } |
|
166 |
|
167 class octave_child_list_rep : public octave_base_list<octave_child> |
|
168 { |
|
169 public: |
|
170 |
|
171 void insert (pid_t pid, octave_child::child_event_handler f); |
|
172 |
|
173 void reap (void); |
|
174 |
|
175 bool wait (void); |
|
176 }; |
2209
|
177 |
|
178 public: |
|
179 |
5142
|
180 ~octave_child_list (void) { } |
|
181 |
|
182 static void insert (pid_t pid, octave_child::child_event_handler f); |
2209
|
183 |
5142
|
184 static void reap (void); |
2926
|
185 |
5142
|
186 static bool wait (void); |
2209
|
187 |
2214
|
188 static void remove (pid_t pid); |
|
189 |
2209
|
190 private: |
|
191 |
5142
|
192 static bool instance_ok (void); |
2209
|
193 |
5142
|
194 static octave_child_list_rep *instance; |
2209
|
195 }; |
|
196 |
1
|
197 #endif |