2 Unix SMB/CIFS implementation.
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/>.
24 #include "system/time.h"
25 #include "system/shmem.h"
26 #include "system/filesys.h"
27 #include "server_prefork.h"
28 #include "../lib/util/util.h"
29 #include "../lib/util/tevent_unix.h"
36 prefork_main_fn_t
*main_fn
;
40 struct pf_worker_data
*pool
;
44 prefork_sigchld_fn_t
*sigchld_fn
;
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
);
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
;
67 time_t now
= time(NULL
);
73 pfp
= talloc_zero(mem_ctx
, struct prefork_pool
);
75 DEBUG(1, ("Out of memory!\n"));
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"));
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
= anonymous_shared_allocate(data_size
);
96 if (pfp
->pool
== NULL
) {
97 DEBUG(1, ("Failed to mmap memory for prefork pool!\n"));
101 talloc_set_destructor(pfp
, prefork_pool_destructor
);
103 for (i
= 0; i
< min_children
; i
++) {
105 pfp
->pool
[i
].allowed_clients
= 1;
106 pfp
->pool
[i
].started
= now
;
111 DEBUG(1, ("Failed to prefork child n. %d !\n", i
));
114 case 0: /* THE CHILD */
116 pfp
->pool
[i
].status
= PF_WORKER_ALIVE
;
117 ret
= pfp
->main_fn(ev_ctx
, msg_ctx
,
118 &pfp
->pool
[i
], i
+ 1,
124 default: /* THE PARENT */
125 pfp
->pool
[i
].pid
= pid
;
130 ok
= prefork_setup_sigchld_handler(ev_ctx
, pfp
);
132 DEBUG(1, ("Failed to setup SIGCHLD Handler!\n"));
141 /* Provide the new max children number in new_max
142 * (must be larger than current max).
143 * Returns: 0 if all fine
144 * ENOSPC if mremap fails to expand
145 * EINVAL if new_max is invalid
147 int prefork_expand_pool(struct prefork_pool
*pfp
, int new_max
)
149 struct prefork_pool
*pool
;
154 if (new_max
<= pfp
->pool_size
) {
158 old_size
= sizeof(struct pf_worker_data
) * pfp
->pool_size
;
159 new_size
= sizeof(struct pf_worker_data
) * new_max
;
161 pool
= anonymous_shared_resize(&pfp
->pool
, new_size
, false);
164 DEBUG(3, ("Failed to mremap memory (%d: %s)!\n",
165 ret
, strerror(ret
)));
169 memset(&pool
[pfp
->pool_size
], 0, new_size
- old_size
);
171 pfp
->pool_size
= new_max
;
176 int prefork_add_children(struct tevent_context
*ev_ctx
,
177 struct messaging_context
*msg_ctx
,
178 struct prefork_pool
*pfp
,
182 time_t now
= time(NULL
);
186 for (i
= 0, j
= 0; i
< pfp
->pool_size
&& j
< num_children
; i
++) {
188 if (pfp
->pool
[i
].status
!= PF_WORKER_NONE
) {
192 pfp
->pool
[i
].allowed_clients
= 1;
193 pfp
->pool
[i
].started
= now
;
198 DEBUG(1, ("Failed to prefork child n. %d !\n", j
));
201 case 0: /* THE CHILD */
203 pfp
->pool
[i
].status
= PF_WORKER_ALIVE
;
204 ret
= pfp
->main_fn(ev_ctx
, msg_ctx
,
205 &pfp
->pool
[i
], i
+ 1,
210 pfp
->pool
[i
].status
= PF_WORKER_EXITING
;
213 default: /* THE PARENT */
214 pfp
->pool
[i
].pid
= pid
;
220 DEBUG(5, ("Added %d children!\n", j
));
225 struct prefork_oldest
{
230 /* sort in inverse order */
231 static int prefork_sort_oldest(const void *ap
, const void *bp
)
233 const struct prefork_oldest
*a
= (const struct prefork_oldest
*)ap
;
234 const struct prefork_oldest
*b
= (const struct prefork_oldest
*)bp
;
236 if (a
->started
== b
->started
) {
239 if (a
->started
< b
->started
) {
245 int prefork_retire_children(struct messaging_context
*msg_ctx
,
246 struct prefork_pool
*pfp
,
247 int num_children
, time_t age_limit
)
249 const DATA_BLOB ping
= data_blob_null
;
250 time_t now
= time(NULL
);
251 struct prefork_oldest
*oldest
;
254 oldest
= talloc_array(pfp
, struct prefork_oldest
, pfp
->pool_size
);
259 for (i
= 0; i
< pfp
->pool_size
; i
++) {
261 if (pfp
->pool
[i
].status
== PF_WORKER_ALIVE
||
262 pfp
->pool
[i
].status
== PF_WORKER_ACCEPTING
) {
263 oldest
[i
].started
= pfp
->pool
[i
].started
;
265 oldest
[i
].started
= now
;
269 qsort(oldest
, pfp
->pool_size
,
270 sizeof(struct prefork_oldest
),
271 prefork_sort_oldest
);
273 for (i
= 0, j
= 0; i
< pfp
->pool_size
&& j
< num_children
; i
++) {
274 if (((pfp
->pool
[i
].status
== PF_WORKER_ALIVE
) &&
275 (pfp
->pool
[i
].num_clients
< 1)) &&
276 (pfp
->pool
[i
].started
<= age_limit
)) {
277 /* tell the child it's time to give up */
278 DEBUG(5, ("Retiring pid %d!\n", pfp
->pool
[i
].pid
));
279 pfp
->pool
[i
].cmds
= PF_SRV_MSG_EXIT
;
280 messaging_send(msg_ctx
,
281 pid_to_procid(pfp
->pool
[i
].pid
),
282 MSG_PREFORK_PARENT_EVENT
, &ping
);
290 int prefork_count_children(struct prefork_pool
*pfp
, int *active
)
296 for (i
= 0; i
< pfp
->pool_size
; i
++) {
297 if (pfp
->pool
[i
].status
== PF_WORKER_NONE
) {
303 if ((pfp
->pool
[i
].status
== PF_WORKER_EXITING
) ||
304 (pfp
->pool
[i
].num_clients
<= 0)) {
317 static void prefork_cleanup_loop(struct prefork_pool
*pfp
)
323 /* TODO: should we use a process group id wait instead of looping ? */
324 for (i
= 0; i
< pfp
->pool_size
; i
++) {
325 if (pfp
->pool
[i
].status
== PF_WORKER_NONE
||
326 pfp
->pool
[i
].pid
== 0) {
330 pid
= sys_waitpid(pfp
->pool
[i
].pid
, &status
, WNOHANG
);
333 if (pfp
->pool
[i
].status
!= PF_WORKER_EXITING
) {
334 DEBUG(3, ("Child (%d) terminated abnormally:"
335 " %d\n", (int)pid
, status
));
337 DEBUG(10, ("Child (%d) terminated with status:"
338 " %d\n", (int)pid
, status
));
342 * this makes status = PF_WORK_NONE */
343 memset(&pfp
->pool
[i
], 0,
344 sizeof(struct pf_worker_data
));
350 int prefork_count_allowed_connections(struct prefork_pool
*pfp
)
356 for (i
= 0; i
< pfp
->pool_size
; i
++) {
357 if (pfp
->pool
[i
].status
== PF_WORKER_NONE
||
358 pfp
->pool
[i
].status
== PF_WORKER_EXITING
) {
362 if (pfp
->pool
[i
].num_clients
< 0) {
366 c
+= pfp
->pool
[i
].allowed_clients
- pfp
->pool
[i
].num_clients
;
372 void prefork_increase_allowed_clients(struct prefork_pool
*pfp
, int max
)
376 for (i
= 0; i
< pfp
->pool_size
; i
++) {
377 if (pfp
->pool
[i
].status
== PF_WORKER_NONE
||
378 pfp
->pool
[i
].status
== PF_WORKER_EXITING
) {
382 if (pfp
->pool
[i
].num_clients
< 0) {
386 if (pfp
->pool
[i
].allowed_clients
< max
) {
387 pfp
->pool
[i
].allowed_clients
++;
392 void prefork_decrease_allowed_clients(struct prefork_pool
*pfp
)
396 for (i
= 0; i
< pfp
->pool_size
; i
++) {
397 if (pfp
->pool
[i
].status
== PF_WORKER_NONE
||
398 pfp
->pool
[i
].status
== PF_WORKER_EXITING
) {
402 if (pfp
->pool
[i
].num_clients
< 0) {
406 if (pfp
->pool
[i
].allowed_clients
> 1) {
407 pfp
->pool
[i
].allowed_clients
--;
412 void prefork_reset_allowed_clients(struct prefork_pool
*pfp
)
416 for (i
= 0; i
< pfp
->pool_size
; i
++) {
417 pfp
->pool
[i
].allowed_clients
= 1;
421 void prefork_send_signal_to_all(struct prefork_pool
*pfp
, int signal_num
)
425 for (i
= 0; i
< pfp
->pool_size
; i
++) {
426 if (pfp
->pool
[i
].status
== PF_WORKER_NONE
) {
430 kill(pfp
->pool
[i
].pid
, signal_num
);
434 void prefork_warn_active_children(struct messaging_context
*msg_ctx
,
435 struct prefork_pool
*pfp
)
437 const DATA_BLOB ping
= data_blob_null
;
440 for (i
= 0; i
< pfp
->pool_size
; i
++) {
441 if (pfp
->pool
[i
].status
== PF_WORKER_NONE
) {
445 messaging_send(msg_ctx
,
446 pid_to_procid(pfp
->pool
[i
].pid
),
447 MSG_PREFORK_PARENT_EVENT
, &ping
);
451 static void prefork_sigchld_handler(struct tevent_context
*ev_ctx
,
452 struct tevent_signal
*se
,
453 int signum
, int count
,
454 void *siginfo
, void *pvt
)
456 struct prefork_pool
*pfp
;
458 pfp
= talloc_get_type_abort(pvt
, struct prefork_pool
);
460 /* run the cleanup function to make sure all dead children are
461 * properly and timely retired. */
462 prefork_cleanup_loop(pfp
);
464 if (pfp
->sigchld_fn
) {
465 pfp
->sigchld_fn(ev_ctx
, pfp
, pfp
->sigchld_data
);
469 static bool prefork_setup_sigchld_handler(struct tevent_context
*ev_ctx
,
470 struct prefork_pool
*pfp
)
472 struct tevent_signal
*se
;
474 se
= tevent_add_signal(ev_ctx
, pfp
, SIGCHLD
, 0,
475 prefork_sigchld_handler
, pfp
);
477 DEBUG(0, ("Failed to setup SIGCHLD handler!\n"));
484 void prefork_set_sigchld_callback(struct prefork_pool
*pfp
,
485 prefork_sigchld_fn_t
*sigchld_fn
,
488 pfp
->sigchld_fn
= sigchld_fn
;
489 pfp
->sigchld_data
= private_data
;
492 /* ==== Functions used by children ==== */
494 struct pf_listen_state
{
495 struct tevent_context
*ev
;
496 struct pf_worker_data
*pf
;
503 struct tsocket_address
*srv_addr
;
504 struct tsocket_address
*cli_addr
;
509 struct pf_listen_ctx
{
511 struct tevent_req
*req
;
515 static void prefork_listen_accept_handler(struct tevent_context
*ev
,
516 struct tevent_fd
*fde
,
517 uint16_t flags
, void *pvt
);
519 struct tevent_req
*prefork_listen_send(TALLOC_CTX
*mem_ctx
,
520 struct tevent_context
*ev
,
521 struct pf_worker_data
*pf
,
525 struct tevent_req
*req
;
526 struct pf_listen_state
*state
;
527 struct pf_listen_ctx
*ctx
;
528 struct tevent_fd
*fde
;
532 req
= tevent_req_create(mem_ctx
, &state
, struct pf_listen_state
);
539 state
->listen_fd_size
= listen_fd_size
;
540 state
->listen_fds
= listen_fds
;
541 state
->accept_fd
= -1;
544 fde_ctx
= talloc_new(state
);
545 if (tevent_req_nomem(fde_ctx
, req
)) {
546 return tevent_req_post(req
, ev
);
550 for (i
= 0; i
< state
->listen_fd_size
; i
++) {
551 ctx
= talloc(fde_ctx
, struct pf_listen_ctx
);
552 if (tevent_req_nomem(ctx
, req
)) {
553 return tevent_req_post(req
, ev
);
555 ctx
->fde_ctx
= fde_ctx
;
557 ctx
->listen_fd
= state
->listen_fds
[i
];
559 fde
= tevent_add_fd(state
->ev
, fde_ctx
,
560 ctx
->listen_fd
, TEVENT_FD_READ
,
561 prefork_listen_accept_handler
, ctx
);
562 if (tevent_req_nomem(fde
, req
)) {
563 return tevent_req_post(req
, ev
);
567 pf
->status
= PF_WORKER_ACCEPTING
;
572 static void prefork_listen_accept_handler(struct tevent_context
*ev
,
573 struct tevent_fd
*fde
,
574 uint16_t flags
, void *pvt
)
576 struct pf_listen_state
*state
;
577 struct tevent_req
*req
;
578 struct pf_listen_ctx
*ctx
;
579 struct sockaddr_storage addr
;
582 socklen_t solen
= sizeof(soerr
);
586 ctx
= talloc_get_type_abort(pvt
, struct pf_listen_ctx
);
588 state
= tevent_req_data(ctx
->req
, struct pf_listen_state
);
590 if ((state
->pf
->cmds
== PF_SRV_MSG_EXIT
) &&
591 (state
->pf
->num_clients
<= 0)) {
592 /* We have been asked to exit, so drop here and the next
593 * child will pick it up */
594 state
->pf
->status
= PF_WORKER_EXITING
;
595 state
->error
= EINTR
;
599 /* before proceeding check that the listening fd is ok */
600 ret
= getsockopt(ctx
->listen_fd
, SOL_SOCKET
, SO_ERROR
, &soerr
, &solen
);
602 /* this is a fatal error, we cannot continue listening */
603 state
->error
= EBADF
;
607 /* this is a fatal error, we cannot continue listening */
608 state
->error
= soerr
;
613 addrlen
= sizeof(addr
);
614 sd
= accept(ctx
->listen_fd
, (struct sockaddr
*)&addr
, &addrlen
);
616 state
->error
= errno
;
617 DEBUG(6, ("Accept failed! (%d, %s)\n",
618 state
->error
, strerror(state
->error
)));
622 state
->accept_fd
= sd
;
624 ret
= tsocket_address_bsd_from_sockaddr(state
,
625 (struct sockaddr
*)(void *)&addr
,
626 addrlen
, &state
->cli_addr
);
628 state
->error
= errno
;
633 addrlen
= sizeof(addr
);
634 ret
= getsockname(sd
, (struct sockaddr
*)(void *)&addr
, &addrlen
);
636 state
->error
= errno
;
640 ret
= tsocket_address_bsd_from_sockaddr(state
,
641 (struct sockaddr
*)(void *)&addr
,
642 addrlen
, &state
->srv_addr
);
644 state
->error
= errno
;
649 /* do not track the listen fds anymore */
650 talloc_free(ctx
->fde_ctx
);
651 tevent_req_done(req
);
654 int prefork_listen_recv(struct tevent_req
*req
,
655 TALLOC_CTX
*mem_ctx
, int *fd
,
656 struct tsocket_address
**srv_addr
,
657 struct tsocket_address
**cli_addr
)
659 struct pf_listen_state
*state
;
662 state
= tevent_req_data(req
, struct pf_listen_state
);
667 tevent_req_is_unix_error(req
, &ret
);
671 if (state
->accept_fd
!= -1) {
672 close(state
->accept_fd
);
675 *fd
= state
->accept_fd
;
676 *srv_addr
= talloc_move(mem_ctx
, &state
->srv_addr
);
677 *cli_addr
= talloc_move(mem_ctx
, &state
->cli_addr
);
678 state
->pf
->num_clients
++;
680 if (state
->pf
->status
== PF_WORKER_ACCEPTING
) {
681 state
->pf
->status
= PF_WORKER_ALIVE
;
684 tevent_req_received(req
);