libdns: Add dns_cli_request
[Samba.git] / source3 / rpc_server / mdssd.c
blobd5c05c7e8e91bef85229d8ea655ed19fbafeb1e4
1 /*
2 * Unix SMB/CIFS implementation.
4 * mds service daemon
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/>.
22 #include "includes.h"
23 #include "messages.h"
24 #include "ntdomain.h"
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"
41 #undef DBGC_CLASS
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,
53 .min_children = 5,
54 .max_children = 25,
55 .spawn_rate = 5,
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,
65 void *private_data,
66 uint32_t msg_type,
67 struct server_id server_id,
68 DATA_BLOB *data)
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());
78 reopen_logs();
79 if (mdssd_child_id == 0) {
80 pfh_daemon_config(DAEMON_NAME,
81 &pf_mdssd_cfg,
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,
89 int signum,
90 int count,
91 void *siginfo,
92 void *private_data)
94 shutdown_rpc_module("mdssvc");
96 DEBUG(0, ("termination signal\n"));
97 exit(0);
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,
105 ev_ctx,
106 SIGTERM, 0,
107 mdssd_sig_term_handler,
108 NULL);
109 if (!se) {
110 DEBUG(0, ("failed to setup SIGTERM handler\n"));
111 exit(1);
115 static void mdssd_sig_hup_handler(struct tevent_context *ev,
116 struct tevent_signal *se,
117 int signum,
118 int count,
119 void *siginfo,
120 void *pvt)
123 change_to_root_user();
124 lp_load_global(get_dyn_CONFIGFILE());
126 reopen_logs();
127 pfh_daemon_config(DAEMON_NAME,
128 &pf_mdssd_cfg,
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,
140 ev_ctx,
141 SIGHUP, 0,
142 mdssd_sig_hup_handler,
143 NULL);
144 if (!se) {
145 DEBUG(0, ("failed to setup SIGHUP handler\n"));
146 exit(1);
150 /**********************************************************
151 * Children
152 **********************************************************/
154 static void mdssd_chld_sig_hup_handler(struct tevent_context *ev,
155 struct tevent_signal *se,
156 int signum,
157 int count,
158 void *siginfo,
159 void *pvt)
161 change_to_root_user();
162 reopen_logs();
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,
170 ev_ctx,
171 SIGHUP, 0,
172 mdssd_chld_sig_hup_handler,
173 NULL);
174 if (!se) {
175 DEBUG(1, ("failed to setup SIGHUP handler"));
176 return false;
179 return true;
182 static void parent_ping(struct messaging_context *msg_ctx,
183 void *private_data,
184 uint32_t msg_type,
185 struct server_id server_id,
186 DATA_BLOB *data)
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"));
195 return;
198 static bool mdssd_child_init(struct tevent_context *ev_ctx,
199 int child_id,
200 struct pf_worker_data *pf)
202 NTSTATUS status;
203 struct messaging_context *msg_ctx = server_messaging_context();
204 bool ok;
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;
214 reopen_logs();
216 ok = mdssd_setup_chld_hup_handler(ev_ctx);
217 if (!ok) {
218 return false;
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);
227 if (!ok) {
228 DBG_ERR("Failed to de-intialize RPC\n");
229 return false;
232 return true;
235 struct mdssd_children_data {
236 struct tevent_context *ev_ctx;
237 struct messaging_context *msg_ctx;
238 struct pf_worker_data *pf;
239 int listen_fd_size;
240 int *listen_fds;
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,
248 int child_id,
249 int listen_fd_size,
250 int *listen_fds,
251 void *private_data)
253 struct mdssd_children_data *data;
254 bool ok;
255 int ret = 0;
257 ok = mdssd_child_init(ev_ctx, child_id, pf);
258 if (!ok) {
259 return 1;
262 data = talloc(ev_ctx, struct mdssd_children_data);
263 if (!data) {
264 return 1;
266 data->pf = pf;
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);
278 if (ret != 0) {
279 DEBUG(0, ("tevent_loop_once() exited with %d: %s\n",
280 ret, strerror(errno)));
281 pf->status = PF_WORKER_EXITING;
285 return ret;
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 */
316 return;
319 next = talloc_zero(data, struct mdssd_new_client);
320 if (!next) {
321 DEBUG(1, ("Out of memory!?\n"));
322 return;
324 next->data = data;
326 req = prefork_listen_send(next,
327 data->ev_ctx,
328 data->pf,
329 data->listen_fd_size,
330 data->listen_fds);
331 if (!req) {
332 DEBUG(1, ("Failed to make listening request!?\n"));
333 talloc_free(next);
334 return;
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;
344 int rc;
345 int sd;
346 TALLOC_CTX *tmp_ctx;
347 struct tsocket_address *srv_addr;
348 struct tsocket_address *cli_addr;
350 client = tevent_req_callback_data(req, struct mdssd_new_client);
351 data = client->data;
353 tmp_ctx = talloc_stackframe();
354 if (tmp_ctx == NULL) {
355 DEBUG(1, ("Failed to allocate stackframe!\n"));
356 return;
359 rc = prefork_listen_recv(req,
360 tmp_ctx,
361 &sd,
362 &srv_addr,
363 &cli_addr);
365 /* this will free the request too */
366 talloc_free(client);
368 if (rc != 0) {
369 DEBUG(6, ("No client connection was available after all!\n"));
370 goto done;
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,
386 data->msg_ctx,
387 NCACN_IP_TCP,
388 "IP",
389 cli_addr,
390 srv_addr,
392 NULL);
393 } else if (tsocket_address_is_unix(srv_addr)) {
394 const char *p;
395 const char *b;
397 p = tsocket_address_unix_path(srv_addr, tmp_ctx);
398 if (p == NULL) {
399 talloc_free(tmp_ctx);
400 return;
403 b = strrchr(p, '/');
404 if (b != NULL) {
405 b++;
406 } else {
407 b = p;
410 if (strstr(p, "/np/")) {
411 named_pipe_accept_function(data->ev_ctx,
412 data->msg_ctx,
415 mdssd_client_terminated,
416 data);
417 } else {
418 dcerpc_ncacn_accept(data->ev_ctx,
419 data->msg_ctx,
420 NCALRPC,
422 cli_addr,
423 srv_addr,
425 NULL);
427 } else {
428 DEBUG(0, ("ERROR: Unsupported socket!\n"));
431 done:
432 talloc_free(tmp_ctx);
436 * MAIN
439 static void child_ping(struct messaging_context *msg_ctx,
440 void *private_data,
441 uint32_t msg_type,
442 struct server_id server_id,
443 DATA_BLOB *data)
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,
460 void *pvt);
462 static void mdssd_sigchld_handler(struct tevent_context *ev_ctx,
463 struct prefork_pool *pfp,
464 void *pvt)
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)
478 bool ok;
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());
485 return ok;
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);
502 if (!te) {
503 DEBUG(2, ("Failed to set up children monitoring!\n"));
504 return false;
507 return true;
510 static void mdssd_check_children(struct tevent_context *ev_ctx,
511 struct tevent_timer *te,
512 struct timeval current_time,
513 void *pvt)
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);
525 * start it up
528 static bool mdssd_create_sockets(struct tevent_context *ev_ctx,
529 struct messaging_context *msg_ctx,
530 int *listen_fd,
531 int *listen_fd_size)
533 struct dcerpc_binding_vector *v, *v_orig;
534 TALLOC_CTX *tmp_ctx;
535 NTSTATUS status;
536 int fd = -1;
537 int rc;
538 bool ok = false;
540 tmp_ctx = talloc_stackframe();
541 if (tmp_ctx == NULL) {
542 return false;
545 status = dcerpc_binding_vector_new(tmp_ctx, &v_orig);
546 if (!NT_STATUS_IS_OK(status)) {
547 goto done;
550 /* mdssvc */
551 fd = create_named_pipe_socket("mdssvc");
552 if (fd < 0) {
553 goto done;
556 rc = listen(fd, pf_mdssd_cfg.max_allowed_clients);
557 if (rc == -1) {
558 goto done;
560 listen_fd[*listen_fd_size] = fd;
561 (*listen_fd_size)++;
563 fd = create_dcerpc_ncalrpc_socket("mdssvc");
564 if (fd < 0) {
565 goto done;
568 rc = listen(fd, pf_mdssd_cfg.max_allowed_clients);
569 if (rc == -1) {
570 goto done;
572 listen_fd[*listen_fd_size] = fd;
573 (*listen_fd_size)++;
574 fd = -1;
576 v = dcerpc_binding_vector_dup(tmp_ctx, v_orig);
577 if (v == NULL) {
578 goto done;
581 status = dcerpc_binding_vector_replace_iface(&ndr_table_mdssvc, v);
582 if (!NT_STATUS_IS_OK(status)) {
583 goto done;
586 status = dcerpc_binding_vector_add_np_default(&ndr_table_mdssvc, v);
587 if (!NT_STATUS_IS_OK(status)) {
588 goto done;
591 status = dcerpc_binding_vector_add_unix(&ndr_table_mdssvc, v, "mdssvc");
592 if (!NT_STATUS_IS_OK(status)) {
593 goto done;
596 ok = true;
597 done:
598 if (fd != -1) {
599 close(fd);
601 talloc_free(tmp_ctx);
602 return ok;
605 void start_mdssd(struct tevent_context *ev_ctx,
606 struct messaging_context *msg_ctx)
608 NTSTATUS status;
609 int listen_fd[MDSSD_MAX_SOCKETS];
610 int listen_fd_size = 0;
611 pid_t pid;
612 int rc;
613 bool ok;
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);
625 pid = fork();
626 if (pid == -1) {
627 DEBUG(0, ("Failed to fork mdssd [%s], aborting ...\n",
628 strerror(errno)));
629 exit(1);
632 /* parent or error */
633 if (pid != 0) {
635 /* Re-enable SIGHUP before returnig */
636 BlockSignals(false, SIGTERM);
637 BlockSignals(false, SIGHUP);
639 return;
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");
648 reopen_logs();
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,
654 &pf_mdssd_cfg,
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);
664 if (!ok) {
665 exit(1);
668 /* start children before any more initialization is done */
669 ok = prefork_create_pool(ev_ctx, /* mem_ctx */
670 ev_ctx,
671 msg_ctx,
672 listen_fd_size,
673 listen_fd,
674 pf_mdssd_cfg.min_children,
675 pf_mdssd_cfg.max_children,
676 &mdssd_children_main,
677 NULL,
678 &mdssd_pool);
679 if (!ok) {
680 exit(1);
683 messaging_register(msg_ctx,
684 ev_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");
691 if (!ok) {
692 exit(1);
695 ok = mdssd_setup_children_monitor(ev_ctx, msg_ctx);
696 if (!ok) {
697 exit(1);
700 DEBUG(1, ("mdssd Daemon Started (%u)\n", (unsigned int)getpid()));
702 /* loop forever */
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)));
708 exit(1);