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/>.
27 #include "lib/id_cache.h"
29 #include "../lib/tsocket/tsocket.h"
30 #include "lib/server_prefork.h"
31 #include "lib/server_prefork_util.h"
32 #include "librpc/rpc/dcerpc_ep.h"
34 #include "rpc_server/rpc_server.h"
35 #include "rpc_server/rpc_ep_register.h"
36 #include "rpc_server/rpc_sock_helper.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 rpc_mdssvc_shutdown();
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 if (!serverid_register(messaging_server_id(msg_ctx
),
226 messaging_register(msg_ctx
, ev_ctx
,
227 MSG_SMB_CONF_UPDATED
, mdssd_smb_conf_updated
);
228 messaging_register(msg_ctx
, ev_ctx
,
229 MSG_PREFORK_PARENT_EVENT
, parent_ping
);
231 status
= rpc_mdssvc_init(NULL
);
232 if (!NT_STATUS_IS_OK(status
)) {
233 DEBUG(0, ("Failed to intialize RPC: %s\n",
241 struct mdssd_children_data
{
242 struct tevent_context
*ev_ctx
;
243 struct messaging_context
*msg_ctx
;
244 struct pf_worker_data
*pf
;
249 static void mdssd_next_client(void *pvt
);
251 static int mdssd_children_main(struct tevent_context
*ev_ctx
,
252 struct messaging_context
*msg_ctx
,
253 struct pf_worker_data
*pf
,
259 struct mdssd_children_data
*data
;
263 ok
= mdssd_child_init(ev_ctx
, child_id
, pf
);
268 data
= talloc(ev_ctx
, struct mdssd_children_data
);
273 data
->ev_ctx
= ev_ctx
;
274 data
->msg_ctx
= msg_ctx
;
275 data
->listen_fd_size
= listen_fd_size
;
276 data
->listen_fds
= listen_fds
;
278 /* loop until it is time to exit */
279 while (pf
->status
!= PF_WORKER_EXITING
) {
280 /* try to see if it is time to schedule the next client */
281 mdssd_next_client(data
);
283 ret
= tevent_loop_once(ev_ctx
);
285 DEBUG(0, ("tevent_loop_once() exited with %d: %s\n",
286 ret
, strerror(errno
)));
287 pf
->status
= PF_WORKER_EXITING
;
294 static void mdssd_client_terminated(void *pvt
)
296 struct mdssd_children_data
*data
;
298 data
= talloc_get_type_abort(pvt
, struct mdssd_children_data
);
300 pfh_client_terminated(data
->pf
);
302 mdssd_next_client(pvt
);
305 struct mdssd_new_client
{
306 struct mdssd_children_data
*data
;
309 static void mdssd_handle_client(struct tevent_req
*req
);
311 static void mdssd_next_client(void *pvt
)
313 struct tevent_req
*req
;
314 struct mdssd_children_data
*data
;
315 struct mdssd_new_client
*next
;
317 data
= talloc_get_type_abort(pvt
, struct mdssd_children_data
);
319 if (!pfh_child_allowed_to_accept(data
->pf
)) {
320 /* nothing to do for now we are already listening
321 * or we are not allowed to listen further */
325 next
= talloc_zero(data
, struct mdssd_new_client
);
327 DEBUG(1, ("Out of memory!?\n"));
332 req
= prefork_listen_send(next
,
335 data
->listen_fd_size
,
338 DEBUG(1, ("Failed to make listening request!?\n"));
342 tevent_req_set_callback(req
, mdssd_handle_client
, next
);
345 static void mdssd_handle_client(struct tevent_req
*req
)
347 struct mdssd_children_data
*data
;
348 struct mdssd_new_client
*client
;
349 const DATA_BLOB ping
= data_blob_null
;
353 struct tsocket_address
*srv_addr
;
354 struct tsocket_address
*cli_addr
;
356 client
= tevent_req_callback_data(req
, struct mdssd_new_client
);
359 tmp_ctx
= talloc_stackframe();
360 if (tmp_ctx
== NULL
) {
361 DEBUG(1, ("Failed to allocate stackframe!\n"));
365 rc
= prefork_listen_recv(req
,
371 /* this will free the request too */
375 DEBUG(6, ("No client connection was available after all!\n"));
379 /* Warn parent that our status changed */
380 messaging_send(data
->msg_ctx
, parent_id
,
381 MSG_PREFORK_CHILD_EVENT
, &ping
);
383 DEBUG(2, ("mdssd preforked child %d got client connection!\n",
384 (int)(data
->pf
->pid
)));
386 if (tsocket_address_is_inet(srv_addr
, "ip")) {
387 DEBUG(3, ("Got a tcpip client connection from %s on inteface %s\n",
388 tsocket_address_string(cli_addr
, tmp_ctx
),
389 tsocket_address_string(srv_addr
, tmp_ctx
)));
391 dcerpc_ncacn_accept(data
->ev_ctx
,
399 } else if (tsocket_address_is_unix(srv_addr
)) {
403 p
= tsocket_address_unix_path(srv_addr
, tmp_ctx
);
405 talloc_free(tmp_ctx
);
416 if (strstr(p
, "/np/")) {
417 named_pipe_accept_function(data
->ev_ctx
,
421 mdssd_client_terminated
,
424 dcerpc_ncacn_accept(data
->ev_ctx
,
434 DEBUG(0, ("ERROR: Unsupported socket!\n"));
438 talloc_free(tmp_ctx
);
445 static void child_ping(struct messaging_context
*msg_ctx
,
448 struct server_id server_id
,
451 struct tevent_context
*ev_ctx
;
453 ev_ctx
= talloc_get_type_abort(private_data
, struct tevent_context
);
455 DEBUG(10, ("Got message that a child changed status.\n"));
456 pfh_manage_pool(ev_ctx
, msg_ctx
, &pf_mdssd_cfg
, mdssd_pool
);
459 static bool mdssd_schedule_check(struct tevent_context
*ev_ctx
,
460 struct messaging_context
*msg_ctx
,
461 struct timeval current_time
);
463 static void mdssd_check_children(struct tevent_context
*ev_ctx
,
464 struct tevent_timer
*te
,
465 struct timeval current_time
,
468 static void mdssd_sigchld_handler(struct tevent_context
*ev_ctx
,
469 struct prefork_pool
*pfp
,
472 struct messaging_context
*msg_ctx
;
474 msg_ctx
= talloc_get_type_abort(pvt
, struct messaging_context
);
476 /* run pool management so we can fork/retire or increase
477 * the allowed connections per child based on load */
478 pfh_manage_pool(ev_ctx
, msg_ctx
, &pf_mdssd_cfg
, mdssd_pool
);
481 static bool mdssd_setup_children_monitor(struct tevent_context
*ev_ctx
,
482 struct messaging_context
*msg_ctx
)
486 /* add our oun sigchld callback */
487 prefork_set_sigchld_callback(mdssd_pool
, mdssd_sigchld_handler
, msg_ctx
);
489 ok
= mdssd_schedule_check(ev_ctx
, msg_ctx
, tevent_timeval_current());
494 static bool mdssd_schedule_check(struct tevent_context
*ev_ctx
,
495 struct messaging_context
*msg_ctx
,
496 struct timeval current_time
)
498 struct tevent_timer
*te
;
499 struct timeval next_event
;
501 /* check situation again in 10 seconds */
502 next_event
= tevent_timeval_current_ofs(10, 0);
504 /* TODO: check when the socket becomes readable, so that children
505 * are checked only when there is some activity ? */
506 te
= tevent_add_timer(ev_ctx
, mdssd_pool
, next_event
,
507 mdssd_check_children
, msg_ctx
);
509 DEBUG(2, ("Failed to set up children monitoring!\n"));
516 static void mdssd_check_children(struct tevent_context
*ev_ctx
,
517 struct tevent_timer
*te
,
518 struct timeval current_time
,
521 struct messaging_context
*msg_ctx
;
523 msg_ctx
= talloc_get_type_abort(pvt
, struct messaging_context
);
525 pfh_manage_pool(ev_ctx
, msg_ctx
, &pf_mdssd_cfg
, mdssd_pool
);
527 mdssd_schedule_check(ev_ctx
, msg_ctx
, current_time
);
534 static bool mdssd_create_sockets(struct tevent_context
*ev_ctx
,
535 struct messaging_context
*msg_ctx
,
539 struct dcerpc_binding_vector
*v
, *v_orig
;
546 tmp_ctx
= talloc_stackframe();
547 if (tmp_ctx
== NULL
) {
551 status
= dcerpc_binding_vector_new(tmp_ctx
, &v_orig
);
552 if (!NT_STATUS_IS_OK(status
)) {
557 fd
= create_named_pipe_socket("mdssvc");
562 rc
= listen(fd
, pf_mdssd_cfg
.max_allowed_clients
);
566 listen_fd
[*listen_fd_size
] = fd
;
569 fd
= create_dcerpc_ncalrpc_socket("mdssvc");
574 rc
= listen(fd
, pf_mdssd_cfg
.max_allowed_clients
);
578 listen_fd
[*listen_fd_size
] = fd
;
582 v
= dcerpc_binding_vector_dup(tmp_ctx
, v_orig
);
587 status
= dcerpc_binding_vector_replace_iface(&ndr_table_mdssvc
, v
);
588 if (!NT_STATUS_IS_OK(status
)) {
592 status
= dcerpc_binding_vector_add_np_default(&ndr_table_mdssvc
, v
);
593 if (!NT_STATUS_IS_OK(status
)) {
597 status
= dcerpc_binding_vector_add_unix(&ndr_table_mdssvc
, v
, "mdssvc");
598 if (!NT_STATUS_IS_OK(status
)) {
607 talloc_free(tmp_ctx
);
611 static bool mdssvc_init_cb(void *ptr
)
613 struct messaging_context
*msg_ctx
=
614 talloc_get_type_abort(ptr
, struct messaging_context
);
617 ok
= init_service_mdssvc(msg_ctx
);
625 static bool mdssvc_shutdown_cb(void *ptr
)
627 shutdown_service_mdssvc();
632 void start_mdssd(struct tevent_context
*ev_ctx
,
633 struct messaging_context
*msg_ctx
)
636 int listen_fd
[MDSSD_MAX_SOCKETS
];
637 int listen_fd_size
= 0;
641 struct rpc_srv_callbacks mdssvc_cb
;
643 DEBUG(1, ("Forking Metadata Service Daemon\n"));
646 * Block signals before forking child as it will have to
647 * set its own handlers. Child will re-enable SIGHUP as
648 * soon as the handlers are set up.
650 BlockSignals(true, SIGTERM
);
651 BlockSignals(true, SIGHUP
);
655 DEBUG(0, ("Failed to fork mdssd [%s], aborting ...\n",
660 /* parent or error */
663 /* Re-enable SIGHUP before returnig */
664 BlockSignals(false, SIGTERM
);
665 BlockSignals(false, SIGHUP
);
670 status
= reinit_after_fork(msg_ctx
, ev_ctx
, true, "mdssd-master");
671 if (!NT_STATUS_IS_OK(status
)) {
672 DEBUG(0,("reinit_after_fork() failed\n"));
673 smb_panic("reinit_after_fork() failed");
678 /* save the parent process id so the children can use it later */
679 parent_id
= messaging_server_id(msg_ctx
);
681 pfh_daemon_config(DAEMON_NAME
,
683 &default_pf_mdssd_cfg
);
685 mdssd_setup_sig_term_handler(ev_ctx
);
686 mdssd_setup_sig_hup_handler(ev_ctx
);
688 BlockSignals(false, SIGTERM
);
689 BlockSignals(false, SIGHUP
);
691 ok
= mdssd_create_sockets(ev_ctx
, msg_ctx
, listen_fd
, &listen_fd_size
);
696 /* start children before any more initialization is done */
697 ok
= prefork_create_pool(ev_ctx
, /* mem_ctx */
702 pf_mdssd_cfg
.min_children
,
703 pf_mdssd_cfg
.max_children
,
704 &mdssd_children_main
,
711 if (!serverid_register(messaging_server_id(msg_ctx
),
716 messaging_register(msg_ctx
,
718 MSG_SMB_CONF_UPDATED
,
719 mdssd_smb_conf_updated
);
720 messaging_register(msg_ctx
, ev_ctx
,
721 MSG_PREFORK_CHILD_EVENT
, child_ping
);
723 mdssvc_cb
.init
= mdssvc_init_cb
;
724 mdssvc_cb
.shutdown
= mdssvc_shutdown_cb
;
725 mdssvc_cb
.private_data
= msg_ctx
;
727 status
= rpc_mdssvc_init(&mdssvc_cb
);
728 if (!NT_STATUS_IS_OK(status
)) {
732 ok
= mdssd_setup_children_monitor(ev_ctx
, msg_ctx
);
737 DEBUG(1, ("mdssd Daemon Started (%u)\n", (unsigned int)getpid()));
740 rc
= tevent_loop_wait(ev_ctx
);
742 /* should not be reached */
743 DEBUG(0,("mdssd: tevent_loop_wait() exited with %d - %s\n",
744 rc
, (rc
== 0) ? "out of events" : strerror(errno
)));