2 * Unix SMB/CIFS implementation.
6 * Copyright (c) 2014 Ralph Boehme <rb@sernet.de>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, see <http://www.gnu.org/licenses/>.
26 #include "lib/id_cache.h"
28 #include "../lib/tsocket/tsocket.h"
29 #include "lib/server_prefork.h"
30 #include "lib/server_prefork_util.h"
31 #include "librpc/rpc/dcerpc_ep.h"
33 #include "rpc_server/rpc_server.h"
34 #include "rpc_server/rpc_ep_register.h"
35 #include "rpc_server/rpc_sock_helper.h"
36 #include "rpc_server/rpc_modules.h"
38 #include "librpc/gen_ndr/srv_mdssvc.h"
39 #include "rpc_server/mdssvc/srv_mdssvc_nt.h"
40 #include "rpc_server/mdssd.h"
43 #define DBGC_CLASS DBGC_RPC_SRV
45 #define DAEMON_NAME "mdssd"
46 #define MDSSD_MAX_SOCKETS 64
48 static struct server_id parent_id
;
49 static struct prefork_pool
*mdssd_pool
= NULL
;
50 static int mdssd_child_id
= 0;
52 static struct pf_daemon_config default_pf_mdssd_cfg
= {
53 .prefork_status
= PFH_INIT
,
57 .max_allowed_clients
= 1000,
58 .child_min_life
= 60 /* 1 minute minimum life time */
60 static struct pf_daemon_config pf_mdssd_cfg
= { 0 };
62 void start_mdssd(struct tevent_context
*ev_ctx
,
63 struct messaging_context
*msg_ctx
);
65 static void mdssd_smb_conf_updated(struct messaging_context
*msg
,
68 struct server_id server_id
,
71 struct tevent_context
*ev_ctx
;
73 DEBUG(10, ("Got message saying smb.conf was updated. Reloading.\n"));
74 ev_ctx
= talloc_get_type_abort(private_data
, struct tevent_context
);
76 change_to_root_user();
77 lp_load_global(get_dyn_CONFIGFILE());
80 if (mdssd_child_id
== 0) {
81 pfh_daemon_config(DAEMON_NAME
,
83 &default_pf_mdssd_cfg
);
84 pfh_manage_pool(ev_ctx
, msg
, &pf_mdssd_cfg
, mdssd_pool
);
88 static void mdssd_sig_term_handler(struct tevent_context
*ev
,
89 struct tevent_signal
*se
,
95 shutdown_rpc_module("mdssvc");
97 DEBUG(0, ("termination signal\n"));
101 static void mdssd_setup_sig_term_handler(struct tevent_context
*ev_ctx
)
103 struct tevent_signal
*se
;
105 se
= tevent_add_signal(ev_ctx
,
108 mdssd_sig_term_handler
,
111 DEBUG(0, ("failed to setup SIGTERM handler\n"));
116 static void mdssd_sig_hup_handler(struct tevent_context
*ev
,
117 struct tevent_signal
*se
,
124 change_to_root_user();
125 lp_load_global(get_dyn_CONFIGFILE());
128 pfh_daemon_config(DAEMON_NAME
,
130 &default_pf_mdssd_cfg
);
132 /* relay to all children */
133 prefork_send_signal_to_all(mdssd_pool
, SIGHUP
);
136 static void mdssd_setup_sig_hup_handler(struct tevent_context
*ev_ctx
)
138 struct tevent_signal
*se
;
140 se
= tevent_add_signal(ev_ctx
,
143 mdssd_sig_hup_handler
,
146 DEBUG(0, ("failed to setup SIGHUP handler\n"));
151 /**********************************************************
153 **********************************************************/
155 static void mdssd_chld_sig_hup_handler(struct tevent_context
*ev
,
156 struct tevent_signal
*se
,
162 change_to_root_user();
166 static bool mdssd_setup_chld_hup_handler(struct tevent_context
*ev_ctx
)
168 struct tevent_signal
*se
;
170 se
= tevent_add_signal(ev_ctx
,
173 mdssd_chld_sig_hup_handler
,
176 DEBUG(1, ("failed to setup SIGHUP handler"));
183 static void parent_ping(struct messaging_context
*msg_ctx
,
186 struct server_id server_id
,
190 * The fact we received this message is enough to let make the
191 * event loop if it was idle. mdssd_children_main will cycle
192 * through mdssd_next_client at least once. That function will
193 * take whatever action is necessary
195 DEBUG(10, ("Got message that the parent changed status.\n"));
199 static bool mdssd_child_init(struct tevent_context
*ev_ctx
,
201 struct pf_worker_data
*pf
)
204 struct messaging_context
*msg_ctx
= global_messaging_context();
207 status
= reinit_after_fork(msg_ctx
, ev_ctx
,
208 true, "mdssd-child");
209 if (!NT_STATUS_IS_OK(status
)) {
210 DEBUG(0,("reinit_after_fork() failed\n"));
211 smb_panic("reinit_after_fork() failed");
214 mdssd_child_id
= child_id
;
217 ok
= mdssd_setup_chld_hup_handler(ev_ctx
);
222 messaging_register(msg_ctx
, ev_ctx
,
223 MSG_SMB_CONF_UPDATED
, mdssd_smb_conf_updated
);
224 messaging_register(msg_ctx
, ev_ctx
,
225 MSG_PREFORK_PARENT_EVENT
, parent_ping
);
227 ok
= init_rpc_module("mdssvc", NULL
);
229 DBG_ERR("Failed to de-intialize RPC\n");
236 struct mdssd_children_data
{
237 struct tevent_context
*ev_ctx
;
238 struct messaging_context
*msg_ctx
;
239 struct pf_worker_data
*pf
;
244 static void mdssd_next_client(void *pvt
);
246 static int mdssd_children_main(struct tevent_context
*ev_ctx
,
247 struct messaging_context
*msg_ctx
,
248 struct pf_worker_data
*pf
,
254 struct mdssd_children_data
*data
;
258 ok
= mdssd_child_init(ev_ctx
, child_id
, pf
);
263 data
= talloc(ev_ctx
, struct mdssd_children_data
);
268 data
->ev_ctx
= ev_ctx
;
269 data
->msg_ctx
= msg_ctx
;
270 data
->listen_fd_size
= listen_fd_size
;
271 data
->listen_fds
= listen_fds
;
273 /* loop until it is time to exit */
274 while (pf
->status
!= PF_WORKER_EXITING
) {
275 /* try to see if it is time to schedule the next client */
276 mdssd_next_client(data
);
278 ret
= tevent_loop_once(ev_ctx
);
280 DEBUG(0, ("tevent_loop_once() exited with %d: %s\n",
281 ret
, strerror(errno
)));
282 pf
->status
= PF_WORKER_EXITING
;
289 static void mdssd_client_terminated(void *pvt
)
291 struct mdssd_children_data
*data
;
293 data
= talloc_get_type_abort(pvt
, struct mdssd_children_data
);
295 pfh_client_terminated(data
->pf
);
297 mdssd_next_client(pvt
);
300 struct mdssd_new_client
{
301 struct mdssd_children_data
*data
;
304 static void mdssd_handle_client(struct tevent_req
*req
);
306 static void mdssd_next_client(void *pvt
)
308 struct tevent_req
*req
;
309 struct mdssd_children_data
*data
;
310 struct mdssd_new_client
*next
;
312 data
= talloc_get_type_abort(pvt
, struct mdssd_children_data
);
314 if (!pfh_child_allowed_to_accept(data
->pf
)) {
315 /* nothing to do for now we are already listening
316 * or we are not allowed to listen further */
320 next
= talloc_zero(data
, struct mdssd_new_client
);
322 DEBUG(1, ("Out of memory!?\n"));
327 req
= prefork_listen_send(next
,
330 data
->listen_fd_size
,
333 DEBUG(1, ("Failed to make listening request!?\n"));
337 tevent_req_set_callback(req
, mdssd_handle_client
, next
);
340 static void mdssd_handle_client(struct tevent_req
*req
)
342 struct mdssd_children_data
*data
;
343 struct mdssd_new_client
*client
;
344 const DATA_BLOB ping
= data_blob_null
;
348 struct tsocket_address
*srv_addr
;
349 struct tsocket_address
*cli_addr
;
351 client
= tevent_req_callback_data(req
, struct mdssd_new_client
);
354 tmp_ctx
= talloc_stackframe();
355 if (tmp_ctx
== NULL
) {
356 DEBUG(1, ("Failed to allocate stackframe!\n"));
360 rc
= prefork_listen_recv(req
,
366 /* this will free the request too */
370 DEBUG(6, ("No client connection was available after all!\n"));
374 /* Warn parent that our status changed */
375 messaging_send(data
->msg_ctx
, parent_id
,
376 MSG_PREFORK_CHILD_EVENT
, &ping
);
378 DEBUG(2, ("mdssd preforked child %d got client connection!\n",
379 (int)(data
->pf
->pid
)));
381 if (tsocket_address_is_inet(srv_addr
, "ip")) {
382 DEBUG(3, ("Got a tcpip client connection from %s on inteface %s\n",
383 tsocket_address_string(cli_addr
, tmp_ctx
),
384 tsocket_address_string(srv_addr
, tmp_ctx
)));
386 dcerpc_ncacn_accept(data
->ev_ctx
,
394 } else if (tsocket_address_is_unix(srv_addr
)) {
398 p
= tsocket_address_unix_path(srv_addr
, tmp_ctx
);
400 talloc_free(tmp_ctx
);
411 if (strstr(p
, "/np/")) {
412 named_pipe_accept_function(data
->ev_ctx
,
416 mdssd_client_terminated
,
419 dcerpc_ncacn_accept(data
->ev_ctx
,
429 DEBUG(0, ("ERROR: Unsupported socket!\n"));
433 talloc_free(tmp_ctx
);
440 static void child_ping(struct messaging_context
*msg_ctx
,
443 struct server_id server_id
,
446 struct tevent_context
*ev_ctx
;
448 ev_ctx
= talloc_get_type_abort(private_data
, struct tevent_context
);
450 DEBUG(10, ("Got message that a child changed status.\n"));
451 pfh_manage_pool(ev_ctx
, msg_ctx
, &pf_mdssd_cfg
, mdssd_pool
);
454 static bool mdssd_schedule_check(struct tevent_context
*ev_ctx
,
455 struct messaging_context
*msg_ctx
,
456 struct timeval current_time
);
458 static void mdssd_check_children(struct tevent_context
*ev_ctx
,
459 struct tevent_timer
*te
,
460 struct timeval current_time
,
463 static void mdssd_sigchld_handler(struct tevent_context
*ev_ctx
,
464 struct prefork_pool
*pfp
,
467 struct messaging_context
*msg_ctx
;
469 msg_ctx
= talloc_get_type_abort(pvt
, struct messaging_context
);
471 /* run pool management so we can fork/retire or increase
472 * the allowed connections per child based on load */
473 pfh_manage_pool(ev_ctx
, msg_ctx
, &pf_mdssd_cfg
, mdssd_pool
);
476 static bool mdssd_setup_children_monitor(struct tevent_context
*ev_ctx
,
477 struct messaging_context
*msg_ctx
)
481 /* add our oun sigchld callback */
482 prefork_set_sigchld_callback(mdssd_pool
, mdssd_sigchld_handler
, msg_ctx
);
484 ok
= mdssd_schedule_check(ev_ctx
, msg_ctx
, tevent_timeval_current());
489 static bool mdssd_schedule_check(struct tevent_context
*ev_ctx
,
490 struct messaging_context
*msg_ctx
,
491 struct timeval current_time
)
493 struct tevent_timer
*te
;
494 struct timeval next_event
;
496 /* check situation again in 10 seconds */
497 next_event
= tevent_timeval_current_ofs(10, 0);
499 /* TODO: check when the socket becomes readable, so that children
500 * are checked only when there is some activity ? */
501 te
= tevent_add_timer(ev_ctx
, mdssd_pool
, next_event
,
502 mdssd_check_children
, msg_ctx
);
504 DEBUG(2, ("Failed to set up children monitoring!\n"));
511 static void mdssd_check_children(struct tevent_context
*ev_ctx
,
512 struct tevent_timer
*te
,
513 struct timeval current_time
,
516 struct messaging_context
*msg_ctx
;
518 msg_ctx
= talloc_get_type_abort(pvt
, struct messaging_context
);
520 pfh_manage_pool(ev_ctx
, msg_ctx
, &pf_mdssd_cfg
, mdssd_pool
);
522 mdssd_schedule_check(ev_ctx
, msg_ctx
, current_time
);
529 static bool mdssd_create_sockets(struct tevent_context
*ev_ctx
,
530 struct messaging_context
*msg_ctx
,
534 struct dcerpc_binding_vector
*v
, *v_orig
;
541 tmp_ctx
= talloc_stackframe();
542 if (tmp_ctx
== NULL
) {
546 status
= dcerpc_binding_vector_new(tmp_ctx
, &v_orig
);
547 if (!NT_STATUS_IS_OK(status
)) {
552 fd
= dcesrv_create_ncacn_np_socket("mdssvc");
557 rc
= listen(fd
, pf_mdssd_cfg
.max_allowed_clients
);
561 listen_fd
[*listen_fd_size
] = fd
;
564 status
= dcesrv_create_ncalrpc_socket("mdssvc", &fd
);
565 if (!NT_STATUS_IS_OK(status
)) {
569 rc
= listen(fd
, pf_mdssd_cfg
.max_allowed_clients
);
573 listen_fd
[*listen_fd_size
] = fd
;
577 v
= dcerpc_binding_vector_dup(tmp_ctx
, v_orig
);
582 status
= dcerpc_binding_vector_replace_iface(&ndr_table_mdssvc
, v
);
583 if (!NT_STATUS_IS_OK(status
)) {
587 status
= dcerpc_binding_vector_add_np_default(&ndr_table_mdssvc
, v
);
588 if (!NT_STATUS_IS_OK(status
)) {
592 status
= dcerpc_binding_vector_add_unix(&ndr_table_mdssvc
, v
, "mdssvc");
593 if (!NT_STATUS_IS_OK(status
)) {
602 talloc_free(tmp_ctx
);
606 void start_mdssd(struct tevent_context
*ev_ctx
,
607 struct messaging_context
*msg_ctx
)
610 int listen_fd
[MDSSD_MAX_SOCKETS
];
611 int listen_fd_size
= 0;
616 DEBUG(1, ("Forking Metadata Service Daemon\n"));
619 * Block signals before forking child as it will have to
620 * set its own handlers. Child will re-enable SIGHUP as
621 * soon as the handlers are set up.
623 BlockSignals(true, SIGTERM
);
624 BlockSignals(true, SIGHUP
);
628 DEBUG(0, ("Failed to fork mdssd [%s], aborting ...\n",
633 /* parent or error */
636 /* Re-enable SIGHUP before returnig */
637 BlockSignals(false, SIGTERM
);
638 BlockSignals(false, SIGHUP
);
643 status
= smbd_reinit_after_fork(msg_ctx
, ev_ctx
, true, "mdssd-master");
644 if (!NT_STATUS_IS_OK(status
)) {
645 DEBUG(0,("reinit_after_fork() failed\n"));
646 smb_panic("reinit_after_fork() failed");
651 /* save the parent process id so the children can use it later */
652 parent_id
= messaging_server_id(msg_ctx
);
654 pfh_daemon_config(DAEMON_NAME
,
656 &default_pf_mdssd_cfg
);
658 mdssd_setup_sig_term_handler(ev_ctx
);
659 mdssd_setup_sig_hup_handler(ev_ctx
);
661 BlockSignals(false, SIGTERM
);
662 BlockSignals(false, SIGHUP
);
664 ok
= mdssd_create_sockets(ev_ctx
, msg_ctx
, listen_fd
, &listen_fd_size
);
669 /* start children before any more initialization is done */
670 ok
= prefork_create_pool(ev_ctx
, /* mem_ctx */
675 pf_mdssd_cfg
.min_children
,
676 pf_mdssd_cfg
.max_children
,
677 &mdssd_children_main
,
684 messaging_register(msg_ctx
,
686 MSG_SMB_CONF_UPDATED
,
687 mdssd_smb_conf_updated
);
688 messaging_register(msg_ctx
, ev_ctx
,
689 MSG_PREFORK_CHILD_EVENT
, child_ping
);
691 ok
= setup_rpc_module(ev_ctx
, msg_ctx
, "mdssvc");
696 ok
= mdssd_setup_children_monitor(ev_ctx
, msg_ctx
);
701 DEBUG(1, ("mdssd Daemon Started (%u)\n", (unsigned int)getpid()));
704 rc
= tevent_loop_wait(ev_ctx
);
706 /* should not be reached */
707 DEBUG(0,("mdssd: tevent_loop_wait() exited with %d - %s\n",
708 rc
, (rc
== 0) ? "out of events" : strerror(errno
)));