summaryrefslogtreecommitdiffstats
path: root/meta-agl-profile-core/recipes-devtools/rpm/files/0001-Factor-out-and-unify-setting-CLOEXEC.patch
blob: cfddec6da3fc7b3fe40fdeb44918bc726f376411 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
From 0a66176074560303bf0870957464239e9757d891 Mon Sep 17 00:00:00 2001
From: Kir Kolyshkin <kolyshkin@gmail.com>
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 <kolyshkin@gmail.com>
(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 <unistd.h>
 #include <utime.h>
 #include <rpm/rpmutil.h>
+#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