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"
42 #define DBGC_CLASS DBGC_RPC_SRV
44 #define DAEMON_NAME "mdssd"
45 #define MDSSD_MAX_SOCKETS 64
47 static struct server_id parent_id
;
48 static struct prefork_pool
*mdssd_pool
= NULL
;
49 static int mdssd_child_id
= 0;
51 static struct pf_daemon_config default_pf_mdssd_cfg
= {
52 .prefork_status
= PFH_INIT
,
56 .max_allowed_clients
= 1000,
57 .child_min_life
= 60 /* 1 minute minimum life time */
59 static struct pf_daemon_config pf_mdssd_cfg
= { 0 };
61 void start_mdssd(struct tevent_context
*ev_ctx
,
62 struct messaging_context
*msg_ctx
);
64 static void mdssd_smb_conf_updated(struct messaging_context
*msg
,
67 struct server_id server_id
,
70 struct tevent_context
*ev_ctx
;
72 DEBUG(10, ("Got message saying smb.conf was updated. Reloading.\n"));
73 ev_ctx
= talloc_get_type_abort(private_data
, struct tevent_context
);
75 change_to_root_user();
76 lp_load_global(get_dyn_CONFIGFILE());
79 if (mdssd_child_id
== 0) {
80 pfh_daemon_config(DAEMON_NAME
,
82 &default_pf_mdssd_cfg
);
83 pfh_manage_pool(ev_ctx
, msg
, &pf_mdssd_cfg
, mdssd_pool
);
87 static void mdssd_sig_term_handler(struct tevent_context
*ev
,
88 struct tevent_signal
*se
,
94 shutdown_rpc_module("mdssvc");
96 DEBUG(0, ("termination signal\n"));
100 static void mdssd_setup_sig_term_handler(struct tevent_context
*ev_ctx
)
102 struct tevent_signal
*se
;
104 se
= tevent_add_signal(ev_ctx
,
107 mdssd_sig_term_handler
,
110 DEBUG(0, ("failed to setup SIGTERM handler\n"));
115 static void mdssd_sig_hup_handler(struct tevent_context
*ev
,
116 struct tevent_signal
*se
,
123 change_to_root_user();
124 lp_load_global(get_dyn_CONFIGFILE());
127 pfh_daemon_config(DAEMON_NAME
,
129 &default_pf_mdssd_cfg
);
131 /* relay to all children */
132 prefork_send_signal_to_all(mdssd_pool
, SIGHUP
);
135 static void mdssd_setup_sig_hup_handler(struct tevent_context
*ev_ctx
)
137 struct tevent_signal
*se
;
139 se
= tevent_add_signal(ev_ctx
,
142 mdssd_sig_hup_handler
,
145 DEBUG(0, ("failed to setup SIGHUP handler\n"));
150 /**********************************************************
152 **********************************************************/
154 static void mdssd_chld_sig_hup_handler(struct tevent_context
*ev
,
155 struct tevent_signal
*se
,
161 change_to_root_user();
165 static bool mdssd_setup_chld_hup_handler(struct tevent_context
*ev_ctx
)
167 struct tevent_signal
*se
;
169 se
= tevent_add_signal(ev_ctx
,
172 mdssd_chld_sig_hup_handler
,
175 DEBUG(1, ("failed to setup SIGHUP handler"));
182 static void parent_ping(struct messaging_context
*msg_ctx
,
185 struct server_id server_id
,
189 * The fact we received this message is enough to let make the
190 * event loop if it was idle. mdssd_children_main will cycle
191 * through mdssd_next_client at least once. That function will
192 * take whatever action is necessary
194 DEBUG(10, ("Got message that the parent changed status.\n"));
198 static bool mdssd_child_init(struct tevent_context
*ev_ctx
,
200 struct pf_worker_data
*pf
)
203 struct messaging_context
*msg_ctx
= server_messaging_context();
206 status
= reinit_after_fork(msg_ctx
, ev_ctx
,
207 true, "mdssd-child");
208 if (!NT_STATUS_IS_OK(status
)) {
209 DEBUG(0,("reinit_after_fork() failed\n"));
210 smb_panic("reinit_after_fork() failed");
213 mdssd_child_id
= child_id
;
216 ok
= mdssd_setup_chld_hup_handler(ev_ctx
);
221 messaging_register(msg_ctx
, ev_ctx
,
222 MSG_SMB_CONF_UPDATED
, mdssd_smb_conf_updated
);
223 messaging_register(msg_ctx
, ev_ctx
,
224 MSG_PREFORK_PARENT_EVENT
, parent_ping
);
226 ok
= init_rpc_module("mdssvc", NULL
);
228 DBG_ERR("Failed to de-intialize RPC\n");
235 struct mdssd_children_data
{
236 struct tevent_context
*ev_ctx
;
237 struct messaging_context
*msg_ctx
;
238 struct pf_worker_data
*pf
;
243 static void mdssd_next_client(void *pvt
);
245 static int mdssd_children_main(struct tevent_context
*ev_ctx
,
246 struct messaging_context
*msg_ctx
,
247 struct pf_worker_data
*pf
,
253 struct mdssd_children_data
*data
;
257 ok
= mdssd_child_init(ev_ctx
, child_id
, pf
);
262 data
= talloc(ev_ctx
, struct mdssd_children_data
);
267 data
->ev_ctx
= ev_ctx
;
268 data
->msg_ctx
= msg_ctx
;
269 data
->listen_fd_size
= listen_fd_size
;
270 data
->listen_fds
= listen_fds
;
272 /* loop until it is time to exit */
273 while (pf
->status
!= PF_WORKER_EXITING
) {
274 /* try to see if it is time to schedule the next client */
275 mdssd_next_client(data
);
277 ret
= tevent_loop_once(ev_ctx
);
279 DEBUG(0, ("tevent_loop_once() exited with %d: %s\n",
280 ret
, strerror(errno
)));
281 pf
->status
= PF_WORKER_EXITING
;
288 static void mdssd_client_terminated(void *pvt
)
290 struct mdssd_children_data
*data
;
292 data
= talloc_get_type_abort(pvt
, struct mdssd_children_data
);
294 pfh_client_terminated(data
->pf
);
296 mdssd_next_client(pvt
);
299 struct mdssd_new_client
{
300 struct mdssd_children_data
*data
;
303 static void mdssd_handle_client(struct tevent_req
*req
);
305 static void mdssd_next_client(void *pvt
)
307 struct tevent_req
*req
;
308 struct mdssd_children_data
*data
;
309 struct mdssd_new_client
*next
;
311 data
= talloc_get_type_abort(pvt
, struct mdssd_children_data
);
313 if (!pfh_child_allowed_to_accept(data
->pf
)) {
314 /* nothing to do for now we are already listening
315 * or we are not allowed to listen further */
319 next
= talloc_zero(data
, struct mdssd_new_client
);
321 DEBUG(1, ("Out of memory!?\n"));
326 req
= prefork_listen_send(next
,
329 data
->listen_fd_size
,
332 DEBUG(1, ("Failed to make listening request!?\n"));
336 tevent_req_set_callback(req
, mdssd_handle_client
, next
);
339 static void mdssd_handle_client(struct tevent_req
*req
)
341 struct mdssd_children_data
*data
;
342 struct mdssd_new_client
*client
;
343 const DATA_BLOB ping
= data_blob_null
;
347 struct tsocket_address
*srv_addr
;
348 struct tsocket_address
*cli_addr
;
350 client
= tevent_req_callback_data(req
, struct mdssd_new_client
);
353 tmp_ctx
= talloc_stackframe();
354 if (tmp_ctx
== NULL
) {
355 DEBUG(1, ("Failed to allocate stackframe!\n"));
359 rc
= prefork_listen_recv(req
,
365 /* this will free the request too */
369 DEBUG(6, ("No client connection was available after all!\n"));
373 /* Warn parent that our status changed */
374 messaging_send(data
->msg_ctx
, parent_id
,
375 MSG_PREFORK_CHILD_EVENT
, &ping
);
377 DEBUG(2, ("mdssd preforked child %d got client connection!\n",
378 (int)(data
->pf
->pid
)));
380 if (tsocket_address_is_inet(srv_addr
, "ip")) {
381 DEBUG(3, ("Got a tcpip client connection from %s on inteface %s\n",
382 tsocket_address_string(cli_addr
, tmp_ctx
),
383 tsocket_address_string(srv_addr
, tmp_ctx
)));
385 dcerpc_ncacn_accept(data
->ev_ctx
,
393 } else if (tsocket_address_is_unix(srv_addr
)) {
397 p
= tsocket_address_unix_path(srv_addr
, tmp_ctx
);
399 talloc_free(tmp_ctx
);
410 if (strstr(p
, "/np/")) {
411 named_pipe_accept_function(data
->ev_ctx
,
415 mdssd_client_terminated
,
418 dcerpc_ncacn_accept(data
->ev_ctx
,
428 DEBUG(0, ("ERROR: Unsupported socket!\n"));
432 talloc_free(tmp_ctx
);
439 static void child_ping(struct messaging_context
*msg_ctx
,
442 struct server_id server_id
,
445 struct tevent_context
*ev_ctx
;
447 ev_ctx
= talloc_get_type_abort(private_data
, struct tevent_context
);
449 DEBUG(10, ("Got message that a child changed status.\n"));
450 pfh_manage_pool(ev_ctx
, msg_ctx
, &pf_mdssd_cfg
, mdssd_pool
);
453 static bool mdssd_schedule_check(struct tevent_context
*ev_ctx
,
454 struct messaging_context
*msg_ctx
,
455 struct timeval current_time
);
457 static void mdssd_check_children(struct tevent_context
*ev_ctx
,
458 struct tevent_timer
*te
,
459 struct timeval current_time
,
462 static void mdssd_sigchld_handler(struct tevent_context
*ev_ctx
,
463 struct prefork_pool
*pfp
,
466 struct messaging_context
*msg_ctx
;
468 msg_ctx
= talloc_get_type_abort(pvt
, struct messaging_context
);
470 /* run pool management so we can fork/retire or increase
471 * the allowed connections per child based on load */
472 pfh_manage_pool(ev_ctx
, msg_ctx
, &pf_mdssd_cfg
, mdssd_pool
);
475 static bool mdssd_setup_children_monitor(struct tevent_context
*ev_ctx
,
476 struct messaging_context
*msg_ctx
)
480 /* add our oun sigchld callback */
481 prefork_set_sigchld_callback(mdssd_pool
, mdssd_sigchld_handler
, msg_ctx
);
483 ok
= mdssd_schedule_check(ev_ctx
, msg_ctx
, tevent_timeval_current());
488 static bool mdssd_schedule_check(struct tevent_context
*ev_ctx
,
489 struct messaging_context
*msg_ctx
,
490 struct timeval current_time
)
492 struct tevent_timer
*te
;
493 struct timeval next_event
;
495 /* check situation again in 10 seconds */
496 next_event
= tevent_timeval_current_ofs(10, 0);
498 /* TODO: check when the socket becomes readable, so that children
499 * are checked only when there is some activity ? */
500 te
= tevent_add_timer(ev_ctx
, mdssd_pool
, next_event
,
501 mdssd_check_children
, msg_ctx
);
503 DEBUG(2, ("Failed to set up children monitoring!\n"));
510 static void mdssd_check_children(struct tevent_context
*ev_ctx
,
511 struct tevent_timer
*te
,
512 struct timeval current_time
,
515 struct messaging_context
*msg_ctx
;
517 msg_ctx
= talloc_get_type_abort(pvt
, struct messaging_context
);
519 pfh_manage_pool(ev_ctx
, msg_ctx
, &pf_mdssd_cfg
, mdssd_pool
);
521 mdssd_schedule_check(ev_ctx
, msg_ctx
, current_time
);
528 static bool mdssd_create_sockets(struct tevent_context
*ev_ctx
,
529 struct messaging_context
*msg_ctx
,
533 struct dcerpc_binding_vector
*v
, *v_orig
;
540 tmp_ctx
= talloc_stackframe();
541 if (tmp_ctx
== NULL
) {
545 status
= dcerpc_binding_vector_new(tmp_ctx
, &v_orig
);
546 if (!NT_STATUS_IS_OK(status
)) {
551 fd
= create_named_pipe_socket("mdssvc");
556 rc
= listen(fd
, pf_mdssd_cfg
.max_allowed_clients
);
560 listen_fd
[*listen_fd_size
] = fd
;
563 fd
= create_dcerpc_ncalrpc_socket("mdssvc");
568 rc
= listen(fd
, pf_mdssd_cfg
.max_allowed_clients
);
572 listen_fd
[*listen_fd_size
] = fd
;
576 v
= dcerpc_binding_vector_dup(tmp_ctx
, v_orig
);
581 status
= dcerpc_binding_vector_replace_iface(&ndr_table_mdssvc
, v
);
582 if (!NT_STATUS_IS_OK(status
)) {
586 status
= dcerpc_binding_vector_add_np_default(&ndr_table_mdssvc
, v
);
587 if (!NT_STATUS_IS_OK(status
)) {
591 status
= dcerpc_binding_vector_add_unix(&ndr_table_mdssvc
, v
, "mdssvc");
592 if (!NT_STATUS_IS_OK(status
)) {
601 talloc_free(tmp_ctx
);
605 void start_mdssd(struct tevent_context
*ev_ctx
,
606 struct messaging_context
*msg_ctx
)
609 int listen_fd
[MDSSD_MAX_SOCKETS
];
610 int listen_fd_size
= 0;
615 DEBUG(1, ("Forking Metadata Service Daemon\n"));
618 * Block signals before forking child as it will have to
619 * set its own handlers. Child will re-enable SIGHUP as
620 * soon as the handlers are set up.
622 BlockSignals(true, SIGTERM
);
623 BlockSignals(true, SIGHUP
);
627 DEBUG(0, ("Failed to fork mdssd [%s], aborting ...\n",
632 /* parent or error */
635 /* Re-enable SIGHUP before returnig */
636 BlockSignals(false, SIGTERM
);
637 BlockSignals(false, SIGHUP
);
642 status
= smbd_reinit_after_fork(msg_ctx
, ev_ctx
, true, "mdssd-master");
643 if (!NT_STATUS_IS_OK(status
)) {
644 DEBUG(0,("reinit_after_fork() failed\n"));
645 smb_panic("reinit_after_fork() failed");
650 /* save the parent process id so the children can use it later */
651 parent_id
= messaging_server_id(msg_ctx
);
653 pfh_daemon_config(DAEMON_NAME
,
655 &default_pf_mdssd_cfg
);
657 mdssd_setup_sig_term_handler(ev_ctx
);
658 mdssd_setup_sig_hup_handler(ev_ctx
);
660 BlockSignals(false, SIGTERM
);
661 BlockSignals(false, SIGHUP
);
663 ok
= mdssd_create_sockets(ev_ctx
, msg_ctx
, listen_fd
, &listen_fd_size
);
668 /* start children before any more initialization is done */
669 ok
= prefork_create_pool(ev_ctx
, /* mem_ctx */
674 pf_mdssd_cfg
.min_children
,
675 pf_mdssd_cfg
.max_children
,
676 &mdssd_children_main
,
683 messaging_register(msg_ctx
,
685 MSG_SMB_CONF_UPDATED
,
686 mdssd_smb_conf_updated
);
687 messaging_register(msg_ctx
, ev_ctx
,
688 MSG_PREFORK_CHILD_EVENT
, child_ping
);
690 ok
= setup_rpc_module(ev_ctx
, msg_ctx
, "mdssvc");
695 ok
= mdssd_setup_children_monitor(ev_ctx
, msg_ctx
);
700 DEBUG(1, ("mdssd Daemon Started (%u)\n", (unsigned int)getpid()));
703 rc
= tevent_loop_wait(ev_ctx
);
705 /* should not be reached */
706 DEBUG(0,("mdssd: tevent_loop_wait() exited with %d - %s\n",
707 rc
, (rc
== 0) ? "out of events" : strerror(errno
)));