From 2b4c25bd2cac034cdc52d29d6bb8c675c87a731c Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Sun, 5 Apr 2020 09:49:57 +0100 Subject: win32: new functions: getpeername(2), mingw_spawn_detach() Implement getpeername(2). Add mingw_spawn_detach() to allow the spawned process to detach from the console. --- include/mingw.h | 3 +++ win32/net.c | 12 ++++++++++++ win32/process.c | 18 +++++++++++++++--- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/include/mingw.h b/include/mingw.h index a5a84cff2..3d0daee38 100644 --- a/include/mingw.h +++ b/include/mingw.h @@ -228,6 +228,7 @@ int mingw_listen(int sockfd, int backlog); int mingw_accept(int sockfd1, struct sockaddr *sa, socklen_t *sz); int mingw_select (int nfds, fd_set *rfds, fd_set *wfds, fd_set *xfds, struct timeval *timeout); +int mingw_getpeername(int fd, struct sockaddr *sa, socklen_t *sz); NOIMPL(mingw_sendto,SOCKET s UNUSED_PARAM, const char *buf UNUSED_PARAM, int len UNUSED_PARAM, int flags UNUSED_PARAM, const struct sockaddr *sa UNUSED_PARAM, int salen UNUSED_PARAM); @@ -240,6 +241,7 @@ NOIMPL(mingw_sendto,SOCKET s UNUSED_PARAM, const char *buf UNUSED_PARAM, int len #define shutdown mingw_shutdown #define accept mingw_accept #define select mingw_select +#define getpeername mingw_getpeername /* * sys/time.h @@ -472,6 +474,7 @@ DIR *mingw_opendir(const char *path); #define is_dir_sep(c) ((c) == '/' || (c) == '\\') pid_t FAST_FUNC mingw_spawn(char **argv); +pid_t FAST_FUNC mingw_spawn_detach(char **argv); intptr_t FAST_FUNC mingw_spawn_proc(const char **argv); int mingw_execv(const char *cmd, char *const *argv); int mingw_execvp(const char *cmd, char *const *argv); diff --git a/win32/net.c b/win32/net.c index 2341119b0..01fa16a4e 100644 --- a/win32/net.c +++ b/win32/net.c @@ -99,3 +99,15 @@ int mingw_accept(int sockfd1, struct sockaddr *sa, socklen_t *sz) } return sockfd2; } + +#undef getpeername +int mingw_getpeername(int fd, struct sockaddr *sa, socklen_t *sz) +{ + SOCKET sock = (SOCKET)_get_osfhandle(fd); + + if (sock == INVALID_SOCKET) { + errno = EBADF; + return -1; + } + return getpeername(sock, sa, sz); +} diff --git a/win32/process.c b/win32/process.c index 4257fd689..67ce3c100 100644 --- a/win32/process.c +++ b/win32/process.c @@ -334,16 +334,28 @@ mingw_spawn_1(int mode, const char *cmd, char *const *argv, char *const *envp) return -1; } -pid_t FAST_FUNC -mingw_spawn(char **argv) +static pid_t +mingw_spawn_pid(int mode, char **argv) { intptr_t ret; - ret = mingw_spawn_proc((const char **)argv); + ret = mingw_spawn_1(mode, argv[0], (char *const *)argv, environ); return ret == -1 ? -1 : GetProcessId((HANDLE)ret); } +pid_t FAST_FUNC +mingw_spawn(char **argv) +{ + return mingw_spawn_pid(P_NOWAIT, argv); +} + +pid_t FAST_FUNC +mingw_spawn_detach(char **argv) +{ + return mingw_spawn_pid(P_DETACH, argv); +} + intptr_t FAST_FUNC mingw_spawn_proc(const char **argv) { -- cgit v1.2.3-55-g6feb