Commit 02fd3fecd51c32e0c589423ddcdf8a9c5e5e9ba7

SSE4 2019-02-22T02:49:49

- initial implementation Signed-off-by: SSE4 <tomskside@gmail.com>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
diff --git a/CMakeLists.txt b/CMakeLists.txt
new file mode 100644
index 0000000..d7fea7a
--- /dev/null
+++ b/CMakeLists.txt
@@ -0,0 +1,7 @@
+cmake_minimum_required(VERSION 2.8)
+
+project(sys_wait_h)
+
+add_executable(example example.c sys/wait.h)
+
+target_include_directories(example PRIVATE ${CMAKE_SOURCE_DIR})
diff --git a/example.c b/example.c
new file mode 100644
index 0000000..7ac818e
--- /dev/null
+++ b/example.c
@@ -0,0 +1,33 @@
+#define _XOPEN_SOURCE
+
+#include <stdlib.h>
+#include <sys/wait.h>
+
+#include <windows.h>
+#include <tchar.h>
+
+void run_process(TCHAR * name)
+{
+    STARTUPINFO si;
+    memset(&si, 0, sizeof(si));
+    si.cb = sizeof(STARTUPINFO);
+
+    PROCESS_INFORMATION pi;
+    memset(&pi, 0, sizeof(pi));
+
+    if (CreateProcess(NULL, name, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
+    {
+        CloseHandle(pi.hProcess);
+        CloseHandle(pi.hThread);
+    }
+}
+
+int main(int argc, char * argv[])
+{
+    run_process(_T("notepad.exe"));
+    run_process(_T("mspaint.exe"));
+
+    while (wait(0) > 0) {}
+
+    return EXIT_SUCCESS;
+}
diff --git a/sys/wait.h b/sys/wait.h
index e69de29..5461f2f 100644
--- a/sys/wait.h
+++ b/sys/wait.h
@@ -0,0 +1,375 @@
+/*
+MIT License
+
+Copyright (c) 2019 win32ports
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+*/
+
+#pragma once
+
+#ifndef __SYS_WAIT_H_B07AB843_5FF0_4FCF_B6F0_8CB97D28F337__
+#define __SYS_WAIT_H_B07AB843_5FF0_4FCF_B6F0_8CB97D28F337__
+
+#ifndef _WIN32
+
+#pragma message("this sys/wait.h implementation is for Windows only!")
+
+#else /* _WIN32 */
+
+#ifdef __cplusplus
+//extern "C" {
+#endif /* __cplusplus */
+
+#ifdef _MSC_VER
+#include <winsock.h> /* for timeval */
+#endif /* _MSC_VER */
+
+#ifdef __GNUC__
+#include <sys/time.h> /* for timeval */
+#endif /* __GNUC__  */
+
+#include <errno.h>
+
+#include <TlHelp32.h>
+
+#ifndef WNOHANG
+#define WNOHANG 1
+#endif /* WNOHANG */
+
+#ifndef WUNTRACED
+#define WUNTRACED 2
+#endif /* WUNTRACED */
+
+#ifndef __WEXITSTATUS
+#define __WEXITSTATUS(status) (((status) & 0xFF00) >> 8)
+#endif /* __WEXITSTATUS */
+
+#ifndef __WIFEXITED
+#define __WIFEXITED(status) (__WTERMSIG(status) == 0)
+#endif /* __WIFEXITED */
+
+#ifndef __WTERMSIG
+#define __WTERMSIG(status) ((status) & 0x7F)
+#endif /* __WTERMSIG */
+
+#ifndef __WIFSIGNALED
+#define __WIFSIGNALED(status) (((signed char)(__WTERMSIG(status) + 1) >> 1) > 0)
+#endif /* __WIFSIGNALED */
+
+#ifndef __WIFSTOPPED
+#define __WIFSTOPPED(status) (((status) & 0xFF) == 0x7F)
+#endif /* __WIFSTOPPED */
+
+#ifndef __WSTOPSIG
+#define __WSTOPSIG(status) __WEXITSTATUS(status)
+#endif /* __WSTOPSIG */
+
+#ifndef __WCONTINUED
+#define __WCONTINUED 8
+#endif /* __WCONTINUED */
+
+#ifndef __WNOWAIT
+#define __WNOWAIT 0x01000000
+#endif /* __WNOWAIT */
+
+#ifndef WEXITSTATUS
+#define WEXITSTATUS(status) __WEXITSTATUS(status)
+#endif /* WEXITSTATUS */
+
+#ifndef WIFEXITED
+#define WIFEXITED(status) __WIFEXITED(status)
+#endif /* WIFEXITED */
+
+#ifndef WIFSIGNALED
+#define WIFSIGNALED(status) __WIFSIGNALED(status)
+#endif /* WIFSIGNALED */
+
+#ifndef WTERMSIG
+#define WTERMSIG(status) __WTERMSIG(status)
+#endif /* WTERMSIG */
+
+#ifndef WIFSTOPPED
+#define WIFSTOPPED(status) __WIFSTOPPED(status)
+#endif /* WIFSTOPPED */
+
+#ifndef WSTOPSIG
+#define WSTOPSIG(status) __WSTOPSIG(status)
+#endif /* WSTOPSIG */
+
+#ifndef __pid_t_defined
+#define __pid_t_defined 1
+typedef int __pid_t;
+typedef __pid_t pid_t;
+#endif /* __pid_t_defined */
+
+#ifndef __id_t_defined
+#define __id_t_defined 1
+typedef unsigned __id_t;
+typedef __id_t id_t;
+#endif /* __id_t_defined */
+
+#ifndef __uid_t_defined
+#define __uid_t_defined 1
+typedef unsigned __uid_t;
+typedef __uid_t uid_t;
+#endif /* __uid_t_defined */
+
+#ifndef __siginfo_t_defined
+#define __siginfo_t_defined 1
+typedef struct
+{
+    int si_signo; /* signal number */
+    int si_code; /* signal code */
+    int si_errno; /* if non-zero, errno associated with this signal, as defined in <errno.h> */
+    pid_t si_pid; /* sending process ID */
+    uid_t si_uid; /* real user ID of sending process */
+    void * si_addr; /* address of faulting instruction */
+    int si_status; /* exit value of signal */
+    long si_band; /* band event of SIGPOLL */
+}
+siginfo_t;
+#endif /* __siginfo_t_defined */
+
+struct rusage
+{
+    struct timeval ru_utime; /* user time used */
+    struct timeval ru_stime; /* system time used */
+};
+
+#ifdef _XOPEN_SOURCE
+
+#ifndef __W_CONTINUED
+#define __W_CONTINUED 0xFFFF
+#endif /* __W_CONTINUED */
+
+#ifndef __WIFCONTINUED
+#define __WIFCONTINUED(status) ((status) == __W_CONTINUED)
+#endif /* __WIFCONTINUED */
+
+#ifndef WIFCONTINUED
+#define WIFCONTINUED(status) __WIFCONTINUED(status)
+#endif /* WIFCONTINUED */
+
+#ifndef WSTOPPED
+#define WSTOPPED 2
+#endif /* WSTOPPED */
+
+#ifndef WEXITED
+#define WEXITED 4
+#endif /* WEXITED */
+
+#ifndef WCONTINUED
+#define WCONTINUED __WCONTINUED
+#endif /* WCONTINUED */
+
+#ifndef WNOWAIT
+#define WNOWAIT __WNOWAIT
+#endif /* WNOWAIT */
+
+typedef enum
+{
+    P_ALL,
+    P_PID,
+    P_PGID
+}
+idtype_t;
+
+#endif /* _XOPEN_SOURCE  */
+
+int __filter_anychild(PROCESSENTRY32 * pe, DWORD pid)
+{
+    return pe->th32ParentProcessID == GetCurrentProcessId();
+}
+
+int __filter_pid(PROCESSENTRY32 * pe, DWORD pid)
+{
+    return pe->th32ProcessID == pid;
+}
+
+void __filetime2timeval(FILETIME time, struct timeval * out)
+{
+    ULONGLONG value = time.dwHighDateTime;
+    value = (value << 32) | time.dwLowDateTime;
+    out->tv_sec = (long)(value / 1000000);
+    out->tv_usec = (long)(value % 1000000);
+}
+
+static int __waitpid_internal(pid_t pid, int * status, int options, siginfo_t * infop, struct rusage * rusage)
+{
+    int retval = 0;
+    int saved_status = 0;
+    HANDLE hProcess = INVALID_HANDLE_VALUE, hSnapshot = INVALID_HANDLE_VALUE;
+    int (*filter)(PROCESSENTRY32*, DWORD);
+    PROCESSENTRY32 pe;
+    DWORD wait_status = 0, exit_code = 0;
+    int untraced = WUNTRACED == (WUNTRACED & options);
+    int nowait = __WNOWAIT == (__WNOWAIT & options);
+    int continued = __WCONTINUED == (__WCONTINUED & options);
+    int nohang = WNOHANG == (WNOHANG & options);
+    options &= ~(WUNTRACED | __WNOWAIT | __WCONTINUED | WNOHANG);
+    if (options)
+    {
+        errno = -EINVAL;
+        return -1;
+    }
+
+    if (pid == -1)
+    {
+        /* wait for any child */
+        filter = __filter_anychild;
+    }
+    else if (pid < -1)
+    {
+        /* wait for any process from the group */
+        int group_id = -pid;
+        abort(); /* not implemented */
+    }
+    else if (pid == 0)
+    {
+        /* wait for any process from the current group */
+        abort(); /* not implemented */
+    }
+    else
+    {
+        /* wait for process with given pid */
+        filter = __filter_pid;
+    }
+
+    hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
+    if (INVALID_HANDLE_VALUE == hSnapshot)
+    {
+        errno = ECHILD;
+        return -1;
+    }
+    pe.dwSize = sizeof(pe);
+    if (!Process32First(hSnapshot, &pe))
+    {
+        CloseHandle(hSnapshot);
+        errno = ECHILD;
+        return -1;
+    }
+    do
+    {
+        if (filter(&pe, pid))
+        {    
+            hProcess = OpenProcess(SYNCHRONIZE | PROCESS_QUERY_INFORMATION, FALSE, pe.th32ProcessID);
+            if (INVALID_HANDLE_VALUE == hProcess)
+            {
+                CloseHandle(hSnapshot);
+                errno = ECHILD;
+                return -1;
+            }
+            break;
+        }
+    }
+    while (Process32Next(hSnapshot, &pe));
+    if (INVALID_HANDLE_VALUE == hProcess)
+    {
+        CloseHandle(hSnapshot);
+        errno = ECHILD;
+        return -1;
+    }
+
+    wait_status = WaitForSingleObject(hProcess, nohang ? 0 : INFINITE);
+
+    if (WAIT_OBJECT_0 == wait_status)
+    {
+        if (GetExitCodeProcess(hProcess, &exit_code))
+            saved_status |= (exit_code & 0xFF) << 8;
+    }
+    else if (WAIT_TIMEOUT == wait_status && nohang)
+    {
+        return 0;
+    }
+    else
+    {
+        CloseHandle(hProcess);
+        CloseHandle(hSnapshot);
+        errno = ECHILD;
+        return -1;
+    }
+    if (rusage)
+    {
+        memset(rusage, 0, sizeof(*rusage));
+        FILETIME creation_time, exit_time, kernel_time, user_time;
+        if (GetProcessTimes(hProcess, &creation_time, &exit_time, &kernel_time, &user_time))
+        {
+             __filetime2timeval(kernel_time, &rusage->ru_stime);
+             __filetime2timeval(user_time, &rusage->ru_utime);
+        }
+    }
+    if (infop)
+    {
+        memset(infop, 0, sizeof(*infop));
+    }
+
+    CloseHandle(hProcess);
+    CloseHandle(hSnapshot);
+
+    if (status)
+        *status = saved_status;
+
+    return pe.th32ParentProcessID;
+}
+
+static int waitpid(pid_t pid, int * status, int options)
+{
+    return __waitpid_internal(pid, status, options, NULL, NULL);
+}
+
+static int wait(int *status)
+{
+    return __waitpid_internal(-1, status, 0, NULL, NULL);
+}
+
+#ifdef _XOPEN_SOURCE
+
+int waitid(idtype_t idtype, id_t id, siginfo_t * infop, int options)
+{
+    pid_t pid;
+    switch (idtype)
+    {
+    case P_PID: pid = id; break;
+    case P_PGID: pid = -(pid_t)id; break;
+    case P_ALL: pid = 0; break;
+    default: errno = EINVAL; return -1;
+    }
+    return __waitpid_internal(pid, NULL, options, infop, NULL);
+}
+
+pid_t wait3(int * status, int options, struct rusage * rusage)
+{
+    return __waitpid_internal(-1, status, options, NULL, rusage);
+}
+
+pid_t wait4(pid_t pid, int * status, int options, struct rusage * rusage)
+{
+    return __waitpid_internal(pid, status, options, NULL, rusage);
+}
+
+#endif /* _XOPEN_SOURCE */
+
+#ifdef __cplusplus
+//}
+#endif /* __cplusplus */
+
+#endif /* _WIN32 */
+
+#endif /* __SYS_WAIT_H_B07AB843_5FF0_4FCF_B6F0_8CB97D28F337__ */