summaryrefslogtreecommitdiff
path: root/networking/udhcp/dhcpd.c
diff options
context:
space:
mode:
authorGlenn L McGrath <bug1@ihug.co.nz>2003-06-10 17:22:49 +0000
committerGlenn L McGrath <bug1@ihug.co.nz>2003-06-10 17:22:49 +0000
commit24833430bc2dbea733c3b0b9ea6c6b976f95474a (patch)
tree805a4197b8a0d36eaa6880dfc23d8c2539359fe9 /networking/udhcp/dhcpd.c
parent6c43f743a3f607f91ee22a53db880fce2df645f0 (diff)
downloadbusybox-w32-24833430bc2dbea733c3b0b9ea6c6b976f95474a.tar.gz
busybox-w32-24833430bc2dbea733c3b0b9ea6c6b976f95474a.tar.bz2
busybox-w32-24833430bc2dbea733c3b0b9ea6c6b976f95474a.zip
Vodz, last_patch_88
Diffstat (limited to 'networking/udhcp/dhcpd.c')
-rw-r--r--networking/udhcp/dhcpd.c77
1 files changed, 17 insertions, 60 deletions
diff --git a/networking/udhcp/dhcpd.c b/networking/udhcp/dhcpd.c
index 56ddaa942..c21cb72a2 100644
--- a/networking/udhcp/dhcpd.c
+++ b/networking/udhcp/dhcpd.c
@@ -25,12 +25,9 @@
25#include <string.h> 25#include <string.h>
26#include <stdlib.h> 26#include <stdlib.h>
27#include <sys/wait.h> 27#include <sys/wait.h>
28#include <sys/stat.h>
29#include <arpa/inet.h> 28#include <arpa/inet.h>
30#include <netdb.h> 29#include <netdb.h>
31#include <netinet/in.h> 30#include <netinet/in.h>
32#include <stdio.h>
33#include <sys/types.h>
34#include <sys/socket.h> 31#include <sys/socket.h>
35#include <unistd.h> 32#include <unistd.h>
36#include <signal.h> 33#include <signal.h>
@@ -39,47 +36,21 @@
39#include <time.h> 36#include <time.h>
40#include <sys/time.h> 37#include <sys/time.h>
41 38
42#include "debug.h"
43#include "dhcpd.h" 39#include "dhcpd.h"
44#include "arpping.h" 40#include "arpping.h"
45#include "socket.h" 41#include "socket.h"
46#include "options.h" 42#include "options.h"
47#include "files.h" 43#include "files.h"
48#include "leases.h"
49#include "packet.h"
50#include "serverpacket.h" 44#include "serverpacket.h"
51#include "pidfile.h" 45#include "common.h"
52 46
53 47
54/* globals */ 48/* globals */
55struct dhcpOfferedAddr *leases; 49struct dhcpOfferedAddr *leases;
56struct server_config_t server_config; 50struct server_config_t server_config;
57static int signal_pipe[2];
58
59/* Exit and cleanup */
60static void exit_server(int retval)
61{
62 pidfile_delete(server_config.pidfile);
63 CLOSE_LOG();
64 exit(retval);
65}
66
67
68/* Signal handler */
69static void signal_handler(int sig)
70{
71 if (send(signal_pipe[1], &sig, sizeof(sig), MSG_DONTWAIT) < 0) {
72 LOG(LOG_ERR, "Could not send signal: %s",
73 strerror(errno));
74 }
75}
76 51
77 52
78#ifdef COMBINED_BINARY
79int udhcpd_main(int argc, char *argv[]) 53int udhcpd_main(int argc, char *argv[])
80#else
81int main(int argc, char *argv[])
82#endif
83{ 54{
84 fd_set rfds; 55 fd_set rfds;
85 struct timeval tv; 56 struct timeval tv;
@@ -92,13 +63,11 @@ int main(int argc, char *argv[])
92 unsigned long timeout_end; 63 unsigned long timeout_end;
93 struct option_set *option; 64 struct option_set *option;
94 struct dhcpOfferedAddr *lease; 65 struct dhcpOfferedAddr *lease;
95 int pid_fd;
96 int max_sock; 66 int max_sock;
97 int sig; 67 int sig;
98 unsigned long num_ips; 68 unsigned long num_ips;
99 69
100 OPEN_LOG("udhcpd"); 70 start_log("server");
101 LOG(LOG_INFO, "udhcp server (v%s) started", VERSION);
102 71
103 memset(&server_config, 0, sizeof(struct server_config_t)); 72 memset(&server_config, 0, sizeof(struct server_config_t));
104 73
@@ -106,9 +75,6 @@ int main(int argc, char *argv[])
106 read_config(DHCPD_CONF_FILE); 75 read_config(DHCPD_CONF_FILE);
107 else read_config(argv[1]); 76 else read_config(argv[1]);
108 77
109 pid_fd = pidfile_acquire(server_config.pidfile);
110 pidfile_write_release(pid_fd);
111
112 if ((option = find_option(server_config.options, DHCP_LEASE_TIME))) { 78 if ((option = find_option(server_config.options, DHCP_LEASE_TIME))) {
113 memcpy(&server_config.lease, option->data + 2, 4); 79 memcpy(&server_config.lease, option->data + 2, 4);
114 server_config.lease = ntohl(server_config.lease); 80 server_config.lease = ntohl(server_config.lease);
@@ -118,51 +84,43 @@ int main(int argc, char *argv[])
118 /* Sanity check */ 84 /* Sanity check */
119 num_ips = ntohl(server_config.end) - ntohl(server_config.start); 85 num_ips = ntohl(server_config.end) - ntohl(server_config.start);
120 if (server_config.max_leases > num_ips) { 86 if (server_config.max_leases > num_ips) {
121 LOG(LOG_ERR, "max_leases value (%lu) not sane, setting to %lu instead", 87 LOG(LOG_ERR, "max_leases value (%lu) not sane, "
88 "setting to %lu instead",
122 server_config.max_leases, num_ips); 89 server_config.max_leases, num_ips);
123 server_config.max_leases = num_ips; 90 server_config.max_leases = num_ips;
124 } 91 }
125 92
126 leases = xmalloc(sizeof(struct dhcpOfferedAddr) * server_config.max_leases); 93 leases = xcalloc(sizeof(struct dhcpOfferedAddr), server_config.max_leases);
127 memset(leases, 0, sizeof(struct dhcpOfferedAddr) * server_config.max_leases);
128 read_leases(server_config.lease_file); 94 read_leases(server_config.lease_file);
129 95
130 if (read_interface(server_config.interface, &server_config.ifindex, 96 if (read_interface(server_config.interface, &server_config.ifindex,
131 &server_config.server, server_config.arp) < 0) 97 &server_config.server, server_config.arp) < 0)
132 exit_server(1); 98 return(1);
133 99
134#ifndef DEBUGGING 100#ifndef CONFIG_FEATURE_UDHCP_DEBUG
135 pid_fd = pidfile_acquire(server_config.pidfile); /* hold lock during fork. */ 101 background(server_config.pidfile);
136 if (daemon(0, 0) == -1) {
137 perror("fork");
138 exit_server(1);
139 }
140 pidfile_write_release(pid_fd);
141#endif 102#endif
142 103
143 104 udhcp_set_signal_pipe(0);
144 socketpair(AF_UNIX, SOCK_STREAM, 0, signal_pipe);
145 signal(SIGUSR1, signal_handler);
146 signal(SIGTERM, signal_handler);
147 105
148 timeout_end = time(0) + server_config.auto_time; 106 timeout_end = time(0) + server_config.auto_time;
149 while(1) { /* loop until universe collapses */ 107 while(1) { /* loop until universe collapses */
150 108
151 if (server_socket < 0) 109 if (server_socket < 0)
152 if ((server_socket = listen_socket(INADDR_ANY, SERVER_PORT, server_config.interface)) < 0) { 110 if ((server_socket = listen_socket(INADDR_ANY, SERVER_PORT, server_config.interface)) < 0) {
153 LOG(LOG_ERR, "FATAL: couldn't create server socket, %s", strerror(errno)); 111 LOG(LOG_ERR, "FATAL: couldn't create server socket, %m");
154 exit_server(0); 112 return(2);
155 } 113 }
156 114
157 FD_ZERO(&rfds); 115 FD_ZERO(&rfds);
158 FD_SET(server_socket, &rfds); 116 FD_SET(server_socket, &rfds);
159 FD_SET(signal_pipe[0], &rfds); 117 FD_SET(udhcp_signal_pipe[0], &rfds);
160 if (server_config.auto_time) { 118 if (server_config.auto_time) {
161 tv.tv_sec = timeout_end - time(0); 119 tv.tv_sec = timeout_end - time(0);
162 tv.tv_usec = 0; 120 tv.tv_usec = 0;
163 } 121 }
164 if (!server_config.auto_time || tv.tv_sec > 0) { 122 if (!server_config.auto_time || tv.tv_sec > 0) {
165 max_sock = server_socket > signal_pipe[0] ? server_socket : signal_pipe[0]; 123 max_sock = server_socket > udhcp_signal_pipe[0] ? server_socket : udhcp_signal_pipe[0];
166 retval = select(max_sock + 1, &rfds, NULL, NULL, 124 retval = select(max_sock + 1, &rfds, NULL, NULL,
167 server_config.auto_time ? &tv : NULL); 125 server_config.auto_time ? &tv : NULL);
168 } else retval = 0; /* If we already timed out, fall through */ 126 } else retval = 0; /* If we already timed out, fall through */
@@ -176,8 +134,8 @@ int main(int argc, char *argv[])
176 continue; 134 continue;
177 } 135 }
178 136
179 if (FD_ISSET(signal_pipe[0], &rfds)) { 137 if (FD_ISSET(udhcp_signal_pipe[0], &rfds)) {
180 if (read(signal_pipe[0], &sig, sizeof(sig)) < 0) 138 if (read(udhcp_signal_pipe[0], &sig, sizeof(sig)) < 0)
181 continue; /* probably just EINTR */ 139 continue; /* probably just EINTR */
182 switch (sig) { 140 switch (sig) {
183 case SIGUSR1: 141 case SIGUSR1:
@@ -188,13 +146,13 @@ int main(int argc, char *argv[])
188 continue; 146 continue;
189 case SIGTERM: 147 case SIGTERM:
190 LOG(LOG_INFO, "Received a SIGTERM"); 148 LOG(LOG_INFO, "Received a SIGTERM");
191 exit_server(0); 149 return(0);
192 } 150 }
193 } 151 }
194 152
195 if ((bytes = get_packet(&packet, server_socket)) < 0) { /* this waits for a packet - idle */ 153 if ((bytes = get_packet(&packet, server_socket)) < 0) { /* this waits for a packet - idle */
196 if (bytes == -1 && errno != EINTR) { 154 if (bytes == -1 && errno != EINTR) {
197 DEBUG(LOG_INFO, "error on read, %s, reopening socket", strerror(errno)); 155 DEBUG(LOG_INFO, "error on read, %m, reopening socket");
198 close(server_socket); 156 close(server_socket);
199 server_socket = -1; 157 server_socket = -1;
200 } 158 }
@@ -293,4 +251,3 @@ int main(int argc, char *argv[])
293 251
294 return 0; 252 return 0;
295} 253}
296