Hash :
229dee8d
Author :
Date :
2019-05-12T16:29:13
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 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
/*
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 __GNUC__
#include <sys/time.h> /* for timeval */
#endif /* __GNUC__ */
#include <errno.h>
#include <string.h>
#ifndef _INC_WINDOWS
typedef unsigned long DWORD;
typedef DWORD * LPDWORD;
#ifdef _WIN64
typedef long long LONG_PTR;
typedef unsigned long long ULONG_PTR;
#else /* _WIN64 */
typedef long LONG_PTR;
typedef unsigned long ULONG_PTR;
#endif
typedef long LONG;
typedef wchar_t WCHAR;
typedef int BOOL;
typedef void VOID;
typedef void * HANDLE;
#ifndef MAX_PATH
#define MAX_PATH 260
#endif /* MAX_PATH */
#ifndef WINAPI
#define WINAPI __stdcall
#endif /* WINAPI */
#ifndef DECLSPEC_DLLIMPORT
#ifdef _MSC_VER
#define DECLSPEC_DLLIMPORT __declspec(dllimport)
#else /* _MSC_VER */
#define DECLSPEC_DLLIMPORT
#endif /* _MSC_VER */
#endif /* DECLSPEC_DLLIMPORT */
#ifndef WINBASEAPI
#define WINBASEAPI DECLSPEC_DLLIMPORT
#endif /* WINBASEAPI */
#ifndef INVALID_HANDLE_VALUE
#define INVALID_HANDLE_VALUE ((HANDLE)(LONG_PTR)-1)
#endif /* INVALID_HANDLE_VALUE */
#ifndef SYNCHRONIZE
#define SYNCHRONIZE (0x00100000L)
#endif /* SYNCHRONIZE */
#ifndef PROCESS_QUERY_INFORMATION
#define PROCESS_QUERY_INFORMATION (0x0400)
#endif /* PROCESS_QUERY_INFORMATION */
#ifndef INFINITE
#define INFINITE 0xFFFFFFFF
#endif /* INFINITE */
#ifndef WAIT_OBJECT_0
#define WAIT_OBJECT_0 0
#endif /* WAIT_OBJECT_0 */
#ifndef WAIT_TIMEOUT
#define WAIT_TIMEOUT 258L
#endif /* WAIT_TIMEOUT */
/*
FIXME: causes FILETIME to conflict
#ifndef _FILETIME_
#define _FILETIME_
typedef struct FILETIME {
DWORD dwLowDateTime;
DWORD dwHighDateTime;
} FILETIME, *PFILETIME, *LPFILETIME;
#endif
*/
WINBASEAPI DWORD WINAPI GetCurrentProcessId(VOID);
WINBASEAPI BOOL WINAPI CloseHandle(HANDLE hObject);
WINBASEAPI HANDLE WINAPI OpenProcess(DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD dwProcessId);
WINBASEAPI DWORD WINAPI WaitForSingleObject(HANDLE hHandle, DWORD dwMilliseconds);
WINBASEAPI BOOL WINAPI GetExitCodeProcess(HANDLE hProcess, LPDWORD lpExitCode);
/*
FIXME: causes FILETIME to conflict
WINBASEAPI BOOL WINAPI GetProcessTimes(HANDLE hProcess, LPFILETIME lpCreationTime, LPFILETIME lpExitTime, LPFILETIME lpKernelTime, LPFILETIME lpUserTime);
*/
#endif /* _INC_WINDOWS */
#ifndef _WINSOCKAPI_
struct private_timeval {
long tv_sec;
long tv_usec;
};
#define timeval private_timeval
#endif /* _WINSOCKAPI_ */
#ifndef _INC_TOOLHELP32
typedef struct tagPROCESSENTRY32W
{
DWORD dwSize;
DWORD cntUsage;
DWORD th32ProcessID;
ULONG_PTR th32DefaultHeapID;
DWORD th32ModuleID;
DWORD cntThreads;
DWORD th32ParentProcessID;
LONG pcPriClassBase;
DWORD dwFlags;
WCHAR szExeFile[MAX_PATH];
} PROCESSENTRY32W;
typedef PROCESSENTRY32W * PPROCESSENTRY32W;
typedef PROCESSENTRY32W * LPPROCESSENTRY32W;
#ifndef TH32CS_SNAPPROCESS
#define TH32CS_SNAPPROCESS 2
#endif /* TH32CS_SNAPPROCESS */
HANDLE WINAPI CreateToolhelp32Snapshot(DWORD dwFlags, DWORD th32ProcessID);
BOOL WINAPI Process32FirstW(HANDLE hSnapshot, LPPROCESSENTRY32W lppe);
BOOL WINAPI Process32NextW(HANDLE hSnapshot, LPPROCESSENTRY32W lppe);
#endif /* _INC_TOOLHELP32 */
#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 */
#if !defined(__pid_t_defined) && !defined(_PID_T_) && !defined(pid_t)
#define __pid_t_defined 1
#define _PID_T_
typedef int __pid_t;
typedef __pid_t pid_t;
#endif /* !defined(__pid_t_defined) && !defined(_PID_T_) && !defined(pid_t) */
#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 */
static int __filter_anychild(PROCESSENTRY32W * pe, DWORD pid)
{
return pe->th32ParentProcessID == GetCurrentProcessId();
}
static int __filter_pid(PROCESSENTRY32W * pe, DWORD pid)
{
return pe->th32ProcessID == pid;
}
/*
FIXME: causes FILETIME to conflict
static void __filetime2timeval(FILETIME time, struct timeval * out)
{
unsigned long long 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 saved_status = 0;
HANDLE hProcess = INVALID_HANDLE_VALUE, hSnapshot = INVALID_HANDLE_VALUE;
int (*filter)(PROCESSENTRY32W*, DWORD);
PROCESSENTRY32W pe;
DWORD wait_status = 0, exit_code = 0;
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 */
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 (!Process32FirstW(hSnapshot, &pe))
{
CloseHandle(hSnapshot);
errno = ECHILD;
return -1;
}
do
{
if (filter(&pe, pid))
{
hProcess = OpenProcess(SYNCHRONIZE | PROCESS_QUERY_INFORMATION, 0, pe.th32ProcessID);
if (INVALID_HANDLE_VALUE == hProcess)
{
CloseHandle(hSnapshot);
errno = ECHILD;
return -1;
}
break;
}
}
while (Process32NextW(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));
/*
FIXME: causes FILETIME to conflict
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
static 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);
}
static pid_t wait3(int * status, int options, struct rusage * rusage)
{
return __waitpid_internal(-1, status, options, NULL, rusage);
}
static pid_t wait4(pid_t pid, int * status, int options, struct rusage * rusage)
{
return __waitpid_internal(pid, status, options, NULL, rusage);
}
#endif /* _XOPEN_SOURCE */
#ifndef _INC_WINDOWS
#undef WAIT_OBJECT_0
#undef FILETIME
#undef PFILETIME
#undef LPFILETIME
#endif /* _INC_WINDOWS */
#ifndef _WINSOCKAPI_
#undef timeval
#endif /* _WINSOCKAPI_ */
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* _WIN32 */
#endif /* __SYS_WAIT_H_B07AB843_5FF0_4FCF_B6F0_8CB97D28F337__ */