Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 11 Jan 2012 16:35:26 +0000 (UTC)
From:      Guy Helmer <ghelmer@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r229966 - head/lib/libutil
Message-ID:  <201201111635.q0BGZQmQ067528@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: ghelmer
Date: Wed Jan 11 16:35:26 2012
New Revision: 229966
URL: http://svn.freebsd.org/changeset/base/229966

Log:
  jilles pointed out that O_CLOEXEC could be used in the open(2) flags
  rather than using fcntl(2) later, and in addition to saving a system
  call, removes a possible race with fork/exec from threads or signal
  handlers.

Modified:
  head/lib/libutil/pidfile.c

Modified: head/lib/libutil/pidfile.c
==============================================================================
--- head/lib/libutil/pidfile.c	Wed Jan 11 15:00:16 2012	(r229965)
+++ head/lib/libutil/pidfile.c	Wed Jan 11 16:35:26 2012	(r229966)
@@ -117,7 +117,7 @@ pidfile_open(const char *path, mode_t mo
 	 * pidfile_write() can be called multiple times.
 	 */
 	fd = flopen(pfh->pf_path,
-	    O_WRONLY | O_CREAT | O_TRUNC | O_NONBLOCK, mode);
+	    O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NONBLOCK, mode);
 	if (fd == -1) {
 		if (errno == EWOULDBLOCK && pidptr != NULL) {
 			count = 20;
@@ -139,19 +139,6 @@ pidfile_open(const char *path, mode_t mo
 	}
 
 	/*
-	 * Prevent the file descriptor from escaping to other
-	 * programs via exec(3).
-	 */
-	if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) {
-		error = errno;
-		unlink(pfh->pf_path);
-		close(fd);
-		free(pfh);
-		errno = error;
-		return (NULL);
-	}
-
-	/*
 	 * Remember file information, so in pidfile_write() we are sure we write
 	 * to the proper descriptor.
 	 */



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201201111635.q0BGZQmQ067528>