diff options
author | Philip Guenther <guenther@cvs.openbsd.org> | 2014-08-15 03:51:41 +0000 |
---|---|---|
committer | Philip Guenther <guenther@cvs.openbsd.org> | 2014-08-15 03:51:41 +0000 |
commit | 145e8eaff9f41d310024ae7144a92ab145cf125e (patch) | |
tree | 4252b4e53a8b19544d09a3efa40df5c6032d79be /lib/libutil/passwd.c | |
parent | d4c6fdba14c8b6b7961206f206cc676c18999e10 (diff) |
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@
Diffstat (limited to 'lib/libutil/passwd.c')
-rw-r--r-- | lib/libutil/passwd.c | 13 |
1 files changed, 3 insertions, 10 deletions
diff --git a/lib/libutil/passwd.c b/lib/libutil/passwd.c index 26db941e83c..31c1ccc006a 100644 --- a/lib/libutil/passwd.c +++ b/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); } |