diff options
author | Glenn Strauss <gstrauss@gluelogic.com> | 2020-11-26 15:17:02 -0500 |
---|---|---|
committer | Glenn Strauss <gstrauss@gluelogic.com> | 2023-05-03 23:11:34 -0400 |
commit | abaf0a5f4d17206340f54420558fc7dfaae62e4f (patch) | |
tree | 10c041f2a1edc46e31424dde903d15b599c9b564 /src | |
parent | 9b9dd8efafbf74b173f7d79f791b0736c797900f (diff) | |
download | lighttpd-git-abaf0a5f4d17206340f54420558fc7dfaae62e4f.tar.gz |
[core] _WIN32 socket-compat, filesystem-compat
_WIN32 is sufficiently different -- *different*; not better -- that
isolating _WIN32 code is clearer than #ifdef _WIN32 in almost every
func in fdevent.c
_WIN32-specific fdevent_socket_* funcs
_WIN32 SOCKET fds must be closed with closesocket(), not close()
_WIN32 HANDLE_FLAG_INHERIT for FD_CLOEXEC
_WIN32 use _sopen_s() without _O_TEMPORARY
Use _sopen_s() without _O_TEMPORARY in fdevent_mkostemp().
_O_TEMPORARY would remove file once last handle to file is closed.
Temporary files in chunkqueue may be closed for large request/response
_WIN32 fdevent_rename() using MoveFileExA
_WIN32 rename() fails if the target file already exists.
Alternatives are MoveFileExA() or ReplaceFileA().
Both of the above fail if either oldfile or newfile are open, so
- not atomic
- may fail sporadically
Diffstat (limited to 'src')
-rw-r--r-- | src/connections.c | 6 | ||||
-rw-r--r-- | src/fdevent.c | 71 | ||||
-rw-r--r-- | src/fdevent.h | 16 | ||||
-rw-r--r-- | src/fdevent_impl.c | 2 | ||||
-rw-r--r-- | src/fdevent_win32.c | 234 | ||||
-rw-r--r-- | src/gw_backend.c | 18 | ||||
-rw-r--r-- | src/h2.c | 4 | ||||
-rw-r--r-- | src/mod_authn_dbi.c | 3 | ||||
-rw-r--r-- | src/mod_vhostdb_dbi.c | 3 | ||||
-rw-r--r-- | src/mod_vhostdb_mysql.c | 3 | ||||
-rw-r--r-- | src/network.c | 18 | ||||
-rw-r--r-- | src/server.c | 13 | ||||
-rw-r--r-- | src/sys-socket.h | 17 |
13 files changed, 326 insertions, 82 deletions
diff --git a/src/connections.c b/src/connections.c index cfc2cb19..ab10011d 100644 --- a/src/connections.c +++ b/src/connections.c @@ -88,11 +88,7 @@ static void connection_close(connection *con) { fdevent_fdnode_event_del(srv->ev, con->fdn); fdevent_unregister(srv->ev, con->fdn); con->fdn = NULL; -#ifdef __WIN32 - if (0 == closesocket(con->fd)) -#else - if (0 == close(con->fd)) -#endif + if (0 == fdio_close_socket(con->fd)) --srv->cur_fds; else log_perror(r->conf.errh, __FILE__, __LINE__, diff --git a/src/fdevent.c b/src/fdevent.c index e424d982..67a7ea60 100644 --- a/src/fdevent.c +++ b/src/fdevent.c @@ -11,16 +11,11 @@ #include <errno.h> #include <fcntl.h> -#ifdef _WIN32 -#include <sys/stat.h> /* _S_IREAD _S_IWRITE */ -#include <io.h> -#include <share.h> /* _SH_DENYRW */ -#include <winsock2.h> -#endif - #include "ck.h" #define force_assert(x) ck_assert(x) +#ifndef _WIN32 + #ifdef SOCK_CLOEXEC static int use_sock_cloexec; #endif @@ -135,7 +130,6 @@ int fdevent_socket_nb_cloexec(int domain, int type, int protocol) { return fd; } -#ifndef _WIN32 #if 0 /* not used */ int fdevent_socketpair_cloexec (int domain, int typ, int protocol, int sv[2]) @@ -145,9 +139,13 @@ int fdevent_socketpair_cloexec (int domain, int typ, int protocol, int sv[2]) return socketpair(domain, typ | SOCK_CLOEXEC, protocol, sv); #else if (0 == socketpair(domain, typ, protocol, sv)) { - fdevent_setfd_cloexec(sv[0]); - fdevent_setfd_cloexec(sv[1]); - return 0; + if (0 == fdevent_socket_set_cloexec(sv[0]) + && 0 == fdevent_socket_set_cloexec(sv[1])) + return 0; + + close(sv[0]); + close(sv[1]); + sv[0] = sv[1] = -1; } return -1; #endif @@ -160,8 +158,8 @@ int fdevent_socketpair_nb_cloexec (int domain, int typ, int protocol, int sv[2]) return socketpair(domain, typ | SOCK_NONBLOCK | SOCK_CLOEXEC, protocol, sv); #else if (0 == socketpair(domain, typ, protocol, sv)) { - if (0 == fdevent_fcntl_set_nb_cloexec(sv[0]) - && 0 == fdevent_fcntl_set_nb_cloexec(sv[1])) + if (0 == fdevent_socket_set_nb_cloexec(sv[0]) + && 0 == fdevent_socket_set_nb_cloexec(sv[1])) return 0; close(sv[0]); @@ -174,8 +172,6 @@ int fdevent_socketpair_nb_cloexec (int domain, int typ, int protocol, int sv[2]) #endif /* not used */ -#endif /* !_WIN32 */ - int fdevent_dup_cloexec (int fd) { #ifdef F_DUPFD_CLOEXEC return fcntl(fd, F_DUPFD_CLOEXEC, 3); @@ -228,9 +224,7 @@ int fdevent_open_cloexec(const char *pathname, int symlinks, int flags, mode_t m int fdevent_open_devnull(void) { - #if defined(_WIN32) - return fdevent_open_cloexec("nul", 0, O_RDWR, 0); - #elif defined(__sun) /* /dev/null is a symlink on Illumos */ + #if defined(__sun) /* /dev/null is a symlink on Illumos */ return fdevent_open_cloexec("/dev/null", 1, O_RDWR, 0); #else return fdevent_open_cloexec("/dev/null", 0, O_RDWR, 0); @@ -254,14 +248,7 @@ int fdevent_open_dirname(char *path, int symlinks) { } -#ifdef _WIN32 -#include <stdio.h> -#endif - int fdevent_pipe_cloexec (int * const fds, const unsigned int bufsz_hint) { - #ifdef _WIN32 - return _pipe(fds, bufsz_hint, _O_BINARY | _O_NOINHERIT); - #else #ifdef HAVE_PIPE2 if (0 != pipe2(fds, O_CLOEXEC)) #endif @@ -281,22 +268,16 @@ int fdevent_pipe_cloexec (int * const fds, const unsigned int bufsz_hint) { UNUSED(bufsz_hint); #endif return 0; - #endif +} + + +int fdevent_socket_close(int fd) { + return close(fd); } int fdevent_mkostemp(char *path, int flags) { - #ifdef _WIN32 - /* future: if _sopen_s() returns EEXIST, might reset template (path) with - * trailing "XXXXXX", and then loop to try again */ - int fd; /*(flags might have _O_APPEND)*/ - return (0 == _mktemp_s(path, strlen(path)+1)) - && (0 == _sopen_s(&fd, path, _O_CREAT | _O_EXCL | _O_TEMPORARY - | flags | _O_BINARY | _O_NOINHERIT, - _SH_DENYRW, _S_IREAD | _S_IWRITE)) - ? fd - : -1; - #elif defined(HAVE_MKOSTEMP) + #if defined(HAVE_MKOSTEMP) return mkostemp(path, O_CLOEXEC | flags); #else #ifdef __COVERITY__ @@ -458,8 +439,6 @@ int fdevent_rename(const char *oldpath, const char *newpath) { } -#ifndef _WIN32 - pid_t fdevent_fork_execve(const char *name, char *argv[], char *envp[], int fdin, int fdout, int fderr, int dfd) { #ifdef HAVE_FORK @@ -531,8 +510,6 @@ int fdevent_waitpid_intr(pid_t pid, int * const status) { return waitpid(pid, status, 0); } -#endif /* !_WIN32 */ - #ifndef MSG_DONTWAIT #define MSG_DONTWAIT 0 @@ -560,14 +537,7 @@ ssize_t fdevent_socket_read_discard (int fd, char *buf, size_t sz, int family, i #ifdef HAVE_SYS_FILIO_H #include <sys/filio.h> /* FIONREAD (for illumos (OpenIndiana)) */ #endif -#ifdef _WIN32 -#include <winsock2.h> -#endif int fdevent_ioctl_fionread (int fd, int fdfmt, int *toread) { - #ifdef _WIN32 - if (fdfmt != S_IFSOCK) { errno = ENOTSOCK; return -1; } - return ioctlsocket(fd, FIONREAD, toread); - #else #ifdef __CYGWIN__ /*(cygwin supports FIONREAD on pipes, not sockets)*/ if (fdfmt != S_IFIFO) { errno = EOPNOTSUPP; return -1; } @@ -575,7 +545,6 @@ int fdevent_ioctl_fionread (int fd, int fdfmt, int *toread) { UNUSED(fdfmt); #endif return ioctl(fd, FIONREAD, toread); - #endif } @@ -587,8 +556,12 @@ int fdevent_connect_status(int fd) { return (0 == getsockopt(fd,SOL_SOCKET,SO_ERROR,&opt,&len)) ? opt : errno; } +#endif /* !_WIN32 */ + +#ifndef _WIN32 #include <netinet/tcp.h> +#endif #if defined(__FreeBSD__) || defined(__NetBSD__) \ || defined(__OpenBSD__) || defined(__DragonFly__) #include <netinet/tcp_fsm.h> diff --git a/src/fdevent.h b/src/fdevent.h index 1848fb6f..8c9950c2 100644 --- a/src/fdevent.h +++ b/src/fdevent.h @@ -115,6 +115,22 @@ void fdevent_win32_cleanup (void); pid_t fdevent_createprocess(char *argv[], char *envp[], intptr_t fdin, intptr_t fdout, int fderr, int dfd); #endif /* _WIN32 */ +#define fdio_close_dirfd(fd) close(fd) +#define fdio_close_file(fd) close(fd) +#define fdio_close_pipe(fd) close(fd) +#define fdio_close_socket(fd) fdevent_socket_close(fd) +#ifdef _WIN32 +int fdevent_socket_set_cloexec(int fd); +int fdevent_socket_clr_cloexec(int fd); +int fdevent_socket_set_nb(int fd); +int fdevent_socket_set_nb_cloexec(int fd); +#else +#define fdevent_socket_set_cloexec(fd) (fdevent_setfd_cloexec(fd), 0) +#define fdevent_socket_clr_cloexec(fd) (fdevent_clrfd_cloexec(fd), 0) +#define fdevent_socket_set_nb(fd) fdevent_fcntl_set_nb(fd) +#define fdevent_socket_set_nb_cloexec(fd) fdevent_fcntl_set_nb_cloexec(fd) +#endif +int fdevent_socket_close (int fd); ssize_t fdevent_socket_read_discard (int fd, char *buf, size_t sz, int family, int so_type); int fdevent_ioctl_fionread (int fd, int fdfmt, int *toread); diff --git a/src/fdevent_impl.c b/src/fdevent_impl.c index 0b1399f9..05283e55 100644 --- a/src/fdevent_impl.c +++ b/src/fdevent_impl.c @@ -172,7 +172,9 @@ fdevent_init (const char *event_handler, int *max_fds, int *cur_fds, log_error_s int type = fdevent_config(&event_handler, errh); if (type <= 0) return NULL; + #ifndef _WIN32 fdevent_socket_nb_cloexec_init(); + #endif #ifdef FDEVENT_USE_SELECT /* select limits itself diff --git a/src/fdevent_win32.c b/src/fdevent_win32.c index de3bd85a..80a2e2e0 100644 --- a/src/fdevent_win32.c +++ b/src/fdevent_win32.c @@ -240,6 +240,95 @@ int fdevent_socketpair_nb_cloexec (int domain, int typ, int protocol, int sv[2]) } +int fdevent_socket_set_cloexec (int fd) +{ + return SetHandleInformation((HANDLE)(uint64_t)fd, + HANDLE_FLAG_INHERIT, 0) ? 0 : -1; +} + + +int fdevent_socket_clr_cloexec (int fd) +{ + return SetHandleInformation((HANDLE)(uint64_t)fd, + HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT) + ? 0 + : -1; +} + + +int fdevent_socket_set_nb (int fd) +{ + u_long ul = 1; + return ioctlsocket(fd, FIONBIO, &ul); +} + + +int fdevent_socket_set_nb_cloexec (int fd) +{ + return SetHandleInformation((HANDLE)(uint64_t)fd, HANDLE_FLAG_INHERIT, 0) + ? fdevent_socket_set_nb(fd) + : -1; +} + + +int fdevent_socket_cloexec (int domain, int type, int protocol) +{ + return WSASocketA(domain, type, protocol, NULL, 0, + WSA_FLAG_OVERLAPPED | WSA_FLAG_NO_HANDLE_INHERIT); +} + + +int fdevent_socket_nb_cloexec (int domain, int type, int protocol) +{ + SOCKET fd = WSASocketA(domain, type, protocol, NULL, 0, + WSA_FLAG_OVERLAPPED | WSA_FLAG_NO_HANDLE_INHERIT); + if (fd != INVALID_SOCKET) { + if (0 != fdevent_socket_set_nb(fd)) { + closesocket(fd); + fd = INVALID_SOCKET; /* INVALID_SOCKET is (unsigned long long)-1 */ + } + } + return fd; +} + + +void fdevent_setfd_cloexec (int fd) +{ + SetHandleInformation((HANDLE)_get_osfhandle(fd), + HANDLE_FLAG_INHERIT, 0); +} + + +void fdevent_clrfd_cloexec (int fd) +{ + SetHandleInformation((HANDLE)_get_osfhandle(fd), + HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT); +} + + +int fdevent_fcntl_set_nb (int fd) +{ + DWORD mode = PIPE_READMODE_BYTE | PIPE_NOWAIT; + return SetNamedPipeHandleState((HANDLE)_get_osfhandle(fd), + &mode, NULL, NULL) ? 0 : -1; +} + + +int fdevent_fcntl_set_nb_cloexec (int fd) +{ + fdevent_setfd_cloexec(fd); + return fdevent_fcntl_set_nb(fd); +} + + +int fdevent_fcntl_set_nb_cloexec_sock (int fd) +{ + /*(should have created listening sockets non-blocking and no-inherit)*/ + UNUSED(fd); + return 0; +} + + #include <windows.h> #include <signal.h> /* sig_atomic_t */ @@ -651,6 +740,151 @@ pid_t fdevent_createprocess (char *argv[], char *envp[], intptr_t fdin, intptr_t } +int fdevent_dup_cloexec (int fd) +{ + const int newfd = _dup(fd); + if (newfd >= 0) fdevent_setfd_cloexec(newfd); + return newfd; +} + + +#include <fcntl.h> + +int fdevent_open_cloexec (const char *pathname, int symlinks, int flags, mode_t mode) +{ + UNUSED(symlinks); + return _open(pathname, flags | _O_BINARY | _O_NOINHERIT, mode); +} + + +int fdevent_open_devnull (void) +{ + return fdevent_open_cloexec("nul:", 0, O_RDWR, 0); +} + + +#if 0 /* not currently used on _WIN32 */ +int fdevent_open_dirname (char *path, int symlinks) +{ + /*(handle special cases of no dirname or dirname is root directory)*/ + char *c = strrchr(path, '/'); + char * const bs = strrchr(path, '\\'); + if (c < bs) c = bs; + const char * const dname = (NULL != c ? c == path ? "/" : path : "."); + if (NULL != c) *c = '\0'; + int dfd = fdevent_open_cloexec(dname, symlinks, _O_RDONLY, 0); + if (NULL != c) *c = (c == bs) ? '\\' : '/'; + return dfd; +} +#endif + + +int fdevent_pipe_cloexec (int * const fds, const unsigned int bufsz_hint) +{ + return _pipe(fds, bufsz_hint, _O_BINARY | _O_NOINHERIT); +} + + +int fdevent_socket_close (int fd) +{ + return closesocket(fd); +} + + +int fdevent_accept_listenfd (int listenfd, struct sockaddr *addr, size_t *addrlen) +{ + socklen_t len = (socklen_t) *addrlen; + SOCKET fd = accept(listenfd, addr, &len); + if (fd == INVALID_SOCKET) + return -1; + *addrlen = (size_t)len; + #if 0 /*WSA_FLAG_NO_HANDLE_INHERIT and non-blocking inherited from listenfd*/ + if (0 != fdevent_socket_set_nb_cloexec(fd)) { + closesocket(fd); + return -1; + } + #endif + #if 0 + if (fd > INT_MAX) + fprintf(stderr, "warning: SOCKET fd > INT_MAX (fd:%llu)", fd); + #endif + return (int)fd; +} + + +char ** fdevent_environ (void) +{ + return environ; +} + + +#include <sys/stat.h> /* _S_IREAD _S_IWRITE */ +#include <share.h> /* _SH_DENYRW */ +int fdevent_mkostemp (char *path, int flags) +{ + /* notes: + * - _O_TEMPORARY omitted since file deleted if all handles closed, + * but close may occur in chunkqueue with large request/response using + * multiple temp files + * - XXX: might convert path from UTF-8 to wide-char + */ + char *p; + for (p = path; *p; ++p) if (*p == '\\') *p = '/'; + int fd; /*(flags might have _O_APPEND)*/ + flags |= _O_RDWR | _O_CREAT | _O_EXCL | _O_BINARY | _O_NOINHERIT; + return (0 == _mktemp_s(path, (size_t)(p - path + 1))) + && (0 == _sopen_s(&fd, path, flags, _SH_DENYRW, _S_IREAD | _S_IWRITE)) + ? fd + : -1; + /* future: if _sopen_s() returns EEXIST, might reset template (path) with + * trailing "XXXXXX", and then loop to try again */ +} + + +int fdevent_rename (const char *oldpath, const char *newpath) +{ + /* MoveFileExA() vs ReplaceFileA() difference should not matter for use in + * caches, e.g. mod_deflate and mod_dirlisting, but ReplaceFileA() may be + * preferred in other places, such as mod_webdav. */ + #if 1 + return MoveFileExA(oldpath, newpath, + MOVEFILE_COPY_ALLOWED + | MOVEFILE_REPLACE_EXISTING) ? 0 : -1; + #else + return ReplaceFileA(newpath, oldpath, NULL, + REPLACEFILE_IGNORE_MERGE_ERRORS + | REPLACEFILE_IGNORE_ACL_ERRORS, NULL, NULL) ? 0 : -1; + #endif +} + + +ssize_t fdevent_socket_read_discard (int fd, char *buf, size_t sz, int family, int so_type) +{ + UNUSED(family); + UNUSED(so_type); + ssize_t rd = recv(fd, buf, sz, 0); + if (rd == SOCKET_ERROR) { + switch (WSAGetLastError()) { + case WSAEINTR: errno = EINTR; break; + case WSAEWOULDBLOCK: errno = EAGAIN; break; + default: errno = EIO; break; + } + } + return rd; +} + + +#include "sys-socket.h" /*(custom definition for S_IFSOCK)*/ +int fdevent_ioctl_fionread (int fd, int fdfmt, int *toread) +{ + if (fdfmt != S_IFSOCK) { errno = ENOTSOCK; return -1; } + u_long l; + int rc = ioctlsocket(fd, FIONREAD, &l); + if (0 == rc) *toread = (int)l; + return rc; +} + + #ifdef TEST int main (void) { diff --git a/src/gw_backend.c b/src/gw_backend.c index 2f356b53..e80a7261 100644 --- a/src/gw_backend.c +++ b/src/gw_backend.c @@ -514,7 +514,7 @@ static int gw_spawn_connection(gw_host * const host, gw_proc * const proc, log_e unlink(proc->unixsocket->ptr); } - close(gw_fd); + fdio_close_socket(gw_fd); if (-1 == status) { /* server is not up, spawn it */ @@ -522,7 +522,15 @@ static int gw_spawn_connection(gw_host * const host, gw_proc * const proc, log_e uint32_t i; /* reopen socket */ + + #ifdef _WIN32 + /* Note: not using WSA_FLAG_OVERLAPPED + * because we are assigning to hStdInput of child process */ + gw_fd = WSASocketA(proc->saddr->sa_family, SOCK_STREAM, 0, NULL, 0, + WSA_FLAG_NO_HANDLE_INHERIT); + #else gw_fd = fdevent_socket_cloexec(proc->saddr->sa_family, SOCK_STREAM, 0); + #endif if (-1 == gw_fd) { log_perror(errh, __FILE__, __LINE__, "socket()"); return -1; @@ -530,7 +538,7 @@ static int gw_spawn_connection(gw_host * const host, gw_proc * const proc, log_e if (fdevent_set_so_reuseaddr(gw_fd, 1) < 0) { log_perror(errh, __FILE__, __LINE__, "socketsockopt()"); - close(gw_fd); + fdio_close_socket(gw_fd); return -1; } @@ -538,13 +546,13 @@ static int gw_spawn_connection(gw_host * const host, gw_proc * const proc, log_e if (-1 == bind(gw_fd, proc->saddr, proc->saddrlen)) { log_perror(errh, __FILE__, __LINE__, "bind failed for: %s", proc->connection_name->ptr); - close(gw_fd); + fdio_close_socket(gw_fd); return -1; } if (-1 == listen(gw_fd, host->listen_backlog)) { log_perror(errh, __FILE__, __LINE__, "listen()"); - close(gw_fd); + fdio_close_socket(gw_fd); return -1; } @@ -627,7 +635,7 @@ static int gw_spawn_connection(gw_host * const host, gw_proc * const proc, log_e for (i = 0; i < env.used; ++i) free(env.ptr[i]); free(env.ptr); if (dfd >= 0) close(dfd); - close(gw_fd); + fdio_close_socket(gw_fd); if (-1 == proc->pid) { proc->pid = 0; @@ -7,7 +7,11 @@ #include "first.h" #include "h2.h" +#ifndef _WIN32 #include <arpa/inet.h> /* htonl() */ +#else +#include <winsock2.h> /* htonl() */ +#endif #include <stdint.h> /* INT32_MAX INT32_MIN */ #include <stdlib.h> #include <string.h> diff --git a/src/mod_authn_dbi.c b/src/mod_authn_dbi.c index f9c27772..825caaed 100644 --- a/src/mod_authn_dbi.c +++ b/src/mod_authn_dbi.c @@ -79,7 +79,8 @@ mod_authn_dbi_error_callback (dbi_conn dbconn, void *vdata) while (++dbconf->reconnect_count <= 3) { /* retry */ if (0 == dbi_conn_connect(dbconn)) { - fdevent_setfd_cloexec(dbi_conn_get_socket(dbconn)); + /* _WIN32: ok if SOCKET (unsigned long long) actually <= INT_MAX */ + (void)fdevent_socket_set_cloexec(dbi_conn_get_socket(dbconn)); return; } } diff --git a/src/mod_vhostdb_dbi.c b/src/mod_vhostdb_dbi.c index 758f442c..c7c11194 100644 --- a/src/mod_vhostdb_dbi.c +++ b/src/mod_vhostdb_dbi.c @@ -54,7 +54,8 @@ static void mod_vhostdb_dbi_error_callback (dbi_conn dbconn, void *vdata) while (++dbconf->reconnect_count <= 3) { /* retry */ if (0 == dbi_conn_connect(dbconn)) { - fdevent_setfd_cloexec(dbi_conn_get_socket(dbconn)); + /* _WIN32: ok if SOCKET (unsigned long long) actually <= INT_MAX */ + (void)fdevent_socket_set_cloexec(dbi_conn_get_socket(dbconn)); return; } } diff --git a/src/mod_vhostdb_mysql.c b/src/mod_vhostdb_mysql.c index 0cf18603..b49b0617 100644 --- a/src/mod_vhostdb_mysql.c +++ b/src/mod_vhostdb_mysql.c @@ -114,7 +114,8 @@ static int mod_vhostdb_dbconf_setup (server *srv, const array *opts, void **vdat return -1; } - fdevent_setfd_cloexec(dbconn->net.fd); + my_socket sfd = mysql_get_socket(dbconn); + (void)fdevent_socket_set_cloexec(sfd); dbconf = (vhostdb_config *)ck_calloc(1, sizeof(*dbconf)); dbconf->dbconn = dbconn; diff --git a/src/network.c b/src/network.c index 6f1def5e..5341d5b3 100644 --- a/src/network.c +++ b/src/network.c @@ -472,8 +472,8 @@ static int network_server_init(server *srv, const network_socket_config *s, buff if (-1 != stdin_fd) { srv_socket->fd = stdin_fd; - if (-1 == fdevent_fcntl_set_nb_cloexec(stdin_fd)) { - log_perror(srv->errh, __FILE__, __LINE__, "fcntl"); + if (-1 == fdevent_socket_set_nb_cloexec(stdin_fd)) { + log_perror(srv->errh, __FILE__, __LINE__, "fcntl()"); return -1; } } else @@ -504,8 +504,8 @@ static int network_server_init(server *srv, const network_socket_config *s, buff return -1; } - if (-1 == fdevent_fcntl_set_nb(srv_socket->fd)) { - log_perror(srv->errh, __FILE__, __LINE__, "fcntl"); + if (-1 == fdevent_socket_set_nb(srv_socket->fd)) { + log_perror(srv->errh, __FILE__, __LINE__, "fcntl()"); return -1; } } else @@ -609,7 +609,7 @@ int network_close(server *srv) { server_socket *srv_socket = srv->srv_sockets.ptr[i]; if (srv_socket->fd != -1) { network_unregister_sock(srv, srv_socket); - close(srv_socket->fd); + fdio_close_socket(srv_socket->fd); } buffer_free(srv_socket->srv_token); @@ -624,7 +624,7 @@ int network_close(server *srv) { for (uint32_t i = 0; i < srv->srv_sockets_inherited.used; ++i) { server_socket *srv_socket = srv->srv_sockets_inherited.ptr[i]; if (srv_socket->fd != -1 && srv_socket->sidx != (unsigned short)~0u) { - close(srv_socket->fd); + fdio_close_socket(srv_socket->fd); } buffer_free(srv_socket->srv_token); @@ -647,7 +647,7 @@ void network_socket_activation_to_env (server * const srv) { server_socket *srv_socket = srv->srv_sockets.ptr[n]; if (srv_socket->fd < fd) continue; if (srv_socket->fd == fd) { - fdevent_clrfd_cloexec(fd); + (void)fdevent_socket_clr_cloexec(fd); ++fd; continue; } @@ -657,7 +657,7 @@ void network_socket_activation_to_env (server * const srv) { break; } if (i < srv->srv_sockets.used) { - fdevent_clrfd_cloexec(fd); + (void)fdevent_socket_clr_cloexec(fd); ++fd; --n; /* loop to reprocess this entry */ continue; @@ -812,7 +812,7 @@ int network_init(server *srv, int stdin_fd) { /*assert(buffer_eq_slen(b, CONST_STR_LEN("/dev/stdin")));*/ rc = (0 == srv->srv_sockets.used) ? network_server_init(srv, &p->defaults, b, 0, stdin_fd) - : close(stdin_fd);/*(graceful restart listening to "/dev/stdin")*/ + : fdio_close_socket(stdin_fd);/*(graceful restart; "/dev/stdin")*/ buffer_free(b); if (0 != rc) break; } diff --git a/src/server.c b/src/server.c index b5cc1c33..33d96f3e 100644 --- a/src/server.c +++ b/src/server.c @@ -473,13 +473,16 @@ static void server_free(server *srv) { fdevent_unregister(srv->ev, oneshot_fdn); oneshot_fdn = NULL; } - close(oneshot_fd); + if (oneshot_fdout >= 0) + fdio_close_pipe(oneshot_fd); + else + fdio_close_socket(oneshot_fd); } if (oneshot_fdout >= 0) { - close(oneshot_fdout); + fdio_close_pipe(oneshot_fdout); } if (srv->stdin_fd >= 0) { - close(srv->stdin_fd); + fdio_close_socket(srv->stdin_fd); } buffer_free(srv->tmp_buf); @@ -714,7 +717,7 @@ static int server_oneshot_init(server *srv, int fd) { } /*(must set flags; fd did not pass through fdevent accept() logic)*/ - if (-1 == fdevent_fcntl_set_nb_cloexec(fd)) { + if (-1 == fdevent_socket_set_nb_cloexec(fd)) { log_perror(srv->errh, __FILE__, __LINE__, "fcntl()"); return 0; } @@ -965,7 +968,7 @@ static void server_sockets_close (server *srv) { server_socket *srv_socket = srv->srv_sockets.ptr[i]; if (-1 == srv_socket->fd) continue; if (2 != srv->sockets_disabled) network_unregister_sock(srv,srv_socket); - close(srv_socket->fd); + fdio_close_socket(srv_socket->fd); srv_socket->fd = -1; /* network_close() will cleanup after us */ } diff --git a/src/sys-socket.h b/src/sys-socket.h index 7851656b..1a4b9e32 100644 --- a/src/sys-socket.h +++ b/src/sys-socket.h @@ -2,14 +2,19 @@ #define WIN32_SOCKET_H #include "first.h" -#ifdef __WIN32 +#ifdef _WIN32 #include <winsock2.h> - -#define ECONNRESET WSAECONNRESET -#define EINPROGRESS WSAEINPROGRESS -#define EALREADY WSAEALREADY -#define ECONNABORTED WSAECONNABORTED +/* https://docs.microsoft.com/en-us/windows/win32/winsock/sockaddr-2 */ +#include <ws2tcpip.h> +typedef uint32_t in_addr_t; +typedef unsigned short sa_family_t; +#define recv(a,b,c,d) recv((a),(char *)(b),(c),(d)) +#define getsockopt(a,b,c,d,e) getsockopt((a),(b),(c),(char *)(d),(e)) +#define setsockopt(a,b,c,d,e) setsockopt((a),(b),(c),(char *)(d),(e)) +#ifndef S_IFSOCK /*(used by lighttpd to mark fd type)*/ +#define S_IFSOCK 0140000 +#endif #else |