Hand the lookup its pipe and child: called directly if a slot is free, * else from dns_pump_queue when one exits. */
| 152 | /* Hand the lookup its pipe and child: called directly if a slot is free, |
| 153 | * else from dns_pump_queue when one exits. */ |
| 154 | static void dns_start(struct dns_lookup *lookup) |
| 155 | { |
| 156 | int fds[2]; |
| 157 | |
| 158 | assert(!lookup->started); |
| 159 | |
| 160 | if (pipe(fds) != 0) { |
| 161 | lookup->err = tal_fmt(lookup, "dns pipe failed: %s", |
| 162 | strerror(errno)); |
| 163 | dns_finish(lookup); |
| 164 | return; |
| 165 | } |
| 166 | /* Not strictly needed (we don't exec), but NSS backends can |
| 167 | * subprocess, and leaking the write end there would pin EOF. */ |
| 168 | fcntl(fds[0], F_SETFD, FD_CLOEXEC); |
| 169 | fcntl(fds[1], F_SETFD, FD_CLOEXEC); |
| 170 | |
| 171 | lookup->child = fork(); |
| 172 | if (lookup->child < 0) { |
| 173 | close(fds[0]); |
| 174 | close(fds[1]); |
| 175 | lookup->err = tal_fmt(lookup, "dns fork failed: %s", |
| 176 | strerror(errno)); |
| 177 | dns_finish(lookup); |
| 178 | return; |
| 179 | } |
| 180 | |
| 181 | if (lookup->child == 0) { |
| 182 | close(fds[0]); |
| 183 | dns_resolve_in_child(fds[1], lookup->hostname, lookup->port); |
| 184 | /* Not reached. */ |
| 185 | } |
| 186 | |
| 187 | close(fds[1]); |
| 188 | |
| 189 | lookup->conn = io_new_conn(lookup, fds[0], dns_read_init, lookup); |
| 190 | if (!lookup->conn) { /* Out of poll fds, etc. */ |
| 191 | close_noerr(fds[0]); |
| 192 | /* child > 0, so dns_finish will kill it. */ |
| 193 | lookup->err = tal_fmt(lookup, "dns pipe setup failed"); |
| 194 | dns_finish(lookup); |
| 195 | return; |
| 196 | } |
| 197 | tal_add_destructor2(lookup->conn, dns_conn_destroy, lookup); |
| 198 | |
| 199 | status_debug("dns: async lookup of %s started", lookup->hostname); |
| 200 | dns_num_active++; |
| 201 | lookup->started = true; |
| 202 | } |
| 203 | |
| 204 | /* Start queued lookups while we have child slots free. A failed start |
| 205 | * finishes its lookup (with an error, via callback) without ever taking a |
no test coverage detected