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 |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
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 RETSIGTYPE |
|
48 #define RETSIGTYPE void |
|
49 #endif |
|
50 #ifndef BADSIG |
|
51 #define BADSIG (RETSIGTYPE (*)(int))-1 |
|
52 #endif |
|
53 |
2693
|
54 #define BLOCK_SIGNAL(sig, nvar, ovar) \ |
|
55 do \ |
|
56 { \ |
|
57 sigemptyset (&nvar); \ |
|
58 sigaddset (&nvar, sig); \ |
|
59 sigemptyset (&ovar); \ |
|
60 sigprocmask (SIG_BLOCK, &nvar, &ovar); \ |
|
61 } \ |
|
62 while (0) |
|
63 |
5144
|
64 #if !defined (SIGCHLD) && defined (SIGCLD) |
|
65 #define SIGCHLD SIGCLD |
|
66 #endif |
|
67 |
2693
|
68 #if defined (HAVE_POSIX_SIGNALS) |
|
69 #define BLOCK_CHILD(nvar, ovar) BLOCK_SIGNAL (SIGCHLD, nvar, ovar) |
|
70 #define UNBLOCK_CHILD(ovar) sigprocmask (SIG_SETMASK, &ovar, 0) |
|
71 #else |
|
72 #define BLOCK_CHILD(nvar, ovar) ovar = sigblock (sigmask (SIGCHLD)) |
|
73 #define UNBLOCK_CHILD(ovar) sigsetmask (ovar) |
|
74 #endif |
|
75 |
290
|
76 typedef RETSIGTYPE sig_handler (int); |
|
77 |
2705
|
78 // XXX FIXME XXX -- the data should probably be private... |
2554
|
79 |
2705
|
80 struct |
|
81 octave_interrupt_handler |
|
82 { |
|
83 #ifdef SIGINT |
|
84 sig_handler *int_handler; |
|
85 #endif |
|
86 |
|
87 #ifdef SIGBREAK |
|
88 sig_handler *brk_handler; |
|
89 #endif |
|
90 }; |
2554
|
91 |
1
|
92 // Nonzero means we have already printed a message for this series of |
|
93 // SIGPIPES. We assume that the writer will eventually give up. |
|
94 extern int pipe_handler_error_count; |
|
95 |
3018
|
96 // TRUE means we can be interrupted. |
|
97 extern bool can_interrupt; |
1
|
98 |
1443
|
99 extern sig_handler *octave_set_signal_handler (int, sig_handler *); |
|
100 |
1
|
101 extern void install_signal_handlers (void); |
|
102 |
5142
|
103 extern void octave_signal_handler (void); |
|
104 |
2705
|
105 extern octave_interrupt_handler octave_catch_interrupts (void); |
2554
|
106 |
2705
|
107 extern octave_interrupt_handler octave_ignore_interrupts (void); |
2554
|
108 |
2705
|
109 extern octave_interrupt_handler |
|
110 octave_set_interrupt_handler (const volatile octave_interrupt_handler&); |
1651
|
111 |
2214
|
112 // extern void ignore_sigchld (void); |
|
113 |
2209
|
114 // Maybe this should be in a separate file? |
|
115 |
|
116 class |
|
117 octave_child |
|
118 { |
|
119 public: |
5142
|
120 |
|
121 // Do whatever to handle event for child with PID (might not |
|
122 // actually be dead, could just be stopped). Return true if |
|
123 // the list element corresponding to PID should be removed from |
|
124 // list. This function should not call any functions that modify |
|
125 // the octave_child_list. |
2209
|
126 |
5142
|
127 typedef bool (*child_event_handler) (pid_t, int); |
|
128 |
|
129 octave_child (pid_t id = -1, child_event_handler f = 0) |
|
130 : pid (id), handler (f), have_status (0), status (0) { } |
2209
|
131 |
|
132 octave_child (const octave_child& oc) |
5142
|
133 : pid (oc.pid), handler (oc.handler), |
|
134 have_status (oc.have_status), status (oc.status) { } |
|
135 |
2209
|
136 octave_child& operator = (const octave_child& oc) |
|
137 { |
|
138 if (&oc != this) |
|
139 { |
|
140 pid = oc.pid; |
|
141 handler = oc.handler; |
5142
|
142 have_status = oc.have_status; |
|
143 status = oc.status; |
2209
|
144 } |
|
145 return *this; |
|
146 } |
|
147 |
|
148 ~octave_child (void) { } |
|
149 |
|
150 // The process id of this child. |
|
151 pid_t pid; |
|
152 |
5142
|
153 // The function we call if an event happens for this child. |
|
154 child_event_handler handler; |
|
155 |
|
156 // Nonzero if this child has stopped or terminated. |
|
157 sig_atomic_t have_status; |
|
158 |
|
159 // The status of this child; 0 if running, otherwise a status value |
|
160 // from waitpid. |
|
161 int status; |
2209
|
162 }; |
|
163 |
|
164 class |
|
165 octave_child_list |
|
166 { |
|
167 protected: |
|
168 |
5142
|
169 octave_child_list (void) { } |
|
170 |
|
171 class octave_child_list_rep : public octave_base_list<octave_child> |
|
172 { |
|
173 public: |
|
174 |
|
175 void insert (pid_t pid, octave_child::child_event_handler f); |
|
176 |
|
177 void reap (void); |
|
178 |
|
179 bool wait (void); |
|
180 }; |
2209
|
181 |
|
182 public: |
|
183 |
5142
|
184 ~octave_child_list (void) { } |
|
185 |
|
186 static void insert (pid_t pid, octave_child::child_event_handler f); |
2209
|
187 |
5142
|
188 static void reap (void); |
2926
|
189 |
5142
|
190 static bool wait (void); |
2209
|
191 |
2214
|
192 static void remove (pid_t pid); |
|
193 |
2209
|
194 private: |
|
195 |
5142
|
196 static bool instance_ok (void); |
2209
|
197 |
5142
|
198 static octave_child_list_rep *instance; |
2209
|
199 }; |
|
200 |
1
|
201 #endif |
|
202 |
|
203 /* |
|
204 ;;; Local Variables: *** |
|
205 ;;; mode: C++ *** |
|
206 ;;; End: *** |
|
207 */ |