forked from cory/tildefriends
libuv 1.44.0
git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3856 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
713
deps/libuv/src/unix/android-ifaddrs.c
vendored
713
deps/libuv/src/unix/android-ifaddrs.c
vendored
@ -1,713 +0,0 @@
|
||||
/*
|
||||
Copyright (c) 2013, Kenneth MacKay
|
||||
Copyright (c) 2014, Emergya (Cloud4all, FP7/2007-2013 grant agreement #289016)
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification,
|
||||
are permitted provided that the following conditions are met:
|
||||
* Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
|
||||
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "uv/android-ifaddrs.h"
|
||||
#include "uv-common.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/socket.h>
|
||||
#include <net/if_arp.h>
|
||||
#include <netinet/in.h>
|
||||
#include <linux/netlink.h>
|
||||
#include <linux/rtnetlink.h>
|
||||
#include <linux/if_packet.h>
|
||||
|
||||
typedef struct NetlinkList
|
||||
{
|
||||
struct NetlinkList *m_next;
|
||||
struct nlmsghdr *m_data;
|
||||
unsigned int m_size;
|
||||
} NetlinkList;
|
||||
|
||||
static int netlink_socket(pid_t *p_pid)
|
||||
{
|
||||
struct sockaddr_nl l_addr;
|
||||
socklen_t l_len;
|
||||
|
||||
int l_socket = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
|
||||
if(l_socket < 0)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
memset(&l_addr, 0, sizeof(l_addr));
|
||||
l_addr.nl_family = AF_NETLINK;
|
||||
if(bind(l_socket, (struct sockaddr *)&l_addr, sizeof(l_addr)) < 0)
|
||||
{
|
||||
close(l_socket);
|
||||
return -1;
|
||||
}
|
||||
|
||||
l_len = sizeof(l_addr);
|
||||
if(getsockname(l_socket, (struct sockaddr *)&l_addr, &l_len) < 0)
|
||||
{
|
||||
close(l_socket);
|
||||
return -1;
|
||||
}
|
||||
*p_pid = l_addr.nl_pid;
|
||||
|
||||
return l_socket;
|
||||
}
|
||||
|
||||
static int netlink_send(int p_socket, int p_request)
|
||||
{
|
||||
char l_buffer[NLMSG_ALIGN(sizeof(struct nlmsghdr)) + NLMSG_ALIGN(sizeof(struct rtgenmsg))];
|
||||
|
||||
struct nlmsghdr *l_hdr;
|
||||
struct rtgenmsg *l_msg;
|
||||
struct sockaddr_nl l_addr;
|
||||
|
||||
memset(l_buffer, 0, sizeof(l_buffer));
|
||||
|
||||
l_hdr = (struct nlmsghdr *)l_buffer;
|
||||
l_msg = (struct rtgenmsg *)NLMSG_DATA(l_hdr);
|
||||
|
||||
l_hdr->nlmsg_len = NLMSG_LENGTH(sizeof(*l_msg));
|
||||
l_hdr->nlmsg_type = p_request;
|
||||
l_hdr->nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
|
||||
l_hdr->nlmsg_pid = 0;
|
||||
l_hdr->nlmsg_seq = p_socket;
|
||||
l_msg->rtgen_family = AF_UNSPEC;
|
||||
|
||||
memset(&l_addr, 0, sizeof(l_addr));
|
||||
l_addr.nl_family = AF_NETLINK;
|
||||
return (sendto(p_socket, l_hdr, l_hdr->nlmsg_len, 0, (struct sockaddr *)&l_addr, sizeof(l_addr)));
|
||||
}
|
||||
|
||||
static int netlink_recv(int p_socket, void *p_buffer, size_t p_len)
|
||||
{
|
||||
struct sockaddr_nl l_addr;
|
||||
struct msghdr l_msg;
|
||||
|
||||
struct iovec l_iov;
|
||||
l_iov.iov_base = p_buffer;
|
||||
l_iov.iov_len = p_len;
|
||||
|
||||
for(;;)
|
||||
{
|
||||
int l_result;
|
||||
l_msg.msg_name = (void *)&l_addr;
|
||||
l_msg.msg_namelen = sizeof(l_addr);
|
||||
l_msg.msg_iov = &l_iov;
|
||||
l_msg.msg_iovlen = 1;
|
||||
l_msg.msg_control = NULL;
|
||||
l_msg.msg_controllen = 0;
|
||||
l_msg.msg_flags = 0;
|
||||
l_result = recvmsg(p_socket, &l_msg, 0);
|
||||
|
||||
if(l_result < 0)
|
||||
{
|
||||
if(errno == EINTR)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
return -2;
|
||||
}
|
||||
|
||||
/* Buffer was too small */
|
||||
if(l_msg.msg_flags & MSG_TRUNC)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
return l_result;
|
||||
}
|
||||
}
|
||||
|
||||
static struct nlmsghdr *getNetlinkResponse(int p_socket, pid_t p_pid, int *p_size, int *p_done)
|
||||
{
|
||||
size_t l_size = 4096;
|
||||
void *l_buffer = NULL;
|
||||
|
||||
for(;;)
|
||||
{
|
||||
int l_read;
|
||||
|
||||
uv__free(l_buffer);
|
||||
l_buffer = uv__malloc(l_size);
|
||||
if (l_buffer == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
l_read = netlink_recv(p_socket, l_buffer, l_size);
|
||||
*p_size = l_read;
|
||||
if(l_read == -2)
|
||||
{
|
||||
uv__free(l_buffer);
|
||||
return NULL;
|
||||
}
|
||||
if(l_read >= 0)
|
||||
{
|
||||
struct nlmsghdr *l_hdr;
|
||||
for(l_hdr = (struct nlmsghdr *)l_buffer; NLMSG_OK(l_hdr, (unsigned int)l_read); l_hdr = (struct nlmsghdr *)NLMSG_NEXT(l_hdr, l_read))
|
||||
{
|
||||
if((pid_t)l_hdr->nlmsg_pid != p_pid || (int)l_hdr->nlmsg_seq != p_socket)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if(l_hdr->nlmsg_type == NLMSG_DONE)
|
||||
{
|
||||
*p_done = 1;
|
||||
break;
|
||||
}
|
||||
|
||||
if(l_hdr->nlmsg_type == NLMSG_ERROR)
|
||||
{
|
||||
uv__free(l_buffer);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
return l_buffer;
|
||||
}
|
||||
|
||||
l_size *= 2;
|
||||
}
|
||||
}
|
||||
|
||||
static NetlinkList *newListItem(struct nlmsghdr *p_data, unsigned int p_size)
|
||||
{
|
||||
NetlinkList *l_item = uv__malloc(sizeof(NetlinkList));
|
||||
if (l_item == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
l_item->m_next = NULL;
|
||||
l_item->m_data = p_data;
|
||||
l_item->m_size = p_size;
|
||||
return l_item;
|
||||
}
|
||||
|
||||
static void freeResultList(NetlinkList *p_list)
|
||||
{
|
||||
NetlinkList *l_cur;
|
||||
while(p_list)
|
||||
{
|
||||
l_cur = p_list;
|
||||
p_list = p_list->m_next;
|
||||
uv__free(l_cur->m_data);
|
||||
uv__free(l_cur);
|
||||
}
|
||||
}
|
||||
|
||||
static NetlinkList *getResultList(int p_socket, int p_request, pid_t p_pid)
|
||||
{
|
||||
int l_size;
|
||||
int l_done;
|
||||
NetlinkList *l_list;
|
||||
NetlinkList *l_end;
|
||||
|
||||
if(netlink_send(p_socket, p_request) < 0)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
l_list = NULL;
|
||||
l_end = NULL;
|
||||
|
||||
l_done = 0;
|
||||
while(!l_done)
|
||||
{
|
||||
NetlinkList *l_item;
|
||||
|
||||
struct nlmsghdr *l_hdr = getNetlinkResponse(p_socket, p_pid, &l_size, &l_done);
|
||||
/* Error */
|
||||
if(!l_hdr)
|
||||
{
|
||||
freeResultList(l_list);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
l_item = newListItem(l_hdr, l_size);
|
||||
if (!l_item)
|
||||
{
|
||||
freeResultList(l_list);
|
||||
return NULL;
|
||||
}
|
||||
if(!l_list)
|
||||
{
|
||||
l_list = l_item;
|
||||
}
|
||||
else
|
||||
{
|
||||
l_end->m_next = l_item;
|
||||
}
|
||||
l_end = l_item;
|
||||
}
|
||||
return l_list;
|
||||
}
|
||||
|
||||
static size_t maxSize(size_t a, size_t b)
|
||||
{
|
||||
return (a > b ? a : b);
|
||||
}
|
||||
|
||||
static size_t calcAddrLen(sa_family_t p_family, int p_dataSize)
|
||||
{
|
||||
switch(p_family)
|
||||
{
|
||||
case AF_INET:
|
||||
return sizeof(struct sockaddr_in);
|
||||
case AF_INET6:
|
||||
return sizeof(struct sockaddr_in6);
|
||||
case AF_PACKET:
|
||||
return maxSize(sizeof(struct sockaddr_ll), offsetof(struct sockaddr_ll, sll_addr) + p_dataSize);
|
||||
default:
|
||||
return maxSize(sizeof(struct sockaddr), offsetof(struct sockaddr, sa_data) + p_dataSize);
|
||||
}
|
||||
}
|
||||
|
||||
static void makeSockaddr(sa_family_t p_family, struct sockaddr *p_dest, void *p_data, size_t p_size)
|
||||
{
|
||||
switch(p_family)
|
||||
{
|
||||
case AF_INET:
|
||||
memcpy(&((struct sockaddr_in*)p_dest)->sin_addr, p_data, p_size);
|
||||
break;
|
||||
case AF_INET6:
|
||||
memcpy(&((struct sockaddr_in6*)p_dest)->sin6_addr, p_data, p_size);
|
||||
break;
|
||||
case AF_PACKET:
|
||||
memcpy(((struct sockaddr_ll*)p_dest)->sll_addr, p_data, p_size);
|
||||
((struct sockaddr_ll*)p_dest)->sll_halen = p_size;
|
||||
break;
|
||||
default:
|
||||
memcpy(p_dest->sa_data, p_data, p_size);
|
||||
break;
|
||||
}
|
||||
p_dest->sa_family = p_family;
|
||||
}
|
||||
|
||||
static void addToEnd(struct ifaddrs **p_resultList, struct ifaddrs *p_entry)
|
||||
{
|
||||
if(!*p_resultList)
|
||||
{
|
||||
*p_resultList = p_entry;
|
||||
}
|
||||
else
|
||||
{
|
||||
struct ifaddrs *l_cur = *p_resultList;
|
||||
while(l_cur->ifa_next)
|
||||
{
|
||||
l_cur = l_cur->ifa_next;
|
||||
}
|
||||
l_cur->ifa_next = p_entry;
|
||||
}
|
||||
}
|
||||
|
||||
static int interpretLink(struct nlmsghdr *p_hdr, struct ifaddrs **p_resultList)
|
||||
{
|
||||
struct ifaddrs *l_entry;
|
||||
|
||||
char *l_index;
|
||||
char *l_name;
|
||||
char *l_addr;
|
||||
char *l_data;
|
||||
|
||||
struct ifinfomsg *l_info = (struct ifinfomsg *)NLMSG_DATA(p_hdr);
|
||||
|
||||
size_t l_nameSize = 0;
|
||||
size_t l_addrSize = 0;
|
||||
size_t l_dataSize = 0;
|
||||
|
||||
size_t l_rtaSize = NLMSG_PAYLOAD(p_hdr, sizeof(struct ifinfomsg));
|
||||
struct rtattr *l_rta;
|
||||
for(l_rta = IFLA_RTA(l_info); RTA_OK(l_rta, l_rtaSize); l_rta = RTA_NEXT(l_rta, l_rtaSize))
|
||||
{
|
||||
size_t l_rtaDataSize = RTA_PAYLOAD(l_rta);
|
||||
switch(l_rta->rta_type)
|
||||
{
|
||||
case IFLA_ADDRESS:
|
||||
case IFLA_BROADCAST:
|
||||
l_addrSize += NLMSG_ALIGN(calcAddrLen(AF_PACKET, l_rtaDataSize));
|
||||
break;
|
||||
case IFLA_IFNAME:
|
||||
l_nameSize += NLMSG_ALIGN(l_rtaSize + 1);
|
||||
break;
|
||||
case IFLA_STATS:
|
||||
l_dataSize += NLMSG_ALIGN(l_rtaSize);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
l_entry = uv__malloc(sizeof(struct ifaddrs) + sizeof(int) + l_nameSize + l_addrSize + l_dataSize);
|
||||
if (l_entry == NULL)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
memset(l_entry, 0, sizeof(struct ifaddrs));
|
||||
l_entry->ifa_name = "";
|
||||
|
||||
l_index = ((char *)l_entry) + sizeof(struct ifaddrs);
|
||||
l_name = l_index + sizeof(int);
|
||||
l_addr = l_name + l_nameSize;
|
||||
l_data = l_addr + l_addrSize;
|
||||
|
||||
/* Save the interface index so we can look it up when handling the
|
||||
* addresses.
|
||||
*/
|
||||
memcpy(l_index, &l_info->ifi_index, sizeof(int));
|
||||
|
||||
l_entry->ifa_flags = l_info->ifi_flags;
|
||||
|
||||
l_rtaSize = NLMSG_PAYLOAD(p_hdr, sizeof(struct ifinfomsg));
|
||||
for(l_rta = IFLA_RTA(l_info); RTA_OK(l_rta, l_rtaSize); l_rta = RTA_NEXT(l_rta, l_rtaSize))
|
||||
{
|
||||
void *l_rtaData = RTA_DATA(l_rta);
|
||||
size_t l_rtaDataSize = RTA_PAYLOAD(l_rta);
|
||||
switch(l_rta->rta_type)
|
||||
{
|
||||
case IFLA_ADDRESS:
|
||||
case IFLA_BROADCAST:
|
||||
{
|
||||
size_t l_addrLen = calcAddrLen(AF_PACKET, l_rtaDataSize);
|
||||
makeSockaddr(AF_PACKET, (struct sockaddr *)l_addr, l_rtaData, l_rtaDataSize);
|
||||
((struct sockaddr_ll *)l_addr)->sll_ifindex = l_info->ifi_index;
|
||||
((struct sockaddr_ll *)l_addr)->sll_hatype = l_info->ifi_type;
|
||||
if(l_rta->rta_type == IFLA_ADDRESS)
|
||||
{
|
||||
l_entry->ifa_addr = (struct sockaddr *)l_addr;
|
||||
}
|
||||
else
|
||||
{
|
||||
l_entry->ifa_broadaddr = (struct sockaddr *)l_addr;
|
||||
}
|
||||
l_addr += NLMSG_ALIGN(l_addrLen);
|
||||
break;
|
||||
}
|
||||
case IFLA_IFNAME:
|
||||
strncpy(l_name, l_rtaData, l_rtaDataSize);
|
||||
l_name[l_rtaDataSize] = '\0';
|
||||
l_entry->ifa_name = l_name;
|
||||
break;
|
||||
case IFLA_STATS:
|
||||
memcpy(l_data, l_rtaData, l_rtaDataSize);
|
||||
l_entry->ifa_data = l_data;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
addToEnd(p_resultList, l_entry);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static struct ifaddrs *findInterface(int p_index, struct ifaddrs **p_links, int p_numLinks)
|
||||
{
|
||||
int l_num = 0;
|
||||
struct ifaddrs *l_cur = *p_links;
|
||||
while(l_cur && l_num < p_numLinks)
|
||||
{
|
||||
char *l_indexPtr = ((char *)l_cur) + sizeof(struct ifaddrs);
|
||||
int l_index;
|
||||
memcpy(&l_index, l_indexPtr, sizeof(int));
|
||||
if(l_index == p_index)
|
||||
{
|
||||
return l_cur;
|
||||
}
|
||||
|
||||
l_cur = l_cur->ifa_next;
|
||||
++l_num;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static int interpretAddr(struct nlmsghdr *p_hdr, struct ifaddrs **p_resultList, int p_numLinks)
|
||||
{
|
||||
struct ifaddrmsg *l_info = (struct ifaddrmsg *)NLMSG_DATA(p_hdr);
|
||||
struct ifaddrs *l_interface = findInterface(l_info->ifa_index, p_resultList, p_numLinks);
|
||||
|
||||
size_t l_nameSize = 0;
|
||||
size_t l_addrSize = 0;
|
||||
|
||||
int l_addedNetmask = 0;
|
||||
|
||||
size_t l_rtaSize = NLMSG_PAYLOAD(p_hdr, sizeof(struct ifaddrmsg));
|
||||
struct rtattr *l_rta;
|
||||
struct ifaddrs *l_entry;
|
||||
|
||||
char *l_name;
|
||||
char *l_addr;
|
||||
|
||||
for(l_rta = IFA_RTA(l_info); RTA_OK(l_rta, l_rtaSize); l_rta = RTA_NEXT(l_rta, l_rtaSize))
|
||||
{
|
||||
size_t l_rtaDataSize = RTA_PAYLOAD(l_rta);
|
||||
if(l_info->ifa_family == AF_PACKET)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
switch(l_rta->rta_type)
|
||||
{
|
||||
case IFA_ADDRESS:
|
||||
case IFA_LOCAL:
|
||||
l_addrSize += NLMSG_ALIGN(calcAddrLen(l_info->ifa_family, l_rtaDataSize));
|
||||
if((l_info->ifa_family == AF_INET || l_info->ifa_family == AF_INET6) && !l_addedNetmask)
|
||||
{
|
||||
/* Make room for netmask */
|
||||
l_addrSize += NLMSG_ALIGN(calcAddrLen(l_info->ifa_family, l_rtaDataSize));
|
||||
l_addedNetmask = 1;
|
||||
}
|
||||
break;
|
||||
case IFA_BROADCAST:
|
||||
l_addrSize += NLMSG_ALIGN(calcAddrLen(l_info->ifa_family, l_rtaDataSize));
|
||||
break;
|
||||
case IFA_LABEL:
|
||||
l_nameSize += NLMSG_ALIGN(l_rtaDataSize + 1);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
l_entry = uv__malloc(sizeof(struct ifaddrs) + l_nameSize + l_addrSize);
|
||||
if (l_entry == NULL)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
memset(l_entry, 0, sizeof(struct ifaddrs));
|
||||
l_entry->ifa_name = (l_interface ? l_interface->ifa_name : "");
|
||||
|
||||
l_name = ((char *)l_entry) + sizeof(struct ifaddrs);
|
||||
l_addr = l_name + l_nameSize;
|
||||
|
||||
l_entry->ifa_flags = l_info->ifa_flags;
|
||||
if(l_interface)
|
||||
{
|
||||
l_entry->ifa_flags |= l_interface->ifa_flags;
|
||||
}
|
||||
|
||||
l_rtaSize = NLMSG_PAYLOAD(p_hdr, sizeof(struct ifaddrmsg));
|
||||
for(l_rta = IFA_RTA(l_info); RTA_OK(l_rta, l_rtaSize); l_rta = RTA_NEXT(l_rta, l_rtaSize))
|
||||
{
|
||||
void *l_rtaData = RTA_DATA(l_rta);
|
||||
size_t l_rtaDataSize = RTA_PAYLOAD(l_rta);
|
||||
switch(l_rta->rta_type)
|
||||
{
|
||||
case IFA_ADDRESS:
|
||||
case IFA_BROADCAST:
|
||||
case IFA_LOCAL:
|
||||
{
|
||||
size_t l_addrLen = calcAddrLen(l_info->ifa_family, l_rtaDataSize);
|
||||
makeSockaddr(l_info->ifa_family, (struct sockaddr *)l_addr, l_rtaData, l_rtaDataSize);
|
||||
if(l_info->ifa_family == AF_INET6)
|
||||
{
|
||||
if(IN6_IS_ADDR_LINKLOCAL((struct in6_addr *)l_rtaData) || IN6_IS_ADDR_MC_LINKLOCAL((struct in6_addr *)l_rtaData))
|
||||
{
|
||||
((struct sockaddr_in6 *)l_addr)->sin6_scope_id = l_info->ifa_index;
|
||||
}
|
||||
}
|
||||
|
||||
/* Apparently in a point-to-point network IFA_ADDRESS contains
|
||||
* the dest address and IFA_LOCAL contains the local address
|
||||
*/
|
||||
if(l_rta->rta_type == IFA_ADDRESS)
|
||||
{
|
||||
if(l_entry->ifa_addr)
|
||||
{
|
||||
l_entry->ifa_dstaddr = (struct sockaddr *)l_addr;
|
||||
}
|
||||
else
|
||||
{
|
||||
l_entry->ifa_addr = (struct sockaddr *)l_addr;
|
||||
}
|
||||
}
|
||||
else if(l_rta->rta_type == IFA_LOCAL)
|
||||
{
|
||||
if(l_entry->ifa_addr)
|
||||
{
|
||||
l_entry->ifa_dstaddr = l_entry->ifa_addr;
|
||||
}
|
||||
l_entry->ifa_addr = (struct sockaddr *)l_addr;
|
||||
}
|
||||
else
|
||||
{
|
||||
l_entry->ifa_broadaddr = (struct sockaddr *)l_addr;
|
||||
}
|
||||
l_addr += NLMSG_ALIGN(l_addrLen);
|
||||
break;
|
||||
}
|
||||
case IFA_LABEL:
|
||||
strncpy(l_name, l_rtaData, l_rtaDataSize);
|
||||
l_name[l_rtaDataSize] = '\0';
|
||||
l_entry->ifa_name = l_name;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(l_entry->ifa_addr && (l_entry->ifa_addr->sa_family == AF_INET || l_entry->ifa_addr->sa_family == AF_INET6))
|
||||
{
|
||||
unsigned l_maxPrefix = (l_entry->ifa_addr->sa_family == AF_INET ? 32 : 128);
|
||||
unsigned l_prefix = (l_info->ifa_prefixlen > l_maxPrefix ? l_maxPrefix : l_info->ifa_prefixlen);
|
||||
unsigned char l_mask[16] = {0};
|
||||
unsigned i;
|
||||
for(i=0; i<(l_prefix/8); ++i)
|
||||
{
|
||||
l_mask[i] = 0xff;
|
||||
}
|
||||
if(l_prefix % 8)
|
||||
{
|
||||
l_mask[i] = 0xff << (8 - (l_prefix % 8));
|
||||
}
|
||||
|
||||
makeSockaddr(l_entry->ifa_addr->sa_family, (struct sockaddr *)l_addr, l_mask, l_maxPrefix / 8);
|
||||
l_entry->ifa_netmask = (struct sockaddr *)l_addr;
|
||||
}
|
||||
|
||||
addToEnd(p_resultList, l_entry);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int interpretLinks(int p_socket, pid_t p_pid, NetlinkList *p_netlinkList, struct ifaddrs **p_resultList)
|
||||
{
|
||||
|
||||
int l_numLinks = 0;
|
||||
for(; p_netlinkList; p_netlinkList = p_netlinkList->m_next)
|
||||
{
|
||||
unsigned int l_nlsize = p_netlinkList->m_size;
|
||||
struct nlmsghdr *l_hdr;
|
||||
for(l_hdr = p_netlinkList->m_data; NLMSG_OK(l_hdr, l_nlsize); l_hdr = NLMSG_NEXT(l_hdr, l_nlsize))
|
||||
{
|
||||
if((pid_t)l_hdr->nlmsg_pid != p_pid || (int)l_hdr->nlmsg_seq != p_socket)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if(l_hdr->nlmsg_type == NLMSG_DONE)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
if(l_hdr->nlmsg_type == RTM_NEWLINK)
|
||||
{
|
||||
if(interpretLink(l_hdr, p_resultList) == -1)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
++l_numLinks;
|
||||
}
|
||||
}
|
||||
}
|
||||
return l_numLinks;
|
||||
}
|
||||
|
||||
static int interpretAddrs(int p_socket, pid_t p_pid, NetlinkList *p_netlinkList, struct ifaddrs **p_resultList, int p_numLinks)
|
||||
{
|
||||
for(; p_netlinkList; p_netlinkList = p_netlinkList->m_next)
|
||||
{
|
||||
unsigned int l_nlsize = p_netlinkList->m_size;
|
||||
struct nlmsghdr *l_hdr;
|
||||
for(l_hdr = p_netlinkList->m_data; NLMSG_OK(l_hdr, l_nlsize); l_hdr = NLMSG_NEXT(l_hdr, l_nlsize))
|
||||
{
|
||||
if((pid_t)l_hdr->nlmsg_pid != p_pid || (int)l_hdr->nlmsg_seq != p_socket)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if(l_hdr->nlmsg_type == NLMSG_DONE)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
if(l_hdr->nlmsg_type == RTM_NEWADDR)
|
||||
{
|
||||
if (interpretAddr(l_hdr, p_resultList, p_numLinks) == -1)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int getifaddrs(struct ifaddrs **ifap)
|
||||
{
|
||||
int l_socket;
|
||||
int l_result;
|
||||
int l_numLinks;
|
||||
pid_t l_pid;
|
||||
NetlinkList *l_linkResults;
|
||||
NetlinkList *l_addrResults;
|
||||
|
||||
if(!ifap)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
*ifap = NULL;
|
||||
|
||||
l_socket = netlink_socket(&l_pid);
|
||||
if(l_socket < 0)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
l_linkResults = getResultList(l_socket, RTM_GETLINK, l_pid);
|
||||
if(!l_linkResults)
|
||||
{
|
||||
close(l_socket);
|
||||
return -1;
|
||||
}
|
||||
|
||||
l_addrResults = getResultList(l_socket, RTM_GETADDR, l_pid);
|
||||
if(!l_addrResults)
|
||||
{
|
||||
close(l_socket);
|
||||
freeResultList(l_linkResults);
|
||||
return -1;
|
||||
}
|
||||
|
||||
l_result = 0;
|
||||
l_numLinks = interpretLinks(l_socket, l_pid, l_linkResults, ifap);
|
||||
if(l_numLinks == -1 || interpretAddrs(l_socket, l_pid, l_addrResults, ifap, l_numLinks) == -1)
|
||||
{
|
||||
l_result = -1;
|
||||
}
|
||||
|
||||
freeResultList(l_linkResults);
|
||||
freeResultList(l_addrResults);
|
||||
close(l_socket);
|
||||
return l_result;
|
||||
}
|
||||
|
||||
void freeifaddrs(struct ifaddrs *ifa)
|
||||
{
|
||||
struct ifaddrs *l_cur;
|
||||
while(ifa)
|
||||
{
|
||||
l_cur = ifa;
|
||||
ifa = ifa->ifa_next;
|
||||
uv__free(l_cur);
|
||||
}
|
||||
}
|
8
deps/libuv/src/unix/bsd-ifaddrs.c
vendored
8
deps/libuv/src/unix/bsd-ifaddrs.c
vendored
@ -27,7 +27,7 @@
|
||||
|
||||
#include <ifaddrs.h>
|
||||
#include <net/if.h>
|
||||
#if !defined(__CYGWIN__) && !defined(__MSYS__)
|
||||
#if !defined(__CYGWIN__) && !defined(__MSYS__) && !defined(__GNU__)
|
||||
#include <net/if_dl.h>
|
||||
#endif
|
||||
|
||||
@ -40,7 +40,7 @@ static int uv__ifaddr_exclude(struct ifaddrs *ent, int exclude_type) {
|
||||
return 1;
|
||||
if (ent->ifa_addr == NULL)
|
||||
return 1;
|
||||
#if !defined(__CYGWIN__) && !defined(__MSYS__)
|
||||
#if !defined(__CYGWIN__) && !defined(__MSYS__) && !defined(__GNU__)
|
||||
/*
|
||||
* If `exclude_type` is `UV__EXCLUDE_IFPHYS`, return whether `sa_family`
|
||||
* equals `AF_LINK`. Otherwise, the result depends on the operating
|
||||
@ -69,7 +69,7 @@ int uv_interface_addresses(uv_interface_address_t** addresses, int* count) {
|
||||
struct ifaddrs* addrs;
|
||||
struct ifaddrs* ent;
|
||||
uv_interface_address_t* address;
|
||||
#if !(defined(__CYGWIN__) || defined(__MSYS__))
|
||||
#if !(defined(__CYGWIN__) || defined(__MSYS__)) && !defined(__GNU__)
|
||||
int i;
|
||||
#endif
|
||||
|
||||
@ -126,7 +126,7 @@ int uv_interface_addresses(uv_interface_address_t** addresses, int* count) {
|
||||
address++;
|
||||
}
|
||||
|
||||
#if !(defined(__CYGWIN__) || defined(__MSYS__))
|
||||
#if !(defined(__CYGWIN__) || defined(__MSYS__)) && !defined(__GNU__)
|
||||
/* Fill in physical addresses for each interface */
|
||||
for (ent = addrs; ent != NULL; ent = ent->ifa_next) {
|
||||
if (uv__ifaddr_exclude(ent, UV__EXCLUDE_IFPHYS))
|
||||
|
1
deps/libuv/src/unix/bsd-proctitle.c
vendored
1
deps/libuv/src/unix/bsd-proctitle.c
vendored
@ -38,6 +38,7 @@ static void init_process_title_mutex_once(void) {
|
||||
|
||||
|
||||
void uv__process_title_cleanup(void) {
|
||||
uv_once(&process_title_mutex_once, init_process_title_mutex_once);
|
||||
uv_mutex_destroy(&process_title_mutex);
|
||||
}
|
||||
|
||||
|
142
deps/libuv/src/unix/core.c
vendored
142
deps/libuv/src/unix/core.c
vendored
@ -84,6 +84,7 @@ extern char** environ;
|
||||
#endif
|
||||
|
||||
#if defined(__linux__)
|
||||
# include <sched.h>
|
||||
# include <sys/syscall.h>
|
||||
# define uv__accept4 accept4
|
||||
#endif
|
||||
@ -96,9 +97,9 @@ static int uv__run_pending(uv_loop_t* loop);
|
||||
|
||||
/* Verify that uv_buf_t is ABI-compatible with struct iovec. */
|
||||
STATIC_ASSERT(sizeof(uv_buf_t) == sizeof(struct iovec));
|
||||
STATIC_ASSERT(sizeof(&((uv_buf_t*) 0)->base) ==
|
||||
STATIC_ASSERT(sizeof(((uv_buf_t*) 0)->base) ==
|
||||
sizeof(((struct iovec*) 0)->iov_base));
|
||||
STATIC_ASSERT(sizeof(&((uv_buf_t*) 0)->len) ==
|
||||
STATIC_ASSERT(sizeof(((uv_buf_t*) 0)->len) ==
|
||||
sizeof(((struct iovec*) 0)->iov_len));
|
||||
STATIC_ASSERT(offsetof(uv_buf_t, base) == offsetof(struct iovec, iov_base));
|
||||
STATIC_ASSERT(offsetof(uv_buf_t, len) == offsetof(struct iovec, iov_len));
|
||||
@ -334,35 +335,36 @@ int uv_backend_fd(const uv_loop_t* loop) {
|
||||
}
|
||||
|
||||
|
||||
int uv_backend_timeout(const uv_loop_t* loop) {
|
||||
if (loop->stop_flag != 0)
|
||||
return 0;
|
||||
|
||||
if (!uv__has_active_handles(loop) && !uv__has_active_reqs(loop))
|
||||
return 0;
|
||||
|
||||
if (!QUEUE_EMPTY(&loop->idle_handles))
|
||||
return 0;
|
||||
|
||||
if (!QUEUE_EMPTY(&loop->pending_queue))
|
||||
return 0;
|
||||
|
||||
if (loop->closing_handles)
|
||||
return 0;
|
||||
|
||||
return uv__next_timeout(loop);
|
||||
}
|
||||
|
||||
|
||||
static int uv__loop_alive(const uv_loop_t* loop) {
|
||||
return uv__has_active_handles(loop) ||
|
||||
uv__has_active_reqs(loop) ||
|
||||
!QUEUE_EMPTY(&loop->pending_queue) ||
|
||||
loop->closing_handles != NULL;
|
||||
}
|
||||
|
||||
|
||||
static int uv__backend_timeout(const uv_loop_t* loop) {
|
||||
if (loop->stop_flag == 0 &&
|
||||
/* uv__loop_alive(loop) && */
|
||||
(uv__has_active_handles(loop) || uv__has_active_reqs(loop)) &&
|
||||
QUEUE_EMPTY(&loop->pending_queue) &&
|
||||
QUEUE_EMPTY(&loop->idle_handles) &&
|
||||
loop->closing_handles == NULL)
|
||||
return uv__next_timeout(loop);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int uv_backend_timeout(const uv_loop_t* loop) {
|
||||
if (QUEUE_EMPTY(&loop->watcher_queue))
|
||||
return uv__backend_timeout(loop);
|
||||
/* Need to call uv_run to update the backend fd state. */
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int uv_loop_alive(const uv_loop_t* loop) {
|
||||
return uv__loop_alive(loop);
|
||||
return uv__loop_alive(loop);
|
||||
}
|
||||
|
||||
|
||||
@ -384,7 +386,7 @@ int uv_run(uv_loop_t* loop, uv_run_mode mode) {
|
||||
|
||||
timeout = 0;
|
||||
if ((mode == UV_RUN_ONCE && !ran_pending) || mode == UV_RUN_DEFAULT)
|
||||
timeout = uv_backend_timeout(loop);
|
||||
timeout = uv__backend_timeout(loop);
|
||||
|
||||
uv__io_poll(loop, timeout);
|
||||
|
||||
@ -597,20 +599,6 @@ int uv__nonblock_ioctl(int fd, int set) {
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int uv__cloexec_ioctl(int fd, int set) {
|
||||
int r;
|
||||
|
||||
do
|
||||
r = ioctl(fd, set ? FIOCLEX : FIONCLEX);
|
||||
while (r == -1 && errno == EINTR);
|
||||
|
||||
if (r)
|
||||
return UV__ERR(errno);
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
@ -645,25 +633,13 @@ int uv__nonblock_fcntl(int fd, int set) {
|
||||
}
|
||||
|
||||
|
||||
int uv__cloexec_fcntl(int fd, int set) {
|
||||
int uv__cloexec(int fd, int set) {
|
||||
int flags;
|
||||
int r;
|
||||
|
||||
do
|
||||
r = fcntl(fd, F_GETFD);
|
||||
while (r == -1 && errno == EINTR);
|
||||
|
||||
if (r == -1)
|
||||
return UV__ERR(errno);
|
||||
|
||||
/* Bail out now if already set/clear. */
|
||||
if (!!(r & FD_CLOEXEC) == !!set)
|
||||
return 0;
|
||||
|
||||
flags = 0;
|
||||
if (set)
|
||||
flags = r | FD_CLOEXEC;
|
||||
else
|
||||
flags = r & ~FD_CLOEXEC;
|
||||
flags = FD_CLOEXEC;
|
||||
|
||||
do
|
||||
r = fcntl(fd, F_SETFD, flags);
|
||||
@ -1036,6 +1012,32 @@ int uv__open_cloexec(const char* path, int flags) {
|
||||
}
|
||||
|
||||
|
||||
int uv__slurp(const char* filename, char* buf, size_t len) {
|
||||
ssize_t n;
|
||||
int fd;
|
||||
|
||||
assert(len > 0);
|
||||
|
||||
fd = uv__open_cloexec(filename, O_RDONLY);
|
||||
if (fd < 0)
|
||||
return fd;
|
||||
|
||||
do
|
||||
n = read(fd, buf, len - 1);
|
||||
while (n == -1 && errno == EINTR);
|
||||
|
||||
if (uv__close_nocheckstdio(fd))
|
||||
abort();
|
||||
|
||||
if (n < 0)
|
||||
return UV__ERR(errno);
|
||||
|
||||
buf[n] = '\0';
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int uv__dup2_cloexec(int oldfd, int newfd) {
|
||||
#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__linux__)
|
||||
int r;
|
||||
@ -1621,3 +1623,37 @@ int uv__search_path(const char* prog, char* buf, size_t* buflen) {
|
||||
/* Out of tokens (path entries), and no match found */
|
||||
return UV_EINVAL;
|
||||
}
|
||||
|
||||
|
||||
unsigned int uv_available_parallelism(void) {
|
||||
#ifdef __linux__
|
||||
cpu_set_t set;
|
||||
long rc;
|
||||
|
||||
memset(&set, 0, sizeof(set));
|
||||
|
||||
/* sysconf(_SC_NPROCESSORS_ONLN) in musl calls sched_getaffinity() but in
|
||||
* glibc it's... complicated... so for consistency try sched_getaffinity()
|
||||
* before falling back to sysconf(_SC_NPROCESSORS_ONLN).
|
||||
*/
|
||||
if (0 == sched_getaffinity(0, sizeof(set), &set))
|
||||
rc = CPU_COUNT(&set);
|
||||
else
|
||||
rc = sysconf(_SC_NPROCESSORS_ONLN);
|
||||
|
||||
if (rc < 1)
|
||||
rc = 1;
|
||||
|
||||
return (unsigned) rc;
|
||||
#elif defined(__MVS__)
|
||||
return 1; /* TODO(bnoordhuis) Read from CSD_NUMBER_ONLINE_CPUS? */
|
||||
#else /* __linux__ */
|
||||
long rc;
|
||||
|
||||
rc = sysconf(_SC_NPROCESSORS_ONLN);
|
||||
if (rc < 1)
|
||||
rc = 1;
|
||||
|
||||
return (unsigned) rc;
|
||||
#endif /* __linux__ */
|
||||
}
|
||||
|
15
deps/libuv/src/unix/freebsd.c
vendored
15
deps/libuv/src/unix/freebsd.c
vendored
@ -287,3 +287,18 @@ int uv__recvmmsg(int fd, struct uv__mmsghdr* mmsg, unsigned int vlen) {
|
||||
return errno = ENOSYS, -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
ssize_t
|
||||
uv__fs_copy_file_range(int fd_in,
|
||||
off_t* off_in,
|
||||
int fd_out,
|
||||
off_t* off_out,
|
||||
size_t len,
|
||||
unsigned int flags)
|
||||
{
|
||||
#if __FreeBSD__ >= 13 && !defined(__DragonFly__)
|
||||
return copy_file_range(fd_in, off_in, fd_out, off_out, len, flags);
|
||||
#else
|
||||
return errno = ENOSYS, -1;
|
||||
#endif
|
||||
}
|
||||
|
17
deps/libuv/src/unix/fs.c
vendored
17
deps/libuv/src/unix/fs.c
vendored
@ -247,7 +247,8 @@ UV_UNUSED(static struct timeval uv__fs_to_timeval(double time)) {
|
||||
static ssize_t uv__fs_futime(uv_fs_t* req) {
|
||||
#if defined(__linux__) \
|
||||
|| defined(_AIX71) \
|
||||
|| defined(__HAIKU__)
|
||||
|| defined(__HAIKU__) \
|
||||
|| defined(__GNU__)
|
||||
struct timespec ts[2];
|
||||
ts[0] = uv__fs_to_timespec(req->atime);
|
||||
ts[1] = uv__fs_to_timespec(req->mtime);
|
||||
@ -1074,6 +1075,17 @@ static ssize_t uv__fs_sendfile(uv_fs_t* req) {
|
||||
*/
|
||||
|
||||
#if defined(__FreeBSD__) || defined(__DragonFly__)
|
||||
#if defined(__FreeBSD__)
|
||||
off_t off;
|
||||
|
||||
off = req->off;
|
||||
r = uv__fs_copy_file_range(in_fd, &off, out_fd, NULL, req->bufsml[0].len, 0);
|
||||
if (r >= 0) {
|
||||
r = off - req->off;
|
||||
req->off = off;
|
||||
return r;
|
||||
}
|
||||
#endif
|
||||
len = 0;
|
||||
r = sendfile(in_fd, out_fd, req->off, req->bufsml[0].len, NULL, &len, 0);
|
||||
#elif defined(__FreeBSD_kernel__)
|
||||
@ -1168,7 +1180,8 @@ static ssize_t uv__fs_lutime(uv_fs_t* req) {
|
||||
#if defined(__linux__) || \
|
||||
defined(_AIX71) || \
|
||||
defined(__sun) || \
|
||||
defined(__HAIKU__)
|
||||
defined(__HAIKU__) || \
|
||||
defined(__GNU__)
|
||||
struct timespec ts[2];
|
||||
ts[0] = uv__fs_to_timespec(req->atime);
|
||||
ts[1] = uv__fs_to_timespec(req->mtime);
|
||||
|
167
deps/libuv/src/unix/hurd.c
vendored
Normal file
167
deps/libuv/src/unix/hurd.c
vendored
Normal file
@ -0,0 +1,167 @@
|
||||
/* Copyright libuv project contributors. All rights reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to
|
||||
* deal in the Software without restriction, including without limitation the
|
||||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
* sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
* IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#define _GNU_SOURCE 1
|
||||
|
||||
#include "uv.h"
|
||||
#include "internal.h"
|
||||
|
||||
#include <hurd.h>
|
||||
#include <hurd/process.h>
|
||||
#include <mach/task_info.h>
|
||||
#include <mach/vm_statistics.h>
|
||||
#include <mach/vm_param.h>
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <stddef.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <limits.h>
|
||||
|
||||
int uv_exepath(char* buffer, size_t* size) {
|
||||
kern_return_t err;
|
||||
/* XXX in current Hurd, strings are char arrays of 1024 elements */
|
||||
string_t exepath;
|
||||
ssize_t copied;
|
||||
|
||||
if (buffer == NULL || size == NULL || *size == 0)
|
||||
return UV_EINVAL;
|
||||
|
||||
if (*size - 1 > 0) {
|
||||
/* XXX limited length of buffer in current Hurd, this API will probably
|
||||
* evolve in the future */
|
||||
err = proc_get_exe(getproc(), getpid(), exepath);
|
||||
|
||||
if (err)
|
||||
return UV__ERR(err);
|
||||
}
|
||||
|
||||
copied = uv__strscpy(buffer, exepath, *size);
|
||||
|
||||
/* do not return error on UV_E2BIG failure */
|
||||
*size = copied < 0 ? strlen(buffer) : (size_t) copied;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int uv_resident_set_memory(size_t* rss) {
|
||||
kern_return_t err;
|
||||
struct task_basic_info bi;
|
||||
mach_msg_type_number_t count;
|
||||
|
||||
count = TASK_BASIC_INFO_COUNT;
|
||||
err = task_info(mach_task_self(), TASK_BASIC_INFO,
|
||||
(task_info_t) &bi, &count);
|
||||
|
||||
if (err)
|
||||
return UV__ERR(err);
|
||||
|
||||
*rss = bi.resident_size;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint64_t uv_get_free_memory(void) {
|
||||
kern_return_t err;
|
||||
struct vm_statistics vmstats;
|
||||
|
||||
err = vm_statistics(mach_task_self(), &vmstats);
|
||||
|
||||
if (err)
|
||||
return 0;
|
||||
|
||||
return vmstats.free_count * vm_page_size;
|
||||
}
|
||||
|
||||
|
||||
uint64_t uv_get_total_memory(void) {
|
||||
kern_return_t err;
|
||||
host_basic_info_data_t hbi;
|
||||
mach_msg_type_number_t cnt;
|
||||
|
||||
cnt = HOST_BASIC_INFO_COUNT;
|
||||
err = host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t) &hbi, &cnt);
|
||||
|
||||
if (err)
|
||||
return 0;
|
||||
|
||||
return hbi.memory_size;
|
||||
}
|
||||
|
||||
|
||||
int uv_uptime(double* uptime) {
|
||||
char buf[128];
|
||||
|
||||
/* Try /proc/uptime first */
|
||||
if (0 == uv__slurp("/proc/uptime", buf, sizeof(buf)))
|
||||
if (1 == sscanf(buf, "%lf", uptime))
|
||||
return 0;
|
||||
|
||||
/* Reimplement here code from procfs to calculate uptime if not mounted? */
|
||||
|
||||
return UV__ERR(EIO);
|
||||
}
|
||||
|
||||
void uv_loadavg(double avg[3]) {
|
||||
char buf[128]; /* Large enough to hold all of /proc/loadavg. */
|
||||
|
||||
if (0 == uv__slurp("/proc/loadavg", buf, sizeof(buf)))
|
||||
if (3 == sscanf(buf, "%lf %lf %lf", &avg[0], &avg[1], &avg[2]))
|
||||
return;
|
||||
|
||||
/* Reimplement here code from procfs to calculate loadavg if not mounted? */
|
||||
}
|
||||
|
||||
|
||||
int uv_cpu_info(uv_cpu_info_t** cpu_infos, int* count) {
|
||||
kern_return_t err;
|
||||
host_basic_info_data_t hbi;
|
||||
mach_msg_type_number_t cnt;
|
||||
|
||||
/* Get count of cpus */
|
||||
cnt = HOST_BASIC_INFO_COUNT;
|
||||
err = host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t) &hbi, &cnt);
|
||||
|
||||
if (err) {
|
||||
err = UV__ERR(err);
|
||||
goto abort;
|
||||
}
|
||||
|
||||
/* XXX not implemented on the Hurd */
|
||||
*cpu_infos = uv__calloc(hbi.avail_cpus, sizeof(**cpu_infos));
|
||||
if (*cpu_infos == NULL) {
|
||||
err = UV_ENOMEM;
|
||||
goto abort;
|
||||
}
|
||||
|
||||
*count = hbi.avail_cpus;
|
||||
|
||||
return 0;
|
||||
|
||||
abort:
|
||||
*cpu_infos = NULL;
|
||||
*count = 0;
|
||||
return err;
|
||||
}
|
||||
|
||||
uint64_t uv_get_constrained_memory(void) {
|
||||
return 0; /* Memory constraints are unknown. */
|
||||
}
|
24
deps/libuv/src/unix/internal.h
vendored
24
deps/libuv/src/unix/internal.h
vendored
@ -145,7 +145,8 @@ typedef struct uv__stream_queued_fds_s uv__stream_queued_fds_t;
|
||||
|
||||
/* loop flags */
|
||||
enum {
|
||||
UV_LOOP_BLOCK_SIGPROF = 1
|
||||
UV_LOOP_BLOCK_SIGPROF = 0x1,
|
||||
UV_LOOP_REAP_CHILDREN = 0x2
|
||||
};
|
||||
|
||||
/* flags of excluding ifaddr */
|
||||
@ -174,11 +175,9 @@ struct uv__stream_queued_fds_s {
|
||||
defined(__linux__) || \
|
||||
defined(__OpenBSD__) || \
|
||||
defined(__NetBSD__)
|
||||
#define uv__cloexec uv__cloexec_ioctl
|
||||
#define uv__nonblock uv__nonblock_ioctl
|
||||
#define UV__NONBLOCK_IS_IOCTL 1
|
||||
#else
|
||||
#define uv__cloexec uv__cloexec_fcntl
|
||||
#define uv__nonblock uv__nonblock_fcntl
|
||||
#define UV__NONBLOCK_IS_IOCTL 0
|
||||
#endif
|
||||
@ -196,8 +195,7 @@ struct uv__stream_queued_fds_s {
|
||||
#endif
|
||||
|
||||
/* core */
|
||||
int uv__cloexec_ioctl(int fd, int set);
|
||||
int uv__cloexec_fcntl(int fd, int set);
|
||||
int uv__cloexec(int fd, int set);
|
||||
int uv__nonblock_ioctl(int fd, int set);
|
||||
int uv__nonblock_fcntl(int fd, int set);
|
||||
int uv__close(int fd); /* preserves errno */
|
||||
@ -241,14 +239,15 @@ void uv__server_io(uv_loop_t* loop, uv__io_t* w, unsigned int events);
|
||||
int uv__accept(int sockfd);
|
||||
int uv__dup2_cloexec(int oldfd, int newfd);
|
||||
int uv__open_cloexec(const char* path, int flags);
|
||||
int uv__slurp(const char* filename, char* buf, size_t len);
|
||||
|
||||
/* tcp */
|
||||
int uv_tcp_listen(uv_tcp_t* tcp, int backlog, uv_connection_cb cb);
|
||||
int uv__tcp_listen(uv_tcp_t* tcp, int backlog, uv_connection_cb cb);
|
||||
int uv__tcp_nodelay(int fd, int on);
|
||||
int uv__tcp_keepalive(int fd, int on, unsigned int delay);
|
||||
|
||||
/* pipe */
|
||||
int uv_pipe_listen(uv_pipe_t* handle, int backlog, uv_connection_cb cb);
|
||||
int uv__pipe_listen(uv_pipe_t* handle, int backlog, uv_connection_cb cb);
|
||||
|
||||
/* signal */
|
||||
void uv__signal_close(uv_signal_t* handle);
|
||||
@ -282,6 +281,7 @@ uv_handle_type uv__handle_type(int fd);
|
||||
FILE* uv__open_file(const char* path);
|
||||
int uv__getpwuid_r(uv_passwd_t* pwd);
|
||||
int uv__search_path(const char* prog, char* buf, size_t* buflen);
|
||||
void uv__wait_children(uv_loop_t* loop);
|
||||
|
||||
/* random */
|
||||
int uv__random_devurandom(void* buf, size_t buflen);
|
||||
@ -356,5 +356,15 @@ size_t strnlen(const char* s, size_t maxlen);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if defined(__FreeBSD__)
|
||||
ssize_t
|
||||
uv__fs_copy_file_range(int fd_in,
|
||||
off_t* off_in,
|
||||
int fd_out,
|
||||
off_t* off_out,
|
||||
size_t len,
|
||||
unsigned int flags);
|
||||
#endif
|
||||
|
||||
|
||||
#endif /* UV_UNIX_INTERNAL_H_ */
|
||||
|
10
deps/libuv/src/unix/kqueue.c
vendored
10
deps/libuv/src/unix/kqueue.c
vendored
@ -284,6 +284,11 @@ void uv__io_poll(uv_loop_t* loop, int timeout) {
|
||||
loop->watchers[loop->nwatchers + 1] = (void*) (uintptr_t) nfds;
|
||||
for (i = 0; i < nfds; i++) {
|
||||
ev = events + i;
|
||||
if (ev->filter == EVFILT_PROC) {
|
||||
loop->flags |= UV_LOOP_REAP_CHILDREN;
|
||||
nevents++;
|
||||
continue;
|
||||
}
|
||||
fd = ev->ident;
|
||||
/* Skip invalidated events, see uv__platform_invalidate_fd */
|
||||
if (fd == -1)
|
||||
@ -377,6 +382,11 @@ void uv__io_poll(uv_loop_t* loop, int timeout) {
|
||||
nevents++;
|
||||
}
|
||||
|
||||
if (loop->flags & UV_LOOP_REAP_CHILDREN) {
|
||||
loop->flags &= ~UV_LOOP_REAP_CHILDREN;
|
||||
uv__wait_children(loop);
|
||||
}
|
||||
|
||||
if (reset_timeout != 0) {
|
||||
timeout = user_timeout;
|
||||
reset_timeout = 0;
|
||||
|
39
deps/libuv/src/unix/linux-core.c
vendored
39
deps/libuv/src/unix/linux-core.c
vendored
@ -45,6 +45,10 @@
|
||||
|
||||
#define HAVE_IFADDRS_H 1
|
||||
|
||||
# if defined(__ANDROID_API__) && __ANDROID_API__ < 24
|
||||
# undef HAVE_IFADDRS_H
|
||||
#endif
|
||||
|
||||
#ifdef __UCLIBC__
|
||||
# if __UCLIBC_MAJOR__ < 0 && __UCLIBC_MINOR__ < 9 && __UCLIBC_SUBLEVEL__ < 32
|
||||
# undef HAVE_IFADDRS_H
|
||||
@ -52,11 +56,7 @@
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_IFADDRS_H
|
||||
# if defined(__ANDROID__)
|
||||
# include "uv/android-ifaddrs.h"
|
||||
# else
|
||||
# include <ifaddrs.h>
|
||||
# endif
|
||||
# include <ifaddrs.h>
|
||||
# include <sys/socket.h>
|
||||
# include <net/ethernet.h>
|
||||
# include <netpacket/packet.h>
|
||||
@ -211,31 +211,6 @@ err:
|
||||
return UV_EINVAL;
|
||||
}
|
||||
|
||||
static int uv__slurp(const char* filename, char* buf, size_t len) {
|
||||
ssize_t n;
|
||||
int fd;
|
||||
|
||||
assert(len > 0);
|
||||
|
||||
fd = uv__open_cloexec(filename, O_RDONLY);
|
||||
if (fd < 0)
|
||||
return fd;
|
||||
|
||||
do
|
||||
n = read(fd, buf, len - 1);
|
||||
while (n == -1 && errno == EINTR);
|
||||
|
||||
if (uv__close_nocheckstdio(fd))
|
||||
abort();
|
||||
|
||||
if (n < 0)
|
||||
return UV__ERR(errno);
|
||||
|
||||
buf[n] = '\0';
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int uv_uptime(double* uptime) {
|
||||
static volatile int no_clock_boottime;
|
||||
char buf[128];
|
||||
@ -243,7 +218,7 @@ int uv_uptime(double* uptime) {
|
||||
int r;
|
||||
|
||||
/* Try /proc/uptime first, then fallback to clock_gettime(). */
|
||||
|
||||
|
||||
if (0 == uv__slurp("/proc/uptime", buf, sizeof(buf)))
|
||||
if (1 == sscanf(buf, "%lf", uptime))
|
||||
return 0;
|
||||
@ -641,6 +616,7 @@ static uint64_t read_cpufreq(unsigned int cpunum) {
|
||||
}
|
||||
|
||||
|
||||
#ifdef HAVE_IFADDRS_H
|
||||
static int uv__ifaddr_exclude(struct ifaddrs *ent, int exclude_type) {
|
||||
if (!((ent->ifa_flags & IFF_UP) && (ent->ifa_flags & IFF_RUNNING)))
|
||||
return 1;
|
||||
@ -654,6 +630,7 @@ static int uv__ifaddr_exclude(struct ifaddrs *ent, int exclude_type) {
|
||||
return exclude_type;
|
||||
return !exclude_type;
|
||||
}
|
||||
#endif
|
||||
|
||||
int uv_interface_addresses(uv_interface_address_t** addresses, int* count) {
|
||||
#ifndef HAVE_IFADDRS_H
|
||||
|
2
deps/libuv/src/unix/pipe.c
vendored
2
deps/libuv/src/unix/pipe.c
vendored
@ -91,7 +91,7 @@ err_socket:
|
||||
}
|
||||
|
||||
|
||||
int uv_pipe_listen(uv_pipe_t* handle, int backlog, uv_connection_cb cb) {
|
||||
int uv__pipe_listen(uv_pipe_t* handle, int backlog, uv_connection_cb cb) {
|
||||
if (uv__stream_fd(handle) == -1)
|
||||
return UV_EINVAL;
|
||||
|
||||
|
753
deps/libuv/src/unix/process.c
vendored
753
deps/libuv/src/unix/process.c
vendored
@ -27,6 +27,7 @@
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <signal.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
@ -35,8 +36,21 @@
|
||||
#include <poll.h>
|
||||
|
||||
#if defined(__APPLE__) && !TARGET_OS_IPHONE
|
||||
# include <spawn.h>
|
||||
# include <paths.h>
|
||||
# include <sys/kauth.h>
|
||||
# include <sys/types.h>
|
||||
# include <sys/sysctl.h>
|
||||
# include <dlfcn.h>
|
||||
# include <crt_externs.h>
|
||||
# include <xlocale.h>
|
||||
# define environ (*_NSGetEnviron())
|
||||
|
||||
/* macOS 10.14 back does not define this constant */
|
||||
# ifndef POSIX_SPAWN_SETSID
|
||||
# define POSIX_SPAWN_SETSID 1024
|
||||
# endif
|
||||
|
||||
#else
|
||||
extern char **environ;
|
||||
#endif
|
||||
@ -49,10 +63,20 @@ extern char **environ;
|
||||
# include "zos-base.h"
|
||||
#endif
|
||||
|
||||
#if defined(__APPLE__) || defined(__DragonFly__) || defined(__FreeBSD__) || defined(__NetBSD__)
|
||||
#include <sys/event.h>
|
||||
#endif
|
||||
|
||||
|
||||
#if !(defined(__APPLE__) || defined(__DragonFly__) || defined(__FreeBSD__) || defined(__NetBSD__))
|
||||
static void uv__chld(uv_signal_t* handle, int signum) {
|
||||
assert(signum == SIGCHLD);
|
||||
uv__wait_children(handle->loop);
|
||||
}
|
||||
#endif
|
||||
|
||||
void uv__wait_children(uv_loop_t* loop) {
|
||||
uv_process_t* process;
|
||||
uv_loop_t* loop;
|
||||
int exit_status;
|
||||
int term_signal;
|
||||
int status;
|
||||
@ -61,10 +85,7 @@ static void uv__chld(uv_signal_t* handle, int signum) {
|
||||
QUEUE* q;
|
||||
QUEUE* h;
|
||||
|
||||
assert(signum == SIGCHLD);
|
||||
|
||||
QUEUE_INIT(&pending);
|
||||
loop = handle->loop;
|
||||
|
||||
h = &loop->process_handles;
|
||||
q = QUEUE_HEAD(h);
|
||||
@ -254,22 +275,33 @@ static void uv__process_child_init(const uv_process_options_t* options,
|
||||
use_fd = pipes[fd][1];
|
||||
if (use_fd < 0 || use_fd >= fd)
|
||||
continue;
|
||||
#ifdef F_DUPFD_CLOEXEC /* POSIX 2008 */
|
||||
pipes[fd][1] = fcntl(use_fd, F_DUPFD_CLOEXEC, stdio_count);
|
||||
#else
|
||||
pipes[fd][1] = fcntl(use_fd, F_DUPFD, stdio_count);
|
||||
#endif
|
||||
if (pipes[fd][1] == -1)
|
||||
uv__write_errno(error_fd);
|
||||
#ifndef F_DUPFD_CLOEXEC /* POSIX 2008 */
|
||||
n = uv__cloexec(pipes[fd][1], 1);
|
||||
if (n) {
|
||||
uv__write_int(error_fd, n);
|
||||
_exit(127);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
for (fd = 0; fd < stdio_count; fd++) {
|
||||
close_fd = pipes[fd][0];
|
||||
close_fd = -1;
|
||||
use_fd = pipes[fd][1];
|
||||
|
||||
if (use_fd < 0) {
|
||||
if (fd >= 3)
|
||||
continue;
|
||||
else {
|
||||
/* redirect stdin, stdout and stderr to /dev/null even if UV_IGNORE is
|
||||
* set
|
||||
*/
|
||||
/* Redirect stdin, stdout and stderr to /dev/null even if UV_IGNORE is
|
||||
* set. */
|
||||
uv__close_nocheckstdio(fd); /* Free up fd, if it happens to be open. */
|
||||
use_fd = open("/dev/null", fd == 0 ? O_RDONLY : O_RDWR);
|
||||
close_fd = use_fd;
|
||||
|
||||
@ -278,28 +310,29 @@ static void uv__process_child_init(const uv_process_options_t* options,
|
||||
}
|
||||
}
|
||||
|
||||
if (fd == use_fd)
|
||||
uv__cloexec_fcntl(use_fd, 0);
|
||||
else
|
||||
if (fd == use_fd) {
|
||||
if (close_fd == -1) {
|
||||
n = uv__cloexec(use_fd, 0);
|
||||
if (n) {
|
||||
uv__write_int(error_fd, n);
|
||||
_exit(127);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
fd = dup2(use_fd, fd);
|
||||
}
|
||||
|
||||
if (fd == -1)
|
||||
uv__write_errno(error_fd);
|
||||
|
||||
if (fd <= 2)
|
||||
if (fd <= 2 && close_fd == -1)
|
||||
uv__nonblock_fcntl(fd, 0);
|
||||
|
||||
if (close_fd >= stdio_count)
|
||||
uv__close(close_fd);
|
||||
}
|
||||
|
||||
for (fd = 0; fd < stdio_count; fd++) {
|
||||
use_fd = pipes[fd][1];
|
||||
|
||||
if (use_fd >= stdio_count)
|
||||
uv__close(use_fd);
|
||||
}
|
||||
|
||||
if (options->cwd != NULL && chdir(options->cwd))
|
||||
uv__write_errno(error_fd);
|
||||
|
||||
@ -320,9 +353,8 @@ static void uv__process_child_init(const uv_process_options_t* options,
|
||||
if ((options->flags & UV_PROCESS_SETUID) && setuid(options->uid))
|
||||
uv__write_errno(error_fd);
|
||||
|
||||
if (options->env != NULL) {
|
||||
if (options->env != NULL)
|
||||
environ = options->env;
|
||||
}
|
||||
|
||||
/* Reset signal mask just before exec. */
|
||||
sigemptyset(&signewset);
|
||||
@ -341,6 +373,562 @@ static void uv__process_child_init(const uv_process_options_t* options,
|
||||
#endif
|
||||
|
||||
|
||||
#if defined(__APPLE__)
|
||||
typedef struct uv__posix_spawn_fncs_tag {
|
||||
struct {
|
||||
int (*addchdir_np)(const posix_spawn_file_actions_t *, const char *);
|
||||
} file_actions;
|
||||
} uv__posix_spawn_fncs_t;
|
||||
|
||||
|
||||
static uv_once_t posix_spawn_init_once = UV_ONCE_INIT;
|
||||
static uv__posix_spawn_fncs_t posix_spawn_fncs;
|
||||
static int posix_spawn_can_use_setsid;
|
||||
|
||||
|
||||
static void uv__spawn_init_posix_spawn_fncs(void) {
|
||||
/* Try to locate all non-portable functions at runtime */
|
||||
posix_spawn_fncs.file_actions.addchdir_np =
|
||||
dlsym(RTLD_DEFAULT, "posix_spawn_file_actions_addchdir_np");
|
||||
}
|
||||
|
||||
|
||||
static void uv__spawn_init_can_use_setsid(void) {
|
||||
static const int MACOS_CATALINA_VERSION_MAJOR = 19;
|
||||
char version_str[256];
|
||||
char* version_major_str;
|
||||
size_t version_str_size = 256;
|
||||
int r;
|
||||
int version_major;
|
||||
|
||||
/* Get a version string */
|
||||
r = sysctlbyname("kern.osrelease", version_str, &version_str_size, NULL, 0);
|
||||
if (r != 0)
|
||||
return;
|
||||
|
||||
/* Try to get the major version number. If not found
|
||||
* fall back to the fork/exec flow */
|
||||
version_major_str = strtok(version_str, ".");
|
||||
if (version_major_str == NULL)
|
||||
return;
|
||||
|
||||
/* Parse the version major as a number. If it is greater than
|
||||
* the major version for macOS Catalina (aka macOS 10.15), then
|
||||
* the POSIX_SPAWN_SETSID flag is available */
|
||||
version_major = atoi_l(version_major_str, NULL); /* Use LC_C_LOCALE */
|
||||
if (version_major >= MACOS_CATALINA_VERSION_MAJOR)
|
||||
posix_spawn_can_use_setsid = 1;
|
||||
}
|
||||
|
||||
|
||||
static void uv__spawn_init_posix_spawn(void) {
|
||||
/* Init handles to all potentially non-defined functions */
|
||||
uv__spawn_init_posix_spawn_fncs();
|
||||
|
||||
/* Init feature detection for POSIX_SPAWN_SETSID flag */
|
||||
uv__spawn_init_can_use_setsid();
|
||||
}
|
||||
|
||||
|
||||
static int uv__spawn_set_posix_spawn_attrs(
|
||||
posix_spawnattr_t* attrs,
|
||||
const uv__posix_spawn_fncs_t* posix_spawn_fncs,
|
||||
const uv_process_options_t* options) {
|
||||
int err;
|
||||
unsigned int flags;
|
||||
sigset_t signal_set;
|
||||
|
||||
err = posix_spawnattr_init(attrs);
|
||||
if (err != 0) {
|
||||
/* If initialization fails, no need to de-init, just return */
|
||||
return err;
|
||||
}
|
||||
|
||||
if (options->flags & (UV_PROCESS_SETUID | UV_PROCESS_SETGID)) {
|
||||
/* kauth_cred_issuser currently requires exactly uid == 0 for these
|
||||
* posixspawn_attrs (set_groups_np, setuid_np, setgid_np), which deviates
|
||||
* from the normal specification of setuid (which also uses euid), and they
|
||||
* are also undocumented syscalls, so we do not use them. */
|
||||
err = ENOSYS;
|
||||
goto error;
|
||||
}
|
||||
|
||||
/* Set flags for spawn behavior
|
||||
* 1) POSIX_SPAWN_CLOEXEC_DEFAULT: (Apple Extension) All descriptors in the
|
||||
* parent will be treated as if they had been created with O_CLOEXEC. The
|
||||
* only fds that will be passed on to the child are those manipulated by
|
||||
* the file actions
|
||||
* 2) POSIX_SPAWN_SETSIGDEF: Signals mentioned in spawn-sigdefault in the
|
||||
* spawn attributes will be reset to behave as their default
|
||||
* 3) POSIX_SPAWN_SETSIGMASK: Signal mask will be set to the value of
|
||||
* spawn-sigmask in attributes
|
||||
* 4) POSIX_SPAWN_SETSID: Make the process a new session leader if a detached
|
||||
* session was requested. */
|
||||
flags = POSIX_SPAWN_CLOEXEC_DEFAULT |
|
||||
POSIX_SPAWN_SETSIGDEF |
|
||||
POSIX_SPAWN_SETSIGMASK;
|
||||
if (options->flags & UV_PROCESS_DETACHED) {
|
||||
/* If running on a version of macOS where this flag is not supported,
|
||||
* revert back to the fork/exec flow. Otherwise posix_spawn will
|
||||
* silently ignore the flag. */
|
||||
if (!posix_spawn_can_use_setsid) {
|
||||
err = ENOSYS;
|
||||
goto error;
|
||||
}
|
||||
|
||||
flags |= POSIX_SPAWN_SETSID;
|
||||
}
|
||||
err = posix_spawnattr_setflags(attrs, flags);
|
||||
if (err != 0)
|
||||
goto error;
|
||||
|
||||
/* Reset all signal the child to their default behavior */
|
||||
sigfillset(&signal_set);
|
||||
err = posix_spawnattr_setsigdefault(attrs, &signal_set);
|
||||
if (err != 0)
|
||||
goto error;
|
||||
|
||||
/* Reset the signal mask for all signals */
|
||||
sigemptyset(&signal_set);
|
||||
err = posix_spawnattr_setsigmask(attrs, &signal_set);
|
||||
if (err != 0)
|
||||
goto error;
|
||||
|
||||
return err;
|
||||
|
||||
error:
|
||||
(void) posix_spawnattr_destroy(attrs);
|
||||
return err;
|
||||
}
|
||||
|
||||
|
||||
static int uv__spawn_set_posix_spawn_file_actions(
|
||||
posix_spawn_file_actions_t* actions,
|
||||
const uv__posix_spawn_fncs_t* posix_spawn_fncs,
|
||||
const uv_process_options_t* options,
|
||||
int stdio_count,
|
||||
int (*pipes)[2]) {
|
||||
int fd;
|
||||
int fd2;
|
||||
int use_fd;
|
||||
int err;
|
||||
|
||||
err = posix_spawn_file_actions_init(actions);
|
||||
if (err != 0) {
|
||||
/* If initialization fails, no need to de-init, just return */
|
||||
return err;
|
||||
}
|
||||
|
||||
/* Set the current working directory if requested */
|
||||
if (options->cwd != NULL) {
|
||||
if (posix_spawn_fncs->file_actions.addchdir_np == NULL) {
|
||||
err = ENOSYS;
|
||||
goto error;
|
||||
}
|
||||
|
||||
err = posix_spawn_fncs->file_actions.addchdir_np(actions, options->cwd);
|
||||
if (err != 0)
|
||||
goto error;
|
||||
}
|
||||
|
||||
/* Do not return ENOSYS after this point, as we may mutate pipes. */
|
||||
|
||||
/* First duplicate low numbered fds, since it's not safe to duplicate them,
|
||||
* they could get replaced. Example: swapping stdout and stderr; without
|
||||
* this fd 2 (stderr) would be duplicated into fd 1, thus making both
|
||||
* stdout and stderr go to the same fd, which was not the intention. */
|
||||
for (fd = 0; fd < stdio_count; fd++) {
|
||||
use_fd = pipes[fd][1];
|
||||
if (use_fd < 0 || use_fd >= fd)
|
||||
continue;
|
||||
use_fd = stdio_count;
|
||||
for (fd2 = 0; fd2 < stdio_count; fd2++) {
|
||||
/* If we were not setting POSIX_SPAWN_CLOEXEC_DEFAULT, we would need to
|
||||
* also consider whether fcntl(fd, F_GETFD) returned without the
|
||||
* FD_CLOEXEC flag set. */
|
||||
if (pipes[fd2][1] == use_fd) {
|
||||
use_fd++;
|
||||
fd2 = 0;
|
||||
}
|
||||
}
|
||||
err = posix_spawn_file_actions_adddup2(
|
||||
actions,
|
||||
pipes[fd][1],
|
||||
use_fd);
|
||||
assert(err != ENOSYS);
|
||||
if (err != 0)
|
||||
goto error;
|
||||
pipes[fd][1] = use_fd;
|
||||
}
|
||||
|
||||
/* Second, move the descriptors into their respective places */
|
||||
for (fd = 0; fd < stdio_count; fd++) {
|
||||
use_fd = pipes[fd][1];
|
||||
if (use_fd < 0) {
|
||||
if (fd >= 3)
|
||||
continue;
|
||||
else {
|
||||
/* If ignored, redirect to (or from) /dev/null, */
|
||||
err = posix_spawn_file_actions_addopen(
|
||||
actions,
|
||||
fd,
|
||||
"/dev/null",
|
||||
fd == 0 ? O_RDONLY : O_RDWR,
|
||||
0);
|
||||
assert(err != ENOSYS);
|
||||
if (err != 0)
|
||||
goto error;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (fd == use_fd)
|
||||
err = posix_spawn_file_actions_addinherit_np(actions, fd);
|
||||
else
|
||||
err = posix_spawn_file_actions_adddup2(actions, use_fd, fd);
|
||||
assert(err != ENOSYS);
|
||||
if (err != 0)
|
||||
goto error;
|
||||
|
||||
/* Make sure the fd is marked as non-blocking (state shared between child
|
||||
* and parent). */
|
||||
uv__nonblock_fcntl(use_fd, 0);
|
||||
}
|
||||
|
||||
/* Finally, close all the superfluous descriptors */
|
||||
for (fd = 0; fd < stdio_count; fd++) {
|
||||
use_fd = pipes[fd][1];
|
||||
if (use_fd < stdio_count)
|
||||
continue;
|
||||
|
||||
/* Check if we already closed this. */
|
||||
for (fd2 = 0; fd2 < fd; fd2++) {
|
||||
if (pipes[fd2][1] == use_fd)
|
||||
break;
|
||||
}
|
||||
if (fd2 < fd)
|
||||
continue;
|
||||
|
||||
err = posix_spawn_file_actions_addclose(actions, use_fd);
|
||||
assert(err != ENOSYS);
|
||||
if (err != 0)
|
||||
goto error;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
error:
|
||||
(void) posix_spawn_file_actions_destroy(actions);
|
||||
return err;
|
||||
}
|
||||
|
||||
char* uv__spawn_find_path_in_env(char** env) {
|
||||
char** env_iterator;
|
||||
const char path_var[] = "PATH=";
|
||||
|
||||
/* Look for an environment variable called PATH in the
|
||||
* provided env array, and return its value if found */
|
||||
for (env_iterator = env; *env_iterator != NULL; env_iterator++) {
|
||||
if (strncmp(*env_iterator, path_var, sizeof(path_var) - 1) == 0) {
|
||||
/* Found "PATH=" at the beginning of the string */
|
||||
return *env_iterator + sizeof(path_var) - 1;
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
static int uv__spawn_resolve_and_spawn(const uv_process_options_t* options,
|
||||
posix_spawnattr_t* attrs,
|
||||
posix_spawn_file_actions_t* actions,
|
||||
pid_t* pid) {
|
||||
const char *p;
|
||||
const char *z;
|
||||
const char *path;
|
||||
size_t l;
|
||||
size_t k;
|
||||
int err;
|
||||
int seen_eacces;
|
||||
|
||||
path = NULL;
|
||||
err = -1;
|
||||
seen_eacces = 0;
|
||||
|
||||
/* Short circuit for erroneous case */
|
||||
if (options->file == NULL)
|
||||
return ENOENT;
|
||||
|
||||
/* The environment for the child process is that of the parent unless overriden
|
||||
* by options->env */
|
||||
char** env = environ;
|
||||
if (options->env != NULL)
|
||||
env = options->env;
|
||||
|
||||
/* If options->file contains a slash, posix_spawn/posix_spawnp behave
|
||||
* the same, and don't involve PATH resolution at all. Otherwise, if
|
||||
* options->file does not include a slash, but no custom environment is
|
||||
* to be used, the environment used for path resolution as well for the
|
||||
* child process is that of the parent process, so posix_spawnp is the
|
||||
* way to go. */
|
||||
if (strchr(options->file, '/') != NULL || options->env == NULL) {
|
||||
do
|
||||
err = posix_spawnp(pid, options->file, actions, attrs, options->args, env);
|
||||
while (err == EINTR);
|
||||
return err;
|
||||
}
|
||||
|
||||
/* Look for the definition of PATH in the provided env */
|
||||
path = uv__spawn_find_path_in_env(options->env);
|
||||
|
||||
/* The following resolution logic (execvpe emulation) is copied from
|
||||
* https://git.musl-libc.org/cgit/musl/tree/src/process/execvp.c
|
||||
* and adapted to work for our specific usage */
|
||||
|
||||
/* If no path was provided in options->env, use the default value
|
||||
* to look for the executable */
|
||||
if (path == NULL)
|
||||
path = _PATH_DEFPATH;
|
||||
|
||||
k = strnlen(options->file, NAME_MAX + 1);
|
||||
if (k > NAME_MAX)
|
||||
return ENAMETOOLONG;
|
||||
|
||||
l = strnlen(path, PATH_MAX - 1) + 1;
|
||||
|
||||
for (p = path;; p = z) {
|
||||
/* Compose the new process file from the entry in the PATH
|
||||
* environment variable and the actual file name */
|
||||
char b[PATH_MAX + NAME_MAX];
|
||||
z = strchr(p, ':');
|
||||
if (!z)
|
||||
z = p + strlen(p);
|
||||
if ((size_t)(z - p) >= l) {
|
||||
if (!*z++)
|
||||
break;
|
||||
|
||||
continue;
|
||||
}
|
||||
memcpy(b, p, z - p);
|
||||
b[z - p] = '/';
|
||||
memcpy(b + (z - p) + (z > p), options->file, k + 1);
|
||||
|
||||
/* Try to spawn the new process file. If it fails with ENOENT, the
|
||||
* new process file is not in this PATH entry, continue with the next
|
||||
* PATH entry. */
|
||||
do
|
||||
err = posix_spawn(pid, b, actions, attrs, options->args, env);
|
||||
while (err == EINTR);
|
||||
|
||||
switch (err) {
|
||||
case EACCES:
|
||||
seen_eacces = 1;
|
||||
break; /* continue search */
|
||||
case ENOENT:
|
||||
case ENOTDIR:
|
||||
break; /* continue search */
|
||||
default:
|
||||
return err;
|
||||
}
|
||||
|
||||
if (!*z++)
|
||||
break;
|
||||
}
|
||||
|
||||
if (seen_eacces)
|
||||
return EACCES;
|
||||
return err;
|
||||
}
|
||||
|
||||
|
||||
static int uv__spawn_and_init_child_posix_spawn(
|
||||
const uv_process_options_t* options,
|
||||
int stdio_count,
|
||||
int (*pipes)[2],
|
||||
pid_t* pid,
|
||||
const uv__posix_spawn_fncs_t* posix_spawn_fncs) {
|
||||
int err;
|
||||
posix_spawnattr_t attrs;
|
||||
posix_spawn_file_actions_t actions;
|
||||
|
||||
err = uv__spawn_set_posix_spawn_attrs(&attrs, posix_spawn_fncs, options);
|
||||
if (err != 0)
|
||||
goto error;
|
||||
|
||||
/* This may mutate pipes. */
|
||||
err = uv__spawn_set_posix_spawn_file_actions(&actions,
|
||||
posix_spawn_fncs,
|
||||
options,
|
||||
stdio_count,
|
||||
pipes);
|
||||
if (err != 0) {
|
||||
(void) posix_spawnattr_destroy(&attrs);
|
||||
goto error;
|
||||
}
|
||||
|
||||
/* Try to spawn options->file resolving in the provided environment
|
||||
* if any */
|
||||
err = uv__spawn_resolve_and_spawn(options, &attrs, &actions, pid);
|
||||
assert(err != ENOSYS);
|
||||
|
||||
/* Destroy the actions/attributes */
|
||||
(void) posix_spawn_file_actions_destroy(&actions);
|
||||
(void) posix_spawnattr_destroy(&attrs);
|
||||
|
||||
error:
|
||||
/* In an error situation, the attributes and file actions are
|
||||
* already destroyed, only the happy path requires cleanup */
|
||||
return UV__ERR(err);
|
||||
}
|
||||
#endif
|
||||
|
||||
static int uv__spawn_and_init_child_fork(const uv_process_options_t* options,
|
||||
int stdio_count,
|
||||
int (*pipes)[2],
|
||||
int error_fd,
|
||||
pid_t* pid) {
|
||||
sigset_t signewset;
|
||||
sigset_t sigoldset;
|
||||
|
||||
/* Start the child with most signals blocked, to avoid any issues before we
|
||||
* can reset them, but allow program failures to exit (and not hang). */
|
||||
sigfillset(&signewset);
|
||||
sigdelset(&signewset, SIGKILL);
|
||||
sigdelset(&signewset, SIGSTOP);
|
||||
sigdelset(&signewset, SIGTRAP);
|
||||
sigdelset(&signewset, SIGSEGV);
|
||||
sigdelset(&signewset, SIGBUS);
|
||||
sigdelset(&signewset, SIGILL);
|
||||
sigdelset(&signewset, SIGSYS);
|
||||
sigdelset(&signewset, SIGABRT);
|
||||
if (pthread_sigmask(SIG_BLOCK, &signewset, &sigoldset) != 0)
|
||||
abort();
|
||||
|
||||
*pid = fork();
|
||||
|
||||
if (*pid == -1) {
|
||||
/* Failed to fork */
|
||||
return UV__ERR(errno);
|
||||
}
|
||||
|
||||
if (*pid == 0) {
|
||||
/* Fork succeeded, in the child process */
|
||||
uv__process_child_init(options, stdio_count, pipes, error_fd);
|
||||
abort();
|
||||
}
|
||||
|
||||
if (pthread_sigmask(SIG_SETMASK, &sigoldset, NULL) != 0)
|
||||
abort();
|
||||
|
||||
/* Fork succeeded, in the parent process */
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int uv__spawn_and_init_child(
|
||||
uv_loop_t* loop,
|
||||
const uv_process_options_t* options,
|
||||
int stdio_count,
|
||||
int (*pipes)[2],
|
||||
pid_t* pid) {
|
||||
int signal_pipe[2] = { -1, -1 };
|
||||
int status;
|
||||
int err;
|
||||
int exec_errorno;
|
||||
ssize_t r;
|
||||
|
||||
#if defined(__APPLE__)
|
||||
uv_once(&posix_spawn_init_once, uv__spawn_init_posix_spawn);
|
||||
|
||||
/* Special child process spawn case for macOS Big Sur (11.0) onwards
|
||||
*
|
||||
* Big Sur introduced a significant performance degradation on a call to
|
||||
* fork/exec when the process has many pages mmaped in with MAP_JIT, like, say
|
||||
* a javascript interpreter. Electron-based applications, for example,
|
||||
* are impacted; though the magnitude of the impact depends on how much the
|
||||
* app relies on subprocesses.
|
||||
*
|
||||
* On macOS, though, posix_spawn is implemented in a way that does not
|
||||
* exhibit the problem. This block implements the forking and preparation
|
||||
* logic with posix_spawn and its related primitives. It also takes advantage of
|
||||
* the macOS extension POSIX_SPAWN_CLOEXEC_DEFAULT that makes impossible to
|
||||
* leak descriptors to the child process. */
|
||||
err = uv__spawn_and_init_child_posix_spawn(options,
|
||||
stdio_count,
|
||||
pipes,
|
||||
pid,
|
||||
&posix_spawn_fncs);
|
||||
|
||||
/* The posix_spawn flow will return UV_ENOSYS if any of the posix_spawn_x_np
|
||||
* non-standard functions is both _needed_ and _undefined_. In those cases,
|
||||
* default back to the fork/execve strategy. For all other errors, just fail. */
|
||||
if (err != UV_ENOSYS)
|
||||
return err;
|
||||
|
||||
#endif
|
||||
|
||||
/* This pipe is used by the parent to wait until
|
||||
* the child has called `execve()`. We need this
|
||||
* to avoid the following race condition:
|
||||
*
|
||||
* if ((pid = fork()) > 0) {
|
||||
* kill(pid, SIGTERM);
|
||||
* }
|
||||
* else if (pid == 0) {
|
||||
* execve("/bin/cat", argp, envp);
|
||||
* }
|
||||
*
|
||||
* The parent sends a signal immediately after forking.
|
||||
* Since the child may not have called `execve()` yet,
|
||||
* there is no telling what process receives the signal,
|
||||
* our fork or /bin/cat.
|
||||
*
|
||||
* To avoid ambiguity, we create a pipe with both ends
|
||||
* marked close-on-exec. Then, after the call to `fork()`,
|
||||
* the parent polls the read end until it EOFs or errors with EPIPE.
|
||||
*/
|
||||
err = uv__make_pipe(signal_pipe, 0);
|
||||
if (err)
|
||||
return err;
|
||||
|
||||
/* Acquire write lock to prevent opening new fds in worker threads */
|
||||
uv_rwlock_wrlock(&loop->cloexec_lock);
|
||||
|
||||
err = uv__spawn_and_init_child_fork(options, stdio_count, pipes, signal_pipe[1], pid);
|
||||
|
||||
/* Release lock in parent process */
|
||||
uv_rwlock_wrunlock(&loop->cloexec_lock);
|
||||
|
||||
uv__close(signal_pipe[1]);
|
||||
|
||||
if (err == 0) {
|
||||
do
|
||||
r = read(signal_pipe[0], &exec_errorno, sizeof(exec_errorno));
|
||||
while (r == -1 && errno == EINTR);
|
||||
|
||||
if (r == 0)
|
||||
; /* okay, EOF */
|
||||
else if (r == sizeof(exec_errorno)) {
|
||||
do
|
||||
err = waitpid(*pid, &status, 0); /* okay, read errorno */
|
||||
while (err == -1 && errno == EINTR);
|
||||
assert(err == *pid);
|
||||
err = exec_errorno;
|
||||
} else if (r == -1 && errno == EPIPE) {
|
||||
/* Something unknown happened to our child before spawn */
|
||||
do
|
||||
err = waitpid(*pid, &status, 0); /* okay, got EPIPE */
|
||||
while (err == -1 && errno == EINTR);
|
||||
assert(err == *pid);
|
||||
err = UV_EPIPE;
|
||||
} else
|
||||
abort();
|
||||
}
|
||||
|
||||
uv__close_nocheckstdio(signal_pipe[0]);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
int uv_spawn(uv_loop_t* loop,
|
||||
uv_process_t* process,
|
||||
const uv_process_options_t* options) {
|
||||
@ -348,18 +936,13 @@ int uv_spawn(uv_loop_t* loop,
|
||||
/* fork is marked __WATCHOS_PROHIBITED __TVOS_PROHIBITED. */
|
||||
return UV_ENOSYS;
|
||||
#else
|
||||
sigset_t signewset;
|
||||
sigset_t sigoldset;
|
||||
int signal_pipe[2] = { -1, -1 };
|
||||
int pipes_storage[8][2];
|
||||
int (*pipes)[2];
|
||||
int stdio_count;
|
||||
ssize_t r;
|
||||
pid_t pid;
|
||||
int err;
|
||||
int exec_errorno;
|
||||
int i;
|
||||
int status;
|
||||
|
||||
assert(options->file != NULL);
|
||||
assert(!(options->flags & ~(UV_PROCESS_DETACHED |
|
||||
@ -372,6 +955,7 @@ int uv_spawn(uv_loop_t* loop,
|
||||
|
||||
uv__handle_init(loop, (uv_handle_t*)process, UV_PROCESS);
|
||||
QUEUE_INIT(&process->queue);
|
||||
process->status = 0;
|
||||
|
||||
stdio_count = options->stdio_count;
|
||||
if (stdio_count < 3)
|
||||
@ -396,92 +980,42 @@ int uv_spawn(uv_loop_t* loop,
|
||||
goto error;
|
||||
}
|
||||
|
||||
/* This pipe is used by the parent to wait until
|
||||
* the child has called `execve()`. We need this
|
||||
* to avoid the following race condition:
|
||||
*
|
||||
* if ((pid = fork()) > 0) {
|
||||
* kill(pid, SIGTERM);
|
||||
* }
|
||||
* else if (pid == 0) {
|
||||
* execve("/bin/cat", argp, envp);
|
||||
* }
|
||||
*
|
||||
* The parent sends a signal immediately after forking.
|
||||
* Since the child may not have called `execve()` yet,
|
||||
* there is no telling what process receives the signal,
|
||||
* our fork or /bin/cat.
|
||||
*
|
||||
* To avoid ambiguity, we create a pipe with both ends
|
||||
* marked close-on-exec. Then, after the call to `fork()`,
|
||||
* the parent polls the read end until it EOFs or errors with EPIPE.
|
||||
*/
|
||||
err = uv__make_pipe(signal_pipe, 0);
|
||||
if (err)
|
||||
goto error;
|
||||
|
||||
#if !(defined(__APPLE__) || defined(__DragonFly__) || defined(__FreeBSD__) || defined(__NetBSD__))
|
||||
uv_signal_start(&loop->child_watcher, uv__chld, SIGCHLD);
|
||||
#endif
|
||||
|
||||
/* Acquire write lock to prevent opening new fds in worker threads */
|
||||
uv_rwlock_wrlock(&loop->cloexec_lock);
|
||||
/* Spawn the child */
|
||||
exec_errorno = uv__spawn_and_init_child(loop, options, stdio_count, pipes, &pid);
|
||||
|
||||
/* Start the child with most signals blocked, to avoid any issues before we
|
||||
* can reset them, but allow program failures to exit (and not hang). */
|
||||
sigfillset(&signewset);
|
||||
sigdelset(&signewset, SIGKILL);
|
||||
sigdelset(&signewset, SIGSTOP);
|
||||
sigdelset(&signewset, SIGTRAP);
|
||||
sigdelset(&signewset, SIGSEGV);
|
||||
sigdelset(&signewset, SIGBUS);
|
||||
sigdelset(&signewset, SIGILL);
|
||||
sigdelset(&signewset, SIGSYS);
|
||||
sigdelset(&signewset, SIGABRT);
|
||||
if (pthread_sigmask(SIG_BLOCK, &signewset, &sigoldset) != 0)
|
||||
abort();
|
||||
#if 0
|
||||
/* This runs into a nodejs issue (it expects initialized streams, even if the
|
||||
* exec failed).
|
||||
* See https://github.com/libuv/libuv/pull/3107#issuecomment-782482608 */
|
||||
if (exec_errorno != 0)
|
||||
goto error;
|
||||
#endif
|
||||
|
||||
pid = fork();
|
||||
if (pid == -1)
|
||||
err = UV__ERR(errno);
|
||||
/* Activate this handle if exec() happened successfully, even if we later
|
||||
* fail to open a stdio handle. This ensures we can eventually reap the child
|
||||
* with waitpid. */
|
||||
if (exec_errorno == 0) {
|
||||
#if defined(__APPLE__) || defined(__DragonFly__) || defined(__FreeBSD__) || defined(__NetBSD__)
|
||||
struct kevent event;
|
||||
EV_SET(&event, pid, EVFILT_PROC, EV_ADD | EV_ONESHOT, NOTE_EXIT, 0, 0);
|
||||
if (kevent(loop->backend_fd, &event, 1, NULL, 0, NULL)) {
|
||||
if (errno != ESRCH)
|
||||
abort();
|
||||
/* Process already exited. Call waitpid on the next loop iteration. */
|
||||
loop->flags |= UV_LOOP_REAP_CHILDREN;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (pid == 0)
|
||||
uv__process_child_init(options, stdio_count, pipes, signal_pipe[1]);
|
||||
|
||||
if (pthread_sigmask(SIG_SETMASK, &sigoldset, NULL) != 0)
|
||||
abort();
|
||||
|
||||
/* Release lock in parent process */
|
||||
uv_rwlock_wrunlock(&loop->cloexec_lock);
|
||||
|
||||
uv__close(signal_pipe[1]);
|
||||
|
||||
if (pid == -1) {
|
||||
uv__close(signal_pipe[0]);
|
||||
goto error;
|
||||
process->pid = pid;
|
||||
process->exit_cb = options->exit_cb;
|
||||
QUEUE_INSERT_TAIL(&loop->process_handles, &process->queue);
|
||||
uv__handle_start(process);
|
||||
}
|
||||
|
||||
process->status = 0;
|
||||
exec_errorno = 0;
|
||||
do
|
||||
r = read(signal_pipe[0], &exec_errorno, sizeof(exec_errorno));
|
||||
while (r == -1 && errno == EINTR);
|
||||
|
||||
if (r == 0)
|
||||
; /* okay, EOF */
|
||||
else if (r == sizeof(exec_errorno)) {
|
||||
do
|
||||
err = waitpid(pid, &status, 0); /* okay, read errorno */
|
||||
while (err == -1 && errno == EINTR);
|
||||
assert(err == pid);
|
||||
} else if (r == -1 && errno == EPIPE) {
|
||||
do
|
||||
err = waitpid(pid, &status, 0); /* okay, got EPIPE */
|
||||
while (err == -1 && errno == EINTR);
|
||||
assert(err == pid);
|
||||
} else
|
||||
abort();
|
||||
|
||||
uv__close_nocheckstdio(signal_pipe[0]);
|
||||
|
||||
for (i = 0; i < options->stdio_count; i++) {
|
||||
err = uv__process_open_stream(options->stdio + i, pipes[i]);
|
||||
if (err == 0)
|
||||
@ -493,15 +1027,6 @@ int uv_spawn(uv_loop_t* loop,
|
||||
goto error;
|
||||
}
|
||||
|
||||
/* Only activate this handle if exec() happened successfully */
|
||||
if (exec_errorno == 0) {
|
||||
QUEUE_INSERT_TAIL(&loop->process_handles, &process->queue);
|
||||
uv__handle_start(process);
|
||||
}
|
||||
|
||||
process->pid = pid;
|
||||
process->exit_cb = options->exit_cb;
|
||||
|
||||
if (pipes != pipes_storage)
|
||||
uv__free(pipes);
|
||||
|
||||
|
38
deps/libuv/src/unix/stream.c
vendored
38
deps/libuv/src/unix/stream.c
vendored
@ -58,20 +58,6 @@ struct uv__stream_select_s {
|
||||
fd_set* swrite;
|
||||
size_t swrite_sz;
|
||||
};
|
||||
|
||||
/* Due to a possible kernel bug at least in OS X 10.10 "Yosemite",
|
||||
* EPROTOTYPE can be returned while trying to write to a socket that is
|
||||
* shutting down. If we retry the write, we should get the expected EPIPE
|
||||
* instead.
|
||||
*/
|
||||
# define RETRY_ON_WRITE_ERROR(errno) (errno == EINTR || errno == EPROTOTYPE)
|
||||
# define IS_TRANSIENT_WRITE_ERROR(errno, send_handle) \
|
||||
(errno == EAGAIN || errno == EWOULDBLOCK || errno == ENOBUFS || \
|
||||
(errno == EMSGSIZE && send_handle != NULL))
|
||||
#else
|
||||
# define RETRY_ON_WRITE_ERROR(errno) (errno == EINTR)
|
||||
# define IS_TRANSIENT_WRITE_ERROR(errno, send_handle) \
|
||||
(errno == EAGAIN || errno == EWOULDBLOCK || errno == ENOBUFS)
|
||||
#endif /* defined(__APPLE__) */
|
||||
|
||||
static void uv__stream_connect(uv_stream_t*);
|
||||
@ -658,11 +644,11 @@ int uv_listen(uv_stream_t* stream, int backlog, uv_connection_cb cb) {
|
||||
|
||||
switch (stream->type) {
|
||||
case UV_TCP:
|
||||
err = uv_tcp_listen((uv_tcp_t*)stream, backlog, cb);
|
||||
err = uv__tcp_listen((uv_tcp_t*)stream, backlog, cb);
|
||||
break;
|
||||
|
||||
case UV_NAMED_PIPE:
|
||||
err = uv_pipe_listen((uv_pipe_t*)stream, backlog, cb);
|
||||
err = uv__pipe_listen((uv_pipe_t*)stream, backlog, cb);
|
||||
break;
|
||||
|
||||
default:
|
||||
@ -866,19 +852,33 @@ static int uv__try_write(uv_stream_t* stream,
|
||||
|
||||
do
|
||||
n = sendmsg(uv__stream_fd(stream), &msg, 0);
|
||||
while (n == -1 && RETRY_ON_WRITE_ERROR(errno));
|
||||
while (n == -1 && errno == EINTR);
|
||||
} else {
|
||||
do
|
||||
n = uv__writev(uv__stream_fd(stream), iov, iovcnt);
|
||||
while (n == -1 && RETRY_ON_WRITE_ERROR(errno));
|
||||
while (n == -1 && errno == EINTR);
|
||||
}
|
||||
|
||||
if (n >= 0)
|
||||
return n;
|
||||
|
||||
if (IS_TRANSIENT_WRITE_ERROR(errno, send_handle))
|
||||
if (errno == EAGAIN || errno == EWOULDBLOCK || errno == ENOBUFS)
|
||||
return UV_EAGAIN;
|
||||
|
||||
#ifdef __APPLE__
|
||||
/* macOS versions 10.10 and 10.15 - and presumbaly 10.11 to 10.14, too -
|
||||
* have a bug where a race condition causes the kernel to return EPROTOTYPE
|
||||
* because the socket isn't fully constructed. It's probably the result of
|
||||
* the peer closing the connection and that is why libuv translates it to
|
||||
* ECONNRESET. Previously, libuv retried until the EPROTOTYPE error went
|
||||
* away but some VPN software causes the same behavior except the error is
|
||||
* permanent, not transient, turning the retry mechanism into an infinite
|
||||
* loop. See https://github.com/libuv/libuv/pull/482.
|
||||
*/
|
||||
if (errno == EPROTOTYPE)
|
||||
return UV_ECONNRESET;
|
||||
#endif /* __APPLE__ */
|
||||
|
||||
return UV__ERR(errno);
|
||||
}
|
||||
|
||||
|
2
deps/libuv/src/unix/tcp.c
vendored
2
deps/libuv/src/unix/tcp.c
vendored
@ -328,7 +328,7 @@ int uv_tcp_close_reset(uv_tcp_t* handle, uv_close_cb close_cb) {
|
||||
}
|
||||
|
||||
|
||||
int uv_tcp_listen(uv_tcp_t* tcp, int backlog, uv_connection_cb cb) {
|
||||
int uv__tcp_listen(uv_tcp_t* tcp, int backlog, uv_connection_cb cb) {
|
||||
static int single_accept_cached = -1;
|
||||
unsigned long flags;
|
||||
int single_accept;
|
||||
|
84
deps/libuv/src/unix/thread.c
vendored
84
deps/libuv/src/unix/thread.c
vendored
@ -162,11 +162,45 @@ void uv_barrier_destroy(uv_barrier_t* barrier) {
|
||||
#endif
|
||||
|
||||
|
||||
/* Musl's PTHREAD_STACK_MIN is 2 KB on all architectures, which is
|
||||
* too small to safely receive signals on.
|
||||
*
|
||||
* Musl's PTHREAD_STACK_MIN + MINSIGSTKSZ == 8192 on arm64 (which has
|
||||
* the largest MINSIGSTKSZ of the architectures that musl supports) so
|
||||
* let's use that as a lower bound.
|
||||
*
|
||||
* We use a hardcoded value because PTHREAD_STACK_MIN + MINSIGSTKSZ
|
||||
* is between 28 and 133 KB when compiling against glibc, depending
|
||||
* on the architecture.
|
||||
*/
|
||||
static size_t uv__min_stack_size(void) {
|
||||
static const size_t min = 8192;
|
||||
|
||||
#ifdef PTHREAD_STACK_MIN /* Not defined on NetBSD. */
|
||||
if (min < (size_t) PTHREAD_STACK_MIN)
|
||||
return PTHREAD_STACK_MIN;
|
||||
#endif /* PTHREAD_STACK_MIN */
|
||||
|
||||
return min;
|
||||
}
|
||||
|
||||
|
||||
/* On Linux, threads created by musl have a much smaller stack than threads
|
||||
* created by glibc (80 vs. 2048 or 4096 kB.) Follow glibc for consistency.
|
||||
*/
|
||||
static size_t uv__default_stack_size(void) {
|
||||
#if !defined(__linux__)
|
||||
return 0;
|
||||
#elif defined(__PPC__) || defined(__ppc__) || defined(__powerpc__)
|
||||
return 4 << 20; /* glibc default. */
|
||||
#else
|
||||
return 2 << 20; /* glibc default. */
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
/* On MacOS, threads other than the main thread are created with a reduced
|
||||
* stack size by default. Adjust to RLIMIT_STACK aligned to the page size.
|
||||
*
|
||||
* On Linux, threads created by musl have a much smaller stack than threads
|
||||
* created by glibc (80 vs. 2048 or 4096 kB.) Follow glibc for consistency.
|
||||
*/
|
||||
size_t uv__thread_stack_size(void) {
|
||||
#if defined(__APPLE__) || defined(__linux__)
|
||||
@ -176,34 +210,20 @@ size_t uv__thread_stack_size(void) {
|
||||
* the system call wrapper invokes the wrong system call. Don't treat
|
||||
* that as fatal, just use the default stack size instead.
|
||||
*/
|
||||
if (0 == getrlimit(RLIMIT_STACK, &lim) && lim.rlim_cur != RLIM_INFINITY) {
|
||||
/* pthread_attr_setstacksize() expects page-aligned values. */
|
||||
lim.rlim_cur -= lim.rlim_cur % (rlim_t) getpagesize();
|
||||
if (getrlimit(RLIMIT_STACK, &lim))
|
||||
return uv__default_stack_size();
|
||||
|
||||
/* Musl's PTHREAD_STACK_MIN is 2 KB on all architectures, which is
|
||||
* too small to safely receive signals on.
|
||||
*
|
||||
* Musl's PTHREAD_STACK_MIN + MINSIGSTKSZ == 8192 on arm64 (which has
|
||||
* the largest MINSIGSTKSZ of the architectures that musl supports) so
|
||||
* let's use that as a lower bound.
|
||||
*
|
||||
* We use a hardcoded value because PTHREAD_STACK_MIN + MINSIGSTKSZ
|
||||
* is between 28 and 133 KB when compiling against glibc, depending
|
||||
* on the architecture.
|
||||
*/
|
||||
if (lim.rlim_cur >= 8192)
|
||||
if (lim.rlim_cur >= PTHREAD_STACK_MIN)
|
||||
return lim.rlim_cur;
|
||||
}
|
||||
if (lim.rlim_cur == RLIM_INFINITY)
|
||||
return uv__default_stack_size();
|
||||
|
||||
/* pthread_attr_setstacksize() expects page-aligned values. */
|
||||
lim.rlim_cur -= lim.rlim_cur % (rlim_t) getpagesize();
|
||||
|
||||
if (lim.rlim_cur >= (rlim_t) uv__min_stack_size())
|
||||
return lim.rlim_cur;
|
||||
#endif
|
||||
|
||||
#if !defined(__linux__)
|
||||
return 0;
|
||||
#elif defined(__PPC__) || defined(__ppc__) || defined(__powerpc__)
|
||||
return 4 << 20; /* glibc default. */
|
||||
#else
|
||||
return 2 << 20; /* glibc default. */
|
||||
#endif
|
||||
return uv__default_stack_size();
|
||||
}
|
||||
|
||||
|
||||
@ -222,6 +242,7 @@ int uv_thread_create_ex(uv_thread_t* tid,
|
||||
pthread_attr_t attr_storage;
|
||||
size_t pagesize;
|
||||
size_t stack_size;
|
||||
size_t min_stack_size;
|
||||
|
||||
/* Used to squelch a -Wcast-function-type warning. */
|
||||
union {
|
||||
@ -239,10 +260,9 @@ int uv_thread_create_ex(uv_thread_t* tid,
|
||||
pagesize = (size_t)getpagesize();
|
||||
/* Round up to the nearest page boundary. */
|
||||
stack_size = (stack_size + pagesize - 1) &~ (pagesize - 1);
|
||||
#ifdef PTHREAD_STACK_MIN
|
||||
if (stack_size < PTHREAD_STACK_MIN)
|
||||
stack_size = PTHREAD_STACK_MIN;
|
||||
#endif
|
||||
min_stack_size = uv__min_stack_size();
|
||||
if (stack_size < min_stack_size)
|
||||
stack_size = min_stack_size;
|
||||
}
|
||||
|
||||
if (stack_size > 0) {
|
||||
|
19
deps/libuv/src/unix/udp.c
vendored
19
deps/libuv/src/unix/udp.c
vendored
@ -201,6 +201,7 @@ static int uv__udp_recvmmsg(uv_udp_t* handle, uv_buf_t* buf) {
|
||||
for (k = 0; k < chunks; ++k) {
|
||||
iov[k].iov_base = buf->base + k * UV__UDP_DGRAM_MAXSIZE;
|
||||
iov[k].iov_len = UV__UDP_DGRAM_MAXSIZE;
|
||||
memset(&msgs[k].msg_hdr, 0, sizeof(msgs[k].msg_hdr));
|
||||
msgs[k].msg_hdr.msg_iov = iov + k;
|
||||
msgs[k].msg_hdr.msg_iovlen = 1;
|
||||
msgs[k].msg_hdr.msg_name = peers + k;
|
||||
@ -494,7 +495,7 @@ static int uv__set_reuse(int fd) {
|
||||
if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &yes, sizeof(yes)))
|
||||
return UV__ERR(errno);
|
||||
}
|
||||
#elif defined(SO_REUSEPORT) && !defined(__linux__)
|
||||
#elif defined(SO_REUSEPORT) && !defined(__linux__) && !defined(__GNU__)
|
||||
if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &yes, sizeof(yes)))
|
||||
return UV__ERR(errno);
|
||||
#else
|
||||
@ -655,16 +656,16 @@ int uv__udp_connect(uv_udp_t* handle,
|
||||
}
|
||||
|
||||
/* From https://pubs.opengroup.org/onlinepubs/9699919799/functions/connect.html
|
||||
* Any of uv supported UNIXs kernel should be standardized, but the kernel
|
||||
* Any of uv supported UNIXs kernel should be standardized, but the kernel
|
||||
* implementation logic not same, let's use pseudocode to explain the udp
|
||||
* disconnect behaviors:
|
||||
*
|
||||
*
|
||||
* Predefined stubs for pseudocode:
|
||||
* 1. sodisconnect: The function to perform the real udp disconnect
|
||||
* 2. pru_connect: The function to perform the real udp connect
|
||||
* 3. so: The kernel object match with socket fd
|
||||
* 4. addr: The sockaddr parameter from user space
|
||||
*
|
||||
*
|
||||
* BSDs:
|
||||
* if(sodisconnect(so) == 0) { // udp disconnect succeed
|
||||
* if (addr->sa_len != so->addr->sa_len) return EINVAL;
|
||||
@ -694,13 +695,13 @@ int uv__udp_disconnect(uv_udp_t* handle) {
|
||||
#endif
|
||||
|
||||
memset(&addr, 0, sizeof(addr));
|
||||
|
||||
|
||||
#if defined(__MVS__)
|
||||
addr.ss_family = AF_UNSPEC;
|
||||
#else
|
||||
addr.sa_family = AF_UNSPEC;
|
||||
#endif
|
||||
|
||||
|
||||
do {
|
||||
errno = 0;
|
||||
r = connect(handle->io_watcher.fd, (struct sockaddr*) &addr, sizeof(addr));
|
||||
@ -927,7 +928,8 @@ static int uv__udp_set_membership6(uv_udp_t* handle,
|
||||
!defined(__NetBSD__) && \
|
||||
!defined(__ANDROID__) && \
|
||||
!defined(__DragonFly__) && \
|
||||
!defined(__QNX__)
|
||||
!defined(__QNX__) && \
|
||||
!defined(__GNU__)
|
||||
static int uv__udp_set_source_membership4(uv_udp_t* handle,
|
||||
const struct sockaddr_in* multicast_addr,
|
||||
const char* interface_addr,
|
||||
@ -1119,7 +1121,8 @@ int uv_udp_set_source_membership(uv_udp_t* handle,
|
||||
!defined(__NetBSD__) && \
|
||||
!defined(__ANDROID__) && \
|
||||
!defined(__DragonFly__) && \
|
||||
!defined(__QNX__)
|
||||
!defined(__QNX__) && \
|
||||
!defined(__GNU__)
|
||||
int err;
|
||||
union uv__sockaddr mcast_addr;
|
||||
union uv__sockaddr src_addr;
|
||||
|
Reference in New Issue
Block a user