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