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