From f2ff836243de7360dc780df641e80ac3039b812d Mon Sep 17 00:00:00 2001 From: Marko Kreen Date: Wed, 3 Dec 2008 11:04:06 +0000 Subject: [PATCH] win32: more fixes - move socket errors back to errno - don't call closesocket() on plain files --- src/main.c | 2 +- src/util.c | 4 ++-- win32/compat_win32.h | 39 +++++++++++++++++++++++++++++++++++---- 3 files changed, 38 insertions(+), 7 deletions(-) diff --git a/src/main.c b/src/main.c index 801e19e..b8ce734 100644 --- a/src/main.c +++ b/src/main.c @@ -549,7 +549,7 @@ static void write_pidfile(void) res = safe_write(fd, buf, strlen(buf)); if (res < 0) fatal_perror(cf_pidfile); - safe_close(fd); + close(fd); /* only remove when we have it actually written */ atexit(remove_pidfile); diff --git a/src/util.c b/src/util.c index c6de1a6..e39bfc0 100644 --- a/src/util.c +++ b/src/util.c @@ -123,7 +123,7 @@ static void write_syslog(const char *pfx, const char *msg) void close_logfile(void) { if (log_fd > 0) { - safe_close(log_fd); + close(log_fd); log_fd = 0; } close_syslog(); @@ -432,7 +432,7 @@ char *load_file(const char *fn) goto load_error; } - safe_close(fd); + close(fd); buf[st.st_size] = 0; return buf; diff --git a/win32/compat_win32.h b/win32/compat_win32.h index ca469c3..d77ef39 100644 --- a/win32/compat_win32.h +++ b/win32/compat_win32.h @@ -15,9 +15,6 @@ #define EAGAIN WSAEWOULDBLOCK // WSAEAGAIN #define EMSGSIZE WSAEMSGSIZE -#undef errno -#define errno WSAGetLastError() - /* dummy types / functions */ #define uid_t int #define gid_t int @@ -89,13 +86,47 @@ struct cmsghdr { (struct cmsghdr *)((u_char *)(cmsg) + CMSG_ALIGN((cmsg)->cmsg_len)))) #define CMSG_SPACE(len) (CMSG_ALIGN(sizeof(struct cmsghdr))+CMSG_ALIGN(len)) +/* + * unify WSAGetLastError() with errno. + */ + +static inline int ewrap(int res) { + if (res < 0) + errno = WSAGetLastError(); + return res; +} + /* proper signature for setsockopt */ static inline int w_setsockopt(int fd, int level, int optname, const void *optval, socklen_t optlen) { - return setsockopt(fd, level, optname, optval, optlen); + return ewrap(setsockopt(fd, level, optname, optval, optlen)); } #define setsockopt(a,b,c,d,e) w_setsockopt(a,b,c,d,e) +#define connect(a,b,c) ewrap(connect(a,b,c)) +#define recv(a,b,c,d) ewrap(recv(a,b,c,d)) +#define send(a,b,c,d) ewrap(send(a,b,c,d)) +#define socket(a,b,c) ewrap(socket(a,b,c)) +#define bind(a,b,c) ewrap(bind(a,b,c)) +#define listen(a,b) ewrap(listen(a,b)) +#define accept(a,b,c) ewrap(accept(a,b,c)) +#define getpeername(a,b,c) ewrap(getpeername(a,b,c)) +#define getsockname(a,b,c) ewrap(getsockname(a,b,c)) + +static inline struct hostent *w_gethostbyname(const char *n) { + struct hostent *res = gethostbyname(n); + if (!res) errno = WSAGetLastError(); + return res; +} +#define gethostbyname(a) w_gethostbyname(a) + +static inline const char *w_strerror(int e) { + /* wsa does not have its own strerror, maybe main one works */ + return strerror(e); +} +#define strerror(x) w_strerror(x) + + /* gettimeoutday() */ static inline int win32_gettimeofday(struct timeval * tp, void * tzp) { -- 2.39.5