Implement optional syslog logging using ordinary
[oweals/busybox.git] / networking / udhcp / dhcpc.c
index 222bd6519b5335815562c4b5325f0e80476e2d96..5b2612e563c3a77d57bbec1f0da3ddac28d21457 100644 (file)
@@ -1,3 +1,4 @@
+/* vi: set sw=4 ts=4: */
 /* dhcpc.c
  *
  * udhcp DHCP client
@@ -7,7 +8,6 @@
  * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
  */
 
-#include <sys/time.h>
 #include <sys/file.h>
 #include <unistd.h>
 #include <getopt.h>
@@ -28,7 +28,6 @@
 #include "options.h"
 #include "clientpacket.h"
 #include "clientsocket.h"
-#include "script.h"
 #include "socket.h"
 #include "signalpipe.h"
 
@@ -63,45 +62,10 @@ struct client_config_t client_config = {
        .arp = "\0\0\0\0\0\0",          /* appease gcc-3.0 */
 };
 
-#ifndef IN_BUSYBOX
-static void ATTRIBUTE_NORETURN show_usage(void)
-{
-       printf(
-"Usage: udhcpc [OPTIONS]\n\n"
-"  -c, --clientid=CLIENTID         Set client identifier - type is first char\n"
-"  -C, --clientid-none             Suppress default client identifier\n"
-"  -V, --vendorclass=CLASSID       Set vendor class identifier\n"
-"  -H, --hostname=HOSTNAME         Client hostname\n"
-"  -h                              Alias for -H\n"
-"  -F, --fqdn=FQDN                 Client fully qualified domain name\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"
-"  -T, --timeout=seconds           Try to get the lease for the amount of\n"
-"                                  seconds (default: 3)\n"
-"  -t, --retries=NUM               Send up to NUM request packets\n"
-"  -v, --version                   Display version\n"
-       );
-       exit(0);
-}
-#else
-#define show_usage bb_show_usage
-extern void show_usage(void) ATTRIBUTE_NORETURN;
-#endif
-
-
 /* just a little helper */
 static void change_mode(int new_mode)
 {
-       DEBUG(LOG_INFO, "entering %s listen mode",
+       DEBUG("entering %s listen mode",
                new_mode ? (new_mode == 1 ? "kernel" : "raw") : "none");
        if (fd >= 0) close(fd);
        fd = -1;
@@ -112,7 +76,7 @@ static void change_mode(int new_mode)
 /* perform a renew */
 static void perform_renew(void)
 {
-       LOG(LOG_INFO, "Performing a DHCP renew");
+       bb_info_msg("Performing a DHCP renew");
        switch (state) {
        case BOUND:
                change_mode(LISTEN_KERNEL);
@@ -121,7 +85,7 @@ static void perform_renew(void)
                state = RENEW_REQUESTED;
                break;
        case RENEW_REQUESTED: /* impatient are we? fine, square 1 */
-               run_script(NULL, "deconfig");
+               udhcp_run_script(NULL, "deconfig");
        case REQUESTING:
        case RELEASED:
                change_mode(LISTEN_RAW);
@@ -150,12 +114,12 @@ static void perform_release(void)
                temp_addr.s_addr = server_addr;
                sprintf(buffer, "%s", inet_ntoa(temp_addr));
                temp_addr.s_addr = requested_ip;
-               LOG(LOG_INFO, "Unicasting a release of %s to %s",
+               bb_info_msg("Unicasting a release of %s to %s",
                                inet_ntoa(temp_addr), buffer);
                send_release(server_addr, requested_ip); /* unicast */
-               run_script(NULL, "deconfig");
+               udhcp_run_script(NULL, "deconfig");
        }
-       LOG(LOG_INFO, "Entering released state");
+       bb_info_msg("Entering released state");
 
        change_mode(LISTEN_NONE);
        state = RELEASED;
@@ -165,17 +129,13 @@ static void perform_release(void)
 
 static void client_background(void)
 {
-       background(client_config.pidfile);
+       udhcp_background(client_config.pidfile);
        client_config.foreground = 1; /* Do not fork again. */
        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
 {
        uint8_t *temp, *message;
        unsigned long t1 = 0, t2 = 0, xid = 0;
@@ -208,7 +168,7 @@ int main(int argc, char *argv[])
                {"script",      required_argument,      0, 's'},
                {"timeout",     required_argument,      0, 'T'},
                {"version",     no_argument,            0, 'v'},
-               {"retries",     required_argument,      0, 't'},                
+               {"retries",     required_argument,      0, 't'},
                {0, 0, 0, 0}
        };
 
@@ -220,7 +180,7 @@ int main(int argc, char *argv[])
 
                switch (c) {
                case 'c':
-                       if (no_clientid) show_usage();
+                       if (no_clientid) bb_show_usage();
                        len = strlen(optarg) > 255 ? 255 : strlen(optarg);
                        free(client_config.clientid);
                        client_config.clientid = xmalloc(len + 2);
@@ -230,7 +190,7 @@ int main(int argc, char *argv[])
                        strncpy((char*)client_config.clientid + OPT_DATA, optarg, len);
                        break;
                case 'C':
-                       if (client_config.clientid) show_usage();
+                       if (client_config.clientid) bb_show_usage();
                        no_clientid = 1;
                        break;
                case 'V':
@@ -298,16 +258,16 @@ int main(int argc, char *argv[])
                        client_config.retries = atoi(optarg);
                        break;
                case 'v':
-                       printf("udhcpcd, version %s\n\n", VERSION);
+                       printf("version %s\n\n", BB_VER);
                        return 0;
                        break;
                default:
-                       show_usage();
+                       bb_show_usage();
                }
        }
 
        /* Start the log, sanitize fd's, and write a pid file */
-       start_log_and_pid("udhcpc", client_config.pidfile);
+       udhcp_start_log_and_pid("udhcpc", client_config.pidfile);
 
        if (read_interface(client_config.interface, &client_config.ifindex,
                           NULL, client_config.arp) < 0)
@@ -323,12 +283,12 @@ int main(int argc, char *argv[])
        }
 
        if (!client_config.vendorclass) {
-               client_config.vendorclass = xmalloc(sizeof("udhcp "VERSION) + 2);
+               client_config.vendorclass = xmalloc(sizeof("udhcp "BB_VER) + 2);
                client_config.vendorclass[OPT_CODE] = DHCP_VENDOR;
-               client_config.vendorclass[OPT_LEN] = sizeof("udhcp "VERSION) - 1;
+               client_config.vendorclass[OPT_LEN] = sizeof("udhcp "BB_VER) - 1;
                client_config.vendorclass[OPT_DATA] = 1;
                memcpy(&client_config.vendorclass[OPT_DATA],
-                       "udhcp "VERSION, sizeof("udhcp "VERSION) - 1);
+                       "udhcp "BB_VER, sizeof("udhcp "BB_VER) - 1);
        }
 
 
@@ -336,7 +296,7 @@ int main(int argc, char *argv[])
        udhcp_sp_setup();
 
        state = INIT_SELECTING;
-       run_script(NULL, "deconfig");
+       udhcp_run_script(NULL, "deconfig");
        change_mode(LISTEN_RAW);
 
        for (;;) {
@@ -350,14 +310,14 @@ 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, %m");
+                               bb_perror_msg("FATAL: couldn't listen on socket");
                                return 0;
                        }
                }
                max_fd = udhcp_sp_fd_set(&rfds, fd);
 
                if (tv.tv_sec > 0) {
-                       DEBUG(LOG_INFO, "Waiting on select...");
+                       DEBUG("Waiting on select...");
                        retval = select(max_fd + 1, &rfds, NULL, NULL, &tv);
                } else retval = 0; /* If we already timed out, fall through */
 
@@ -376,12 +336,12 @@ int main(int argc, char *argv[])
                                        timeout = now + client_config.timeout;
                                        packet_num++;
                                } else {
-                                       run_script(NULL, "leasefail");
+                                       udhcp_run_script(NULL, "leasefail");
                                        if (client_config.background_if_no_lease) {
-                                               LOG(LOG_INFO, "No lease, forking to background.");
+                                               bb_info_msg("No lease, forking to background");
                                                client_background();
                                        } else if (client_config.abort_if_no_lease) {
-                                               LOG(LOG_INFO, "No lease, failing.");
+                                               bb_info_msg("No lease, failing");
                                                return 1;
                                        }
                                        /* wait to try again */
@@ -401,7 +361,7 @@ int main(int argc, char *argv[])
                                        packet_num++;
                                } else {
                                        /* timed out, go back to init state */
-                                       if (state == RENEW_REQUESTED) run_script(NULL, "deconfig");
+                                       if (state == RENEW_REQUESTED) udhcp_run_script(NULL, "deconfig");
                                        state = INIT_SELECTING;
                                        timeout = now;
                                        packet_num = 0;
@@ -412,7 +372,7 @@ int main(int argc, char *argv[])
                                /* Lease is starting to run out, time to enter renewing state */
                                state = RENEWING;
                                change_mode(LISTEN_KERNEL);
-                               DEBUG(LOG_INFO, "Entering renew state");
+                               DEBUG("Entering renew state");
                                /* fall right through */
                        case RENEWING:
                                /* Either set a new T1, or enter REBINDING state */
@@ -420,7 +380,7 @@ int main(int argc, char *argv[])
                                        /* timed out, enter rebinding state */
                                        state = REBINDING;
                                        timeout = now + (t2 - t1);
-                                       DEBUG(LOG_INFO, "Entering rebinding state");
+                                       DEBUG("Entering rebinding state");
                                } else {
                                        /* send a request packet */
                                        send_renew(xid, server_addr, requested_ip); /* unicast */
@@ -434,8 +394,8 @@ int main(int argc, char *argv[])
                                if ((lease - t2) <= (lease / 14400 + 1)) {
                                        /* timed out, enter init state */
                                        state = INIT_SELECTING;
-                                       LOG(LOG_INFO, "Lease lost, entering init state");
-                                       run_script(NULL, "deconfig");
+                                       bb_info_msg("Lease lost, entering init state");
+                                       udhcp_run_script(NULL, "deconfig");
                                        timeout = now;
                                        packet_num = 0;
                                        change_mode(LISTEN_RAW);
@@ -456,29 +416,29 @@ int main(int argc, char *argv[])
                        /* a packet is ready, read it */
 
                        if (listen_mode == LISTEN_KERNEL)
-                               len = get_packet(&packet, fd);
+                               len = udhcp_get_packet(&packet, fd);
                        else len = get_raw_packet(&packet, fd);
 
                        if (len == -1 && errno != EINTR) {
-                               DEBUG(LOG_INFO, "error on read, %m, reopening socket");
+                               DEBUG("error on read, %s, reopening socket", strerror(errno));
                                change_mode(listen_mode); /* just close and reopen */
                        }
                        if (len < 0) continue;
 
                        if (packet.xid != xid) {
-                               DEBUG(LOG_INFO, "Ignoring XID %lx (our xid is %lx)",
+                               DEBUG("Ignoring XID %lx (our xid is %lx)",
                                        (unsigned long) packet.xid, xid);
                                continue;
                        }
 
                        /* Ignore packets that aren't for us */
                        if (memcmp(packet.chaddr, client_config.arp, 6)) {
-                               DEBUG(LOG_INFO, "packet does not have our chaddr -- ignoring");
+                               DEBUG("Packet does not have our chaddr - ignoring");
                                continue;
                        }
 
                        if ((message = get_option(&packet, DHCP_MESSAGE_TYPE)) == NULL) {
-                               DEBUG(LOG_ERR, "couldnt get option from packet -- ignoring");
+                               bb_error_msg("Couldnt get option from packet - ignoring");
                                continue;
                        }
 
@@ -496,7 +456,7 @@ int main(int argc, char *argv[])
                                                timeout = now;
                                                packet_num = 0;
                                        } else {
-                                               DEBUG(LOG_ERR, "No server ID in message");
+                                               bb_error_msg("No server ID in message");
                                        }
                                }
                                break;
@@ -506,7 +466,7 @@ int main(int argc, char *argv[])
                        case REBINDING:
                                if (*message == DHCPACK) {
                                        if (!(temp = get_option(&packet, DHCP_LEASE_TIME))) {
-                                               LOG(LOG_ERR, "No lease time with ACK, using 1 hour lease");
+                                               bb_error_msg("No lease time with ACK, using 1 hour lease");
                                                lease = 60 * 60;
                                        } else {
                                                memcpy(&lease, temp, 4);
@@ -519,12 +479,12 @@ int main(int argc, char *argv[])
                                        /* little fixed point for n * .875 */
                                        t2 = (lease * 0x7) >> 3;
                                        temp_addr.s_addr = packet.yiaddr;
-                                       LOG(LOG_INFO, "Lease of %s obtained, lease time %ld",
+                                       bb_info_msg("Lease of %s obtained, lease time %ld",
                                                inet_ntoa(temp_addr), lease);
                                        start = now;
                                        timeout = t1 + start;
                                        requested_ip = packet.yiaddr;
-                                       run_script(&packet,
+                                       udhcp_run_script(&packet,
                                                   ((state == RENEWING || state == REBINDING) ? "renew" : "bound"));
 
                                        state = BOUND;
@@ -536,10 +496,10 @@ int main(int argc, char *argv[])
 
                                } else if (*message == DHCPNAK) {
                                        /* return to init state */
-                                       LOG(LOG_INFO, "Received DHCP NAK");
-                                       run_script(&packet, "nak");
+                                       bb_info_msg("Received DHCP NAK");
+                                       udhcp_run_script(&packet, "nak");
                                        if (state != REQUESTING)
-                                               run_script(NULL, "deconfig");
+                                               udhcp_run_script(NULL, "deconfig");
                                        state = INIT_SELECTING;
                                        timeout = now;
                                        requested_ip = 0;
@@ -559,14 +519,14 @@ int main(int argc, char *argv[])
                                perform_release();
                                break;
                        case SIGTERM:
-                               LOG(LOG_INFO, "Received SIGTERM");
+                               bb_info_msg("Received SIGTERM");
                                return 0;
                        }
                } else if (retval == -1 && errno == EINTR) {
                        /* a signal was caught */
                } else {
                        /* An error occured */
-                       DEBUG(LOG_ERR, "Error on select");
+                       bb_perror_msg("select");
                }
 
        }