mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-07 03:38:31 +00:00
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
This commit is contained in:
parent
342d0c81e5
commit
6e6fc38935
863 changed files with 9201 additions and 4627 deletions
|
@ -126,8 +126,10 @@ textwindows int sys_accept_nt(struct Fd *f, struct sockaddr_storage *addr,
|
|||
// create file descriptor for new socket
|
||||
// don't inherit the file open mode bits
|
||||
int oflags = 0;
|
||||
if (accept4_flags & SOCK_CLOEXEC) oflags |= O_CLOEXEC;
|
||||
if (accept4_flags & SOCK_NONBLOCK) oflags |= O_NONBLOCK;
|
||||
if (accept4_flags & SOCK_CLOEXEC)
|
||||
oflags |= O_CLOEXEC;
|
||||
if (accept4_flags & SOCK_NONBLOCK)
|
||||
oflags |= O_NONBLOCK;
|
||||
client = __reservefd(-1);
|
||||
g_fds.p[client].flags = oflags;
|
||||
g_fds.p[client].mode = 0140666;
|
||||
|
|
|
@ -37,7 +37,8 @@ int sys_accept4(int server, struct sockaddr_storage *addr, int flags) {
|
|||
if (client == -1 && errno == ENOSYS) {
|
||||
// XNU/RHEL5/etc. don't support accept4(), but it's easilly polyfilled
|
||||
errno = olderr;
|
||||
if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK)) return einval();
|
||||
if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
|
||||
return einval();
|
||||
if ((client = __sys_accept(server, addr, &size, 0)) != -1) {
|
||||
// __sys_accept() has inconsistent flag inheritence across platforms
|
||||
// this is one of the issues that accept4() was invented for solving
|
||||
|
|
|
@ -22,12 +22,15 @@
|
|||
#if IsAsan()
|
||||
|
||||
bool __asan_is_valid_msghdr(const struct msghdr *msg) {
|
||||
if (!__asan_is_valid(msg, sizeof(struct msghdr))) return false;
|
||||
if (!__asan_is_valid(msg, sizeof(struct msghdr)))
|
||||
return false;
|
||||
if (msg->msg_name) {
|
||||
if (!__asan_is_valid(msg->msg_name, msg->msg_namelen)) return false;
|
||||
if (!__asan_is_valid(msg->msg_name, msg->msg_namelen))
|
||||
return false;
|
||||
}
|
||||
if (msg->msg_control) {
|
||||
if (!__asan_is_valid(msg->msg_control, msg->msg_controllen)) return false;
|
||||
if (!__asan_is_valid(msg->msg_control, msg->msg_controllen))
|
||||
return false;
|
||||
}
|
||||
return __asan_is_valid_iov(msg->msg_iov, msg->msg_iovlen);
|
||||
}
|
||||
|
|
|
@ -37,8 +37,10 @@ textwindows int64_t GetNtBaseSocket(int64_t socket) {
|
|||
int64_t base_socket;
|
||||
for (;;) {
|
||||
base_socket = GetNtBspSocket(socket, kNtSioBaseHandle);
|
||||
if (base_socket != -1) return base_socket;
|
||||
if (WSAGetLastError() == WSAENOTSOCK) return __winsockerr();
|
||||
if (base_socket != -1)
|
||||
return base_socket;
|
||||
if (WSAGetLastError() == WSAENOTSOCK)
|
||||
return __winsockerr();
|
||||
/*
|
||||
* Even though Microsoft documentation clearly states that Layered
|
||||
* Spyware Providers must never ever intercept the SIO_BASE_HANDLE
|
||||
|
|
|
@ -63,7 +63,8 @@ void sys_connect_nt_cleanup(struct Fd *f, bool cancel) {
|
|||
struct NtOverlapped *overlap;
|
||||
if ((overlap = f->connect_op)) {
|
||||
uint32_t got, flags;
|
||||
if (cancel) CancelIoEx(f->handle, overlap);
|
||||
if (cancel)
|
||||
CancelIoEx(f->handle, overlap);
|
||||
if (WSAGetOverlappedResult(f->handle, overlap, &got, cancel, &flags) ||
|
||||
WSAGetLastError() != kNtErrorIoIncomplete) {
|
||||
WSACloseEvent(overlap->hEvent);
|
||||
|
@ -92,13 +93,15 @@ static textwindows int sys_connect_nt_impl(struct Fd *f, const void *addr,
|
|||
cosmo_once(&g_connectex.once, connectex_init);
|
||||
|
||||
// fail if previous connect() is still in progress
|
||||
if (f->connect_op) return ealready();
|
||||
if (f->connect_op)
|
||||
return ealready();
|
||||
|
||||
// ConnectEx() requires bind() be called beforehand
|
||||
if (!f->isbound) {
|
||||
struct sockaddr_storage ss = {0};
|
||||
ss.ss_family = ((struct sockaddr *)addr)->sa_family;
|
||||
if (sys_bind_nt(f, &ss, sizeof(ss)) == -1) return -1;
|
||||
if (sys_bind_nt(f, &ss, sizeof(ss)) == -1)
|
||||
return -1;
|
||||
}
|
||||
|
||||
// perform normal connect
|
||||
|
@ -119,7 +122,8 @@ static textwindows int sys_connect_nt_impl(struct Fd *f, const void *addr,
|
|||
// 2. poll(POLLOUT)
|
||||
bool32 ok;
|
||||
struct NtOverlapped *overlap = calloc(1, sizeof(struct NtOverlapped));
|
||||
if (!overlap) return -1;
|
||||
if (!overlap)
|
||||
return -1;
|
||||
overlap->hEvent = WSACreateEvent();
|
||||
ok = g_connectex.lpConnectEx(f->handle, addr, addrsize, 0, 0, 0, overlap);
|
||||
if (ok) {
|
||||
|
|
|
@ -144,7 +144,8 @@ https://github.com/piscisaureus/wepoll");
|
|||
q->parent = parent; \
|
||||
p->parent = q; \
|
||||
p->trans = q->cis; \
|
||||
if (p->trans) p->trans->parent = p; \
|
||||
if (p->trans) \
|
||||
p->trans->parent = p; \
|
||||
q->cis = p;
|
||||
|
||||
#define TREE__INSERT_OR_DESCEND(side) \
|
||||
|
@ -402,7 +403,8 @@ static textwindows int afd_cancel_poll(
|
|||
struct NtIoStatusBlock cancel_iosb;
|
||||
/* If the poll operation has already completed or has been cancelled
|
||||
earlier, there's nothing left for us to do. */
|
||||
if (io_status_block->Status != kNtStatusPending) return 0;
|
||||
if (io_status_block->Status != kNtStatusPending)
|
||||
return 0;
|
||||
cancel_status =
|
||||
NtCancelIoFileEx(afd_device_handle, io_status_block, &cancel_iosb);
|
||||
/* NtCancelIoFileEx() may return STATUS_NOT_FOUND if the operation completed
|
||||
|
@ -479,20 +481,23 @@ static textwindows void queue_remove(struct QueueNode *node) {
|
|||
|
||||
static textwindows struct PortState *port__alloc(void) {
|
||||
struct PortState *port_state = malloc(sizeof *port_state);
|
||||
if (!port_state) RETURN_SET_ERROR(NULL, kNtErrorNotEnoughMemory);
|
||||
if (!port_state)
|
||||
RETURN_SET_ERROR(NULL, kNtErrorNotEnoughMemory);
|
||||
return port_state;
|
||||
}
|
||||
|
||||
static textwindows int64_t port__create_iocp(void) {
|
||||
int64_t iocp_handle = CreateIoCompletionPort(kNtInvalidHandleValue, 0, 0, 0);
|
||||
if (!iocp_handle) RETURN_MAP_ERROR(0);
|
||||
if (!iocp_handle)
|
||||
RETURN_MAP_ERROR(0);
|
||||
return iocp_handle;
|
||||
}
|
||||
|
||||
static textwindows int port__close_iocp(struct PortState *port_state) {
|
||||
int64_t iocp_handle = port_state->iocp_handle;
|
||||
port_state->iocp_handle = 0;
|
||||
if (!CloseHandle(iocp_handle)) RETURN_MAP_ERROR(-1);
|
||||
if (!CloseHandle(iocp_handle))
|
||||
RETURN_MAP_ERROR(-1);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -569,9 +574,11 @@ static textwindows struct PortState *port_new(int64_t *iocp_handle_out) {
|
|||
struct PortState *port_state;
|
||||
int64_t iocp_handle;
|
||||
port_state = port__alloc();
|
||||
if (!port_state) goto err1;
|
||||
if (!port_state)
|
||||
goto err1;
|
||||
iocp_handle = port__create_iocp();
|
||||
if (!iocp_handle) goto err2;
|
||||
if (!iocp_handle)
|
||||
goto err2;
|
||||
bzero(port_state, sizeof *port_state);
|
||||
port_state->iocp_handle = iocp_handle;
|
||||
tree_init(&port_state->sock_tree);
|
||||
|
@ -601,7 +608,8 @@ static textwindows int sock__cancel_poll(struct SockState *sock_state) {
|
|||
|
||||
static textwindows void port_cancel_socket_update(
|
||||
struct PortState *port_state, struct SockState *sock_state) {
|
||||
if (!queue_is_enqueued(sock_state_to_queue_node(sock_state))) return;
|
||||
if (!queue_is_enqueued(sock_state_to_queue_node(sock_state)))
|
||||
return;
|
||||
queue_remove(sock_state_to_queue_node(sock_state));
|
||||
}
|
||||
|
||||
|
@ -623,7 +631,8 @@ static textwindows struct TreeNode *tree_find(const struct Tree *tree,
|
|||
static textwindows struct TsTreeNode *ts_tree__find_node(struct TsTree *ts_tree,
|
||||
uintptr_t key) {
|
||||
struct TreeNode *tree_node = tree_find(&ts_tree->tree, key);
|
||||
if (!tree_node) return NULL;
|
||||
if (!tree_node)
|
||||
return NULL;
|
||||
return CONTAINOF(tree_node, struct TsTreeNode, tree_node);
|
||||
}
|
||||
|
||||
|
@ -639,7 +648,8 @@ static textwindows void tree_del(struct Tree *tree, struct TreeNode *node) {
|
|||
next = left;
|
||||
} else {
|
||||
next = right;
|
||||
while (next->left) next = next->left;
|
||||
while (next->left)
|
||||
next = next->left;
|
||||
}
|
||||
if (parent) {
|
||||
if (parent->left == node) {
|
||||
|
@ -671,14 +681,17 @@ static textwindows void tree_del(struct Tree *tree, struct TreeNode *node) {
|
|||
red = node->red;
|
||||
node = next;
|
||||
}
|
||||
if (node) node->parent = parent;
|
||||
if (red) return;
|
||||
if (node)
|
||||
node->parent = parent;
|
||||
if (red)
|
||||
return;
|
||||
if (node && node->red) {
|
||||
node->red = false;
|
||||
return;
|
||||
}
|
||||
do {
|
||||
if (node == tree->root) break;
|
||||
if (node == tree->root)
|
||||
break;
|
||||
if (node == parent->left) {
|
||||
TREE__REBALANCE_AFTER_REMOVE(left, right)
|
||||
} else {
|
||||
|
@ -687,19 +700,22 @@ static textwindows void tree_del(struct Tree *tree, struct TreeNode *node) {
|
|||
node = parent;
|
||||
parent = parent->parent;
|
||||
} while (!node->red);
|
||||
if (node) node->red = false;
|
||||
if (node)
|
||||
node->red = false;
|
||||
}
|
||||
|
||||
static textwindows void reflock__signal_event(void *address) {
|
||||
NtStatus status =
|
||||
NtReleaseKeyedEvent(reflock__keyed_event, address, false, NULL);
|
||||
if (status != kNtStatusSuccess) abort();
|
||||
if (status != kNtStatusSuccess)
|
||||
abort();
|
||||
}
|
||||
|
||||
static textwindows void reflock__await_event(void *address) {
|
||||
NtStatus status =
|
||||
NtWaitForKeyedEvent(reflock__keyed_event, address, false, NULL);
|
||||
if (status != kNtStatusSuccess) abort();
|
||||
if (status != kNtStatusSuccess)
|
||||
abort();
|
||||
}
|
||||
|
||||
static textwindows void reflock_ref(struct RefLock *reflock) {
|
||||
|
@ -712,7 +728,8 @@ static textwindows void reflock_unref(struct RefLock *reflock) {
|
|||
long state = InterlockedAdd(&reflock->state, -REFLOCK__REF);
|
||||
/* Verify that the lock was referenced and not already destroyed.*/
|
||||
npassert((state & REFLOCK__DESTROY_MASK & ~REFLOCK__DESTROY) == 0);
|
||||
if (state == REFLOCK__DESTROY) reflock__signal_event(reflock);
|
||||
if (state == REFLOCK__DESTROY)
|
||||
reflock__signal_event(reflock);
|
||||
}
|
||||
|
||||
static textwindows struct TsTreeNode *ts_tree_del_and_ref(
|
||||
|
@ -733,7 +750,8 @@ static textwindows struct TsTreeNode *ts_tree_find_and_ref(
|
|||
struct TsTreeNode *ts_tree_node;
|
||||
AcquireSRWLockShared(&ts_tree->lock);
|
||||
ts_tree_node = ts_tree__find_node(ts_tree, key);
|
||||
if (ts_tree_node != NULL) reflock_ref(&ts_tree_node->reflock);
|
||||
if (ts_tree_node != NULL)
|
||||
reflock_ref(&ts_tree_node->reflock);
|
||||
ReleaseSRWLockShared(&ts_tree->lock);
|
||||
return ts_tree_node;
|
||||
}
|
||||
|
@ -748,7 +766,8 @@ static textwindows void reflock_unref_and_destroy(struct RefLock *reflock) {
|
|||
ref_count = state & REFLOCK__REF_MASK;
|
||||
/* Verify that the lock was referenced and not already destroyed. */
|
||||
npassert((state & REFLOCK__DESTROY_MASK) == REFLOCK__DESTROY);
|
||||
if (ref_count != 0) reflock__await_event(reflock);
|
||||
if (ref_count != 0)
|
||||
reflock__await_event(reflock);
|
||||
state = InterlockedExchange(&reflock->state, REFLOCK__POISON);
|
||||
npassert(state == REFLOCK__DESTROY);
|
||||
}
|
||||
|
@ -765,7 +784,8 @@ static textwindows void port_unregister_socket(struct PortState *port_state,
|
|||
|
||||
static textwindows void port_remove_deleted_socket(
|
||||
struct PortState *port_state, struct SockState *sock_state) {
|
||||
if (!queue_is_enqueued(sock_state_to_queue_node(sock_state))) return;
|
||||
if (!queue_is_enqueued(sock_state_to_queue_node(sock_state)))
|
||||
return;
|
||||
queue_remove(sock_state_to_queue_node(sock_state));
|
||||
}
|
||||
|
||||
|
@ -790,7 +810,8 @@ static textwindows void sock__free(struct SockState *sock_state) {
|
|||
|
||||
static textwindows void port_add_deleted_socket(struct PortState *port_state,
|
||||
struct SockState *sock_state) {
|
||||
if (queue_is_enqueued(sock_state_to_queue_node(sock_state))) return;
|
||||
if (queue_is_enqueued(sock_state_to_queue_node(sock_state)))
|
||||
return;
|
||||
queue_append(&port_state->sock_deleted_queue,
|
||||
sock_state_to_queue_node(sock_state));
|
||||
}
|
||||
|
@ -871,7 +892,8 @@ static textwindows struct PollGroup *poll_group__new(
|
|||
int64_t iocp_handle = port_get_iocp_handle(port_state);
|
||||
struct Queue *poll_group_queue = port_get_poll_group_queue(port_state);
|
||||
struct PollGroup *poll_group = malloc(sizeof *poll_group);
|
||||
if (!poll_group) RETURN_SET_ERROR(NULL, kNtErrorNotEnoughMemory);
|
||||
if (!poll_group)
|
||||
RETURN_SET_ERROR(NULL, kNtErrorNotEnoughMemory);
|
||||
bzero(poll_group, sizeof *poll_group);
|
||||
queue_node_init(&poll_group->queue_node);
|
||||
poll_group->port_state = port_state;
|
||||
|
@ -893,7 +915,8 @@ static textwindows struct PollGroup *poll_group_acquire(
|
|||
: NULL;
|
||||
if (!poll_group || poll_group->group_size >= MAX_GROUP_SIZE)
|
||||
poll_group = poll_group__new(port_state);
|
||||
if (!poll_group) return NULL;
|
||||
if (!poll_group)
|
||||
return NULL;
|
||||
if (++poll_group->group_size == MAX_GROUP_SIZE)
|
||||
queue_move_to_start(poll_group_queue, &poll_group->queue_node);
|
||||
return poll_group;
|
||||
|
@ -911,22 +934,33 @@ static textwindows uint32_t sock__epoll_events_to_afd_events(uint32_t e) {
|
|||
/* Always monitor for kNtAfdPollLocalClose, which is triggered when
|
||||
the socket is closed with closesocket() or CloseHandle(). */
|
||||
uint32_t a = kNtAfdPollLocalClose;
|
||||
if (e & (EPOLLIN | EPOLLRDNORM)) a |= kNtAfdPollReceive | kNtAfdPollAccept;
|
||||
if (e & (EPOLLPRI | EPOLLRDBAND)) a |= kNtAfdPollReceiveExpedited;
|
||||
if (e & (EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND)) a |= kNtAfdPollSend;
|
||||
if (e & (EPOLLIN | EPOLLRDNORM | EPOLLRDHUP)) a |= kNtAfdPollDisconnect;
|
||||
if (e & EPOLLHUP) a |= kNtAfdPollAbort;
|
||||
if (e & EPOLLERR) a |= kNtAfdPollConnectFail;
|
||||
if (e & (EPOLLIN | EPOLLRDNORM))
|
||||
a |= kNtAfdPollReceive | kNtAfdPollAccept;
|
||||
if (e & (EPOLLPRI | EPOLLRDBAND))
|
||||
a |= kNtAfdPollReceiveExpedited;
|
||||
if (e & (EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND))
|
||||
a |= kNtAfdPollSend;
|
||||
if (e & (EPOLLIN | EPOLLRDNORM | EPOLLRDHUP))
|
||||
a |= kNtAfdPollDisconnect;
|
||||
if (e & EPOLLHUP)
|
||||
a |= kNtAfdPollAbort;
|
||||
if (e & EPOLLERR)
|
||||
a |= kNtAfdPollConnectFail;
|
||||
return a;
|
||||
}
|
||||
|
||||
static textwindows uint32_t sock__afd_events_to_epoll_events(uint32_t a) {
|
||||
uint32_t e = 0;
|
||||
if (a & (kNtAfdPollReceive | kNtAfdPollAccept)) e |= EPOLLIN | EPOLLRDNORM;
|
||||
if (a & kNtAfdPollReceiveExpedited) e |= EPOLLPRI | EPOLLRDBAND;
|
||||
if (a & kNtAfdPollSend) e |= EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND;
|
||||
if (a & kNtAfdPollDisconnect) e |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
|
||||
if (a & kNtAfdPollAbort) e |= EPOLLHUP;
|
||||
if (a & (kNtAfdPollReceive | kNtAfdPollAccept))
|
||||
e |= EPOLLIN | EPOLLRDNORM;
|
||||
if (a & kNtAfdPollReceiveExpedited)
|
||||
e |= EPOLLPRI | EPOLLRDBAND;
|
||||
if (a & kNtAfdPollSend)
|
||||
e |= EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND;
|
||||
if (a & kNtAfdPollDisconnect)
|
||||
e |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
|
||||
if (a & kNtAfdPollAbort)
|
||||
e |= EPOLLHUP;
|
||||
if (a & kNtAfdPollConnectFail) {
|
||||
/* Linux reports all these events after connect() has failed. */
|
||||
e |= EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLRDNORM | EPOLLWRNORM | EPOLLRDHUP;
|
||||
|
@ -950,7 +984,8 @@ static textwindows int sock_update(struct PortState *port_state,
|
|||
the pending *poll operation; when we receive it's completion
|
||||
package, a new poll *operation will be submitted with the correct
|
||||
event mask. */
|
||||
if (sock__cancel_poll(sock_state) < 0) return -1;
|
||||
if (sock__cancel_poll(sock_state) < 0)
|
||||
return -1;
|
||||
} else if (sock_state->poll_status == kPollCancelled) {
|
||||
/* The poll operation has already been cancelled, we're still waiting for
|
||||
it to return.For now, there' s nothing that needs to be done. */
|
||||
|
@ -995,7 +1030,8 @@ static textwindows int port__update_events(struct PortState *port_state) {
|
|||
while (!queue_is_empty(sock_update_queue)) {
|
||||
queue_node = queue_first(sock_update_queue);
|
||||
sock_state = sock_state_from_queue_node(queue_node);
|
||||
if (sock_update(port_state, sock_state) < 0) return -1;
|
||||
if (sock_update(port_state, sock_state) < 0)
|
||||
return -1;
|
||||
/* sock_update() removes the socket from the update queue.*/
|
||||
}
|
||||
return 0;
|
||||
|
@ -1003,12 +1039,14 @@ static textwindows int port__update_events(struct PortState *port_state) {
|
|||
|
||||
static textwindows void port__update_events_if_polling(
|
||||
struct PortState *port_state) {
|
||||
if (port_state->active_poll_count > 0) port__update_events(port_state);
|
||||
if (port_state->active_poll_count > 0)
|
||||
port__update_events(port_state);
|
||||
}
|
||||
|
||||
static textwindows void port_request_socket_update(
|
||||
struct PortState *port_state, struct SockState *sock_state) {
|
||||
if (queue_is_enqueued(sock_state_to_queue_node(sock_state))) return;
|
||||
if (queue_is_enqueued(sock_state_to_queue_node(sock_state)))
|
||||
return;
|
||||
queue_append(&port_state->sock_update_queue,
|
||||
sock_state_to_queue_node(sock_state));
|
||||
}
|
||||
|
@ -1047,7 +1085,8 @@ static textwindows int sock_feed_event(struct PortState *port_state,
|
|||
/* Filter out events that the user didn't ask for. */
|
||||
epoll_events &= sock_state->user_events;
|
||||
/* Return if there are no epoll events to report.*/
|
||||
if (epoll_events == 0) return 0;
|
||||
if (epoll_events == 0)
|
||||
return 0;
|
||||
/* If the the socket has the EPOLLONESHOT flag set, unmonitor all
|
||||
events, even EPOLLERR and EPOLLHUP. But always keep looking for
|
||||
closed sockets. */
|
||||
|
@ -1082,14 +1121,16 @@ static textwindows int port__poll(struct PortState *port_state,
|
|||
uint32_t maxevents, uint32_t timeout) {
|
||||
bool32 r;
|
||||
uint32_t completion_count;
|
||||
if (port__update_events(port_state) < 0) return -1;
|
||||
if (port__update_events(port_state) < 0)
|
||||
return -1;
|
||||
port_state->active_poll_count++;
|
||||
LeaveCriticalSection(&port_state->lock);
|
||||
r = GetQueuedCompletionStatusEx(port_state->iocp_handle, iocp_events,
|
||||
maxevents, &completion_count, timeout, false);
|
||||
EnterCriticalSection(&port_state->lock);
|
||||
port_state->active_poll_count--;
|
||||
if (!r) RETURN_MAP_ERROR(-1);
|
||||
if (!r)
|
||||
RETURN_MAP_ERROR(-1);
|
||||
return port__feed_events(port_state, epoll_events, iocp_events,
|
||||
completion_count);
|
||||
}
|
||||
|
@ -1103,7 +1144,8 @@ static textwindows int port_wait(struct PortState *port_state,
|
|||
struct NtOverlappedEntry *iocp_events;
|
||||
struct NtOverlappedEntry stack_iocp_events[64];
|
||||
/* Check whether `maxevents` is in range.*/
|
||||
if (maxevents <= 0) RETURN_SET_ERROR(-1, kNtErrorInvalidParameter);
|
||||
if (maxevents <= 0)
|
||||
RETURN_SET_ERROR(-1, kNtErrorInvalidParameter);
|
||||
/* Decide whether the IOCP completion list can live on the stack, or
|
||||
allocate memory for it on the heap. */
|
||||
if ((size_t)maxevents <= ARRAYLEN(stack_iocp_events)) {
|
||||
|
@ -1129,9 +1171,11 @@ static textwindows int port_wait(struct PortState *port_state,
|
|||
for (;;) {
|
||||
result = port__poll(port_state, events, iocp_events, (uint32_t)maxevents,
|
||||
gqcs_timeout);
|
||||
if (result < 0 || result > 0) break;
|
||||
if (result < 0 || result > 0)
|
||||
break;
|
||||
/* Result, error, or time - out. */
|
||||
if (timeout < 0) continue;
|
||||
if (timeout < 0)
|
||||
continue;
|
||||
/* When timeout is negative, never time out. */
|
||||
/* Update time. */
|
||||
now = GetTickCount64();
|
||||
|
@ -1204,7 +1248,8 @@ static textwindows int64_t ws_get_base_socket(int64_t socket) {
|
|||
|
||||
static textwindows struct SockState *sock__alloc(void) {
|
||||
struct SockState *sock_state = malloc(sizeof *sock_state);
|
||||
if (!sock_state) RETURN_SET_ERROR(NULL, kNtErrorNotEnoughMemory);
|
||||
if (!sock_state)
|
||||
RETURN_SET_ERROR(NULL, kNtErrorNotEnoughMemory);
|
||||
return sock_state;
|
||||
}
|
||||
|
||||
|
@ -1223,19 +1268,24 @@ static textwindows struct SockState *sock_new(struct PortState *port_state,
|
|||
int64_t base_socket;
|
||||
struct PollGroup *poll_group;
|
||||
struct SockState *sock_state;
|
||||
if (socket == 0 || socket == -1) RETURN_SET_ERROR(0, kNtErrorInvalidHandle);
|
||||
if (socket == 0 || socket == -1)
|
||||
RETURN_SET_ERROR(0, kNtErrorInvalidHandle);
|
||||
base_socket = ws_get_base_socket(socket);
|
||||
if (base_socket == -1) return NULL;
|
||||
if (base_socket == -1)
|
||||
return NULL;
|
||||
poll_group = poll_group_acquire(port_state);
|
||||
if (!poll_group) return NULL;
|
||||
if (!poll_group)
|
||||
return NULL;
|
||||
sock_state = sock__alloc();
|
||||
if (!sock_state) goto err1;
|
||||
if (!sock_state)
|
||||
goto err1;
|
||||
bzero(sock_state, sizeof *sock_state);
|
||||
sock_state->base_socket = base_socket;
|
||||
sock_state->poll_group = poll_group;
|
||||
tree_node_init(&sock_state->tree_node);
|
||||
queue_node_init(&sock_state->queue_node);
|
||||
if (port_register_socket(port_state, sock_state, socket) < 0) goto err2;
|
||||
if (port_register_socket(port_state, sock_state, socket) < 0)
|
||||
goto err2;
|
||||
return sock_state;
|
||||
err2:
|
||||
sock__free(sock_state);
|
||||
|
@ -1262,7 +1312,8 @@ static textwindows int sock_set_event(struct PortState *port_state,
|
|||
static textwindows int port__ctl_add(struct PortState *port_state, int64_t sock,
|
||||
struct epoll_event *ev) {
|
||||
struct SockState *sock_state = sock_new(port_state, sock);
|
||||
if (!sock_state) return -1;
|
||||
if (!sock_state)
|
||||
return -1;
|
||||
if (sock_set_event(port_state, sock_state, ev) < 0) {
|
||||
sock_delete(port_state, sock_state);
|
||||
return -1;
|
||||
|
@ -1274,15 +1325,18 @@ static textwindows int port__ctl_add(struct PortState *port_state, int64_t sock,
|
|||
static textwindows struct SockState *port_find_socket(
|
||||
struct PortState *port_state, int64_t socket) {
|
||||
struct TreeNode *tree_node = tree_find(&port_state->sock_tree, socket);
|
||||
if (!tree_node) RETURN_SET_ERROR(NULL, kNtErrorNotFound);
|
||||
if (!tree_node)
|
||||
RETURN_SET_ERROR(NULL, kNtErrorNotFound);
|
||||
return sock_state_from_tree_node(tree_node);
|
||||
}
|
||||
|
||||
static textwindows int port__ctl_mod(struct PortState *port_state, int64_t sock,
|
||||
struct epoll_event *ev) {
|
||||
struct SockState *sock_state = port_find_socket(port_state, sock);
|
||||
if (!sock_state) return -1;
|
||||
if (sock_set_event(port_state, sock_state, ev) < 0) return -1;
|
||||
if (!sock_state)
|
||||
return -1;
|
||||
if (sock_set_event(port_state, sock_state, ev) < 0)
|
||||
return -1;
|
||||
port__update_events_if_polling(port_state);
|
||||
return 0;
|
||||
}
|
||||
|
@ -1290,7 +1344,8 @@ static textwindows int port__ctl_mod(struct PortState *port_state, int64_t sock,
|
|||
static textwindows int port__ctl_del(struct PortState *port_state,
|
||||
int64_t sock) {
|
||||
struct SockState *sock_state = port_find_socket(port_state, sock);
|
||||
if (!sock_state) return -1;
|
||||
if (!sock_state)
|
||||
return -1;
|
||||
sock_delete(port_state, sock_state);
|
||||
return 0;
|
||||
}
|
||||
|
@ -1328,9 +1383,11 @@ static textwindows dontinline int sys_epoll_create1_nt(uint32_t flags) {
|
|||
int64_t ephnd;
|
||||
struct PortState *port_state;
|
||||
struct TsTreeNode *tree_node;
|
||||
if (wepoll_init() < 0) return -1;
|
||||
if (wepoll_init() < 0)
|
||||
return -1;
|
||||
fd = __reservefd(-1);
|
||||
if (fd == -1) return -1;
|
||||
if (fd == -1)
|
||||
return -1;
|
||||
port_state = port_new(&ephnd);
|
||||
if (!port_state) {
|
||||
__releasefd(fd);
|
||||
|
@ -1361,9 +1418,12 @@ static textwindows dontinline int sys_epoll_ctl_nt(int epfd, int op, int fd,
|
|||
if (!IsWindows()) {
|
||||
return sys_epoll_ctl(epfd, op, fd, ev);
|
||||
} else {
|
||||
if (wepoll_init() < 0) return -1;
|
||||
if (!__isfdopen(fd)) return ebadf();
|
||||
if (!__isfdkind(epfd, kFdEpoll)) return ebadf();
|
||||
if (wepoll_init() < 0)
|
||||
return -1;
|
||||
if (!__isfdopen(fd))
|
||||
return ebadf();
|
||||
if (!__isfdkind(epfd, kFdEpoll))
|
||||
return ebadf();
|
||||
tree_node = ts_tree_find_and_ref(&epoll__handle_tree, g_fds.p[epfd].handle);
|
||||
if (!tree_node) {
|
||||
err_set_win_error(kNtErrorInvalidParameter);
|
||||
|
@ -1372,7 +1432,8 @@ static textwindows dontinline int sys_epoll_ctl_nt(int epfd, int op, int fd,
|
|||
port_state = port_state_from_handle_tree_node(tree_node);
|
||||
r = port_ctl(port_state, op, g_fds.p[fd].handle, ev);
|
||||
ts_tree_node_unref(tree_node);
|
||||
if (r < 0) goto err;
|
||||
if (r < 0)
|
||||
goto err;
|
||||
return 0;
|
||||
err:
|
||||
/* On Linux, in the case of epoll_ctl(), EBADF takes priority over
|
||||
|
@ -1390,9 +1451,12 @@ static textwindows dontinline int sys_epoll_wait_nt(int epfd,
|
|||
int num_events;
|
||||
struct PortState *port_state;
|
||||
struct TsTreeNode *tree_node;
|
||||
if (!__isfdkind(epfd, kFdEpoll)) return ebadf();
|
||||
if (maxevents <= 0) return einval();
|
||||
if (wepoll_init() < 0) return -1;
|
||||
if (!__isfdkind(epfd, kFdEpoll))
|
||||
return ebadf();
|
||||
if (maxevents <= 0)
|
||||
return einval();
|
||||
if (wepoll_init() < 0)
|
||||
return -1;
|
||||
tree_node = ts_tree_find_and_ref(&epoll__handle_tree, g_fds.p[epfd].handle);
|
||||
if (!tree_node) {
|
||||
err_set_win_error(kNtErrorInvalidParameter);
|
||||
|
@ -1401,7 +1465,8 @@ static textwindows dontinline int sys_epoll_wait_nt(int epfd,
|
|||
port_state = port_state_from_handle_tree_node(tree_node);
|
||||
num_events = port_wait(port_state, events, maxevents, timeoutms);
|
||||
ts_tree_node_unref(tree_node);
|
||||
if (num_events < 0) goto err;
|
||||
if (num_events < 0)
|
||||
goto err;
|
||||
return num_events;
|
||||
err:
|
||||
err_check_handle(g_fds.p[epfd].handle);
|
||||
|
@ -1412,7 +1477,8 @@ err:
|
|||
textwindows int sys_close_epoll_nt(int fd) {
|
||||
struct PortState *port_state;
|
||||
struct TsTreeNode *tree_node;
|
||||
if (wepoll_init() < 0) return -1;
|
||||
if (wepoll_init() < 0)
|
||||
return -1;
|
||||
tree_node = ts_tree_del_and_ref(&epoll__handle_tree, g_fds.p[fd].handle);
|
||||
if (!tree_node) {
|
||||
err_set_win_error(kNtErrorInvalidParameter);
|
||||
|
@ -1570,9 +1636,11 @@ int epoll_pwait(int epfd, struct epoll_event *events, int maxevents,
|
|||
sizeof(*sigmask));
|
||||
if (rc == -1 && errno == ENOSYS) {
|
||||
errno = e;
|
||||
if (sigmask) sys_sigprocmask(SIG_SETMASK, sigmask, &oldmask);
|
||||
if (sigmask)
|
||||
sys_sigprocmask(SIG_SETMASK, sigmask, &oldmask);
|
||||
rc = sys_epoll_wait(epfd, events, maxevents, timeoutms);
|
||||
if (sigmask) sys_sigprocmask(SIG_SETMASK, &oldmask, 0);
|
||||
if (sigmask)
|
||||
sys_sigprocmask(SIG_SETMASK, &oldmask, 0);
|
||||
}
|
||||
} else {
|
||||
BLOCK_SIGNALS;
|
||||
|
|
|
@ -37,7 +37,8 @@ static uint32_t *GetUnixIps(void) {
|
|||
uint64_t z;
|
||||
uint32_t *a;
|
||||
char *b, *p, *e, c[16];
|
||||
if ((fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP)) == -1) return 0;
|
||||
if ((fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP)) == -1)
|
||||
return 0;
|
||||
a = 0;
|
||||
n = 0;
|
||||
z = 15000;
|
||||
|
@ -47,7 +48,8 @@ static uint32_t *GetUnixIps(void) {
|
|||
if (sys_ioctl(fd, SIOCGIFCONF, &c) != -1) {
|
||||
for (p = b, e = p + MIN(z, READ32LE(c)); p + 16 + 16 <= e;
|
||||
p += IsBsd() ? 16 + MAX(16, p[16] & 255) : 40) {
|
||||
if ((p[IsBsd() ? 17 : 16] & 255) != AF_INET) continue;
|
||||
if ((p[IsBsd() ? 17 : 16] & 255) != AF_INET)
|
||||
continue;
|
||||
a = realloc(a, ++n * sizeof(*a));
|
||||
a[n - 1] = READ32BE(p + 20);
|
||||
}
|
||||
|
@ -66,13 +68,15 @@ static textwindows uint32_t *GetWindowsIps(void) {
|
|||
i = 0;
|
||||
z = 15000;
|
||||
do {
|
||||
if (!(ifaces = malloc(z))) return 0;
|
||||
if (!(ifaces = malloc(z)))
|
||||
return 0;
|
||||
rc = GetAdaptersAddresses(AF_INET,
|
||||
kNtGaaFlagSkipAnycast | kNtGaaFlagSkipMulticast |
|
||||
kNtGaaFlagSkipDnsServer |
|
||||
kNtGaaFlagSkipFriendlyName,
|
||||
0, ifaces, &z);
|
||||
if (rc != kNtErrorBufferOverflow) break;
|
||||
if (rc != kNtErrorBufferOverflow)
|
||||
break;
|
||||
free(ifaces);
|
||||
ifaces = 0;
|
||||
} while (++i < 3);
|
||||
|
@ -80,9 +84,11 @@ static textwindows uint32_t *GetWindowsIps(void) {
|
|||
a = calloc(1, sizeof(*a));
|
||||
} else if (rc == kNtNoError) {
|
||||
for (a = 0, n = 0, p = ifaces; p; p = p->Next) {
|
||||
if (p->OperStatus != kNtIfOperStatusUp) continue;
|
||||
if (p->OperStatus != kNtIfOperStatusUp)
|
||||
continue;
|
||||
for (u = p->FirstUnicastAddress; u; u = u->Next) {
|
||||
if (u->Address.lpSockaddr->sa_family != AF_INET) continue;
|
||||
if (u->Address.lpSockaddr->sa_family != AF_INET)
|
||||
continue;
|
||||
a = realloc(a, ++n * sizeof(*a));
|
||||
a[n - 1] = ntohl(
|
||||
((struct sockaddr_in *)u->Address.lpSockaddr)->sin_addr.s_addr);
|
||||
|
|
|
@ -25,7 +25,8 @@
|
|||
#include "libc/sysv/consts/tcp.h"
|
||||
|
||||
static bool Tune(int fd, int a, int b, int x) {
|
||||
if (!b) return false;
|
||||
if (!b)
|
||||
return false;
|
||||
return setsockopt(fd, a, b, &x, sizeof(x)) != -1;
|
||||
}
|
||||
|
||||
|
|
|
@ -42,11 +42,14 @@ int inet_aton(const char *s0, struct in_addr *dest) {
|
|||
int i;
|
||||
for (i = 0; i < 4; i++) {
|
||||
a[i] = strtoul(s, &z, 0);
|
||||
if (z == s || (*z && *z != '.') || !isdigit(*s)) return 0;
|
||||
if (!*z) break;
|
||||
if (z == s || (*z && *z != '.') || !isdigit(*s))
|
||||
return 0;
|
||||
if (!*z)
|
||||
break;
|
||||
s = z + 1;
|
||||
}
|
||||
if (i == 4) return 0;
|
||||
if (i == 4)
|
||||
return 0;
|
||||
switch (i) {
|
||||
case 0:
|
||||
a[1] = a[0] & 0xffffff;
|
||||
|
@ -59,7 +62,8 @@ int inet_aton(const char *s0, struct in_addr *dest) {
|
|||
a[2] >>= 8;
|
||||
}
|
||||
for (i = 0; i < 4; i++) {
|
||||
if (a[i] > 255) return 0;
|
||||
if (a[i] > 255)
|
||||
return 0;
|
||||
d[i] = a[i];
|
||||
}
|
||||
return 1;
|
||||
|
|
|
@ -35,7 +35,8 @@ const char *inet_ntop(int af, const void *src, char *dst, uint32_t size) {
|
|||
int i, t, a, b, c, d;
|
||||
const unsigned char *ip;
|
||||
p = dst;
|
||||
if (!size) return dst;
|
||||
if (!size)
|
||||
return dst;
|
||||
if ((ip = src)) {
|
||||
if (af == AF_INET) {
|
||||
if (size >= 16) {
|
||||
|
@ -84,9 +85,12 @@ const char *inet_ntop(int af, const void *src, char *dst, uint32_t size) {
|
|||
b = (ip[i + 0] & 0x0F) >> 0;
|
||||
c = (ip[i + 1] & 0xF0) >> 4;
|
||||
d = (ip[i + 1] & 0x0F) >> 0;
|
||||
if (a) *p++ = "0123456789abcdef"[a];
|
||||
if (a || b) *p++ = "0123456789abcdef"[b];
|
||||
if (a || b || c) *p++ = "0123456789abcdef"[c];
|
||||
if (a)
|
||||
*p++ = "0123456789abcdef"[a];
|
||||
if (a || b)
|
||||
*p++ = "0123456789abcdef"[b];
|
||||
if (a || b || c)
|
||||
*p++ = "0123456789abcdef"[c];
|
||||
*p++ = "0123456789abcdef"[d];
|
||||
}
|
||||
*p = '\0';
|
||||
|
|
|
@ -159,8 +159,10 @@ static int inet_pton_inet6_impl(const char *src, uint8_t *dst) {
|
|||
int inet_pton(int af, const char *src, void *dst) {
|
||||
uint8_t *p;
|
||||
int b, c, j;
|
||||
if (af == AF_INET6) return inet_pton_inet6_impl(src, dst);
|
||||
if (af != AF_INET) return eafnosupport();
|
||||
if (af == AF_INET6)
|
||||
return inet_pton_inet6_impl(src, dst);
|
||||
if (af != AF_INET)
|
||||
return eafnosupport();
|
||||
j = 0;
|
||||
p = dst;
|
||||
p[0] = 0;
|
||||
|
@ -168,9 +170,11 @@ int inet_pton(int af, const char *src, void *dst) {
|
|||
if (isdigit(c)) {
|
||||
b = c - '0' + p[j] * 10;
|
||||
p[j] = MIN(255, b);
|
||||
if (b > 255) return 0;
|
||||
if (b > 255)
|
||||
return 0;
|
||||
} else if (c == '.') {
|
||||
if (++j == 4) return 0;
|
||||
if (++j == 4)
|
||||
return 0;
|
||||
p[j] = 0;
|
||||
} else {
|
||||
return 0;
|
||||
|
|
|
@ -55,7 +55,8 @@ textwindows ssize_t sys_recvfrom_nt(int fd, const struct iovec *iov,
|
|||
size_t iovlen, uint32_t flags,
|
||||
void *opt_out_srcaddr,
|
||||
uint32_t *opt_inout_srcaddrsize) {
|
||||
if (flags & ~(_MSG_DONTWAIT | _MSG_OOB | _MSG_PEEK)) return einval();
|
||||
if (flags & ~(_MSG_DONTWAIT | _MSG_OOB | _MSG_PEEK))
|
||||
return einval();
|
||||
ssize_t rc;
|
||||
struct Fd *f = g_fds.p + fd;
|
||||
sigset_t m = __sig_block();
|
||||
|
|
|
@ -113,7 +113,8 @@ ssize_t recvmsg(int fd, struct msghdr *msg, int flags) {
|
|||
kprintf(".name=%#.*hhs, ", msg->msg_namelen, msg->msg_name);
|
||||
if (msg->msg_controllen)
|
||||
kprintf(".control=%#.*hhs, ", msg->msg_controllen, msg->msg_control);
|
||||
if (msg->msg_flags) kprintf(".flags=%#x, ", msg->msg_flags);
|
||||
if (msg->msg_flags)
|
||||
kprintf(".flags=%#x, ", msg->msg_flags);
|
||||
kprintf(".iov=%s", DescribeIovec(rc, msg->msg_iov, msg->msg_iovlen));
|
||||
kprintf("], %#x) → %'ld% m\n", flags, rc);
|
||||
}
|
||||
|
|
|
@ -49,7 +49,8 @@ static textwindows int sys_send_nt_start(int64_t handle,
|
|||
|
||||
textwindows ssize_t sys_send_nt(int fd, const struct iovec *iov, size_t iovlen,
|
||||
uint32_t flags) {
|
||||
if (flags & ~(_MSG_DONTWAIT | _MSG_OOB | _MSG_DONTROUTE)) return einval();
|
||||
if (flags & ~(_MSG_DONTWAIT | _MSG_OOB | _MSG_DONTROUTE))
|
||||
return einval();
|
||||
ssize_t rc;
|
||||
struct Fd *f = g_fds.p + fd;
|
||||
sigset_t m = __sig_block();
|
||||
|
|
|
@ -65,8 +65,10 @@ static dontinline textwindows ssize_t sys_sendfile_nt(
|
|||
uint32_t flags = 0;
|
||||
int64_t ih, oh, eof, offset;
|
||||
struct NtByHandleFileInformation wst;
|
||||
if (!__isfdkind(infd, kFdFile)) return ebadf();
|
||||
if (!__isfdkind(outfd, kFdSocket)) return ebadf();
|
||||
if (!__isfdkind(infd, kFdFile))
|
||||
return ebadf();
|
||||
if (!__isfdkind(outfd, kFdSocket))
|
||||
return ebadf();
|
||||
ih = g_fds.p[infd].handle;
|
||||
oh = g_fds.p[outfd].handle;
|
||||
if (opt_in_out_inoffset) {
|
||||
|
@ -119,12 +121,14 @@ static ssize_t sys_sendfile_bsd(int outfd, int infd,
|
|||
}
|
||||
if (IsFreebsd()) {
|
||||
rc = sys_sendfile_freebsd(infd, outfd, offset, uptobytes, 0, &sbytes, 0);
|
||||
if (rc == -1 && errno == ENOBUFS) errno = ENOMEM;
|
||||
if (rc == -1 && errno == ENOBUFS)
|
||||
errno = ENOMEM;
|
||||
} else {
|
||||
sbytes = uptobytes;
|
||||
rc = sys_sendfile_xnu(infd, outfd, offset, &sbytes, 0, 0);
|
||||
}
|
||||
if (rc == -1 && errno == ENOTSOCK) errno = EBADF;
|
||||
if (rc == -1 && errno == ENOTSOCK)
|
||||
errno = EBADF;
|
||||
if (rc != -1) {
|
||||
if (opt_in_out_inoffset) {
|
||||
*opt_in_out_inoffset += sbytes;
|
||||
|
|
|
@ -99,7 +99,8 @@ ssize_t sendmsg(int fd, const struct msghdr *msg, int flags) {
|
|||
kprintf(".name=%#.*hhs, ", msg->msg_namelen, msg->msg_name);
|
||||
if (msg->msg_controllen)
|
||||
kprintf(", .control=%#.*hhs, ", msg->msg_controllen, msg->msg_control);
|
||||
if (msg->msg_flags) kprintf(".flags=%#x, ", msg->msg_flags);
|
||||
if (msg->msg_flags)
|
||||
kprintf(".flags=%#x, ", msg->msg_flags);
|
||||
kprintf(", .iov=%s",
|
||||
DescribeIovec(rc != -1 ? rc : -2, msg->msg_iov, msg->msg_iovlen));
|
||||
}
|
||||
|
|
|
@ -52,7 +52,8 @@ static textwindows int sys_sendto_nt_start(int64_t handle,
|
|||
textwindows ssize_t sys_sendto_nt(int fd, const struct iovec *iov,
|
||||
size_t iovlen, uint32_t flags,
|
||||
void *opt_in_addr, uint32_t in_addrsize) {
|
||||
if (flags & ~(_MSG_DONTWAIT | _MSG_OOB | _MSG_DONTROUTE)) return einval();
|
||||
if (flags & ~(_MSG_DONTWAIT | _MSG_OOB | _MSG_DONTROUTE))
|
||||
return einval();
|
||||
ssize_t rc;
|
||||
struct Fd *f = g_fds.p + fd;
|
||||
sigset_t m = __sig_block();
|
||||
|
|
|
@ -38,12 +38,16 @@ textwindows int sys_setsockopt_nt(struct Fd *fd, int level, int optname,
|
|||
// socket read/write timeouts
|
||||
if (level == SOL_SOCKET &&
|
||||
(optname == SO_RCVTIMEO || optname == SO_SNDTIMEO)) {
|
||||
if (!(optval && optlen == sizeof(struct timeval))) return einval();
|
||||
if (!(optval && optlen == sizeof(struct timeval)))
|
||||
return einval();
|
||||
const struct timeval *tv = optval;
|
||||
int64_t ms = timeval_tomillis(*tv);
|
||||
if (ms >= 0xffffffffu) ms = 0; // wait forever (default)
|
||||
if (optname == SO_RCVTIMEO) fd->rcvtimeo = ms;
|
||||
if (optname == SO_SNDTIMEO) fd->sndtimeo = ms;
|
||||
if (ms >= 0xffffffffu)
|
||||
ms = 0; // wait forever (default)
|
||||
if (optname == SO_RCVTIMEO)
|
||||
fd->rcvtimeo = ms;
|
||||
if (optname == SO_SNDTIMEO)
|
||||
fd->sndtimeo = ms;
|
||||
return 0; // we want to handle this on our own
|
||||
}
|
||||
|
||||
|
|
|
@ -64,12 +64,16 @@ void __convert_sockaddr_to_bsd(struct sockaddr_storage *addr) {
|
|||
// copies sockaddr from internal memory to user's buffer
|
||||
void __write_sockaddr(const struct sockaddr_storage *addr, void *out_addr,
|
||||
uint32_t *inout_addrsize) {
|
||||
if (!out_addr) return;
|
||||
if (!inout_addrsize) return;
|
||||
if (!out_addr)
|
||||
return;
|
||||
if (!inout_addrsize)
|
||||
return;
|
||||
uint32_t insize = *inout_addrsize;
|
||||
if (insize) bzero(out_addr, insize);
|
||||
if (insize)
|
||||
bzero(out_addr, insize);
|
||||
uint32_t outsize = __get_sockaddr_len(addr);
|
||||
uint32_t copysize = MIN(insize, outsize);
|
||||
if (copysize) memcpy(out_addr, addr, copysize);
|
||||
if (copysize)
|
||||
memcpy(out_addr, addr, copysize);
|
||||
*inout_addrsize = outsize;
|
||||
}
|
||||
|
|
|
@ -64,7 +64,8 @@ void sockaddr2linux(const union sockaddr_storage_bsd *addr, uint32_t addrsize,
|
|||
sizeof(addr->sun.sun_family)),
|
||||
size - sizeof(out_addr->sun.sun_family)));
|
||||
out_addr->sun.sun_family = AF_UNIX;
|
||||
if (len) memcpy(out_addr->sun.sun_path, addr->sun.sun_path, len);
|
||||
if (len)
|
||||
memcpy(out_addr->sun.sun_path, addr->sun.sun_path, len);
|
||||
*inout_addrsize = sizeof(out_addr->sun.sun_family) + len + 1;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -30,8 +30,10 @@ static textwindows int sockatmark_nt(int fd, unsigned long magnum) {
|
|||
bool32 res;
|
||||
int64_t hand;
|
||||
uint32_t bytes;
|
||||
if (fd >= g_fds.n) return ebadf();
|
||||
if (g_fds.p[fd].kind != kFdSocket) return einval();
|
||||
if (fd >= g_fds.n)
|
||||
return ebadf();
|
||||
if (g_fds.p[fd].kind != kFdSocket)
|
||||
return einval();
|
||||
hand = g_fds.p[fd].handle;
|
||||
if (WSAIoctl(hand, magnum, 0, 0, &res, sizeof(res), &bytes, 0, 0) == -1) {
|
||||
return __winsockerr();
|
||||
|
|
|
@ -64,7 +64,8 @@ const char *(DescribeSockaddr)(char buf[128], const struct sockaddr *sa,
|
|||
unix = (const struct sockaddr_un *)sa;
|
||||
n = strnlen(unix->sun_path, sizeof(unix->sun_path));
|
||||
n = MIN(n, 128 - 1);
|
||||
if (n) memcpy(buf, unix->sun_path, n);
|
||||
if (n)
|
||||
memcpy(buf, unix->sun_path, n);
|
||||
buf[n] = 0;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -34,13 +34,16 @@ textwindows int sys_socket_nt(int family, int type, int protocol) {
|
|||
int64_t h;
|
||||
int fd, oflags, truetype;
|
||||
fd = __reservefd(-1);
|
||||
if (fd == -1) return -1;
|
||||
if (fd == -1)
|
||||
return -1;
|
||||
truetype = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
|
||||
if ((h = WSASocket(family, truetype, protocol, NULL, 0,
|
||||
kNtWsaFlagOverlapped)) != -1) {
|
||||
oflags = O_RDWR;
|
||||
if (type & SOCK_CLOEXEC) oflags |= O_CLOEXEC;
|
||||
if (type & SOCK_NONBLOCK) oflags |= O_NONBLOCK;
|
||||
if (type & SOCK_CLOEXEC)
|
||||
oflags |= O_CLOEXEC;
|
||||
if (type & SOCK_NONBLOCK)
|
||||
oflags |= O_NONBLOCK;
|
||||
g_fds.p[fd].family = family;
|
||||
g_fds.p[fd].type = truetype;
|
||||
g_fds.p[fd].protocol = protocol;
|
||||
|
|
|
@ -45,7 +45,8 @@ textwindows int sys_socketpair_nt(int family, int type, int proto, int sv[2]) {
|
|||
}
|
||||
|
||||
oflags = 0;
|
||||
if (type & SOCK_CLOEXEC) oflags |= O_CLOEXEC;
|
||||
if (type & SOCK_CLOEXEC)
|
||||
oflags |= O_CLOEXEC;
|
||||
type &= ~SOCK_CLOEXEC;
|
||||
|
||||
if (type == SOCK_STREAM) {
|
||||
|
@ -62,8 +63,10 @@ textwindows int sys_socketpair_nt(int family, int type, int proto, int sv[2]) {
|
|||
writer = __reservefd_unlocked(-1);
|
||||
__fds_unlock();
|
||||
if (reader == -1 || writer == -1) {
|
||||
if (reader != -1) __releasefd(reader);
|
||||
if (writer != -1) __releasefd(writer);
|
||||
if (reader != -1)
|
||||
__releasefd(reader);
|
||||
if (writer != -1)
|
||||
__releasefd(writer);
|
||||
return -1;
|
||||
}
|
||||
if ((hpipe = CreateNamedPipe(
|
||||
|
|
|
@ -23,7 +23,8 @@
|
|||
|
||||
int sys_socketpair(int family, int type, int protocol, int sv[2]) {
|
||||
int e = errno;
|
||||
if (__sys_socketpair(family, type, protocol, sv) != -1) return 0;
|
||||
if (__sys_socketpair(family, type, protocol, sv) != -1)
|
||||
return 0;
|
||||
if ((type & (SOCK_CLOEXEC | SOCK_NONBLOCK)) &&
|
||||
(errno == EINVAL || errno == EPROTOTYPE || errno == EPROTONOSUPPORT)) {
|
||||
errno = e;
|
||||
|
|
|
@ -37,7 +37,8 @@
|
|||
* @asyncsignalsafe
|
||||
*/
|
||||
int socketpair(int family, int type, int protocol, int sv[2]) {
|
||||
if (family == AF_UNSPEC) family = AF_UNIX;
|
||||
if (family == AF_UNSPEC)
|
||||
family = AF_UNIX;
|
||||
if (!IsWindows()) {
|
||||
return sys_socketpair(family, type, protocol, sv);
|
||||
} else {
|
||||
|
|
|
@ -62,7 +62,8 @@ static struct sockaddr_un log_addr = {AF_UNIX, "/dev/log"};
|
|||
static int64_t Time(int64_t *tp) {
|
||||
struct timespec ts;
|
||||
clock_gettime(CLOCK_REALTIME, &ts);
|
||||
if (tp) *tp = ts.tv_sec;
|
||||
if (tp)
|
||||
*tp = ts.tv_sec;
|
||||
return ts.tv_sec;
|
||||
}
|
||||
|
||||
|
@ -125,8 +126,10 @@ void vsyslog(int priority, const char *message, va_list ap) {
|
|||
int hlen; /* If LOG_CONS is specified, use to store the point in
|
||||
* the header message after the timestamp */
|
||||
BLOCK_CANCELATION;
|
||||
if (log_fd < 0) __openlog();
|
||||
if (!(priority & LOG_FACMASK)) priority |= log_facility;
|
||||
if (log_fd < 0)
|
||||
__openlog();
|
||||
if (!(priority & LOG_FACMASK))
|
||||
priority |= log_facility;
|
||||
/* Build the time string */
|
||||
now = Time(NULL);
|
||||
gmtime_r(&now, &tm);
|
||||
|
@ -226,9 +229,11 @@ void vsyslog(int priority, const char *message, va_list ap) {
|
|||
*/
|
||||
int setlogmask(int maskpri) {
|
||||
int ret;
|
||||
if (log_facility == -1) __initlog();
|
||||
if (log_facility == -1)
|
||||
__initlog();
|
||||
ret = log_mask;
|
||||
if (maskpri) log_mask = LOG_PRI(maskpri);
|
||||
if (maskpri)
|
||||
log_mask = LOG_PRI(maskpri);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
@ -261,13 +266,16 @@ int setlogmask(int maskpri) {
|
|||
*/
|
||||
void openlog(const char *ident, int opt, int facility) {
|
||||
BLOCK_CANCELATION;
|
||||
if (log_facility == -1) __initlog();
|
||||
if (!ident) ident = firstnonnull(program_invocation_short_name, "unknown");
|
||||
if (log_facility == -1)
|
||||
__initlog();
|
||||
if (!ident)
|
||||
ident = firstnonnull(program_invocation_short_name, "unknown");
|
||||
tprecode8to16(log_ident, ARRAYLEN(log_ident), ident);
|
||||
log_opt = opt;
|
||||
log_facility = facility;
|
||||
log_id = 0;
|
||||
if ((opt & LOG_NDELAY) && log_fd < 0) __openlog();
|
||||
if ((opt & LOG_NDELAY) && log_fd < 0)
|
||||
__openlog();
|
||||
ALLOW_CANCELATION;
|
||||
}
|
||||
|
||||
|
|
|
@ -42,7 +42,8 @@ __winsock_block(int64_t handle, uint32_t flags, bool nonblock,
|
|||
RestartOperation:
|
||||
int rc, sig, reason = 0;
|
||||
uint32_t status, exchanged;
|
||||
if (_check_cancel() == -1) return -1; // ECANCELED
|
||||
if (_check_cancel() == -1)
|
||||
return -1; // ECANCELED
|
||||
if (_weaken(__sig_get) && (sig = _weaken(__sig_get)(waitmask))) {
|
||||
goto HandleInterrupt;
|
||||
}
|
||||
|
@ -93,8 +94,10 @@ RestartOperation:
|
|||
if (_weaken(__sig_get) && (sig = _weaken(__sig_get)(waitmask))) {
|
||||
HandleInterrupt:
|
||||
int handler_was_called = _weaken(__sig_relay)(sig, SI_KERNEL, waitmask);
|
||||
if (_check_cancel() == -1) return -1;
|
||||
if (handler_was_called != 1) goto RestartOperation;
|
||||
if (_check_cancel() == -1)
|
||||
return -1;
|
||||
if (handler_was_called != 1)
|
||||
goto RestartOperation;
|
||||
}
|
||||
return eintr();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue