GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
wait-wrappers.c
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2016-2024 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 // These functions may be provided by gnulib. We don't include gnulib
27 // headers directly in Octave's C++ source files to avoid problems that
28 // may be caused by the way that gnulib overrides standard library
29 // functions.
30 
31 #if defined (HAVE_CONFIG_H)
32 # include "config.h"
33 #endif
34 
35 #include <sys/types.h>
36 #include <sys/wait.h>
37 
38 #if defined (__WIN32__) && ! defined (__CYGWIN__)
39 # include <windows.h>
40 #endif
41 
42 #include "wait-wrappers.h"
43 
44 #if ! defined (WCONTINUE)
45 # define WCONTINUE 0
46 #endif
47 
48 #if ! defined (WNOHANG)
49 # define WNOHANG 0
50 #endif
51 
52 #if ! defined (WUNTRACED)
53 # define WUNTRACED 0
54 #endif
55 
56 #if ! defined (WIFCONTINUED)
57 # define WIFCONTINUED(x) false
58 #endif
59 
60 pid_t
61 octave_waitpid_wrapper (pid_t pid, int *statusp, int options)
62 {
63 #if defined (__WIN32__) && ! defined (__CYGWIN__)
64  // gnulib's waitpid replacement currently uses _cwait, which
65  // apparently only works with console applications.
66  // Implement our own wrapper using win32 API functions.
67 
68  octave_unused_parameter (options);
69 
70  pid_t retval = -1;
71  DWORD status = 0;
72 
73  HANDLE hProcess = OpenProcess (PROCESS_QUERY_INFORMATION | SYNCHRONIZE,
74  false, pid);
75 
76  if (! hProcess)
77  return retval;
78 
79  if (WaitForSingleObject (hProcess, INFINITE) != WAIT_OBJECT_0)
80  {
81  CloseHandle (hProcess);
82  return retval;
83  }
84 
85  if (! GetExitCodeProcess (hProcess, &status))
86  {
87  CloseHandle (hProcess);
88  return retval;
89  }
90 
91  CloseHandle (hProcess);
92 
93  if (statusp)
94  *statusp = status;
95 
96  retval = pid;
97 
98  return retval;
99 
100 #else
101  return waitpid (pid, statusp, options);
102 #endif
103 }
104 
105 int
107 {
108  return WCONTINUE;
109 }
110 
111 int
113 {
114  return WNOHANG;
115 }
116 
117 int
119 {
120  return WUNTRACED;
121 }
122 
123 #if defined (HAVE_PRAGMA_GCC_DIAGNOSTIC)
124 // Disable the unused parameter warning for the following wrapper functions.
125 // The <sys/wait.h> header provided by gnulib may define some of the W*
126 // macros to expand to a constant and ignore the parameter.
127 # pragma GCC diagnostic push
128 # pragma GCC diagnostic ignored "-Wunused-parameter"
129 #endif
130 
131 int
133 {
134  return WCOREDUMP (status);
135 }
136 
137 int
139 {
140  return WEXITSTATUS (status);
141 }
142 
143 bool
145 {
146  return WIFCONTINUED (status);
147 }
148 
149 bool
151 {
152  return WIFEXITED (status);
153 }
154 
155 bool
157 {
158  return WIFSIGNALED (status);
159 }
160 
161 bool
163 {
164  return WIFSTOPPED (status);
165 }
166 
167 int
169 {
170  return WSTOPSIG (status);
171 }
172 
173 int
175 {
176  return WTERMSIG (status);
177 }
178 
179 #if defined (HAVE_PRAGMA_GCC_DIAGNOSTIC)
180 // Restore prevailing warning state for remainder of the file.
181 # pragma GCC diagnostic pop
182 #endif
pid_t waitpid(pid_t pid, int *status, int options)
int octave_wuntraced_wrapper(void)
pid_t octave_waitpid_wrapper(pid_t pid, int *statusp, int options)
Definition: wait-wrappers.c:61
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)
bool octave_wifexited_wrapper(int status)
#define WIFCONTINUED(x)
Definition: wait-wrappers.c:57
int octave_wtermsig_wrapper(int status)
int octave_wstopsig_wrapper(int status)
int octave_wnohang_wrapper(void)
#define WCONTINUE
Definition: wait-wrappers.c:45
#define WUNTRACED
Definition: wait-wrappers.c:53
#define WNOHANG
Definition: wait-wrappers.c:49