2941
|
1 /* |
|
2 |
|
3 Copyright (C) 1996, 1997 John W. Eaton |
|
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. |
2941
|
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/>. |
2941
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
|
27 #include <cerrno> |
6482
|
28 #include <cstdlib> |
2941
|
29 |
|
30 #include <string.h> |
|
31 |
|
32 #ifdef HAVE_SYS_TYPES_H |
|
33 #include <sys/types.h> |
|
34 #endif |
|
35 |
|
36 #ifdef HAVE_UNISTD_H |
|
37 #include <unistd.h> |
|
38 #endif |
|
39 |
|
40 #ifdef HAVE_FCNTL_H |
|
41 #include <fcntl.h> |
|
42 #endif |
|
43 |
4299
|
44 // We can't use csignal as kill is not in the std namespace, and picky |
|
45 // compiler runtimes will also exclude it from global scope as well. |
|
46 |
|
47 #include <signal.h> |
|
48 |
5453
|
49 #include "lo-utils.h" |
6321
|
50 #include "lo-sysdep.h" |
2941
|
51 #include "oct-syscalls.h" |
|
52 #include "str-vec.h" |
|
53 |
|
54 #define NOT_SUPPORTED(nm) \ |
4062
|
55 nm ": not supported on this system" |
2941
|
56 |
|
57 int |
|
58 octave_syscalls::dup2 (int old_fd, int new_fd) |
|
59 { |
3504
|
60 std::string msg; |
2941
|
61 return dup2 (old_fd, new_fd, msg); |
|
62 } |
|
63 |
|
64 int |
3504
|
65 octave_syscalls::dup2 (int old_fd, int new_fd, std::string& msg) |
2941
|
66 { |
3504
|
67 msg = std::string (); |
2941
|
68 |
|
69 int status = -1; |
|
70 |
|
71 #if defined (HAVE_DUP2) |
|
72 status = ::dup2 (old_fd, new_fd); |
|
73 |
|
74 if (status < 0) |
3504
|
75 { |
|
76 using namespace std; |
|
77 msg = ::strerror (errno); |
|
78 } |
2941
|
79 #else |
|
80 msg = NOT_SUPPORTED ("dup2"); |
|
81 #endif |
|
82 |
|
83 return status; |
|
84 } |
|
85 |
|
86 int |
3504
|
87 octave_syscalls::execvp (const std::string& file, const string_vector& argv) |
2941
|
88 { |
3504
|
89 std::string msg; |
2941
|
90 return execvp (file, argv, msg); |
|
91 } |
|
92 |
|
93 int |
3504
|
94 octave_syscalls::execvp (const std::string& file, const string_vector& args, |
|
95 std::string& msg) |
2941
|
96 { |
3504
|
97 msg = std::string (); |
2941
|
98 |
|
99 int status = -1; |
|
100 |
|
101 #if defined (HAVE_EXECVP) |
|
102 char **argv = args.c_str_vec (); |
|
103 |
|
104 status = ::execvp (file.c_str (), argv); |
|
105 |
|
106 string_vector::delete_c_str_vec (argv); |
|
107 |
|
108 if (status < 0) |
3504
|
109 { |
|
110 using namespace std; |
|
111 msg = ::strerror (errno); |
|
112 } |
2941
|
113 #else |
|
114 msg = NOT_SUPPORTED ("execvp"); |
|
115 #endif |
|
116 |
|
117 return status; |
|
118 } |
|
119 |
|
120 int |
|
121 octave_syscalls::fcntl (int fd, int cmd, long arg) |
|
122 { |
3504
|
123 std::string msg; |
2941
|
124 return fcntl (fd, cmd, arg, msg); |
|
125 } |
|
126 |
|
127 int |
3504
|
128 octave_syscalls::fcntl (int fd, int cmd, long arg, std::string& msg) |
2941
|
129 { |
3504
|
130 msg = std::string (); |
2941
|
131 |
|
132 int status = -1; |
|
133 |
|
134 #if defined (HAVE_FCNTL) |
|
135 status = ::fcntl (fd, cmd, arg); |
|
136 |
|
137 if (status < 0) |
3504
|
138 { |
|
139 using namespace std; |
|
140 msg = ::strerror (errno); |
|
141 } |
2941
|
142 #else |
|
143 msg = NOT_SUPPORTED ("fcntl"); |
|
144 #endif |
|
145 |
|
146 return status; |
|
147 } |
|
148 |
|
149 pid_t |
3504
|
150 octave_syscalls::fork (std::string& msg) |
2941
|
151 { |
|
152 pid_t status = -1; |
|
153 |
|
154 #if defined (HAVE_FORK) |
|
155 status = ::fork (); |
|
156 |
|
157 if (status < 0) |
3504
|
158 { |
|
159 using namespace std; |
|
160 msg = ::strerror (errno); |
|
161 } |
2941
|
162 #else |
|
163 msg = NOT_SUPPORTED ("fork"); |
|
164 #endif |
|
165 |
|
166 return status; |
|
167 } |
|
168 |
|
169 pid_t |
3504
|
170 octave_syscalls::vfork (std::string& msg) |
3147
|
171 { |
|
172 pid_t status = -1; |
|
173 |
|
174 #if defined (HAVE_VFORK) || defined (HAVE_FORK) |
|
175 #if defined (HAVE_VFORK) |
|
176 status = ::vfork (); |
|
177 #else |
3148
|
178 status = ::fork (); |
3147
|
179 #endif |
|
180 |
|
181 if (status < 0) |
3504
|
182 { |
|
183 using namespace std; |
|
184 msg = ::strerror (errno); |
|
185 } |
3147
|
186 #else |
|
187 msg = NOT_SUPPORTED ("vfork"); |
|
188 #endif |
|
189 |
|
190 return status; |
|
191 } |
|
192 |
|
193 pid_t |
3504
|
194 octave_syscalls::getpgrp (std::string& msg) |
2941
|
195 { |
|
196 pid_t status = -1; |
|
197 |
|
198 #if defined (HAVE_GETPGRP) |
|
199 status = ::getpgrp (); |
|
200 |
|
201 if (status < 0) |
3504
|
202 { |
|
203 using namespace std; |
|
204 msg = ::strerror (errno); |
|
205 } |
2941
|
206 #else |
|
207 msg = NOT_SUPPORTED ("getpgrp"); |
|
208 #endif |
|
209 |
|
210 return status; |
|
211 } |
|
212 |
|
213 pid_t |
|
214 octave_syscalls::getpid (void) |
|
215 { |
|
216 #if defined (HAVE_GETPID) |
|
217 return ::getpid (); |
|
218 #else |
|
219 return 0; |
|
220 #endif |
|
221 } |
|
222 |
|
223 pid_t |
|
224 octave_syscalls::getppid (void) |
|
225 { |
|
226 #if defined (HAVE_GETPPID) |
|
227 return ::getppid (); |
|
228 #else |
|
229 return 0; |
|
230 #endif |
|
231 } |
|
232 |
|
233 gid_t |
|
234 octave_syscalls::getgid (void) |
|
235 { |
|
236 #if defined (HAVE_GETGID) |
|
237 return ::getgid (); |
|
238 #else |
|
239 return 0; |
|
240 #endif |
|
241 } |
|
242 |
|
243 gid_t |
|
244 octave_syscalls::getegid (void) |
|
245 { |
|
246 #if defined (HAVE_GETEGID) |
|
247 return ::getegid (); |
|
248 #else |
|
249 return 0; |
|
250 #endif |
|
251 } |
|
252 |
|
253 uid_t |
|
254 octave_syscalls::getuid (void) |
|
255 { |
|
256 #if defined (HAVE_GETUID) |
|
257 return ::getuid (); |
|
258 #else |
|
259 return 0; |
|
260 #endif |
|
261 } |
|
262 |
|
263 uid_t |
|
264 octave_syscalls::geteuid (void) |
|
265 { |
|
266 #if defined (HAVE_GETEUID) |
|
267 return ::geteuid (); |
|
268 #else |
|
269 return 0; |
|
270 #endif |
|
271 } |
|
272 |
|
273 int |
|
274 octave_syscalls::pipe (int *fildes) |
|
275 { |
3504
|
276 std::string msg; |
6123
|
277 return pipe (fildes, msg); |
2941
|
278 } |
|
279 |
|
280 int |
3504
|
281 octave_syscalls::pipe (int *fildes, std::string& msg) |
2941
|
282 { |
3504
|
283 msg = std::string (); |
2941
|
284 |
|
285 int status = -1; |
|
286 |
|
287 #if defined (HAVE_PIPE) |
|
288 status = ::pipe (fildes); |
|
289 |
|
290 if (status < 0) |
3504
|
291 { |
|
292 using namespace std; |
|
293 msg = ::strerror (errno); |
|
294 } |
2941
|
295 #else |
|
296 msg = NOT_SUPPORTED ("pipe"); |
|
297 #endif |
|
298 |
|
299 return status; |
|
300 } |
|
301 |
|
302 pid_t |
5453
|
303 octave_syscalls::waitpid (pid_t pid, int *status, int options) |
2941
|
304 { |
3504
|
305 std::string msg; |
5453
|
306 return waitpid (pid, status, options, msg); |
2941
|
307 } |
|
308 |
|
309 pid_t |
5453
|
310 octave_syscalls::waitpid (pid_t pid, int *status, int options, |
|
311 std::string& msg) |
2941
|
312 { |
6060
|
313 pid_t retval = -1; |
3504
|
314 msg = std::string (); |
2941
|
315 |
5453
|
316 #if defined (HAVE_WAITPID) |
6060
|
317 retval = ::octave_waitpid (pid, status, options); |
2941
|
318 |
5453
|
319 if (retval < 0) |
3504
|
320 { |
|
321 using namespace std; |
|
322 msg = ::strerror (errno); |
|
323 } |
2941
|
324 #else |
|
325 msg = NOT_SUPPORTED ("waitpid"); |
|
326 #endif |
|
327 |
5453
|
328 return retval; |
2941
|
329 } |
|
330 |
4294
|
331 int |
|
332 octave_syscalls::kill (pid_t pid, int sig) |
|
333 { |
|
334 std::string msg; |
|
335 return kill (pid, sig, msg); |
|
336 } |
|
337 |
|
338 int |
|
339 octave_syscalls::kill (pid_t pid, int sig, std::string& msg) |
|
340 { |
|
341 msg = std::string (); |
|
342 |
|
343 int status = -1; |
|
344 |
|
345 #if defined (HAVE_KILL) |
|
346 status = ::kill (pid, sig); |
|
347 |
|
348 if (status < 0) |
|
349 { |
|
350 using namespace std; |
|
351 msg = ::strerror (errno); |
|
352 } |
|
353 #else |
|
354 msg = NOT_SUPPORTED ("kill"); |
|
355 #endif |
|
356 |
|
357 return status; |
|
358 } |
|
359 |
6321
|
360 pid_t |
|
361 octave_syscalls::popen2 (const std::string& cmd, const string_vector& args, |
|
362 bool sync_mode, int *fildes) |
|
363 { |
|
364 std::string msg; |
|
365 bool interactive = false; |
|
366 return popen2 (cmd, args, sync_mode, fildes, msg, interactive); |
|
367 } |
|
368 |
|
369 pid_t |
|
370 octave_syscalls::popen2 (const std::string& cmd, const string_vector& args, |
|
371 bool sync_mode, int *fildes, std::string& msg) |
|
372 { |
|
373 bool interactive = false; |
|
374 return popen2 (cmd, args, sync_mode, fildes, msg, interactive); |
|
375 } |
|
376 |
|
377 pid_t |
|
378 octave_syscalls::popen2 (const std::string& cmd, const string_vector& args, |
|
379 bool sync_mode, int *fildes, std::string& msg, bool &interactive) |
|
380 { |
|
381 #if defined (__WIN32__) && ! defined (__CYGWIN__) |
|
382 return ::octave_popen2 (cmd, args, sync_mode, fildes, msg); |
|
383 #else |
|
384 pid_t pid; |
|
385 int child_stdin[2], child_stdout[2]; |
|
386 |
|
387 if (pipe (child_stdin, msg) == 0) |
|
388 { |
|
389 if (pipe (child_stdout, msg) == 0) |
|
390 { |
|
391 pid = fork (msg); |
|
392 if (pid < 0) |
|
393 msg = "popen2: process creation failed -- " + msg; |
|
394 else if (pid == 0) |
|
395 { |
|
396 std::string child_msg; |
|
397 |
|
398 interactive = false; |
|
399 |
|
400 // Child process |
|
401 ::close (child_stdin[1]); |
|
402 ::close (child_stdout[0]); |
|
403 |
|
404 if (dup2 (child_stdin[0], STDIN_FILENO) >= 0) |
|
405 { |
|
406 ::close (child_stdin[0]); |
|
407 if (dup2 (child_stdout[1], STDOUT_FILENO) >= 0) |
|
408 { |
|
409 ::close (child_stdout[1]); |
|
410 if (execvp (cmd, args, child_msg) < 0) |
|
411 child_msg = "popen2 (child): unable to start process -- " + child_msg; |
|
412 } |
|
413 else |
|
414 child_msg = "popen2 (child): file handle duplication failed -- " + child_msg; |
|
415 } |
|
416 else |
|
417 child_msg = "popen2 (child): file handle duplication failed -- " + child_msg; |
|
418 |
|
419 (*current_liboctave_error_handler)(child_msg.c_str()); |
|
420 |
|
421 exit(0); |
|
422 } |
|
423 else |
|
424 { |
|
425 // Parent process |
|
426 ::close (child_stdin[0]); |
|
427 ::close (child_stdout[1]); |
|
428 #if defined (F_SETFL) && defined (O_NONBLOCK) |
|
429 if (! sync_mode && fcntl (child_stdout[0], F_SETFL, O_NONBLOCK, msg) < 0) |
|
430 msg = "popen2: error setting file mode -- " + msg; |
|
431 else |
|
432 #endif |
|
433 { |
|
434 fildes[0] = child_stdin [1]; |
|
435 fildes[1] = child_stdout [0]; |
|
436 return pid; |
|
437 } |
|
438 } |
|
439 ::close (child_stdout[0]); |
|
440 ::close (child_stdout[1]); |
|
441 } |
|
442 else |
|
443 msg = "popen2: pipe creation failed -- " + msg; |
|
444 ::close (child_stdin[0]); |
|
445 ::close (child_stdin[1]); |
|
446 } |
|
447 else |
|
448 msg = "popen2: pipe creation failed -- " + msg; |
|
449 |
|
450 return -1; |
|
451 #endif |
|
452 } |
|
453 |
2941
|
454 /* |
|
455 ;;; Local Variables: *** |
|
456 ;;; mode: C++ *** |
|
457 ;;; End: *** |
|
458 */ |