Branch
Hash :
de1e0c3a
Author :
Thomas de Grivel
Date :
2024-11-27T15:36:46
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
/*
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.
*/
#ifdef WIN32
#include <stdlib.h>
#include "wait.h"
int __filter_anychild (PROCESSENTRY32W * pe, DWORD pid)
{
(void) pid;
return pe->th32ParentProcessID == GetCurrentProcessId();
}
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);
}
*/
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_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;
}
int waitpid(pid_t pid, int * status, int options)
{
return __waitpid_internal(pid, status, options, NULL, NULL);
}
int wait(int *status)
{
return __waitpid_internal(-1, status, 0, NULL, NULL);
}
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);
}
#else
void _kc3_wait (void) {}
#endif