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"
37 #include "rpc_server/rpc_modules.h"
39 #include "librpc/gen_ndr/srv_mdssvc.h"
40 #include "rpc_server/mdssvc/srv_mdssvc_nt.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 = server_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 if (!serverid_register(messaging_server_id(msg_ctx),
227 messaging_register(msg_ctx, ev_ctx,
228 MSG_SMB_CONF_UPDATED, mdssd_smb_conf_updated);
229 messaging_register(msg_ctx, ev_ctx,
230 MSG_PREFORK_PARENT_EVENT, parent_ping);
232 ok = init_rpc_module("mdssvc", NULL);
234 DBG_ERR("Failed to de-intialize RPC\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 void start_mdssd(struct tevent_context *ev_ctx,
612 struct messaging_context *msg_ctx)
615 int listen_fd[MDSSD_MAX_SOCKETS];
616 int listen_fd_size = 0;
621 DEBUG(1, ("Forking Metadata Service Daemon\n"));
624 * Block signals before forking child as it will have to
625 * set its own handlers. Child will re-enable SIGHUP as
626 * soon as the handlers are set up.
628 BlockSignals(true, SIGTERM);
629 BlockSignals(true, SIGHUP);
633 DEBUG(0, ("Failed to fork mdssd [%s], aborting ...\n",
638 /* parent or error */
641 /* Re-enable SIGHUP before returnig */
642 BlockSignals(false, SIGTERM);
643 BlockSignals(false, SIGHUP);
648 status = smbd_reinit_after_fork(msg_ctx, ev_ctx, true, "mdssd-master");
649 if (!NT_STATUS_IS_OK(status)) {
650 DEBUG(0,("reinit_after_fork() failed\n"));
651 smb_panic("reinit_after_fork() failed");
656 /* save the parent process id so the children can use it later */
657 parent_id = messaging_server_id(msg_ctx);
659 pfh_daemon_config(DAEMON_NAME,
661 &default_pf_mdssd_cfg);
663 mdssd_setup_sig_term_handler(ev_ctx);
664 mdssd_setup_sig_hup_handler(ev_ctx);
666 BlockSignals(false, SIGTERM);
667 BlockSignals(false, SIGHUP);
669 ok = mdssd_create_sockets(ev_ctx, msg_ctx, listen_fd, &listen_fd_size);
674 /* start children before any more initialization is done */
675 ok = prefork_create_pool(ev_ctx, /* mem_ctx */
680 pf_mdssd_cfg.min_children,
681 pf_mdssd_cfg.max_children,
682 &mdssd_children_main,
689 if (!serverid_register(messaging_server_id(msg_ctx),
694 messaging_register(msg_ctx,
696 MSG_SMB_CONF_UPDATED,
697 mdssd_smb_conf_updated);
698 messaging_register(msg_ctx, ev_ctx,
699 MSG_PREFORK_CHILD_EVENT, child_ping);
701 ok = setup_rpc_module(ev_ctx, msg_ctx, "mdssvc");
706 ok = mdssd_setup_children_monitor(ev_ctx, msg_ctx);
711 DEBUG(1, ("mdssd Daemon Started (%u)\n", (unsigned int)getpid()));
714 rc = tevent_loop_wait(ev_ctx);
716 /* should not be reached */
717 DEBUG(0,("mdssd: tevent_loop_wait() exited with %d - %s\n",
718 rc, (rc == 0) ? "out of events" : strerror(errno)));