diff options
Diffstat (limited to 'networking/udhcp/dhcpc.c')
-rw-r--r-- | networking/udhcp/dhcpc.c | 51 |
1 files changed, 23 insertions, 28 deletions
diff --git a/networking/udhcp/dhcpc.c b/networking/udhcp/dhcpc.c index 55664abf9..a180cc514 100644 --- a/networking/udhcp/dhcpc.c +++ b/networking/udhcp/dhcpc.c | |||
@@ -38,9 +38,11 @@ | |||
38 | #include "dhcpc.h" | 38 | #include "dhcpc.h" |
39 | #include "options.h" | 39 | #include "options.h" |
40 | #include "clientpacket.h" | 40 | #include "clientpacket.h" |
41 | #include "clientsocket.h" | ||
41 | #include "script.h" | 42 | #include "script.h" |
42 | #include "socket.h" | 43 | #include "socket.h" |
43 | #include "common.h" | 44 | #include "common.h" |
45 | #include "signalpipe.h" | ||
44 | 46 | ||
45 | static int state; | 47 | static int state; |
46 | static unsigned long requested_ip; /* = 0 */ | 48 | static unsigned long requested_ip; /* = 0 */ |
@@ -54,12 +56,6 @@ static int fd = -1; | |||
54 | #define LISTEN_RAW 2 | 56 | #define LISTEN_RAW 2 |
55 | static int listen_mode; | 57 | static int listen_mode; |
56 | 58 | ||
57 | #ifdef CONFIG_INSTALL_NO_USR | ||
58 | #define DEFAULT_SCRIPT "/share/udhcpc/default.script" | ||
59 | #else | ||
60 | #define DEFAULT_SCRIPT "/usr/share/udhcpc/default.script" | ||
61 | #endif | ||
62 | |||
63 | struct client_config_t client_config = { | 59 | struct client_config_t client_config = { |
64 | /* Default options. */ | 60 | /* Default options. */ |
65 | abort_if_no_lease: 0, | 61 | abort_if_no_lease: 0, |
@@ -78,7 +74,7 @@ struct client_config_t client_config = { | |||
78 | #ifndef IN_BUSYBOX | 74 | #ifndef IN_BUSYBOX |
79 | static void __attribute__ ((noreturn)) show_usage(void) | 75 | static void __attribute__ ((noreturn)) show_usage(void) |
80 | { | 76 | { |
81 | printf( | 77 | printf( |
82 | "Usage: udhcpc [OPTIONS]\n\n" | 78 | "Usage: udhcpc [OPTIONS]\n\n" |
83 | " -c, --clientid=CLIENTID Client identifier\n" | 79 | " -c, --clientid=CLIENTID Client identifier\n" |
84 | " -H, --hostname=HOSTNAME Client hostname\n" | 80 | " -H, --hostname=HOSTNAME Client hostname\n" |
@@ -95,8 +91,8 @@ static void __attribute__ ((noreturn)) show_usage(void) | |||
95 | " -s, --script=file Run file at dhcp events (default:\n" | 91 | " -s, --script=file Run file at dhcp events (default:\n" |
96 | " " DEFAULT_SCRIPT ")\n" | 92 | " " DEFAULT_SCRIPT ")\n" |
97 | " -v, --version Display version\n" | 93 | " -v, --version Display version\n" |
98 | ); | 94 | ); |
99 | exit(0); | 95 | exit(0); |
100 | } | 96 | } |
101 | #else | 97 | #else |
102 | #define show_usage bb_show_usage | 98 | #define show_usage bb_show_usage |
@@ -177,7 +173,11 @@ static void client_background(void) | |||
177 | } | 173 | } |
178 | 174 | ||
179 | 175 | ||
176 | #ifdef COMBINED_BINARY | ||
180 | int udhcpc_main(int argc, char *argv[]) | 177 | int udhcpc_main(int argc, char *argv[]) |
178 | #else | ||
179 | int main(int argc, char *argv[]) | ||
180 | #endif | ||
181 | { | 181 | { |
182 | unsigned char *temp, *message; | 182 | unsigned char *temp, *message; |
183 | unsigned long t1 = 0, t2 = 0, xid = 0; | 183 | unsigned long t1 = 0, t2 = 0, xid = 0; |
@@ -258,18 +258,20 @@ int udhcpc_main(int argc, char *argv[]) | |||
258 | client_config.script = optarg; | 258 | client_config.script = optarg; |
259 | break; | 259 | break; |
260 | case 'v': | 260 | case 'v': |
261 | bb_error_msg("version %s\n", VERSION); | 261 | printf("udhcpcd, version %s\n\n", VERSION); |
262 | return(0); | 262 | return 0; |
263 | break; | 263 | break; |
264 | default: | 264 | default: |
265 | show_usage(); | 265 | show_usage(); |
266 | } | 266 | } |
267 | } | 267 | } |
268 | 268 | ||
269 | start_log("client"); | 269 | /* Start the log, sanitize fd's, and write a pid file */ |
270 | start_log_and_pid("udhcpc", client_config.pidfile); | ||
271 | |||
270 | if (read_interface(client_config.interface, &client_config.ifindex, | 272 | if (read_interface(client_config.interface, &client_config.ifindex, |
271 | NULL, client_config.arp) < 0) | 273 | NULL, client_config.arp) < 0) |
272 | return(1); | 274 | return 1; |
273 | 275 | ||
274 | if (!client_config.clientid) { | 276 | if (!client_config.clientid) { |
275 | client_config.clientid = xmalloc(6 + 3); | 277 | client_config.clientid = xmalloc(6 + 3); |
@@ -279,8 +281,8 @@ int udhcpc_main(int argc, char *argv[]) | |||
279 | memcpy(client_config.clientid + 3, client_config.arp, 6); | 281 | memcpy(client_config.clientid + 3, client_config.arp, 6); |
280 | } | 282 | } |
281 | 283 | ||
282 | /* setup signal handlers */ | 284 | /* setup the signal pipe */ |
283 | udhcp_set_signal_pipe(SIGUSR2); | 285 | udhcp_sp_setup(); |
284 | 286 | ||
285 | state = INIT_SELECTING; | 287 | state = INIT_SELECTING; |
286 | run_script(NULL, "deconfig"); | 288 | run_script(NULL, "deconfig"); |
@@ -290,7 +292,6 @@ int udhcpc_main(int argc, char *argv[]) | |||
290 | 292 | ||
291 | tv.tv_sec = timeout - time(0); | 293 | tv.tv_sec = timeout - time(0); |
292 | tv.tv_usec = 0; | 294 | tv.tv_usec = 0; |
293 | FD_ZERO(&rfds); | ||
294 | 295 | ||
295 | if (listen_mode != LISTEN_NONE && fd < 0) { | 296 | if (listen_mode != LISTEN_NONE && fd < 0) { |
296 | if (listen_mode == LISTEN_KERNEL) | 297 | if (listen_mode == LISTEN_KERNEL) |
@@ -299,15 +300,13 @@ int udhcpc_main(int argc, char *argv[]) | |||
299 | fd = raw_socket(client_config.ifindex); | 300 | fd = raw_socket(client_config.ifindex); |
300 | if (fd < 0) { | 301 | if (fd < 0) { |
301 | LOG(LOG_ERR, "FATAL: couldn't listen on socket, %m"); | 302 | LOG(LOG_ERR, "FATAL: couldn't listen on socket, %m"); |
302 | return(0); | 303 | return 0; |
303 | } | 304 | } |
304 | } | 305 | } |
305 | if (fd >= 0) FD_SET(fd, &rfds); | 306 | max_fd = udhcp_sp_fd_set(&rfds, fd); |
306 | FD_SET(udhcp_signal_pipe[0], &rfds); | ||
307 | 307 | ||
308 | if (tv.tv_sec > 0) { | 308 | if (tv.tv_sec > 0) { |
309 | DEBUG(LOG_INFO, "Waiting on select..."); | 309 | DEBUG(LOG_INFO, "Waiting on select..."); |
310 | max_fd = udhcp_signal_pipe[0] > fd ? udhcp_signal_pipe[0] : fd; | ||
311 | retval = select(max_fd + 1, &rfds, NULL, NULL, &tv); | 310 | retval = select(max_fd + 1, &rfds, NULL, NULL, &tv); |
312 | } else retval = 0; /* If we already timed out, fall through */ | 311 | } else retval = 0; /* If we already timed out, fall through */ |
313 | 312 | ||
@@ -332,7 +331,7 @@ int udhcpc_main(int argc, char *argv[]) | |||
332 | client_background(); | 331 | client_background(); |
333 | } else if (client_config.abort_if_no_lease) { | 332 | } else if (client_config.abort_if_no_lease) { |
334 | LOG(LOG_INFO, "No lease, failing."); | 333 | LOG(LOG_INFO, "No lease, failing."); |
335 | return(1); | 334 | return 1; |
336 | } | 335 | } |
337 | /* wait to try again */ | 336 | /* wait to try again */ |
338 | packet_num = 0; | 337 | packet_num = 0; |
@@ -474,7 +473,7 @@ int udhcpc_main(int argc, char *argv[]) | |||
474 | state = BOUND; | 473 | state = BOUND; |
475 | change_mode(LISTEN_NONE); | 474 | change_mode(LISTEN_NONE); |
476 | if (client_config.quit_after_lease) | 475 | if (client_config.quit_after_lease) |
477 | return(0); | 476 | return 0; |
478 | if (!client_config.foreground) | 477 | if (!client_config.foreground) |
479 | client_background(); | 478 | client_background(); |
480 | 479 | ||
@@ -494,11 +493,7 @@ int udhcpc_main(int argc, char *argv[]) | |||
494 | break; | 493 | break; |
495 | /* case BOUND, RELEASED: - ignore all packets */ | 494 | /* case BOUND, RELEASED: - ignore all packets */ |
496 | } | 495 | } |
497 | } else if (retval > 0 && FD_ISSET(udhcp_signal_pipe[0], &rfds)) { | 496 | } else if (retval > 0 && (sig = udhcp_sp_read(&rfds))) { |
498 | if (read(udhcp_signal_pipe[0], &sig, sizeof(sig)) < 0) { | ||
499 | DEBUG(LOG_ERR, "Could not read signal: %m"); | ||
500 | continue; /* probably just EINTR */ | ||
501 | } | ||
502 | switch (sig) { | 497 | switch (sig) { |
503 | case SIGUSR1: | 498 | case SIGUSR1: |
504 | perform_renew(); | 499 | perform_renew(); |
@@ -508,7 +503,7 @@ int udhcpc_main(int argc, char *argv[]) | |||
508 | break; | 503 | break; |
509 | case SIGTERM: | 504 | case SIGTERM: |
510 | LOG(LOG_INFO, "Received SIGTERM"); | 505 | LOG(LOG_INFO, "Received SIGTERM"); |
511 | return(0); | 506 | return 0; |
512 | } | 507 | } |
513 | } else if (retval == -1 && errno == EINTR) { | 508 | } else if (retval == -1 && errno == EINTR) { |
514 | /* a signal was caught */ | 509 | /* a signal was caught */ |