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 exit_server_cleanly("termination signal");
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 exit_server("failed to setup SIGTERM handler");
114 static void mdssd_sig_hup_handler(struct tevent_context
*ev
,
115 struct tevent_signal
*se
,
122 change_to_root_user();
123 lp_load_global(get_dyn_CONFIGFILE());
126 pfh_daemon_config(DAEMON_NAME
,
128 &default_pf_mdssd_cfg
);
130 /* relay to all children */
131 prefork_send_signal_to_all(mdssd_pool
, SIGHUP
);
134 static void mdssd_setup_sig_hup_handler(struct tevent_context
*ev_ctx
)
136 struct tevent_signal
*se
;
138 se
= tevent_add_signal(ev_ctx
,
141 mdssd_sig_hup_handler
,
144 DEBUG(0, ("failed to setup SIGHUP handler\n"));
149 /**********************************************************
151 **********************************************************/
153 static void mdssd_chld_sig_hup_handler(struct tevent_context
*ev
,
154 struct tevent_signal
*se
,
160 change_to_root_user();
164 static bool mdssd_setup_chld_hup_handler(struct tevent_context
*ev_ctx
)
166 struct tevent_signal
*se
;
168 se
= tevent_add_signal(ev_ctx
,
171 mdssd_chld_sig_hup_handler
,
174 DEBUG(1, ("failed to setup SIGHUP handler"));
181 static void parent_ping(struct messaging_context
*msg_ctx
,
184 struct server_id server_id
,
188 * The fact we received this message is enough to let make the
189 * event loop if it was idle. mdssd_children_main will cycle
190 * through mdssd_next_client at least once. That function will
191 * take whatever action is necessary
193 DEBUG(10, ("Got message that the parent changed status.\n"));
197 static bool mdssd_child_init(struct tevent_context
*ev_ctx
,
199 struct pf_worker_data
*pf
)
202 struct messaging_context
*msg_ctx
= global_messaging_context();
205 status
= reinit_after_fork(msg_ctx
, ev_ctx
,
206 true, "mdssd-child");
207 if (!NT_STATUS_IS_OK(status
)) {
208 DEBUG(0,("reinit_after_fork() failed\n"));
209 smb_panic("reinit_after_fork() failed");
212 mdssd_child_id
= child_id
;
215 ok
= mdssd_setup_chld_hup_handler(ev_ctx
);
220 messaging_register(msg_ctx
, ev_ctx
,
221 MSG_SMB_CONF_UPDATED
, mdssd_smb_conf_updated
);
222 messaging_register(msg_ctx
, ev_ctx
,
223 MSG_PREFORK_PARENT_EVENT
, parent_ping
);
225 ok
= setup_rpc_module(ev_ctx
, msg_ctx
, "mdssvc");
227 DBG_ERR("Failed to initialize mdssvc module\n");
234 struct mdssd_children_data
{
235 struct tevent_context
*ev_ctx
;
236 struct messaging_context
*msg_ctx
;
237 struct pf_worker_data
*pf
;
239 struct pf_listen_fd
*listen_fds
;
242 static void mdssd_next_client(void *pvt
);
244 static int mdssd_children_main(struct tevent_context
*ev_ctx
,
245 struct messaging_context
*msg_ctx
,
246 struct pf_worker_data
*pf
,
249 struct pf_listen_fd
*listen_fds
,
252 struct mdssd_children_data
*data
;
256 ok
= mdssd_child_init(ev_ctx
, child_id
, pf
);
261 data
= talloc(ev_ctx
, struct mdssd_children_data
);
266 data
->ev_ctx
= ev_ctx
;
267 data
->msg_ctx
= msg_ctx
;
268 data
->listen_fd_size
= listen_fd_size
;
269 data
->listen_fds
= listen_fds
;
271 /* loop until it is time to exit */
272 while (pf
->status
!= PF_WORKER_EXITING
) {
273 /* try to see if it is time to schedule the next client */
274 mdssd_next_client(data
);
276 ret
= tevent_loop_once(ev_ctx
);
278 DEBUG(0, ("tevent_loop_once() exited with %d: %s\n",
279 ret
, strerror(errno
)));
280 pf
->status
= PF_WORKER_EXITING
;
287 static void mdssd_client_terminated(struct pipes_struct
*p
, void *pvt
)
289 struct mdssd_children_data
*data
;
291 data
= talloc_get_type_abort(pvt
, struct mdssd_children_data
);
293 pfh_client_terminated(data
->pf
);
295 mdssd_next_client(pvt
);
298 struct mdssd_new_client
{
299 struct mdssd_children_data
*data
;
302 static void mdssd_handle_client(struct tevent_req
*req
);
304 static void mdssd_next_client(void *pvt
)
306 struct tevent_req
*req
;
307 struct mdssd_children_data
*data
;
308 struct mdssd_new_client
*next
;
310 data
= talloc_get_type_abort(pvt
, struct mdssd_children_data
);
312 if (!pfh_child_allowed_to_accept(data
->pf
)) {
313 /* nothing to do for now we are already listening
314 * or we are not allowed to listen further */
318 next
= talloc_zero(data
, struct mdssd_new_client
);
320 DEBUG(1, ("Out of memory!?\n"));
325 req
= prefork_listen_send(next
,
328 data
->listen_fd_size
,
331 DEBUG(1, ("Failed to make listening request!?\n"));
335 tevent_req_set_callback(req
, mdssd_handle_client
, next
);
338 static void mdssd_handle_client(struct tevent_req
*req
)
340 struct mdssd_children_data
*data
;
341 struct mdssd_new_client
*client
;
342 const DATA_BLOB ping
= data_blob_null
;
346 struct tsocket_address
*srv_addr
;
347 struct tsocket_address
*cli_addr
;
349 client
= tevent_req_callback_data(req
, struct mdssd_new_client
);
352 tmp_ctx
= talloc_stackframe();
353 if (tmp_ctx
== NULL
) {
354 DEBUG(1, ("Failed to allocate stackframe!\n"));
358 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
,
392 mdssd_client_terminated
,
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 dcerpc_ncacn_accept(data
->ev_ctx
,
416 NULL
, /* remote client address */
417 NULL
, /* local server address */
419 mdssd_client_terminated
,
422 dcerpc_ncacn_accept(data
->ev_ctx
,
429 mdssd_client_terminated
,
433 DEBUG(0, ("ERROR: Unsupported socket!\n"));
437 talloc_free(tmp_ctx
);
444 static void child_ping(struct messaging_context
*msg_ctx
,
447 struct server_id server_id
,
450 struct tevent_context
*ev_ctx
;
452 ev_ctx
= talloc_get_type_abort(private_data
, struct tevent_context
);
454 DEBUG(10, ("Got message that a child changed status.\n"));
455 pfh_manage_pool(ev_ctx
, msg_ctx
, &pf_mdssd_cfg
, mdssd_pool
);
458 static bool mdssd_schedule_check(struct tevent_context
*ev_ctx
,
459 struct messaging_context
*msg_ctx
,
460 struct timeval current_time
);
462 static void mdssd_check_children(struct tevent_context
*ev_ctx
,
463 struct tevent_timer
*te
,
464 struct timeval current_time
,
467 static void mdssd_sigchld_handler(struct tevent_context
*ev_ctx
,
468 struct prefork_pool
*pfp
,
471 struct messaging_context
*msg_ctx
;
473 msg_ctx
= talloc_get_type_abort(pvt
, struct messaging_context
);
475 /* run pool management so we can fork/retire or increase
476 * the allowed connections per child based on load */
477 pfh_manage_pool(ev_ctx
, msg_ctx
, &pf_mdssd_cfg
, mdssd_pool
);
480 static bool mdssd_setup_children_monitor(struct tevent_context
*ev_ctx
,
481 struct messaging_context
*msg_ctx
)
485 /* add our oun sigchld callback */
486 prefork_set_sigchld_callback(mdssd_pool
, mdssd_sigchld_handler
, msg_ctx
);
488 ok
= mdssd_schedule_check(ev_ctx
, msg_ctx
, tevent_timeval_current());
493 static bool mdssd_schedule_check(struct tevent_context
*ev_ctx
,
494 struct messaging_context
*msg_ctx
,
495 struct timeval current_time
)
497 struct tevent_timer
*te
;
498 struct timeval next_event
;
500 /* check situation again in 10 seconds */
501 next_event
= tevent_timeval_current_ofs(10, 0);
503 /* TODO: check when the socket becomes readable, so that children
504 * are checked only when there is some activity ? */
505 te
= tevent_add_timer(ev_ctx
, mdssd_pool
, next_event
,
506 mdssd_check_children
, msg_ctx
);
508 DEBUG(2, ("Failed to set up children monitoring!\n"));
515 static void mdssd_check_children(struct tevent_context
*ev_ctx
,
516 struct tevent_timer
*te
,
517 struct timeval current_time
,
520 struct messaging_context
*msg_ctx
;
522 msg_ctx
= talloc_get_type_abort(pvt
, struct messaging_context
);
524 pfh_manage_pool(ev_ctx
, msg_ctx
, &pf_mdssd_cfg
, mdssd_pool
);
526 mdssd_schedule_check(ev_ctx
, msg_ctx
, current_time
);
533 static bool mdssd_create_sockets(struct tevent_context
*ev_ctx
,
534 struct messaging_context
*msg_ctx
,
535 struct pf_listen_fd
*listen_fd
,
538 struct dcerpc_binding_vector
*v
, *v_orig
;
545 tmp_ctx
= talloc_stackframe();
546 if (tmp_ctx
== NULL
) {
550 status
= dcerpc_binding_vector_new(tmp_ctx
, &v_orig
);
551 if (!NT_STATUS_IS_OK(status
)) {
556 status
= dcesrv_create_ncacn_np_socket("mdssvc", &fd
);
557 if (!NT_STATUS_IS_OK(status
)) {
561 rc
= listen(fd
, pf_mdssd_cfg
.max_allowed_clients
);
565 listen_fd
[*listen_fd_size
].fd
= fd
;
566 listen_fd
[*listen_fd_size
].fd_data
= NULL
;
570 status
= dcesrv_create_ncalrpc_socket("mdssvc", &fd
);
571 if (!NT_STATUS_IS_OK(status
)) {
575 rc
= listen(fd
, pf_mdssd_cfg
.max_allowed_clients
);
579 listen_fd
[*listen_fd_size
].fd
= fd
;
580 listen_fd
[*listen_fd_size
].fd_data
= NULL
;
584 v
= dcerpc_binding_vector_dup(tmp_ctx
, v_orig
);
589 status
= dcerpc_binding_vector_replace_iface(&ndr_table_mdssvc
, v
);
590 if (!NT_STATUS_IS_OK(status
)) {
594 status
= dcerpc_binding_vector_add_np_default(&ndr_table_mdssvc
, v
);
595 if (!NT_STATUS_IS_OK(status
)) {
599 status
= dcerpc_binding_vector_add_unix(&ndr_table_mdssvc
, v
, "mdssvc");
600 if (!NT_STATUS_IS_OK(status
)) {
609 talloc_free(tmp_ctx
);
613 void start_mdssd(struct tevent_context
*ev_ctx
,
614 struct messaging_context
*msg_ctx
)
617 struct pf_listen_fd listen_fd
[MDSSD_MAX_SOCKETS
];
618 int listen_fd_size
= 0;
623 DEBUG(1, ("Forking Metadata Service Daemon\n"));
626 * Block signals before forking child as it will have to
627 * set its own handlers. Child will re-enable SIGHUP as
628 * soon as the handlers are set up.
630 BlockSignals(true, SIGTERM
);
631 BlockSignals(true, SIGHUP
);
635 DEBUG(0, ("Failed to fork mdssd [%s], aborting ...\n",
640 /* parent or error */
643 /* Re-enable SIGHUP before returnig */
644 BlockSignals(false, SIGTERM
);
645 BlockSignals(false, SIGHUP
);
650 status
= smbd_reinit_after_fork(msg_ctx
, ev_ctx
, true, "mdssd-master");
651 if (!NT_STATUS_IS_OK(status
)) {
652 DEBUG(0,("reinit_after_fork() failed\n"));
653 smb_panic("reinit_after_fork() failed");
658 /* save the parent process id so the children can use it later */
659 parent_id
= messaging_server_id(msg_ctx
);
661 pfh_daemon_config(DAEMON_NAME
,
663 &default_pf_mdssd_cfg
);
665 mdssd_setup_sig_term_handler(ev_ctx
);
666 mdssd_setup_sig_hup_handler(ev_ctx
);
668 BlockSignals(false, SIGTERM
);
669 BlockSignals(false, SIGHUP
);
671 ok
= mdssd_create_sockets(ev_ctx
, msg_ctx
, listen_fd
, &listen_fd_size
);
676 /* start children before any more initialization is done */
677 ok
= prefork_create_pool(ev_ctx
, /* mem_ctx */
682 pf_mdssd_cfg
.min_children
,
683 pf_mdssd_cfg
.max_children
,
684 &mdssd_children_main
,
691 messaging_register(msg_ctx
,
693 MSG_SMB_CONF_UPDATED
,
694 mdssd_smb_conf_updated
);
695 messaging_register(msg_ctx
, ev_ctx
,
696 MSG_PREFORK_CHILD_EVENT
, child_ping
);
698 ok
= mdssd_setup_children_monitor(ev_ctx
, msg_ctx
);
703 DEBUG(1, ("mdssd Daemon Started (%u)\n", (unsigned int)getpid()));
706 rc
= tevent_loop_wait(ev_ctx
);
708 /* should not be reached */
709 DEBUG(0,("mdssd: tevent_loop_wait() exited with %d - %s\n",
710 rc
, (rc
== 0) ? "out of events" : strerror(errno
)));