Skip to content

Commit

Permalink
pipe: Add pipe_chmod binding
Browse files Browse the repository at this point in the history
Contributes towards luvit#410
  • Loading branch information
squeek502 committed May 11, 2020
1 parent ffe83b1 commit 0017e18
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
14 changes: 14 additions & 0 deletions docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -1663,6 +1663,20 @@ the given type, returned by `uv.pipe_pending_type()` and call

**Returns:** `string`

### `uv.pipe_chmod(pipe, flags)`

> method form `pipe:chmod(flags)`
**Parameters:**
- `pipe`: `uv_pipe_t userdata`
- `flags`: `string`

Alters pipe permissions, allowing it to be accessed from processes run by different users.
Makes the pipe writable or readable by all users. `flags` are: `"r"`, `"w"`, `"rw"`, or `"wr"`
where `r` is `READABLE` and `w` is `WRITABLE`. This function is blocking.

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

## `uv_tty_t` — TTY handle

[`uv_tty_t`]: #uv_tty_t--tty-handle
Expand Down
6 changes: 6 additions & 0 deletions src/luv.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ static const luaL_Reg luv_functions[] = {
{"new_pipe", luv_new_pipe},
{"pipe_open", luv_pipe_open},
{"pipe_bind", luv_pipe_bind},
#if LUV_UV_VERSION_GEQ(1, 16, 0)
{"pipe_chmod", luv_pipe_chmod},
#endif
{"pipe_connect", luv_pipe_connect},
{"pipe_getsockname", luv_pipe_getsockname},
#if LUV_UV_VERSION_GEQ(1, 3, 0)
Expand Down Expand Up @@ -422,6 +425,9 @@ static const luaL_Reg luv_stream_methods[] = {
static const luaL_Reg luv_pipe_methods[] = {
{"open", luv_pipe_open},
{"bind", luv_pipe_bind},
#if LUV_UV_VERSION_GEQ(1, 16, 0)
{"chmod", luv_pipe_chmod},
#endif
{"connect", luv_pipe_connect},
{"getsockname", luv_pipe_getsockname},
#if LUV_UV_VERSION_GEQ(1, 3, 0)
Expand Down
19 changes: 19 additions & 0 deletions src/pipe.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,22 @@ static int luv_pipe_pending_type(lua_State* L) {
lua_pushstring(L, type_name);
return 1;
}

#if LUV_UV_VERSION_GEQ(1,16,0)
static const char *const luv_pipe_chmod_flags[] = {
"r", "w", "rw", "wr", NULL
};

static int luv_pipe_chmod(lua_State* L) {
uv_pipe_t* handle = luv_check_pipe(L, 1);
int flags;
switch (luaL_checkoption(L, 2, NULL, luv_pipe_chmod_flags)) {
case 0: flags = UV_READABLE; break;
case 1: flags = UV_WRITABLE; break;
case 2: case 3: flags = UV_READABLE | UV_WRITABLE; break;
default: flags = 0; /* unreachable */
}
int ret = uv_pipe_chmod(handle, flags);
return luv_result(L, ret);
}
#endif
23 changes: 23 additions & 0 deletions tests/test-pipe.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
local TEST_PIPENAME = "\\\\?\\pipe\\luv-test"

return require('lib/tap')(function (test)
test("pipe chmod", function (print, p, expect, uv)
local pipe = assert(uv.new_pipe())
assert(pipe:bind(TEST_PIPENAME))
local _, err, errname = pipe:chmod("r")
if errname == "EPERM" then
print("Insufficient privileges to alter pipe fmode, skipping")
pipe:close()
return
end
assert(not err, err)
assert(pipe:chmod("w"))
assert(pipe:chmod("rw"))
assert(pipe:chmod("wr"))

local ok = pcall(function() uv.pipe_chmod(pipe, "bad flags") end)
assert(not ok)

pipe:close()
end, "1.16.0")
end)

0 comments on commit 0017e18

Please sign in to comment.