Browse Source

FD_CLOEXEC for input files

Serj Kalichev 9 years ago
parent
commit
4c81e49af6
2 changed files with 11 additions and 2 deletions
  1. 6 2
      bin/clish.c
  2. 5 0
      clish/shell/shell_file.c

+ 6 - 2
bin/clish.c

@@ -15,6 +15,7 @@
 #include <string.h>
 #include <unistd.h>
 #include <syslog.h>
+#include <fcntl.h>
 
 #if WITH_INTERNAL_GETOPT
 #include "libc/getopt.h"
@@ -306,8 +307,11 @@ int main(int argc, char **argv)
 			clish_shell_push_file(shell, argv[i], stop_on_error);
 	} else {
 		/* The interactive shell */
-		clish_shell_push_fd(shell, fdopen(dup(fileno(stdin)), "r"),
-			stop_on_error);
+		int tmpfd = dup(fileno(stdin));
+#ifdef FD_CLOEXEC
+		fcntl(tmpfd, F_SETFD, fcntl(tmpfd, F_GETFD) | FD_CLOEXEC);
+#endif
+		clish_shell_push_fd(shell, fdopen(tmpfd, "r"), stop_on_error);
 	}
 
 	/* Execute startup */

+ 5 - 0
clish/shell/shell_file.c

@@ -1,5 +1,7 @@
 #include <stdlib.h>
 #include <assert.h>
+#include <unistd.h>
+#include <fcntl.h>
 
 #include "lub/string.h"
 #include "private.h"
@@ -46,6 +48,9 @@ int clish_shell_push_file(clish_shell_t * this, const char * fname,
 	file = fopen(fname, "r");
 	if (!file)
 		return -1;
+#ifdef FD_CLOEXEC
+       fcntl(fileno(file), F_SETFD, fcntl(fileno(file), F_GETFD) | FD_CLOEXEC);
+#endif
 	res = clish_shell_push(this, file, fname, stop_on_error);
 	if (res)
 		fclose(file);