Implement optional syslog logging using ordinary
[oweals/busybox.git] / networking / udhcp / dhcpc.c
index a1b6d46265d9b2485d1f10b342875084e4a0b404..5b2612e563c3a77d57bbec1f0da3ddac28d21457 100644 (file)
@@ -1,3 +1,4 @@
+/* vi: set sw=4 ts=4: */
 /* dhcpc.c
  *
  * udhcp DHCP client
@@ -61,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;
@@ -110,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);
@@ -148,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 */
                udhcp_run_script(NULL, "deconfig");
        }
-       LOG(LOG_INFO, "Entering released state");
+       bb_info_msg("Entering released state");
 
        change_mode(LISTEN_NONE);
        state = RELEASED;
@@ -169,11 +135,7 @@ static void client_background(void)
 }
 
 
-#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;
@@ -218,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);
@@ -228,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':
@@ -300,7 +262,7 @@ int main(int argc, char *argv[])
                        return 0;
                        break;
                default:
-                       show_usage();
+                       bb_show_usage();
                }
        }
 
@@ -348,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,10 +338,10 @@ int main(int argc, char *argv[])
                                } else {
                                        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 */
@@ -410,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 */
@@ -418,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 */
@@ -432,7 +394,7 @@ 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");
+                                       bb_info_msg("Lease lost, entering init state");
                                        udhcp_run_script(NULL, "deconfig");
                                        timeout = now;
                                        packet_num = 0;
@@ -458,25 +420,25 @@ int main(int argc, char *argv[])
                        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;
                        }
 
@@ -494,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;
@@ -504,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);
@@ -517,7 +479,7 @@ 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;
@@ -534,7 +496,7 @@ int main(int argc, char *argv[])
 
                                } else if (*message == DHCPNAK) {
                                        /* return to init state */
-                                       LOG(LOG_INFO, "Received DHCP NAK");
+                                       bb_info_msg("Received DHCP NAK");
                                        udhcp_run_script(&packet, "nak");
                                        if (state != REQUESTING)
                                                udhcp_run_script(NULL, "deconfig");
@@ -557,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");
                }
 
        }