[PATCH 1/6] fds: Provide cloexec() and use it in udp.c
Ian Jackson
ijackson at chiark.greenend.org.uk
Mon Sep 22 16:45:08 BST 2014
Signed-off-by: Ian Jackson <ijackson at chiark.greenend.org.uk>
---
secnet.h | 4 ++++
udp.c | 5 +----
util.c | 8 ++++++++
3 files changed, 13 insertions(+), 4 deletions(-)
diff --git a/secnet.h b/secnet.h
index 194341c..ce01d24 100644
--- a/secnet.h
+++ b/secnet.h
@@ -9,6 +9,8 @@
#include <stdio.h>
#include <string.h>
#include <assert.h>
+#include <fcntl.h>
+#include <unistd.h>
#include <sys/poll.h>
#include <sys/types.h>
#include <sys/time.h>
@@ -136,6 +138,8 @@ extern char *safe_strdup(const char *string, const char *message);
extern void *safe_malloc(size_t size, const char *message);
extern void *safe_malloc_ary(size_t size, size_t count, const char *message);
+void setcloexec(int fd); /* cannot fail */
+
extern int sys_cmd(const char *file, const char *argc, ...);
extern uint64_t now_global;
diff --git a/udp.c b/udp.c
index 552a58e..bb82026 100644
--- a/udp.c
+++ b/udp.c
@@ -224,10 +224,7 @@ static void udp_phase_hook(void *sst, uint32_t new_phase)
fatal_perror("udp (%s:%d): fcntl(set O_NONBLOCK)",
st->loc.file,st->loc.line);
}
- if (fcntl(st->fd, F_SETFD, FD_CLOEXEC)==-1) {
- fatal_perror("udp (%s:%d): fcntl(set FD_CLOEXEC)",
- st->loc.file,st->loc.line);
- }
+ setcloexec(st->fd);
FILLZERO(addr);
addr.sin_family=AF_INET;
diff --git a/util.c b/util.c
index 094870f..cfa6b4c 100644
--- a/util.c
+++ b/util.c
@@ -162,6 +162,14 @@ int32_t write_mpbin(MP_INT *a, uint8_t *buffer, int32_t buflen)
return i;
}
+void setcloexec(int fd) {
+ int r=fcntl(fd, F_GETFD);
+ if (r<0) fatal_perror("fcntl(,F_GETFD) failed");
+ r |= FD_CLOEXEC;
+ r=fcntl(fd, F_SETFD, r|FD_CLOEXEC);
+ if (r<0) fatal_perror("fcntl(,F_SETFD,|FD_CLOEXEC) failed");
+}
+
static const char *phases[NR_PHASES]={
"PHASE_INIT",
"PHASE_GETOPTS",
--
1.7.10.4
More information about the sgo-software-discuss
mailing list