From c268bc15d8d7bc5c98679083af4568348f923764 Mon Sep 17 00:00:00 2001 From: guenther <> Date: Fri, 15 Aug 2014 03:51:40 +0000 Subject: [PATCH] Use O_CLOEXEC wherever we open a file and then call fcntl(F_SETFD, FD_CLOEXEC) on it, simplifying error checking, reducing system calls, and improving thread-safety for libraries. ok miod@ --- src/lib/libutil/passwd.c | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/src/lib/libutil/passwd.c b/src/lib/libutil/passwd.c index 26db941e..31c1ccc0 100644 --- a/src/lib/libutil/passwd.c +++ b/src/lib/libutil/passwd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: passwd.c,v 1.52 2013/08/17 06:54:21 guenther Exp $ */ +/* $OpenBSD: passwd.c,v 1.53 2014/08/15 03:51:40 guenther Exp $ */ /* * Copyright (c) 1987, 1993, 1994, 1995 @@ -93,7 +93,6 @@ pw_lock(int retries) { int i, fd; mode_t old_mode; - int save_errno; if (!pw_lck) { errno = EINVAL; @@ -101,18 +100,12 @@ pw_lock(int retries) } /* Acquire the lock file. */ old_mode = umask(0); - fd = open(pw_lck, O_WRONLY|O_CREAT|O_EXCL, 0600); + fd = open(pw_lck, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC, 0600); for (i = 0; i < retries && fd < 0 && errno == EEXIST; i++) { sleep(1); - fd = open(pw_lck, O_WRONLY|O_CREAT|O_EXCL, 0600); - } - save_errno = errno; - if (fd != -1 && fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) { - close(fd); - fd = -1; + fd = open(pw_lck, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC, 0600); } (void) umask(old_mode); - errno = save_errno; return (fd); }