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