Browse Source

Do not inherit open file descriptors on fork() in MacEthernetTapAgent

Adam Ierymenko 4 years ago
parent
commit
d2974f2e60
1 changed files with 11 additions and 0 deletions
  1. 11 0
      osdep/MacEthernetTapAgent.c

+ 11 - 0
osdep/MacEthernetTapAgent.c

@@ -64,6 +64,7 @@
 #include <sys/ioctl.h>
 #include <sys/socket.h>
 #include <sys/sysctl.h>
+#include <sys/resource.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
 #include <net/bpf.h>
@@ -181,6 +182,14 @@ static void die()
 		run("/sbin/ifconfig",s_peerDeviceName,"destroy",(char *)0);
 }
 
+static inline void close_inherited_fds()
+{
+	struct rlimit lim;
+	getrlimit(RLIMIT_NOFILE, &lim);
+	for (int i=3,j=(int)lim.rlim_cur;i<j;++i)
+		close(i);
+}
+
 int main(int argc,char **argv)
 {
 	char buf[128];
@@ -206,6 +215,8 @@ int main(int argc,char **argv)
 	signal(SIGINT,&exit);
 	signal(SIGPIPE,&exit);
 
+	close_inherited_fds();
+
 	if (getuid() != 0) {
 		if (setuid(0) != 0) {
 			fprintf(stderr,"E must be run as root or with root setuid bit on executable\n");