GNU Octave  6.2.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
oct-syscalls.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1996-2021 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if defined (HAVE_CONFIG_H)
27 # include "config.h"
28 #endif
29 
30 #include <ctime>
31 #include <cerrno>
32 #include <cstdlib>
33 #include <cstring>
34 
35 #include "fcntl-wrappers.h"
36 #include "lo-utils.h"
37 #include "lo-sysdep.h"
38 #include "oct-syscalls.h"
39 #include "octave-popen2.h"
40 #include "signal-wrappers.h"
41 #include "str-vec.h"
42 #include "unistd-wrappers.h"
43 #include "wait-wrappers.h"
44 
45 #define NOT_SUPPORTED(nm) \
46  nm ": not supported on this system"
47 
48 namespace octave
49 {
50  namespace sys
51  {
52  int
53  dup2 (int old_fd, int new_fd)
54  {
55  std::string msg;
56  return sys::dup2 (old_fd, new_fd, msg);
57  }
58 
59  int
60  dup2 (int old_fd, int new_fd, std::string& msg)
61  {
62  msg = "";
63 
64  int status = -1;
65 
66  status = octave_dup2_wrapper (old_fd, new_fd);
67 
68  if (status < 0)
69  msg = std::strerror (errno);
70 
71  return status;
72  }
73 
74  int
75  execvp (const std::string& file, const string_vector& argv)
76  {
77  std::string msg;
78  return sys::execvp (file, argv, msg);
79  }
80 
81  int
82  execvp (const std::string& file, const string_vector& args,
83  std::string& msg)
84  {
85  msg = "";
86 
87  char **argv = args.c_str_vec ();
88 
89  int status = octave_execvp_wrapper (file.c_str (), argv);
90 
92 
93  if (status < 0)
94  msg = std::strerror (errno);
95 
96  return status;
97  }
98 
99  pid_t
100  fork (std::string& msg)
101  {
102  pid_t status = -1;
103 
104  if (octave_have_fork ())
105  {
106  status = octave_fork_wrapper ();
107 
108  if (status < 0)
109  msg = std::strerror (errno);
110  }
111  else
112  msg = NOT_SUPPORTED ("fork");
113 
114  return status;
115  }
116 
117  pid_t
118  vfork (std::string& msg)
119  {
120  pid_t status = -1;
121 
122  if (octave_have_vfork () || octave_have_fork ())
123  {
124  if (octave_have_vfork ())
125  status = octave_vfork_wrapper ();
126  else
127  status = octave_fork_wrapper ();
128 
129  if (status < 0)
130  msg = std::strerror (errno);
131  }
132  else
133  msg = NOT_SUPPORTED ("vfork");
134 
135  return status;
136  }
137 
138  pid_t
139  getpgrp (std::string& msg)
140  {
141  pid_t status = octave_getpgrp_wrapper ();
142 
143  if (status < 0)
144  msg = std::strerror (errno);
145 
146  return status;
147  }
148 
149  pid_t
150  getpid (void)
151  {
152  return octave_getpid_wrapper ();
153  }
154 
155  pid_t
156  getppid (void)
157  {
158  return octave_getppid_wrapper ();
159  }
160 
161  gid_t
162  getgid (void)
163  {
164  return octave_getgid_wrapper ();
165  }
166 
167  gid_t
168  getegid (void)
169  {
170  return octave_getegid_wrapper ();
171  }
172 
173  uid_t
174  getuid (void)
175  {
176  return octave_getuid_wrapper ();
177  }
178 
179  uid_t
180  geteuid (void)
181  {
182  return octave_geteuid_wrapper ();
183  }
184 
185  int
186  pipe (int *fildes)
187  {
188  std::string msg;
189  return sys::pipe (fildes, msg);
190  }
191 
192  int
193  pipe (int *fildes, std::string& msg)
194  {
195  msg = "";
196 
197  int status = -1;
198 
199  status = octave_pipe_wrapper (fildes);
200 
201  if (status < 0)
202  msg = std::strerror (errno);
203 
204  return status;
205  }
206 
207  pid_t
208  waitpid (pid_t pid, int *status, int options)
209  {
210  std::string msg;
211  return sys::waitpid (pid, status, options, msg);
212  }
213 
214  pid_t
215  waitpid (pid_t pid, int *status, int options,
216  std::string& msg)
217  {
218  pid_t retval = -1;
219  msg = "";
220 
221  retval = octave_waitpid_wrapper (pid, status, options);
222 
223  if (retval < 0)
224  msg = std::strerror (errno);
225 
226  return retval;
227  }
228 
229  int
230  wcontinue (void)
231  {
232  return octave_wcontinue_wrapper ();
233  }
234 
235  int
236  wcoredump (int status)
237  {
238  return octave_wcoredump_wrapper (status);
239  }
240 
241  bool
242  wifcontinued (int status)
243  {
244  return octave_wifcontinued_wrapper (status);
245  }
246 
247  bool
248  wifexited (int status)
249  {
250  return octave_wifexited_wrapper (status);
251  }
252 
253  bool
254  wifsignaled (int status)
255  {
256  return octave_wifsignaled_wrapper (status);
257  }
258 
259  bool
260  wifstopped (int status)
261  {
262  return octave_wifstopped_wrapper (status);
263  }
264 
265  int
266  wexitstatus (int status)
267  {
268  return octave_wexitstatus_wrapper (status);
269  }
270 
271  int
272  wnohang (void)
273  {
274  return octave_wnohang_wrapper ();
275  }
276 
277  int
278  wstopsig (int status)
279  {
280  return octave_wstopsig_wrapper (status);
281  }
282 
283  int
284  wtermsig (int status)
285  {
286  return octave_wtermsig_wrapper (status);
287  }
288 
289  int
290  wuntraced (void)
291  {
292  return octave_wuntraced_wrapper ();
293  }
294 
295  int
296  kill (pid_t pid, int sig)
297  {
298  std::string msg;
299  return sys::kill (pid, sig, msg);
300  }
301 
302  int
303  kill (pid_t pid, int sig, std::string& msg)
304  {
305  msg = "";
306 
307  int status = -1;
308 
309  if (octave_have_kill ())
310  {
311  status = octave_kill_wrapper (pid, sig);
312 
313  if (status < 0)
314  msg = std::strerror (errno);
315  }
316  else
317  msg = NOT_SUPPORTED ("kill");
318 
319  return status;
320  }
321 
322  pid_t
323  popen2 (const std::string& cmd, const string_vector& args,
324  bool sync_mode, int *fildes)
325  {
326  std::string msg;
327  return sys::popen2 (cmd, args, sync_mode, fildes, msg);
328  }
329 
330  pid_t
331  popen2 (const std::string& cmd, const string_vector& args,
332  bool sync_mode, int *fildes, std::string& msg)
333  {
334  char **argv = args.c_str_vec ();
335  const char *errmsg;
336 
337  pid_t pid = octave_popen2 (cmd.c_str (), argv, sync_mode, fildes,
338  &errmsg);
339 
341 
342  if (pid < 0)
343  msg = errmsg;
344 
345  return pid;
346  }
347 
348  int
349  fcntl (int fd, int cmd, long arg)
350  {
351  std::string msg;
352  return sys::fcntl (fd, cmd, arg, msg);
353  }
354 
355  int
356  fcntl (int fd, int cmd, long arg, std::string& msg)
357  {
358  msg = "";
359 
360  int status = -1;
361 
362  status = octave_fcntl_wrapper (fd, cmd, arg);
363 
364  if (status < 0)
365  msg = std::strerror (errno);
366 
367  return status;
368  }
369  }
370 }
char ** c_str_vec(void) const
Definition: str-vec.cc:157
static void delete_c_str_vec(const char *const *)
Definition: str-vec.cc:185
int octave_fcntl_wrapper(int fd, int cmd, int arg)
pid_t getppid(void)
int wexitstatus(int status)
int wtermsig(int status)
pid_t getpid(void)
int pipe(int *fildes)
gid_t getegid(void)
gid_t getgid(void)
pid_t getpgrp(std::string &msg)
bool wifstopped(int status)
pid_t popen2(const std::string &cmd, const string_vector &args, bool sync_mode, int *fildes)
int execvp(const std::string &file, const string_vector &argv)
Definition: oct-syscalls.cc:75
int wstopsig(int status)
bool wifsignaled(int status)
int kill(pid_t pid, int sig)
pid_t waitpid(pid_t pid, int *status, int options)
bool wifcontinued(int status)
int wcontinue(void)
uid_t getuid(void)
int fcntl(int fd, int cmd, long arg)
int dup2(int old_fd, int new_fd)
Definition: oct-syscalls.cc:53
int wnohang(void)
bool wifexited(int status)
uid_t geteuid(void)
int wcoredump(int status)
pid_t fork(std::string &msg)
int wuntraced(void)
pid_t vfork(std::string &msg)
#define NOT_SUPPORTED(nm)
Definition: oct-syscalls.cc:45
pid_t octave_popen2(const char *cmd, char *const *args, bool sync_mode, int *fildes, const char **errmsg)
octave_value::octave_value(const Array< char > &chm, char type) return retval
Definition: ov.cc:811
int octave_kill_wrapper(pid_t pid, int signum)
bool octave_have_kill(void)
gid_t octave_getgid_wrapper(void)
uid_t octave_geteuid_wrapper(void)
pid_t octave_fork_wrapper(void)
pid_t octave_getpgrp_wrapper(void)
pid_t octave_getppid_wrapper(void)
bool octave_have_vfork(void)
int octave_execvp_wrapper(const char *file, char *const *argv)
gid_t octave_getegid_wrapper(void)
pid_t octave_getpid_wrapper(void)
uid_t octave_getuid_wrapper(void)
pid_t octave_vfork_wrapper(void)
bool octave_have_fork(void)
int octave_dup2_wrapper(int fd1, int fd2)
int octave_pipe_wrapper(int *fd)
int octave_wuntraced_wrapper(void)
Definition: wait-wrappers.c:86
pid_t octave_waitpid_wrapper(pid_t pid, int *statusp, int options)
Definition: wait-wrappers.c:57
int octave_wcoredump_wrapper(int status)
bool octave_wifsignaled_wrapper(int status)
bool octave_wifcontinued_wrapper(int status)
bool octave_wifstopped_wrapper(int status)
int octave_wexitstatus_wrapper(int status)
int octave_wcontinue_wrapper(void)
Definition: wait-wrappers.c:74
bool octave_wifexited_wrapper(int status)
int octave_wtermsig_wrapper(int status)
int octave_wstopsig_wrapper(int status)
int octave_wnohang_wrapper(void)
Definition: wait-wrappers.c:80