Lubos Kardos 0b025e
From 19fe0d9ae12644b8af9513aa2a8cf7d16f7caa61 Mon Sep 17 00:00:00 2001
Lubos Kardos 0b025e
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
Lubos Kardos 0b025e
Date: Fri, 4 Mar 2016 16:40:30 -0500
Lubos Kardos 0b025e
Subject: [PATCH] Add posix.redirect2null
Lubos Kardos 0b025e
Lubos Kardos 0b025e
This is useful to silence output in forked programs:
Lubos Kardos 0b025e
https://bugzilla.redhat.com/show_bug.cgi?id=1287918
Lubos Kardos 0b025e
Lubos Kardos 0b025e
Tested with the following scriptlet:
Lubos Kardos 0b025e
%post -p <lua>
Lubos Kardos 0b025e
pid = posix.fork()
Lubos Kardos 0b025e
if pid == 0 then
Lubos Kardos 0b025e
    assert(posix.exec("/bin/sed"))
Lubos Kardos 0b025e
elseif pid > 0 then
Lubos Kardos 0b025e
    posix.wait(pid)
Lubos Kardos 0b025e
end
Lubos Kardos 0b025e
pid = posix.fork()
Lubos Kardos 0b025e
if pid == 0 then
Lubos Kardos 0b025e
    posix.redirect2null(2)
Lubos Kardos 0b025e
    assert(posix.exec("/bin/awk"))
Lubos Kardos 0b025e
elseif pid > 0 then
Lubos Kardos 0b025e
    posix.wait(pid)
Lubos Kardos 0b025e
end
Lubos Kardos 0b025e
Lubos Kardos 0b025e
As expected, the error message from sed is printed, the error message
Lubos Kardos 0b025e
from awk is not.
Lubos Kardos 0b025e
---
Lubos Kardos 0b025e
 luaext/lposix.c | 20 ++++++++++++++++++++
Lubos Kardos 0b025e
 1 file changed, 20 insertions(+)
Lubos Kardos 0b025e
Lubos Kardos 0b025e
diff --git a/luaext/lposix.c b/luaext/lposix.c
Lubos Kardos 0b025e
index 51ea2b3..c578c5a 100644
Lubos Kardos 0b025e
--- a/luaext/lposix.c
Lubos Kardos 0b025e
+++ b/luaext/lposix.c
Lubos Kardos 0b025e
@@ -819,6 +819,25 @@ static int Pmkstemp(lua_State *L)
Lubos Kardos 0b025e
 	return 2;
Lubos Kardos 0b025e
 }
Lubos Kardos 0b025e
 
Lubos Kardos 0b025e
+static int Predirect2null(lua_State *L)
Lubos Kardos 0b025e
+{
Lubos Kardos 0b025e
+    int target_fd, fd, r, e;
Lubos Kardos 0b025e
+
Lubos Kardos 0b025e
+    if (!have_forked)
Lubos Kardos 0b025e
+	return luaL_error(L, "silence_file_descriptor not permitted in this context");
Lubos Kardos 0b025e
+
Lubos Kardos 0b025e
+    target_fd = luaL_checkinteger(L, 1);
Lubos Kardos 0b025e
+
Lubos Kardos 0b025e
+    r = fd = open("/dev/null", O_WRONLY);
Lubos Kardos 0b025e
+    if (fd >= 0 && fd != target_fd) {
Lubos Kardos 0b025e
+	r = dup2(fd, target_fd);
Lubos Kardos 0b025e
+	e = errno;
Lubos Kardos 0b025e
+	(void) close(fd);
Lubos Kardos 0b025e
+	errno = e;
Lubos Kardos 0b025e
+    }
Lubos Kardos 0b025e
+    return pushresult(L, r, NULL);
Lubos Kardos 0b025e
+}
Lubos Kardos 0b025e
+
Lubos Kardos 0b025e
 static const luaL_Reg R[] =
Lubos Kardos 0b025e
 {
Lubos Kardos 0b025e
 	{"access",		Paccess},
Lubos Kardos 0b025e
@@ -861,6 +880,7 @@ static const luaL_Reg R[] =
Lubos Kardos 0b025e
 	{"wait",		Pwait},
Lubos Kardos 0b025e
 	{"setenv",		Psetenv},
Lubos Kardos 0b025e
 	{"unsetenv",		Punsetenv},
Lubos Kardos 0b025e
+	{"redirect2null",       Predirect2null},
Lubos Kardos 0b025e
 	{NULL,			NULL}
Lubos Kardos 0b025e
 };
Lubos Kardos 0b025e
 
Lubos Kardos 0b025e
-- 
Lubos Kardos 0b025e
1.9.3
Lubos Kardos 0b025e