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> | |
15 | #include <semaphore.h> | |
16 | #include <sys/shm.h> | |
17 | #include <sys/mman.h> | |
18 | #include <sys/uio.h> | |
19 | #include <sys/resource.h> | |
20 | #include <sys/poll.h> | |
21 | ||
22 | #include "../os-windows.h" | |
671b0600 | 23 | #include "../../lib/hweight.h" |
9277ec14 | 24 | |
21c75387 BC |
25 | extern unsigned long mtime_since_now(struct timeval *); |
26 | extern void fio_gettime(struct timeval *, void *); | |
27 | ||
ad9c0fbc BC |
28 | /* These aren't defined in the MinGW headers */ |
29 | HRESULT WINAPI StringCchCopyA( | |
30 | char *pszDest, | |
31 | size_t cchDest, | |
32 | const char *pszSrc); | |
33 | ||
34 | HRESULT WINAPI StringCchPrintfA( | |
35 | char *pszDest, | |
36 | size_t cchDest, | |
37 | const char *pszFormat, | |
38 | ...); | |
39 | ||
40 | int vsprintf_s( | |
41 | char *buffer, | |
42 | size_t numberOfElements, | |
43 | const char *format, | |
44 | va_list argptr); | |
45 | ||
10a6b3c6 BC |
46 | int win_to_posix_error(DWORD winerr) |
47 | { | |
48 | switch (winerr) | |
49 | { | |
50 | case ERROR_FILE_NOT_FOUND: return ENOENT; | |
51 | case ERROR_PATH_NOT_FOUND: return ENOENT; | |
52 | case ERROR_ACCESS_DENIED: return EACCES; | |
53 | case ERROR_INVALID_HANDLE: return EBADF; | |
54 | case ERROR_NOT_ENOUGH_MEMORY: return ENOMEM; | |
55 | case ERROR_INVALID_DATA: return EINVAL; | |
56 | case ERROR_OUTOFMEMORY: return ENOMEM; | |
57 | case ERROR_INVALID_DRIVE: return ENODEV; | |
58 | case ERROR_NOT_SAME_DEVICE: return EXDEV; | |
59 | case ERROR_WRITE_PROTECT: return EROFS; | |
60 | case ERROR_BAD_UNIT: return ENODEV; | |
61 | case ERROR_SHARING_VIOLATION: return EACCES; | |
62 | case ERROR_LOCK_VIOLATION: return EACCES; | |
63 | case ERROR_SHARING_BUFFER_EXCEEDED: return ENOLCK; | |
64 | case ERROR_HANDLE_DISK_FULL: return ENOSPC; | |
65 | case ERROR_NOT_SUPPORTED: return ENOSYS; | |
66 | case ERROR_FILE_EXISTS: return EEXIST; | |
67 | case ERROR_CANNOT_MAKE: return EPERM; | |
68 | case ERROR_INVALID_PARAMETER: return EINVAL; | |
69 | case ERROR_NO_PROC_SLOTS: return EAGAIN; | |
70 | case ERROR_BROKEN_PIPE: return EPIPE; | |
71 | case ERROR_OPEN_FAILED: return EIO; | |
72 | case ERROR_NO_MORE_SEARCH_HANDLES: return ENFILE; | |
73 | case ERROR_CALL_NOT_IMPLEMENTED: return ENOSYS; | |
74 | case ERROR_INVALID_NAME: return ENOENT; | |
75 | case ERROR_WAIT_NO_CHILDREN: return ECHILD; | |
76 | case ERROR_CHILD_NOT_COMPLETE: return EBUSY; | |
77 | case ERROR_DIR_NOT_EMPTY: return ENOTEMPTY; | |
78 | case ERROR_SIGNAL_REFUSED: return EIO; | |
79 | case ERROR_BAD_PATHNAME: return ENOENT; | |
80 | case ERROR_SIGNAL_PENDING: return EBUSY; | |
81 | case ERROR_MAX_THRDS_REACHED: return EAGAIN; | |
82 | case ERROR_BUSY: return EBUSY; | |
83 | case ERROR_ALREADY_EXISTS: return EEXIST; | |
84 | case ERROR_NO_SIGNAL_SENT: return EIO; | |
85 | case ERROR_FILENAME_EXCED_RANGE: return EINVAL; | |
86 | case ERROR_META_EXPANSION_TOO_LONG: return EINVAL; | |
87 | case ERROR_INVALID_SIGNAL_NUMBER: return EINVAL; | |
88 | case ERROR_THREAD_1_INACTIVE: return EINVAL; | |
89 | case ERROR_BAD_PIPE: return EINVAL; | |
90 | case ERROR_PIPE_BUSY: return EBUSY; | |
91 | case ERROR_NO_DATA: return EPIPE; | |
92 | case ERROR_MORE_DATA: return EAGAIN; | |
93 | case ERROR_DIRECTORY: return ENOTDIR; | |
94 | case ERROR_PIPE_CONNECTED: return EBUSY; | |
95 | case ERROR_NO_TOKEN: return EINVAL; | |
96 | case ERROR_PROCESS_ABORTED: return EFAULT; | |
97 | case ERROR_BAD_DEVICE: return ENODEV; | |
98 | case ERROR_BAD_USERNAME: return EINVAL; | |
99 | case ERROR_OPEN_FILES: return EAGAIN; | |
100 | case ERROR_ACTIVE_CONNECTIONS: return EAGAIN; | |
101 | case ERROR_DEVICE_IN_USE: return EAGAIN; | |
102 | case ERROR_INVALID_AT_INTERRUPT_TIME: return EINTR; | |
103 | case ERROR_IO_DEVICE: return EIO; | |
104 | case ERROR_NOT_OWNER: return EPERM; | |
105 | case ERROR_END_OF_MEDIA: return ENOSPC; | |
106 | case ERROR_EOM_OVERFLOW: return ENOSPC; | |
107 | case ERROR_BEGINNING_OF_MEDIA: return ESPIPE; | |
108 | case ERROR_SETMARK_DETECTED: return ESPIPE; | |
109 | case ERROR_NO_DATA_DETECTED: return ENOSPC; | |
110 | case ERROR_POSSIBLE_DEADLOCK: return EDEADLOCK; | |
111 | case ERROR_CRC: return EIO; | |
112 | case ERROR_NEGATIVE_SEEK: return EINVAL; | |
113 | case ERROR_DISK_FULL: return ENOSPC; | |
114 | case ERROR_NOACCESS: return EFAULT; | |
115 | case ERROR_FILE_INVALID: return ENXIO; | |
116 | } | |
117 | ||
118 | return winerr; | |
119 | } | |
120 | ||
671b0600 BC |
121 | int GetNumLogicalProcessors(void) |
122 | { | |
123 | SYSTEM_LOGICAL_PROCESSOR_INFORMATION *processor_info = NULL; | |
124 | DWORD len = 0; | |
125 | DWORD num_processors = 0; | |
126 | DWORD error = 0; | |
127 | DWORD i; | |
128 | ||
129 | while (!GetLogicalProcessorInformation(processor_info, &len)) { | |
130 | error = GetLastError(); | |
131 | if (error == ERROR_INSUFFICIENT_BUFFER) | |
132 | processor_info = malloc(len); | |
133 | else { | |
134 | log_err("Error: GetLogicalProcessorInformation failed: %d\n", error); | |
135 | return -1; | |
136 | } | |
137 | ||
138 | if (processor_info == NULL) { | |
139 | log_err("Error: failed to allocate memory for GetLogicalProcessorInformation"); | |
140 | return -1; | |
141 | } | |
142 | } | |
143 | ||
144 | for (i = 0; i < len / sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION); i++) | |
145 | { | |
146 | if (processor_info[i].Relationship == RelationProcessorCore) | |
4ee47af0 | 147 | num_processors += hweight64(processor_info[i].ProcessorMask); |
671b0600 BC |
148 | } |
149 | ||
150 | free(processor_info); | |
151 | return num_processors; | |
152 | } | |
153 | ||
9277ec14 BC |
154 | long sysconf(int name) |
155 | { | |
671b0600 | 156 | long val = -1; |
01d26955 | 157 | long val2 = -1; |
9277ec14 BC |
158 | SYSTEM_INFO sysInfo; |
159 | MEMORYSTATUSEX status; | |
160 | ||
161 | switch (name) | |
162 | { | |
163 | case _SC_NPROCESSORS_ONLN: | |
671b0600 BC |
164 | val = GetNumLogicalProcessors(); |
165 | if (val == -1) | |
01d26955 | 166 | log_err("sysconf(_SC_NPROCESSORS_ONLN) failed\n"); |
671b0600 | 167 | |
9277ec14 BC |
168 | break; |
169 | ||
170 | case _SC_PAGESIZE: | |
171 | GetSystemInfo(&sysInfo); | |
172 | val = sysInfo.dwPageSize; | |
173 | break; | |
174 | ||
175 | case _SC_PHYS_PAGES: | |
176 | status.dwLength = sizeof(status); | |
01d26955 BC |
177 | val2 = sysconf(_SC_PAGESIZE); |
178 | if (GlobalMemoryStatusEx(&status) && val2 != -1) | |
179 | val = status.ullTotalPhys / val2; | |
180 | else | |
181 | log_err("sysconf(_SC_PHYS_PAGES) failed\n"); | |
9277ec14 BC |
182 | break; |
183 | default: | |
184 | log_err("sysconf(%d) is not implemented\n", name); | |
185 | break; | |
186 | } | |
187 | ||
188 | return val; | |
189 | } | |
190 | ||
191 | char *dl_error = NULL; | |
192 | ||
193 | int dlclose(void *handle) | |
194 | { | |
195 | return !FreeLibrary((HMODULE)handle); | |
196 | } | |
197 | ||
198 | void *dlopen(const char *file, int mode) | |
199 | { | |
200 | HMODULE hMod; | |
201 | ||
202 | hMod = LoadLibrary(file); | |
203 | if (hMod == INVALID_HANDLE_VALUE) | |
204 | dl_error = (char*)"LoadLibrary failed"; | |
205 | else | |
206 | dl_error = NULL; | |
207 | ||
208 | return hMod; | |
209 | } | |
210 | ||
211 | void *dlsym(void *handle, const char *name) | |
212 | { | |
213 | FARPROC fnPtr; | |
214 | ||
215 | fnPtr = GetProcAddress((HMODULE)handle, name); | |
216 | if (fnPtr == NULL) | |
217 | dl_error = (char*)"GetProcAddress failed"; | |
218 | else | |
219 | dl_error = NULL; | |
220 | ||
221 | return fnPtr; | |
222 | } | |
223 | ||
224 | char *dlerror(void) | |
225 | { | |
226 | return dl_error; | |
227 | } | |
228 | ||
229 | int gettimeofday(struct timeval *restrict tp, void *restrict tzp) | |
230 | { | |
231 | FILETIME fileTime; | |
9576f613 BC |
232 | uint64_t unix_time, windows_time; |
233 | const uint64_t MILLISECONDS_BETWEEN_1601_AND_1970 = 11644473600000; | |
9277ec14 | 234 | |
fc0b830f | 235 | /* Ignore the timezone parameter */ |
9277ec14 BC |
236 | (void)tzp; |
237 | ||
238 | /* | |
239 | * Windows time is stored as the number 100 ns intervals since January 1 1601. | |
240 | * Conversion details from http://www.informit.com/articles/article.aspx?p=102236&seqNum=3 | |
241 | * Its precision is 100 ns but accuracy is only one clock tick, or normally around 15 ms. | |
242 | */ | |
243 | GetSystemTimeAsFileTime(&fileTime); | |
9576f613 | 244 | windows_time = ((uint64_t)fileTime.dwHighDateTime << 32) + fileTime.dwLowDateTime; |
9277ec14 BC |
245 | /* Divide by 10,000 to convert to ms and subtract the time between 1601 and 1970 */ |
246 | unix_time = (((windows_time)/10000) - MILLISECONDS_BETWEEN_1601_AND_1970); | |
247 | /* unix_time is now the number of milliseconds since 1970 (the Unix epoch) */ | |
248 | tp->tv_sec = unix_time / 1000; | |
249 | tp->tv_usec = (unix_time % 1000) * 1000; | |
250 | return 0; | |
251 | } | |
252 | ||
9277ec14 BC |
253 | int sigaction(int sig, const struct sigaction *act, |
254 | struct sigaction *oact) | |
255 | { | |
e5b8f91c BC |
256 | int rc = 0; |
257 | void (*prev_handler)(int); | |
258 | ||
259 | prev_handler = signal(sig, act->sa_handler); | |
260 | if (oact != NULL) | |
261 | oact->sa_handler = prev_handler; | |
262 | ||
263 | if (prev_handler == SIG_ERR) | |
264 | rc = -1; | |
265 | ||
266 | return rc; | |
9277ec14 BC |
267 | } |
268 | ||
269 | int lstat(const char * path, struct stat * buf) | |
270 | { | |
271 | return stat(path, buf); | |
272 | } | |
273 | ||
274 | void *mmap(void *addr, size_t len, int prot, int flags, | |
275 | int fildes, off_t off) | |
276 | { | |
277 | DWORD vaProt = 0; | |
278 | void* allocAddr = NULL; | |
279 | ||
280 | if (prot & PROT_NONE) | |
281 | vaProt |= PAGE_NOACCESS; | |
282 | ||
283 | if ((prot & PROT_READ) && !(prot & PROT_WRITE)) | |
284 | vaProt |= PAGE_READONLY; | |
285 | ||
286 | if (prot & PROT_WRITE) | |
287 | vaProt |= PAGE_READWRITE; | |
288 | ||
289 | if ((flags & MAP_ANON) | (flags & MAP_ANONYMOUS)) | |
290 | { | |
291 | allocAddr = VirtualAlloc(addr, len, MEM_COMMIT, vaProt); | |
10a6b3c6 BC |
292 | if (allocAddr == NULL) |
293 | errno = win_to_posix_error(GetLastError()); | |
9277ec14 BC |
294 | } |
295 | ||
296 | return allocAddr; | |
297 | } | |
298 | ||
299 | int munmap(void *addr, size_t len) | |
300 | { | |
10a6b3c6 BC |
301 | if (!VirtualFree(addr, 0, MEM_RELEASE)) { |
302 | errno = win_to_posix_error(GetLastError()); | |
303 | return -1; | |
304 | } | |
305 | ||
306 | return 0; | |
9277ec14 BC |
307 | } |
308 | ||
309 | int fork(void) | |
310 | { | |
311 | log_err("%s is not implemented\n", __func__); | |
312 | errno = ENOSYS; | |
10a6b3c6 | 313 | return -1; |
9277ec14 BC |
314 | } |
315 | ||
316 | pid_t setsid(void) | |
317 | { | |
318 | log_err("%s is not implemented\n", __func__); | |
319 | errno = ENOSYS; | |
10a6b3c6 | 320 | return -1; |
9277ec14 BC |
321 | } |
322 | ||
ad9c0fbc BC |
323 | static HANDLE log_file = INVALID_HANDLE_VALUE; |
324 | ||
9277ec14 BC |
325 | void openlog(const char *ident, int logopt, int facility) |
326 | { | |
ad9c0fbc BC |
327 | if (log_file == INVALID_HANDLE_VALUE) |
328 | log_file = CreateFileA("syslog.txt", GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, 0, NULL); | |
9277ec14 BC |
329 | } |
330 | ||
331 | void closelog(void) | |
332 | { | |
ad9c0fbc BC |
333 | CloseHandle(log_file); |
334 | log_file = INVALID_HANDLE_VALUE; | |
335 | } | |
336 | ||
337 | void syslog(int priority, const char *message, ... /* argument */) | |
338 | { | |
339 | va_list v; | |
340 | int len; | |
341 | char *output; | |
342 | DWORD bytes_written; | |
343 | ||
344 | if (log_file == INVALID_HANDLE_VALUE) { | |
345 | log_file = CreateFileA("syslog.txt", GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, 0, NULL); | |
346 | } | |
347 | ||
348 | if (log_file == INVALID_HANDLE_VALUE) { | |
349 | log_err("syslog: failed to open log file\n"); | |
350 | return; | |
351 | } | |
352 | ||
353 | va_start(v, message); | |
354 | len = _vscprintf(message, v); | |
355 | output = malloc(len + sizeof(char)); | |
98dc2db5 | 356 | vsprintf(output, message, v); |
ad9c0fbc BC |
357 | WriteFile(log_file, output, len, &bytes_written, NULL); |
358 | va_end(v); | |
98dc2db5 | 359 | free(output); |
9277ec14 BC |
360 | } |
361 | ||
362 | int kill(pid_t pid, int sig) | |
363 | { | |
364 | errno = ESRCH; | |
10a6b3c6 | 365 | return -1; |
9277ec14 BC |
366 | } |
367 | ||
fc0b830f BC |
368 | /* |
369 | * This is assumed to be used only by the network code, | |
370 | * and so doesn't try and handle any of the other cases | |
371 | */ | |
9277ec14 BC |
372 | int fcntl(int fildes, int cmd, ...) |
373 | { | |
fc0b830f BC |
374 | /* |
375 | * non-blocking mode doesn't work the same as in BSD sockets, | |
376 | * so ignore it. | |
377 | */ | |
9277ec14 BC |
378 | #if 0 |
379 | va_list ap; | |
380 | int val, opt, status; | |
381 | ||
382 | if (cmd == F_GETFL) | |
383 | return 0; | |
384 | else if (cmd != F_SETFL) { | |
385 | errno = EINVAL; | |
10a6b3c6 | 386 | return -1; |
9277ec14 BC |
387 | } |
388 | ||
389 | va_start(ap, 1); | |
390 | ||
391 | opt = va_arg(ap, int); | |
392 | if (opt & O_NONBLOCK) | |
393 | val = 1; | |
394 | else | |
395 | val = 0; | |
396 | ||
397 | status = ioctlsocket((SOCKET)fildes, opt, &val); | |
398 | ||
399 | if (status == SOCKET_ERROR) { | |
400 | errno = EINVAL; | |
401 | val = -1; | |
402 | } | |
403 | ||
404 | va_end(ap); | |
405 | ||
406 | return val; | |
407 | #endif | |
408 | return 0; | |
409 | } | |
410 | ||
411 | /* | |
412 | * Get the value of a local clock source. | |
413 | * This implementation supports 2 clocks: CLOCK_MONOTONIC provides high-accuracy | |
414 | * relative time, while CLOCK_REALTIME provides a low-accuracy wall time. | |
415 | */ | |
416 | int clock_gettime(clockid_t clock_id, struct timespec *tp) | |
417 | { | |
418 | int rc = 0; | |
419 | ||
420 | if (clock_id == CLOCK_MONOTONIC) | |
421 | { | |
422 | static LARGE_INTEGER freq = {{0,0}}; | |
423 | LARGE_INTEGER counts; | |
424 | ||
425 | QueryPerformanceCounter(&counts); | |
426 | if (freq.QuadPart == 0) | |
427 | QueryPerformanceFrequency(&freq); | |
428 | ||
429 | tp->tv_sec = counts.QuadPart / freq.QuadPart; | |
430 | /* Get the difference between the number of ns stored | |
431 | * in 'tv_sec' and that stored in 'counts' */ | |
9576f613 | 432 | uint64_t t = tp->tv_sec * freq.QuadPart; |
9277ec14 BC |
433 | t = counts.QuadPart - t; |
434 | /* 't' now contains the number of cycles since the last second. | |
435 | * We want the number of nanoseconds, so multiply out by 1,000,000,000 | |
436 | * and then divide by the frequency. */ | |
437 | t *= 1000000000; | |
438 | tp->tv_nsec = t / freq.QuadPart; | |
439 | } | |
440 | else if (clock_id == CLOCK_REALTIME) | |
441 | { | |
442 | /* clock_gettime(CLOCK_REALTIME,...) is just an alias for gettimeofday with a | |
443 | * higher-precision field. */ | |
444 | struct timeval tv; | |
445 | gettimeofday(&tv, NULL); | |
446 | tp->tv_sec = tv.tv_sec; | |
447 | tp->tv_nsec = tv.tv_usec * 1000; | |
448 | } else { | |
449 | errno = EINVAL; | |
450 | rc = -1; | |
451 | } | |
452 | ||
453 | return rc; | |
454 | } | |
455 | ||
456 | int mlock(const void * addr, size_t len) | |
457 | { | |
10a6b3c6 BC |
458 | SIZE_T min, max; |
459 | BOOL success; | |
460 | HANDLE process = GetCurrentProcess(); | |
461 | ||
462 | success = GetProcessWorkingSetSize(process, &min, &max); | |
463 | if (!success) { | |
464 | errno = win_to_posix_error(GetLastError()); | |
465 | return -1; | |
466 | } | |
467 | ||
468 | min += len; | |
469 | max += len; | |
470 | success = SetProcessWorkingSetSize(process, min, max); | |
471 | if (!success) { | |
472 | errno = win_to_posix_error(GetLastError()); | |
473 | return -1; | |
474 | } | |
475 | ||
476 | success = VirtualLock((LPVOID)addr, len); | |
477 | if (!success) { | |
478 | errno = win_to_posix_error(GetLastError()); | |
479 | return -1; | |
480 | } | |
481 | ||
482 | return 0; | |
9277ec14 BC |
483 | } |
484 | ||
485 | int munlock(const void * addr, size_t len) | |
486 | { | |
10a6b3c6 BC |
487 | BOOL success = VirtualUnlock((LPVOID)addr, len); |
488 | if (!success) { | |
489 | errno = win_to_posix_error(GetLastError()); | |
490 | return -1; | |
491 | } | |
492 | ||
493 | return 0; | |
9277ec14 BC |
494 | } |
495 | ||
496 | pid_t waitpid(pid_t pid, int *stat_loc, int options) | |
497 | { | |
498 | log_err("%s is not implemented\n", __func__); | |
499 | errno = ENOSYS; | |
500 | return -1; | |
501 | } | |
502 | ||
503 | int usleep(useconds_t useconds) | |
504 | { | |
505 | Sleep(useconds / 1000); | |
506 | return 0; | |
507 | } | |
508 | ||
509 | char *basename(char *path) | |
510 | { | |
511 | static char name[MAX_PATH]; | |
512 | int i; | |
513 | ||
514 | if (path == NULL || strlen(path) == 0) | |
515 | return (char*)"."; | |
516 | ||
517 | i = strlen(path) - 1; | |
518 | ||
9576f613 | 519 | while (path[i] != '\\' && path[i] != '/' && i >= 0) |
9277ec14 BC |
520 | i--; |
521 | ||
9576f613 | 522 | strncpy(name, path + i + 1, MAX_PATH); |
9277ec14 BC |
523 | |
524 | return name; | |
525 | } | |
526 | ||
9277ec14 BC |
527 | int fsync(int fildes) |
528 | { | |
529 | HANDLE hFile = (HANDLE)_get_osfhandle(fildes); | |
10a6b3c6 BC |
530 | if (!FlushFileBuffers(hFile)) { |
531 | errno = win_to_posix_error(GetLastError()); | |
532 | return -1; | |
533 | } | |
534 | ||
535 | return 0; | |
9277ec14 BC |
536 | } |
537 | ||
538 | int nFileMappings = 0; | |
539 | HANDLE fileMappings[1024]; | |
540 | ||
541 | int shmget(key_t key, size_t size, int shmflg) | |
542 | { | |
543 | int mapid = -1; | |
d3987946 BC |
544 | uint32_t size_low = size & 0xFFFFFFFF; |
545 | uint32_t size_high = ((uint64_t)size) >> 32; | |
546 | HANDLE hMapping = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, (PAGE_EXECUTE_READWRITE | SEC_RESERVE), size_high, size_low, NULL); | |
9277ec14 BC |
547 | if (hMapping != NULL) { |
548 | fileMappings[nFileMappings] = hMapping; | |
549 | mapid = nFileMappings; | |
550 | nFileMappings++; | |
551 | } else { | |
552 | errno = ENOSYS; | |
553 | } | |
554 | ||
555 | return mapid; | |
556 | } | |
557 | ||
558 | void *shmat(int shmid, const void *shmaddr, int shmflg) | |
559 | { | |
560 | void* mapAddr; | |
561 | MEMORY_BASIC_INFORMATION memInfo; | |
562 | mapAddr = MapViewOfFile(fileMappings[shmid], FILE_MAP_ALL_ACCESS, 0, 0, 0); | |
10a6b3c6 BC |
563 | if (mapAddr == NULL) { |
564 | errno = win_to_posix_error(GetLastError()); | |
565 | return (void*)-1; | |
566 | } | |
567 | ||
568 | if (VirtualQuery(mapAddr, &memInfo, sizeof(memInfo)) == 0) { | |
569 | errno = win_to_posix_error(GetLastError()); | |
570 | return (void*)-1; | |
571 | } | |
572 | ||
9277ec14 | 573 | mapAddr = VirtualAlloc(mapAddr, memInfo.RegionSize, MEM_COMMIT, PAGE_READWRITE); |
10a6b3c6 BC |
574 | if (mapAddr == NULL) { |
575 | errno = win_to_posix_error(GetLastError()); | |
576 | return (void*)-1; | |
577 | } | |
578 | ||
9277ec14 BC |
579 | return mapAddr; |
580 | } | |
581 | ||
582 | int shmdt(const void *shmaddr) | |
583 | { | |
10a6b3c6 BC |
584 | if (!UnmapViewOfFile(shmaddr)) { |
585 | errno = win_to_posix_error(GetLastError()); | |
586 | return -1; | |
587 | } | |
588 | ||
589 | return 0; | |
9277ec14 BC |
590 | } |
591 | ||
592 | int shmctl(int shmid, int cmd, struct shmid_ds *buf) | |
593 | { | |
594 | if (cmd == IPC_RMID) { | |
595 | fileMappings[shmid] = INVALID_HANDLE_VALUE; | |
596 | return 0; | |
597 | } else { | |
598 | log_err("%s is not implemented\n", __func__); | |
599 | } | |
10a6b3c6 BC |
600 | errno = ENOSYS; |
601 | return -1; | |
9277ec14 BC |
602 | } |
603 | ||
604 | int setuid(uid_t uid) | |
605 | { | |
606 | log_err("%s is not implemented\n", __func__); | |
607 | errno = ENOSYS; | |
10a6b3c6 | 608 | return -1; |
9277ec14 BC |
609 | } |
610 | ||
611 | int setgid(gid_t gid) | |
612 | { | |
613 | log_err("%s is not implemented\n", __func__); | |
614 | errno = ENOSYS; | |
10a6b3c6 | 615 | return -1; |
9277ec14 BC |
616 | } |
617 | ||
618 | int nice(int incr) | |
619 | { | |
620 | if (incr != 0) { | |
621 | errno = EINVAL; | |
622 | return -1; | |
623 | } | |
624 | ||
625 | return 0; | |
626 | } | |
627 | ||
628 | int getrusage(int who, struct rusage *r_usage) | |
629 | { | |
9576f613 | 630 | const uint64_t SECONDS_BETWEEN_1601_AND_1970 = 11644473600; |
9277ec14 BC |
631 | FILETIME cTime, eTime, kTime, uTime; |
632 | time_t time; | |
7732a09b | 633 | HANDLE h; |
9277ec14 BC |
634 | |
635 | memset(r_usage, 0, sizeof(*r_usage)); | |
636 | ||
7732a09b HL |
637 | if (who == RUSAGE_SELF) { |
638 | h = GetCurrentProcess(); | |
639 | GetProcessTimes(h, &cTime, &eTime, &kTime, &uTime); | |
640 | } else if (who == RUSAGE_THREAD) { | |
641 | h = GetCurrentThread(); | |
642 | GetThreadTimes(h, &cTime, &eTime, &kTime, &uTime); | |
643 | } else { | |
644 | log_err("fio: getrusage %d is not implemented\n", who); | |
645 | return -1; | |
646 | } | |
647 | ||
9576f613 | 648 | time = ((uint64_t)uTime.dwHighDateTime << 32) + uTime.dwLowDateTime; |
9277ec14 BC |
649 | /* Divide by 10,000,000 to get the number of seconds and move the epoch from |
650 | * 1601 to 1970 */ | |
651 | time = (time_t)(((time)/10000000) - SECONDS_BETWEEN_1601_AND_1970); | |
652 | r_usage->ru_utime.tv_sec = time; | |
653 | /* getrusage() doesn't care about anything other than seconds, so set tv_usec to 0 */ | |
654 | r_usage->ru_utime.tv_usec = 0; | |
9576f613 | 655 | time = ((uint64_t)kTime.dwHighDateTime << 32) + kTime.dwLowDateTime; |
9277ec14 BC |
656 | /* Divide by 10,000,000 to get the number of seconds and move the epoch from |
657 | * 1601 to 1970 */ | |
658 | time = (time_t)(((time)/10000000) - SECONDS_BETWEEN_1601_AND_1970); | |
659 | r_usage->ru_stime.tv_sec = time; | |
660 | r_usage->ru_stime.tv_usec = 0; | |
661 | return 0; | |
662 | } | |
663 | ||
2277d5d5 BC |
664 | int posix_fadvise(int fd, off_t offset, off_t len, int advice) |
665 | { | |
666 | return 0; | |
667 | } | |
668 | ||
9277ec14 BC |
669 | int posix_madvise(void *addr, size_t len, int advice) |
670 | { | |
671 | log_err("%s is not implemented\n", __func__); | |
672 | return ENOSYS; | |
673 | } | |
674 | ||
fc0b830f | 675 | /* Windows doesn't support advice for memory pages. Just ignore it. */ |
9277ec14 BC |
676 | int msync(void *addr, size_t len, int flags) |
677 | { | |
9277ec14 BC |
678 | errno = ENOSYS; |
679 | return -1; | |
680 | } | |
681 | ||
682 | int fdatasync(int fildes) | |
683 | { | |
684 | return fsync(fildes); | |
685 | } | |
686 | ||
687 | ssize_t pwrite(int fildes, const void *buf, size_t nbyte, | |
688 | off_t offset) | |
689 | { | |
9576f613 BC |
690 | int64_t pos = _telli64(fildes); |
691 | ssize_t len = _write(fildes, buf, nbyte); | |
692 | _lseeki64(fildes, pos, SEEK_SET); | |
9277ec14 BC |
693 | return len; |
694 | } | |
695 | ||
696 | ssize_t pread(int fildes, void *buf, size_t nbyte, off_t offset) | |
697 | { | |
9576f613 | 698 | int64_t pos = _telli64(fildes); |
9277ec14 | 699 | ssize_t len = read(fildes, buf, nbyte); |
9576f613 | 700 | _lseeki64(fildes, pos, SEEK_SET); |
9277ec14 BC |
701 | return len; |
702 | } | |
703 | ||
704 | ssize_t readv(int fildes, const struct iovec *iov, int iovcnt) | |
705 | { | |
706 | log_err("%s is not implemented\n", __func__); | |
707 | errno = ENOSYS; | |
10a6b3c6 | 708 | return -1; |
9277ec14 BC |
709 | } |
710 | ||
711 | ssize_t writev(int fildes, const struct iovec *iov, int iovcnt) | |
712 | { | |
713 | log_err("%s is not implemented\n", __func__); | |
714 | errno = ENOSYS; | |
10a6b3c6 | 715 | return -1; |
9277ec14 BC |
716 | } |
717 | ||
718 | long long strtoll(const char *restrict str, char **restrict endptr, | |
719 | int base) | |
720 | { | |
721 | return _strtoi64(str, endptr, base); | |
722 | } | |
723 | ||
9277ec14 BC |
724 | int poll(struct pollfd fds[], nfds_t nfds, int timeout) |
725 | { | |
726 | struct timeval tv; | |
727 | struct timeval *to = NULL; | |
728 | fd_set readfds, writefds, exceptfds; | |
729 | int i; | |
730 | int rc; | |
731 | ||
3f457bea | 732 | if (timeout != -1) { |
f9a58c2a | 733 | to = &tv; |
3f457bea BC |
734 | to->tv_sec = timeout / 1000; |
735 | to->tv_usec = (timeout % 1000) * 1000; | |
736 | } | |
9277ec14 BC |
737 | |
738 | FD_ZERO(&readfds); | |
739 | FD_ZERO(&writefds); | |
740 | FD_ZERO(&exceptfds); | |
741 | ||
742 | for (i = 0; i < nfds; i++) | |
743 | { | |
744 | if (fds[i].fd < 0) { | |
745 | fds[i].revents = 0; | |
746 | continue; | |
747 | } | |
748 | ||
749 | if (fds[i].events & POLLIN) | |
750 | FD_SET(fds[i].fd, &readfds); | |
751 | ||
752 | if (fds[i].events & POLLOUT) | |
753 | FD_SET(fds[i].fd, &writefds); | |
754 | ||
f9a58c2a | 755 | FD_SET(fds[i].fd, &exceptfds); |
9277ec14 BC |
756 | } |
757 | ||
758 | rc = select(nfds, &readfds, &writefds, &exceptfds, to); | |
759 | ||
760 | if (rc != SOCKET_ERROR) { | |
761 | for (i = 0; i < nfds; i++) | |
762 | { | |
763 | if (fds[i].fd < 0) { | |
764 | continue; | |
765 | } | |
766 | ||
767 | if ((fds[i].events & POLLIN) && FD_ISSET(fds[i].fd, &readfds)) | |
768 | fds[i].revents |= POLLIN; | |
769 | ||
770 | if ((fds[i].events & POLLOUT) && FD_ISSET(fds[i].fd, &writefds)) | |
771 | fds[i].revents |= POLLOUT; | |
772 | ||
773 | if (FD_ISSET(fds[i].fd, &exceptfds)) | |
774 | fds[i].revents |= POLLHUP; | |
775 | } | |
776 | } | |
777 | ||
778 | return rc; | |
779 | } | |
780 | ||
781 | int nanosleep(const struct timespec *rqtp, struct timespec *rmtp) | |
782 | { | |
21c75387 BC |
783 | struct timeval tv; |
784 | DWORD ms_remaining; | |
785 | DWORD ms_total = (rqtp->tv_sec * 1000) + (rqtp->tv_nsec / 1000000.0); | |
786 | ||
787 | if (ms_total == 0) | |
788 | ms_total = 1; | |
789 | ||
790 | ms_remaining = ms_total; | |
791 | ||
792 | /* Since Sleep() can sleep for less than the requested time, add a loop to | |
793 | ensure we only return after the requested length of time has elapsed */ | |
794 | do { | |
795 | fio_gettime(&tv, NULL); | |
796 | Sleep(ms_remaining); | |
797 | ms_remaining = ms_total - mtime_since_now(&tv); | |
798 | } while (ms_remaining > 0 && ms_remaining < ms_total); | |
799 | ||
800 | /* this implementation will never sleep for less than the requested time */ | |
801 | if (rmtp != NULL) { | |
802 | rmtp->tv_sec = 0; | |
803 | rmtp->tv_nsec = 0; | |
804 | } | |
805 | ||
806 | return 0; | |
9277ec14 BC |
807 | } |
808 | ||
809 | DIR *opendir(const char *dirname) | |
810 | { | |
01d26955 BC |
811 | struct dirent_ctx *dc = NULL; |
812 | ||
813 | /* See if we can open it. If not, we'll return an error here */ | |
814 | HANDLE file = CreateFileA(dirname, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); | |
815 | if (file != INVALID_HANDLE_VALUE) { | |
816 | CloseHandle(file); | |
817 | dc = (struct dirent_ctx*)malloc(sizeof(struct dirent_ctx)); | |
818 | StringCchCopyA(dc->dirname, MAX_PATH, dirname); | |
819 | dc->find_handle = INVALID_HANDLE_VALUE; | |
820 | } else { | |
821 | DWORD error = GetLastError(); | |
822 | if (error == ERROR_FILE_NOT_FOUND) | |
823 | errno = ENOENT; | |
824 | ||
825 | else if (error == ERROR_PATH_NOT_FOUND) | |
826 | errno = ENOTDIR; | |
827 | else if (error == ERROR_TOO_MANY_OPEN_FILES) | |
828 | errno = ENFILE; | |
829 | else if (error == ERROR_ACCESS_DENIED) | |
830 | errno = EACCES; | |
831 | else | |
832 | errno = error; | |
833 | } | |
834 | ||
835 | return dc; | |
9277ec14 BC |
836 | } |
837 | ||
838 | int closedir(DIR *dirp) | |
839 | { | |
01d26955 BC |
840 | if (dirp != NULL && dirp->find_handle != INVALID_HANDLE_VALUE) |
841 | FindClose(dirp->find_handle); | |
ad9c0fbc | 842 | |
01d26955 BC |
843 | free(dirp); |
844 | return 0; | |
9277ec14 BC |
845 | } |
846 | ||
847 | struct dirent *readdir(DIR *dirp) | |
848 | { | |
ad9c0fbc BC |
849 | static struct dirent de; |
850 | WIN32_FIND_DATA find_data; | |
851 | ||
852 | if (dirp == NULL) | |
853 | return NULL; | |
854 | ||
855 | if (dirp->find_handle == INVALID_HANDLE_VALUE) { | |
856 | char search_pattern[MAX_PATH]; | |
857 | StringCchPrintfA(search_pattern, MAX_PATH, "%s\\*", dirp->dirname); | |
858 | dirp->find_handle = FindFirstFileA(search_pattern, &find_data); | |
859 | if (dirp->find_handle == INVALID_HANDLE_VALUE) | |
860 | return NULL; | |
861 | } else { | |
862 | if (!FindNextFile(dirp->find_handle, &find_data)) | |
863 | return NULL; | |
864 | } | |
865 | ||
866 | StringCchCopyA(de.d_name, MAX_PATH, find_data.cFileName); | |
867 | de.d_ino = 0; | |
868 | ||
869 | return &de; | |
9277ec14 BC |
870 | } |
871 | ||
872 | uid_t geteuid(void) | |
873 | { | |
874 | log_err("%s is not implemented\n", __func__); | |
875 | errno = ENOSYS; | |
876 | return -1; | |
877 | } | |
878 | ||
9277ec14 BC |
879 | const char* inet_ntop(int af, const void *restrict src, |
880 | char *restrict dst, socklen_t size) | |
881 | { | |
882 | INT status = SOCKET_ERROR; | |
883 | WSADATA wsd; | |
884 | char *ret = NULL; | |
885 | ||
886 | if (af != AF_INET && af != AF_INET6) { | |
887 | errno = EAFNOSUPPORT; | |
888 | return NULL; | |
889 | } | |
890 | ||
891 | WSAStartup(MAKEWORD(2,2), &wsd); | |
892 | ||
893 | if (af == AF_INET) { | |
894 | struct sockaddr_in si; | |
895 | DWORD len = size; | |
896 | memset(&si, 0, sizeof(si)); | |
897 | si.sin_family = af; | |
898 | memcpy(&si.sin_addr, src, sizeof(si.sin_addr)); | |
899 | status = WSAAddressToString((struct sockaddr*)&si, sizeof(si), NULL, dst, &len); | |
900 | } else if (af == AF_INET6) { | |
901 | struct sockaddr_in6 si6; | |
902 | DWORD len = size; | |
903 | memset(&si6, 0, sizeof(si6)); | |
904 | si6.sin6_family = af; | |
905 | memcpy(&si6.sin6_addr, src, sizeof(si6.sin6_addr)); | |
906 | status = WSAAddressToString((struct sockaddr*)&si6, sizeof(si6), NULL, dst, &len); | |
907 | } | |
908 | ||
909 | if (status != SOCKET_ERROR) | |
910 | ret = dst; | |
911 | else | |
912 | errno = ENOSPC; | |
913 | ||
914 | WSACleanup(); | |
3f457bea | 915 | |
9277ec14 BC |
916 | return ret; |
917 | } | |
918 | ||
919 | int inet_pton(int af, const char *restrict src, void *restrict dst) | |
920 | { | |
921 | INT status = SOCKET_ERROR; | |
922 | WSADATA wsd; | |
923 | int ret = 1; | |
924 | ||
925 | if (af != AF_INET && af != AF_INET6) { | |
926 | errno = EAFNOSUPPORT; | |
927 | return -1; | |
928 | } | |
929 | ||
930 | WSAStartup(MAKEWORD(2,2), &wsd); | |
931 | ||
932 | if (af == AF_INET) { | |
933 | struct sockaddr_in si; | |
934 | INT len = sizeof(si); | |
935 | memset(&si, 0, sizeof(si)); | |
936 | si.sin_family = af; | |
937 | status = WSAStringToAddressA((char*)src, af, NULL, (struct sockaddr*)&si, &len); | |
938 | if (status != SOCKET_ERROR) | |
939 | memcpy(dst, &si.sin_addr, sizeof(si.sin_addr)); | |
940 | } else if (af == AF_INET6) { | |
941 | struct sockaddr_in6 si6; | |
942 | INT len = sizeof(si6); | |
943 | memset(&si6, 0, sizeof(si6)); | |
944 | si6.sin6_family = af; | |
945 | status = WSAStringToAddressA((char*)src, af, NULL, (struct sockaddr*)&si6, &len); | |
946 | if (status != SOCKET_ERROR) | |
947 | memcpy(dst, &si6.sin6_addr, sizeof(si6.sin6_addr)); | |
948 | } | |
949 | ||
950 | if (status == SOCKET_ERROR) { | |
951 | errno = ENOSPC; | |
952 | ret = 0; | |
953 | } | |
954 | ||
955 | WSACleanup(); | |
956 | ||
957 | return ret; | |
958 | } |