[Midnightbsd-cvs] src [10494] trunk/lib/libutil/pidfile.c: use O_CLOEXEC

laffer1 at midnightbsd.org laffer1 at midnightbsd.org
Wed Jun 6 21:34:12 EDT 2018


Revision: 10494
          http://svnweb.midnightbsd.org/src/?rev=10494
Author:   laffer1
Date:     2018-06-06 21:34:11 -0400 (Wed, 06 Jun 2018)
Log Message:
-----------
use O_CLOEXEC

Modified Paths:
--------------
    trunk/lib/libutil/pidfile.c

Modified: trunk/lib/libutil/pidfile.c
===================================================================
--- trunk/lib/libutil/pidfile.c	2018-06-07 01:33:10 UTC (rev 10493)
+++ trunk/lib/libutil/pidfile.c	2018-06-07 01:34:11 UTC (rev 10494)
@@ -73,7 +73,7 @@
 	char buf[16], *endptr;
 	int error, fd, i;
 
-	fd = open(path, O_RDONLY);
+	fd = open(path, O_RDONLY | O_CLOEXEC);
 	if (fd == -1)
 		return (errno);
 
@@ -126,24 +126,30 @@
 	fd = flopen(pfh->pf_path,
 	    O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NONBLOCK, mode);
 	if (fd == -1) {
-		count = 0;
-		rqtp.tv_sec = 0;
-		rqtp.tv_nsec = 5000000;
-		if (errno == EWOULDBLOCK && pidptr != NULL) {
-		again:
-			errno = pidfile_read(pfh->pf_path, pidptr);
-			if (errno == 0)
+		if (errno == EWOULDBLOCK) {
+			if (pidptr == NULL) {
 				errno = EEXIST;
-			else if (errno == EAGAIN) {
-				if (++count <= 3) {
+			} else {
+				count = 20;
+				rqtp.tv_sec = 0;
+				rqtp.tv_nsec = 5000000;
+				for (;;) {
+					errno = pidfile_read(pfh->pf_path,
+					    pidptr);
+					if (errno != EAGAIN || --count == 0)
+						break;
 					nanosleep(&rqtp, 0);
-					goto again;
 				}
+				if (errno == EAGAIN)
+					*pidptr = -1;
+				if (errno == 0 || errno == EAGAIN)
+					errno = EEXIST;
 			}
 		}
 		free(pfh);
 		return (NULL);
 	}
+
 	/*
 	 * Remember file information, so in pidfile_write() we are sure we write
 	 * to the proper descriptor.



More information about the Midnightbsd-cvs mailing list