Commit | Line | Data |
---|---|---|
9277ec14 BC |
1 | /* This file contains functions which implement those POSIX and Linux functions |
2 | * that MinGW and Microsoft don't provide. The implementations contain just enough | |
3 | * functionality to support fio. | |
4 | */ | |
5 | ||
6 | #include <arpa/inet.h> | |
7 | #include <netinet/in.h> | |
8 | #include <windows.h> | |
9 | #include <stddef.h> | |
ad9c0fbc | 10 | #include <string.h> |
9277ec14 BC |
11 | #include <stdlib.h> |
12 | #include <unistd.h> | |
13 | #include <dirent.h> | |
14 | #include <pthread.h> | |
f16b7405 | 15 | #include <time.h> |
9277ec14 BC |
16 | #include <semaphore.h> |
17 | #include <sys/shm.h> | |
18 | #include <sys/mman.h> | |
19 | #include <sys/uio.h> | |
20 | #include <sys/resource.h> | |
8393ca93 | 21 | #include <poll.h> |
70a61165 BC |
22 | #include <sys/wait.h> |
23 | #include <setjmp.h> | |
9277ec14 BC |
24 | |
25 | #include "../os-windows.h" | |
671b0600 | 26 | #include "../../lib/hweight.h" |
9277ec14 | 27 | |
8b6a404c VF |
28 | extern unsigned long mtime_since_now(struct timespec *); |
29 | extern void fio_gettime(struct timespec *, void *); | |
21c75387 | 30 | |
10a6b3c6 BC |
31 | int win_to_posix_error(DWORD winerr) |
32 | { | |
e8ed50bc | 33 | switch (winerr) { |
bae743b1 JA |
34 | case ERROR_SUCCESS: |
35 | return 0; | |
e8ed50bc JA |
36 | case ERROR_FILE_NOT_FOUND: |
37 | return ENOENT; | |
38 | case ERROR_PATH_NOT_FOUND: | |
39 | return ENOENT; | |
40 | case ERROR_ACCESS_DENIED: | |
41 | return EACCES; | |
42 | case ERROR_INVALID_HANDLE: | |
43 | return EBADF; | |
44 | case ERROR_NOT_ENOUGH_MEMORY: | |
45 | return ENOMEM; | |
46 | case ERROR_INVALID_DATA: | |
47 | return EINVAL; | |
48 | case ERROR_OUTOFMEMORY: | |
49 | return ENOMEM; | |
50 | case ERROR_INVALID_DRIVE: | |
51 | return ENODEV; | |
52 | case ERROR_NOT_SAME_DEVICE: | |
53 | return EXDEV; | |
54 | case ERROR_WRITE_PROTECT: | |
55 | return EROFS; | |
56 | case ERROR_BAD_UNIT: | |
57 | return ENODEV; | |
58 | case ERROR_NOT_READY: | |
59 | return EAGAIN; | |
60 | case ERROR_SHARING_VIOLATION: | |
61 | return EACCES; | |
62 | case ERROR_LOCK_VIOLATION: | |
63 | return EACCES; | |
64 | case ERROR_SHARING_BUFFER_EXCEEDED: | |
65 | return ENOLCK; | |
66 | case ERROR_HANDLE_DISK_FULL: | |
67 | return ENOSPC; | |
68 | case ERROR_NOT_SUPPORTED: | |
69 | return ENOSYS; | |
70 | case ERROR_FILE_EXISTS: | |
71 | return EEXIST; | |
72 | case ERROR_CANNOT_MAKE: | |
73 | return EPERM; | |
74 | case ERROR_INVALID_PARAMETER: | |
75 | return EINVAL; | |
76 | case ERROR_NO_PROC_SLOTS: | |
77 | return EAGAIN; | |
78 | case ERROR_BROKEN_PIPE: | |
79 | return EPIPE; | |
80 | case ERROR_OPEN_FAILED: | |
81 | return EIO; | |
82 | case ERROR_NO_MORE_SEARCH_HANDLES: | |
83 | return ENFILE; | |
84 | case ERROR_CALL_NOT_IMPLEMENTED: | |
85 | return ENOSYS; | |
86 | case ERROR_INVALID_NAME: | |
87 | return ENOENT; | |
88 | case ERROR_WAIT_NO_CHILDREN: | |
89 | return ECHILD; | |
90 | case ERROR_CHILD_NOT_COMPLETE: | |
91 | return EBUSY; | |
92 | case ERROR_DIR_NOT_EMPTY: | |
93 | return ENOTEMPTY; | |
94 | case ERROR_SIGNAL_REFUSED: | |
95 | return EIO; | |
96 | case ERROR_BAD_PATHNAME: | |
97 | return ENOENT; | |
98 | case ERROR_SIGNAL_PENDING: | |
99 | return EBUSY; | |
100 | case ERROR_MAX_THRDS_REACHED: | |
101 | return EAGAIN; | |
102 | case ERROR_BUSY: | |
103 | return EBUSY; | |
104 | case ERROR_ALREADY_EXISTS: | |
105 | return EEXIST; | |
106 | case ERROR_NO_SIGNAL_SENT: | |
107 | return EIO; | |
108 | case ERROR_FILENAME_EXCED_RANGE: | |
109 | return EINVAL; | |
110 | case ERROR_META_EXPANSION_TOO_LONG: | |
111 | return EINVAL; | |
112 | case ERROR_INVALID_SIGNAL_NUMBER: | |
113 | return EINVAL; | |
114 | case ERROR_THREAD_1_INACTIVE: | |
115 | return EINVAL; | |
116 | case ERROR_BAD_PIPE: | |
117 | return EINVAL; | |
118 | case ERROR_PIPE_BUSY: | |
119 | return EBUSY; | |
120 | case ERROR_NO_DATA: | |
121 | return EPIPE; | |
122 | case ERROR_MORE_DATA: | |
123 | return EAGAIN; | |
124 | case ERROR_DIRECTORY: | |
125 | return ENOTDIR; | |
126 | case ERROR_PIPE_CONNECTED: | |
127 | return EBUSY; | |
128 | case ERROR_NO_TOKEN: | |
129 | return EINVAL; | |
130 | case ERROR_PROCESS_ABORTED: | |
131 | return EFAULT; | |
132 | case ERROR_BAD_DEVICE: | |
133 | return ENODEV; | |
134 | case ERROR_BAD_USERNAME: | |
135 | return EINVAL; | |
136 | case ERROR_OPEN_FILES: | |
137 | return EAGAIN; | |
138 | case ERROR_ACTIVE_CONNECTIONS: | |
139 | return EAGAIN; | |
140 | case ERROR_DEVICE_IN_USE: | |
141 | return EBUSY; | |
142 | case ERROR_INVALID_AT_INTERRUPT_TIME: | |
143 | return EINTR; | |
144 | case ERROR_IO_DEVICE: | |
145 | return EIO; | |
146 | case ERROR_NOT_OWNER: | |
147 | return EPERM; | |
148 | case ERROR_END_OF_MEDIA: | |
149 | return ENOSPC; | |
150 | case ERROR_EOM_OVERFLOW: | |
151 | return ENOSPC; | |
152 | case ERROR_BEGINNING_OF_MEDIA: | |
153 | return ESPIPE; | |
154 | case ERROR_SETMARK_DETECTED: | |
155 | return ESPIPE; | |
156 | case ERROR_NO_DATA_DETECTED: | |
157 | return ENOSPC; | |
158 | case ERROR_POSSIBLE_DEADLOCK: | |
159 | return EDEADLOCK; | |
160 | case ERROR_CRC: | |
161 | return EIO; | |
162 | case ERROR_NEGATIVE_SEEK: | |
163 | return EINVAL; | |
164 | case ERROR_DISK_FULL: | |
165 | return ENOSPC; | |
166 | case ERROR_NOACCESS: | |
167 | return EFAULT; | |
168 | case ERROR_FILE_INVALID: | |
169 | return ENXIO; | |
749dff99 | 170 | default: |
4937100f | 171 | log_err("fio: windows error %lu not handled\n", winerr); |
749dff99 | 172 | return EIO; |
10a6b3c6 BC |
173 | } |
174 | ||
175 | return winerr; | |
176 | } | |
177 | ||
671b0600 BC |
178 | int GetNumLogicalProcessors(void) |
179 | { | |
180 | SYSTEM_LOGICAL_PROCESSOR_INFORMATION *processor_info = NULL; | |
181 | DWORD len = 0; | |
182 | DWORD num_processors = 0; | |
183 | DWORD error = 0; | |
184 | DWORD i; | |
185 | ||
186 | while (!GetLogicalProcessorInformation(processor_info, &len)) { | |
187 | error = GetLastError(); | |
188 | if (error == ERROR_INSUFFICIENT_BUFFER) | |
189 | processor_info = malloc(len); | |
190 | else { | |
4937100f SW |
191 | log_err("Error: GetLogicalProcessorInformation failed: %lu\n", |
192 | error); | |
671b0600 BC |
193 | return -1; |
194 | } | |
195 | ||
196 | if (processor_info == NULL) { | |
197 | log_err("Error: failed to allocate memory for GetLogicalProcessorInformation"); | |
198 | return -1; | |
199 | } | |
200 | } | |
201 | ||
e8ed50bc | 202 | for (i = 0; i < len / sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION); i++) { |
671b0600 | 203 | if (processor_info[i].Relationship == RelationProcessorCore) |
4ee47af0 | 204 | num_processors += hweight64(processor_info[i].ProcessorMask); |
671b0600 BC |
205 | } |
206 | ||
207 | free(processor_info); | |
208 | return num_processors; | |
209 | } | |
210 | ||
9277ec14 BC |
211 | long sysconf(int name) |
212 | { | |
671b0600 | 213 | long val = -1; |
01d26955 | 214 | long val2 = -1; |
9277ec14 BC |
215 | SYSTEM_INFO sysInfo; |
216 | MEMORYSTATUSEX status; | |
217 | ||
e8ed50bc | 218 | switch (name) { |
40f61ec7 | 219 | case _SC_NPROCESSORS_CONF: |
220 | /* | |
221 | * Using GetMaximumProcessorCount introduces a problem in | |
222 | * gettime.c because Windows does not have | |
223 | * fio_get_thread_affinity. Log sample (see #1479): | |
224 | * | |
225 | * CPU mask contains processor beyond last active processor index (2) | |
226 | * clock setaffinity failed: No error | |
227 | */ | |
228 | val = GetActiveProcessorCount(ALL_PROCESSOR_GROUPS); | |
671b0600 | 229 | if (val == -1) |
40f61ec7 | 230 | log_err("sysconf(_SC_NPROCESSORS_CONF) failed\n"); |
671b0600 | 231 | |
9277ec14 BC |
232 | break; |
233 | ||
234 | case _SC_PAGESIZE: | |
235 | GetSystemInfo(&sysInfo); | |
236 | val = sysInfo.dwPageSize; | |
237 | break; | |
238 | ||
239 | case _SC_PHYS_PAGES: | |
240 | status.dwLength = sizeof(status); | |
01d26955 BC |
241 | val2 = sysconf(_SC_PAGESIZE); |
242 | if (GlobalMemoryStatusEx(&status) && val2 != -1) | |
243 | val = status.ullTotalPhys / val2; | |
244 | else | |
245 | log_err("sysconf(_SC_PHYS_PAGES) failed\n"); | |
9277ec14 BC |
246 | break; |
247 | default: | |
248 | log_err("sysconf(%d) is not implemented\n", name); | |
249 | break; | |
250 | } | |
251 | ||
252 | return val; | |
253 | } | |
254 | ||
255 | char *dl_error = NULL; | |
256 | ||
257 | int dlclose(void *handle) | |
258 | { | |
259 | return !FreeLibrary((HMODULE)handle); | |
260 | } | |
261 | ||
262 | void *dlopen(const char *file, int mode) | |
263 | { | |
264 | HMODULE hMod; | |
265 | ||
266 | hMod = LoadLibrary(file); | |
267 | if (hMod == INVALID_HANDLE_VALUE) | |
268 | dl_error = (char*)"LoadLibrary failed"; | |
269 | else | |
270 | dl_error = NULL; | |
271 | ||
272 | return hMod; | |
273 | } | |
274 | ||
275 | void *dlsym(void *handle, const char *name) | |
276 | { | |
277 | FARPROC fnPtr; | |
278 | ||
279 | fnPtr = GetProcAddress((HMODULE)handle, name); | |
280 | if (fnPtr == NULL) | |
281 | dl_error = (char*)"GetProcAddress failed"; | |
282 | else | |
283 | dl_error = NULL; | |
284 | ||
285 | return fnPtr; | |
286 | } | |
287 | ||
288 | char *dlerror(void) | |
289 | { | |
290 | return dl_error; | |
291 | } | |
292 | ||
ba55bfa9 BC |
293 | /* Copied from http://blogs.msdn.com/b/joshpoley/archive/2007/12/19/date-time-formats-and-conversions.aspx */ |
294 | void Time_tToSystemTime(time_t dosTime, SYSTEMTIME *systemTime) | |
295 | { | |
e8ed50bc JA |
296 | FILETIME utcFT; |
297 | LONGLONG jan1970; | |
7ff0297f | 298 | SYSTEMTIME tempSystemTime; |
5de1ade5 | 299 | |
e8ed50bc JA |
300 | jan1970 = Int32x32To64(dosTime, 10000000) + 116444736000000000; |
301 | utcFT.dwLowDateTime = (DWORD)jan1970; | |
302 | utcFT.dwHighDateTime = jan1970 >> 32; | |
ba55bfa9 | 303 | |
e8ed50bc | 304 | FileTimeToSystemTime((FILETIME*)&utcFT, &tempSystemTime); |
7ff0297f | 305 | SystemTimeToTzSpecificLocalTime(NULL, &tempSystemTime, systemTime); |
ba55bfa9 BC |
306 | } |
307 | ||
e8ed50bc | 308 | char *ctime_r(const time_t *t, char *buf) |
ba55bfa9 | 309 | { |
e8ed50bc JA |
310 | SYSTEMTIME systime; |
311 | const char * const dayOfWeek[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; | |
312 | const char * const monthOfYear[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; | |
313 | ||
314 | Time_tToSystemTime(*t, &systime); | |
315 | ||
316 | /* | |
317 | * We don't know how long `buf` is, but assume it's rounded up from | |
318 | * the minimum of 25 to 32 | |
319 | */ | |
a4820445 BVA |
320 | snprintf(buf, 32, "%s %s %d %02d:%02d:%02d %04d\n", |
321 | dayOfWeek[systime.wDayOfWeek % 7], | |
322 | monthOfYear[(systime.wMonth - 1) % 12], | |
323 | systime.wDay, systime.wHour, systime.wMinute, | |
324 | systime.wSecond, systime.wYear); | |
e8ed50bc | 325 | return buf; |
ba55bfa9 BC |
326 | } |
327 | ||
9277ec14 BC |
328 | int gettimeofday(struct timeval *restrict tp, void *restrict tzp) |
329 | { | |
330 | FILETIME fileTime; | |
9576f613 BC |
331 | uint64_t unix_time, windows_time; |
332 | const uint64_t MILLISECONDS_BETWEEN_1601_AND_1970 = 11644473600000; | |
9277ec14 | 333 | |
fc0b830f | 334 | /* Ignore the timezone parameter */ |
9277ec14 BC |
335 | (void)tzp; |
336 | ||
337 | /* | |
338 | * Windows time is stored as the number 100 ns intervals since January 1 1601. | |
339 | * Conversion details from http://www.informit.com/articles/article.aspx?p=102236&seqNum=3 | |
340 | * Its precision is 100 ns but accuracy is only one clock tick, or normally around 15 ms. | |
341 | */ | |
342 | GetSystemTimeAsFileTime(&fileTime); | |
9576f613 | 343 | windows_time = ((uint64_t)fileTime.dwHighDateTime << 32) + fileTime.dwLowDateTime; |
9277ec14 BC |
344 | /* Divide by 10,000 to convert to ms and subtract the time between 1601 and 1970 */ |
345 | unix_time = (((windows_time)/10000) - MILLISECONDS_BETWEEN_1601_AND_1970); | |
346 | /* unix_time is now the number of milliseconds since 1970 (the Unix epoch) */ | |
347 | tp->tv_sec = unix_time / 1000; | |
348 | tp->tv_usec = (unix_time % 1000) * 1000; | |
349 | return 0; | |
350 | } | |
351 | ||
e8ed50bc | 352 | int sigaction(int sig, const struct sigaction *act, struct sigaction *oact) |
9277ec14 | 353 | { |
e5b8f91c BC |
354 | int rc = 0; |
355 | void (*prev_handler)(int); | |
356 | ||
357 | prev_handler = signal(sig, act->sa_handler); | |
358 | if (oact != NULL) | |
359 | oact->sa_handler = prev_handler; | |
360 | ||
361 | if (prev_handler == SIG_ERR) | |
362 | rc = -1; | |
363 | ||
364 | return rc; | |
9277ec14 BC |
365 | } |
366 | ||
e8ed50bc | 367 | int lstat(const char *path, struct stat *buf) |
9277ec14 BC |
368 | { |
369 | return stat(path, buf); | |
370 | } | |
371 | ||
e8ed50bc | 372 | void *mmap(void *addr, size_t len, int prot, int flags, int fildes, off_t off) |
9277ec14 BC |
373 | { |
374 | DWORD vaProt = 0; | |
06cbb3c7 RC |
375 | DWORD mapAccess = 0; |
376 | DWORD lenlow; | |
377 | DWORD lenhigh; | |
378 | HANDLE hMap; | |
9277ec14 BC |
379 | void* allocAddr = NULL; |
380 | ||
381 | if (prot & PROT_NONE) | |
382 | vaProt |= PAGE_NOACCESS; | |
383 | ||
06cbb3c7 | 384 | if ((prot & PROT_READ) && !(prot & PROT_WRITE)) { |
9277ec14 | 385 | vaProt |= PAGE_READONLY; |
06cbb3c7 RC |
386 | mapAccess = FILE_MAP_READ; |
387 | } | |
9277ec14 | 388 | |
06cbb3c7 | 389 | if (prot & PROT_WRITE) { |
9277ec14 | 390 | vaProt |= PAGE_READWRITE; |
06cbb3c7 RC |
391 | mapAccess |= FILE_MAP_WRITE; |
392 | } | |
393 | ||
394 | lenlow = len & 0xFFFF; | |
395 | lenhigh = len >> 16; | |
396 | /* If the low DWORD is zero and the high DWORD is non-zero, `CreateFileMapping` | |
397 | will return ERROR_INVALID_PARAMETER. To avoid this, set both to zero. */ | |
e8ed50bc | 398 | if (lenlow == 0) |
06cbb3c7 | 399 | lenhigh = 0; |
9277ec14 | 400 | |
e8ed50bc | 401 | if (flags & MAP_ANON || flags & MAP_ANONYMOUS) { |
9277ec14 | 402 | allocAddr = VirtualAlloc(addr, len, MEM_COMMIT, vaProt); |
10a6b3c6 BC |
403 | if (allocAddr == NULL) |
404 | errno = win_to_posix_error(GetLastError()); | |
e8ed50bc JA |
405 | } else { |
406 | hMap = CreateFileMapping((HANDLE)_get_osfhandle(fildes), NULL, | |
407 | vaProt, lenhigh, lenlow, NULL); | |
06cbb3c7 RC |
408 | |
409 | if (hMap != NULL) | |
e8ed50bc JA |
410 | allocAddr = MapViewOfFile(hMap, mapAccess, off >> 16, |
411 | off & 0xFFFF, len); | |
06cbb3c7 RC |
412 | if (hMap == NULL || allocAddr == NULL) |
413 | errno = win_to_posix_error(GetLastError()); | |
414 | ||
415 | } | |
9277ec14 BC |
416 | |
417 | return allocAddr; | |
418 | } | |
419 | ||
420 | int munmap(void *addr, size_t len) | |
421 | { | |
06cbb3c7 RC |
422 | BOOL success; |
423 | ||
424 | /* We may have allocated the memory with either MapViewOfFile or | |
425 | VirtualAlloc. Therefore, try calling UnmapViewOfFile first, and if that | |
426 | fails, call VirtualFree. */ | |
427 | success = UnmapViewOfFile(addr); | |
428 | ||
429 | if (!success) | |
06cbb3c7 | 430 | success = VirtualFree(addr, 0, MEM_RELEASE); |
10a6b3c6 | 431 | |
06cbb3c7 RC |
432 | return !success; |
433 | } | |
434 | ||
435 | int msync(void *addr, size_t len, int flags) | |
436 | { | |
437 | return !FlushViewOfFile(addr, len); | |
9277ec14 BC |
438 | } |
439 | ||
440 | int fork(void) | |
441 | { | |
442 | log_err("%s is not implemented\n", __func__); | |
443 | errno = ENOSYS; | |
10a6b3c6 | 444 | return -1; |
9277ec14 BC |
445 | } |
446 | ||
447 | pid_t setsid(void) | |
448 | { | |
449 | log_err("%s is not implemented\n", __func__); | |
450 | errno = ENOSYS; | |
10a6b3c6 | 451 | return -1; |
9277ec14 BC |
452 | } |
453 | ||
ad9c0fbc BC |
454 | static HANDLE log_file = INVALID_HANDLE_VALUE; |
455 | ||
9277ec14 BC |
456 | void openlog(const char *ident, int logopt, int facility) |
457 | { | |
e8ed50bc JA |
458 | if (log_file != INVALID_HANDLE_VALUE) |
459 | return; | |
460 | ||
461 | log_file = CreateFileA("syslog.txt", GENERIC_WRITE, | |
462 | FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, | |
463 | OPEN_ALWAYS, 0, NULL); | |
9277ec14 BC |
464 | } |
465 | ||
466 | void closelog(void) | |
467 | { | |
ad9c0fbc BC |
468 | CloseHandle(log_file); |
469 | log_file = INVALID_HANDLE_VALUE; | |
470 | } | |
471 | ||
472 | void syslog(int priority, const char *message, ... /* argument */) | |
473 | { | |
474 | va_list v; | |
475 | int len; | |
476 | char *output; | |
477 | DWORD bytes_written; | |
478 | ||
479 | if (log_file == INVALID_HANDLE_VALUE) { | |
e8ed50bc JA |
480 | log_file = CreateFileA("syslog.txt", GENERIC_WRITE, |
481 | FILE_SHARE_READ | FILE_SHARE_WRITE, | |
482 | NULL, OPEN_ALWAYS, 0, NULL); | |
ad9c0fbc BC |
483 | } |
484 | ||
485 | if (log_file == INVALID_HANDLE_VALUE) { | |
486 | log_err("syslog: failed to open log file\n"); | |
487 | return; | |
488 | } | |
489 | ||
490 | va_start(v, message); | |
491 | len = _vscprintf(message, v); | |
492 | output = malloc(len + sizeof(char)); | |
98dc2db5 | 493 | vsprintf(output, message, v); |
ad9c0fbc BC |
494 | WriteFile(log_file, output, len, &bytes_written, NULL); |
495 | va_end(v); | |
98dc2db5 | 496 | free(output); |
9277ec14 BC |
497 | } |
498 | ||
499 | int kill(pid_t pid, int sig) | |
500 | { | |
501 | errno = ESRCH; | |
10a6b3c6 | 502 | return -1; |
9277ec14 BC |
503 | } |
504 | ||
fc0b830f BC |
505 | /* |
506 | * This is assumed to be used only by the network code, | |
507 | * and so doesn't try and handle any of the other cases | |
508 | */ | |
9277ec14 BC |
509 | int fcntl(int fildes, int cmd, ...) |
510 | { | |
fc0b830f BC |
511 | /* |
512 | * non-blocking mode doesn't work the same as in BSD sockets, | |
513 | * so ignore it. | |
514 | */ | |
9277ec14 BC |
515 | #if 0 |
516 | va_list ap; | |
517 | int val, opt, status; | |
518 | ||
519 | if (cmd == F_GETFL) | |
520 | return 0; | |
521 | else if (cmd != F_SETFL) { | |
522 | errno = EINVAL; | |
10a6b3c6 | 523 | return -1; |
9277ec14 BC |
524 | } |
525 | ||
526 | va_start(ap, 1); | |
527 | ||
528 | opt = va_arg(ap, int); | |
529 | if (opt & O_NONBLOCK) | |
530 | val = 1; | |
531 | else | |
532 | val = 0; | |
533 | ||
534 | status = ioctlsocket((SOCKET)fildes, opt, &val); | |
535 | ||
536 | if (status == SOCKET_ERROR) { | |
537 | errno = EINVAL; | |
538 | val = -1; | |
539 | } | |
540 | ||
541 | va_end(ap); | |
542 | ||
543 | return val; | |
544 | #endif | |
545 | return 0; | |
546 | } | |
547 | ||
d5b3cfd4 | 548 | #ifndef CLOCK_MONOTONIC_RAW |
549 | #define CLOCK_MONOTONIC_RAW 4 | |
550 | #endif | |
551 | ||
9277ec14 BC |
552 | int mlock(const void * addr, size_t len) |
553 | { | |
10a6b3c6 BC |
554 | SIZE_T min, max; |
555 | BOOL success; | |
556 | HANDLE process = GetCurrentProcess(); | |
557 | ||
558 | success = GetProcessWorkingSetSize(process, &min, &max); | |
559 | if (!success) { | |
560 | errno = win_to_posix_error(GetLastError()); | |
561 | return -1; | |
562 | } | |
563 | ||
564 | min += len; | |
565 | max += len; | |
566 | success = SetProcessWorkingSetSize(process, min, max); | |
567 | if (!success) { | |
568 | errno = win_to_posix_error(GetLastError()); | |
569 | return -1; | |
570 | } | |
571 | ||
572 | success = VirtualLock((LPVOID)addr, len); | |
573 | if (!success) { | |
574 | errno = win_to_posix_error(GetLastError()); | |
575 | return -1; | |
576 | } | |
577 | ||
578 | return 0; | |
9277ec14 BC |
579 | } |
580 | ||
581 | int munlock(const void * addr, size_t len) | |
582 | { | |
10a6b3c6 | 583 | BOOL success = VirtualUnlock((LPVOID)addr, len); |
e8ed50bc | 584 | |
10a6b3c6 BC |
585 | if (!success) { |
586 | errno = win_to_posix_error(GetLastError()); | |
587 | return -1; | |
588 | } | |
589 | ||
590 | return 0; | |
9277ec14 BC |
591 | } |
592 | ||
593 | pid_t waitpid(pid_t pid, int *stat_loc, int options) | |
594 | { | |
595 | log_err("%s is not implemented\n", __func__); | |
596 | errno = ENOSYS; | |
597 | return -1; | |
598 | } | |
599 | ||
600 | int usleep(useconds_t useconds) | |
601 | { | |
602 | Sleep(useconds / 1000); | |
603 | return 0; | |
604 | } | |
605 | ||
606 | char *basename(char *path) | |
607 | { | |
608 | static char name[MAX_PATH]; | |
609 | int i; | |
610 | ||
611 | if (path == NULL || strlen(path) == 0) | |
612 | return (char*)"."; | |
613 | ||
614 | i = strlen(path) - 1; | |
615 | ||
9576f613 | 616 | while (path[i] != '\\' && path[i] != '/' && i >= 0) |
9277ec14 BC |
617 | i--; |
618 | ||
13a85be9 TK |
619 | name[MAX_PATH - 1] = '\0'; |
620 | strncpy(name, path + i + 1, MAX_PATH - 1); | |
9277ec14 BC |
621 | |
622 | return name; | |
623 | } | |
624 | ||
9277ec14 BC |
625 | int fsync(int fildes) |
626 | { | |
627 | HANDLE hFile = (HANDLE)_get_osfhandle(fildes); | |
10a6b3c6 BC |
628 | if (!FlushFileBuffers(hFile)) { |
629 | errno = win_to_posix_error(GetLastError()); | |
630 | return -1; | |
631 | } | |
632 | ||
633 | return 0; | |
9277ec14 BC |
634 | } |
635 | ||
636 | int nFileMappings = 0; | |
637 | HANDLE fileMappings[1024]; | |
638 | ||
639 | int shmget(key_t key, size_t size, int shmflg) | |
640 | { | |
641 | int mapid = -1; | |
d3987946 BC |
642 | uint32_t size_low = size & 0xFFFFFFFF; |
643 | uint32_t size_high = ((uint64_t)size) >> 32; | |
e8ed50bc JA |
644 | HANDLE hMapping; |
645 | ||
646 | hMapping = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, | |
647 | PAGE_EXECUTE_READWRITE | SEC_RESERVE, | |
648 | size_high, size_low, NULL); | |
9277ec14 BC |
649 | if (hMapping != NULL) { |
650 | fileMappings[nFileMappings] = hMapping; | |
651 | mapid = nFileMappings; | |
652 | nFileMappings++; | |
e8ed50bc | 653 | } else |
9277ec14 | 654 | errno = ENOSYS; |
9277ec14 BC |
655 | |
656 | return mapid; | |
657 | } | |
658 | ||
659 | void *shmat(int shmid, const void *shmaddr, int shmflg) | |
660 | { | |
e8ed50bc | 661 | void *mapAddr; |
9277ec14 | 662 | MEMORY_BASIC_INFORMATION memInfo; |
e8ed50bc | 663 | |
9277ec14 | 664 | mapAddr = MapViewOfFile(fileMappings[shmid], FILE_MAP_ALL_ACCESS, 0, 0, 0); |
10a6b3c6 BC |
665 | if (mapAddr == NULL) { |
666 | errno = win_to_posix_error(GetLastError()); | |
667 | return (void*)-1; | |
668 | } | |
669 | ||
670 | if (VirtualQuery(mapAddr, &memInfo, sizeof(memInfo)) == 0) { | |
671 | errno = win_to_posix_error(GetLastError()); | |
672 | return (void*)-1; | |
673 | } | |
674 | ||
9277ec14 | 675 | mapAddr = VirtualAlloc(mapAddr, memInfo.RegionSize, MEM_COMMIT, PAGE_READWRITE); |
10a6b3c6 BC |
676 | if (mapAddr == NULL) { |
677 | errno = win_to_posix_error(GetLastError()); | |
678 | return (void*)-1; | |
679 | } | |
680 | ||
9277ec14 BC |
681 | return mapAddr; |
682 | } | |
683 | ||
684 | int shmdt(const void *shmaddr) | |
685 | { | |
10a6b3c6 BC |
686 | if (!UnmapViewOfFile(shmaddr)) { |
687 | errno = win_to_posix_error(GetLastError()); | |
688 | return -1; | |
689 | } | |
690 | ||
691 | return 0; | |
9277ec14 BC |
692 | } |
693 | ||
694 | int shmctl(int shmid, int cmd, struct shmid_ds *buf) | |
695 | { | |
696 | if (cmd == IPC_RMID) { | |
697 | fileMappings[shmid] = INVALID_HANDLE_VALUE; | |
698 | return 0; | |
9277ec14 | 699 | } |
e8ed50bc JA |
700 | |
701 | log_err("%s is not implemented\n", __func__); | |
10a6b3c6 BC |
702 | errno = ENOSYS; |
703 | return -1; | |
9277ec14 BC |
704 | } |
705 | ||
706 | int setuid(uid_t uid) | |
707 | { | |
708 | log_err("%s is not implemented\n", __func__); | |
709 | errno = ENOSYS; | |
10a6b3c6 | 710 | return -1; |
9277ec14 BC |
711 | } |
712 | ||
713 | int setgid(gid_t gid) | |
714 | { | |
715 | log_err("%s is not implemented\n", __func__); | |
716 | errno = ENOSYS; | |
10a6b3c6 | 717 | return -1; |
9277ec14 BC |
718 | } |
719 | ||
720 | int nice(int incr) | |
721 | { | |
24a2bb13 | 722 | DWORD prioclass = NORMAL_PRIORITY_CLASS; |
1e7fa601 | 723 | |
24a2bb13 BC |
724 | if (incr < -15) |
725 | prioclass = HIGH_PRIORITY_CLASS; | |
726 | else if (incr < 0) | |
727 | prioclass = ABOVE_NORMAL_PRIORITY_CLASS; | |
728 | else if (incr > 15) | |
729 | prioclass = IDLE_PRIORITY_CLASS; | |
730 | else if (incr > 0) | |
731 | prioclass = BELOW_NORMAL_PRIORITY_CLASS; | |
1e7fa601 | 732 | |
24a2bb13 BC |
733 | if (!SetPriorityClass(GetCurrentProcess(), prioclass)) |
734 | log_err("fio: SetPriorityClass failed\n"); | |
9277ec14 BC |
735 | |
736 | return 0; | |
737 | } | |
738 | ||
739 | int getrusage(int who, struct rusage *r_usage) | |
740 | { | |
9576f613 | 741 | const uint64_t SECONDS_BETWEEN_1601_AND_1970 = 11644473600; |
9277ec14 BC |
742 | FILETIME cTime, eTime, kTime, uTime; |
743 | time_t time; | |
7732a09b | 744 | HANDLE h; |
9277ec14 BC |
745 | |
746 | memset(r_usage, 0, sizeof(*r_usage)); | |
747 | ||
7732a09b HL |
748 | if (who == RUSAGE_SELF) { |
749 | h = GetCurrentProcess(); | |
750 | GetProcessTimes(h, &cTime, &eTime, &kTime, &uTime); | |
751 | } else if (who == RUSAGE_THREAD) { | |
752 | h = GetCurrentThread(); | |
753 | GetThreadTimes(h, &cTime, &eTime, &kTime, &uTime); | |
754 | } else { | |
755 | log_err("fio: getrusage %d is not implemented\n", who); | |
756 | return -1; | |
757 | } | |
758 | ||
9576f613 | 759 | time = ((uint64_t)uTime.dwHighDateTime << 32) + uTime.dwLowDateTime; |
9277ec14 BC |
760 | /* Divide by 10,000,000 to get the number of seconds and move the epoch from |
761 | * 1601 to 1970 */ | |
762 | time = (time_t)(((time)/10000000) - SECONDS_BETWEEN_1601_AND_1970); | |
763 | r_usage->ru_utime.tv_sec = time; | |
764 | /* getrusage() doesn't care about anything other than seconds, so set tv_usec to 0 */ | |
765 | r_usage->ru_utime.tv_usec = 0; | |
9576f613 | 766 | time = ((uint64_t)kTime.dwHighDateTime << 32) + kTime.dwLowDateTime; |
9277ec14 BC |
767 | /* Divide by 10,000,000 to get the number of seconds and move the epoch from |
768 | * 1601 to 1970 */ | |
769 | time = (time_t)(((time)/10000000) - SECONDS_BETWEEN_1601_AND_1970); | |
770 | r_usage->ru_stime.tv_sec = time; | |
771 | r_usage->ru_stime.tv_usec = 0; | |
772 | return 0; | |
773 | } | |
774 | ||
775 | int posix_madvise(void *addr, size_t len, int advice) | |
776 | { | |
9277ec14 BC |
777 | return ENOSYS; |
778 | } | |
779 | ||
9277ec14 BC |
780 | int fdatasync(int fildes) |
781 | { | |
782 | return fsync(fildes); | |
783 | } | |
784 | ||
785 | ssize_t pwrite(int fildes, const void *buf, size_t nbyte, | |
786 | off_t offset) | |
787 | { | |
9576f613 | 788 | int64_t pos = _telli64(fildes); |
3a2b42b3 | 789 | ssize_t len; |
e8ed50bc | 790 | |
3a2b42b3 VF |
791 | _lseeki64(fildes, offset, SEEK_SET); |
792 | len = _write(fildes, buf, nbyte); | |
9576f613 | 793 | _lseeki64(fildes, pos, SEEK_SET); |
3a2b42b3 | 794 | |
9277ec14 BC |
795 | return len; |
796 | } | |
797 | ||
798 | ssize_t pread(int fildes, void *buf, size_t nbyte, off_t offset) | |
799 | { | |
9576f613 | 800 | int64_t pos = _telli64(fildes); |
3a2b42b3 | 801 | ssize_t len; |
e8ed50bc | 802 | |
3a2b42b3 VF |
803 | _lseeki64(fildes, offset, SEEK_SET); |
804 | len = read(fildes, buf, nbyte); | |
9576f613 | 805 | _lseeki64(fildes, pos, SEEK_SET); |
3a2b42b3 | 806 | |
9277ec14 BC |
807 | return len; |
808 | } | |
809 | ||
810 | ssize_t readv(int fildes, const struct iovec *iov, int iovcnt) | |
811 | { | |
812 | log_err("%s is not implemented\n", __func__); | |
813 | errno = ENOSYS; | |
10a6b3c6 | 814 | return -1; |
9277ec14 BC |
815 | } |
816 | ||
817 | ssize_t writev(int fildes, const struct iovec *iov, int iovcnt) | |
818 | { | |
70a61165 BC |
819 | int i; |
820 | DWORD bytes_written = 0; | |
e8ed50bc JA |
821 | |
822 | for (i = 0; i < iovcnt; i++) { | |
823 | int len; | |
824 | ||
825 | len = send((SOCKET)fildes, iov[i].iov_base, iov[i].iov_len, 0); | |
826 | if (len == SOCKET_ERROR) { | |
70a61165 BC |
827 | DWORD err = GetLastError(); |
828 | errno = win_to_posix_error(err); | |
829 | bytes_written = -1; | |
830 | break; | |
831 | } | |
832 | bytes_written += len; | |
833 | } | |
834 | ||
835 | return bytes_written; | |
9277ec14 BC |
836 | } |
837 | ||
6eaa6422 | 838 | #ifndef _WIN32 |
e8ed50bc | 839 | long long strtoll(const char *restrict str, char **restrict endptr, int base) |
9277ec14 BC |
840 | { |
841 | return _strtoi64(str, endptr, base); | |
842 | } | |
6eaa6422 | 843 | #endif |
9277ec14 | 844 | |
9277ec14 BC |
845 | int poll(struct pollfd fds[], nfds_t nfds, int timeout) |
846 | { | |
847 | struct timeval tv; | |
848 | struct timeval *to = NULL; | |
849 | fd_set readfds, writefds, exceptfds; | |
850 | int i; | |
851 | int rc; | |
852 | ||
3f457bea | 853 | if (timeout != -1) { |
f9a58c2a | 854 | to = &tv; |
3f457bea BC |
855 | to->tv_sec = timeout / 1000; |
856 | to->tv_usec = (timeout % 1000) * 1000; | |
857 | } | |
9277ec14 BC |
858 | |
859 | FD_ZERO(&readfds); | |
860 | FD_ZERO(&writefds); | |
861 | FD_ZERO(&exceptfds); | |
862 | ||
e8ed50bc | 863 | for (i = 0; i < nfds; i++) { |
b7be1e42 | 864 | fds[i].revents = 0; |
865 | if (fds[i].fd == INVALID_SOCKET) | |
9277ec14 | 866 | continue; |
9277ec14 BC |
867 | |
868 | if (fds[i].events & POLLIN) | |
869 | FD_SET(fds[i].fd, &readfds); | |
870 | ||
871 | if (fds[i].events & POLLOUT) | |
872 | FD_SET(fds[i].fd, &writefds); | |
873 | ||
f9a58c2a | 874 | FD_SET(fds[i].fd, &exceptfds); |
9277ec14 | 875 | } |
9277ec14 BC |
876 | rc = select(nfds, &readfds, &writefds, &exceptfds, to); |
877 | ||
878 | if (rc != SOCKET_ERROR) { | |
e8ed50bc | 879 | for (i = 0; i < nfds; i++) { |
2ec7cd03 | 880 | if (fds[i].fd == INVALID_SOCKET) |
9277ec14 | 881 | continue; |
9277ec14 BC |
882 | |
883 | if ((fds[i].events & POLLIN) && FD_ISSET(fds[i].fd, &readfds)) | |
884 | fds[i].revents |= POLLIN; | |
885 | ||
886 | if ((fds[i].events & POLLOUT) && FD_ISSET(fds[i].fd, &writefds)) | |
887 | fds[i].revents |= POLLOUT; | |
888 | ||
889 | if (FD_ISSET(fds[i].fd, &exceptfds)) | |
890 | fds[i].revents |= POLLHUP; | |
891 | } | |
892 | } | |
9277ec14 BC |
893 | return rc; |
894 | } | |
895 | ||
9277ec14 BC |
896 | DIR *opendir(const char *dirname) |
897 | { | |
01d26955 | 898 | struct dirent_ctx *dc = NULL; |
e8ed50bc | 899 | HANDLE file; |
01d26955 BC |
900 | |
901 | /* See if we can open it. If not, we'll return an error here */ | |
e8ed50bc JA |
902 | file = CreateFileA(dirname, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, |
903 | OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); | |
01d26955 BC |
904 | if (file != INVALID_HANDLE_VALUE) { |
905 | CloseHandle(file); | |
a4820445 BVA |
906 | dc = malloc(sizeof(struct dirent_ctx)); |
907 | snprintf(dc->dirname, sizeof(dc->dirname), "%s", dirname); | |
01d26955 BC |
908 | dc->find_handle = INVALID_HANDLE_VALUE; |
909 | } else { | |
910 | DWORD error = GetLastError(); | |
911 | if (error == ERROR_FILE_NOT_FOUND) | |
912 | errno = ENOENT; | |
913 | ||
914 | else if (error == ERROR_PATH_NOT_FOUND) | |
915 | errno = ENOTDIR; | |
916 | else if (error == ERROR_TOO_MANY_OPEN_FILES) | |
917 | errno = ENFILE; | |
918 | else if (error == ERROR_ACCESS_DENIED) | |
919 | errno = EACCES; | |
920 | else | |
921 | errno = error; | |
922 | } | |
923 | ||
924 | return dc; | |
9277ec14 BC |
925 | } |
926 | ||
927 | int closedir(DIR *dirp) | |
928 | { | |
01d26955 BC |
929 | if (dirp != NULL && dirp->find_handle != INVALID_HANDLE_VALUE) |
930 | FindClose(dirp->find_handle); | |
ad9c0fbc | 931 | |
01d26955 BC |
932 | free(dirp); |
933 | return 0; | |
9277ec14 BC |
934 | } |
935 | ||
936 | struct dirent *readdir(DIR *dirp) | |
937 | { | |
ad9c0fbc BC |
938 | static struct dirent de; |
939 | WIN32_FIND_DATA find_data; | |
940 | ||
941 | if (dirp == NULL) | |
942 | return NULL; | |
943 | ||
944 | if (dirp->find_handle == INVALID_HANDLE_VALUE) { | |
945 | char search_pattern[MAX_PATH]; | |
e8ed50bc | 946 | |
a4820445 BVA |
947 | snprintf(search_pattern, sizeof(search_pattern), "%s\\*", |
948 | dirp->dirname); | |
ad9c0fbc BC |
949 | dirp->find_handle = FindFirstFileA(search_pattern, &find_data); |
950 | if (dirp->find_handle == INVALID_HANDLE_VALUE) | |
951 | return NULL; | |
952 | } else { | |
953 | if (!FindNextFile(dirp->find_handle, &find_data)) | |
954 | return NULL; | |
955 | } | |
956 | ||
a4820445 | 957 | snprintf(de.d_name, sizeof(de.d_name), find_data.cFileName); |
ad9c0fbc BC |
958 | de.d_ino = 0; |
959 | ||
960 | return &de; | |
9277ec14 BC |
961 | } |
962 | ||
963 | uid_t geteuid(void) | |
964 | { | |
965 | log_err("%s is not implemented\n", __func__); | |
966 | errno = ENOSYS; | |
967 | return -1; | |
968 | } | |
969 | ||
f16b7405 BC |
970 | in_addr_t inet_network(const char *cp) |
971 | { | |
972 | in_addr_t hbo; | |
973 | in_addr_t nbo = inet_addr(cp); | |
974 | hbo = ((nbo & 0xFF) << 24) + ((nbo & 0xFF00) << 8) + ((nbo & 0xFF0000) >> 8) + ((nbo & 0xFF000000) >> 24); | |
975 | return hbo; | |
976 | } | |
f8fef4c6 | 977 | |
978 | static HANDLE create_named_pipe(char *pipe_name, int wait_connect_time) | |
979 | { | |
980 | HANDLE hpipe; | |
981 | ||
982 | hpipe = CreateNamedPipe ( | |
983 | pipe_name, | |
984 | PIPE_ACCESS_DUPLEX, | |
985 | PIPE_WAIT | PIPE_TYPE_BYTE, | |
986 | 1, 0, 0, wait_connect_time, NULL); | |
987 | ||
988 | if (hpipe == INVALID_HANDLE_VALUE) { | |
989 | log_err("ConnectNamedPipe failed (%lu).\n", GetLastError()); | |
990 | return INVALID_HANDLE_VALUE; | |
991 | } | |
992 | ||
993 | if (!ConnectNamedPipe(hpipe, NULL)) { | |
994 | log_err("ConnectNamedPipe failed (%lu).\n", GetLastError()); | |
995 | CloseHandle(hpipe); | |
996 | return INVALID_HANDLE_VALUE; | |
997 | } | |
998 | ||
999 | return hpipe; | |
1000 | } | |
1001 | ||
1002 | static BOOL windows_create_process(PROCESS_INFORMATION *pi, const char *args, HANDLE *hjob) | |
1003 | { | |
1004 | LPSTR this_cmd_line = GetCommandLine(); | |
1005 | LPSTR new_process_cmd_line = malloc((strlen(this_cmd_line)+strlen(args)) * sizeof(char *)); | |
1006 | STARTUPINFO si = {0}; | |
1007 | DWORD flags = 0; | |
1008 | ||
1009 | strcpy(new_process_cmd_line, this_cmd_line); | |
1010 | strcat(new_process_cmd_line, args); | |
1011 | ||
1012 | si.cb = sizeof(si); | |
1013 | memset(pi, 0, sizeof(*pi)); | |
1014 | ||
1015 | if ((hjob != NULL) && (*hjob != INVALID_HANDLE_VALUE)) | |
1016 | flags = CREATE_SUSPENDED | CREATE_BREAKAWAY_FROM_JOB; | |
1017 | ||
1018 | flags |= CREATE_NEW_CONSOLE; | |
1019 | ||
1020 | if( !CreateProcess( NULL, | |
1021 | new_process_cmd_line, | |
1022 | NULL, /* Process handle not inherited */ | |
1023 | NULL, /* Thread handle not inherited */ | |
1024 | TRUE, /* no handle inheritance */ | |
1025 | flags, | |
1026 | NULL, /* Use parent's environment block */ | |
1027 | NULL, /* Use parent's starting directory */ | |
1028 | &si, | |
1029 | pi ) | |
1030 | ) | |
1031 | { | |
1032 | log_err("CreateProcess failed (%lu).\n", GetLastError() ); | |
1033 | free(new_process_cmd_line); | |
1034 | return 1; | |
1035 | } | |
1036 | if ((hjob != NULL) && (*hjob != INVALID_HANDLE_VALUE)) { | |
1037 | BOOL ret = AssignProcessToJobObject(*hjob, pi->hProcess); | |
1038 | if (!ret) { | |
1039 | log_err("AssignProcessToJobObject failed (%lu).\n", GetLastError() ); | |
1040 | return 1; | |
1041 | } | |
1042 | ||
1043 | ResumeThread(pi->hThread); | |
1044 | } | |
1045 | ||
1046 | free(new_process_cmd_line); | |
1047 | return 0; | |
1048 | } | |
1049 | ||
1050 | HANDLE windows_create_job(void) | |
1051 | { | |
1052 | JOBOBJECT_EXTENDED_LIMIT_INFORMATION jeli = { 0 }; | |
1053 | BOOL success; | |
1054 | HANDLE hjob = CreateJobObject(NULL, NULL); | |
1055 | ||
1056 | jeli.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE; | |
1057 | success = SetInformationJobObject(hjob, JobObjectExtendedLimitInformation, &jeli, sizeof(jeli)); | |
1058 | if ( success == 0 ) { | |
1059 | log_err( "SetInformationJobObject failed: error %lu\n", GetLastError() ); | |
1060 | return INVALID_HANDLE_VALUE; | |
1061 | } | |
1062 | return hjob; | |
1063 | } | |
1064 | ||
1065 | /* wait for a child process to either exit or connect to a child */ | |
1066 | static bool monitor_process_till_connect(PROCESS_INFORMATION *pi, HANDLE *hpipe) | |
1067 | { | |
1068 | bool connected = FALSE; | |
1069 | bool process_alive = TRUE; | |
1070 | char buffer[32] = {0}; | |
1071 | DWORD bytes_read; | |
1072 | ||
1073 | do { | |
1074 | DWORD exit_code; | |
1075 | GetExitCodeProcess(pi->hProcess, &exit_code); | |
1076 | if (exit_code != STILL_ACTIVE) { | |
1077 | dprint(FD_PROCESS, "process %u exited %d\n", GetProcessId(pi->hProcess), exit_code); | |
1078 | break; | |
1079 | } | |
1080 | ||
1081 | memset(buffer, 0, sizeof(buffer)); | |
1082 | ReadFile(*hpipe, &buffer, sizeof(buffer) - 1, &bytes_read, NULL); | |
1083 | if (bytes_read && strstr(buffer, "connected")) { | |
1084 | dprint(FD_PROCESS, "process %u connected to client\n", GetProcessId(pi->hProcess)); | |
1085 | connected = TRUE; | |
1086 | } | |
1087 | usleep(10*1000); | |
1088 | } while (process_alive && !connected); | |
1089 | return connected; | |
1090 | } | |
1091 | ||
1092 | /*create a process with --server-internal to emulate fork() */ | |
1093 | HANDLE windows_handle_connection(HANDLE hjob, int sk) | |
1094 | { | |
1095 | char pipe_name[64] = "\\\\.\\pipe\\fiointernal-"; | |
1096 | char args[128] = " --server-internal="; | |
1097 | PROCESS_INFORMATION pi; | |
1098 | HANDLE hpipe = INVALID_HANDLE_VALUE; | |
1099 | WSAPROTOCOL_INFO protocol_info; | |
1100 | HANDLE ret; | |
1101 | ||
1102 | sprintf(pipe_name+strlen(pipe_name), "%d", GetCurrentProcessId()); | |
1103 | sprintf(args+strlen(args), "%s", pipe_name); | |
1104 | ||
1105 | if (windows_create_process(&pi, args, &hjob) != 0) | |
1106 | return INVALID_HANDLE_VALUE; | |
1107 | else | |
1108 | ret = pi.hProcess; | |
1109 | ||
1110 | /* duplicate socket and write the protocol_info to pipe so child can | |
fc002f14 | 1111 | * duplicate the communication socket */ |
f8fef4c6 | 1112 | if (WSADuplicateSocket(sk, GetProcessId(pi.hProcess), &protocol_info)) { |
1113 | log_err("WSADuplicateSocket failed (%lu).\n", GetLastError()); | |
1114 | ret = INVALID_HANDLE_VALUE; | |
1115 | goto cleanup; | |
1116 | } | |
1117 | ||
1118 | /* make a pipe with a unique name based upon processid */ | |
1119 | hpipe = create_named_pipe(pipe_name, 1000); | |
1120 | if (hpipe == INVALID_HANDLE_VALUE) { | |
1121 | ret = INVALID_HANDLE_VALUE; | |
1122 | goto cleanup; | |
1123 | } | |
1124 | ||
1125 | if (!WriteFile(hpipe, &protocol_info, sizeof(protocol_info), NULL, NULL)) { | |
1126 | log_err("WriteFile failed (%lu).\n", GetLastError()); | |
1127 | ret = INVALID_HANDLE_VALUE; | |
1128 | goto cleanup; | |
1129 | } | |
1130 | ||
1131 | dprint(FD_PROCESS, "process %d created child process %u\n", GetCurrentProcessId(), GetProcessId(pi.hProcess)); | |
1132 | ||
1133 | /* monitor the process until it either exits or connects. This level | |
1134 | * doesnt care which of those occurs because the result is that it | |
1135 | * needs to loop around and create another child process to monitor */ | |
1136 | if (!monitor_process_till_connect(&pi, &hpipe)) | |
1137 | ret = INVALID_HANDLE_VALUE; | |
1138 | ||
1139 | cleanup: | |
1140 | /* close the handles and pipes because this thread is done monitoring them */ | |
1141 | if (ret == INVALID_HANDLE_VALUE) | |
1142 | CloseHandle(pi.hProcess); | |
1143 | CloseHandle(pi.hThread); | |
1144 | DisconnectNamedPipe(hpipe); | |
1145 | CloseHandle(hpipe); | |
1146 | return ret; | |
40f61ec7 | 1147 | } |