diff options
author | Russ Dill <Russ.Dill@asu.edu> | 2003-12-18 22:25:38 +0000 |
---|---|---|
committer | Russ Dill <Russ.Dill@asu.edu> | 2003-12-18 22:25:38 +0000 |
commit | 4e864a36b611f56c6b347be1dace4e5e805a3eb8 (patch) | |
tree | 6582084290de4bead010f5fb8ff0a0a6d2b573fd /networking/udhcp/dhcpd.c | |
parent | e30495654d8bb38f7ea234d9d0ab0929525501e3 (diff) | |
download | busybox-w32-4e864a36b611f56c6b347be1dace4e5e805a3eb8.tar.gz busybox-w32-4e864a36b611f56c6b347be1dace4e5e805a3eb8.tar.bz2 busybox-w32-4e864a36b611f56c6b347be1dace4e5e805a3eb8.zip |
Finish remerging busybox udhcp and udhcp. Some cleanups as well.
Diffstat (limited to 'networking/udhcp/dhcpd.c')
-rw-r--r-- | networking/udhcp/dhcpd.c | 61 |
1 files changed, 30 insertions, 31 deletions
diff --git a/networking/udhcp/dhcpd.c b/networking/udhcp/dhcpd.c index c21cb72a2..aabab38b0 100644 --- a/networking/udhcp/dhcpd.c +++ b/networking/udhcp/dhcpd.c | |||
@@ -43,6 +43,7 @@ | |||
43 | #include "files.h" | 43 | #include "files.h" |
44 | #include "serverpacket.h" | 44 | #include "serverpacket.h" |
45 | #include "common.h" | 45 | #include "common.h" |
46 | #include "signalpipe.h" | ||
46 | 47 | ||
47 | 48 | ||
48 | /* globals */ | 49 | /* globals */ |
@@ -50,7 +51,11 @@ struct dhcpOfferedAddr *leases; | |||
50 | struct server_config_t server_config; | 51 | struct server_config_t server_config; |
51 | 52 | ||
52 | 53 | ||
54 | #ifdef COMBINED_BINARY | ||
53 | int udhcpd_main(int argc, char *argv[]) | 55 | int udhcpd_main(int argc, char *argv[]) |
56 | #else | ||
57 | int main(int argc, char *argv[]) | ||
58 | #endif | ||
54 | { | 59 | { |
55 | fd_set rfds; | 60 | fd_set rfds; |
56 | struct timeval tv; | 61 | struct timeval tv; |
@@ -64,16 +69,13 @@ int udhcpd_main(int argc, char *argv[]) | |||
64 | struct option_set *option; | 69 | struct option_set *option; |
65 | struct dhcpOfferedAddr *lease; | 70 | struct dhcpOfferedAddr *lease; |
66 | int max_sock; | 71 | int max_sock; |
67 | int sig; | ||
68 | unsigned long num_ips; | 72 | unsigned long num_ips; |
69 | 73 | ||
70 | start_log("server"); | ||
71 | |||
72 | memset(&server_config, 0, sizeof(struct server_config_t)); | 74 | memset(&server_config, 0, sizeof(struct server_config_t)); |
73 | 75 | read_config(argc < 2 ? DHCPD_CONF_FILE : argv[1]); | |
74 | if (argc < 2) | 76 | |
75 | read_config(DHCPD_CONF_FILE); | 77 | /* Start the log, sanitize fd's, and write a pid file */ |
76 | else read_config(argv[1]); | 78 | start_log_and_pid("udhcpd", server_config.pidfile); |
77 | 79 | ||
78 | if ((option = find_option(server_config.options, DHCP_LEASE_TIME))) { | 80 | if ((option = find_option(server_config.options, DHCP_LEASE_TIME))) { |
79 | memcpy(&server_config.lease, option->data + 2, 4); | 81 | memcpy(&server_config.lease, option->data + 2, 4); |
@@ -90,18 +92,19 @@ int udhcpd_main(int argc, char *argv[]) | |||
90 | server_config.max_leases = num_ips; | 92 | server_config.max_leases = num_ips; |
91 | } | 93 | } |
92 | 94 | ||
93 | leases = xcalloc(sizeof(struct dhcpOfferedAddr), server_config.max_leases); | 95 | leases = xcalloc(server_config.max_leases, sizeof(struct dhcpOfferedAddr)); |
94 | read_leases(server_config.lease_file); | 96 | read_leases(server_config.lease_file); |
95 | 97 | ||
96 | if (read_interface(server_config.interface, &server_config.ifindex, | 98 | if (read_interface(server_config.interface, &server_config.ifindex, |
97 | &server_config.server, server_config.arp) < 0) | 99 | &server_config.server, server_config.arp) < 0) |
98 | return(1); | 100 | return 1; |
99 | 101 | ||
100 | #ifndef CONFIG_FEATURE_UDHCP_DEBUG | 102 | #ifndef UDHCP_DEBUG |
101 | background(server_config.pidfile); | 103 | background(server_config.pidfile); /* hold lock during fork. */ |
102 | #endif | 104 | #endif |
103 | 105 | ||
104 | udhcp_set_signal_pipe(0); | 106 | /* Setup the signal pipe */ |
107 | udhcp_sp_setup(); | ||
105 | 108 | ||
106 | timeout_end = time(0) + server_config.auto_time; | 109 | timeout_end = time(0) + server_config.auto_time; |
107 | while(1) { /* loop until universe collapses */ | 110 | while(1) { /* loop until universe collapses */ |
@@ -109,18 +112,15 @@ int udhcpd_main(int argc, char *argv[]) | |||
109 | if (server_socket < 0) | 112 | if (server_socket < 0) |
110 | if ((server_socket = listen_socket(INADDR_ANY, SERVER_PORT, server_config.interface)) < 0) { | 113 | if ((server_socket = listen_socket(INADDR_ANY, SERVER_PORT, server_config.interface)) < 0) { |
111 | LOG(LOG_ERR, "FATAL: couldn't create server socket, %m"); | 114 | LOG(LOG_ERR, "FATAL: couldn't create server socket, %m"); |
112 | return(2); | 115 | return 2; |
113 | } | 116 | } |
114 | 117 | ||
115 | FD_ZERO(&rfds); | 118 | max_sock = udhcp_sp_fd_set(&rfds, server_socket); |
116 | FD_SET(server_socket, &rfds); | ||
117 | FD_SET(udhcp_signal_pipe[0], &rfds); | ||
118 | if (server_config.auto_time) { | 119 | if (server_config.auto_time) { |
119 | tv.tv_sec = timeout_end - time(0); | 120 | tv.tv_sec = timeout_end - time(0); |
120 | tv.tv_usec = 0; | 121 | tv.tv_usec = 0; |
121 | } | 122 | } |
122 | if (!server_config.auto_time || tv.tv_sec > 0) { | 123 | if (!server_config.auto_time || tv.tv_sec > 0) { |
123 | max_sock = server_socket > udhcp_signal_pipe[0] ? server_socket : udhcp_signal_pipe[0]; | ||
124 | retval = select(max_sock + 1, &rfds, NULL, NULL, | 124 | retval = select(max_sock + 1, &rfds, NULL, NULL, |
125 | server_config.auto_time ? &tv : NULL); | 125 | server_config.auto_time ? &tv : NULL); |
126 | } else retval = 0; /* If we already timed out, fall through */ | 126 | } else retval = 0; /* If we already timed out, fall through */ |
@@ -134,20 +134,18 @@ int udhcpd_main(int argc, char *argv[]) | |||
134 | continue; | 134 | continue; |
135 | } | 135 | } |
136 | 136 | ||
137 | if (FD_ISSET(udhcp_signal_pipe[0], &rfds)) { | 137 | switch (udhcp_sp_read(&rfds)) { |
138 | if (read(udhcp_signal_pipe[0], &sig, sizeof(sig)) < 0) | 138 | case SIGUSR1: |
139 | continue; /* probably just EINTR */ | 139 | LOG(LOG_INFO, "Received a SIGUSR1"); |
140 | switch (sig) { | 140 | write_leases(); |
141 | case SIGUSR1: | 141 | /* why not just reset the timeout, eh */ |
142 | LOG(LOG_INFO, "Received a SIGUSR1"); | 142 | timeout_end = time(0) + server_config.auto_time; |
143 | write_leases(); | 143 | continue; |
144 | /* why not just reset the timeout, eh */ | 144 | case SIGTERM: |
145 | timeout_end = time(0) + server_config.auto_time; | 145 | LOG(LOG_INFO, "Received a SIGTERM"); |
146 | continue; | 146 | return 0; |
147 | case SIGTERM: | 147 | case 0: break; /* no signal */ |
148 | LOG(LOG_INFO, "Received a SIGTERM"); | 148 | default: continue; /* signal or error (probably EINTR) */ |
149 | return(0); | ||
150 | } | ||
151 | } | 149 | } |
152 | 150 | ||
153 | if ((bytes = get_packet(&packet, server_socket)) < 0) { /* this waits for a packet - idle */ | 151 | if ((bytes = get_packet(&packet, server_socket)) < 0) { /* this waits for a packet - idle */ |
@@ -251,3 +249,4 @@ int udhcpd_main(int argc, char *argv[]) | |||
251 | 249 | ||
252 | return 0; | 250 | return 0; |
253 | } | 251 | } |
252 | |||