]> git.ipfire.org Git - people/ms/dnsmasq.git/blame - src/dnsmasq.c
Correct listener logic when no TFTP and no-interface listen address.
[people/ms/dnsmasq.git] / src / dnsmasq.c
CommitLineData
59546085 1/* dnsmasq is Copyright (c) 2000-2012 Simon Kelley
9e4abcb5
SK
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
824af85b
SK
5 the Free Software Foundation; version 2 dated June, 1991, or
6 (at your option) version 3 dated 29 June, 2007.
7
9e4abcb5
SK
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
824af85b 12
73a08a24
SK
13 You should have received a copy of the GNU General Public License
14 along with this program. If not, see <http://www.gnu.org/licenses/>.
9e4abcb5
SK
15*/
16
c72daea8
SK
17/* Declare static char *compiler_opts in config.h */
18#define DNSMASQ_COMPILE_OPTS
19
9e4abcb5
SK
20#include "dnsmasq.h"
21
5aabfc78
SK
22struct daemon *daemon;
23
5aabfc78
SK
24static volatile pid_t pid = 0;
25static volatile int pipewrite;
9e4abcb5 26
5aabfc78
SK
27static int set_dns_listeners(time_t now, fd_set *set, int *maxfdp);
28static void check_dns_listeners(fd_set *set, time_t now);
3be34541 29static void sig_handler(int sig);
5aabfc78 30static void async_event(int pipe, time_t now);
c72daea8
SK
31static void fatal_event(struct event_desc *ev, char *msg);
32static int read_event(int fd, struct event_desc *evp, char **msg);
9e4abcb5
SK
33
34int main (int argc, char **argv)
35{
de37951c 36 int bind_fallback = 0;
9009d746 37 time_t now;
9e4abcb5 38 struct sigaction sigact;
26128d27 39 struct iname *if_tmp;
1a6bca81
SK
40 int piperead, pipefd[2], err_pipe[2];
41 struct passwd *ent_pw = NULL;
c72daea8 42#if defined(HAVE_SCRIPT)
1a6bca81
SK
43 uid_t script_uid = 0;
44 gid_t script_gid = 0;
7622fc06
SK
45#endif
46 struct group *gp = NULL;
5aabfc78 47 long i, max_fd = sysconf(_SC_OPEN_MAX);
1a6bca81
SK
48 char *baduser = NULL;
49 int log_err;
50#if defined(HAVE_LINUX_NETWORK)
51 cap_user_header_t hdr = NULL;
52 cap_user_data_t data = NULL;
53#endif
5aabfc78 54
824af85b 55#ifdef LOCALEDIR
b8187c80
SK
56 setlocale(LC_ALL, "");
57 bindtextdomain("dnsmasq", LOCALEDIR);
58 textdomain("dnsmasq");
59#endif
60
9e4abcb5
SK
61 sigact.sa_handler = sig_handler;
62 sigact.sa_flags = 0;
63 sigemptyset(&sigact.sa_mask);
64 sigaction(SIGUSR1, &sigact, NULL);
5aabfc78 65 sigaction(SIGUSR2, &sigact, NULL);
9e4abcb5
SK
66 sigaction(SIGHUP, &sigact, NULL);
67 sigaction(SIGTERM, &sigact, NULL);
44a2a316 68 sigaction(SIGALRM, &sigact, NULL);
feba5c1d
SK
69 sigaction(SIGCHLD, &sigact, NULL);
70
71 /* ignore SIGPIPE */
72 sigact.sa_handler = SIG_IGN;
73 sigaction(SIGPIPE, &sigact, NULL);
9e4abcb5 74
5aabfc78
SK
75 umask(022); /* known umask, create leases and pid files as 0644 */
76
77 read_opts(argc, argv, compile_opts);
78
3be34541
SK
79 if (daemon->edns_pktsz < PACKETSZ)
80 daemon->edns_pktsz = PACKETSZ;
0a852541
SK
81 daemon->packet_buff_sz = daemon->edns_pktsz > DNSMASQ_PACKETSZ ?
82 daemon->edns_pktsz : DNSMASQ_PACKETSZ;
83 daemon->packet = safe_malloc(daemon->packet_buff_sz);
1a6bca81 84
c72daea8
SK
85 daemon->addrbuff = safe_malloc(ADDRSTRLEN);
86
7622fc06 87#ifdef HAVE_DHCP
3be34541 88 if (!daemon->lease_file)
9e4abcb5 89 {
52b92f4d 90 if (daemon->dhcp || daemon->dhcp6)
3be34541 91 daemon->lease_file = LEASEFILE;
9e4abcb5 92 }
7622fc06 93#endif
9e4abcb5 94
a2761754
SK
95 /* Close any file descriptors we inherited apart from std{in|out|err}
96
97 Ensure that at least stdin, stdout and stderr (fd 0, 1, 2) exist,
98 otherwise file descriptors we create can end up being 0, 1, or 2
99 and then get accidentally closed later when we make 0, 1, and 2
100 open to /dev/null. Normally we'll be started with 0, 1 and 2 open,
101 but it's not guaranteed. By opening /dev/null three times, we
102 ensure that we're not using those fds for real stuff. */
5aabfc78
SK
103 for (i = 0; i < max_fd; i++)
104 if (i != STDOUT_FILENO && i != STDERR_FILENO && i != STDIN_FILENO)
105 close(i);
a2761754
SK
106 else
107 open("/dev/null", O_RDWR);
5aabfc78 108
801ca9a7
SK
109#ifndef HAVE_LINUX_NETWORK
110# if !(defined(IP_RECVDSTADDR) && defined(IP_RECVIF) && defined(IP_SENDSRCADDR))
28866e95 111 if (!option_bool(OPT_NOWILD))
de37951c
SK
112 {
113 bind_fallback = 1;
28866e95 114 set_option_bool(OPT_NOWILD);
de37951c 115 }
801ca9a7 116# endif
309331f5
SK
117#endif
118
832af0ba 119#ifndef HAVE_TFTP
8ef5ada2 120 if (daemon->tftp_unlimited || daemon->tftp_interfaces)
5aabfc78 121 die(_("TFTP server not available: set HAVE_TFTP in src/config.h"), NULL, EC_BADCONF);
832af0ba
SK
122#endif
123
7de060b0
SK
124#ifdef HAVE_CONNTRACK
125 if (option_bool(OPT_CONNTRACK) && (daemon->query_port != 0 || daemon->osport))
126 die (_("Cannot use --conntrack AND --query-port"), NULL, EC_BADCONF);
127#else
128 if (option_bool(OPT_CONNTRACK))
129 die(_("Conntrack support not available: set HAVE_CONNTRACK in src/config.h"), NULL, EC_BADCONF);
130#endif
131
824af85b
SK
132#ifdef HAVE_SOLARIS_NETWORK
133 if (daemon->max_logs != 0)
134 die(_("asychronous logging is not available under Solaris"), NULL, EC_BADCONF);
135#endif
136
572b41eb
SK
137#ifdef __ANDROID__
138 if (daemon->max_logs != 0)
139 die(_("asychronous logging is not available under Android"), NULL, EC_BADCONF);
140#endif
141
1a6bca81
SK
142 rand_init();
143
5aabfc78
SK
144 now = dnsmasq_time();
145
7622fc06 146#ifdef HAVE_DHCP
52b92f4d 147 if (daemon->dhcp || daemon->dhcp6)
5aabfc78 148 {
5aabfc78
SK
149 /* Note that order matters here, we must call lease_init before
150 creating any file descriptors which shouldn't be leaked
4cb1b320
SK
151 to the lease-script init process. We need to call common_init
152 before lease_init to allocate buffers it uses.*/
153 dhcp_common_init();
5aabfc78 154 lease_init(now);
843c96b4 155
52b92f4d
SK
156 if (daemon->dhcp)
157 dhcp_init();
843c96b4
SK
158 }
159
160# ifdef HAVE_DHCP6
161 /* Start RA subsystem if --enable-ra OR dhcp-range=<subnet>, ra-only */
162 if (daemon->ra_contexts || option_bool(OPT_RA))
163 {
164 /* link the DHCP6 contexts to the ra-only ones so we can traverse them all
165 from ->ra_contexts, but only the non-ra-onlies from ->dhcp6 */
166 struct dhcp_context *context;
167
168 if (!daemon->ra_contexts)
169 daemon->ra_contexts = daemon->dhcp6;
170 else
c5ad4e79 171 {
843c96b4
SK
172 for (context = daemon->ra_contexts; context->next; context = context->next);
173 context->next = daemon->dhcp6;
c5ad4e79 174 }
843c96b4 175 ra_init(now);
5aabfc78 176 }
843c96b4
SK
177
178 if (daemon->dhcp6)
179 dhcp6_init();
180
801ca9a7
SK
181# endif
182
183#endif
184
185#ifdef HAVE_LINUX_NETWORK
186 /* After lease_init */
187 netlink_init();
188#endif
189
190#ifdef HAVE_DHCP6
191 /* after netlink_init */
843c96b4
SK
192 if (daemon->ra_contexts || daemon->dhcp6)
193 join_multicast();
801ca9a7 194#endif
843c96b4 195
801ca9a7
SK
196#ifdef HAVE_DHCP
197 /* after netlink_init */
198 if (daemon->dhcp || daemon->dhcp6)
8b372704 199 lease_find_interfaces(now);
7622fc06 200#endif
5aabfc78
SK
201
202 if (!enumerate_interfaces())
203 die(_("failed to find list of interfaces: %s"), NULL, EC_MISC);
843c96b4 204
28866e95 205 if (option_bool(OPT_NOWILD))
de37951c 206 {
74c95c25 207 create_bound_listeners(1);
de37951c 208
3be34541 209 for (if_tmp = daemon->if_names; if_tmp; if_tmp = if_tmp->next)
de37951c 210 if (if_tmp->name && !if_tmp->used)
5aabfc78 211 die(_("unknown interface %s"), if_tmp->name, EC_BADNET);
9380ba70
SK
212
213#if defined(HAVE_LINUX_NETWORK) && defined(HAVE_DHCP)
214 /* after enumerate_interfaces() */
215 if (daemon->dhcp)
216 {
217 bindtodevice(daemon->dhcpfd);
218 if (daemon->enable_pxe)
219 bindtodevice(daemon->pxefd);
220 }
221#endif
222
223#if defined(HAVE_LINUX_NETWORK) && defined(HAVE_DHCP6)
224 if (daemon->dhcp6)
225 bindtodevice(daemon->dhcp6fd);
226#endif
de37951c 227 }
28866e95 228 else
74c95c25 229 create_wildcard_listeners();
de37951c 230
824af85b
SK
231 if (daemon->port != 0)
232 cache_init();
1a6bca81 233
28866e95 234 if (option_bool(OPT_DBUS))
3d8df260
SK
235#ifdef HAVE_DBUS
236 {
237 char *err;
238 daemon->dbus = NULL;
239 daemon->watches = NULL;
5aabfc78
SK
240 if ((err = dbus_init()))
241 die(_("DBus error: %s"), err, EC_MISC);
3d8df260
SK
242 }
243#else
5aabfc78 244 die(_("DBus not available: set HAVE_DBUS in src/config.h"), NULL, EC_BADCONF);
3d8df260
SK
245#endif
246
824af85b
SK
247 if (daemon->port != 0)
248 pre_allocate_sfds();
1a6bca81 249
c72daea8 250#if defined(HAVE_SCRIPT)
1a6bca81 251 /* Note getpwnam returns static storage */
843c96b4
SK
252 if ((daemon->dhcp || daemon->dhcp6) &&
253 daemon->scriptuser &&
c72daea8 254 (daemon->lease_change_command || daemon->luascript))
1a6bca81
SK
255 {
256 if ((ent_pw = getpwnam(daemon->scriptuser)))
257 {
258 script_uid = ent_pw->pw_uid;
259 script_gid = ent_pw->pw_gid;
260 }
261 else
262 baduser = daemon->scriptuser;
263 }
7622fc06 264#endif
9e4abcb5 265
1a6bca81
SK
266 if (daemon->username && !(ent_pw = getpwnam(daemon->username)))
267 baduser = daemon->username;
268 else if (daemon->groupname && !(gp = getgrnam(daemon->groupname)))
269 baduser = daemon->groupname;
270
271 if (baduser)
272 die(_("unknown user or group: %s"), baduser, EC_BADCONF);
273
274 /* implement group defaults, "dip" if available, or group associated with uid */
275 if (!daemon->group_set && !gp)
276 {
277 if (!(gp = getgrnam(CHGRP)) && ent_pw)
278 gp = getgrgid(ent_pw->pw_gid);
279
280 /* for error message */
281 if (gp)
282 daemon->groupname = gp->gr_name;
283 }
284
285#if defined(HAVE_LINUX_NETWORK)
286 /* determine capability API version here, while we can still
287 call safe_malloc */
288 if (ent_pw && ent_pw->pw_uid != 0)
289 {
1a6bca81 290 int capsize = 1; /* for header version 1 */
3927da46
SK
291 hdr = safe_malloc(sizeof(*hdr));
292
1a6bca81
SK
293 /* find version supported by kernel */
294 memset(hdr, 0, sizeof(*hdr));
295 capget(hdr, NULL);
296
297 if (hdr->version != LINUX_CAPABILITY_VERSION_1)
298 {
299 /* if unknown version, use largest supported version (3) */
300 if (hdr->version != LINUX_CAPABILITY_VERSION_2)
301 hdr->version = LINUX_CAPABILITY_VERSION_3;
302 capsize = 2;
303 }
304
305 data = safe_malloc(sizeof(*data) * capsize);
306 memset(data, 0, sizeof(*data) * capsize);
307 }
308#endif
309
5aabfc78 310 /* Use a pipe to carry signals and other events back to the event loop
1a6bca81
SK
311 in a race-free manner and another to carry errors to daemon-invoking process */
312 safe_pipe(pipefd, 1);
5e9e0efb
SK
313
314 piperead = pipefd[0];
315 pipewrite = pipefd[1];
316 /* prime the pipe to load stuff first time. */
c72daea8 317 send_event(pipewrite, EVENT_RELOAD, 0, NULL);
1a6bca81
SK
318
319 err_pipe[1] = -1;
1697269c 320
28866e95 321 if (!option_bool(OPT_DEBUG))
9e4abcb5 322 {
9e4abcb5
SK
323 /* The following code "daemonizes" the process.
324 See Stevens section 12.4 */
1a6bca81 325
9e038946
SK
326 if (chdir("/") != 0)
327 die(_("cannot chdir to filesystem root: %s"), NULL, EC_MISC);
328
1697269c 329#ifndef NO_FORK
28866e95 330 if (!option_bool(OPT_NO_FORK))
3be34541 331 {
5aabfc78 332 pid_t pid;
3be34541 333
1a6bca81
SK
334 /* pipe to carry errors back to original process.
335 When startup is complete we close this and the process terminates. */
336 safe_pipe(err_pipe, 0);
337
7622fc06
SK
338 if ((pid = fork()) == -1)
339 /* fd == -1 since we've not forked, never returns. */
c72daea8 340 send_event(-1, EVENT_FORK_ERR, errno, NULL);
9e038946 341
5aabfc78 342 if (pid != 0)
1a6bca81
SK
343 {
344 struct event_desc ev;
c72daea8
SK
345 char *msg;
346
1a6bca81
SK
347 /* close our copy of write-end */
348 close(err_pipe[1]);
349
350 /* check for errors after the fork */
c72daea8
SK
351 if (read_event(err_pipe[0], &ev, &msg))
352 fatal_event(&ev, msg);
1a6bca81
SK
353
354 _exit(EC_GOOD);
355 }
356
357 close(err_pipe[0]);
358
359 /* NO calls to die() from here on. */
3be34541 360
5aabfc78 361 setsid();
7622fc06
SK
362
363 if ((pid = fork()) == -1)
c72daea8 364 send_event(err_pipe[1], EVENT_FORK_ERR, errno, NULL);
7622fc06
SK
365
366 if (pid != 0)
7cebd20f 367 _exit(0);
3be34541 368 }
9e4abcb5 369#endif
9e038946 370
9e4abcb5 371 /* write pidfile _after_ forking ! */
1a6bca81
SK
372 if (daemon->runfile)
373 {
374 FILE *pidfile;
375
376 /* only complain if started as root */
377 if ((pidfile = fopen(daemon->runfile, "w")))
378 {
379 fprintf(pidfile, "%d\n", (int) getpid());
380 fclose(pidfile);
381 }
382 else if (getuid() == 0)
383 {
c72daea8 384 send_event(err_pipe[1], EVENT_PIDFILE, errno, daemon->runfile);
1a6bca81
SK
385 _exit(0);
386 }
9e4abcb5 387 }
1697269c
SK
388 }
389
8ef5ada2
SK
390 log_err = log_start(ent_pw, err_pipe[1]);
391
28866e95 392 if (!option_bool(OPT_DEBUG))
8ef5ada2
SK
393 {
394 /* open stdout etc to /dev/null */
395 int nullfd = open("/dev/null", O_RDWR);
396 dup2(nullfd, STDOUT_FILENO);
397 dup2(nullfd, STDERR_FILENO);
398 dup2(nullfd, STDIN_FILENO);
399 close(nullfd);
400 }
1a6bca81
SK
401
402 /* if we are to run scripts, we need to fork a helper before dropping root. */
403 daemon->helperfd = -1;
c72daea8 404#ifdef HAVE_SCRIPT
52b92f4d 405 if ((daemon->dhcp || daemon->dhcp6) && (daemon->lease_change_command || daemon->luascript))
1a6bca81 406 daemon->helperfd = create_helper(pipewrite, err_pipe[1], script_uid, script_gid, max_fd);
5aabfc78 407#endif
5aabfc78 408
28866e95 409 if (!option_bool(OPT_DEBUG) && getuid() == 0)
1697269c 410 {
1a6bca81
SK
411 int bad_capabilities = 0;
412 gid_t dummy;
413
414 /* remove all supplimentary groups */
415 if (gp &&
416 (setgroups(0, &dummy) == -1 ||
417 setgid(gp->gr_gid) == -1))
9e4abcb5 418 {
c72daea8 419 send_event(err_pipe[1], EVENT_GROUP_ERR, errno, daemon->groupname);
1a6bca81 420 _exit(0);
7cebd20f 421 }
1a6bca81 422
7cebd20f 423 if (ent_pw && ent_pw->pw_uid != 0)
1697269c 424 {
74c95c25 425#if defined(HAVE_LINUX_NETWORK)
1697269c 426 /* On linux, we keep CAP_NETADMIN (for ARP-injection) and
74c95c25
SK
427 CAP_NET_RAW (for icmp) if we're doing dhcp. If we have yet to bind
428 ports because of DAD, we need CAP_NET_BIND_SERVICE too. */
429 if (is_dad_listeners())
430 data->effective = data->permitted = data->inheritable =
431 (1 << CAP_NET_ADMIN) | (1 << CAP_NET_RAW) |
432 (1 << CAP_SETUID) | (1 << CAP_NET_BIND_SERVICE);
433 else
434 data->effective = data->permitted = data->inheritable =
435 (1 << CAP_NET_ADMIN) | (1 << CAP_NET_RAW) | (1 << CAP_SETUID);
5e9e0efb 436
1697269c 437 /* Tell kernel to not clear capabilities when dropping root */
572b41eb 438 if (capset(hdr, data) == -1 || prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0) == -1)
1697269c 439 bad_capabilities = errno;
1a6bca81 440
7622fc06 441#elif defined(HAVE_SOLARIS_NETWORK)
824af85b
SK
442 /* http://developers.sun.com/solaris/articles/program_privileges.html */
443 priv_set_t *priv_set;
444
445 if (!(priv_set = priv_str_to_set("basic", ",", NULL)) ||
446 priv_addset(priv_set, PRIV_NET_ICMPACCESS) == -1 ||
447 priv_addset(priv_set, PRIV_SYS_NET_CONFIG) == -1)
448 bad_capabilities = errno;
449
450 if (priv_set && bad_capabilities == 0)
451 {
452 priv_inverse(priv_set);
453
454 if (setppriv(PRIV_OFF, PRIV_LIMIT, priv_set) == -1)
455 bad_capabilities = errno;
456 }
457
458 if (priv_set)
459 priv_freeset(priv_set);
460
824af85b
SK
461#endif
462
1a6bca81 463 if (bad_capabilities != 0)
1697269c 464 {
c72daea8 465 send_event(err_pipe[1], EVENT_CAP_ERR, bad_capabilities, NULL);
1a6bca81
SK
466 _exit(0);
467 }
468
469 /* finally drop root */
470 if (setuid(ent_pw->pw_uid) == -1)
471 {
c72daea8 472 send_event(err_pipe[1], EVENT_USER_ERR, errno, daemon->username);
1a6bca81
SK
473 _exit(0);
474 }
475
1697269c 476#ifdef HAVE_LINUX_NETWORK
74c95c25
SK
477 if (is_dad_listeners())
478 data->effective = data->permitted =
479 (1 << CAP_NET_ADMIN) | (1 << CAP_NET_RAW) | (1 << CAP_NET_BIND_SERVICE);
480 else
481 data->effective = data->permitted =
482 (1 << CAP_NET_ADMIN) | (1 << CAP_NET_RAW);
1a6bca81
SK
483 data->inheritable = 0;
484
485 /* lose the setuid and setgid capbilities */
486 if (capset(hdr, data) == -1)
487 {
c72daea8 488 send_event(err_pipe[1], EVENT_CAP_ERR, errno, NULL);
1a6bca81 489 _exit(0);
1697269c 490 }
1a6bca81
SK
491#endif
492
9e4abcb5
SK
493 }
494 }
1697269c 495
1697269c 496#ifdef HAVE_LINUX_NETWORK
28866e95 497 if (option_bool(OPT_DEBUG))
572b41eb 498 prctl(PR_SET_DUMPABLE, 1, 0, 0, 0);
1697269c 499#endif
9e4abcb5 500
824af85b
SK
501 if (daemon->port == 0)
502 my_syslog(LOG_INFO, _("started, version %s DNS disabled"), VERSION);
503 else if (daemon->cachesize != 0)
f2621c7f 504 my_syslog(LOG_INFO, _("started, version %s cachesize %d"), VERSION, daemon->cachesize);
9e4abcb5 505 else
f2621c7f 506 my_syslog(LOG_INFO, _("started, version %s cache disabled"), VERSION);
1697269c 507
f2621c7f 508 my_syslog(LOG_INFO, _("compile time options: %s"), compile_opts);
1697269c 509
3d8df260 510#ifdef HAVE_DBUS
28866e95 511 if (option_bool(OPT_DBUS))
3d8df260
SK
512 {
513 if (daemon->dbus)
f2621c7f 514 my_syslog(LOG_INFO, _("DBus support enabled: connected to system bus"));
3d8df260 515 else
f2621c7f 516 my_syslog(LOG_INFO, _("DBus support enabled: bus connection pending"));
3d8df260
SK
517 }
518#endif
519
1a6bca81
SK
520 if (log_err != 0)
521 my_syslog(LOG_WARNING, _("warning: failed to change owner of %s: %s"),
522 daemon->log_file, strerror(log_err));
523
de37951c 524 if (bind_fallback)
f2621c7f 525 my_syslog(LOG_WARNING, _("setting --bind-interfaces option because of OS limitations"));
de37951c 526
28866e95 527 if (!option_bool(OPT_NOWILD))
26128d27
SK
528 for (if_tmp = daemon->if_names; if_tmp; if_tmp = if_tmp->next)
529 if (if_tmp->name && !if_tmp->used)
f2621c7f 530 my_syslog(LOG_WARNING, _("warning: interface %s does not currently exist"), if_tmp->name);
5e9e0efb 531
28866e95 532 if (daemon->port != 0 && option_bool(OPT_NO_RESOLV))
208b65c5
SK
533 {
534 if (daemon->resolv_files && !daemon->resolv_files->is_default)
f2621c7f 535 my_syslog(LOG_WARNING, _("warning: ignoring resolv-file flag because no-resolv is set"));
208b65c5 536 daemon->resolv_files = NULL;
1b7ecd11 537 if (!daemon->servers)
f2621c7f 538 my_syslog(LOG_WARNING, _("warning: no upstream servers configured"));
208b65c5
SK
539 }
540
f2621c7f
SK
541 if (daemon->max_logs != 0)
542 my_syslog(LOG_INFO, _("asynchronous logging enabled, queue limit is %d messages"), daemon->max_logs);
c5ad4e79 543
843c96b4 544 if (daemon->ra_contexts)
c5ad4e79 545 my_syslog(MS_DHCP | LOG_INFO, _("IPv6 router advertisement enabled"));
f2621c7f 546
7622fc06 547#ifdef HAVE_DHCP
c8257540 548 if (daemon->dhcp || daemon->dhcp6 || daemon->ra_contexts)
9e4abcb5 549 {
3be34541 550 struct dhcp_context *dhcp_tmp;
52b92f4d
SK
551 int family = AF_INET;
552 dhcp_tmp = daemon->dhcp;
553
3268e90f 554#ifdef HAVE_DHCP6
52b92f4d 555 again:
3268e90f 556#endif
52b92f4d 557 for (; dhcp_tmp; dhcp_tmp = dhcp_tmp->next)
feba5c1d 558 {
52b92f4d
SK
559 void *start = &dhcp_tmp->start;
560 void *end = &dhcp_tmp->end;
c8257540 561
52b92f4d
SK
562#ifdef HAVE_DHCP6
563 if (family == AF_INET6)
564 {
565 start = &dhcp_tmp->start6;
566 end = &dhcp_tmp->end6;
30cd9666
SK
567 struct in6_addr subnet = dhcp_tmp->start6;
568 setaddr6part(&subnet, 0);
c8257540 569 inet_ntop(AF_INET6, &subnet, daemon->addrbuff, 256);
52b92f4d
SK
570 }
571#endif
572
c8257540
SK
573 if (family != AF_INET && (dhcp_tmp->flags & CONTEXT_DEPRECATE))
574 strcpy(daemon->namebuff, _("prefix deprecated"));
575 else
576 {
577 char *p = daemon->namebuff;
578 p += sprintf(p, _("lease time "));
579 prettyprint_time(p, dhcp_tmp->lease_time);
580 }
581
582 if (daemon->dhcp_buff)
583 inet_ntop(family, start, daemon->dhcp_buff, 256);
584 if (daemon->dhcp_buff3)
585 inet_ntop(family, end, daemon->dhcp_buff3, 256);
30cd9666
SK
586 if ((dhcp_tmp->flags & CONTEXT_DHCP) || family == AF_INET)
587 my_syslog(MS_DHCP | LOG_INFO,
30cd9666 588 (dhcp_tmp->flags & CONTEXT_RA_STATELESS) ?
c8257540 589 _("stateless DHCPv6 on %s%.0s%.0s") :
22407048 590 (dhcp_tmp->flags & CONTEXT_STATIC) ?
c8257540 591 _("DHCP, static leases only on %.0s%s, %s") :
30cd9666
SK
592 (dhcp_tmp->flags & CONTEXT_PROXY) ?
593 _("DHCP, proxy on subnet %.0s%s%.0s") :
c8257540 594 _("DHCP, IP range %s -- %s, %s"),
30cd9666 595 daemon->dhcp_buff, daemon->dhcp_buff3, daemon->namebuff);
c8257540 596
30cd9666 597 if (dhcp_tmp->flags & CONTEXT_RA_NAME)
c8257540
SK
598 my_syslog(MS_DHCP | LOG_INFO, _("DHCPv4-derived IPv6 names on %s"),
599 daemon->addrbuff);
600 if (dhcp_tmp->flags & (CONTEXT_RA_ONLY | CONTEXT_RA_NAME | CONTEXT_RA_STATELESS))
601 {
602 if (!(dhcp_tmp->flags & CONTEXT_DEPRECATE))
603 {
604 char *p = daemon->namebuff;
605 p += sprintf(p, _("prefix valid "));
606 prettyprint_time(p, dhcp_tmp->lease_time > 7200 ? dhcp_tmp->lease_time : 7200);
607 }
608 my_syslog(MS_DHCP | LOG_INFO, _("SLAAC on %s %s"),
609 daemon->addrbuff, daemon->namebuff);
610 }
52b92f4d
SK
611 }
612
613#ifdef HAVE_DHCP6
614 if (family == AF_INET)
615 {
616 family = AF_INET6;
c5ad4e79
SK
617 if (daemon->ra_contexts)
618 dhcp_tmp = daemon->ra_contexts;
619 else
620 dhcp_tmp = daemon->dhcp6;
52b92f4d 621 goto again;
feba5c1d 622 }
52b92f4d
SK
623#endif
624
26128d27 625 }
7622fc06 626#endif
26128d27 627
52b92f4d 628
832af0ba 629#ifdef HAVE_TFTP
8ef5ada2 630 if (daemon->tftp_unlimited || daemon->tftp_interfaces)
832af0ba 631 {
832af0ba 632#ifdef FD_SETSIZE
5aabfc78 633 if (FD_SETSIZE < (unsigned)max_fd)
832af0ba
SK
634 max_fd = FD_SETSIZE;
635#endif
636
7622fc06 637 my_syslog(MS_TFTP | LOG_INFO, "TFTP %s%s %s",
f2621c7f
SK
638 daemon->tftp_prefix ? _("root is ") : _("enabled"),
639 daemon->tftp_prefix ? daemon->tftp_prefix: "",
28866e95 640 option_bool(OPT_TFTP_SECURE) ? _("secure mode") : "");
f2621c7f 641
832af0ba 642 /* This is a guess, it assumes that for small limits,
f2621c7f 643 disjoint files might be served, but for large limits,
832af0ba
SK
644 a single file will be sent to may clients (the file only needs
645 one fd). */
646
647 max_fd -= 30; /* use other than TFTP */
648
649 if (max_fd < 0)
650 max_fd = 5;
651 else if (max_fd < 100)
652 max_fd = max_fd/2;
653 else
654 max_fd = max_fd - 20;
824af85b
SK
655
656 /* if we have to use a limited range of ports,
657 that will limit the number of transfers */
658 if (daemon->start_tftp_port != 0 &&
659 daemon->end_tftp_port - daemon->start_tftp_port + 1 < max_fd)
660 max_fd = daemon->end_tftp_port - daemon->start_tftp_port + 1;
832af0ba
SK
661
662 if (daemon->tftp_max > max_fd)
663 {
664 daemon->tftp_max = max_fd;
7622fc06 665 my_syslog(MS_TFTP | LOG_WARNING,
f2621c7f
SK
666 _("restricting maximum simultaneous TFTP transfers to %d"),
667 daemon->tftp_max);
832af0ba
SK
668 }
669 }
670#endif
671
1a6bca81
SK
672 /* finished start-up - release original process */
673 if (err_pipe[1] != -1)
674 close(err_pipe[1]);
9e4abcb5 675
824af85b
SK
676 if (daemon->port != 0)
677 check_servers();
678
7cebd20f
SK
679 pid = getpid();
680
5e9e0efb 681 while (1)
9e4abcb5 682 {
1697269c 683 int maxfd = -1;
5e9e0efb 684 struct timeval t, *tp = NULL;
3d8df260 685 fd_set rset, wset, eset;
9e4abcb5
SK
686
687 FD_ZERO(&rset);
3d8df260
SK
688 FD_ZERO(&wset);
689 FD_ZERO(&eset);
9e4abcb5 690
1697269c
SK
691 /* if we are out of resources, find how long we have to wait
692 for some to come free, we'll loop around then and restart
693 listening for queries */
5aabfc78 694 if ((t.tv_sec = set_dns_listeners(now, &rset, &maxfd)) != 0)
1697269c
SK
695 {
696 t.tv_usec = 0;
697 tp = &t;
698 }
699
832af0ba
SK
700 /* Whilst polling for the dbus, or doing a tftp transfer, wake every quarter second */
701 if (daemon->tftp_trans ||
28866e95 702 (option_bool(OPT_DBUS) && !daemon->dbus))
5e9e0efb 703 {
1697269c
SK
704 t.tv_sec = 0;
705 t.tv_usec = 250000;
5e9e0efb 706 tp = &t;
5e9e0efb 707 }
74c95c25
SK
708 /* Wake every second whilst waiting for DAD to complete */
709 else if (is_dad_listeners())
710 {
711 t.tv_sec = 1;
712 t.tv_usec = 0;
713 tp = &t;
714 }
44a2a316 715
832af0ba 716#ifdef HAVE_DBUS
5aabfc78 717 set_dbus_listeners(&maxfd, &rset, &wset, &eset);
5e9e0efb
SK
718#endif
719
7622fc06 720#ifdef HAVE_DHCP
5e9e0efb
SK
721 if (daemon->dhcp)
722 {
723 FD_SET(daemon->dhcpfd, &rset);
1697269c 724 bump_maxfd(daemon->dhcpfd, &maxfd);
316e2730
SK
725 if (daemon->pxefd != -1)
726 {
727 FD_SET(daemon->pxefd, &rset);
728 bump_maxfd(daemon->pxefd, &maxfd);
729 }
5e9e0efb 730 }
7622fc06 731#endif
cdeda28f 732
52b92f4d
SK
733#ifdef HAVE_DHCP6
734 if (daemon->dhcp6)
735 {
736 FD_SET(daemon->dhcp6fd, &rset);
c5ad4e79 737 bump_maxfd(daemon->dhcp6fd, &maxfd);
5d71d834
SK
738 }
739
740 if (daemon->ra_contexts)
741 {
742 FD_SET(daemon->icmp6fd, &rset);
743 bump_maxfd(daemon->icmp6fd, &maxfd);
52b92f4d
SK
744 }
745#endif
746
5e9e0efb
SK
747#ifdef HAVE_LINUX_NETWORK
748 FD_SET(daemon->netlinkfd, &rset);
1697269c 749 bump_maxfd(daemon->netlinkfd, &maxfd);
3d8df260 750#endif
3d8df260 751
5e9e0efb 752 FD_SET(piperead, &rset);
1697269c
SK
753 bump_maxfd(piperead, &maxfd);
754
7622fc06 755#ifdef HAVE_DHCP
1f15b81d 756# ifdef HAVE_SCRIPT
5aabfc78 757 while (helper_buf_empty() && do_script_run(now));
1697269c 758
a9530964
SK
759# ifdef HAVE_TFTP
760 while (helper_buf_empty() && do_tftp_script_run());
761# endif
762
1697269c
SK
763 if (!helper_buf_empty())
764 {
765 FD_SET(daemon->helperfd, &wset);
766 bump_maxfd(daemon->helperfd, &maxfd);
767 }
7622fc06 768# else
5aabfc78
SK
769 /* need this for other side-effects */
770 while (do_script_run(now));
a9530964
SK
771
772# ifdef HAVE_TFTP
773 while (do_tftp_script_run());
774# endif
775
7622fc06 776# endif
5aabfc78 777#endif
7622fc06 778
f2621c7f
SK
779 /* must do this just before select(), when we know no
780 more calls to my_syslog() can occur */
781 set_log_writer(&wset, &maxfd);
782
5e9e0efb
SK
783 if (select(maxfd+1, &rset, &wset, &eset, tp) < 0)
784 {
785 /* otherwise undefined after error */
786 FD_ZERO(&rset); FD_ZERO(&wset); FD_ZERO(&eset);
787 }
788
789 now = dnsmasq_time();
9e4abcb5 790
f2621c7f 791 check_log_writer(&wset);
74c95c25
SK
792
793 /* Check the interfaces to see if any have exited DAD state
794 and if so, bind the address. */
795 if (is_dad_listeners())
796 {
797 enumerate_interfaces();
798 /* NB, is_dad_listeners() == 1 --> we're binding interfaces */
799 create_bound_listeners(0);
800 }
f2621c7f 801
c52e1897
SK
802#ifdef HAVE_LINUX_NETWORK
803 if (FD_ISSET(daemon->netlinkfd, &rset))
804 netlink_multicast();
805#endif
806
9e4abcb5 807 /* Check for changes to resolv files once per second max. */
3d8df260 808 /* Don't go silent for long periods if the clock goes backwards. */
9009d746
SK
809 if (daemon->last_resolv == 0 ||
810 difftime(now, daemon->last_resolv) > 1.0 ||
811 difftime(now, daemon->last_resolv) < -1.0)
9e4abcb5 812 {
8ef5ada2
SK
813 /* poll_resolv doesn't need to reload first time through, since
814 that's queued anyway. */
33820b7e 815
8ef5ada2
SK
816 poll_resolv(0, daemon->last_resolv != 0, now);
817 daemon->last_resolv = now;
9e4abcb5 818 }
5aabfc78 819
5e9e0efb 820 if (FD_ISSET(piperead, &rset))
5aabfc78 821 async_event(piperead, now);
7cebd20f 822
3d8df260
SK
823#ifdef HAVE_DBUS
824 /* if we didn't create a DBus connection, retry now. */
28866e95 825 if (option_bool(OPT_DBUS) && !daemon->dbus)
3d8df260
SK
826 {
827 char *err;
5aabfc78 828 if ((err = dbus_init()))
f2621c7f 829 my_syslog(LOG_WARNING, _("DBus error: %s"), err);
3d8df260 830 if (daemon->dbus)
f2621c7f 831 my_syslog(LOG_INFO, _("connected to system DBus"));
3d8df260 832 }
5aabfc78 833 check_dbus_listeners(&rset, &wset, &eset);
3d8df260 834#endif
824af85b 835
5aabfc78 836 check_dns_listeners(&rset, now);
832af0ba
SK
837
838#ifdef HAVE_TFTP
5aabfc78 839 check_tftp_listeners(&rset, now);
832af0ba
SK
840#endif
841
7622fc06 842#ifdef HAVE_DHCP
316e2730
SK
843 if (daemon->dhcp)
844 {
845 if (FD_ISSET(daemon->dhcpfd, &rset))
846 dhcp_packet(now, 0);
847 if (daemon->pxefd != -1 && FD_ISSET(daemon->pxefd, &rset))
848 dhcp_packet(now, 1);
849 }
1697269c 850
52b92f4d 851#ifdef HAVE_DHCP6
18c63eff
SK
852 if (daemon->dhcp6 && FD_ISSET(daemon->dhcp6fd, &rset))
853 dhcp6_packet(now);
c5ad4e79 854
18c63eff
SK
855 if (daemon->ra_contexts && FD_ISSET(daemon->icmp6fd, &rset))
856 icmp6_packet();
52b92f4d
SK
857#endif
858
1f15b81d 859# ifdef HAVE_SCRIPT
1697269c 860 if (daemon->helperfd != -1 && FD_ISSET(daemon->helperfd, &wset))
5aabfc78 861 helper_write();
7622fc06 862# endif
5aabfc78
SK
863#endif
864
9e4abcb5 865 }
9e4abcb5
SK
866}
867
3be34541
SK
868static void sig_handler(int sig)
869{
5e9e0efb 870 if (pid == 0)
3be34541 871 {
1697269c
SK
872 /* ignore anything other than TERM during startup
873 and in helper proc. (helper ignore TERM too) */
5e9e0efb 874 if (sig == SIGTERM)
5aabfc78 875 exit(EC_MISC);
3be34541 876 }
5aabfc78 877 else if (pid != getpid())
5e9e0efb 878 {
1697269c 879 /* alarm is used to kill TCP children after a fixed time. */
5e9e0efb 880 if (sig == SIGALRM)
7cebd20f 881 _exit(0);
3be34541 882 }
5aabfc78
SK
883 else
884 {
885 /* master process */
886 int event, errsave = errno;
887
888 if (sig == SIGHUP)
889 event = EVENT_RELOAD;
890 else if (sig == SIGCHLD)
891 event = EVENT_CHILD;
892 else if (sig == SIGALRM)
893 event = EVENT_ALARM;
894 else if (sig == SIGTERM)
895 event = EVENT_TERM;
896 else if (sig == SIGUSR1)
897 event = EVENT_DUMP;
898 else if (sig == SIGUSR2)
899 event = EVENT_REOPEN;
900 else
901 return;
902
c72daea8 903 send_event(pipewrite, event, 0, NULL);
5aabfc78
SK
904 errno = errsave;
905 }
906}
907
353ae4d2
SK
908/* now == 0 -> queue immediate callback */
909void send_alarm(time_t event, time_t now)
741c2952 910{
884a6dfe 911 if (now == 0 || event != 0)
353ae4d2 912 {
884a6dfe
SK
913 /* alarm(0) or alarm(-ve) doesn't do what we want.... */
914 if ((now == 0 || difftime(event, now) <= 0.0))
915 send_event(pipewrite, EVENT_ALARM, 0, NULL);
916 else
917 alarm((unsigned)difftime(event, now));
353ae4d2 918 }
741c2952
SK
919}
920
c72daea8 921void send_event(int fd, int event, int data, char *msg)
5aabfc78
SK
922{
923 struct event_desc ev;
c72daea8
SK
924 struct iovec iov[2];
925
5aabfc78
SK
926 ev.event = event;
927 ev.data = data;
c72daea8
SK
928 ev.msg_sz = msg ? strlen(msg) : 0;
929
930 iov[0].iov_base = &ev;
931 iov[0].iov_len = sizeof(ev);
932 iov[1].iov_base = msg;
933 iov[1].iov_len = ev.msg_sz;
1a6bca81
SK
934
935 /* error pipe, debug mode. */
936 if (fd == -1)
c72daea8 937 fatal_event(&ev, msg);
1a6bca81
SK
938 else
939 /* pipe is non-blocking and struct event_desc is smaller than
940 PIPE_BUF, so this either fails or writes everything */
c72daea8 941 while (writev(fd, iov, msg ? 2 : 1) == -1 && errno == EINTR);
5aabfc78
SK
942}
943
c72daea8
SK
944/* NOTE: the memory used to return msg is leaked: use msgs in events only
945 to describe fatal errors. */
946static int read_event(int fd, struct event_desc *evp, char **msg)
947{
948 char *buf;
949
950 if (!read_write(fd, (unsigned char *)evp, sizeof(struct event_desc), 1))
951 return 0;
952
953 *msg = NULL;
954
955 if (evp->msg_sz != 0 &&
956 (buf = malloc(evp->msg_sz + 1)) &&
957 read_write(fd, (unsigned char *)buf, evp->msg_sz, 1))
958 {
959 buf[evp->msg_sz] = 0;
960 *msg = buf;
961 }
962
963 return 1;
964}
965
966static void fatal_event(struct event_desc *ev, char *msg)
1a6bca81
SK
967{
968 errno = ev->data;
969
970 switch (ev->event)
971 {
972 case EVENT_DIE:
973 exit(0);
7622fc06
SK
974
975 case EVENT_FORK_ERR:
976 die(_("cannot fork into background: %s"), NULL, EC_MISC);
1a6bca81
SK
977
978 case EVENT_PIPE_ERR:
979 die(_("failed to create helper: %s"), NULL, EC_MISC);
980
981 case EVENT_CAP_ERR:
982 die(_("setting capabilities failed: %s"), NULL, EC_MISC);
983
984 case EVENT_USER_ERR:
c72daea8 985 die(_("failed to change user-id to %s: %s"), msg, EC_MISC);
1a6bca81
SK
986
987 case EVENT_GROUP_ERR:
c72daea8 988 die(_("failed to change group-id to %s: %s"), msg, EC_MISC);
1a6bca81
SK
989
990 case EVENT_PIDFILE:
c72daea8 991 die(_("failed to open pidfile %s: %s"), msg, EC_FILE);
1a6bca81
SK
992
993 case EVENT_LOG_ERR:
c72daea8
SK
994 die(_("cannot open log %s: %s"), msg, EC_FILE);
995
996 case EVENT_LUA_ERR:
997 die(_("failed to load Lua script: %s"), msg, EC_MISC);
1a6bca81
SK
998 }
999}
1000
5aabfc78
SK
1001static void async_event(int pipe, time_t now)
1002{
1003 pid_t p;
1004 struct event_desc ev;
1005 int i;
c72daea8
SK
1006 char *msg;
1007
1008 /* NOTE: the memory used to return msg is leaked: use msgs in events only
1009 to describe fatal errors. */
1010
1011 if (read_event(pipe, &ev, &msg))
5aabfc78
SK
1012 switch (ev.event)
1013 {
1014 case EVENT_RELOAD:
1015 clear_cache_and_reload(now);
28866e95 1016 if (daemon->port != 0 && daemon->resolv_files && option_bool(OPT_NO_POLL))
5aabfc78
SK
1017 {
1018 reload_servers(daemon->resolv_files->name);
1019 check_servers();
1020 }
7622fc06 1021#ifdef HAVE_DHCP
5aabfc78 1022 rerun_scripts();
7622fc06 1023#endif
5aabfc78
SK
1024 break;
1025
1026 case EVENT_DUMP:
824af85b
SK
1027 if (daemon->port != 0)
1028 dump_cache(now);
5aabfc78
SK
1029 break;
1030
1031 case EVENT_ALARM:
7622fc06 1032#ifdef HAVE_DHCP
52b92f4d 1033 if (daemon->dhcp || daemon->dhcp6)
5aabfc78
SK
1034 {
1035 lease_prune(NULL, now);
1036 lease_update_file(now);
1037 }
843c96b4
SK
1038#ifdef HAVE_DHCP6
1039 else if (daemon->ra_contexts)
353ae4d2
SK
1040 /* Not doing DHCP, so no lease system, manage alarms for ra only */
1041 send_alarm(periodic_ra(now), now);
843c96b4 1042#endif
7622fc06 1043#endif
5aabfc78
SK
1044 break;
1045
1046 case EVENT_CHILD:
1047 /* See Stevens 5.10 */
1048 while ((p = waitpid(-1, NULL, WNOHANG)) != 0)
1049 if (p == -1)
1050 {
1051 if (errno != EINTR)
1052 break;
1053 }
1054 else
1055 for (i = 0 ; i < MAX_PROCS; i++)
1056 if (daemon->tcp_pids[i] == p)
1057 daemon->tcp_pids[i] = 0;
1058 break;
1059
1060 case EVENT_KILLED:
c72daea8 1061 my_syslog(LOG_WARNING, _("script process killed by signal %d"), ev.data);
5aabfc78
SK
1062 break;
1063
1064 case EVENT_EXITED:
c72daea8 1065 my_syslog(LOG_WARNING, _("script process exited with status %d"), ev.data);
5aabfc78
SK
1066 break;
1067
1068 case EVENT_EXEC_ERR:
9e038946
SK
1069 my_syslog(LOG_ERR, _("failed to execute %s: %s"),
1070 daemon->lease_change_command, strerror(ev.data));
5aabfc78
SK
1071 break;
1072
1a6bca81 1073 /* necessary for fatal errors in helper */
c72daea8 1074 case EVENT_USER_ERR:
1a6bca81 1075 case EVENT_DIE:
c72daea8
SK
1076 case EVENT_LUA_ERR:
1077 fatal_event(&ev, msg);
9e038946
SK
1078 break;
1079
5aabfc78
SK
1080 case EVENT_REOPEN:
1081 /* Note: this may leave TCP-handling processes with the old file still open.
1082 Since any such process will die in CHILD_LIFETIME or probably much sooner,
1083 we leave them logging to the old file. */
1084 if (daemon->log_file != NULL)
1085 log_reopen(daemon->log_file);
1086 break;
1087
1088 case EVENT_TERM:
1089 /* Knock all our children on the head. */
1090 for (i = 0; i < MAX_PROCS; i++)
1091 if (daemon->tcp_pids[i] != 0)
1092 kill(daemon->tcp_pids[i], SIGALRM);
1093
c72daea8 1094#if defined(HAVE_SCRIPT)
5aabfc78
SK
1095 /* handle pending lease transitions */
1096 if (daemon->helperfd != -1)
1097 {
1098 /* block in writes until all done */
1099 if ((i = fcntl(daemon->helperfd, F_GETFL)) != -1)
1100 fcntl(daemon->helperfd, F_SETFL, i & ~O_NONBLOCK);
1101 do {
1102 helper_write();
1103 } while (!helper_buf_empty() || do_script_run(now));
1104 close(daemon->helperfd);
1105 }
1106#endif
1107
1108 if (daemon->lease_stream)
1109 fclose(daemon->lease_stream);
73a08a24
SK
1110
1111 if (daemon->runfile)
1112 unlink(daemon->runfile);
5aabfc78
SK
1113
1114 my_syslog(LOG_INFO, _("exiting on receipt of SIGTERM"));
1115 flush_log();
1116 exit(EC_GOOD);
1117 }
3be34541
SK
1118}
1119
8ef5ada2 1120void poll_resolv(int force, int do_reload, time_t now)
5aabfc78
SK
1121{
1122 struct resolvc *res, *latest;
1123 struct stat statbuf;
1124 time_t last_change = 0;
1125 /* There may be more than one possible file.
1126 Go through and find the one which changed _last_.
1127 Warn of any which can't be read. */
8ef5ada2 1128
28866e95 1129 if (daemon->port == 0 || option_bool(OPT_NO_POLL))
8ef5ada2
SK
1130 return;
1131
5aabfc78
SK
1132 for (latest = NULL, res = daemon->resolv_files; res; res = res->next)
1133 if (stat(res->name, &statbuf) == -1)
1134 {
8ef5ada2
SK
1135 if (force)
1136 {
1137 res->mtime = 0;
1138 continue;
1139 }
1140
5aabfc78
SK
1141 if (!res->logged)
1142 my_syslog(LOG_WARNING, _("failed to access %s: %s"), res->name, strerror(errno));
1143 res->logged = 1;
8ef5ada2
SK
1144
1145 if (res->mtime != 0)
1146 {
1147 /* existing file evaporated, force selection of the latest
1148 file even if its mtime hasn't changed since we last looked */
1149 poll_resolv(1, do_reload, now);
1150 return;
1151 }
5aabfc78
SK
1152 }
1153 else
1154 {
1155 res->logged = 0;
8ef5ada2
SK
1156 if (force || (statbuf.st_mtime != res->mtime))
1157 {
1158 res->mtime = statbuf.st_mtime;
5aabfc78
SK
1159 if (difftime(statbuf.st_mtime, last_change) > 0.0)
1160 {
1161 last_change = statbuf.st_mtime;
1162 latest = res;
1163 }
1164 }
1165 }
1166
1167 if (latest)
1168 {
1169 static int warned = 0;
1170 if (reload_servers(latest->name))
1171 {
1172 my_syslog(LOG_INFO, _("reading %s"), latest->name);
1173 warned = 0;
1174 check_servers();
28866e95 1175 if (option_bool(OPT_RELOAD) && do_reload)
8ef5ada2 1176 clear_cache_and_reload(now);
5aabfc78
SK
1177 }
1178 else
1179 {
1180 latest->mtime = 0;
1181 if (!warned)
1182 {
1183 my_syslog(LOG_WARNING, _("no servers found in %s, will retry"), latest->name);
1184 warned = 1;
1185 }
1186 }
1187 }
1188}
3d8df260 1189
5aabfc78 1190void clear_cache_and_reload(time_t now)
3d8df260 1191{
824af85b 1192 if (daemon->port != 0)
7622fc06 1193 cache_reload();
824af85b 1194
7622fc06 1195#ifdef HAVE_DHCP
52b92f4d 1196 if (daemon->dhcp || daemon->dhcp6)
3d8df260 1197 {
28866e95 1198 if (option_bool(OPT_ETHERS))
5aabfc78 1199 dhcp_read_ethers();
824af85b 1200 reread_dhcp();
3d8df260 1201 dhcp_update_configs(daemon->dhcp_conf);
824af85b 1202 check_dhcp_hosts(0);
5aabfc78
SK
1203 lease_update_from_configs();
1204 lease_update_file(now);
353ae4d2 1205 lease_update_dns(1);
3d8df260 1206 }
843c96b4
SK
1207#ifdef HAVE_DHCP6
1208 else if (daemon->ra_contexts)
2021c662
SK
1209 /* Not doing DHCP, so no lease system, manage
1210 alarms for ra only */
1211 send_alarm(periodic_ra(now), now);
843c96b4 1212#endif
7622fc06 1213#endif
3d8df260
SK
1214}
1215
5aabfc78 1216static int set_dns_listeners(time_t now, fd_set *set, int *maxfdp)
3be34541
SK
1217{
1218 struct serverfd *serverfdp;
1219 struct listener *listener;
824af85b 1220 int wait = 0, i;
832af0ba
SK
1221
1222#ifdef HAVE_TFTP
1223 int tftp = 0;
1224 struct tftp_transfer *transfer;
1225 for (transfer = daemon->tftp_trans; transfer; transfer = transfer->next)
1226 {
1227 tftp++;
1228 FD_SET(transfer->sockfd, set);
1229 bump_maxfd(transfer->sockfd, maxfdp);
1230 }
1231#endif
1232
1697269c 1233 /* will we be able to get memory? */
824af85b
SK
1234 if (daemon->port != 0)
1235 get_new_frec(now, &wait);
1697269c 1236
3be34541
SK
1237 for (serverfdp = daemon->sfds; serverfdp; serverfdp = serverfdp->next)
1238 {
1239 FD_SET(serverfdp->fd, set);
1697269c 1240 bump_maxfd(serverfdp->fd, maxfdp);
3be34541 1241 }
1a6bca81
SK
1242
1243 if (daemon->port != 0 && !daemon->osport)
1244 for (i = 0; i < RANDOM_SOCKS; i++)
1245 if (daemon->randomsocks[i].refcount != 0)
1246 {
1247 FD_SET(daemon->randomsocks[i].fd, set);
1248 bump_maxfd(daemon->randomsocks[i].fd, maxfdp);
1249 }
1250
3be34541
SK
1251 for (listener = daemon->listeners; listener; listener = listener->next)
1252 {
1697269c 1253 /* only listen for queries if we have resources */
824af85b 1254 if (listener->fd != -1 && wait == 0)
1697269c
SK
1255 {
1256 FD_SET(listener->fd, set);
1257 bump_maxfd(listener->fd, maxfdp);
1258 }
1259
1260 /* death of a child goes through the select loop, so
1261 we don't need to explicitly arrange to wake up here */
824af85b
SK
1262 if (listener->tcpfd != -1)
1263 for (i = 0; i < MAX_PROCS; i++)
1264 if (daemon->tcp_pids[i] == 0)
1265 {
1266 FD_SET(listener->tcpfd, set);
1267 bump_maxfd(listener->tcpfd, maxfdp);
1268 break;
1269 }
9e4abcb5 1270
832af0ba
SK
1271#ifdef HAVE_TFTP
1272 if (tftp <= daemon->tftp_max && listener->tftpfd != -1)
1273 {
1274 FD_SET(listener->tftpfd, set);
1275 bump_maxfd(listener->tftpfd, maxfdp);
1276 }
1277#endif
1278
1279 }
1280
1697269c 1281 return wait;
3be34541 1282}
9e4abcb5 1283
5aabfc78 1284static void check_dns_listeners(fd_set *set, time_t now)
3be34541
SK
1285{
1286 struct serverfd *serverfdp;
1a6bca81
SK
1287 struct listener *listener;
1288 int i;
1289
832af0ba
SK
1290 for (serverfdp = daemon->sfds; serverfdp; serverfdp = serverfdp->next)
1291 if (FD_ISSET(serverfdp->fd, set))
1a6bca81
SK
1292 reply_query(serverfdp->fd, serverfdp->source_addr.sa.sa_family, now);
1293
1294 if (daemon->port != 0 && !daemon->osport)
1295 for (i = 0; i < RANDOM_SOCKS; i++)
1296 if (daemon->randomsocks[i].refcount != 0 &&
1297 FD_ISSET(daemon->randomsocks[i].fd, set))
1298 reply_query(daemon->randomsocks[i].fd, daemon->randomsocks[i].family, now);
832af0ba
SK
1299
1300 for (listener = daemon->listeners; listener; listener = listener->next)
1301 {
824af85b 1302 if (listener->fd != -1 && FD_ISSET(listener->fd, set))
5aabfc78 1303 receive_query(listener, now);
1a6bca81 1304
832af0ba
SK
1305#ifdef HAVE_TFTP
1306 if (listener->tftpfd != -1 && FD_ISSET(listener->tftpfd, set))
5aabfc78 1307 tftp_request(listener, now);
832af0ba 1308#endif
3be34541 1309
824af85b 1310 if (listener->tcpfd != -1 && FD_ISSET(listener->tcpfd, set))
832af0ba
SK
1311 {
1312 int confd;
1313 struct irec *iface = NULL;
1314 pid_t p;
52d4abf2
SK
1315 union mysockaddr tcp_addr;
1316 socklen_t tcp_len = sizeof(union mysockaddr);
1317
1318 while ((confd = accept(listener->tcpfd, NULL, NULL)) == -1 && errno == EINTR);
832af0ba 1319
52d4abf2
SK
1320 if (confd == -1 ||
1321 getsockname(confd, (struct sockaddr *)&tcp_addr, &tcp_len) == -1)
832af0ba
SK
1322 continue;
1323
28866e95 1324 if (option_bool(OPT_NOWILD))
52d4abf2 1325 iface = listener->iface; /* May be NULL */
832af0ba
SK
1326 else
1327 {
832af0ba
SK
1328 /* Check for allowed interfaces when binding the wildcard address:
1329 we do this by looking for an interface with the same address as
1330 the local address of the TCP connection, then looking to see if that's
1331 an allowed interface. As a side effect, we get the netmask of the
1332 interface too, for localisation. */
3be34541 1333
832af0ba 1334 /* interface may be new since startup */
52d4abf2 1335 if (enumerate_interfaces())
832af0ba
SK
1336 for (iface = daemon->interfaces; iface; iface = iface->next)
1337 if (sockaddr_isequal(&iface->addr, &tcp_addr))
1338 break;
1339 }
1340
52d4abf2 1341 if (!iface && !option_bool(OPT_NOWILD))
832af0ba
SK
1342 {
1343 shutdown(confd, SHUT_RDWR);
1344 close(confd);
1345 }
59353a6b 1346#ifndef NO_FORK
28866e95 1347 else if (!option_bool(OPT_DEBUG) && (p = fork()) != 0)
832af0ba
SK
1348 {
1349 if (p != -1)
1350 {
1351 int i;
1352 for (i = 0; i < MAX_PROCS; i++)
1353 if (daemon->tcp_pids[i] == 0)
1354 {
1355 daemon->tcp_pids[i] = p;
1356 break;
1357 }
1358 }
1359 close(confd);
1360 }
1361#endif
1362 else
1363 {
1364 unsigned char *buff;
1365 struct server *s;
1366 int flags;
52d4abf2
SK
1367 struct in_addr netmask;
1368
1369 if (iface)
1370 netmask = iface->netmask;
1371 else
1372 netmask.s_addr = 0;
1373
8ef5ada2
SK
1374#ifndef NO_FORK
1375 /* Arrange for SIGALARM after CHILD_LIFETIME seconds to
1376 terminate the process. */
28866e95 1377 if (!option_bool(OPT_DEBUG))
832af0ba 1378 alarm(CHILD_LIFETIME);
8ef5ada2
SK
1379#endif
1380
832af0ba
SK
1381 /* start with no upstream connections. */
1382 for (s = daemon->servers; s; s = s->next)
7cebd20f 1383 s->tcpfd = -1;
832af0ba
SK
1384
1385 /* The connected socket inherits non-blocking
1386 attribute from the listening socket.
1387 Reset that here. */
1388 if ((flags = fcntl(confd, F_GETFL, 0)) != -1)
1389 fcntl(confd, F_SETFL, flags & ~O_NONBLOCK);
1390
52d4abf2 1391 buff = tcp_request(confd, now, &tcp_addr, netmask);
7cebd20f 1392
832af0ba
SK
1393 shutdown(confd, SHUT_RDWR);
1394 close(confd);
1395
1396 if (buff)
1397 free(buff);
1398
1399 for (s = daemon->servers; s; s = s->next)
1400 if (s->tcpfd != -1)
1401 {
1402 shutdown(s->tcpfd, SHUT_RDWR);
1403 close(s->tcpfd);
1404 }
7cebd20f 1405#ifndef NO_FORK
28866e95 1406 if (!option_bool(OPT_DEBUG))
5aabfc78
SK
1407 {
1408 flush_log();
1409 _exit(0);
1410 }
59353a6b 1411#endif
832af0ba
SK
1412 }
1413 }
1414 }
3be34541
SK
1415}
1416
7622fc06 1417#ifdef HAVE_DHCP
5e9e0efb
SK
1418int make_icmp_sock(void)
1419{
7cebd20f 1420 int fd;
5e9e0efb
SK
1421 int zeroopt = 0;
1422
1423 if ((fd = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP)) != -1)
1424 {
7cebd20f 1425 if (!fix_fd(fd) ||
5e9e0efb
SK
1426 setsockopt(fd, SOL_SOCKET, SO_DONTROUTE, &zeroopt, sizeof(zeroopt)) == -1)
1427 {
1428 close(fd);
1429 fd = -1;
1430 }
1431 }
1432
1433 return fd;
1434}
1435
5aabfc78 1436int icmp_ping(struct in_addr addr)
3be34541 1437{
5e9e0efb 1438 /* Try and get an ICMP echo from a machine. */
3be34541
SK
1439
1440 /* Note that whilst in the three second wait, we check for
832af0ba 1441 (and service) events on the DNS and TFTP sockets, (so doing that
3be34541
SK
1442 better not use any resources our caller has in use...)
1443 but we remain deaf to signals or further DHCP packets. */
1444
5e9e0efb 1445 int fd;
3be34541
SK
1446 struct sockaddr_in saddr;
1447 struct {
1448 struct ip ip;
1449 struct icmp icmp;
1450 } packet;
1451 unsigned short id = rand16();
1452 unsigned int i, j;
5e9e0efb 1453 int gotreply = 0;
3be34541 1454 time_t start, now;
5e9e0efb 1455
824af85b 1456#if defined(HAVE_LINUX_NETWORK) || defined (HAVE_SOLARIS_NETWORK)
5e9e0efb
SK
1457 if ((fd = make_icmp_sock()) == -1)
1458 return 0;
1459#else
1460 int opt = 2000;
1461 fd = daemon->dhcp_icmp_fd;
1462 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(opt));
1463#endif
1464
3be34541
SK
1465 saddr.sin_family = AF_INET;
1466 saddr.sin_port = 0;
1467 saddr.sin_addr = addr;
1468#ifdef HAVE_SOCKADDR_SA_LEN
1469 saddr.sin_len = sizeof(struct sockaddr_in);
1470#endif
1471
1472 memset(&packet.icmp, 0, sizeof(packet.icmp));
1473 packet.icmp.icmp_type = ICMP_ECHO;
1474 packet.icmp.icmp_id = id;
1475 for (j = 0, i = 0; i < sizeof(struct icmp) / 2; i++)
1476 j += ((u16 *)&packet.icmp)[i];
1477 while (j>>16)
1478 j = (j & 0xffff) + (j >> 16);
1479 packet.icmp.icmp_cksum = (j == 0xffff) ? j : ~j;
1480
5e9e0efb 1481 while (sendto(fd, (char *)&packet.icmp, sizeof(struct icmp), 0,
fd9fa481
SK
1482 (struct sockaddr *)&saddr, sizeof(saddr)) == -1 &&
1483 retry_send());
1484
5e9e0efb
SK
1485 for (now = start = dnsmasq_time();
1486 difftime(now, start) < (float)PING_WAIT;)
fd9fa481
SK
1487 {
1488 struct timeval tv;
f2621c7f 1489 fd_set rset, wset;
fd9fa481 1490 struct sockaddr_in faddr;
1697269c 1491 int maxfd = fd;
3d8df260 1492 socklen_t len = sizeof(faddr);
fd9fa481
SK
1493
1494 tv.tv_usec = 250000;
1495 tv.tv_sec = 0;
1496
1497 FD_ZERO(&rset);
f2621c7f 1498 FD_ZERO(&wset);
5e9e0efb 1499 FD_SET(fd, &rset);
5aabfc78 1500 set_dns_listeners(now, &rset, &maxfd);
f2621c7f 1501 set_log_writer(&wset, &maxfd);
c5ad4e79
SK
1502
1503#ifdef HAVE_DHCP6
843c96b4 1504 if (daemon->ra_contexts)
c5ad4e79
SK
1505 {
1506 FD_SET(daemon->icmp6fd, &rset);
1507 bump_maxfd(daemon->icmp6fd, &maxfd);
1508 }
1509#endif
1510
f2621c7f
SK
1511 if (select(maxfd+1, &rset, &wset, NULL, &tv) < 0)
1512 {
1513 FD_ZERO(&rset);
1514 FD_ZERO(&wset);
1515 }
1516
5e9e0efb 1517 now = dnsmasq_time();
f2621c7f
SK
1518
1519 check_log_writer(&wset);
5aabfc78 1520 check_dns_listeners(&rset, now);
832af0ba 1521
c5ad4e79 1522#ifdef HAVE_DHCP6
843c96b4 1523 if (daemon->ra_contexts && FD_ISSET(daemon->icmp6fd, &rset))
c5ad4e79
SK
1524 icmp6_packet();
1525#endif
1526
832af0ba 1527#ifdef HAVE_TFTP
5aabfc78 1528 check_tftp_listeners(&rset, now);
832af0ba
SK
1529#endif
1530
5e9e0efb
SK
1531 if (FD_ISSET(fd, &rset) &&
1532 recvfrom(fd, &packet, sizeof(packet), 0,
fd9fa481
SK
1533 (struct sockaddr *)&faddr, &len) == sizeof(packet) &&
1534 saddr.sin_addr.s_addr == faddr.sin_addr.s_addr &&
1535 packet.icmp.icmp_type == ICMP_ECHOREPLY &&
1536 packet.icmp.icmp_seq == 0 &&
1537 packet.icmp.icmp_id == id)
1538 {
1539 gotreply = 1;
1540 break;
1541 }
1542 }
1543
824af85b 1544#if defined(HAVE_LINUX_NETWORK) || defined(HAVE_SOLARIS_NETWORK)
5e9e0efb
SK
1545 close(fd);
1546#else
3be34541 1547 opt = 1;
5e9e0efb
SK
1548 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(opt));
1549#endif
1550
3be34541
SK
1551 return gotreply;
1552}
7622fc06 1553#endif
0a852541
SK
1554
1555