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