From 0a66176074560303bf0870957464239e9757d891 Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Tue, 29 May 2018 17:37:05 -0700 Subject: [PATCH 1/3] Factor out and unify setting CLOEXEC Commit 7a7c31f5 ("Set FD_CLOEXEC on opened files before exec from lua script is called") copied the code that sets CLOEXEC flag on all possible file descriptors from lib/rpmscript.c to luaext/lposix.c, essentially creating two copies of the same code (modulo comments and the unused assignment). This commit moves the functionality into its own function, without any code modifications, using the version from luaext/lposix.c. Signed-off-by: Kir Kolyshkin (cherry picked from commit 9c3e5de3240554c8ea1b29d52eeadee4840fefac) --- lib/rpmscript.c | 17 ++--------------- luaext/lposix.c | 13 ++----------- rpmio/rpmio.c | 14 ++++++++++++++ rpmio/rpmio_internal.h | 6 ++++++ 4 files changed, 24 insertions(+), 26 deletions(-) diff --git a/lib/rpmscript.c b/lib/rpmscript.c index 98d3f42..61dff83 100644 --- a/lib/rpmscript.c +++ b/lib/rpmscript.c @@ -18,6 +18,7 @@ #include "rpmio/rpmlua.h" #include "lib/rpmscript.h" +#include "rpmio/rpmio_internal.h" #include "lib/rpmplugins.h" /* rpm plugins hooks */ @@ -161,25 +162,11 @@ static const char * const SCRIPT_PATH = "PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr static void doScriptExec(ARGV_const_t argv, ARGV_const_t prefixes, FD_t scriptFd, FD_t out) { - int flag; - int fdno; int xx; - int open_max; (void) signal(SIGPIPE, SIG_DFL); - /* XXX Force FD_CLOEXEC on all inherited fdno's. */ - open_max = sysconf(_SC_OPEN_MAX); - if (open_max == -1) { - open_max = 1024; - } - for (fdno = 3; fdno < open_max; fdno++) { - flag = fcntl(fdno, F_GETFD); - if (flag == -1 || (flag & FD_CLOEXEC)) - continue; - xx = fcntl(fdno, F_SETFD, FD_CLOEXEC); - /* XXX W2DO? debug msg for inheirited fdno w/o FD_CLOEXEC */ - } + rpmSetCloseOnExec(); if (scriptFd != NULL) { int sfdno = Fileno(scriptFd); diff --git a/luaext/lposix.c b/luaext/lposix.c index 0a7c26c..5d7ad3c 100644 --- a/luaext/lposix.c +++ b/luaext/lposix.c @@ -27,6 +27,7 @@ #include #include #include +#include "rpmio/rpmio_internal.h" #define MYNAME "posix" #define MYVERSION MYNAME " library for " LUA_VERSION " / Nov 2003" @@ -335,21 +336,11 @@ static int Pexec(lua_State *L) /** exec(path,[args]) */ const char *path = luaL_checkstring(L, 1); int i,n=lua_gettop(L); char **argv; - int flag, fdno, open_max; if (!have_forked) return luaL_error(L, "exec not permitted in this context"); - open_max = sysconf(_SC_OPEN_MAX); - if (open_max == -1) { - open_max = 1024; - } - for (fdno = 3; fdno < open_max; fdno++) { - flag = fcntl(fdno, F_GETFD); - if (flag == -1 || (flag & FD_CLOEXEC)) - continue; - fcntl(fdno, F_SETFD, FD_CLOEXEC); - } + rpmSetCloseOnExec(); argv = malloc((n+1)*sizeof(char*)); if (argv==NULL) return luaL_error(L,"not enough memory"); diff --git a/rpmio/rpmio.c b/rpmio/rpmio.c index 0b90984..747bf3c 100644 --- a/rpmio/rpmio.c +++ b/rpmio/rpmio.c @@ -1477,4 +1477,18 @@ void fdFiniDigest(FD_t fd, int hashalgo, } } +void rpmSetCloseOnExec(void) +{ + int flag, fdno, open_max; + open_max = sysconf(_SC_OPEN_MAX); + if (open_max == -1) { + open_max = 1024; + } + for (fdno = 3; fdno < open_max; fdno++) { + flag = fcntl(fdno, F_GETFD); + if (flag == -1 || (flag & FD_CLOEXEC)) + continue; + fcntl(fdno, F_SETFD, FD_CLOEXEC); + } +} diff --git a/rpmio/rpmio_internal.h b/rpmio/rpmio_internal.h index 8c9f1a8..da39250 100644 --- a/rpmio/rpmio_internal.h +++ b/rpmio/rpmio_internal.h @@ -37,6 +37,12 @@ void fdFiniDigest(FD_t fd, int hashalgo, int rpmioSlurp(const char * fn, uint8_t ** bp, ssize_t * blenp); +/** + * Set close-on-exec flag for all opened file descriptors, except + * stdin/stdout/stderr. + */ +void rpmSetCloseOnExec(void); + #ifdef __cplusplus } #endif -- 2.7.4