1 From 0a66176074560303bf0870957464239e9757d891 Mon Sep 17 00:00:00 2001
2 From: Kir Kolyshkin <kolyshkin@gmail.com>
3 Date: Tue, 29 May 2018 17:37:05 -0700
4 Subject: [PATCH 1/3] Factor out and unify setting CLOEXEC
6 Commit 7a7c31f5 ("Set FD_CLOEXEC on opened files before exec from
7 lua script is called") copied the code that sets CLOEXEC flag on all
8 possible file descriptors from lib/rpmscript.c to luaext/lposix.c,
9 essentially creating two copies of the same code (modulo comments
10 and the unused assignment).
12 This commit moves the functionality into its own function, without
13 any code modifications, using the version from luaext/lposix.c.
15 Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
16 (cherry picked from commit 9c3e5de3240554c8ea1b29d52eeadee4840fefac)
18 lib/rpmscript.c | 17 ++---------------
19 luaext/lposix.c | 13 ++-----------
20 rpmio/rpmio.c | 14 ++++++++++++++
21 rpmio/rpmio_internal.h | 6 ++++++
22 4 files changed, 24 insertions(+), 26 deletions(-)
24 diff --git a/lib/rpmscript.c b/lib/rpmscript.c
25 index 98d3f42..61dff83 100644
30 #include "rpmio/rpmlua.h"
31 #include "lib/rpmscript.h"
32 +#include "rpmio/rpmio_internal.h"
34 #include "lib/rpmplugins.h" /* rpm plugins hooks */
36 @@ -161,25 +162,11 @@ static const char * const SCRIPT_PATH = "PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr
37 static void doScriptExec(ARGV_const_t argv, ARGV_const_t prefixes,
38 FD_t scriptFd, FD_t out)
45 (void) signal(SIGPIPE, SIG_DFL);
47 - /* XXX Force FD_CLOEXEC on all inherited fdno's. */
48 - open_max = sysconf(_SC_OPEN_MAX);
49 - if (open_max == -1) {
52 - for (fdno = 3; fdno < open_max; fdno++) {
53 - flag = fcntl(fdno, F_GETFD);
54 - if (flag == -1 || (flag & FD_CLOEXEC))
56 - xx = fcntl(fdno, F_SETFD, FD_CLOEXEC);
57 - /* XXX W2DO? debug msg for inheirited fdno w/o FD_CLOEXEC */
59 + rpmSetCloseOnExec();
61 if (scriptFd != NULL) {
62 int sfdno = Fileno(scriptFd);
63 diff --git a/luaext/lposix.c b/luaext/lposix.c
64 index 0a7c26c..5d7ad3c 100644
70 #include <rpm/rpmutil.h>
71 +#include "rpmio/rpmio_internal.h"
73 #define MYNAME "posix"
74 #define MYVERSION MYNAME " library for " LUA_VERSION " / Nov 2003"
75 @@ -335,21 +336,11 @@ static int Pexec(lua_State *L) /** exec(path,[args]) */
76 const char *path = luaL_checkstring(L, 1);
77 int i,n=lua_gettop(L);
79 - int flag, fdno, open_max;
82 return luaL_error(L, "exec not permitted in this context");
84 - open_max = sysconf(_SC_OPEN_MAX);
85 - if (open_max == -1) {
88 - for (fdno = 3; fdno < open_max; fdno++) {
89 - flag = fcntl(fdno, F_GETFD);
90 - if (flag == -1 || (flag & FD_CLOEXEC))
92 - fcntl(fdno, F_SETFD, FD_CLOEXEC);
94 + rpmSetCloseOnExec();
96 argv = malloc((n+1)*sizeof(char*));
97 if (argv==NULL) return luaL_error(L,"not enough memory");
98 diff --git a/rpmio/rpmio.c b/rpmio/rpmio.c
99 index 0b90984..747bf3c 100644
102 @@ -1477,4 +1477,18 @@ void fdFiniDigest(FD_t fd, int hashalgo,
106 +void rpmSetCloseOnExec(void)
108 + int flag, fdno, open_max;
110 + open_max = sysconf(_SC_OPEN_MAX);
111 + if (open_max == -1) {
114 + for (fdno = 3; fdno < open_max; fdno++) {
115 + flag = fcntl(fdno, F_GETFD);
116 + if (flag == -1 || (flag & FD_CLOEXEC))
118 + fcntl(fdno, F_SETFD, FD_CLOEXEC);
121 diff --git a/rpmio/rpmio_internal.h b/rpmio/rpmio_internal.h
122 index 8c9f1a8..da39250 100644
123 --- a/rpmio/rpmio_internal.h
124 +++ b/rpmio/rpmio_internal.h
125 @@ -37,6 +37,12 @@ void fdFiniDigest(FD_t fd, int hashalgo,
126 int rpmioSlurp(const char * fn,
127 uint8_t ** bp, ssize_t * blenp);
130 + * Set close-on-exec flag for all opened file descriptors, except
131 + * stdin/stdout/stderr.
133 +void rpmSetCloseOnExec(void);