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