From 80edead5ea731c0c144def4d249eb88fb16001ef Mon Sep 17 00:00:00 2001 From: Denis Vlasenko Date: Thu, 2 Aug 2007 22:31:05 +0000 Subject: [PATCH] udhcp: slight shrink udhcpd_main 1171 1208 +37 udhcpc_main 2363 2387 +24 dhcprelay_main 1145 1146 +1 dhcprelay_stopflag 4 1 -3 dhcprelay_signal_handler 11 8 -3 client_background 46 42 -4 udhcp_read_interface 230 211 -19 udhcp_make_pidfile 76 - -76 ------------------------------------------------------------------------------ (add/remove: 0/1 grow/shrink: 3/4 up/down: 62/-105) Total: -43 bytes text data bss dec hex filename 770052 1096 11228 782376 bf028 busybox_old 769980 1096 11228 782304 befe0 busybox_unstripped --- include/libbb.h | 4 +-- libbb/pidfile.c | 4 ++- networking/udhcp/common.c | 21 ----------- networking/udhcp/common.h | 2 -- networking/udhcp/dhcpc.c | 69 ++++++++++++++++++++---------------- networking/udhcp/dhcpd.c | 13 +++++-- networking/udhcp/dhcprelay.c | 17 +++++---- networking/udhcp/socket.c | 11 +++--- 8 files changed, 69 insertions(+), 72 deletions(-) diff --git a/include/libbb.h b/include/libbb.h index 1cbcb41e4..13bd2d688 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -619,8 +619,8 @@ llist_t *llist_rev(llist_t *list); * llist_t *llist_add_to(llist_t *old_head, void *data) * etc does not result in smaller code... */ -/* start_stop_daemon and (udhcpc with ifupdown) are special - they want to - * create pidfiles regardless of FEATURE_PIDFILE. */ +/* start_stop_daemon and udhcpc are special - they want + * to create pidfiles regardless of FEATURE_PIDFILE */ #if ENABLE_FEATURE_PIDFILE || defined(WANT_PIDFILE) int write_pidfile(const char *path); #define remove_pidfile(f) ((void)unlink(f)) diff --git a/libbb/pidfile.c b/libbb/pidfile.c index 50af91f4e..64e67c322 100644 --- a/libbb/pidfile.c +++ b/libbb/pidfile.c @@ -17,13 +17,15 @@ int write_pidfile(const char *path) char *end; char buf[sizeof(int)*3 + 2]; + if (!path) + return 1; /* we will overwrite stale pidfile */ pid_fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, 0666); if (pid_fd < 0) return 0; /* few bytes larger, but doesn't use stdio */ end = utoa_to_buf(getpid(), buf, sizeof(buf)); - end[0] = '\n'; + *end = '\n'; full_write(pid_fd, buf, end - buf + 1); close(pid_fd); return 1; diff --git a/networking/udhcp/common.c b/networking/udhcp/common.c index 108ab2e95..59c015575 100644 --- a/networking/udhcp/common.c +++ b/networking/udhcp/common.c @@ -1,11 +1,5 @@ /* vi: set sw=4 ts=4: */ /* common.c - * - * Functions for debugging and logging as well as some other - * simple helper functions. - * - * Russ Dill 2001-2003 - * Rewritten by Vladimir Oleynik (C) 2003 * * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. */ @@ -13,18 +7,3 @@ #include "common.h" const uint8_t MAC_BCAST_ADDR[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; - -void udhcp_make_pidfile(const char *pidfile) -{ - /* Make sure fd 0,1,2 are open */ - bb_sanitize_stdio(); - - /* Equivalent of doing a fflush after every \n */ - setlinebuf(stdout); - - /* Create pidfile */ - if (pidfile && !write_pidfile(pidfile)) - bb_perror_msg("cannot create pidfile %s", pidfile); - - bb_info_msg("%s (v%s) started", applet_name, BB_VER); -} diff --git a/networking/udhcp/common.h b/networking/udhcp/common.h index 588750462..bc21176a5 100644 --- a/networking/udhcp/common.h +++ b/networking/udhcp/common.h @@ -60,8 +60,6 @@ int udhcp_kernel_packet(struct dhcpMessage *payload, /**/ -void udhcp_make_pidfile(const char *pidfile); - void udhcp_run_script(struct dhcpMessage *packet, const char *name); // Still need to clean these up... diff --git a/networking/udhcp/dhcpc.c b/networking/udhcp/dhcpc.c index 4bb90c2da..efe208814 100644 --- a/networking/udhcp/dhcpc.c +++ b/networking/udhcp/dhcpc.c @@ -109,7 +109,7 @@ static void perform_release(void) static void client_background(void) { -#ifdef __uClinux__ +#if !BB_MMU bb_error_msg("cannot background in uclinux (yet)"); /* ... mainly because udhcpc calls client_background() * in _the _middle _of _udhcpc _run_, not at the start! @@ -119,8 +119,7 @@ static void client_background(void) bb_daemonize(0); logmode &= ~LOGMODE_STDIO; /* rewrite pidfile, as our pid is different now */ - if (client_config.pidfile) - write_pidfile(client_config.pidfile); + write_pidfile(client_config.pidfile); #endif /* Do not fork again. */ client_config.foreground = 1; @@ -148,19 +147,17 @@ int udhcpc_main(int argc, char **argv) char *str_c, *str_V, *str_h, *str_F, *str_r, *str_T, *str_t; uint32_t xid = 0; uint32_t lease = 0; /* can be given as 32-bit quantity */ - unsigned t1 = 0, t2 = 0; + unsigned t1 = 0, t2 = 0; /* what a wonderful names */ unsigned start = 0; unsigned now; unsigned opt; int max_fd; - int sig; int retval; int len; - int no_clientid = 0; - fd_set rfds; struct timeval tv; - struct dhcpMessage packet; struct in_addr temp_addr; + struct dhcpMessage packet; + fd_set rfds; enum { OPT_c = 1 << 0, @@ -224,8 +221,7 @@ int udhcpc_main(int argc, char **argv) if (opt & OPT_c) client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, str_c, 0); - if (opt & OPT_C) - no_clientid = 1; + //if (opt & OPT_C) if (opt & OPT_V) client_config.vendorclass = alloc_dhcp_option(DHCP_VENDOR, str_V, 0); if (opt & OPT_f) @@ -262,7 +258,7 @@ int udhcpc_main(int argc, char **argv) if (opt & OPT_t) client_config.retries = xatoi_u(str_t); if (opt & OPT_v) { - printf("version %s\n\n", BB_VER); + printf("version %s\n", BB_VER); return 0; } @@ -272,14 +268,23 @@ int udhcpc_main(int argc, char **argv) } if (read_interface(client_config.interface, &client_config.ifindex, - NULL, client_config.arp) < 0) + NULL, client_config.arp)) return 1; - /* Sanitize fd's and write pidfile */ - udhcp_make_pidfile(client_config.pidfile); + /* Make sure fd 0,1,2 are open */ + bb_sanitize_stdio(); + /* Equivalent of doing a fflush after every \n */ + setlinebuf(stdout); + + /* Create pidfile */ + write_pidfile(client_config.pidfile); + /* if (!..) bb_perror_msg("cannot create pidfile %s", pidfile); */ + + /* Goes to stdout and possibly syslog */ + bb_info_msg("%s (v%s) started", applet_name, BB_VER); /* if not set, and not suppressed, setup the default client ID */ - if (!client_config.clientid && !no_clientid) { + if (!client_config.clientid && !(opt & OPT_C)) { client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, "", 7); client_config.clientid[OPT_DATA] = 1; memcpy(client_config.clientid + OPT_DATA+1, client_config.arp, 6); @@ -294,9 +299,12 @@ int udhcpc_main(int argc, char **argv) state = INIT_SELECTING; udhcp_run_script(NULL, "deconfig"); change_mode(LISTEN_RAW); + tv.tv_sec = 0; + goto jump_in; for (;;) { tv.tv_sec = timeout - monotonic_sec(); + jump_in: tv.tv_usec = 0; if (listen_mode != LISTEN_NONE && sockfd < 0) { @@ -307,13 +315,20 @@ int udhcpc_main(int argc, char **argv) } max_fd = udhcp_sp_fd_set(&rfds, sockfd); + retval = 0; /* If we already timed out, fall through, else... */ if (tv.tv_sec > 0) { DEBUG("Waiting on select..."); retval = select(max_fd + 1, &rfds, NULL, NULL, &tv); - } else retval = 0; /* If we already timed out, fall through */ + } now = monotonic_sec(); - if (retval == 0) { + if (retval < 0) { + /* EINTR? signal was caught, don't panic */ + if (errno != EINTR) { + /* Else: an error occured, panic! */ + bb_perror_msg_and_die("select"); + } + } else if (retval == 0) { /* timeout dropped to zero */ switch (state) { case INIT_SELECTING: @@ -377,9 +392,8 @@ int udhcpc_main(int argc, char **argv) } else { /* send a request packet */ send_renew(xid, server_addr, requested_ip); /* unicast */ - t1 = (t2 - t1) / 2 + t1; - timeout = t1 + start; + timeout = start + t1; } break; case REBINDING: @@ -395,9 +409,8 @@ int udhcpc_main(int argc, char **argv) } else { /* send a request packet */ send_renew(xid, 0, requested_ip); /* broadcast */ - t2 = (lease - t2) / 2 + t2; - timeout = t2 + start; + timeout = start + t2; } break; case RELEASED: @@ -405,7 +418,7 @@ int udhcpc_main(int argc, char **argv) timeout = INT_MAX; break; } - } else if (retval > 0 && listen_mode != LISTEN_NONE && FD_ISSET(sockfd, &rfds)) { + } else if (listen_mode != LISTEN_NONE && FD_ISSET(sockfd, &rfds)) { /* a packet is ready, read it */ if (listen_mode == LISTEN_KERNEL) @@ -480,7 +493,7 @@ int udhcpc_main(int argc, char **argv) bb_info_msg("Lease of %s obtained, lease time %u", inet_ntoa(temp_addr), (unsigned)lease); start = now; - timeout = t1 + start; + timeout = start + t1; requested_ip = packet.yiaddr; udhcp_run_script(&packet, ((state == RENEWING || state == REBINDING) ? "renew" : "bound")); @@ -511,8 +524,9 @@ int udhcpc_main(int argc, char **argv) break; /* case BOUND, RELEASED: - ignore all packets */ } - } else if (retval > 0 && (sig = udhcp_sp_read(&rfds))) { - switch (sig) { + } else { + int signo = udhcp_sp_read(&rfds); + switch (signo) { case SIGUSR1: perform_renew(); break; @@ -525,11 +539,6 @@ int udhcpc_main(int argc, char **argv) perform_release(); goto ret0; } - } else if (retval == -1 && errno == EINTR) { - /* a signal was caught */ - } else { - /* An error occured */ - bb_perror_msg("select"); } } /* for (;;) */ ret0: diff --git a/networking/udhcp/dhcpd.c b/networking/udhcp/dhcpd.c index 8cac68195..89f57bfe8 100644 --- a/networking/udhcp/dhcpd.c +++ b/networking/udhcp/dhcpd.c @@ -51,7 +51,16 @@ int udhcpd_main(int argc, char **argv) * otherwise NOMMU machines will parse config twice */ read_config(argv[1] ? argv[1] : DHCPD_CONF_FILE); - udhcp_make_pidfile(server_config.pidfile); + /* Make sure fd 0,1,2 are open */ + bb_sanitize_stdio(); + /* Equivalent of doing a fflush after every \n */ + setlinebuf(stdout); + + /* Create pidfile */ + write_pidfile(server_config.pidfile); + /* if (!..) bb_perror_msg("cannot create pidfile %s", pidfile); */ + + bb_info_msg("%s (v%s) started", applet_name, BB_VER); option = find_option(server_config.options, DHCP_LEASE_TIME); server_config.lease = LEASE_TIME; @@ -72,7 +81,7 @@ int udhcpd_main(int argc, char **argv) read_leases(server_config.lease_file); if (read_interface(server_config.interface, &server_config.ifindex, - &server_config.server, server_config.arp) < 0) { + &server_config.server, server_config.arp)) { retval = 1; goto ret; } diff --git a/networking/udhcp/dhcprelay.c b/networking/udhcp/dhcprelay.c index 9bb7aeab5..4ed65a2d6 100644 --- a/networking/udhcp/dhcprelay.c +++ b/networking/udhcp/dhcprelay.c @@ -30,7 +30,7 @@ static struct xid_item { } dhcprelay_xid_list = {0, {0}, 0, 0, NULL}; -static struct xid_item * xid_add(uint32_t xid, struct sockaddr_in *ip, int client) +static struct xid_item *xid_add(uint32_t xid, struct sockaddr_in *ip, int client) { struct xid_item *item; @@ -48,7 +48,6 @@ static struct xid_item * xid_add(uint32_t xid, struct sockaddr_in *ip, int clien return item; } - static void xid_expire(void) { struct xid_item *item = dhcprelay_xid_list.next; @@ -56,7 +55,7 @@ static void xid_expire(void) time_t current_time = time(NULL); while (item != NULL) { - if ((current_time-item->timestamp) > MAX_LIFETIME) { + if ((current_time - item->timestamp) > MAX_LIFETIME) { last->next = item->next; free(item); item = last->next; @@ -67,7 +66,7 @@ static void xid_expire(void) } } -static struct xid_item * xid_find(uint32_t xid) +static struct xid_item *xid_find(uint32_t xid) { struct xid_item *item = dhcprelay_xid_list.next; while (item != NULL) { @@ -95,7 +94,6 @@ static void xid_del(uint32_t xid) } } - /** * get_dhcp_packet_type - gets the message type of a dhcp packet * p - pointer to the dhcp packet @@ -119,7 +117,8 @@ static int get_dhcp_packet_type(struct dhcpMessage *p) * signal_handler - handles signals ;-) * sig - sent signal */ -static int dhcprelay_stopflag; +static smallint dhcprelay_stopflag; + static void dhcprelay_signal_handler(int sig) { dhcprelay_stopflag = 1; @@ -130,7 +129,7 @@ static void dhcprelay_signal_handler(int sig) * dev_list - comma separated list of devices * returns array */ -static char ** get_client_devices(char *dev_list, int *client_number) +static char **get_client_devices(char *dev_list, int *client_number) { char *s, *list, **client_dev; int i, cn; @@ -286,7 +285,7 @@ static void dhcprelay_loop(int *fds, int num_sockets, int max_socket, char **cli (struct sockaddr *)(&client_addr), &addr_size); if (packlen <= 0) continue; - if (read_interface(clients[i-1], NULL, &dhcp_msg.giaddr, NULL) < 0) + if (read_interface(clients[i-1], NULL, &dhcp_msg.giaddr, NULL)) dhcp_msg.giaddr = gw_ip; pass_on(&dhcp_msg, packlen, i, fds, &client_addr, server_addr); } @@ -322,7 +321,7 @@ int dhcprelay_main(int argc, char **argv) num_sockets = init_sockets(clients, num_sockets, argv[2], fds, &max_socket); - if (read_interface(argv[2], NULL, &gw_ip, NULL) == -1) + if (read_interface(argv[2], NULL, &gw_ip, NULL)) return 1; dhcprelay_loop(fds, num_sockets, max_socket, clients, &server_addr, gw_ip); diff --git a/networking/udhcp/socket.c b/networking/udhcp/socket.c index 648f7623c..1e62f8e40 100644 --- a/networking/udhcp/socket.c +++ b/networking/udhcp/socket.c @@ -44,11 +44,11 @@ int read_interface(const char *interface, int *ifindex, uint32_t *addr, uint8_t struct sockaddr_in *our_ip; memset(&ifr, 0, sizeof(ifr)); - fd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW); - if (fd < 0) { - bb_perror_msg("socket failed"); - return -1; - } + fd = xsocket(AF_INET, SOCK_RAW, IPPROTO_RAW); +// if (fd < 0) { +// bb_perror_msg("socket failed"); +// return -1; +// } ifr.ifr_addr.sa_family = AF_INET; strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name)); @@ -83,6 +83,7 @@ int read_interface(const char *interface, int *ifindex, uint32_t *addr, uint8_t arp[0], arp[1], arp[2], arp[3], arp[4], arp[5]); } + close(fd); return 0; } -- 2.25.1