Skip to content

Commit

Permalink
common: Extend win32 compatbility layer
Browse files Browse the repository at this point in the history
For Windows compatibility, we're defining the following:

* random
* get_page_size
* setenv
* unsetenv
* _putenv_s - defined by Windows headers but not exposed by Mingw
              when _POSIX is set

* EDQUOT
* uint

While at it, we're going to avoid importing sys/resourcectl.h on
Windows.

Signed-off-by: Lucian Petrut <[email protected]>
  • Loading branch information
petrutlucian94 committed Oct 12, 2020
1 parent a333b8d commit 78bbf80
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
25 changes: 25 additions & 0 deletions src/common/compat.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include <thread>
#ifndef _WIN32
#include <sys/mount.h>
#else
#include <stdlib.h>
#endif
#include <sys/param.h>
#include <sys/socket.h>
Expand Down Expand Up @@ -279,6 +281,10 @@ long int lrand48(void) {
return val;
}

int random() {
return rand();
}

int fsync(int fd) {
HANDLE handle = (HANDLE*)_get_osfhandle(fd);
if (handle == INVALID_HANDLE_VALUE)
Expand Down Expand Up @@ -457,4 +463,23 @@ int win_socketpair(int socks[2])
return SOCKET_ERROR;
}

unsigned get_page_size() {
SYSTEM_INFO system_info;
GetSystemInfo(&system_info);
return system_info.dwPageSize;
}

int setenv(const char *name, const char *value, int overwrite) {
if (!overwrite && getenv(name)) {
return 0;
}
return _putenv_s(name, value);
}

#else

unsigned get_page_size() {
return sysconf(_SC_PAGESIZE);
}

#endif /* _WIN32 */
16 changes: 16 additions & 0 deletions src/include/compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ extern "C" {

int pipe_cloexec(int pipefd[2], int flags);
char *ceph_strerror_r(int errnum, char *buf, size_t buflen);
unsigned get_page_size();

#ifdef __cplusplus
}
Expand All @@ -221,6 +222,10 @@ char *ceph_strerror_r(int errnum, char *buf, size_t buflen);
#define WIN32_ERROR 0
#undef ERROR

#ifndef uint
typedef unsigned int uint;
#endif

typedef _sigset_t sigset_t;

typedef int uid_t;
Expand Down Expand Up @@ -262,6 +267,10 @@ struct iovec {
#define ENODATA 120
#endif

#ifndef EDQUOT
#define EDQUOT ENOSPC
#endif

#define ESHUTDOWN ECONNABORTED
#define ESTALE 256
#define EREMOTEIO 257
Expand All @@ -280,6 +289,7 @@ ssize_t pread(int fd, void *buf, size_t count, off_t offset);
ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset);

long int lrand48(void);
int random();

int pipe(int pipefd[2]);

Expand All @@ -290,9 +300,15 @@ char *strptime(const char *s, const char *format, struct tm *tm);
int chown(const char *path, uid_t owner, gid_t group);
int fchown(int fd, uid_t owner, gid_t group);
int lchown(const char *path, uid_t owner, gid_t group);
int setenv(const char *name, const char *value, int overwrite);
#define unsetenv(name) _putenv_s(name, "")

int win_socketpair(int socks[2]);

#ifdef __MINGW32__
extern _CRTIMP errno_t __cdecl _putenv_s(const char *_Name,const char *_Value);
#endif

#ifdef __cplusplus
}
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/include/coredumpctl.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ class PrCtl {
};

#else
#include <sys/resource.h>
#ifdef RLIMIT_CORE
#include <sys/resource.h>
#include <iostream>
#include <sys/resource.h>
#include "common/errno.h"
Expand Down

0 comments on commit 78bbf80

Please sign in to comment.