dhcp: get rid of static data signal_pipe
authorDenys Vlasenko <vda.linux@googlemail.com>
Fri, 31 May 2019 21:39:22 +0000 (23:39 +0200)
committerDenys Vlasenko <vda.linux@googlemail.com>
Fri, 31 May 2019 21:39:22 +0000 (23:39 +0200)
function                                             old     new   delta
udhcp_sp_setup                                        65     110     +45
udhcp_sp_fd_set                                       60      59      -1
udhcpd_main                                         1442    1437      -5
udhcpc_main                                         2684    2679      -5
signal_pipe                                            8       -      -8
packed_usage                                       33292   33284      -8
------------------------------------------------------------------------------
(add/remove: 0/1 grow/shrink: 1/4 up/down: 45/-27)             Total: 18 bytes
   text    data     bss     dec     hex filename
 952746     481    7296  960523   ea80b busybox_old
 952768     481    7288  960537   ea819 busybox_unstripped

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
networking/udhcp/d6_dhcpc.c
networking/udhcp/dhcpc.c
networking/udhcp/dhcpd.c
networking/udhcp/signalpipe.c

index 15e9f39240acae469b62d7e13eef278efc17bd1e..02766ba168f1a4342d7303416d162a728229114b 100644 (file)
@@ -1174,6 +1174,10 @@ int udhcpc6_main(int argc UNUSED_PARAM, char **argv)
        client_data.script = CONFIG_UDHCPC_DEFAULT_SCRIPT;
        client_data.sockfd = -1;
 
+       /* Make sure fd 0,1,2 are open */
+       /* Set up the signal pipe on fds 3,4 - must be before openlog() */
+       udhcp_sp_setup();
+
        /* Parse command line */
        opt = getopt32long(argv, "^"
                /* O,x: list; -T,-t,-A take numeric param */
@@ -1268,14 +1272,10 @@ int udhcpc6_main(int argc UNUSED_PARAM, char **argv)
                logmode |= LOGMODE_SYSLOG;
        }
 
-       /* Make sure fd 0,1,2 are open */
-       bb_sanitize_stdio();
        /* Create pidfile */
        write_pidfile(client_data.pidfile);
        /* Goes to stdout (unless NOMMU) and possibly syslog */
        bb_info_msg("started, v"BB_VER);
-       /* Set up the signal pipe */
-       udhcp_sp_setup();
 
        client_data.state = INIT_SELECTING;
        d6_run_script_no_option("deconfig");
index cb85fa9e3756cd9c9518f41ad9996ac605d35a23..f040e93f7587a0307f74dd2b6165e93c2ada0f87 100644 (file)
@@ -1271,6 +1271,10 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv)
        client_data.sockfd = -1;
        str_V = "udhcp "BB_VER;
 
+       /* Make sure fd 0,1,2 are open */
+       /* Set up the signal pipe on fds 3,4 - must be before openlog() */
+       udhcp_sp_setup();
+
        /* Parse command line */
        opt = getopt32long(argv, "^"
                /* O,x: list; -T,-t,-A take numeric param */
@@ -1385,14 +1389,10 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv)
                logmode |= LOGMODE_SYSLOG;
        }
 
-       /* Make sure fd 0,1,2 are open */
-       bb_sanitize_stdio();
        /* Create pidfile */
        write_pidfile(client_data.pidfile);
        /* Goes to stdout (unless NOMMU) and possibly syslog */
        bb_info_msg("started, v"BB_VER);
-       /* Set up the signal pipe */
-       udhcp_sp_setup();
        /* We want random_xid to be random... */
        srand(monotonic_us());
 
index 6e426e6a23c1cb9321348725b8db3b1f41b392c3..022b8721bac22b910f3620b0be017202ae88e602 100644 (file)
@@ -37,6 +37,8 @@
 //usage:       IF_FEATURE_UDHCP_PORT(
 //usage:     "\n       -P N    Use port N (default 67)"
 //usage:       )
+//usage:     "\nSignals:"
+//usage:     "\n       USR1    Update lease file"
 
 #include <netinet/ether.h>
 #include <syslog.h>
@@ -863,6 +865,10 @@ int udhcpd_main(int argc UNUSED_PARAM, char **argv)
        IF_FEATURE_UDHCP_PORT(SERVER_PORT = 67;)
        IF_FEATURE_UDHCP_PORT(CLIENT_PORT = 68;)
 
+       /* Make sure fd 0,1,2 are open */
+       /* Setup the signal pipe on fds 3,4 - must be before openlog() */
+       udhcp_sp_setup();
+
        opt = getopt32(argv, "^"
                "fSI:va:"IF_FEATURE_UDHCP_PORT("P:")
                "\0"
@@ -904,9 +910,6 @@ int udhcpd_main(int argc UNUSED_PARAM, char **argv)
        if (server_data.auto_time > INT_MAX / 1000)
                server_data.auto_time = INT_MAX / 1000;
 
-       /* Make sure fd 0,1,2 are open */
-       bb_sanitize_stdio();
-
        /* Create pidfile */
        write_pidfile(server_data.pidfile);
        /* if (!..) bb_perror_msg("can't create pidfile %s", pidfile); */
@@ -942,9 +945,6 @@ int udhcpd_main(int argc UNUSED_PARAM, char **argv)
                goto ret;
        }
 
-       /* Setup the signal pipe */
-       udhcp_sp_setup();
-
  continue_with_autotime:
        timeout_end = monotonic_sec() + server_data.auto_time;
        while (1) { /* loop until universe collapses */
index 2ff78f0f25c788ee3c3a46488d611ec4b41ba099..81d1fc01a898382067febf3624b0e5fbacff3f04 100644 (file)
  */
 #include "common.h"
 
-/* Global variable: we access it from signal handler */
-static struct fd_pair signal_pipe;
+#define READ_FD  3
+#define WRITE_FD 4
 
 static void signal_handler(int sig)
 {
        int sv = errno;
        unsigned char ch = sig; /* use char, avoid dealing with partial writes */
-       if (write(signal_pipe.wr, &ch, 1) != 1)
+       if (write(WRITE_FD, &ch, 1) != 1)
                bb_perror_msg("can't send signal");
        errno = sv;
 }
@@ -36,12 +36,25 @@ static void signal_handler(int sig)
  * and installs the signal handler */
 void FAST_FUNC udhcp_sp_setup(void)
 {
+       struct fd_pair signal_pipe;
+
+       /* All callers also want this, so... */
+       bb_sanitize_stdio();
+
        /* was socketpair, but it needs AF_UNIX in kernel */
        xpiped_pair(signal_pipe);
-       close_on_exec_on(signal_pipe.rd);
-       close_on_exec_on(signal_pipe.wr);
-       ndelay_on(signal_pipe.rd);
-       ndelay_on(signal_pipe.wr);
+
+       /* usually we get fds 3 and 4, but if we get higher ones... */
+       if (signal_pipe.rd != READ_FD)
+               xmove_fd(signal_pipe.rd, READ_FD);
+       if (signal_pipe.wr != WRITE_FD)
+               xmove_fd(signal_pipe.wr, WRITE_FD);
+
+       close_on_exec_on(READ_FD);
+       close_on_exec_on(WRITE_FD);
+       ndelay_on(READ_FD);
+       ndelay_on(WRITE_FD);
+
        bb_signals(0
                + (1 << SIGUSR1)
                + (1 << SIGUSR2)
@@ -54,7 +67,7 @@ void FAST_FUNC udhcp_sp_setup(void)
  */
 void FAST_FUNC udhcp_sp_fd_set(struct pollfd pfds[2], int extra_fd)
 {
-       pfds[0].fd = signal_pipe.rd;
+       pfds[0].fd = READ_FD;
        pfds[0].events = POLLIN;
        pfds[1].fd = -1;
        if (extra_fd >= 0) {
@@ -74,7 +87,7 @@ int FAST_FUNC udhcp_sp_read(void)
        unsigned char sig;
 
        /* Can't block here, fd is in nonblocking mode */
-       if (safe_read(signal_pipe.rd, &sig, 1) != 1)
+       if (safe_read(READ_FD, &sig, 1) != 1)
                return 0;
 
        return sig;