]> git.ipfire.org Git - thirdparty/dhcpcd.git/commitdiff
if: introduce xsocketpair, similar to xsocket
authorRoy Marples <roy@marples.name>
Sat, 5 Sep 2020 15:10:30 +0000 (16:10 +0100)
committerRoy Marples <roy@marples.name>
Sat, 5 Sep 2020 15:10:30 +0000 (16:10 +0100)
Old systems don't have SOCK_CLOEXEC, etc, this makes it easy.
While here, right limit the sockets.

src/if.c
src/if.h

index d711542ab277ec07b0fa2d5f9c2f62ec3f25204f..2e0920f9f60b4d5f67755263da5759cba389180a 100644 (file)
--- a/src/if.c
+++ b/src/if.c
@@ -995,3 +995,55 @@ out:
        return -1;
 #endif
 }
+
+int
+xsocketpair(int domain, int type, int protocol, int fd[2])
+{
+       int s;
+#if !defined(HAVE_SOCK_CLOEXEC) || !defined(HAVE_SOCK_NONBLOCK)
+       int xflags, xtype = type;
+#endif
+
+#ifndef HAVE_SOCK_CLOEXEC
+       if (xtype & SOCK_CLOEXEC)
+               type &= ~SOCK_CLOEXEC;
+#endif
+#ifndef HAVE_SOCK_NONBLOCK
+       if (xtype & SOCK_NONBLOCK)
+               type &= ~SOCK_NONBLOCK;
+#endif
+
+       if ((s = socketpair(domain, type, protocol, fd)) == -1)
+               return -1;
+
+#ifdef PRIVSEP_RIGHTS
+       if (ps_rights_limit_fdpair(fd) == -1)
+               goto out;
+#endif
+#ifndef HAVE_SOCK_CLOEXEC
+       if ((xtype & SOCK_CLOEXEC) && ((xflags = fcntl(fd[0], F_GETFD)) == -1 ||
+           fcntl(fd[0], F_SETFD, xflags | FD_CLOEXEC) == -1))
+               goto out;
+       if ((xtype & SOCK_CLOEXEC) && ((xflags = fcntl(fd[1], F_GETFD)) == -1 ||
+           fcntl(fd[1], F_SETFD, xflags | FD_CLOEXEC) == -1))
+               goto out;
+#endif
+#ifndef HAVE_SOCK_NONBLOCK
+       if ((xtype & SOCK_NONBLOCK) && ((xflags = fcntl(fd[0], F_GETFL)) == -1 ||
+           fcntl(fd[0], F_SETFL, xflags | O_NONBLOCK) == -1))
+               goto out;
+       if ((xtype & SOCK_NONBLOCK) && ((xflags = fcntl(fd[1], F_GETFL)) == -1 ||
+           fcntl(fd[1], F_SETFL, xflags | O_NONBLOCK) == -1))
+               goto out;
+#endif
+
+       return s;
+
+#if defined(PRIVSEP_RIGHTS) || \
+       !defined(HAVE_SOCK_CLOEXEC) || !defined(HAVE_SOCK_NONBLOCK)
+out:
+       close(fd[0]);
+       close(fd[1]);
+       return -1;
+#endif
+}
index 2323501f3b2964cd0b1e173995ceb874637e35d3..e053fe5b62d7bf2c420937ead5b53bc432295f70 100644 (file)
--- a/src/if.h
+++ b/src/if.h
@@ -224,6 +224,8 @@ int if_setmac(struct interface *ifp, void *, uint8_t);
 #ifndef SOCK_CXNB
 #define        SOCK_CXNB       SOCK_CLOEXEC | SOCK_NONBLOCK
 #endif
+int xsocket(int, int, int);
+int xsocketpair(int, int, int, int[2]);
 
 int if_route(unsigned char, const struct rt *rt);
 int if_initrt(struct dhcpcd_ctx *, rb_tree_t *, int);
@@ -259,7 +261,6 @@ int if_getlifetime6(struct ipv6_addr *);
 int if_machinearch(char *, size_t);
 struct interface *if_findifpfromcmsg(struct dhcpcd_ctx *,
     struct msghdr *, int *);
-int xsocket(int, int, int);
 
 #ifdef __linux__
 int if_linksocket(struct sockaddr_nl *, int, int);