Changeset

13563:5d4d7ed83f1a

util.pposix: Add pipe() (with support for pipe2() flags on Linux)
author Matthew Wild <mwild1@gmail.com>
date Sat, 16 Nov 2024 12:26:55 +0000
parents 13562:f77c247258cc
children 13564:a350f6332bd8
files util-src/pposix.c
diffstat 1 files changed, 43 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/util-src/pposix.c	Fri Nov 15 15:52:37 2024 +0100
+++ b/util-src/pposix.c	Sat Nov 16 12:26:55 2024 +0000
@@ -13,7 +13,7 @@
 * POSIX support functions for Lua
 */
 
-#define MODULE_VERSION "0.4.0"
+#define MODULE_VERSION "0.4.1"
 
 
 #if defined(__linux__)
@@ -654,6 +654,46 @@
 	return 0;
 }
 
+const char *pipe_flag_names[] = {
+	"cloexec",
+	"direct",
+	"nonblock"
+};
+const int pipe_flag_values[] = {
+	O_CLOEXEC,
+	O_DIRECT,
+	O_NONBLOCK
+};
+
+
+static int lc_pipe(lua_State *L) {
+	int fds[2];
+	int nflags = lua_gettop(L);
+
+#if defined(__linux__)
+	int flags=0;
+	for(int i = 1; i<=nflags; i++) {
+		int flag_index = luaL_checkoption(L, i, NULL, pipe_flag_names);
+		flags |= pipe_flag_values[flag_index];
+	}
+
+	if(pipe2(fds, flags) == -1) {
+#else
+	if(nflags != 0) {
+		luaL_argerror(L, 1, "Flags are not supported on this platform");
+	}
+	if(pipe(fds) == -1) {
+#endif
+		luaL_pushfail(L);
+		lua_pushstring(L, strerror(errno));
+		return 2;
+	}
+
+	lua_pushinteger(L, fds[0]);
+	lua_pushinteger(L, fds[1]);
+	return 2;
+}
+
 static int lc_uname(lua_State *L) {
 	struct utsname uname_info;
 
@@ -870,6 +910,8 @@
 
 		{ "mkdir", lc_mkdir },
 
+		{ "pipe", lc_pipe },
+
 		{ "setrlimit", lc_setrlimit },
 		{ "getrlimit", lc_getrlimit },