chiark / gitweb /
Introduce setnonblock()
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 27 Sep 2014 12:26:17 +0000 (13:26 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Thu, 2 Oct 2014 15:40:17 +0000 (16:40 +0100)
This involves reworking setcloexec()'s implementation so that we can
reuse it.

Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
process.c
secnet.h
udp.c
util.c

index edc87ac191081f4b64db3e479b848a25be52deea..12e2caac5fc6d7d2749422434af2cda54245d056 100644 (file)
--- a/process.c
+++ b/process.c
@@ -301,9 +301,7 @@ void start_signal_handling(void)
     pipe_cloexec(p);
     spw=p[1];
     spr=p[0];
-    if (fcntl(spw, F_SETFL, fcntl(spw, F_GETFL)|O_NONBLOCK)==-1) {
-       fatal_perror("start_signal_handling: fcntl(O_NONBLOCK)");
-    }
+    setnonblock(spw);
 
     register_for_poll(NULL,signal_beforepoll,signal_afterpoll,"signal");
     signal_handling=True;
index dad9fbd04df6bd33c8ad3ffbcdb8178092b9b05e..2e73238a9a075ee5af0546d3f9729008986e814d 100644 (file)
--- a/secnet.h
+++ b/secnet.h
@@ -174,6 +174,7 @@ extern void *safe_realloc_ary(void *p, size_t size, size_t count,
                              const char *message);
 
 void setcloexec(int fd); /* cannot fail */
+void setnonblock(int fd); /* cannot fail */
 void pipe_cloexec(int fd[2]); /* pipe(), setcloexec() twice; cannot fail */
 
 extern int sys_cmd(const char *file, const char *argc, ...);
diff --git a/udp.c b/udp.c
index fb00a5627ebe43e3d2ca9473ce7ffb2de6a18e7c..4cf19584663bfdbcb277a72217b6e8144f603af5 100644 (file)
--- a/udp.c
+++ b/udp.c
@@ -197,8 +197,7 @@ bool_t udp_make_socket(struct udpcommon *uc, struct udpsock *us,
 
     us->fd=socket(addr->sa.sa_family, SOCK_DGRAM, IPPROTO_UDP);
     if (us->fd<0) FAIL("socket");
-    if (fcntl(us->fd, F_SETFL, fcntl(us->fd, F_GETFL)|O_NONBLOCK)==-1)
-       FAIL("fcntl(set O_NONBLOCK)");
+    setnonblock(us->fd);
     setcloexec(us->fd);
 #ifdef CONFIG_IPV6
     if (addr->sa.sa_family==AF_INET6) {
diff --git a/util.c b/util.c
index 8525c63222cdf98c73f2ca1c334b78445e0b2e21..5bcf746ff3524fadabd8141708342e2b20557816 100644 (file)
--- a/util.c
+++ b/util.c
@@ -175,13 +175,17 @@ 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=fcntl(fd, F_SETFD, r|FD_CLOEXEC);
-    if (r<0) fatal_perror("fcntl(,F_SETFD,|FD_CLOEXEC) failed");
+#define DEFINE_SETFDFLAG(fn,FL,FLAG)                                   \
+void fn(int fd) {                                                      \
+    int r=fcntl(fd, F_GET##FL);                                                \
+    if (r<0) fatal_perror("fcntl(,F_GET" #FL ") failed");              \
+    r=fcntl(fd, F_SET##FL, r|FLAG);                                    \
+    if (r<0) fatal_perror("fcntl(,F_SET" #FL ",|" #FLAG ") failed");   \
 }
 
+DEFINE_SETFDFLAG(setcloexec,FD,FD_CLOEXEC);
+DEFINE_SETFDFLAG(setnonblock,FL,O_NONBLOCK);
+
 void pipe_cloexec(int fd[2]) {
     int r=pipe(fd);
     if (r) fatal_perror("pipe");