1*09d4459fSDaniel Fojt /* cloexec.c - set or clear the close-on-exec descriptor flag
2cf28ed85SJohn Marino
3*09d4459fSDaniel Fojt Copyright (C) 1991, 2004-2006, 2009-2020 Free Software Foundation, Inc.
4cf28ed85SJohn Marino
5cf28ed85SJohn Marino This program is free software: you can redistribute it and/or modify
6cf28ed85SJohn Marino it under the terms of the GNU General Public License as published by
7cf28ed85SJohn Marino the Free Software Foundation; either version 3 of the License, or
8cf28ed85SJohn Marino (at your option) any later version.
9cf28ed85SJohn Marino
10cf28ed85SJohn Marino This program is distributed in the hope that it will be useful,
11cf28ed85SJohn Marino but WITHOUT ANY WARRANTY; without even the implied warranty of
12cf28ed85SJohn Marino MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13cf28ed85SJohn Marino GNU General Public License for more details.
14cf28ed85SJohn Marino
15cf28ed85SJohn Marino You should have received a copy of the GNU General Public License
16*09d4459fSDaniel Fojt along with this program. If not, see <https://www.gnu.org/licenses/>.
17cf28ed85SJohn Marino
18cf28ed85SJohn Marino The code is taken from glibc/manual/llio.texi */
19cf28ed85SJohn Marino
20cf28ed85SJohn Marino #include <config.h>
21cf28ed85SJohn Marino
22cf28ed85SJohn Marino #include "cloexec.h"
23cf28ed85SJohn Marino
24cf28ed85SJohn Marino #include <errno.h>
25cf28ed85SJohn Marino #include <fcntl.h>
26cf28ed85SJohn Marino #include <unistd.h>
27cf28ed85SJohn Marino
28cf28ed85SJohn Marino /* Set the 'FD_CLOEXEC' flag of DESC if VALUE is true,
29cf28ed85SJohn Marino or clear the flag if VALUE is false.
30cf28ed85SJohn Marino Return 0 on success, or -1 on error with 'errno' set.
31cf28ed85SJohn Marino
32cf28ed85SJohn Marino Note that on MingW, this function does NOT protect DESC from being
33cf28ed85SJohn Marino inherited into spawned children. Instead, either use dup_cloexec
34cf28ed85SJohn Marino followed by closing the original DESC, or use interfaces such as
35cf28ed85SJohn Marino open or pipe2 that accept flags like O_CLOEXEC to create DESC
36cf28ed85SJohn Marino non-inheritable in the first place. */
37cf28ed85SJohn Marino
38cf28ed85SJohn Marino int
set_cloexec_flag(int desc,bool value)39cf28ed85SJohn Marino set_cloexec_flag (int desc, bool value)
40cf28ed85SJohn Marino {
41cf28ed85SJohn Marino #ifdef F_SETFD
42cf28ed85SJohn Marino
43cf28ed85SJohn Marino int flags = fcntl (desc, F_GETFD, 0);
44cf28ed85SJohn Marino
45cf28ed85SJohn Marino if (0 <= flags)
46cf28ed85SJohn Marino {
47cf28ed85SJohn Marino int newflags = (value ? flags | FD_CLOEXEC : flags & ~FD_CLOEXEC);
48cf28ed85SJohn Marino
49cf28ed85SJohn Marino if (flags == newflags
50cf28ed85SJohn Marino || fcntl (desc, F_SETFD, newflags) != -1)
51cf28ed85SJohn Marino return 0;
52cf28ed85SJohn Marino }
53cf28ed85SJohn Marino
54cf28ed85SJohn Marino return -1;
55cf28ed85SJohn Marino
56cf28ed85SJohn Marino #else /* !F_SETFD */
57cf28ed85SJohn Marino
58cf28ed85SJohn Marino /* Use dup2 to reject invalid file descriptors; the cloexec flag
59cf28ed85SJohn Marino will be unaffected. */
60cf28ed85SJohn Marino if (desc < 0)
61cf28ed85SJohn Marino {
62cf28ed85SJohn Marino errno = EBADF;
63cf28ed85SJohn Marino return -1;
64cf28ed85SJohn Marino }
65cf28ed85SJohn Marino if (dup2 (desc, desc) < 0)
66cf28ed85SJohn Marino /* errno is EBADF here. */
67cf28ed85SJohn Marino return -1;
68cf28ed85SJohn Marino
69cf28ed85SJohn Marino /* There is nothing we can do on this kind of platform. Punt. */
70cf28ed85SJohn Marino return 0;
71cf28ed85SJohn Marino #endif /* !F_SETFD */
72cf28ed85SJohn Marino }
73cf28ed85SJohn Marino
74cf28ed85SJohn Marino
75cf28ed85SJohn Marino /* Duplicates a file handle FD, while marking the copy to be closed
76cf28ed85SJohn Marino prior to exec or spawn. Returns -1 and sets errno if FD could not
77cf28ed85SJohn Marino be duplicated. */
78cf28ed85SJohn Marino
79cf28ed85SJohn Marino int
dup_cloexec(int fd)80cf28ed85SJohn Marino dup_cloexec (int fd)
81cf28ed85SJohn Marino {
82cf28ed85SJohn Marino return fcntl (fd, F_DUPFD_CLOEXEC, 0);
83cf28ed85SJohn Marino }
84