Skip to content

Commit 7a7c31f

Browse files
author
Lubos Kardos
committed
Set FD_CLOEXEC on opened files before exec from lua script is called
rhbz:919801
1 parent 816c7cf commit 7a7c31f

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

luaext/lposix.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,10 +335,22 @@ static int Pexec(lua_State *L) /** exec(path,[args]) */
335335
const char *path = luaL_checkstring(L, 1);
336336
int i,n=lua_gettop(L);
337337
char **argv;
338+
int flag, fdno, open_max;
338339

339340
if (!have_forked)
340341
return luaL_error(L, "exec not permitted in this context");
341342

343+
open_max = sysconf(_SC_OPEN_MAX);
344+
if (open_max == -1) {
345+
open_max = 1024;
346+
}
347+
for (fdno = 3; fdno < open_max; fdno++) {
348+
flag = fcntl(fdno, F_GETFD);
349+
if (flag == -1 || (flag & FD_CLOEXEC))
350+
continue;
351+
fcntl(fdno, F_SETFD, FD_CLOEXEC);
352+
}
353+
342354
argv = malloc((n+1)*sizeof(char*));
343355
if (argv==NULL) return luaL_error(L,"not enough memory");
344356
argv[0] = (char*)path;

0 commit comments

Comments
 (0)