Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

uv: upgrade to 3de0411

  • Loading branch information...
bnoordhuis committed Jan 27, 2012
1 parent 85a86b5 commit 14b7c4327f4547bed14bf2059321375b75d9c45e
@@ -70,7 +70,7 @@ ifeq (FreeBSD,$(uname_S))
EV_CONFIG=config_freebsd.h
EIO_CONFIG=config_freebsd.h
CPPFLAGS += -Isrc/ares/config_freebsd
LINKFLAGS+=
LINKFLAGS+=-lkvm
OBJS += src/unix/freebsd.o
OBJS += src/unix/kqueue.o
endif
@@ -115,7 +115,8 @@ typedef intptr_t ssize_t;
XX( 45, EAISOCKTYPE, "") \
XX( 46, ESHUTDOWN, "") \
XX( 47, EEXIST, "file already exists") \
XX( 48, ESRCH, "no such process")
XX( 48, ESRCH, "no such process") \
XX( 49, ENAMETOOLONG, "name too long")


#define UV_ERRNO_GEN(val, name, s) UV_##name = val,
@@ -653,6 +654,20 @@ UV_EXTERN int uv_udp_set_membership(uv_udp_t* handle,
const char* multicast_addr, const char* interface_addr,
uv_membership membership);

/*
* Set IP multicast loop flag. Makes multicast packets loop back to
* local sockets.
*
* Arguments:
* handle UDP handle. Should have been initialized with
* `uv_udp_init`.
* on 1 for on, 0 for off
*
* Returns:
* 0 on success, -1 on error.
*/
UV_EXTERN int uv_udp_set_multicast_loop(uv_udp_t* handle, int on);

/*
* Set the multicast ttl
*
@@ -664,7 +679,7 @@ UV_EXTERN int uv_udp_set_membership(uv_udp_t* handle,
* Returns:
* 0 on success, -1 on error.
*/
int uv_udp_set_multicast_ttl(uv_udp_t* handle, int ttl);
UV_EXTERN int uv_udp_set_multicast_ttl(uv_udp_t* handle, int ttl);

/*
* Set broadcast on or off
@@ -677,7 +692,20 @@ int uv_udp_set_multicast_ttl(uv_udp_t* handle, int ttl);
* Returns:
* 0 on success, -1 on error.
*/
int uv_udp_set_broadcast(uv_udp_t* handle, int on);
UV_EXTERN int uv_udp_set_broadcast(uv_udp_t* handle, int on);

/*
* Set the time to live
*
* Arguments:
* handle UDP handle. Should have been initialized with
* `uv_udp_init`.
* ttl 1 through 255
*
* Returns:
* 0 on success, -1 on error.
*/
UV_EXTERN int uv_udp_set_ttl(uv_udp_t* handle, int ttl);

/*
* Send data. If the socket has not previously been bound with `uv_udp_bind`
@@ -71,6 +71,7 @@ uv_err_code uv_translate_sys_error(int sys_errno) {
case EFAULT: return UV_EFAULT;
case EMFILE: return UV_EMFILE;
case EMSGSIZE: return UV_EMSGSIZE;
case ENAMETOOLONG: return UV_ENAMETOOLONG;
case EINVAL: return UV_EINVAL;
case ECONNREFUSED: return UV_ECONNREFUSED;
case EADDRINUSE: return UV_EADDRINUSE;
@@ -197,10 +197,12 @@ uv_err_t uv_cpu_info(uv_cpu_info_t** cpu_infos, int* count) {
unsigned int ticks = (unsigned int)sysconf(_SC_CLK_TCK),
multiplier = ((uint64_t)1000L / ticks), cpuspeed, maxcpus,
cur = 0;
uv_cpu_info_t* cpu_info;
char model[512];
long* cp_times;
int numcpus;
size_t size;
uv_cpu_info_t* cpu_info;
int i;

size = sizeof(model);
if (sysctlbyname("hw.model", &model, &size, NULL, 0) < 0) {
@@ -223,20 +225,28 @@ uv_err_t uv_cpu_info(uv_cpu_info_t** cpu_infos, int* count) {
free(*cpu_infos);
return uv__new_sys_error(errno);
}
// kern.cp_times on FreeBSD i386 gives an array up to maxcpus instead of ncpu
/* kern.cp_times on FreeBSD i386 gives an array up to maxcpus instead of ncpu */
size = sizeof(maxcpus);
if (sysctlbyname("kern.smp.maxcpus", &maxcpus, &size, NULL, 0) < 0) {
free(*cpu_infos);
return uv__new_sys_error(errno);
}

size = maxcpus * CPUSTATES * sizeof(long);
long cp_times[size];

cp_times = malloc(size);
if (cp_times == NULL) {
free(*cpu_infos);
return uv__new_sys_error(ENOMEM);
}

if (sysctlbyname("kern.cp_times", &cp_times, &size, NULL, 0) < 0) {
free(cp_times);
free(*cpu_infos);
return uv__new_sys_error(errno);
}

for (int i = 0; i < numcpus; i++) {
for (i = 0; i < numcpus; i++) {
cpu_info = &(*cpu_infos)[i];

cpu_info->cpu_times.user = (uint64_t)(cp_times[CP_USER+cur]) * multiplier;
@@ -251,6 +261,7 @@ uv_err_t uv_cpu_info(uv_cpu_info_t** cpu_infos, int* count) {
cur+=CPUSTATES;
}

free(cp_times);
return uv_ok_;
}

@@ -31,6 +31,7 @@
#include <sys/sysctl.h>
#include <sys/types.h>
#include <sys/event.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>

@@ -22,6 +22,7 @@
#include "uv.h"
#include "internal.h"

#include <unistd.h>
#include <assert.h>
#include <errno.h>

@@ -26,6 +26,7 @@
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>


static void uv__udp_run_completed(uv_udp_t* handle);
@@ -343,6 +344,14 @@ static int uv__bind(uv_udp_t* handle,
goto out;
}

#ifdef SO_REUSEPORT /* Apple's version of SO_REUSEADDR... */
yes = 1;
if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &yes, sizeof yes) == -1) {
uv__set_sys_error(handle->loop, errno);
goto out;
}
#endif

if (flags & UV_UDP_IPV6ONLY) {
#ifdef IPV6_V6ONLY
yes = 1;
@@ -512,27 +521,25 @@ int uv_udp_set_membership(uv_udp_t* handle, const char* multicast_addr,
return 0;
}

int uv_udp_set_multicast_ttl(uv_udp_t* handle, int ttl) {
if (setsockopt(handle->fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof ttl) == -1) {
uv__set_sys_error(handle->loop, errno);
return -1;
}

return 0;
}

int uv_udp_set_broadcast(uv_udp_t* handle, int on) {
if (setsockopt(handle->fd, SOL_SOCKET, SO_BROADCAST, &on, sizeof on) == -1) {
uv__set_sys_error(handle->loop, errno);
return -1;
#define X(name, level, option) \
int uv_udp_set_##name(uv_udp_t* handle, int flag) { \
if (setsockopt(handle->fd, level, option, &flag, sizeof(flag))) { \
uv__set_sys_error(handle->loop, errno); \
return -1; \
} \
return 0; \
}

return 0;
}
X(multicast_loop, IPPROTO_IP, IP_MULTICAST_LOOP)
X(multicast_ttl, IPPROTO_IP, IP_MULTICAST_TTL)
X(broadcast, SOL_SOCKET, SO_BROADCAST)
X(ttl, IPPROTO_IP, IP_TTL)

#undef X


int uv_udp_getsockname(uv_udp_t* handle, struct sockaddr* name,
int* namelen) {
int uv_udp_getsockname(uv_udp_t* handle, struct sockaddr* name, int* namelen) {
socklen_t socklen;
int saved_errno;
int rv = 0;
@@ -94,6 +94,7 @@ uv_err_code uv_translate_sys_error(int sys_errno) {
case WSAEMSGSIZE: return UV_EMSGSIZE;
case ERROR_NETWORK_UNREACHABLE: return UV_ENETUNREACH;
case WSAENETUNREACH: return UV_ENETUNREACH;
case WSAENOBUFS: return UV_ENOBUFS;
case ERROR_OUTOFMEMORY: return UV_ENOMEM;
case ERROR_NOT_CONNECTED: return UV_ENOTCONN;
case WSAENOTCONN: return UV_ENOTCONN;
@@ -489,13 +489,134 @@ void fs__readdir(uv_fs_t* req, const wchar_t* path, int flags) {
}


#define IS_SLASH(c) \
((wchar_t) c == L'/' || (wchar_t) c == L'\\')
#define IS_COLON(c) \
((wchar_t) c == L':')
#define IS_LETTER(c) \
((((wchar_t) c >= L'a') && ((wchar_t) c <= L'z')) || \
(((wchar_t) c >= L'A') && ((wchar_t) c <= L'Z')))
#define IS_QUESTION(c) \
((wchar_t) c == L'?')


static int uv__count_slash_separated_words(const wchar_t* pos,
const wchar_t* end,
int limit) {
char last_was_slash = 1, count = 0;

for (; pos < end; pos++) {
if (IS_SLASH(*pos)) {
/* Don't accept double slashes */
if (last_was_slash) {
return 0;
} else {
last_was_slash = 1;
}
} else {
if (last_was_slash) {
/* Found a new word */
count++;
if (count > limit) {
return -1;
}
last_was_slash = 0;
}
}
}

return count;
}

/*
* Returns true if the given path is a root directory. The following patterns
* are recognized:
* \
* c:\ (must have trailing slash)
* \\server\share (trailing slash optional)
* \\?\c: (trailing slash optional)
* \\?\UNC\server\share (trailing slash optional)
*/
static int uv__is_root(const wchar_t* path) {
size_t len = wcslen(path);

/* Test for \ */
if (len == 0 && IS_SLASH(path[0])) {
return 1;
}

if (len < 3) {
return 0;
}

/* Test for c:\ */
if (IS_LETTER(path[0]) && IS_COLON(path[1]) && IS_SLASH(path[2])) {
return 1;
}

if (!IS_SLASH(path[0]) || !IS_SLASH(path[1])) {
return 0;
}

/* Test for \\server\share */
if (!IS_QUESTION(path[2])) {
return uv__count_slash_separated_words(path + 2, path + len, 2) == 2;
}

if (!IS_SLASH(path[3])) {
return 0;
}

if ((len == 6 || len == 7) &&
IS_LETTER(path[4]) && IS_COLON(path[5]) &&
(len == 6 || IS_SLASH(path[6]))) {
return 1;
}

/* Test for \\?\UNC\server\share */
if (len >= 8 &&
(path[4] == L'u' || path[4] == L'U') &&
(path[5] == L'n' || path[5] == L'N') &&
(path[6] == L'c' || path[6] == L'C') &&
IS_SLASH(path[7])) {
return uv__count_slash_separated_words(path + 8, path + len, 2) == 2;
}

return 0;
}


void fs__stat(uv_fs_t* req, const wchar_t* path) {
HANDLE file;
WIN32_FIND_DATAW ent;
int result;

req->ptr = NULL;

if (uv__is_root(path)) {
/* We can't stat root directories like c:\. _wstati64 can't either, but */
/* it will make up something reasonable. */
DWORD drive_type = GetDriveTypeW(path);
if (drive_type == DRIVE_UNKNOWN || drive_type == DRIVE_NO_ROOT_DIR) {
req->last_error = ERROR_PATH_NOT_FOUND;
req->errorno = UV_ENOENT;
req->result = -1;
return;
}

memset(&req->stat, 0, sizeof req->stat);

req->stat.st_nlink = 1;
req->stat.st_mode = ((_S_IREAD|_S_IWRITE) + ((_S_IREAD|_S_IWRITE) >> 3) +
((_S_IREAD|_S_IWRITE) >> 6)) | S_IFDIR;

req->last_error = ERROR_SUCCESS;
req->errorno = UV_OK;
req->result = 0;
req->ptr = &req->stat;
return;
}

file = FindFirstFileExW(path, FindExInfoStandard, &ent,
FindExSearchNameMatch, NULL, 0);

@@ -516,7 +637,7 @@ void fs__stat(uv_fs_t* req, const wchar_t* path) {
if (result != -1) {
req->ptr = &req->stat;
}

SET_REQ_RESULT(req, result);
}

0 comments on commit 14b7c43

Please sign in to comment.
You can’t perform that action at this time.