aboutsummaryrefslogtreecommitdiff
path: root/networking/udhcp/dhcpc.c
diff options
context:
space:
mode:
Diffstat (limited to 'networking/udhcp/dhcpc.c')
-rw-r--r--networking/udhcp/dhcpc.c120
1 files changed, 22 insertions, 98 deletions
diff --git a/networking/udhcp/dhcpc.c b/networking/udhcp/dhcpc.c
index c97e422e2..4612c4100 100644
--- a/networking/udhcp/dhcpc.c
+++ b/networking/udhcp/dhcpc.c
@@ -19,9 +19,7 @@
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
-#include <stdio.h>
#include <sys/time.h>
-#include <sys/types.h>
#include <sys/file.h>
#include <unistd.h>
#include <getopt.h>
@@ -40,11 +38,9 @@
#include "dhcpc.h"
#include "options.h"
#include "clientpacket.h"
-#include "packet.h"
#include "script.h"
#include "socket.h"
-#include "debug.h"
-#include "pidfile.h"
+#include "common.h"
static int state;
static unsigned long requested_ip; /* = 0 */
@@ -52,7 +48,6 @@ static unsigned long server_addr;
static unsigned long timeout;
static int packet_num; /* = 0 */
static int fd = -1;
-static int signal_pipe[2];
#define LISTEN_NONE 0
#define LISTEN_KERNEL 1
@@ -80,34 +75,6 @@ struct client_config_t client_config = {
arp: "\0\0\0\0\0\0", /* appease gcc-3.0 */
};
-#ifndef IN_BUSYBOX
-static void __attribute__ ((noreturn)) bb_show_usage(void)
-{
- printf(
-"Usage: udhcpc [OPTIONS]\n\n"
-" -c, --clientid=CLIENTID Client identifier\n"
-" -H, --hostname=HOSTNAME Client hostname\n"
-" -h Alias for -H\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"
-" -v, --version Display version\n"
- );
- exit(0);
-}
-#else
-extern void bb_show_usage(void) __attribute__ ((noreturn));
-#endif
-
-
/* just a little helper */
static void change_mode(int new_mode)
{
@@ -172,46 +139,15 @@ static void perform_release(void)
}
-/* Exit and cleanup */
-static void exit_client(int retval)
-{
- pidfile_delete(client_config.pidfile);
- CLOSE_LOG();
- exit(retval);
-}
-
-
-/* Signal handler */
-static void signal_handler(int sig)
-{
- if (send(signal_pipe[1], &sig, sizeof(sig), MSG_DONTWAIT) < 0) {
- LOG(LOG_ERR, "Could not send signal: %s",
- strerror(errno));
- }
-}
-
-
-static void background(void)
+static void client_background(void)
{
- int pid_fd;
-
- pid_fd = pidfile_acquire(client_config.pidfile); /* hold lock during fork. */
- while (pid_fd >= 0 && pid_fd < 3) pid_fd = dup(pid_fd); /* don't let daemon close it */
- if (daemon(0, 0) == -1) {
- perror("fork");
- exit_client(1);
- }
+ background(client_config.pidfile);
client_config.foreground = 1; /* Do not fork again. */
client_config.background_if_no_lease = 0;
- pidfile_write_release(pid_fd);
}
-#ifdef COMBINED_BINARY
int udhcpc_main(int argc, char *argv[])
-#else
-int main(int argc, char *argv[])
-#endif
{
unsigned char *temp, *message;
unsigned long t1 = 0, t2 = 0, xid = 0;
@@ -222,12 +158,11 @@ int main(int argc, char *argv[])
int c, len;
struct dhcpMessage packet;
struct in_addr temp_addr;
- int pid_fd;
time_t now;
int max_fd;
int sig;
- static struct option arg_options[] = {
+ static const struct option arg_options[] = {
{"clientid", required_argument, 0, 'c'},
{"foreground", no_argument, 0, 'f'},
{"background", no_argument, 0, 'b'},
@@ -240,7 +175,6 @@ int main(int argc, char *argv[])
{"request", required_argument, 0, 'r'},
{"script", required_argument, 0, 's'},
{"version", no_argument, 0, 'v'},
- {"help", no_argument, 0, '?'},
{0, 0, 0, 0}
};
@@ -294,23 +228,18 @@ int main(int argc, char *argv[])
client_config.script = optarg;
break;
case 'v':
- printf("udhcpcd, version %s\n\n", VERSION);
- exit_client(0);
+ bb_error_msg("version %s\n", VERSION);
+ return(0);
break;
default:
bb_show_usage();
}
}
- OPEN_LOG("udhcpc");
- LOG(LOG_INFO, "udhcp client (v%s) started", VERSION);
-
- pid_fd = pidfile_acquire(client_config.pidfile);
- pidfile_write_release(pid_fd);
-
+ start_log("client");
if (read_interface(client_config.interface, &client_config.ifindex,
NULL, client_config.arp) < 0)
- exit_client(1);
+ return(1);
if (!client_config.clientid) {
client_config.clientid = xmalloc(6 + 3);
@@ -321,10 +250,7 @@ int main(int argc, char *argv[])
}
/* setup signal handlers */
- socketpair(AF_UNIX, SOCK_STREAM, 0, signal_pipe);
- signal(SIGUSR1, signal_handler);
- signal(SIGUSR2, signal_handler);
- signal(SIGTERM, signal_handler);
+ udhcp_set_signal_pipe(SIGUSR2);
state = INIT_SELECTING;
run_script(NULL, "deconfig");
@@ -342,16 +268,16 @@ 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, %s", strerror(errno));
- exit_client(0);
+ LOG(LOG_ERR, "FATAL: couldn't listen on socket, %m");
+ return(0);
}
}
if (fd >= 0) FD_SET(fd, &rfds);
- FD_SET(signal_pipe[0], &rfds);
+ FD_SET(udhcp_signal_pipe[0], &rfds);
if (tv.tv_sec > 0) {
DEBUG(LOG_INFO, "Waiting on select...\n");
- max_fd = signal_pipe[0] > fd ? signal_pipe[0] : fd;
+ max_fd = udhcp_signal_pipe[0] > fd ? udhcp_signal_pipe[0] : fd;
retval = select(max_fd + 1, &rfds, NULL, NULL, &tv);
} else retval = 0; /* If we already timed out, fall through */
@@ -372,10 +298,10 @@ int main(int argc, char *argv[])
} else {
if (client_config.background_if_no_lease) {
LOG(LOG_INFO, "No lease, forking to background.");
- background();
+ client_background();
} else if (client_config.abort_if_no_lease) {
LOG(LOG_INFO, "No lease, failing.");
- exit_client(1);
+ return(1);
}
/* wait to try again */
packet_num = 0;
@@ -453,7 +379,7 @@ int main(int argc, char *argv[])
else len = get_raw_packet(&packet, fd);
if (len == -1 && errno != EINTR) {
- DEBUG(LOG_INFO, "error on read, %s, reopening socket", strerror(errno));
+ DEBUG(LOG_INFO, "error on read, %m, reopening socket");
change_mode(listen_mode); /* just close and reopen */
}
if (len < 0) continue;
@@ -517,9 +443,9 @@ int main(int argc, char *argv[])
state = BOUND;
change_mode(LISTEN_NONE);
if (client_config.quit_after_lease)
- exit_client(0);
+ return(0);
if (!client_config.foreground)
- background();
+ client_background();
} else if (*message == DHCPNAK) {
/* return to init state */
@@ -537,10 +463,9 @@ int main(int argc, char *argv[])
break;
/* case BOUND, RELEASED: - ignore all packets */
}
- } else if (retval > 0 && FD_ISSET(signal_pipe[0], &rfds)) {
- if (read(signal_pipe[0], &sig, sizeof(sig)) < 0) {
- DEBUG(LOG_ERR, "Could not read signal: %s",
- strerror(errno));
+ } else if (retval > 0 && FD_ISSET(udhcp_signal_pipe[0], &rfds)) {
+ if (read(udhcp_signal_pipe[0], &sig, sizeof(sig)) < 0) {
+ DEBUG(LOG_ERR, "Could not read signal: %m");
continue; /* probably just EINTR */
}
switch (sig) {
@@ -552,7 +477,7 @@ int main(int argc, char *argv[])
break;
case SIGTERM:
LOG(LOG_INFO, "Received SIGTERM");
- exit_client(0);
+ return(0);
}
} else if (retval == -1 && errno == EINTR) {
/* a signal was caught */
@@ -564,4 +489,3 @@ int main(int argc, char *argv[])
}
return 0;
}
-