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)
128 void (*prev_handler)(int);
130 prev_handler = signal(sig, act->sa_handler);
132 oact->sa_handler = prev_handler;
134 if (prev_handler == SIG_ERR)
140 int lstat(const char * path, struct stat * buf)
142 return stat(path, buf);
145 void *mmap(void *addr, size_t len, int prot, int flags,
146 int fildes, off_t off)
149 void* allocAddr = NULL;
151 if (prot & PROT_NONE)
152 vaProt |= PAGE_NOACCESS;
154 if ((prot & PROT_READ) && !(prot & PROT_WRITE))
155 vaProt |= PAGE_READONLY;
157 if (prot & PROT_WRITE)
158 vaProt |= PAGE_READWRITE;
160 if ((flags & MAP_ANON) | (flags & MAP_ANONYMOUS))
162 allocAddr = VirtualAlloc(addr, len, MEM_COMMIT, vaProt);
168 int munmap(void *addr, size_t len)
170 return !VirtualFree(addr, 0, MEM_RELEASE);
175 log_err("%s is not implemented\n", __func__);
182 log_err("%s is not implemented\n", __func__);
187 void openlog(const char *ident, int logopt, int facility)
189 log_err("%s is not implemented\n", __func__);
194 log_err("%s is not implemented\n", __func__);
197 int kill(pid_t pid, int sig)
204 * This is assumed to be used only by the network code,
205 * and so doesn't try and handle any of the other cases
207 int fcntl(int fildes, int cmd, ...)
210 * non-blocking mode doesn't work the same as in BSD sockets,
215 int val, opt, status;
219 else if (cmd != F_SETFL) {
226 opt = va_arg(ap, int);
227 if (opt & O_NONBLOCK)
232 status = ioctlsocket((SOCKET)fildes, opt, &val);
234 if (status == SOCKET_ERROR) {
247 * Get the value of a local clock source.
248 * This implementation supports 2 clocks: CLOCK_MONOTONIC provides high-accuracy
249 * relative time, while CLOCK_REALTIME provides a low-accuracy wall time.
251 int clock_gettime(clockid_t clock_id, struct timespec *tp)
255 if (clock_id == CLOCK_MONOTONIC)
257 static LARGE_INTEGER freq = {{0,0}};
258 LARGE_INTEGER counts;
260 QueryPerformanceCounter(&counts);
261 if (freq.QuadPart == 0)
262 QueryPerformanceFrequency(&freq);
264 tp->tv_sec = counts.QuadPart / freq.QuadPart;
265 /* Get the difference between the number of ns stored
266 * in 'tv_sec' and that stored in 'counts' */
267 unsigned long long t = tp->tv_sec * freq.QuadPart;
268 t = counts.QuadPart - t;
269 /* 't' now contains the number of cycles since the last second.
270 * We want the number of nanoseconds, so multiply out by 1,000,000,000
271 * and then divide by the frequency. */
273 tp->tv_nsec = t / freq.QuadPart;
275 else if (clock_id == CLOCK_REALTIME)
277 /* clock_gettime(CLOCK_REALTIME,...) is just an alias for gettimeofday with a
278 * higher-precision field. */
280 gettimeofday(&tv, NULL);
281 tp->tv_sec = tv.tv_sec;
282 tp->tv_nsec = tv.tv_usec * 1000;
291 int mlock(const void * addr, size_t len)
293 return !VirtualLock((LPVOID)addr, len);
296 int munlock(const void * addr, size_t len)
298 return !VirtualUnlock((LPVOID)addr, len);
301 pid_t waitpid(pid_t pid, int *stat_loc, int options)
303 log_err("%s is not implemented\n", __func__);
308 int usleep(useconds_t useconds)
310 Sleep(useconds / 1000);
314 char *basename(char *path)
316 static char name[MAX_PATH];
319 if (path == NULL || strlen(path) == 0)
322 i = strlen(path) - 1;
324 while (name[i] != '\\' && name[i] != '/' && i >= 0)
327 strcpy(name, path + i);
332 int posix_fallocate(int fd, off_t offset, off_t len)
334 log_err("%s is not implemented\n", __func__);
339 int ftruncate(int fildes, off_t length)
342 int old_pos = tell(fildes);
343 lseek(fildes, length, SEEK_SET);
344 HANDLE hFile = (HANDLE)_get_osfhandle(fildes);
345 bSuccess = SetEndOfFile(hFile);
346 lseek(fildes, old_pos, SEEK_SET);
350 int fsync(int fildes)
352 HANDLE hFile = (HANDLE)_get_osfhandle(fildes);
353 return !FlushFileBuffers(hFile);
356 int nFileMappings = 0;
357 HANDLE fileMappings[1024];
359 int shmget(key_t key, size_t size, int shmflg)
362 HANDLE hMapping = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, (PAGE_EXECUTE_READWRITE | SEC_RESERVE), size >> 32, size & 0xFFFFFFFF, NULL);
363 if (hMapping != NULL) {
364 fileMappings[nFileMappings] = hMapping;
365 mapid = nFileMappings;
374 void *shmat(int shmid, const void *shmaddr, int shmflg)
377 MEMORY_BASIC_INFORMATION memInfo;
378 mapAddr = MapViewOfFile(fileMappings[shmid], FILE_MAP_ALL_ACCESS, 0, 0, 0);
379 VirtualQuery(mapAddr, &memInfo, sizeof(memInfo));
380 mapAddr = VirtualAlloc(mapAddr, memInfo.RegionSize, MEM_COMMIT, PAGE_READWRITE);
384 int shmdt(const void *shmaddr)
386 return !UnmapViewOfFile(shmaddr);
389 int shmctl(int shmid, int cmd, struct shmid_ds *buf)
391 if (cmd == IPC_RMID) {
392 fileMappings[shmid] = INVALID_HANDLE_VALUE;
395 log_err("%s is not implemented\n", __func__);
400 int setuid(uid_t uid)
402 log_err("%s is not implemented\n", __func__);
407 int setgid(gid_t gid)
409 log_err("%s is not implemented\n", __func__);
424 int getrusage(int who, struct rusage *r_usage)
426 const time_t SECONDS_BETWEEN_1601_AND_1970 = 11644473600;
427 FILETIME cTime, eTime, kTime, uTime;
430 memset(r_usage, 0, sizeof(*r_usage));
432 HANDLE hProcess = GetCurrentProcess();
433 GetProcessTimes(hProcess, &cTime, &eTime, &kTime, &uTime);
434 time = ((unsigned long long)uTime.dwHighDateTime << 32) + uTime.dwLowDateTime;
435 /* Divide by 10,000,000 to get the number of seconds and move the epoch from
437 time = (time_t)(((time)/10000000) - SECONDS_BETWEEN_1601_AND_1970);
438 r_usage->ru_utime.tv_sec = time;
439 /* getrusage() doesn't care about anything other than seconds, so set tv_usec to 0 */
440 r_usage->ru_utime.tv_usec = 0;
441 time = ((unsigned long long)kTime.dwHighDateTime << 32) + kTime.dwLowDateTime;
442 /* Divide by 10,000,000 to get the number of seconds and move the epoch from
444 time = (time_t)(((time)/10000000) - SECONDS_BETWEEN_1601_AND_1970);
445 r_usage->ru_stime.tv_sec = time;
446 r_usage->ru_stime.tv_usec = 0;
450 int posix_madvise(void *addr, size_t len, int advice)
452 log_err("%s is not implemented\n", __func__);
456 /* Windows doesn't support advice for memory pages. Just ignore it. */
457 int msync(void *addr, size_t len, int flags)
459 log_err("%s is not implemented\n", __func__);
464 int fdatasync(int fildes)
466 return fsync(fildes);
469 ssize_t pwrite(int fildes, const void *buf, size_t nbyte,
472 long pos = tell(fildes);
473 ssize_t len = write(fildes, buf, nbyte);
474 lseek(fildes, pos, SEEK_SET);
478 ssize_t pread(int fildes, void *buf, size_t nbyte, off_t offset)
480 long pos = tell(fildes);
481 ssize_t len = read(fildes, buf, nbyte);
482 lseek(fildes, pos, SEEK_SET);
486 ssize_t readv(int fildes, const struct iovec *iov, int iovcnt)
488 log_err("%s is not implemented\n", __func__);
493 ssize_t writev(int fildes, const struct iovec *iov, int iovcnt)
495 log_err("%s is not implemented\n", __func__);
500 long long strtoll(const char *restrict str, char **restrict endptr,
503 return _strtoi64(str, endptr, base);
506 char *strsep(char **stringp, const char *delim)
508 char *orig = *stringp;
509 BOOL gotMatch = FALSE;
513 if (*stringp == NULL)
516 while ((*stringp)[i] != '\0') {
518 while (delim[j] != '\0') {
519 if ((*stringp)[i] == delim[j]) {
521 (*stringp)[i] = '\0';
522 *stringp = *stringp + i + 1;
539 int poll(struct pollfd fds[], nfds_t nfds, int timeout)
542 struct timeval *to = NULL;
543 fd_set readfds, writefds, exceptfds;
550 to->tv_sec = timeout / 1000;
551 to->tv_usec = (timeout % 1000) * 1000;
557 for (i = 0; i < nfds; i++)
564 if (fds[i].events & POLLIN)
565 FD_SET(fds[i].fd, &readfds);
567 if (fds[i].events & POLLOUT)
568 FD_SET(fds[i].fd, &writefds);
570 FD_SET(fds[i].fd, &exceptfds);
573 rc = select(nfds, &readfds, &writefds, &exceptfds, to);
575 if (rc != SOCKET_ERROR) {
576 for (i = 0; i < nfds; i++)
582 if ((fds[i].events & POLLIN) && FD_ISSET(fds[i].fd, &readfds))
583 fds[i].revents |= POLLIN;
585 if ((fds[i].events & POLLOUT) && FD_ISSET(fds[i].fd, &writefds))
586 fds[i].revents |= POLLOUT;
588 if (FD_ISSET(fds[i].fd, &exceptfds))
589 fds[i].revents |= POLLHUP;
596 int nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
598 log_err("%s is not implemented\n", __func__);
603 DIR *opendir(const char *dirname)
605 log_err("%s is not implemented\n", __func__);
610 int closedir(DIR *dirp)
612 log_err("%s is not implemented\n", __func__);
617 struct dirent *readdir(DIR *dirp)
619 log_err("%s is not implemented\n", __func__);
626 log_err("%s is not implemented\n", __func__);
631 int inet_aton(char *addr)
633 log_err("%s is not implemented\n", __func__);
638 const char* inet_ntop(int af, const void *restrict src,
639 char *restrict dst, socklen_t size)
641 INT status = SOCKET_ERROR;
645 if (af != AF_INET && af != AF_INET6) {
646 errno = EAFNOSUPPORT;
650 WSAStartup(MAKEWORD(2,2), &wsd);
653 struct sockaddr_in si;
655 memset(&si, 0, sizeof(si));
657 memcpy(&si.sin_addr, src, sizeof(si.sin_addr));
658 status = WSAAddressToString((struct sockaddr*)&si, sizeof(si), NULL, dst, &len);
659 } else if (af == AF_INET6) {
660 struct sockaddr_in6 si6;
662 memset(&si6, 0, sizeof(si6));
663 si6.sin6_family = af;
664 memcpy(&si6.sin6_addr, src, sizeof(si6.sin6_addr));
665 status = WSAAddressToString((struct sockaddr*)&si6, sizeof(si6), NULL, dst, &len);
668 if (status != SOCKET_ERROR)
677 int inet_pton(int af, const char *restrict src, void *restrict dst)
679 INT status = SOCKET_ERROR;
683 if (af != AF_INET && af != AF_INET6) {
684 errno = EAFNOSUPPORT;
688 WSAStartup(MAKEWORD(2,2), &wsd);
691 struct sockaddr_in si;
692 INT len = sizeof(si);
693 memset(&si, 0, sizeof(si));
695 status = WSAStringToAddressA((char*)src, af, NULL, (struct sockaddr*)&si, &len);
696 if (status != SOCKET_ERROR)
697 memcpy(dst, &si.sin_addr, sizeof(si.sin_addr));
698 } else if (af == AF_INET6) {
699 struct sockaddr_in6 si6;
700 INT len = sizeof(si6);
701 memset(&si6, 0, sizeof(si6));
702 si6.sin6_family = af;
703 status = WSAStringToAddressA((char*)src, af, NULL, (struct sockaddr*)&si6, &len);
704 if (status != SOCKET_ERROR)
705 memcpy(dst, &si6.sin6_addr, sizeof(si6.sin6_addr));
708 if (status == SOCKET_ERROR) {