summaryrefslogtreecommitdiffstats
path: root/lib/cloexec.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/cloexec.c')
-rw-r--r--lib/cloexec.c35
1 files changed, 16 insertions, 19 deletions
diff --git a/lib/cloexec.c b/lib/cloexec.c
index 20f30db4..cf2308ca 100644
--- a/lib/cloexec.c
+++ b/lib/cloexec.c
@@ -1,5 +1,5 @@
1/* closexec.c - set or clear the close-on-exec descriptor flag 1/* closexec.c - set or clear the close-on-exec descriptor flag
2 Copyright (C) 1991, 2004 Free Software Foundation, Inc. 2 Copyright (C) 1991, 2004, 2005 Free Software Foundation, Inc.
3 3
4 This program is free software; you can redistribute it and/or modify 4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by 5 it under the terms of the GNU General Public License as published by
@@ -13,23 +13,18 @@
13 13
14 You should have received a copy of the GNU General Public License 14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation, 15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 The code is taken from glibc/manual/llio.texi */ 18 The code is taken from glibc/manual/llio.texi */
19 19
20#if HAVE_CONFIG_H 20#ifdef HAVE_CONFIG_H
21# include <config.h> 21# include <config.h>
22#endif 22#endif
23 23
24#include "cloexec.h" 24#include "cloexec.h"
25 25
26#if HAVE_UNISTD_H 26#include <unistd.h>
27# include <unistd.h> 27#include <fcntl.h>
28#endif
29
30#if HAVE_FCNTL_H
31# include <fcntl.h>
32#endif
33 28
34#ifndef FD_CLOEXEC 29#ifndef FD_CLOEXEC
35# define FD_CLOEXEC 1 30# define FD_CLOEXEC 1
@@ -37,27 +32,29 @@
37 32
38/* Set the `FD_CLOEXEC' flag of DESC if VALUE is true, 33/* Set the `FD_CLOEXEC' flag of DESC if VALUE is true,
39 or clear the flag if VALUE is false. 34 or clear the flag if VALUE is false.
40 Return true on success, or false on error with `errno' set. */ 35 Return 0 on success, or -1 on error with `errno' set. */
41 36
42bool 37int
43set_cloexec_flag (int desc, bool value) 38set_cloexec_flag (int desc, bool value)
44{ 39{
45#if defined F_GETFD && defined F_SETFD 40#if defined F_GETFD && defined F_SETFD
46 41
47 int flags = fcntl (desc, F_GETFD, 0); 42 int flags = fcntl (desc, F_GETFD, 0);
48 int newflags;
49 43
50 if (flags < 0) 44 if (0 <= flags)
51 return false; 45 {
46 int newflags = (value ? flags | FD_CLOEXEC : flags & ~FD_CLOEXEC);
52 47
53 newflags = (value ? flags | FD_CLOEXEC : flags & ~FD_CLOEXEC); 48 if (flags == newflags
49 || fcntl (desc, F_SETFD, newflags) != -1)
50 return 0;
51 }
54 52
55 return (flags == newflags 53 return -1;
56 || fcntl (desc, F_SETFD, newflags) != -1);
57 54
58#else 55#else
59 56
60 return true; 57 return 0;
61 58
62#endif 59#endif
63} 60}