e83fb19
diff -up dhcp-4.3.0a1/client/clparse.c.cloexec dhcp-4.3.0a1/client/clparse.c
e83fb19
--- dhcp-4.3.0a1/client/clparse.c.cloexec	2013-12-19 15:34:41.638886256 +0100
e83fb19
+++ dhcp-4.3.0a1/client/clparse.c	2013-12-19 15:34:41.657885985 +0100
e83fb19
@@ -253,7 +253,7 @@ int read_client_conf_file (const char *n
bb77af8
 	int token;
bb77af8
 	isc_result_t status;
bb77af8
 
bb77af8
-	if ((file = open (name, O_RDONLY)) < 0)
bb77af8
+	if ((file = open (name, O_RDONLY | O_CLOEXEC)) < 0)
bb77af8
 		return uerr2isc (errno);
bb77af8
 
bb77af8
 	cfile = NULL;
e83fb19
@@ -290,7 +290,7 @@ void read_client_leases ()
bb77af8
 
bb77af8
 	/* Open the lease file.   If we can't open it, just return -
bb77af8
 	   we can safely trust the server to remember our state. */
bb77af8
-	if ((file = open (path_dhclient_db, O_RDONLY)) < 0)
bb77af8
+	if ((file = open (path_dhclient_db, O_RDONLY | O_CLOEXEC)) < 0)
bb77af8
 		return;
bb77af8
 
bb77af8
 	cfile = NULL;
e83fb19
diff -up dhcp-4.3.0a1/client/dhclient.c.cloexec dhcp-4.3.0a1/client/dhclient.c
e83fb19
--- dhcp-4.3.0a1/client/dhclient.c.cloexec	2013-12-19 15:34:41.629886384 +0100
e83fb19
+++ dhcp-4.3.0a1/client/dhclient.c	2013-12-19 15:36:41.608180467 +0100
bb77af8
@@ -148,11 +148,11 @@ main(int argc, char **argv) {
bb77af8
 	/* Make sure that file descriptors 0 (stdin), 1, (stdout), and
bb77af8
 	   2 (stderr) are open. To do this, we assume that when we
bb77af8
 	   open a file the lowest available file descriptor is used. */
bb77af8
-	fd = open("/dev/null", O_RDWR);
bb77af8
+	fd = open("/dev/null", O_RDWR | O_CLOEXEC);
bb77af8
 	if (fd == 0)
bb77af8
-		fd = open("/dev/null", O_RDWR);
bb77af8
+		fd = open("/dev/null", O_RDWR | O_CLOEXEC);
bb77af8
 	if (fd == 1)
bb77af8
-		fd = open("/dev/null", O_RDWR);
bb77af8
+		fd = open("/dev/null", O_RDWR | O_CLOEXEC);
bb77af8
 	if (fd == 2)
bb77af8
 		log_perror = 0; /* No sense logging to /dev/null. */
bb77af8
 	else if (fd != -1)
e83fb19
@@ -504,7 +504,7 @@ main(int argc, char **argv) {
e83fb19
 		long temp;
bb77af8
 		int e;
bb77af8
 
bb77af8
-		if ((pidfd = fopen(path_dhclient_pid, "r")) != NULL) {
bb77af8
+		if ((pidfd = fopen(path_dhclient_pid, "re")) != NULL) {
bb77af8
 			e = fscanf(pidfd, "%ld\n", &temp);
bb77af8
 			oldpid = (pid_t)temp;
bb77af8
 
e83fb19
@@ -554,7 +554,7 @@ main(int argc, char **argv) {
bb77af8
 					strncpy(new_path_dhclient_pid, path_dhclient_pid, pfx);
bb77af8
 					sprintf(new_path_dhclient_pid + pfx, "-%s.pid", ip->name);
bb77af8
 
bb77af8
-					if ((pidfd = fopen(new_path_dhclient_pid, "r")) != NULL) {
bb77af8
+					if ((pidfd = fopen(new_path_dhclient_pid, "re")) != NULL) {
bb77af8
 						e = fscanf(pidfd, "%ld\n", &temp);
bb77af8
 						oldpid = (pid_t)temp;
bb77af8
 
e83fb19
@@ -579,7 +579,7 @@ main(int argc, char **argv) {
bb77af8
 		int dhc_running = 0;
bb77af8
 		char procfn[256] = "";
bb77af8
 
bb77af8
-		if ((pidfp = fopen(path_dhclient_pid, "r")) != NULL) {
bb77af8
+		if ((pidfp = fopen(path_dhclient_pid, "re")) != NULL) {
bb77af8
 			if ((fscanf(pidfp, "%ld", &temp)==1) && ((dhcpid=(pid_t)temp) > 0)) {
bb77af8
 				snprintf(procfn,256,"/proc/%u",dhcpid);
bb77af8
 				dhc_running = (access(procfn, F_OK) == 0);
e83fb19
@@ -3077,7 +3077,7 @@ void rewrite_client_leases ()
bb77af8
 
bb77af8
 	if (leaseFile != NULL)
bb77af8
 		fclose (leaseFile);
bb77af8
-	leaseFile = fopen (path_dhclient_db, "w");
bb77af8
+	leaseFile = fopen (path_dhclient_db, "we");
bb77af8
 	if (leaseFile == NULL) {
bb77af8
 		log_error ("can't create %s: %m", path_dhclient_db);
bb77af8
 		return;
e83fb19
@@ -3261,7 +3261,7 @@ write_duid(struct data_string *duid)
bb77af8
 		return DHCP_R_INVALIDARG;
bb77af8
 
bb77af8
 	if (leaseFile == NULL) {	/* XXX? */
bb77af8
-		leaseFile = fopen(path_dhclient_db, "w");
bb77af8
+		leaseFile = fopen(path_dhclient_db, "we");
bb77af8
 		if (leaseFile == NULL) {
bb77af8
 			log_error("can't create %s: %m", path_dhclient_db);
bb77af8
 			return ISC_R_IOERROR;
e83fb19
@@ -3441,7 +3441,7 @@ int write_client_lease (client, lease, r
bb77af8
 		return 1;
bb77af8
 
bb77af8
 	if (leaseFile == NULL) {	/* XXX */
bb77af8
-		leaseFile = fopen (path_dhclient_db, "w");
bb77af8
+		leaseFile = fopen (path_dhclient_db, "we");
bb77af8
 		if (leaseFile == NULL) {
bb77af8
 			log_error ("can't create %s: %m", path_dhclient_db);
bb77af8
 			return 0;
e83fb19
@@ -3952,9 +3952,9 @@ void go_daemon ()
e83fb19
 	(void) close(2);
bb77af8
 
bb77af8
 	/* Reopen them on /dev/null. */
e83fb19
-	(void) open("/dev/null", O_RDWR);
e83fb19
-	(void) open("/dev/null", O_RDWR);
e83fb19
-	(void) open("/dev/null", O_RDWR);
e83fb19
+	(void) open("/dev/null", O_RDWR | O_CLOEXEC);
e83fb19
+	(void) open("/dev/null", O_RDWR | O_CLOEXEC);
e83fb19
+	(void) open("/dev/null", O_RDWR | O_CLOEXEC);
bb77af8
 
bb77af8
 	write_client_pid_file ();
bb77af8
 
e83fb19
@@ -3971,14 +3971,14 @@ void write_client_pid_file ()
bb77af8
 		return;
bb77af8
 	}
bb77af8
 
bb77af8
-	pfdesc = open (path_dhclient_pid, O_CREAT | O_TRUNC | O_WRONLY, 0644);
bb77af8
+	pfdesc = open (path_dhclient_pid, O_CREAT | O_TRUNC | O_WRONLY | O_CLOEXEC, 0644);
bb77af8
 
bb77af8
 	if (pfdesc < 0) {
bb77af8
 		log_error ("Can't create %s: %m", path_dhclient_pid);
bb77af8
 		return;
bb77af8
 	}
bb77af8
 
bb77af8
-	pf = fdopen (pfdesc, "w");
bb77af8
+	pf = fdopen (pfdesc, "we");
bb77af8
 	if (!pf) {
bb77af8
 		close(pfdesc);
bb77af8
 		log_error ("Can't fdopen %s: %m", path_dhclient_pid);
e83fb19
diff -up dhcp-4.3.0a1/common/bpf.c.cloexec dhcp-4.3.0a1/common/bpf.c
e83fb19
--- dhcp-4.3.0a1/common/bpf.c.cloexec	2013-12-19 15:34:41.640886227 +0100
e83fb19
+++ dhcp-4.3.0a1/common/bpf.c	2013-12-19 15:34:41.661885928 +0100
e83fb19
@@ -95,7 +95,7 @@ int if_register_bpf (info)
bb77af8
 	for (b = 0; 1; b++) {
bb77af8
 		/* %Audit% 31 bytes max. %2004.06.17,Safe% */
bb77af8
 		sprintf(filename, BPF_FORMAT, b);
bb77af8
-		sock = open (filename, O_RDWR, 0);
bb77af8
+		sock = open (filename, O_RDWR | O_CLOEXEC, 0);
bb77af8
 		if (sock < 0) {
bb77af8
 			if (errno == EBUSY) {
bb77af8
 				continue;
e83fb19
diff -up dhcp-4.3.0a1/common/dlpi.c.cloexec dhcp-4.3.0a1/common/dlpi.c
e83fb19
--- dhcp-4.3.0a1/common/dlpi.c.cloexec	2013-12-19 15:34:41.641886213 +0100
e83fb19
+++ dhcp-4.3.0a1/common/dlpi.c	2013-12-19 15:34:41.662885914 +0100
e83fb19
@@ -804,7 +804,7 @@ dlpiopen(const char *ifname) {
bb77af8
 	}
bb77af8
 	*dp = '\0';
bb77af8
 	
bb77af8
-	return open (devname, O_RDWR, 0);
bb77af8
+	return open (devname, O_RDWR | O_CLOEXEC, 0);
bb77af8
 }
bb77af8
 
bb77af8
 /*
e83fb19
diff -up dhcp-4.3.0a1/common/nit.c.cloexec dhcp-4.3.0a1/common/nit.c
e83fb19
--- dhcp-4.3.0a1/common/nit.c.cloexec	2013-12-19 15:34:41.642886199 +0100
e83fb19
+++ dhcp-4.3.0a1/common/nit.c	2013-12-19 15:34:41.662885914 +0100
bb77af8
@@ -81,7 +81,7 @@ int if_register_nit (info)
bb77af8
 	struct strioctl sio;
bb77af8
 
bb77af8
 	/* Open a NIT device */
bb77af8
-	sock = open ("/dev/nit", O_RDWR);
bb77af8
+	sock = open ("/dev/nit", O_RDWR | O_CLOEXEC);
bb77af8
 	if (sock < 0)
bb77af8
 		log_fatal ("Can't open NIT device for %s: %m", info -> name);
bb77af8
 
e83fb19
diff -up dhcp-4.3.0a1/common/resolv.c.cloexec dhcp-4.3.0a1/common/resolv.c
e83fb19
--- dhcp-4.3.0a1/common/resolv.c.cloexec	2013-12-11 01:25:12.000000000 +0100
e83fb19
+++ dhcp-4.3.0a1/common/resolv.c	2013-12-19 15:34:41.663885900 +0100
e83fb19
@@ -50,7 +50,7 @@ void read_resolv_conf (parse_time)
bb77af8
 	struct domain_search_list *dp, *dl, *nd;
bb77af8
 	isc_result_t status;
bb77af8
 
bb77af8
-	if ((file = open (path_resolv_conf, O_RDONLY)) < 0) {
bb77af8
+	if ((file = open (path_resolv_conf, O_RDONLY | O_CLOEXEC)) < 0) {
bb77af8
 		log_error ("Can't open %s: %m", path_resolv_conf);
bb77af8
 		return;
bb77af8
 	}
e83fb19
diff -up dhcp-4.3.0a1/common/upf.c.cloexec dhcp-4.3.0a1/common/upf.c
e83fb19
--- dhcp-4.3.0a1/common/upf.c.cloexec	2013-12-19 15:34:41.642886199 +0100
e83fb19
+++ dhcp-4.3.0a1/common/upf.c	2013-12-19 15:34:41.663885900 +0100
bb77af8
@@ -77,7 +77,7 @@ int if_register_upf (info)
bb77af8
 		/* %Audit% Cannot exceed 36 bytes. %2004.06.17,Safe% */
bb77af8
 		sprintf(filename, "/dev/pf/pfilt%d", b);
bb77af8
 
bb77af8
-		sock = open (filename, O_RDWR, 0);
bb77af8
+		sock = open (filename, O_RDWR | O_CLOEXEC, 0);
bb77af8
 		if (sock < 0) {
bb77af8
 			if (errno == EBUSY) {
bb77af8
 				continue;
e83fb19
diff -up dhcp-4.3.0a1/omapip/trace.c.cloexec dhcp-4.3.0a1/omapip/trace.c
e83fb19
--- dhcp-4.3.0a1/omapip/trace.c.cloexec	2013-12-11 01:01:03.000000000 +0100
e83fb19
+++ dhcp-4.3.0a1/omapip/trace.c	2013-12-19 15:34:41.663885900 +0100
e83fb19
@@ -142,10 +142,10 @@ isc_result_t trace_begin (const char *fi
bb77af8
 		return DHCP_R_INVALIDARG;
bb77af8
 	}
bb77af8
 
bb77af8
-	traceoutfile = open (filename, O_CREAT | O_WRONLY | O_EXCL, 0600);
bb77af8
+	traceoutfile = open (filename, O_CREAT | O_WRONLY | O_EXCL | O_CLOEXEC, 0600);
bb77af8
 	if (traceoutfile < 0 && errno == EEXIST) {
bb77af8
 		log_error ("WARNING: Overwriting trace file \"%s\"", filename);
bb77af8
-		traceoutfile = open (filename, O_WRONLY | O_EXCL | O_TRUNC,
bb77af8
+		traceoutfile = open (filename, O_WRONLY | O_EXCL | O_TRUNC | O_CLOEXEC,
bb77af8
 				     0600);
bb77af8
 	}
bb77af8
 
e83fb19
@@ -433,7 +433,7 @@ void trace_file_replay (const char *file
bb77af8
 	isc_result_t result;
bb77af8
 	int len;
bb77af8
 
bb77af8
-	traceinfile = fopen (filename, "r");
bb77af8
+	traceinfile = fopen (filename, "re");
bb77af8
 	if (!traceinfile) {
bb77af8
 		log_error("Can't open tracefile %s: %m", filename);
bb77af8
 		return;
e83fb19
diff -up dhcp-4.3.0a1/relay/dhcrelay.c.cloexec dhcp-4.3.0a1/relay/dhcrelay.c
e83fb19
--- dhcp-4.3.0a1/relay/dhcrelay.c.cloexec	2013-12-13 22:26:21.000000000 +0100
e83fb19
+++ dhcp-4.3.0a1/relay/dhcrelay.c	2013-12-19 15:34:41.664885886 +0100
e83fb19
@@ -193,11 +193,11 @@ main(int argc, char **argv) {
bb77af8
 	/* Make sure that file descriptors 0(stdin), 1,(stdout), and
bb77af8
 	   2(stderr) are open. To do this, we assume that when we
bb77af8
 	   open a file the lowest available file descriptor is used. */
bb77af8
-	fd = open("/dev/null", O_RDWR);
bb77af8
+	fd = open("/dev/null", O_RDWR | O_CLOEXEC);
bb77af8
 	if (fd == 0)
bb77af8
-		fd = open("/dev/null", O_RDWR);
bb77af8
+		fd = open("/dev/null", O_RDWR | O_CLOEXEC);
bb77af8
 	if (fd == 1)
bb77af8
-		fd = open("/dev/null", O_RDWR);
bb77af8
+		fd = open("/dev/null", O_RDWR | O_CLOEXEC);
bb77af8
 	if (fd == 2)
bb77af8
 		log_perror = 0; /* No sense logging to /dev/null. */
bb77af8
 	else if (fd != -1)
e83fb19
@@ -564,13 +564,13 @@ main(int argc, char **argv) {
bb77af8
 
bb77af8
 		if (no_pid_file == ISC_FALSE) {
bb77af8
 			pfdesc = open(path_dhcrelay_pid,
bb77af8
-				      O_CREAT | O_TRUNC | O_WRONLY, 0644);
bb77af8
+				      O_CREAT | O_TRUNC | O_WRONLY | O_CLOEXEC, 0644);
bb77af8
 
bb77af8
 			if (pfdesc < 0) {
bb77af8
 				log_error("Can't create %s: %m",
bb77af8
 					  path_dhcrelay_pid);
bb77af8
 			} else {
bb77af8
-				pf = fdopen(pfdesc, "w");
bb77af8
+				pf = fdopen(pfdesc, "we");
bb77af8
 				if (!pf)
bb77af8
 					log_error("Can't fdopen %s: %m",
bb77af8
 						  path_dhcrelay_pid);
e83fb19
diff -up dhcp-4.3.0a1/server/confpars.c.cloexec dhcp-4.3.0a1/server/confpars.c
e83fb19
--- dhcp-4.3.0a1/server/confpars.c.cloexec	2013-12-11 01:25:12.000000000 +0100
e83fb19
+++ dhcp-4.3.0a1/server/confpars.c	2013-12-19 15:34:41.665885871 +0100
e83fb19
@@ -117,7 +117,7 @@ isc_result_t read_conf_file (const char
bb77af8
 	}
bb77af8
 #endif
bb77af8
 
bb77af8
-	if ((file = open (filename, O_RDONLY)) < 0) {
bb77af8
+	if ((file = open (filename, O_RDONLY | O_CLOEXEC)) < 0) {
bb77af8
 		if (leasep) {
bb77af8
 			log_error ("Can't open lease database %s: %m --",
bb77af8
 				   path_dhcpd_db);
e83fb19
diff -up dhcp-4.3.0a1/server/db.c.cloexec dhcp-4.3.0a1/server/db.c
e83fb19
--- dhcp-4.3.0a1/server/db.c.cloexec	2013-12-11 01:25:12.000000000 +0100
e83fb19
+++ dhcp-4.3.0a1/server/db.c	2013-12-19 15:34:41.666885857 +0100
e83fb19
@@ -1081,7 +1081,7 @@ void db_startup (testp)
bb77af8
 	}
bb77af8
 #endif
bb77af8
 	if (!testp) {
bb77af8
-		db_file = fopen (path_dhcpd_db, "a");
bb77af8
+		db_file = fopen (path_dhcpd_db, "ae");
bb77af8
 		if (!db_file)
bb77af8
 			log_fatal ("Can't open %s for append.", path_dhcpd_db);
bb77af8
 		expire_all_pools ();
e83fb19
@@ -1129,12 +1129,12 @@ int new_lease_file ()
bb77af8
 		     path_dhcpd_db, (int)t) >= sizeof newfname)
bb77af8
 		log_fatal("new_lease_file: lease file path too long");
bb77af8
 
bb77af8
-	db_fd = open (newfname, O_WRONLY | O_TRUNC | O_CREAT, 0664);
bb77af8
+	db_fd = open (newfname, O_WRONLY | O_TRUNC | O_CREAT | O_CLOEXEC, 0664);
bb77af8
 	if (db_fd < 0) {
bb77af8
 		log_error ("Can't create new lease file: %m");
bb77af8
 		return 0;
bb77af8
 	}
bb77af8
-	if ((new_db_file = fdopen(db_fd, "w")) == NULL) {
bb77af8
+	if ((new_db_file = fdopen(db_fd, "we")) == NULL) {
bb77af8
 		log_error("Can't fdopen new lease file: %m");
bb77af8
 		close(db_fd);
bb77af8
 		goto fdfail;
e83fb19
diff -up dhcp-4.3.0a1/server/dhcpd.c.cloexec dhcp-4.3.0a1/server/dhcpd.c
e83fb19
--- dhcp-4.3.0a1/server/dhcpd.c.cloexec	2013-12-13 22:26:01.000000000 +0100
e83fb19
+++ dhcp-4.3.0a1/server/dhcpd.c	2013-12-19 15:37:17.258674472 +0100
e83fb19
@@ -193,11 +193,11 @@ main(int argc, char **argv) {
bb77af8
         /* Make sure that file descriptors 0 (stdin), 1, (stdout), and
bb77af8
            2 (stderr) are open. To do this, we assume that when we
bb77af8
            open a file the lowest available file descriptor is used. */
bb77af8
-        fd = open("/dev/null", O_RDWR);
bb77af8
+        fd = open("/dev/null", O_RDWR | O_CLOEXEC);
bb77af8
         if (fd == 0)
bb77af8
-                fd = open("/dev/null", O_RDWR);
bb77af8
+                fd = open("/dev/null", O_RDWR | O_CLOEXEC);
bb77af8
         if (fd == 1)
bb77af8
-                fd = open("/dev/null", O_RDWR);
bb77af8
+                fd = open("/dev/null", O_RDWR | O_CLOEXEC);
bb77af8
         if (fd == 2)
bb77af8
                 log_perror = 0; /* No sense logging to /dev/null. */
bb77af8
         else if (fd != -1)
e83fb19
@@ -716,7 +716,7 @@ main(int argc, char **argv) {
bb77af8
 	 */
bb77af8
 	if (no_pid_file == ISC_FALSE) {
bb77af8
 		/*Read previous pid file. */
bb77af8
-		if ((i = open (path_dhcpd_pid, O_RDONLY)) >= 0) {
bb77af8
+		if ((i = open (path_dhcpd_pid, O_RDONLY | O_CLOEXEC)) >= 0) {
bb77af8
 			status = read(i, pbuf, (sizeof pbuf) - 1);
bb77af8
 			close (i);
bb77af8
 			if (status > 0) {
e83fb19
@@ -735,7 +735,7 @@ main(int argc, char **argv) {
bb77af8
 		}
bb77af8
 
bb77af8
 		/* Write new pid file. */
bb77af8
-		i = open(path_dhcpd_pid, O_WRONLY|O_CREAT|O_TRUNC, 0644);
bb77af8
+		i = open(path_dhcpd_pid, O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0644);
bb77af8
 		if (i >= 0) {
bb77af8
 			sprintf(pbuf, "%d\n", (int) getpid());
bb77af8
 			IGNORE_RET (write(i, pbuf, strlen(pbuf)));
e83fb19
@@ -763,9 +763,9 @@ main(int argc, char **argv) {
e83fb19
                 (void) close(2);
bb77af8
 
bb77af8
                 /* Reopen them on /dev/null. */
e83fb19
-                (void) open("/dev/null", O_RDWR);
e83fb19
-                (void) open("/dev/null", O_RDWR);
e83fb19
-                (void) open("/dev/null", O_RDWR);
e83fb19
+                (void) open("/dev/null", O_RDWR | O_CLOEXEC);
e83fb19
+                (void) open("/dev/null", O_RDWR | O_CLOEXEC);
e83fb19
+                (void) open("/dev/null", O_RDWR | O_CLOEXEC);
bb77af8
                 log_perror = 0; /* No sense logging to /dev/null. */
bb77af8
 
bb77af8
        		IGNORE_RET (chdir("/"));
e83fb19
diff -up dhcp-4.3.0a1/server/ldap.c.cloexec dhcp-4.3.0a1/server/ldap.c
e83fb19
--- dhcp-4.3.0a1/server/ldap.c.cloexec	2013-12-11 01:25:12.000000000 +0100
e83fb19
+++ dhcp-4.3.0a1/server/ldap.c	2013-12-19 15:34:41.667885843 +0100
e83fb19
@@ -684,7 +684,7 @@ ldap_start (void)
bb77af8
 
bb77af8
   if (ldap_debug_file != NULL && ldap_debug_fd == -1)
bb77af8
     {
bb77af8
-      if ((ldap_debug_fd = open (ldap_debug_file, O_CREAT | O_TRUNC | O_WRONLY,
bb77af8
+      if ((ldap_debug_fd = open (ldap_debug_file, O_CREAT | O_TRUNC | O_WRONLY | O_CLOEXEC,
bb77af8
                                  S_IRUSR | S_IWUSR)) < 0)
bb77af8
         log_error ("Error opening debug LDAP log file %s: %s", ldap_debug_file,
bb77af8
                    strerror (errno));