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