From: Ulrich Drepper Date: Sat, 11 Aug 2007 02:56:12 +0000 (+0000) Subject: * shadow/lckpwdf.c (__lckpwdf): Use O_CLOEXEC if possible. X-Git-Tag: cvs/fedora-glibc-20070814T0643~18 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=7cf89e95994db2b6a96edac8688420a8415e45e8;p=platform%2Fupstream%2Fglibc.git * shadow/lckpwdf.c (__lckpwdf): Use O_CLOEXEC if possible. --- diff --git a/ChangeLog b/ChangeLog index 2213f6b..a25af78 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,7 @@ 2007-08-10 Ulrich Drepper + * shadow/lckpwdf.c (__lckpwdf): Use O_CLOEXEC if possible. + * nscd/connections.c: Use O_CLOEXEC is possible. Use mkostemp instead of mkstemp. diff --git a/shadow/lckpwdf.c b/shadow/lckpwdf.c index 67d9937..2770fb5 100644 --- a/shadow/lckpwdf.c +++ b/shadow/lckpwdf.c @@ -1,5 +1,5 @@ /* Handle locking of password file. - Copyright (C) 1996,98,2000,02 Free Software Foundation, Inc. + Copyright (C) 1996, 1998, 2000, 2002, 2007 Free Software Foundation, Inc. This file is part of the GNU C Library. Contributed by Ulrich Drepper , 1996. @@ -26,6 +26,8 @@ #include #include +#include + /* Name of the lock file. */ #define PWD_LOCKFILE "/etc/.pwd.lock" @@ -96,20 +98,38 @@ __lckpwdf (void) /* Prevent problems caused by multiple threads. */ __libc_lock_lock (lock); - lock_fd = __open (PWD_LOCKFILE, O_WRONLY | O_CREAT, 0600); + int oflags = O_WRONLY | O_CREAT; +#ifdef O_CLOEXEC + oflags |= O_CLOEXEC; +#endif + lock_fd = __open (PWD_LOCKFILE, oflags, 0600); if (lock_fd == -1) /* Cannot create lock file. */ RETURN_CLOSE_FD (-1); - /* Make sure file gets correctly closed when process finished. */ - flags = __fcntl (lock_fd, F_GETFD, 0); - if (flags == -1) - /* Cannot get file flags. */ - RETURN_CLOSE_FD (-1); - flags |= FD_CLOEXEC; /* Close on exit. */ - if (__fcntl (lock_fd, F_SETFD, flags) < 0) - /* Cannot set new flags. */ - RETURN_CLOSE_FD (-1); +#ifndef __ASSUME_O_CLOEXEC +# ifdef O_CLOEXEC + if (__have_o_cloexec <= 0) +# endif + { + /* Make sure file gets correctly closed when process finished. */ + flags = __fcntl (lock_fd, F_GETFD, 0); + if (flags == -1) + /* Cannot get file flags. */ + RETURN_CLOSE_FD (-1); +# ifdef O_CLOEXEC + if (__have_o_cloexec == 0) + __have_o_cloexec = (flags & FD_CLOEXEC) == 0 ? -1 : 1; + if (__have_o_cloexec < 0) +# endif + { + flags |= FD_CLOEXEC; /* Close on exit. */ + if (__fcntl (lock_fd, F_SETFD, flags) < 0) + /* Cannot set new flags. */ + RETURN_CLOSE_FD (-1); + } + } +#endif /* Now we have to get exclusive write access. Since multiple process could try this we won't stop when it first fails.