1
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 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 |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
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 |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
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. |
|
98 extern bool can_interrupt; |
1
|
99 |
5770
|
100 extern sig_handler *octave_set_signal_handler (int, sig_handler *, |
|
101 bool restart_syscalls = true); |
1443
|
102 |
1
|
103 extern void install_signal_handlers (void); |
|
104 |
5142
|
105 extern void octave_signal_handler (void); |
|
106 |
2705
|
107 extern octave_interrupt_handler octave_catch_interrupts (void); |
2554
|
108 |
2705
|
109 extern octave_interrupt_handler octave_ignore_interrupts (void); |
2554
|
110 |
2705
|
111 extern 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 |
|
120 octave_child |
|
121 { |
|
122 public: |
5142
|
123 |
|
124 // Do whatever to handle event for child with PID (might not |
|
125 // actually be dead, could just be stopped). Return true if |
|
126 // the list element corresponding to PID should be removed from |
|
127 // list. This function should not call any functions that modify |
|
128 // the octave_child_list. |
2209
|
129 |
5142
|
130 typedef bool (*child_event_handler) (pid_t, int); |
|
131 |
|
132 octave_child (pid_t id = -1, child_event_handler f = 0) |
|
133 : pid (id), handler (f), have_status (0), status (0) { } |
2209
|
134 |
|
135 octave_child (const octave_child& oc) |
5142
|
136 : pid (oc.pid), handler (oc.handler), |
|
137 have_status (oc.have_status), status (oc.status) { } |
|
138 |
2209
|
139 octave_child& operator = (const octave_child& oc) |
|
140 { |
|
141 if (&oc != this) |
|
142 { |
|
143 pid = oc.pid; |
|
144 handler = oc.handler; |
5142
|
145 have_status = oc.have_status; |
|
146 status = oc.status; |
2209
|
147 } |
|
148 return *this; |
|
149 } |
|
150 |
|
151 ~octave_child (void) { } |
|
152 |
|
153 // The process id of this child. |
|
154 pid_t pid; |
|
155 |
5142
|
156 // The function we call if an event happens for this child. |
|
157 child_event_handler handler; |
|
158 |
|
159 // Nonzero if this child has stopped or terminated. |
|
160 sig_atomic_t have_status; |
|
161 |
|
162 // The status of this child; 0 if running, otherwise a status value |
|
163 // from waitpid. |
|
164 int status; |
2209
|
165 }; |
|
166 |
|
167 class |
|
168 octave_child_list |
|
169 { |
|
170 protected: |
|
171 |
5142
|
172 octave_child_list (void) { } |
|
173 |
|
174 class octave_child_list_rep : public octave_base_list<octave_child> |
|
175 { |
|
176 public: |
|
177 |
|
178 void insert (pid_t pid, octave_child::child_event_handler f); |
|
179 |
|
180 void reap (void); |
|
181 |
|
182 bool wait (void); |
|
183 }; |
2209
|
184 |
|
185 public: |
|
186 |
5142
|
187 ~octave_child_list (void) { } |
|
188 |
|
189 static void insert (pid_t pid, octave_child::child_event_handler f); |
2209
|
190 |
5142
|
191 static void reap (void); |
2926
|
192 |
5142
|
193 static bool wait (void); |
2209
|
194 |
2214
|
195 static void remove (pid_t pid); |
|
196 |
2209
|
197 private: |
|
198 |
5142
|
199 static bool instance_ok (void); |
2209
|
200 |
5142
|
201 static octave_child_list_rep *instance; |
2209
|
202 }; |
|
203 |
1
|
204 #endif |
|
205 |
|
206 /* |
|
207 ;;; Local Variables: *** |
|
208 ;;; mode: C++ *** |
|
209 ;;; End: *** |
|
210 */ |