summaryrefslogtreecommitdiffstats
path: root/common.c
diff options
context:
space:
mode:
authorRoy Marples <roy@marples.name>2008-02-09 21:58:05 +0000
committerRoy Marples <roy@marples.name>2008-02-09 21:58:05 +0000
commitca1e79c0f854186ce84eb0526e9a29bbb0d0c084 (patch)
treef997ba7bd44c5c4d140669804086a102b38383b8 /common.c
parent2136d44495f4bd145ab6df48bc951abb70bfe564 (diff)
downloaddhcpcd-ca1e79c0f854186ce84eb0526e9a29bbb0d0c084.tar.xz
We should only have one bit of code to mark sockets close_on_exec.
Diffstat (limited to 'common.c')
-rw-r--r--common.c18
1 files changed, 16 insertions, 2 deletions
diff --git a/common.c b/common.c
index 5d5bacfb..8860ec69 100644
--- a/common.c
+++ b/common.c
@@ -114,13 +114,13 @@ size_t strlcpy (char *dst, const char *src, size_t size)
#endif
/* Close our fd's */
-void close_fds (void)
+int close_fds (void)
{
int fd;
if ((fd = open ("/dev/null", O_RDWR)) == -1) {
logger (LOG_ERR, "open `/dev/null': %s", strerror (errno));
- return;
+ return (-1);
}
dup2 (fd, fileno (stdin));
@@ -128,6 +128,20 @@ void close_fds (void)
dup2 (fd, fileno (stderr));
if (fd > 2)
close (fd);
+ return (0);
+}
+
+int close_on_exec (int fd)
+{
+ int flags;
+
+ if ((flags = fcntl (fd, F_GETFD, 0)) == -1
+ || fcntl (fd, F_SETFD, flags | FD_CLOEXEC) == -1)
+ {
+ logger (LOG_ERR, "fcntl: %s", strerror (errno));
+ return (-1);
+ }
+ return (0);
}
/* Handy function to get the time.