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