2020-06-15 14:18:57 +00:00
|
|
|
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
2023-12-08 03:11:56 +00:00
|
|
|
│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │
|
2020-06-15 14:18:57 +00:00
|
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
|
|
|
│ Copyright 2020 Justine Alexandra Roberts Tunney │
|
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ Permission to use, copy, modify, and/or distribute this software for │
|
|
|
|
│ any purpose with or without fee is hereby granted, provided that the │
|
|
|
|
│ above copyright notice and this permission notice appear in all copies. │
|
2020-06-15 14:18:57 +00:00
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
|
|
|
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
|
|
|
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
|
|
|
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
|
|
|
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
|
|
|
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
|
|
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
|
|
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
2020-06-15 14:18:57 +00:00
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
2024-09-13 06:01:20 +00:00
|
|
|
#include "libc/calls/internal.h"
|
|
|
|
#include "libc/calls/struct/sigset.h"
|
2023-10-13 01:53:17 +00:00
|
|
|
#include "libc/calls/struct/sigset.internal.h"
|
2024-09-15 03:32:46 +00:00
|
|
|
#include "libc/calls/syscall_support-nt.internal.h"
|
2023-10-09 18:56:21 +00:00
|
|
|
#include "libc/errno.h"
|
2024-08-25 18:26:21 +00:00
|
|
|
#include "libc/macros.h"
|
2023-10-13 01:53:17 +00:00
|
|
|
#include "libc/nt/errors.h"
|
2024-09-13 06:01:20 +00:00
|
|
|
#include "libc/nt/struct/fdset.h"
|
|
|
|
#include "libc/nt/struct/pollfd.h"
|
|
|
|
#include "libc/nt/struct/timeval.h"
|
2023-10-13 01:53:17 +00:00
|
|
|
#include "libc/nt/thunk/msabi.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/nt/winsock.h"
|
|
|
|
#include "libc/sock/internal.h"
|
2023-10-13 01:53:17 +00:00
|
|
|
#include "libc/sock/struct/sockaddr.h"
|
2022-05-23 22:06:11 +00:00
|
|
|
#include "libc/sock/syscall_fd.internal.h"
|
2024-09-13 06:01:20 +00:00
|
|
|
#include "libc/sysv/consts/fio.h"
|
2023-10-13 01:53:17 +00:00
|
|
|
#include "libc/sysv/consts/o.h"
|
2024-09-13 06:01:20 +00:00
|
|
|
#include "libc/sysv/consts/poll.h"
|
|
|
|
#include "libc/sysv/consts/so.h"
|
2024-05-17 09:45:30 +00:00
|
|
|
#include "libc/sysv/consts/sol.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/sysv/errfuns.h"
|
Make improvements
- We now serialize the file descriptor table when spawning / executing
processes on Windows. This means you can now inherit more stuff than
just standard i/o. It's needed by bash, which duplicates the console
to file descriptor #255. We also now do a better job serializing the
environment variables, so you're less likely to encounter E2BIG when
using your bash shell. We also no longer coerce environ to uppercase
- execve() on Windows now remotely controls its parent process to make
them spawn a replacement for itself. Then it'll be able to terminate
immediately once the spawn succeeds, without having to linger around
for the lifetime as a shell process for proxying the exit code. When
process worker thread running in the parent sees the child die, it's
given a handle to the new child, to replace it in the process table.
- execve() and posix_spawn() on Windows will now provide CreateProcess
an explicit handle list. This allows us to remove handle locks which
enables better fork/spawn concurrency, with seriously correct thread
safety. Other codebases like Go use the same technique. On the other
hand fork() still favors the conventional WIN32 inheritence approach
which can be a little bit messy, but is *controlled* by guaranteeing
perfectly clean slates at both the spawning and execution boundaries
- sigset_t is now 64 bits. Having it be 128 bits was a mistake because
there's no reason to use that and it's only supported by FreeBSD. By
using the system word size, signal mask manipulation on Windows goes
very fast. Furthermore @asyncsignalsafe funcs have been rewritten on
Windows to take advantage of signal masking, now that it's much more
pleasant to use.
- All the overlapped i/o code on Windows has been rewritten for pretty
good signal and cancelation safety. We're now able to ensure overlap
data structures are cleaned up so long as you don't longjmp() out of
out of a signal handler that interrupted an i/o operation. Latencies
are also improved thanks to the removal of lots of "busy wait" code.
Waits should be optimal for everything except poll(), which shall be
the last and final demon we slay in the win32 i/o horror show.
- getrusage() on Windows is now able to report RUSAGE_CHILDREN as well
as RUSAGE_SELF, thanks to aggregation in the process manager thread.
2023-10-08 12:36:18 +00:00
|
|
|
#ifdef __x86_64__
|
2023-10-13 01:53:17 +00:00
|
|
|
|
2024-09-13 06:01:20 +00:00
|
|
|
#define UNCONNECTED 0
|
|
|
|
#define CONNECTING 1
|
|
|
|
#define CONNECTED 2
|
2023-10-13 01:53:17 +00:00
|
|
|
|
2024-09-13 06:01:20 +00:00
|
|
|
__msabi extern typeof(__sys_getsockopt_nt) *const __imp_getsockopt;
|
|
|
|
__msabi extern typeof(__sys_ioctlsocket_nt) *const __imp_ioctlsocket;
|
|
|
|
__msabi extern typeof(__sys_select_nt) *const __imp_select;
|
2023-10-13 01:53:17 +00:00
|
|
|
|
2024-09-13 06:01:20 +00:00
|
|
|
textwindows static int sys_connect_nt_impl(struct Fd *f, const void *addr,
|
|
|
|
uint32_t addrsize,
|
|
|
|
sigset_t waitmask) {
|
2023-10-13 01:53:17 +00:00
|
|
|
|
2024-09-13 06:01:20 +00:00
|
|
|
// check if already connected
|
|
|
|
if (f->connecting == 2)
|
|
|
|
return eisconn();
|
2023-10-13 01:53:17 +00:00
|
|
|
|
2024-09-13 06:01:20 +00:00
|
|
|
// winsock requires bind() be called beforehand
|
2023-10-13 01:53:17 +00:00
|
|
|
if (!f->isbound) {
|
|
|
|
struct sockaddr_storage ss = {0};
|
|
|
|
ss.ss_family = ((struct sockaddr *)addr)->sa_family;
|
Apply clang-format update to repo (#1154)
Commit bc6c183 introduced a bunch of discrepancies between what files
look like in the repo and what clang-format says they should look like.
However, there were already a few discrepancies prior to that. Most of
these discrepancies seemed to be unintentional, but a few of them were
load-bearing (e.g., a #include that violated header ordering needing
something to have been #defined by a 'later' #include.)
I opted to take what I hope is a relatively smooth-brained approach: I
reverted the .clang-format change, ran clang-format on the whole repo,
reapplied the .clang-format change, reran clang-format again, and then
reverted the commit that contained the first run. Thus the full effect
of this PR should only be to apply the changed formatting rules to the
repo, and from skimming the results, this seems to be the case.
My work can be checked by applying the short, manual commits, and then
rerunning the command listed in the autogenerated commits (those whose
messages I have prefixed auto:) and seeing if your results agree.
It might be that the other diffs should be fixed at some point but I'm
leaving that aside for now.
fd '\.c(c|pp)?$' --print0| xargs -0 clang-format -i
2024-04-25 17:38:00 +00:00
|
|
|
if (sys_bind_nt(f, &ss, sizeof(ss)) == -1)
|
|
|
|
return -1;
|
2023-10-13 01:53:17 +00:00
|
|
|
}
|
|
|
|
|
2024-09-13 06:01:20 +00:00
|
|
|
if (f->connecting == UNCONNECTED) {
|
|
|
|
|
|
|
|
// make sure winsock is in non-blocking mode
|
|
|
|
uint32_t mode = 1;
|
|
|
|
if (__imp_ioctlsocket(f->handle, FIONBIO, &mode))
|
|
|
|
return __winsockerr();
|
|
|
|
|
|
|
|
// perform non-blocking connect
|
|
|
|
if (!WSAConnect(f->handle, addr, addrsize, 0, 0, 0, 0)) {
|
|
|
|
f->connecting = CONNECTED;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for errors
|
|
|
|
switch (WSAGetLastError()) {
|
|
|
|
case WSAEISCONN:
|
|
|
|
f->connecting = CONNECTED;
|
|
|
|
return eisconn();
|
|
|
|
case WSAEALREADY:
|
|
|
|
f->connecting = CONNECTING;
|
|
|
|
break;
|
|
|
|
case WSAEWOULDBLOCK:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return __winsockerr();
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle non-blocking
|
|
|
|
if (f->flags & O_NONBLOCK) {
|
|
|
|
if (f->connecting == UNCONNECTED) {
|
|
|
|
f->connecting = CONNECTING;
|
|
|
|
return einprogress();
|
|
|
|
} else {
|
|
|
|
return ealready();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
f->connecting = CONNECTING;
|
2023-10-13 01:53:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-13 06:01:20 +00:00
|
|
|
for (;;) {
|
|
|
|
|
|
|
|
// check for signals and thread cancelation
|
|
|
|
// connect() will restart if SA_RESTART is used
|
|
|
|
if (!(f->flags & O_NONBLOCK))
|
|
|
|
if (__sigcheck(waitmask, true) == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
//
|
|
|
|
// "Use select to determine the completion of the connection request
|
|
|
|
// by checking if the socket is writable."
|
|
|
|
//
|
|
|
|
// —Quoth MSDN § WSAConnect function
|
|
|
|
//
|
|
|
|
// "If a socket is processing a connect call (nonblocking), failure
|
|
|
|
// of the connect attempt is indicated in exceptfds (application
|
|
|
|
// must then call getsockopt SO_ERROR to determine the error value
|
|
|
|
// to describe why the failure occurred). This document does not
|
|
|
|
// define which other errors will be included."
|
|
|
|
//
|
|
|
|
// —Quoth MSDN § select function
|
|
|
|
//
|
|
|
|
struct NtFdSet wrfds;
|
|
|
|
struct NtFdSet exfds;
|
|
|
|
struct NtTimeval timeout;
|
|
|
|
wrfds.fd_count = 1;
|
|
|
|
wrfds.fd_array[0] = f->handle;
|
|
|
|
exfds.fd_count = 1;
|
|
|
|
exfds.fd_array[0] = f->handle;
|
|
|
|
if (f->flags & O_NONBLOCK) {
|
|
|
|
timeout.tv_sec = 0;
|
|
|
|
timeout.tv_usec = 0;
|
|
|
|
} else {
|
|
|
|
timeout.tv_sec = POLL_INTERVAL_MS / 1000;
|
|
|
|
timeout.tv_usec = POLL_INTERVAL_MS % 1000 * 1000;
|
|
|
|
}
|
|
|
|
int ready = __imp_select(1, 0, &wrfds, &exfds, &timeout);
|
|
|
|
if (ready == -1)
|
2024-05-17 09:45:30 +00:00
|
|
|
return __winsockerr();
|
2024-09-13 06:01:20 +00:00
|
|
|
|
|
|
|
// check if we still need more time
|
|
|
|
if (!ready) {
|
|
|
|
if (f->flags & O_NONBLOCK) {
|
2024-09-15 07:03:48 +00:00
|
|
|
return etimedout();
|
2024-09-13 06:01:20 +00:00
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
2024-05-17 09:45:30 +00:00
|
|
|
}
|
2024-09-13 06:01:20 +00:00
|
|
|
|
|
|
|
// check if connect failed
|
|
|
|
if (exfds.fd_count) {
|
|
|
|
int err;
|
|
|
|
uint32_t len = sizeof(err);
|
|
|
|
if (__imp_getsockopt(f->handle, SOL_SOCKET, SO_ERROR, &err, &len) == -1)
|
|
|
|
return __winsockerr();
|
|
|
|
if (!err)
|
|
|
|
return eio(); // should be impossible
|
|
|
|
errno = __dos2errno(err);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle successful connection
|
|
|
|
if (!wrfds.fd_count)
|
|
|
|
return eio(); // should be impossible
|
|
|
|
f->connecting = CONNECTED;
|
2024-05-17 09:45:30 +00:00
|
|
|
return 0;
|
Make improvements
- We now serialize the file descriptor table when spawning / executing
processes on Windows. This means you can now inherit more stuff than
just standard i/o. It's needed by bash, which duplicates the console
to file descriptor #255. We also now do a better job serializing the
environment variables, so you're less likely to encounter E2BIG when
using your bash shell. We also no longer coerce environ to uppercase
- execve() on Windows now remotely controls its parent process to make
them spawn a replacement for itself. Then it'll be able to terminate
immediately once the spawn succeeds, without having to linger around
for the lifetime as a shell process for proxying the exit code. When
process worker thread running in the parent sees the child die, it's
given a handle to the new child, to replace it in the process table.
- execve() and posix_spawn() on Windows will now provide CreateProcess
an explicit handle list. This allows us to remove handle locks which
enables better fork/spawn concurrency, with seriously correct thread
safety. Other codebases like Go use the same technique. On the other
hand fork() still favors the conventional WIN32 inheritence approach
which can be a little bit messy, but is *controlled* by guaranteeing
perfectly clean slates at both the spawning and execution boundaries
- sigset_t is now 64 bits. Having it be 128 bits was a mistake because
there's no reason to use that and it's only supported by FreeBSD. By
using the system word size, signal mask manipulation on Windows goes
very fast. Furthermore @asyncsignalsafe funcs have been rewritten on
Windows to take advantage of signal masking, now that it's much more
pleasant to use.
- All the overlapped i/o code on Windows has been rewritten for pretty
good signal and cancelation safety. We're now able to ensure overlap
data structures are cleaned up so long as you don't longjmp() out of
out of a signal handler that interrupted an i/o operation. Latencies
are also improved thanks to the removal of lots of "busy wait" code.
Waits should be optimal for everything except poll(), which shall be
the last and final demon we slay in the win32 i/o horror show.
- getrusage() on Windows is now able to report RUSAGE_CHILDREN as well
as RUSAGE_SELF, thanks to aggregation in the process manager thread.
2023-10-08 12:36:18 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-15 14:18:57 +00:00
|
|
|
|
2023-10-13 01:53:17 +00:00
|
|
|
textwindows int sys_connect_nt(struct Fd *f, const void *addr,
|
2021-02-04 03:35:29 +00:00
|
|
|
uint32_t addrsize) {
|
2024-09-13 06:01:20 +00:00
|
|
|
int rc;
|
|
|
|
BLOCK_SIGNALS;
|
|
|
|
rc = sys_connect_nt_impl(f, addr, addrsize, _SigMask);
|
|
|
|
ALLOW_SIGNALS;
|
2023-10-13 01:53:17 +00:00
|
|
|
return rc;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
Make improvements
- We now serialize the file descriptor table when spawning / executing
processes on Windows. This means you can now inherit more stuff than
just standard i/o. It's needed by bash, which duplicates the console
to file descriptor #255. We also now do a better job serializing the
environment variables, so you're less likely to encounter E2BIG when
using your bash shell. We also no longer coerce environ to uppercase
- execve() on Windows now remotely controls its parent process to make
them spawn a replacement for itself. Then it'll be able to terminate
immediately once the spawn succeeds, without having to linger around
for the lifetime as a shell process for proxying the exit code. When
process worker thread running in the parent sees the child die, it's
given a handle to the new child, to replace it in the process table.
- execve() and posix_spawn() on Windows will now provide CreateProcess
an explicit handle list. This allows us to remove handle locks which
enables better fork/spawn concurrency, with seriously correct thread
safety. Other codebases like Go use the same technique. On the other
hand fork() still favors the conventional WIN32 inheritence approach
which can be a little bit messy, but is *controlled* by guaranteeing
perfectly clean slates at both the spawning and execution boundaries
- sigset_t is now 64 bits. Having it be 128 bits was a mistake because
there's no reason to use that and it's only supported by FreeBSD. By
using the system word size, signal mask manipulation on Windows goes
very fast. Furthermore @asyncsignalsafe funcs have been rewritten on
Windows to take advantage of signal masking, now that it's much more
pleasant to use.
- All the overlapped i/o code on Windows has been rewritten for pretty
good signal and cancelation safety. We're now able to ensure overlap
data structures are cleaned up so long as you don't longjmp() out of
out of a signal handler that interrupted an i/o operation. Latencies
are also improved thanks to the removal of lots of "busy wait" code.
Waits should be optimal for everything except poll(), which shall be
the last and final demon we slay in the win32 i/o horror show.
- getrusage() on Windows is now able to report RUSAGE_CHILDREN as well
as RUSAGE_SELF, thanks to aggregation in the process manager thread.
2023-10-08 12:36:18 +00:00
|
|
|
|
|
|
|
#endif /* __x86_64__ */
|