5 #include "ntp_workimpl.h"
14 #include "ntp_stdlib.h"
15 #include "ntp_malloc.h"
16 #include "ntp_syslog.h"
19 #include "ntp_assert.h"
20 #include "ntp_unixtime.h"
21 #include "intreswork.h"
24 #define CHILD_MAX_IDLE (3 * 60) /* seconds, idle worker limit */
26 blocking_child ** blocking_children;
27 size_t blocking_children_alloc;
28 int worker_per_query; /* boolean */
29 int intres_req_pending;
30 volatile u_int blocking_child_ready_seen;
31 volatile u_int blocking_child_ready_done;
34 #ifndef HAVE_IO_COMPLETION_PORT
38 * Provides an AF_UNIX socketpair on systems which have them, otherwise
39 * pair of unidirectional pipes.
51 #ifdef HAVE_SOCKETPAIR
52 rc = socketpair(AF_UNIX, SOCK_STREAM, 0, &fds[0]);
67 caller_fds[0] = fds[0];
68 caller_fds[1] = fds[1];
70 *is_pipe = called_pipe;
79 * Close all file descriptors except the given keep_fd.
88 for (fd = 0; fd < keep_fd; fd++)
91 close_all_beyond(keep_fd);
98 * Close all file descriptors after the given keep_fd, which is the
99 * highest fd to keep open.
106 # ifdef HAVE_CLOSEFROM
107 closefrom(keep_fd + 1);
108 # elif defined(F_CLOSEM)
110 * From 'Writing Reliable AIX Daemons,' SG24-4946-00,
111 * by Eric Agar (saves us from doing 32767 system
114 if (fcntl(keep_fd + 1, F_CLOSEM, 0) == -1)
115 msyslog(LOG_ERR, "F_CLOSEM(%d): %m", keep_fd + 1);
116 # else /* !HAVE_CLOSEFROM && !F_CLOSEM follows */
120 max_fd = GETDTABLESIZE();
121 for (fd = keep_fd + 1; fd < max_fd; fd++)
123 # endif /* !HAVE_CLOSEFROM && !F_CLOSEM */
125 #endif /* HAVE_IO_COMPLETION_PORT */
129 available_blocking_child_slot(void)
131 const size_t each = sizeof(blocking_children[0]);
138 for (slot = 0; slot < blocking_children_alloc; slot++) {
139 if (NULL == blocking_children[slot])
141 if (blocking_children[slot]->reusable) {
142 blocking_children[slot]->reusable = FALSE;
147 prev_alloc = blocking_children_alloc;
148 prev_octets = prev_alloc * each;
149 new_alloc = blocking_children_alloc + 4;
150 octets = new_alloc * each;
151 blocking_children = erealloc_zero(blocking_children, octets,
153 blocking_children_alloc = new_alloc;
155 /* assume we'll never have enough workers to overflow u_int */
156 return (u_int)prev_alloc;
161 queue_blocking_request(
162 blocking_work_req rtype,
165 blocking_work_callback done_func,
169 static u_int intres_slot = UINT_MAX;
172 blocking_pipe_header req_hdr;
174 req_hdr.octets = sizeof(req_hdr) + reqsize;
175 req_hdr.magic_sig = BLOCKING_REQ_MAGIC;
176 req_hdr.rtype = rtype;
177 req_hdr.done_func = done_func;
178 req_hdr.context = context;
180 child_slot = UINT_MAX;
181 if (worker_per_query || UINT_MAX == intres_slot ||
182 blocking_children[intres_slot]->reusable)
183 child_slot = available_blocking_child_slot();
184 if (!worker_per_query) {
185 if (UINT_MAX == intres_slot)
186 intres_slot = child_slot;
188 child_slot = intres_slot;
189 if (0 == intres_req_pending)
190 intres_timeout_req(0);
192 intres_req_pending++;
193 INSIST(UINT_MAX != child_slot);
194 c = blocking_children[child_slot];
196 c = emalloc_zero(sizeof(*c));
198 c->req_read_pipe = -1;
199 c->req_write_pipe = -1;
202 c->resp_read_pipe = -1;
203 c->resp_write_pipe = -1;
205 blocking_children[child_slot] = c;
207 req_hdr.child_idx = child_slot;
209 return send_blocking_req_internal(c, &req_hdr, req);
213 int queue_blocking_response(
215 blocking_pipe_header * resp,
217 const blocking_pipe_header * req
220 resp->octets = respsize;
221 resp->magic_sig = BLOCKING_RESP_MAGIC;
222 resp->rtype = req->rtype;
223 resp->context = req->context;
224 resp->done_func = req->done_func;
226 return send_blocking_resp_internal(c, resp);
231 process_blocking_resp(
235 blocking_pipe_header * resp;
239 * On Windows send_blocking_resp_internal() may signal the
240 * blocking_response_ready event multiple times while we're
241 * processing a response, so always consume all available
242 * responses before returning to test the event again.
247 resp = receive_blocking_resp_internal(c);
249 DEBUG_REQUIRE(BLOCKING_RESP_MAGIC ==
251 data = (char *)resp + sizeof(*resp);
252 intres_req_pending--;
253 (*resp->done_func)(resp->rtype, resp->context,
254 resp->octets - sizeof(*resp),
259 } while (NULL != resp);
261 if (!worker_per_query && 0 == intres_req_pending)
262 intres_timeout_req(CHILD_MAX_IDLE);
263 else if (worker_per_query)
268 harvest_blocking_responses(void)
272 u_int scseen, scdone;
274 scseen = blocking_child_ready_seen;
275 scdone = blocking_child_ready_done;
276 if (scdone != scseen) {
277 blocking_child_ready_done = scseen;
278 for (idx = 0; idx < blocking_children_alloc; idx++) {
279 cp = blocking_children[idx];
282 scseen = cp->resp_ready_seen;
283 scdone = cp->resp_ready_done;
284 if (scdone != scseen) {
285 cp->resp_ready_done = scseen;
286 process_blocking_resp(cp);
294 * blocking_child_common runs as a forked child or a thread
297 blocking_child_common(
302 blocking_pipe_header *req;
306 req = receive_blocking_req_internal(c);
312 DEBUG_REQUIRE(BLOCKING_REQ_MAGIC == req->magic_sig);
314 switch (req->rtype) {
315 case BLOCKING_GETADDRINFO:
316 if (blocking_getaddrinfo(c, req))
320 case BLOCKING_GETNAMEINFO:
321 if (blocking_getnameinfo(c, req))
326 msyslog(LOG_ERR, "unknown req %d to blocking worker", req->rtype);
338 * worker_idle_timer_fired()
340 * The parent starts this timer when the last pending response has been
341 * received from the child, making it idle, and clears the timer when a
342 * request is dispatched to the child. Once the timer expires, the
343 * child is sent packing.
345 * This is called when worker_idle_timer is nonzero and less than or
346 * equal to current_time.
349 worker_idle_timer_fired(void)
354 DEBUG_REQUIRE(0 == intres_req_pending);
356 intres_timeout_req(0);
357 for (idx = 0; idx < blocking_children_alloc; idx++) {
358 c = blocking_children[idx];
366 #else /* !WORKER follows */
367 int ntp_worker_nonempty_compilation_unit;