]> git.ipfire.org Git - thirdparty/dhcpcd.git/commitdiff
We should only have one bit of code to mark sockets close_on_exec.
authorRoy Marples <roy@marples.name>
Sat, 9 Feb 2008 21:58:05 +0000 (21:58 +0000)
committerRoy Marples <roy@marples.name>
Sat, 9 Feb 2008 21:58:05 +0000 (21:58 +0000)
client.c
common.c
common.h
dhcp.c
signal.c
socket.c

index aa8d709aba5c4181ffbeeb6aae551d5913502850..2975ac8582bafebdf100a18b89421e87246e6d38 100644 (file)
--- a/client.c
+++ b/client.c
@@ -38,7 +38,6 @@
 #endif
 #include <ctype.h>
 #include <errno.h>
-#include <fcntl.h>
 #include <signal.h>
 #include <stdbool.h>
 #include <stdlib.h>
index 5d5bacfb618d370cd984e0c720ee3b59d155c5f3..8860ec69150dee5ac2f83f27712174510da3855e 100644 (file)
--- 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.
index 4b26318b02ec40c461f1a73cccfad6136efab926..0a59a75aa82e44eb07b69d00d5b2e7fc4f9dbfa9 100644 (file)
--- a/common.h
+++ b/common.h
@@ -52,7 +52,8 @@ size_t strlcpy (char *dst, const char *src, size_t size);
 void srandomdev (void);
 #endif
 
-void close_fds (void);
+int close_fds (void);
+int close_on_exec (int fd);
 char *get_line (FILE *fp);
 int get_time (struct timeval *tp);
 time_t uptime (void);
diff --git a/dhcp.c b/dhcp.c
index 9d81d43b528fa3edb6b203f669a30a6645d8b222..8620c5be2303a65286635775e2cf64f0080f4434 100644 (file)
--- a/dhcp.c
+++ b/dhcp.c
@@ -398,8 +398,8 @@ static unsigned int decode_search (const unsigned char *p, int len, char *out)
 static route_t *decode_CSR(const unsigned char *p, int len)
 {
        const unsigned char *q = p;
-       int cidr;
-       int ocets;
+       unsigned int cidr;
+       unsigned int ocets;
        route_t *first;
        route_t *route;
 
index b68375c756aaaad289cad2a2798cbfb4ed5d1c15..c9430e1c9f9e585272ab614cc54b08728b53a396 100644 (file)
--- a/signal.c
+++ b/signal.c
 #include <sys/socket.h>
 #include <sys/select.h>
 #include <errno.h>
-#include <fcntl.h>
 #include <signal.h>
 #include <string.h>
 #include <unistd.h>
 
+#include "common.h"
 #include "logger.h"
 #include "signal.h"
 
@@ -127,8 +127,6 @@ int signal_read (fd_set *rset)
  * and installs the signal handler */
 int signal_init (void)
 {
-       unsigned int i;
-       int flags;
        struct sigaction sa;
 
        if (pipe (signal_pipe) == -1) {
@@ -137,10 +135,8 @@ int signal_init (void)
        }
 
        /* Stop any scripts from inheriting us */
-       for (i = 0; i < 2; i++)
-               if ((flags = fcntl (signal_pipe[i], F_GETFD, 0)) == -1 ||
-                   fcntl (signal_pipe[i], F_SETFD, flags | FD_CLOEXEC) == -1)
-                       logger (LOG_ERR ,"fcntl: %s", strerror (errno));
+       close_on_exec (signal_pipe[0]);
+       close_on_exec (signal_pipe[1]);
 
        /* Ignore child signals and don't make zombies.
         * Because we do this, we don't need to be in signal_setup */
index f35c69d2b388d89a2a0a3921495ec58e1ad18449..814d051ba7dd772320dda30066755133add03204 100644 (file)
--- a/socket.c
+++ b/socket.c
@@ -40,7 +40,6 @@
 #include <netinet/udp.h>
 #include <arpa/inet.h>
 #include <errno.h>
-#include <fcntl.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -280,13 +279,7 @@ int open_socket (interface_t *iface, int protocol)
                return -1;
        }
 
-       if ((flags = fcntl (fd, F_GETFD, 0)) == -1
-           || fcntl (fd, F_SETFD, flags | FD_CLOEXEC) == -1)
-       {
-               logger (LOG_ERR, "fcntl: %s", strerror (errno));
-               close (fd);
-               return -1;
-       }
+       close_on_exec (fd);
 
        memset (&ifr, 0, sizeof (ifr));
        strlcpy (ifr.ifr_name, iface->name, sizeof (ifr.ifr_name));
@@ -457,7 +450,6 @@ ssize_t get_packet (const interface_t *iface, unsigned char *data,
 int open_socket (interface_t *iface, int protocol)
 {
        int fd;
-       int flags;
        union sockunion {
                struct sockaddr sa;
                struct sockaddr_ll sll;
@@ -470,13 +462,7 @@ int open_socket (interface_t *iface, int protocol)
                return (-1);
        }
 
-       if ((flags = fcntl (fd, F_GETFD, 0)) == -1
-           || fcntl (fd, F_SETFD, flags | FD_CLOEXEC) == -1)
-       {
-               logger (LOG_ERR, "fcntl: %s", strerror (errno));
-               close (fd);
-               return (-1);
-       }
+       close_on_exec (fd);
 
        memset (&su, 0, sizeof (su));
        su.sll.sll_family = PF_PACKET;