smbd: Avoid checking the_lock->id for fresh locks
[Samba.git] / source3 / lib / server_prefork.c
blobc725607e9578f4ce0800b59c7050c82ec41c66ca
1 /*
2 Unix SMB/CIFS implementation.
3 Common server globals
5 Copyright (C) Simo Sorce <idra@samba.org> 2011
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
22 #include "serverid.h"
23 #include "messages.h"
24 #include "system/time.h"
25 #include "system/shmem.h"
26 #include "system/filesys.h"
27 #include "server_prefork.h"
28 #include "../lib/util/samba_util.h"
29 #include "../lib/util/tevent_unix.h"
31 struct prefork_pool {
33 int listen_fd_size;
34 int *listen_fds;
36 prefork_main_fn_t *main_fn;
37 void *private_data;
39 int pool_size;
40 struct pf_worker_data *pool;
42 int allowed_clients;
44 prefork_sigchld_fn_t *sigchld_fn;
45 void *sigchld_data;
48 static bool prefork_setup_sigchld_handler(struct tevent_context *ev_ctx,
49 struct prefork_pool *pfp);
51 static int prefork_pool_destructor(struct prefork_pool *pfp)
53 anonymous_shared_free(pfp->pool);
54 return 0;
57 bool prefork_create_pool(TALLOC_CTX *mem_ctx,
58 struct tevent_context *ev_ctx,
59 struct messaging_context *msg_ctx,
60 int listen_fd_size, int *listen_fds,
61 int min_children, int max_children,
62 prefork_main_fn_t *main_fn, void *private_data,
63 struct prefork_pool **pf_pool)
65 struct prefork_pool *pfp;
66 pid_t pid;
67 time_t now = time(NULL);
68 size_t data_size;
69 int ret;
70 int i;
71 bool ok;
73 pfp = talloc_zero(mem_ctx, struct prefork_pool);
74 if (!pfp) {
75 DEBUG(1, ("Out of memory!\n"));
76 return false;
78 pfp->listen_fd_size = listen_fd_size;
79 pfp->listen_fds = talloc_array(pfp, int, listen_fd_size);
80 if (!pfp->listen_fds) {
81 DEBUG(1, ("Out of memory!\n"));
82 return false;
84 for (i = 0; i < listen_fd_size; i++) {
85 pfp->listen_fds[i] = listen_fds[i];
86 /* force sockets in non-blocking mode */
87 set_blocking(listen_fds[i], false);
89 pfp->main_fn = main_fn;
90 pfp->private_data = private_data;
92 pfp->pool_size = max_children;
93 data_size = sizeof(struct pf_worker_data) * max_children;
95 pfp->pool = (struct pf_worker_data *)anonymous_shared_allocate(
96 data_size);
97 if (pfp->pool == NULL) {
98 DEBUG(1, ("Failed to mmap memory for prefork pool!\n"));
99 talloc_free(pfp);
100 return false;
102 talloc_set_destructor(pfp, prefork_pool_destructor);
104 for (i = 0; i < min_children; i++) {
106 pfp->pool[i].allowed_clients = 1;
107 pfp->pool[i].started = now;
109 pid = fork();
110 switch (pid) {
111 case -1:
112 DEBUG(1, ("Failed to prefork child n. %d !\n", i));
113 break;
115 case 0: /* THE CHILD */
117 pfp->pool[i].status = PF_WORKER_ALIVE;
118 ret = pfp->main_fn(ev_ctx, msg_ctx,
119 &pfp->pool[i], i + 1,
120 pfp->listen_fd_size,
121 pfp->listen_fds,
122 pfp->private_data);
123 exit(ret);
125 default: /* THE PARENT */
126 pfp->pool[i].pid = pid;
127 break;
131 ok = prefork_setup_sigchld_handler(ev_ctx, pfp);
132 if (!ok) {
133 DEBUG(1, ("Failed to setup SIGCHLD Handler!\n"));
134 talloc_free(pfp);
135 return false;
138 *pf_pool = pfp;
139 return true;
142 /* Provide the new max children number in new_max
143 * (must be larger than current max).
144 * Returns: 0 if all fine
145 * ENOSPC if mremap fails to expand
146 * EINVAL if new_max is invalid
148 int prefork_expand_pool(struct prefork_pool *pfp, int new_max)
150 struct prefork_pool *pool;
151 size_t old_size;
152 size_t new_size;
153 int ret;
155 if (new_max <= pfp->pool_size) {
156 return EINVAL;
159 old_size = sizeof(struct pf_worker_data) * pfp->pool_size;
160 new_size = sizeof(struct pf_worker_data) * new_max;
162 pool = (struct prefork_pool *)anonymous_shared_resize(
163 &pfp->pool, new_size, false);
164 if (pool == NULL) {
165 ret = errno;
166 DEBUG(3, ("Failed to mremap memory (%d: %s)!\n",
167 ret, strerror(ret)));
168 return ret;
171 memset(&pool[pfp->pool_size], 0, new_size - old_size);
173 pfp->pool_size = new_max;
175 return 0;
178 int prefork_add_children(struct tevent_context *ev_ctx,
179 struct messaging_context *msg_ctx,
180 struct prefork_pool *pfp,
181 int num_children)
183 pid_t pid;
184 time_t now = time(NULL);
185 int ret;
186 int i, j;
188 for (i = 0, j = 0; i < pfp->pool_size && j < num_children; i++) {
190 if (pfp->pool[i].status != PF_WORKER_NONE) {
191 continue;
194 pfp->pool[i].allowed_clients = 1;
195 pfp->pool[i].started = now;
197 pid = fork();
198 switch (pid) {
199 case -1:
200 DEBUG(1, ("Failed to prefork child n. %d !\n", j));
201 break;
203 case 0: /* THE CHILD */
205 pfp->pool[i].status = PF_WORKER_ALIVE;
206 ret = pfp->main_fn(ev_ctx, msg_ctx,
207 &pfp->pool[i], i + 1,
208 pfp->listen_fd_size,
209 pfp->listen_fds,
210 pfp->private_data);
212 pfp->pool[i].status = PF_WORKER_EXITING;
213 exit(ret);
215 default: /* THE PARENT */
216 pfp->pool[i].pid = pid;
217 j++;
218 break;
222 DEBUG(5, ("Added %d children!\n", j));
224 return j;
227 struct prefork_oldest {
228 int num;
229 time_t started;
232 /* sort in inverse order */
233 static int prefork_sort_oldest(const void *ap, const void *bp)
235 const struct prefork_oldest *a = (const struct prefork_oldest *)ap;
236 const struct prefork_oldest *b = (const struct prefork_oldest *)bp;
238 if (a->started == b->started) {
239 return 0;
241 if (a->started < b->started) {
242 return 1;
244 return -1;
247 int prefork_retire_children(struct messaging_context *msg_ctx,
248 struct prefork_pool *pfp,
249 int num_children, time_t age_limit)
251 const DATA_BLOB ping = data_blob_null;
252 time_t now = time(NULL);
253 struct prefork_oldest *oldest;
254 int i, j;
256 oldest = talloc_array(pfp, struct prefork_oldest, pfp->pool_size);
257 if (!oldest) {
258 return -1;
261 for (i = 0; i < pfp->pool_size; i++) {
262 oldest[i].num = i;
263 if (pfp->pool[i].status == PF_WORKER_ALIVE ||
264 pfp->pool[i].status == PF_WORKER_ACCEPTING) {
265 oldest[i].started = pfp->pool[i].started;
266 } else {
267 oldest[i].started = now;
271 qsort(oldest, pfp->pool_size,
272 sizeof(struct prefork_oldest),
273 prefork_sort_oldest);
275 for (i = 0, j = 0; i < pfp->pool_size && j < num_children; i++) {
276 if (((pfp->pool[i].status == PF_WORKER_ALIVE) &&
277 (pfp->pool[i].num_clients < 1)) &&
278 (pfp->pool[i].started <= age_limit)) {
279 /* tell the child it's time to give up */
280 DEBUG(5, ("Retiring pid %u!\n", (unsigned int)pfp->pool[i].pid));
281 pfp->pool[i].cmds = PF_SRV_MSG_EXIT;
282 messaging_send(msg_ctx,
283 pid_to_procid(pfp->pool[i].pid),
284 MSG_PREFORK_PARENT_EVENT, &ping);
285 j++;
289 return j;
292 int prefork_count_children(struct prefork_pool *pfp, int *active)
294 int i, a, t;
296 a = 0;
297 t = 0;
298 for (i = 0; i < pfp->pool_size; i++) {
299 if (pfp->pool[i].status == PF_WORKER_NONE) {
300 continue;
303 t++;
305 if ((pfp->pool[i].status == PF_WORKER_EXITING) ||
306 (pfp->pool[i].num_clients <= 0)) {
307 continue;
310 a++;
313 if (active) {
314 *active = a;
316 return t;
319 static void prefork_cleanup_loop(struct prefork_pool *pfp)
321 int status;
322 pid_t pid;
323 int i;
325 /* TODO: should we use a process group id wait instead of looping ? */
326 for (i = 0; i < pfp->pool_size; i++) {
327 if (pfp->pool[i].status == PF_WORKER_NONE ||
328 pfp->pool[i].pid == 0) {
329 continue;
332 pid = sys_waitpid(pfp->pool[i].pid, &status, WNOHANG);
333 if (pid > 0) {
335 if (pfp->pool[i].status != PF_WORKER_EXITING) {
336 DEBUG(3, ("Child (%d) terminated abnormally:"
337 " %d\n", (int)pid, status));
338 } else {
339 DEBUG(10, ("Child (%d) terminated with status:"
340 " %d\n", (int)pid, status));
343 /* reset all fields,
344 * this makes status = PF_WORK_NONE */
345 memset(&pfp->pool[i], 0,
346 sizeof(struct pf_worker_data));
352 int prefork_count_allowed_connections(struct prefork_pool *pfp)
354 int c;
355 int i;
357 c = 0;
358 for (i = 0; i < pfp->pool_size; i++) {
359 if (pfp->pool[i].status == PF_WORKER_NONE ||
360 pfp->pool[i].status == PF_WORKER_EXITING) {
361 continue;
364 if (pfp->pool[i].num_clients < 0) {
365 continue;
368 c += pfp->pool[i].allowed_clients - pfp->pool[i].num_clients;
371 return c;
374 void prefork_increase_allowed_clients(struct prefork_pool *pfp, int max)
376 int i;
378 for (i = 0; i < pfp->pool_size; i++) {
379 if (pfp->pool[i].status == PF_WORKER_NONE ||
380 pfp->pool[i].status == PF_WORKER_EXITING) {
381 continue;
384 if (pfp->pool[i].num_clients < 0) {
385 continue;
388 if (pfp->pool[i].allowed_clients < max) {
389 pfp->pool[i].allowed_clients++;
394 void prefork_decrease_allowed_clients(struct prefork_pool *pfp)
396 int i;
398 for (i = 0; i < pfp->pool_size; i++) {
399 if (pfp->pool[i].status == PF_WORKER_NONE ||
400 pfp->pool[i].status == PF_WORKER_EXITING) {
401 continue;
404 if (pfp->pool[i].num_clients < 0) {
405 continue;
408 if (pfp->pool[i].allowed_clients > 1) {
409 pfp->pool[i].allowed_clients--;
414 void prefork_reset_allowed_clients(struct prefork_pool *pfp)
416 int i;
418 for (i = 0; i < pfp->pool_size; i++) {
419 pfp->pool[i].allowed_clients = 1;
423 void prefork_send_signal_to_all(struct prefork_pool *pfp, int signal_num)
425 int i;
427 for (i = 0; i < pfp->pool_size; i++) {
428 if (pfp->pool[i].status == PF_WORKER_NONE) {
429 continue;
432 kill(pfp->pool[i].pid, signal_num);
436 void prefork_warn_active_children(struct messaging_context *msg_ctx,
437 struct prefork_pool *pfp)
439 const DATA_BLOB ping = data_blob_null;
440 int i;
442 for (i = 0; i < pfp->pool_size; i++) {
443 if (pfp->pool[i].status == PF_WORKER_NONE) {
444 continue;
447 messaging_send(msg_ctx,
448 pid_to_procid(pfp->pool[i].pid),
449 MSG_PREFORK_PARENT_EVENT, &ping);
453 static void prefork_sigchld_handler(struct tevent_context *ev_ctx,
454 struct tevent_signal *se,
455 int signum, int count,
456 void *siginfo, void *pvt)
458 struct prefork_pool *pfp;
460 pfp = talloc_get_type_abort(pvt, struct prefork_pool);
462 /* run the cleanup function to make sure all dead children are
463 * properly and timely retired. */
464 prefork_cleanup_loop(pfp);
466 if (pfp->sigchld_fn) {
467 pfp->sigchld_fn(ev_ctx, pfp, pfp->sigchld_data);
471 static bool prefork_setup_sigchld_handler(struct tevent_context *ev_ctx,
472 struct prefork_pool *pfp)
474 struct tevent_signal *se;
476 se = tevent_add_signal(ev_ctx, pfp, SIGCHLD, 0,
477 prefork_sigchld_handler, pfp);
478 if (!se) {
479 DEBUG(0, ("Failed to setup SIGCHLD handler!\n"));
480 return false;
483 return true;
486 void prefork_set_sigchld_callback(struct prefork_pool *pfp,
487 prefork_sigchld_fn_t *sigchld_fn,
488 void *private_data)
490 pfp->sigchld_fn = sigchld_fn;
491 pfp->sigchld_data = private_data;
494 /* ==== Functions used by children ==== */
496 struct pf_listen_state {
497 struct tevent_context *ev;
498 struct pf_worker_data *pf;
500 int listen_fd_size;
501 int *listen_fds;
503 int accept_fd;
505 struct tsocket_address *srv_addr;
506 struct tsocket_address *cli_addr;
508 int error;
511 struct pf_listen_ctx {
512 TALLOC_CTX *fde_ctx;
513 struct tevent_req *req;
514 int listen_fd;
517 static void prefork_listen_accept_handler(struct tevent_context *ev,
518 struct tevent_fd *fde,
519 uint16_t flags, void *pvt);
521 struct tevent_req *prefork_listen_send(TALLOC_CTX *mem_ctx,
522 struct tevent_context *ev,
523 struct pf_worker_data *pf,
524 int listen_fd_size,
525 int *listen_fds)
527 struct tevent_req *req;
528 struct pf_listen_state *state;
529 struct pf_listen_ctx *ctx;
530 struct tevent_fd *fde;
531 TALLOC_CTX *fde_ctx;
532 int i;
534 req = tevent_req_create(mem_ctx, &state, struct pf_listen_state);
535 if (!req) {
536 return NULL;
539 state->ev = ev;
540 state->pf = pf;
541 state->listen_fd_size = listen_fd_size;
542 state->listen_fds = listen_fds;
543 state->accept_fd = -1;
544 state->error = 0;
546 fde_ctx = talloc_new(state);
547 if (tevent_req_nomem(fde_ctx, req)) {
548 return tevent_req_post(req, ev);
551 /* race on accept */
552 for (i = 0; i < state->listen_fd_size; i++) {
553 ctx = talloc(fde_ctx, struct pf_listen_ctx);
554 if (tevent_req_nomem(ctx, req)) {
555 return tevent_req_post(req, ev);
557 ctx->fde_ctx = fde_ctx;
558 ctx->req = req;
559 ctx->listen_fd = state->listen_fds[i];
561 fde = tevent_add_fd(state->ev, fde_ctx,
562 ctx->listen_fd, TEVENT_FD_READ,
563 prefork_listen_accept_handler, ctx);
564 if (tevent_req_nomem(fde, req)) {
565 return tevent_req_post(req, ev);
569 pf->status = PF_WORKER_ACCEPTING;
571 return req;
574 static void prefork_listen_accept_handler(struct tevent_context *ev,
575 struct tevent_fd *fde,
576 uint16_t flags, void *pvt)
578 struct pf_listen_state *state;
579 struct tevent_req *req;
580 struct pf_listen_ctx *ctx;
581 struct sockaddr_storage addr;
582 socklen_t addrlen;
583 int soerr = 0;
584 socklen_t solen = sizeof(soerr);
585 int sd = -1;
586 int ret;
588 ctx = talloc_get_type_abort(pvt, struct pf_listen_ctx);
589 req = ctx->req;
590 state = tevent_req_data(ctx->req, struct pf_listen_state);
592 if ((state->pf->cmds == PF_SRV_MSG_EXIT) &&
593 (state->pf->num_clients <= 0)) {
594 /* We have been asked to exit, so drop here and the next
595 * child will pick it up */
596 state->pf->status = PF_WORKER_EXITING;
597 state->error = EINTR;
598 goto done;
601 /* before proceeding check that the listening fd is ok */
602 ret = getsockopt(ctx->listen_fd, SOL_SOCKET, SO_ERROR, &soerr, &solen);
603 if (ret == -1) {
604 /* this is a fatal error, we cannot continue listening */
605 state->error = EBADF;
606 goto done;
608 if (soerr != 0) {
609 /* this is a fatal error, we cannot continue listening */
610 state->error = soerr;
611 goto done;
614 ZERO_STRUCT(addr);
615 addrlen = sizeof(addr);
616 sd = accept(ctx->listen_fd, (struct sockaddr *)&addr, &addrlen);
617 if (sd == -1) {
618 state->error = errno;
619 DEBUG(6, ("Accept failed! (%d, %s)\n",
620 state->error, strerror(state->error)));
621 goto done;
624 state->accept_fd = sd;
626 ret = tsocket_address_bsd_from_sockaddr(state,
627 (struct sockaddr *)(void *)&addr,
628 addrlen, &state->cli_addr);
629 if (ret < 0) {
630 state->error = errno;
631 goto done;
634 ZERO_STRUCT(addr);
635 addrlen = sizeof(addr);
636 ret = getsockname(sd, (struct sockaddr *)(void *)&addr, &addrlen);
637 if (ret < 0) {
638 state->error = errno;
639 goto done;
642 ret = tsocket_address_bsd_from_sockaddr(state,
643 (struct sockaddr *)(void *)&addr,
644 addrlen, &state->srv_addr);
645 if (ret < 0) {
646 state->error = errno;
647 goto done;
650 done:
651 /* do not track the listen fds anymore */
652 talloc_free(ctx->fde_ctx);
653 tevent_req_done(req);
656 int prefork_listen_recv(struct tevent_req *req,
657 TALLOC_CTX *mem_ctx, int *fd,
658 struct tsocket_address **srv_addr,
659 struct tsocket_address **cli_addr)
661 struct pf_listen_state *state;
662 int ret = 0;
664 state = tevent_req_data(req, struct pf_listen_state);
666 if (state->error) {
667 ret = state->error;
668 } else {
669 if (!tevent_req_is_unix_error(req, &ret)) {
670 ret = 0;
674 if (ret) {
675 if (state->accept_fd != -1) {
676 close(state->accept_fd);
678 } else {
679 *fd = state->accept_fd;
680 *srv_addr = talloc_move(mem_ctx, &state->srv_addr);
681 *cli_addr = talloc_move(mem_ctx, &state->cli_addr);
682 state->pf->num_clients++;
684 if (state->pf->status == PF_WORKER_ACCEPTING) {
685 state->pf->status = PF_WORKER_ALIVE;
688 tevent_req_received(req);
689 return ret;