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.
7 #include <netinet/in.h>
14 #include <semaphore.h>
18 #include <sys/resource.h>
21 #include "../os-windows.h"
23 long sysconf(int name)
27 SYSTEM_LOGICAL_PROCESSOR_INFORMATION processorInfo;
29 MEMORYSTATUSEX status;
33 case _SC_NPROCESSORS_ONLN:
34 len = sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
35 GetLogicalProcessorInformation(&processorInfo, &len);
36 val = len / sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
40 GetSystemInfo(&sysInfo);
41 val = sysInfo.dwPageSize;
45 status.dwLength = sizeof(status);
46 GlobalMemoryStatusEx(&status);
47 val = status.ullTotalPhys;
50 log_err("sysconf(%d) is not implemented\n", name);
57 char *dl_error = NULL;
59 int dlclose(void *handle)
61 return !FreeLibrary((HMODULE)handle);
64 void *dlopen(const char *file, int mode)
68 hMod = LoadLibrary(file);
69 if (hMod == INVALID_HANDLE_VALUE)
70 dl_error = (char*)"LoadLibrary failed";
77 void *dlsym(void *handle, const char *name)
81 fnPtr = GetProcAddress((HMODULE)handle, name);
83 dl_error = (char*)"GetProcAddress failed";
95 int gettimeofday(struct timeval *restrict tp, void *restrict tzp)
98 unsigned long long unix_time, windows_time;
99 const time_t MILLISECONDS_BETWEEN_1601_AND_1970 = 11644473600000;
101 // Ignore the timezone parameter
105 * Windows time is stored as the number 100 ns intervals since January 1 1601.
106 * Conversion details from http://www.informit.com/articles/article.aspx?p=102236&seqNum=3
107 * Its precision is 100 ns but accuracy is only one clock tick, or normally around 15 ms.
109 GetSystemTimeAsFileTime(&fileTime);
110 windows_time = ((unsigned long long)fileTime.dwHighDateTime << 32) + fileTime.dwLowDateTime;
111 /* Divide by 10,000 to convert to ms and subtract the time between 1601 and 1970 */
112 unix_time = (((windows_time)/10000) - MILLISECONDS_BETWEEN_1601_AND_1970);
113 /* unix_time is now the number of milliseconds since 1970 (the Unix epoch) */
114 tp->tv_sec = unix_time / 1000;
115 tp->tv_usec = (unix_time % 1000) * 1000;
119 void syslog(int priority, const char *message, ... /* argument */)
121 log_err("%s is not implemented\n", __func__);
124 int sigaction(int sig, const struct sigaction *act,
125 struct sigaction *oact)
131 int lstat(const char * path, struct stat * buf)
133 return stat(path, buf);
136 void *mmap(void *addr, size_t len, int prot, int flags,
137 int fildes, off_t off)
140 void* allocAddr = NULL;
142 if (prot & PROT_NONE)
143 vaProt |= PAGE_NOACCESS;
145 if ((prot & PROT_READ) && !(prot & PROT_WRITE))
146 vaProt |= PAGE_READONLY;
148 if (prot & PROT_WRITE)
149 vaProt |= PAGE_READWRITE;
151 if ((flags & MAP_ANON) | (flags & MAP_ANONYMOUS))
153 allocAddr = VirtualAlloc(addr, len, MEM_COMMIT, vaProt);
159 int munmap(void *addr, size_t len)
161 return !VirtualFree(addr, 0, MEM_RELEASE);
166 log_err("%s is not implemented\n", __func__);
173 log_err("%s is not implemented\n", __func__);
178 void openlog(const char *ident, int logopt, int facility)
180 log_err("%s is not implemented\n", __func__);
185 log_err("%s is not implemented\n", __func__);
188 int kill(pid_t pid, int sig)
194 // This is assumed to be used only by the network code,
195 // and so doesn't try and handle any of the other cases
196 int fcntl(int fildes, int cmd, ...)
198 // non-blocking mode doesn't work the same as in BSD sockets,
202 int val, opt, status;
206 else if (cmd != F_SETFL) {
213 opt = va_arg(ap, int);
214 if (opt & O_NONBLOCK)
219 status = ioctlsocket((SOCKET)fildes, opt, &val);
221 if (status == SOCKET_ERROR) {
234 * Get the value of a local clock source.
235 * This implementation supports 2 clocks: CLOCK_MONOTONIC provides high-accuracy
236 * relative time, while CLOCK_REALTIME provides a low-accuracy wall time.
238 int clock_gettime(clockid_t clock_id, struct timespec *tp)
242 if (clock_id == CLOCK_MONOTONIC)
244 static LARGE_INTEGER freq = {{0,0}};
245 LARGE_INTEGER counts;
247 QueryPerformanceCounter(&counts);
248 if (freq.QuadPart == 0)
249 QueryPerformanceFrequency(&freq);
251 tp->tv_sec = counts.QuadPart / freq.QuadPart;
252 /* Get the difference between the number of ns stored
253 * in 'tv_sec' and that stored in 'counts' */
254 unsigned long long t = tp->tv_sec * freq.QuadPart;
255 t = counts.QuadPart - t;
256 /* 't' now contains the number of cycles since the last second.
257 * We want the number of nanoseconds, so multiply out by 1,000,000,000
258 * and then divide by the frequency. */
260 tp->tv_nsec = t / freq.QuadPart;
262 else if (clock_id == CLOCK_REALTIME)
264 /* clock_gettime(CLOCK_REALTIME,...) is just an alias for gettimeofday with a
265 * higher-precision field. */
267 gettimeofday(&tv, NULL);
268 tp->tv_sec = tv.tv_sec;
269 tp->tv_nsec = tv.tv_usec * 1000;
278 int mlock(const void * addr, size_t len)
280 return !VirtualLock((LPVOID)addr, len);
283 int munlock(const void * addr, size_t len)
285 return !VirtualUnlock((LPVOID)addr, len);
288 pid_t waitpid(pid_t pid, int *stat_loc, int options)
290 log_err("%s is not implemented\n", __func__);
295 int usleep(useconds_t useconds)
297 Sleep(useconds / 1000);
301 char *basename(char *path)
303 static char name[MAX_PATH];
306 if (path == NULL || strlen(path) == 0)
309 i = strlen(path) - 1;
311 while (name[i] != '\\' && name[i] != '/' && i >= 0)
314 strcpy(name, path + i);
319 int posix_fallocate(int fd, off_t offset, off_t len)
321 log_err("%s is not implemented\n", __func__);
326 int ftruncate(int fildes, off_t length)
329 int old_pos = tell(fildes);
330 lseek(fildes, length, SEEK_SET);
331 HANDLE hFile = (HANDLE)_get_osfhandle(fildes);
332 bSuccess = SetEndOfFile(hFile);
333 lseek(fildes, old_pos, SEEK_SET);
337 int fsync(int fildes)
339 HANDLE hFile = (HANDLE)_get_osfhandle(fildes);
340 return !FlushFileBuffers(hFile);
343 int nFileMappings = 0;
344 HANDLE fileMappings[1024];
346 int shmget(key_t key, size_t size, int shmflg)
349 HANDLE hMapping = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, (PAGE_EXECUTE_READWRITE | SEC_RESERVE), size >> 32, size & 0xFFFFFFFF, NULL);
350 if (hMapping != NULL) {
351 fileMappings[nFileMappings] = hMapping;
352 mapid = nFileMappings;
361 void *shmat(int shmid, const void *shmaddr, int shmflg)
364 MEMORY_BASIC_INFORMATION memInfo;
365 mapAddr = MapViewOfFile(fileMappings[shmid], FILE_MAP_ALL_ACCESS, 0, 0, 0);
366 VirtualQuery(mapAddr, &memInfo, sizeof(memInfo));
367 mapAddr = VirtualAlloc(mapAddr, memInfo.RegionSize, MEM_COMMIT, PAGE_READWRITE);
371 int shmdt(const void *shmaddr)
373 return !UnmapViewOfFile(shmaddr);
376 int shmctl(int shmid, int cmd, struct shmid_ds *buf)
378 if (cmd == IPC_RMID) {
379 fileMappings[shmid] = INVALID_HANDLE_VALUE;
382 log_err("%s is not implemented\n", __func__);
387 int setuid(uid_t uid)
389 log_err("%s is not implemented\n", __func__);
394 int setgid(gid_t gid)
396 log_err("%s is not implemented\n", __func__);
411 int getrusage(int who, struct rusage *r_usage)
413 const time_t SECONDS_BETWEEN_1601_AND_1970 = 11644473600;
414 FILETIME cTime, eTime, kTime, uTime;
417 memset(r_usage, 0, sizeof(*r_usage));
419 HANDLE hProcess = GetCurrentProcess();
420 GetProcessTimes(hProcess, &cTime, &eTime, &kTime, &uTime);
421 time = ((unsigned long long)uTime.dwHighDateTime << 32) + uTime.dwLowDateTime;
422 /* Divide by 10,000,000 to get the number of seconds and move the epoch from
424 time = (time_t)(((time)/10000000) - SECONDS_BETWEEN_1601_AND_1970);
425 r_usage->ru_utime.tv_sec = time;
426 /* getrusage() doesn't care about anything other than seconds, so set tv_usec to 0 */
427 r_usage->ru_utime.tv_usec = 0;
428 time = ((unsigned long long)kTime.dwHighDateTime << 32) + kTime.dwLowDateTime;
429 /* Divide by 10,000,000 to get the number of seconds and move the epoch from
431 time = (time_t)(((time)/10000000) - SECONDS_BETWEEN_1601_AND_1970);
432 r_usage->ru_stime.tv_sec = time;
433 r_usage->ru_stime.tv_usec = 0;
437 int posix_madvise(void *addr, size_t len, int advice)
439 log_err("%s is not implemented\n", __func__);
443 // Windows doesn't support advice for memory pages. Just ignore it.
444 int msync(void *addr, size_t len, int flags)
446 log_err("%s is not implemented\n", __func__);
451 int fdatasync(int fildes)
453 return fsync(fildes);
456 ssize_t pwrite(int fildes, const void *buf, size_t nbyte,
459 long pos = tell(fildes);
460 ssize_t len = write(fildes, buf, nbyte);
461 lseek(fildes, pos, SEEK_SET);
465 ssize_t pread(int fildes, void *buf, size_t nbyte, off_t offset)
467 long pos = tell(fildes);
468 ssize_t len = read(fildes, buf, nbyte);
469 lseek(fildes, pos, SEEK_SET);
473 ssize_t readv(int fildes, const struct iovec *iov, int iovcnt)
475 log_err("%s is not implemented\n", __func__);
480 ssize_t writev(int fildes, const struct iovec *iov, int iovcnt)
482 log_err("%s is not implemented\n", __func__);
487 long long strtoll(const char *restrict str, char **restrict endptr,
490 return _strtoi64(str, endptr, base);
493 char *strsep(char **stringp, const char *delim)
495 char *orig = *stringp;
496 BOOL gotMatch = FALSE;
500 if (*stringp == NULL)
503 while ((*stringp)[i] != '\0') {
505 while (delim[j] != '\0') {
506 if ((*stringp)[i] == delim[j]) {
508 (*stringp)[i] = '\0';
509 *stringp = *stringp + i + 1;
526 int poll(struct pollfd fds[], nfds_t nfds, int timeout)
529 struct timeval *to = NULL;
530 fd_set readfds, writefds, exceptfds;
537 to->tv_sec = timeout / 1000;
538 to->tv_usec = (timeout % 1000) * 1000;
544 for (i = 0; i < nfds; i++)
551 if (fds[i].events & POLLIN)
552 FD_SET(fds[i].fd, &readfds);
554 if (fds[i].events & POLLOUT)
555 FD_SET(fds[i].fd, &writefds);
557 FD_SET(fds[i].fd, &exceptfds);
560 rc = select(nfds, &readfds, &writefds, &exceptfds, to);
562 if (rc != SOCKET_ERROR) {
563 for (i = 0; i < nfds; i++)
569 if ((fds[i].events & POLLIN) && FD_ISSET(fds[i].fd, &readfds))
570 fds[i].revents |= POLLIN;
572 if ((fds[i].events & POLLOUT) && FD_ISSET(fds[i].fd, &writefds))
573 fds[i].revents |= POLLOUT;
575 if (FD_ISSET(fds[i].fd, &exceptfds))
576 fds[i].revents |= POLLHUP;
583 int nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
585 log_err("%s is not implemented\n", __func__);
590 DIR *opendir(const char *dirname)
592 log_err("%s is not implemented\n", __func__);
597 int closedir(DIR *dirp)
599 log_err("%s is not implemented\n", __func__);
604 struct dirent *readdir(DIR *dirp)
606 log_err("%s is not implemented\n", __func__);
613 log_err("%s is not implemented\n", __func__);
618 int inet_aton(char *addr)
620 log_err("%s is not implemented\n", __func__);
625 const char* inet_ntop(int af, const void *restrict src,
626 char *restrict dst, socklen_t size)
628 INT status = SOCKET_ERROR;
632 if (af != AF_INET && af != AF_INET6) {
633 errno = EAFNOSUPPORT;
637 WSAStartup(MAKEWORD(2,2), &wsd);
640 struct sockaddr_in si;
642 memset(&si, 0, sizeof(si));
644 memcpy(&si.sin_addr, src, sizeof(si.sin_addr));
645 status = WSAAddressToString((struct sockaddr*)&si, sizeof(si), NULL, dst, &len);
646 } else if (af == AF_INET6) {
647 struct sockaddr_in6 si6;
649 memset(&si6, 0, sizeof(si6));
650 si6.sin6_family = af;
651 memcpy(&si6.sin6_addr, src, sizeof(si6.sin6_addr));
652 status = WSAAddressToString((struct sockaddr*)&si6, sizeof(si6), NULL, dst, &len);
655 if (status != SOCKET_ERROR)
664 int inet_pton(int af, const char *restrict src, void *restrict dst)
666 INT status = SOCKET_ERROR;
670 if (af != AF_INET && af != AF_INET6) {
671 errno = EAFNOSUPPORT;
675 WSAStartup(MAKEWORD(2,2), &wsd);
678 struct sockaddr_in si;
679 INT len = sizeof(si);
680 memset(&si, 0, sizeof(si));
682 status = WSAStringToAddressA((char*)src, af, NULL, (struct sockaddr*)&si, &len);
683 if (status != SOCKET_ERROR)
684 memcpy(dst, &si.sin_addr, sizeof(si.sin_addr));
685 } else if (af == AF_INET6) {
686 struct sockaddr_in6 si6;
687 INT len = sizeof(si6);
688 memset(&si6, 0, sizeof(si6));
689 si6.sin6_family = af;
690 status = WSAStringToAddressA((char*)src, af, NULL, (struct sockaddr*)&si6, &len);
691 if (status != SOCKET_ERROR)
692 memcpy(dst, &si6.sin6_addr, sizeof(si6.sin6_addr));
695 if (status == SOCKET_ERROR) {