tar -Z, uncompress support
[oweals/busybox.git] / networking / udhcp / dhcpc.c
index b50b1ed030ce9469f3827655bb99ebd90a5a0a99..d18a963a99d9287e2233ad817019e3021612b798 100644 (file)
@@ -19,9 +19,7 @@
  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
  
-#include <stdio.h>
 #include <sys/time.h>
-#include <sys/types.h>
 #include <sys/file.h>
 #include <unistd.h>
 #include <getopt.h>
 #include "dhcpc.h"
 #include "options.h"
 #include "clientpacket.h"
-#include "packet.h"
 #include "script.h"
 #include "socket.h"
-#include "debug.h"
-#include "pidfile.h"
+#include "common.h"
 
 static int state;
 static unsigned long requested_ip; /* = 0 */
 static unsigned long server_addr;
 static unsigned long timeout;
 static int packet_num; /* = 0 */
-static int fd;
-static int signal_pipe[2];
+static int fd = -1;
 
 #define LISTEN_NONE 0
 #define LISTEN_KERNEL 1
 #define LISTEN_RAW 2
 static int listen_mode;
 
+#ifdef CONFIG_INSTALL_NO_USR
+#define DEFAULT_SCRIPT "/share/udhcpc/default.script"
+#else
 #define DEFAULT_SCRIPT "/usr/share/udhcpc/default.script"
+#endif
 
 struct client_config_t client_config = {
        /* Default options. */
@@ -76,38 +75,12 @@ struct client_config_t client_config = {
        arp: "\0\0\0\0\0\0",            /* appease gcc-3.0 */
 };
 
-#ifndef BB_VER
-static void show_usage(void)
-{
-       printf(
-"Usage: udhcpc [OPTIONS]\n\n"
-"  -c, --clientid=CLIENTID         Client identifier\n"
-"  -H, --hostname=HOSTNAME         Client hostname\n"
-"  -h                              Alias for -H\n"
-"  -f, --foreground                Do not fork after getting lease\n"
-"  -b, --background                Fork to background if lease cannot be\n"
-"                                  immediately negotiated.\n"
-"  -i, --interface=INTERFACE       Interface to use (default: eth0)\n"
-"  -n, --now                       Exit with failure if lease cannot be\n"
-"                                  immediately negotiated.\n"
-"  -p, --pidfile=file              Store process ID of daemon in file\n"
-"  -q, --quit                      Quit after obtaining lease\n"
-"  -r, --request=IP                IP address to request (default: none)\n"
-"  -s, --script=file               Run file at dhcp events (default:\n"
-"                                  " DEFAULT_SCRIPT ")\n"
-"  -v, --version                   Display version\n"
-       );
-       exit(0);
-}
-#endif
-
-
 /* just a little helper */
 static void change_mode(int new_mode)
 {
        DEBUG(LOG_INFO, "entering %s listen mode",
                new_mode ? (new_mode == 1 ? "kernel" : "raw") : "none");
-       close(fd);
+       if (fd >= 0) close(fd);
        fd = -1;
        listen_mode = new_mode;
 }
@@ -132,6 +105,7 @@ static void perform_renew(void)
                state = INIT_SELECTING;
                break;
        case INIT_SELECTING:
+               break;
        }
 
        /* start things over */
@@ -166,45 +140,15 @@ static void perform_release(void)
 }
 
 
-/* Exit and cleanup */
-static void exit_client(int retval)
-{
-       pidfile_delete(client_config.pidfile);
-       CLOSE_LOG();
-       exit(retval);
-}
-
-
-/* Signal handler */
-static void signal_handler(int sig)
+static void client_background(void)
 {
-       if (send(signal_pipe[1], &sig, sizeof(sig), MSG_DONTWAIT) < 0) {
-               LOG(LOG_ERR, "Could not send signal: %s",
-                       strerror(errno));
-       }
-}
-
-
-static void background(void)
-{
-       int pid_fd;
-
-       pid_fd = pidfile_acquire(client_config.pidfile); /* hold lock during fork. */
-       while (pid_fd >= 0 && pid_fd < 3) pid_fd = dup(pid_fd); /* don't let daemon close it */
-       if (daemon(0, 0) == -1) {
-               perror("fork");
-               exit_client(1);
-       }
+       background(client_config.pidfile);
        client_config.foreground = 1; /* Do not fork again. */
-       pidfile_write_release(pid_fd);
+       client_config.background_if_no_lease = 0;
 }
 
 
-#ifdef COMBINED_BINARY
 int udhcpc_main(int argc, char *argv[])
-#else
-int main(int argc, char *argv[])
-#endif
 {
        unsigned char *temp, *message;
        unsigned long t1 = 0, t2 = 0, xid = 0;
@@ -215,12 +159,11 @@ int main(int argc, char *argv[])
        int c, len;
        struct dhcpMessage packet;
        struct in_addr temp_addr;
-       int pid_fd;
        time_t now;
        int max_fd;
        int sig;
 
-       static struct option arg_options[] = {
+       static const struct option arg_options[] = {
                {"clientid",    required_argument,      0, 'c'},
                {"foreground",  no_argument,            0, 'f'},
                {"background",  no_argument,            0, 'b'},
@@ -233,7 +176,6 @@ int main(int argc, char *argv[])
                {"request",     required_argument,      0, 'r'},
                {"script",      required_argument,      0, 's'},
                {"version",     no_argument,            0, 'v'},
-               {"help",        no_argument,            0, '?'},
                {0, 0, 0, 0}
        };
 
@@ -246,7 +188,7 @@ int main(int argc, char *argv[])
                switch (c) {
                case 'c':
                        len = strlen(optarg) > 255 ? 255 : strlen(optarg);
-                       free(client_config.clientid);
+                       if (client_config.clientid) free(client_config.clientid);
                        client_config.clientid = xmalloc(len + 2);
                        client_config.clientid[OPT_CODE] = DHCP_CLIENT_ID;
                        client_config.clientid[OPT_LEN] = len;
@@ -262,7 +204,7 @@ int main(int argc, char *argv[])
                case 'h':
                case 'H':
                        len = strlen(optarg) > 255 ? 255 : strlen(optarg);
-                       free(client_config.hostname);
+                       if (client_config.hostname) free(client_config.hostname);
                        client_config.hostname = xmalloc(len + 2);
                        client_config.hostname[OPT_CODE] = DHCP_HOST_NAME;
                        client_config.hostname[OPT_LEN] = len;
@@ -287,23 +229,18 @@ int main(int argc, char *argv[])
                        client_config.script = optarg;
                        break;
                case 'v':
-                       printf("udhcpcd, version %s\n\n", VERSION);
-                       exit_client(0);
+                       bb_error_msg("version %s\n", VERSION);
+                       return(0);
                        break;
                default:
-                       show_usage();
+                       bb_show_usage();
                }
        }
 
-       OPEN_LOG("udhcpc");
-       LOG(LOG_INFO, "udhcp client (v%s) started", VERSION);
-
-       pid_fd = pidfile_acquire(client_config.pidfile);
-       pidfile_write_release(pid_fd);
-
+       start_log("client");
        if (read_interface(client_config.interface, &client_config.ifindex, 
                           NULL, client_config.arp) < 0)
-               exit_client(1);
+               return(1);
                
        if (!client_config.clientid) {
                client_config.clientid = xmalloc(6 + 3);
@@ -314,10 +251,7 @@ int main(int argc, char *argv[])
        }
 
        /* setup signal handlers */
-       socketpair(AF_UNIX, SOCK_STREAM, 0, signal_pipe);
-       signal(SIGUSR1, signal_handler);
-       signal(SIGUSR2, signal_handler);
-       signal(SIGTERM, signal_handler);
+       udhcp_set_signal_pipe(SIGUSR2);
        
        state = INIT_SELECTING;
        run_script(NULL, "deconfig");
@@ -335,16 +269,16 @@ int main(int argc, char *argv[])
                        else
                                fd = raw_socket(client_config.ifindex);
                        if (fd < 0) {
-                               LOG(LOG_ERR, "FATAL: couldn't listen on socket, %s", strerror(errno));
-                               exit_client(0);
+                               LOG(LOG_ERR, "FATAL: couldn't listen on socket, %m");
+                               return(0);
                        }
                }
                if (fd >= 0) FD_SET(fd, &rfds);
-               FD_SET(signal_pipe[0], &rfds);          
+               FD_SET(udhcp_signal_pipe[0], &rfds);
 
                if (tv.tv_sec > 0) {
-                       DEBUG(LOG_INFO, "Waiting on select...\n");
-                       max_fd = signal_pipe[0] > fd ? signal_pipe[0] : fd;
+                       DEBUG(LOG_INFO, "Waiting on select...");
+                       max_fd = udhcp_signal_pipe[0] > fd ? udhcp_signal_pipe[0] : fd;
                        retval = select(max_fd + 1, &rfds, NULL, NULL, &tv);
                } else retval = 0; /* If we already timed out, fall through */
 
@@ -363,12 +297,13 @@ int main(int argc, char *argv[])
                                        timeout = now + ((packet_num == 2) ? 4 : 2);
                                        packet_num++;
                                } else {
+                                       run_script(NULL, "leasefail");
                                        if (client_config.background_if_no_lease) {
                                                LOG(LOG_INFO, "No lease, forking to background.");
-                                               background();
+                                               client_background();
                                        } else if (client_config.abort_if_no_lease) {
                                                LOG(LOG_INFO, "No lease, failing.");
-                                               exit_client(1);
+                                               return(1);
                                        }
                                        /* wait to try again */
                                        packet_num = 0;
@@ -446,7 +381,7 @@ int main(int argc, char *argv[])
                        else len = get_raw_packet(&packet, fd);
                        
                        if (len == -1 && errno != EINTR) {
-                               DEBUG(LOG_INFO, "error on read, %s, reopening socket", strerror(errno));
+                               DEBUG(LOG_INFO, "error on read, %m, reopening socket");
                                change_mode(listen_mode); /* just close and reopen */
                        }
                        if (len < 0) continue;
@@ -510,9 +445,9 @@ int main(int argc, char *argv[])
                                        state = BOUND;
                                        change_mode(LISTEN_NONE);
                                        if (client_config.quit_after_lease) 
-                                               exit_client(0);
+                                               return(0);
                                        if (!client_config.foreground)
-                                               background();
+                                               client_background();
 
                                } else if (*message == DHCPNAK) {
                                        /* return to init state */
@@ -530,10 +465,9 @@ int main(int argc, char *argv[])
                                break;
                        /* case BOUND, RELEASED: - ignore all packets */
                        }       
-               } else if (retval > 0 && FD_ISSET(signal_pipe[0], &rfds)) {
-                       if (read(signal_pipe[0], &sig, sizeof(signal)) < 0) {
-                               DEBUG(LOG_ERR, "Could not read signal: %s", 
-                                       strerror(errno));
+               } else if (retval > 0 && FD_ISSET(udhcp_signal_pipe[0], &rfds)) {
+                       if (read(udhcp_signal_pipe[0], &sig, sizeof(sig)) < 0) {
+                               DEBUG(LOG_ERR, "Could not read signal: %m");
                                continue; /* probably just EINTR */
                        }
                        switch (sig) {
@@ -545,7 +479,7 @@ int main(int argc, char *argv[])
                                break;
                        case SIGTERM:
                                LOG(LOG_INFO, "Received SIGTERM");
-                               exit_client(0);
+                               return(0);
                        }
                } else if (retval == -1 && errno == EINTR) {
                        /* a signal was caught */               
@@ -557,4 +491,3 @@ int main(int argc, char *argv[])
        }
        return 0;
 }
-