Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update libuv to 1.41.0 #534

Merged
merged 7 commits into from
Apr 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .ci/bindcov.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ for fn in `grep -oP "UV_EXTERN [^\(]+ uv_[^\(]+\(" deps/libuv/include/uv.h | sed
continue
fi
# count all uses
count=`grep -o "$fn" src/*.c | wc -l`
count=`grep -o "\b$fn\b" src/*.c | wc -l`
# check if a luv_ version exists
grep -Fq "l$fn" src/*.c
grep -q "\bl$fn\b" src/*.c
bound=$?
# not bound
if [ ! $bound -eq 0 ] ; then
Expand Down
2 changes: 1 addition & 1 deletion deps/libuv
Submodule libuv updated 68 files
+17 −0 .github/workflows/sanitizer.yml
+2 −0 .mailmap
+12 −1 AUTHORS
+12 −2 CMakeLists.txt
+80 −1 ChangeLog
+2 −5 Makefile.am
+10 −0 README.md
+2 −2 SUPPORTED_PLATFORMS.md
+2 −1 configure.ac
+18 −0 docs/src/pipe.rst
+44 −17 docs/src/poll.rst
+7 −5 docs/src/process.rst
+5 −0 docs/src/stream.rst
+20 −4 docs/src/tcp.rst
+7 −5 docs/src/udp.rst
+12 −3 include/uv.h
+1 −1 include/uv/version.h
+1 −0 src/timer.c
+1 −1 src/unix/async.c
+3 −3 src/unix/bsd-ifaddrs.c
+2 −3 src/unix/core.c
+31 −13 src/unix/fs.c
+0 −6 src/unix/internal.h
+33 −28 src/unix/linux-core.c
+17 −20 src/unix/linux-syscalls.c
+2 −2 src/unix/linux-syscalls.h
+54 −0 src/unix/pipe.c
+9 −0 src/unix/poll.c
+1 −63 src/unix/process.c
+1 −0 src/unix/proctitle.c
+1 −1 src/unix/signal.c
+3 −9 src/unix/stream.c
+51 −2 src/unix/tcp.c
+19 −0 src/uv-common.c
+4 −0 src/uv-common.h
+24 −25 src/win/fs.c
+2 −2 src/win/internal.h
+213 −15 src/win/pipe.c
+0 −96 src/win/process-stdio.c
+3 −10 src/win/stream.c
+137 −6 src/win/tcp.c
+2 −0 test/benchmark-pump.c
+1 −0 test/blackhole-server.c
+3 −2 test/echo-server.c
+27 −9 test/run-tests.c
+22 −18 test/task.h
+17 −13 test/test-close-fd.c
+3 −0 test/test-error.c
+3 −0 test/test-fs-copyfile.c
+3 −0 test/test-fs-event.c
+3 −0 test/test-fs-readdir.c
+108 −42 test/test-fs.c
+2 −2 test/test-getaddrinfo.c
+6 −1 test/test-ipc.c
+21 −9 test/test-list.h
+178 −43 test/test-ping-pong.c
+3 −0 test/test-pipe-connect-error.c
+6 −2 test/test-pipe-getsockname.c
+39 −14 test/test-pipe-set-non-blocking.c
+1 −0 test/test-platform-output.c
+99 −0 test/test-poll-multiple-handles.c
+7 −1 test/test-shutdown-eof.c
+64 −60 test/test-spawn.c
+47 −2 test/test-tcp-bind-error.c
+1 −1 test/test-tcp-connect-timeout.c
+3 −0 test/test-tty.c
+1 −1 test/test-udp-connect.c
+3 −5 tools/make_dist_html.py
78 changes: 78 additions & 0 deletions docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -1609,6 +1609,48 @@ and `uv.tcp_close_reset()` calls is not allowed.

**Returns:** `0` or `fail`

### `uv.socketpair([socktype], [protocol], [flags1], [flags2])`

**Parameters:**
- `socktype`: `string`, `integer` or `nil` (default: `stream`)
- `protocol`: `string`, `integer` or `nil` (default: 0)
- `flags1`: `table` or `nil`
- `nonblock`: `boolean` (default: `false`)
- `flags2`: `table` or `nil`
- `nonblock`: `boolean` (default: `false`)

Create a pair of connected sockets with the specified properties. The resulting handles can be passed to `uv.tcp_open`, used with `uv.spawn`, or for any other purpose.

When specified as a string, `socktype` must be one of `"stream"`, `"dgram"`, `"raw"`,
`"rdm"`, or `"seqpacket"`.

When `protocol` is set to 0 or nil, it will be automatically chosen based on the socket's domain and type. When `protocol` is specified as a string, it will be looked up using the `getprotobyname(3)` function (examples: `"ip"`, `"icmp"`, `"tcp"`, `"udp"`, etc).

Flags:
- `nonblock`: Opens the specified socket handle for `OVERLAPPED` or `FIONBIO`/`O_NONBLOCK` I/O usage. This is recommended for handles that will be used by libuv, and not usually recommended otherwise.

Equivalent to `socketpair(2)` with a domain of `AF_UNIX`.

**Returns:** `table` or `fail`
- `[1, 2]` : `integer` (file descriptor)

```lua
-- Simple read/write with tcp
local fds = uv.socketpair(nil, nil, {nonblock=true}, {nonblock=true})

local sock1 = uv.new_tcp()
sock1:open(fds[1])

local sock2 = uv.new_tcp()
sock2:open(fds[2])

sock1:write("hello")
sock2:read_start(function(err, chunk)
assert(not err, err)
print(chunk)
end)
```

## `uv_pipe_t` — Pipe handle

[`uv_pipe_t`]: #uv_pipe_t--pipe-handle
Expand Down Expand Up @@ -1765,6 +1807,42 @@ where `r` is `READABLE` and `w` is `WRITABLE`. This function is blocking.

**Returns:** `0` or `fail`

### `uv.pipe(read_flags, write_flags)`

**Parameters:**
- `read_flags`: `table` or `nil`
- `nonblock`: `boolean` (default: `false`)
- `write_flags`: `table` or `nil`
- `nonblock`: `boolean` (default: `false`)

Create a pair of connected pipe handles. Data may be written to the `write` fd and read from the `read` fd. The resulting handles can be passed to `pipe_open`, used with `spawn`, or for any other purpose.

Flags:
- `nonblock`: Opens the specified socket handle for `OVERLAPPED` or `FIONBIO`/`O_NONBLOCK` I/O usage. This is recommended for handles that will be used by libuv, and not usually recommended otherwise.

Equivalent to `pipe(2)` with the `O_CLOEXEC` flag set.

**Returns:** `table` or `fail`
- `read` : `integer` (file descriptor)
- `write` : `integer` (file descriptor)

```lua
-- Simple read/write with pipe_open
local fds = uv.pipe({nonblock=true}, {nonblock=true})

local read_pipe = uv.new_pipe()
read_pipe:open(fds.read)

local write_pipe = uv.new_pipe()
write_pipe:open(fds.write)

write_pipe:write("hello")
read_pipe:read_start(function(err, chunk)
assert(not err, err)
print(chunk)
end)
```

## `uv_tty_t` — TTY handle

[`uv_tty_t`]: #uv_tty_t--tty-handle
Expand Down
8 changes: 8 additions & 0 deletions src/constants.c
Original file line number Diff line number Diff line change
Expand Up @@ -667,3 +667,11 @@ static const char* luv_sig_num_to_string(const int num) {
}
return NULL;
}

static int luv_proto_string_to_num(const char* string) {
struct protoent* proto;
if (!string) return -1;
proto = getprotobyname(string);
if (!proto) return -1;
return proto->p_proto;
}
6 changes: 6 additions & 0 deletions src/luv.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ static const luaL_Reg luv_functions[] = {
#if LUV_UV_VERSION_GEQ(1, 32, 0)
{"tcp_close_reset", luv_tcp_close_reset},
#endif
#if LUV_UV_VERSION_GEQ(1, 41, 0)
{"socketpair", luv_socketpair},
#endif

// pipe.c
{"new_pipe", luv_new_pipe},
Expand All @@ -185,6 +188,9 @@ static const luaL_Reg luv_functions[] = {
{"pipe_pending_instances", luv_pipe_pending_instances},
{"pipe_pending_count", luv_pipe_pending_count},
{"pipe_pending_type", luv_pipe_pending_type},
#if LUV_UV_VERSION_GEQ(1, 41, 0)
{"pipe", luv_pipe},
#endif

// tty.c
{"new_tty", luv_new_tty},
Expand Down
32 changes: 32 additions & 0 deletions src/pipe.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,35 @@ static int luv_pipe_chmod(lua_State* L) {
return luv_result(L, ret);
}
#endif

#if LUV_UV_VERSION_GEQ(1,41,0)
static int luv_pipe(lua_State* L) {
luv_ctx_t* ctx = luv_context(L);
int read_flags = 0, write_flags = 0;
uv_file fds[2];
int ret;
if (lua_type(L, 1) == LUA_TTABLE) {
lua_getfield(L, 1, "nonblock");
if (lua_toboolean(L, -1)) read_flags |= UV_NONBLOCK_PIPE;
lua_pop(L, 1);
} else if (!lua_isnoneornil(L, 1)) {
luv_arg_type_error(L, 1, "table or nil expected, got %s");
}
Comment on lines +140 to +146
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to wrap it as a static function to reuse the code.

if (lua_type(L, 2) == LUA_TTABLE) {
lua_getfield(L, 2, "nonblock");
if (lua_toboolean(L, -1)) write_flags |= UV_NONBLOCK_PIPE;
lua_pop(L, 1);
} else if (!lua_isnoneornil(L, 2)) {
luv_arg_type_error(L, 2, "table or nil expected, got %s");
}
ret = uv_pipe(fds, read_flags, write_flags);
if (ret < 0) return luv_error(L, ret);
// return as a table with 'read' and 'write' keys containing the corresponding fds
lua_createtable(L, 0, 2);
lua_pushinteger(L, fds[0]);
lua_setfield(L, -2, "read");
lua_pushinteger(L, fds[1]);
lua_setfield(L, -2, "write");
return 1;
}
#endif
1 change: 1 addition & 0 deletions src/private.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ static int luv_sock_string_to_num(const char* string);
static const char* luv_sock_num_to_string(const int num);
static int luv_sig_string_to_num(const char* string);
static const char* luv_sig_num_to_string(const int num);
static int luv_proto_string_to_num(const char* string);

/* From util.c */
// Push a Libuv error code onto the Lua stack
Expand Down
62 changes: 62 additions & 0 deletions src/tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,65 @@ static int luv_tcp_close_reset(lua_State* L) {
}
#endif

#if LUV_UV_VERSION_GEQ(1, 41, 0)
static int luv_socketpair(lua_State* L) {
int ret;
int socktype = SOCK_STREAM;
int protocol = 0; // 0 = let the protocol be chosen automatically
int flags0 = 0, flags1 = 0;
uv_os_sock_t socks[2];

// socktype
if (lua_isnumber(L, 1)) {
socktype = lua_tointeger(L, 1);
}
else if (lua_isstring(L, 1)) {
socktype = luv_sock_string_to_num(lua_tostring(L, 1));
if (socktype == 0) {
return luaL_argerror(L, 1, lua_pushfstring(L, "invalid socket type: %s", lua_tostring(L, 1)));
}
}
else if (!lua_isnoneornil(L, 1)) {
return luv_arg_type_error(L, 1, "socket type must be string or integer if set, got %s");
}
// protocol
if (lua_isnumber(L, 2)) {
protocol = lua_tointeger(L, 2);
}
else if (lua_isstring(L, 2)) {
protocol = luv_proto_string_to_num(lua_tostring(L, 2));
if (protocol < 0) {
return luaL_argerror(L, 2, lua_pushfstring(L, "invalid protocol: %s", lua_tostring(L, 2)));
}
}
else if (!lua_isnoneornil(L, 2)) {
return luv_arg_type_error(L, 2, "protocol must be string or integer if set, got %s");
}
// flags0
if (lua_type(L, 3) == LUA_TTABLE) {
lua_getfield(L, 3, "nonblock");
if (lua_toboolean(L, -1)) flags0 |= UV_NONBLOCK_PIPE;
lua_pop(L, 1);
} else if (!lua_isnoneornil(L, 3)) {
luv_arg_type_error(L, 3, "table or nil expected, got %s");
}
// flags1
if (lua_type(L, 4) == LUA_TTABLE) {
lua_getfield(L, 4, "nonblock");
if (lua_toboolean(L, -1)) flags1 |= UV_NONBLOCK_PIPE;
lua_pop(L, 1);
} else if (!lua_isnoneornil(L, 4)) {
luv_arg_type_error(L, 4, "table or nil expected, got %s");
}

ret = uv_socketpair(socktype, protocol, socks, flags0, flags1);
if (ret < 0) return luv_error(L, ret);

lua_createtable(L, 2, 0);
lua_pushinteger(L, socks[0]);
lua_rawseti(L, -2, 1);
lua_pushinteger(L, socks[1]);
lua_rawseti(L, -2, 2);
return 1;
}
#endif
75 changes: 75 additions & 0 deletions tests/test-pipe.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,79 @@ return require('lib/tap')(function (test)

pipe:close()
end, "1.16.0")

test("pipe ping pong", function(print, p, expect, uv)
local PING = "PING\n"
local NUM_PINGS = 4

local fds = assert(uv.pipe({nonblock=true}, {nonblock=true}))
assert(uv.guess_handle(fds.read) == "pipe")
assert(uv.guess_handle(fds.write) == "pipe")

local ponger = assert(uv.new_pipe())
assert(ponger:open(fds.read))

local pinger = assert(uv.new_pipe())
assert(pinger:open(fds.write))

local ping = function()
assert(pinger:write(PING, expect(function(err)
assert(not err, err)
end)))
end

local pongs = 0
ping()

assert(ponger:read_start(function(err, chunk)
assert(not err, err)
if chunk == nil then
return
end
assert(chunk == PING)
pongs = pongs + 1
if pongs == NUM_PINGS then
ponger:close()
pinger:close()
else
ping()
end
end))
end, "1.41.0")

test("pipe close fd", function(print, p, expect, uv)
local fds = assert(uv.pipe())

local pipe_handle = assert(uv.new_pipe())
assert(pipe_handle:open(fds.read))
-- pipe_open takes ownership of the file descriptor
fds.read = nil

assert(uv.fs_write(fds.write, "PING"))
assert(uv.fs_close(fds.write))
fds.write = nil

local read_cb_called = 0
local read_cb = expect(function(err, chunk)
assert(not err, err)
read_cb_called = read_cb_called + 1
if read_cb_called == 1 then
assert(chunk == "PING")
pipe_handle:read_stop()
elseif read_cb_called == 2 then
assert(chunk == nil)
pipe_handle:close()
end
end, 2)

assert(pipe_handle:read_start(read_cb))
uv.run()
assert(read_cb_called == 1)

assert(pipe_handle:read_start(read_cb))
uv.run()
assert(read_cb_called == 2)

assert(pipe_handle:is_closing())
end, "1.41.0")
end)
49 changes: 49 additions & 0 deletions tests/test-tcp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -304,4 +304,53 @@ return require('lib/tap')(function (test)
p{socket=socket,req=req}
end)))
end, "1.32.0")

test("socketpair ping pong", function(print, p, expect, uv)
local PING = "PING\n"
local PONG = "PONG\n"
local NUM_PINGS = 4

local fds = assert(uv.socketpair("stream", 0, {nonblock=true}, {nonblock=true}))

local pinger = uv.new_tcp()
pinger:open(fds[2])

local ponger = uv.new_tcp()
ponger:open(fds[1])

local ping = function()
assert(pinger:write(PING, expect(function(err)
assert(not err, err)
end)))
end

local pongs = 0
ping()

assert(pinger:read_start(function(err, chunk)
assert(not err, err)
if chunk == nil then
return
end
assert(chunk == PONG)
pongs = pongs + 1
if pongs == NUM_PINGS then
ponger:close()
pinger:close()
else
ping()
end
end))

assert(ponger:read_start(function(err, chunk)
assert(not err, err)
if chunk == nil then
return
end
assert(chunk == PING)
ponger:write(PONG, expect(function(err)
assert(not err, err)
end))
end))
end, "1.41.0")
end)