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