Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / ibm-public / postfix / dist / src / util / close_on_exec.c
blobfd8ff1f9608848d999dae82a2e1e1b3bb977f675
1 /* $NetBSD$ */
3 /*++
4 /* NAME
5 /* close_on_exec 3
6 /* SUMMARY
7 /* set/clear close-on-exec flag
8 /* SYNOPSIS
9 /* #include <iostuff.h>
11 /* int close_on_exec(int fd, int on)
12 /* DESCRIPTION
13 /* the \fIclose_on_exec\fR() function manipulates the close-on-exec
14 /* flag for the specified open file, and returns the old setting.
16 /* Arguments:
17 /* .IP fd
18 /* A file descriptor.
19 /* .IP on
20 /* Use CLOSE_ON_EXEC or PASS_ON_EXEC.
21 /* DIAGNOSTICS
22 /* All errors are fatal.
23 /* LICENSE
24 /* .ad
25 /* .fi
26 /* The Secure Mailer license must be distributed with this software.
27 /* AUTHOR(S)
28 /* Wietse Venema
29 /* IBM T.J. Watson Research
30 /* P.O. Box 704
31 /* Yorktown Heights, NY 10598, USA
32 /*--*/
34 /* System interfaces. */
36 #include <sys_defs.h>
37 #include <fcntl.h>
39 /* Utility library. */
41 #include "msg.h"
43 /* Application-specific. */
45 #include "iostuff.h"
47 #define PATTERN FD_CLOEXEC
49 /* close_on_exec - set/clear close-on-exec flag */
51 int close_on_exec(fd, on)
52 int fd;
53 int on;
55 int flags;
57 if ((flags = fcntl(fd, F_GETFD, 0)) < 0)
58 msg_fatal("fcntl: get flags: %m");
59 if (fcntl(fd, F_SETFD, on ? flags | PATTERN : flags & ~PATTERN) < 0)
60 msg_fatal("fcntl: set close-on-exec flag %s: %m", on ? "on" : "off");
61 return ((flags & PATTERN) != 0);