2094
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
2094
|
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 |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
|
27 #include <cerrno> |
|
28 |
|
29 #include <iostream.h> |
|
30 |
|
31 #ifdef HAVE_UNISTD_H |
2442
|
32 #ifdef HAVE_SYS_TYPES_H |
2094
|
33 #include <sys/types.h> |
2442
|
34 #endif |
2094
|
35 #include <unistd.h> |
|
36 #endif |
|
37 |
3174
|
38 #include "lo-mappers.h" |
|
39 #include "lo-utils.h" |
2094
|
40 #include "oct-procbuf.h" |
|
41 #include "syswait.h" |
3176
|
42 #include "variables.h" |
2094
|
43 |
3174
|
44 #include "defun.h" |
|
45 #include "gripes.h" |
|
46 |
|
47 // Number of microseconds to delay in the parent after forking. |
|
48 #if defined (__CYGWIN32__) |
|
49 static int Vkluge_procbuf_delay = 500000; |
|
50 #else |
|
51 static int Vkluge_procbuf_delay = 0; |
|
52 #endif |
|
53 |
2094
|
54 // This class is based on the procbuf class from libg++, written by |
|
55 // Per Bothner, Copyright (C) 1993 Free Software Foundation. |
|
56 // |
|
57 // It should work with the filebuf class from libg++, but it might not |
|
58 // work with others since it depends on being able to get at the |
|
59 // underlying file descriptor with filebuf::fd(), which is not |
|
60 // standard. |
|
61 |
|
62 static octave_procbuf *octave_procbuf_list = 0; |
|
63 |
|
64 octave_procbuf * |
|
65 octave_procbuf::open (const char *command, int mode) |
|
66 { |
|
67 #if defined (HAVE_SYS_WAIT_H) |
|
68 |
|
69 int pipe_fds[2]; |
|
70 |
3156
|
71 volatile int child_std_end = (mode & ios::in) ? 1 : 0; |
|
72 |
3147
|
73 volatile int parent_end, child_end; |
2094
|
74 |
|
75 if (is_open ()) |
|
76 return 0; |
|
77 |
|
78 if (pipe (pipe_fds) < 0) |
|
79 return 0; |
|
80 |
|
81 if (mode & ios::in) |
|
82 { |
|
83 parent_end = pipe_fds[0]; |
|
84 child_end = pipe_fds[1]; |
|
85 } |
|
86 else |
|
87 { |
|
88 parent_end = pipe_fds[1]; |
|
89 child_end = pipe_fds[0]; |
|
90 } |
|
91 |
3156
|
92 proc_pid = ::fork (); |
2094
|
93 |
|
94 if (proc_pid == 0) |
|
95 { |
|
96 ::close (parent_end); |
|
97 |
|
98 if (child_end != child_std_end) |
|
99 { |
3156
|
100 ::dup2 (child_end, child_std_end); |
2094
|
101 ::close (child_end); |
|
102 } |
|
103 |
|
104 while (octave_procbuf_list) |
|
105 { |
|
106 ::close (octave_procbuf_list->fd ()); |
|
107 octave_procbuf_list = octave_procbuf_list->next; |
|
108 } |
|
109 |
|
110 execl ("/bin/sh", "sh", "-c", command, NULL); |
|
111 |
|
112 exit (127); |
|
113 } |
|
114 |
3174
|
115 #if defined (HAVE_USLEEP) |
|
116 if (Vkluge_procbuf_delay > 0) |
|
117 usleep (Vkluge_procbuf_delay); |
|
118 #else |
|
119 if (Vkluge_procbuf_delay > 499999) |
|
120 sleep ((Vkluge_procbuf_delay + 500000) / 1000000); |
|
121 #endif |
|
122 |
2094
|
123 ::close (child_end); |
|
124 |
|
125 if (proc_pid < 0) |
|
126 { |
|
127 ::close (parent_end); |
|
128 return 0; |
|
129 } |
|
130 |
|
131 attach (parent_end); |
|
132 |
|
133 next = octave_procbuf_list; |
|
134 octave_procbuf_list = this; |
|
135 |
|
136 return this; |
|
137 |
|
138 #else |
|
139 |
|
140 return 0; |
|
141 |
|
142 #endif |
|
143 } |
|
144 |
|
145 int |
|
146 octave_procbuf::sys_close (void) |
|
147 { |
|
148 #if defined (HAVE_SYS_WAIT_H) |
|
149 |
|
150 pid_t wait_pid; |
|
151 |
|
152 int status = -1; |
|
153 |
|
154 for (octave_procbuf **ptr = &octave_procbuf_list; |
|
155 *ptr != 0; |
|
156 ptr = &(*ptr)->next) |
|
157 { |
|
158 if (*ptr == this) |
|
159 { |
|
160 *ptr = (*ptr)->next; |
|
161 status = 0; |
|
162 break; |
|
163 } |
|
164 } |
|
165 |
|
166 if (status < 0 || ::close (fd ()) < 0) |
|
167 return -1; |
|
168 |
|
169 do |
|
170 { |
3156
|
171 wait_pid = ::waitpid (proc_pid, &wstatus, 0); |
2094
|
172 } |
|
173 while (wait_pid == -1 && errno == EINTR); |
|
174 |
|
175 if (wait_pid == -1) |
|
176 return -1; |
|
177 |
|
178 return wstatus; |
|
179 |
|
180 #else |
|
181 |
|
182 return -1; |
|
183 |
|
184 #endif |
|
185 } |
|
186 |
3174
|
187 static int |
|
188 kluge_procbuf_delay (void) |
|
189 { |
|
190 double val; |
|
191 if (builtin_real_scalar_variable ("kluge_procbuf_delay", val) |
|
192 && ! xisnan (val)) |
|
193 { |
|
194 int ival = NINT (val); |
|
195 if (ival >= 0 && (double) ival == val) |
|
196 { |
|
197 Vkluge_procbuf_delay = ival; |
|
198 return 0; |
|
199 } |
|
200 } |
|
201 gripe_invalid_value_specified ("kluge_procbuf_delay"); |
|
202 return -1; |
|
203 } |
|
204 |
3195
|
205 void |
3174
|
206 symbols_of_oct_procbuf (void) |
|
207 { |
3176
|
208 DEFVAR (kluge_procbuf_delay, static_cast<double> (Vkluge_procbuf_delay), |
3258
|
209 kluge_procbuf_delay, |
3174
|
210 "number of microseconds to delay in the parent after forking"); |
|
211 } |
|
212 |
2094
|
213 /* |
|
214 ;;; Local Variables: *** |
|
215 ;;; mode: C++ *** |
|
216 ;;; End: *** |
|
217 */ |