Fix do_rootfs eats huge time on docker environment
[AGL/meta-agl.git] / meta-agl-profile-core / recipes-devtools / rpm / files / 0001-Factor-out-and-unify-setting-CLOEXEC.patch
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
5
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).
11
12 This commit moves the functionality into its own function, without
13 any code modifications, using the version from luaext/lposix.c.
14
15 Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
16 (cherry picked from commit 9c3e5de3240554c8ea1b29d52eeadee4840fefac)
17 ---
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(-)
23
24 diff --git a/lib/rpmscript.c b/lib/rpmscript.c
25 index 98d3f42..61dff83 100644
26 --- a/lib/rpmscript.c
27 +++ b/lib/rpmscript.c
28 @@ -18,6 +18,7 @@
29  
30  #include "rpmio/rpmlua.h"
31  #include "lib/rpmscript.h"
32 +#include "rpmio/rpmio_internal.h"
33  
34  #include "lib/rpmplugins.h"     /* rpm plugins hooks */
35  
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)
39  {
40 -    int flag;
41 -    int fdno;
42      int xx;
43 -    int open_max;
44  
45      (void) signal(SIGPIPE, SIG_DFL);
46  
47 -    /* XXX Force FD_CLOEXEC on all inherited fdno's. */
48 -    open_max = sysconf(_SC_OPEN_MAX);
49 -    if (open_max == -1) {
50 -       open_max = 1024;
51 -    }
52 -    for (fdno = 3; fdno < open_max; fdno++) {
53 -       flag = fcntl(fdno, F_GETFD);
54 -       if (flag == -1 || (flag & FD_CLOEXEC))
55 -           continue;
56 -       xx = fcntl(fdno, F_SETFD, FD_CLOEXEC);
57 -       /* XXX W2DO? debug msg for inheirited fdno w/o FD_CLOEXEC */
58 -    }
59 +    rpmSetCloseOnExec();
60  
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
65 --- a/luaext/lposix.c
66 +++ b/luaext/lposix.c
67 @@ -27,6 +27,7 @@
68  #include <unistd.h>
69  #include <utime.h>
70  #include <rpm/rpmutil.h>
71 +#include "rpmio/rpmio_internal.h"
72  
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);
78         char **argv;
79 -       int flag, fdno, open_max;
80  
81         if (!have_forked)
82             return luaL_error(L, "exec not permitted in this context");
83  
84 -       open_max = sysconf(_SC_OPEN_MAX);
85 -       if (open_max == -1) {
86 -           open_max = 1024;
87 -       }
88 -       for (fdno = 3; fdno < open_max; fdno++) {
89 -           flag = fcntl(fdno, F_GETFD);
90 -           if (flag == -1 || (flag & FD_CLOEXEC))
91 -               continue;
92 -           fcntl(fdno, F_SETFD, FD_CLOEXEC);
93 -       }
94 +       rpmSetCloseOnExec();
95  
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
100 --- a/rpmio/rpmio.c
101 +++ b/rpmio/rpmio.c
102 @@ -1477,4 +1477,18 @@ void fdFiniDigest(FD_t fd, int hashalgo,
103      }
104  }
105  
106 +void rpmSetCloseOnExec(void)
107 +{
108 +       int flag, fdno, open_max;
109  
110 +       open_max = sysconf(_SC_OPEN_MAX);
111 +       if (open_max == -1) {
112 +               open_max = 1024;
113 +       }
114 +       for (fdno = 3; fdno < open_max; fdno++) {
115 +               flag = fcntl(fdno, F_GETFD);
116 +               if (flag == -1 || (flag & FD_CLOEXEC))
117 +                       continue;
118 +               fcntl(fdno, F_SETFD, FD_CLOEXEC);
119 +       }
120 +}
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);
128  
129 +/**
130 + * Set close-on-exec flag for all opened file descriptors, except
131 + * stdin/stdout/stderr.
132 + */
133 +void rpmSetCloseOnExec(void);
134 +
135  #ifdef __cplusplus
136  }
137  #endif
138 -- 
139 2.7.4
140