blob: 6f440c617812d4a44451da2281220a6359378db9 [file] [log] [blame]
Brad Bishop316dfdd2018-06-25 12:45:53 -04001From 9c3e5de3240554c8ea1b29d52eeadee4840fefac Mon Sep 17 00:00:00 2001
2From: Kir Kolyshkin <kolyshkin@gmail.com>
3Date: Tue, 29 May 2018 17:37:05 -0700
4Subject: [PATCH 1/3] Factor out and unify setting CLOEXEC
5
6Commit 7a7c31f5 ("Set FD_CLOEXEC on opened files before exec from
7lua script is called") copied the code that sets CLOEXEC flag on all
8possible file descriptors from lib/rpmscript.c to luaext/lposix.c,
9essentially creating two copies of the same code (modulo comments
10and the unused assignment).
11
12This commit moves the functionality into its own function, without
13any code modifications, using the version from luaext/lposix.c.
14
15Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
16Upstream-Status: Accepted [https://github.com/rpm-software-management/rpm/pull/444]
17Signed-off-by: Peter Kjellerstedt <peter.kjellerstedt@axis.com>
18---
19 lib/rpmscript.c | 18 ++----------------
20 luaext/lposix.c | 13 ++-----------
21 rpmio/rpmio.c | 16 ++++++++++++++++
22 rpmio/rpmio_internal.h | 6 ++++++
23 4 files changed, 26 insertions(+), 27 deletions(-)
24
25diff --git a/lib/rpmscript.c b/lib/rpmscript.c
26index 747385a5b..b4ccd3246 100644
27--- a/lib/rpmscript.c
28+++ b/lib/rpmscript.c
29@@ -3,7 +3,6 @@
30 #include <sys/types.h>
31 #include <sys/wait.h>
32 #include <errno.h>
33-#include <unistd.h>
34
35 #include <rpm/rpmfileutil.h>
36 #include <rpm/rpmmacro.h>
37@@ -14,6 +13,7 @@
38
39 #include "rpmio/rpmlua.h"
40 #include "lib/rpmscript.h"
41+#include "rpmio/rpmio_internal.h"
42
43 #include "lib/rpmplugins.h" /* rpm plugins hooks */
44
45@@ -170,26 +170,12 @@ static const char * const SCRIPT_PATH = "PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr
46 static void doScriptExec(ARGV_const_t argv, ARGV_const_t prefixes,
47 FD_t scriptFd, FD_t out)
48 {
49- int flag;
50- int fdno;
51 int xx;
52- int open_max;
53
54 /* SIGPIPE is ignored in rpm, reset to default for the scriptlet */
55 (void) signal(SIGPIPE, SIG_DFL);
56
57- /* XXX Force FD_CLOEXEC on all inherited fdno's. */
58- open_max = sysconf(_SC_OPEN_MAX);
59- if (open_max == -1) {
60- open_max = 1024;
61- }
62- for (fdno = 3; fdno < open_max; fdno++) {
63- flag = fcntl(fdno, F_GETFD);
64- if (flag == -1 || (flag & FD_CLOEXEC))
65- continue;
66- xx = fcntl(fdno, F_SETFD, FD_CLOEXEC);
67- /* XXX W2DO? debug msg for inheirited fdno w/o FD_CLOEXEC */
68- }
69+ rpmSetCloseOnExec();
70
71 if (scriptFd != NULL) {
72 int sfdno = Fileno(scriptFd);
73diff --git a/luaext/lposix.c b/luaext/lposix.c
74index 0a7c26c71..5d7ad3c87 100644
75--- a/luaext/lposix.c
76+++ b/luaext/lposix.c
77@@ -27,6 +27,7 @@
78 #include <unistd.h>
79 #include <utime.h>
80 #include <rpm/rpmutil.h>
81+#include "rpmio/rpmio_internal.h"
82
83 #define MYNAME "posix"
84 #define MYVERSION MYNAME " library for " LUA_VERSION " / Nov 2003"
85@@ -335,21 +336,11 @@ static int Pexec(lua_State *L) /** exec(path,[args]) */
86 const char *path = luaL_checkstring(L, 1);
87 int i,n=lua_gettop(L);
88 char **argv;
89- int flag, fdno, open_max;
90
91 if (!have_forked)
92 return luaL_error(L, "exec not permitted in this context");
93
94- open_max = sysconf(_SC_OPEN_MAX);
95- if (open_max == -1) {
96- open_max = 1024;
97- }
98- for (fdno = 3; fdno < open_max; fdno++) {
99- flag = fcntl(fdno, F_GETFD);
100- if (flag == -1 || (flag & FD_CLOEXEC))
101- continue;
102- fcntl(fdno, F_SETFD, FD_CLOEXEC);
103- }
104+ rpmSetCloseOnExec();
105
106 argv = malloc((n+1)*sizeof(char*));
107 if (argv==NULL) return luaL_error(L,"not enough memory");
108diff --git a/rpmio/rpmio.c b/rpmio/rpmio.c
109index c7cbc32aa..ea111d2ec 100644
110--- a/rpmio/rpmio.c
111+++ b/rpmio/rpmio.c
112@@ -1759,3 +1759,19 @@ DIGEST_CTX fdDupDigest(FD_t fd, int id)
113
114 return ctx;
115 }
116+
117+void rpmSetCloseOnExec(void)
118+{
119+ int flag, fdno, open_max;
120+
121+ open_max = sysconf(_SC_OPEN_MAX);
122+ if (open_max == -1) {
123+ open_max = 1024;
124+ }
125+ for (fdno = 3; fdno < open_max; fdno++) {
126+ flag = fcntl(fdno, F_GETFD);
127+ if (flag == -1 || (flag & FD_CLOEXEC))
128+ continue;
129+ fcntl(fdno, F_SETFD, FD_CLOEXEC);
130+ }
131+}
132diff --git a/rpmio/rpmio_internal.h b/rpmio/rpmio_internal.h
133index fbed183b0..370cbdc75 100644
134--- a/rpmio/rpmio_internal.h
135+++ b/rpmio/rpmio_internal.h
136@@ -41,6 +41,12 @@ DIGEST_CTX fdDupDigest(FD_t fd, int id);
137 int rpmioSlurp(const char * fn,
138 uint8_t ** bp, ssize_t * blenp);
139
140+/**
141+ * Set close-on-exec flag for all opened file descriptors, except
142+ * stdin/stdout/stderr.
143+ */
144+void rpmSetCloseOnExec(void);
145+
146 #ifdef __cplusplus
147 }
148 #endif