s3:smbd: use smbd_reinit_after_fork
[Samba.git] / source3 / rpc_server / lsasd.c
blobd287bff89ee1903a8804972b7dd89c3e59108e32
1 /*
2 * Unix SMB/CIFS implementation.
4 * LSA service daemon
6 * Copyright (c) 2011 Andreas Schneider <asn@samba.org>
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 "serverid.h"
24 #include "messages.h"
25 #include "ntdomain.h"
27 #include "lib/util/util_process.h"
29 #include "lib/id_cache.h"
31 #include "../lib/tsocket/tsocket.h"
32 #include "lib/server_prefork.h"
33 #include "lib/server_prefork_util.h"
34 #include "librpc/rpc/dcerpc_ep.h"
36 #include "rpc_server/rpc_server.h"
37 #include "rpc_server/rpc_ep_register.h"
38 #include "rpc_server/rpc_sock_helper.h"
40 #include "librpc/gen_ndr/srv_lsa.h"
41 #include "librpc/gen_ndr/srv_samr.h"
42 #include "librpc/gen_ndr/srv_netlogon.h"
44 #define DAEMON_NAME "lsasd"
45 #define LSASD_MAX_SOCKETS 64
47 static struct server_id parent_id;
48 static struct prefork_pool *lsasd_pool = NULL;
49 static int lsasd_child_id = 0;
51 static struct pf_daemon_config default_pf_lsasd_cfg = {
52 .prefork_status = PFH_INIT,
53 .min_children = 5,
54 .max_children = 25,
55 .spawn_rate = 5,
56 .max_allowed_clients = 100,
57 .child_min_life = 60 /* 1 minute minimum life time */
59 static struct pf_daemon_config pf_lsasd_cfg = { 0 };
61 void start_lsasd(struct tevent_context *ev_ctx,
62 struct messaging_context *msg_ctx);
64 static void lsasd_reopen_logs(int child_id)
66 char *lfile = lp_logfile(talloc_tos());
67 char *extension;
68 int rc;
70 if (child_id) {
71 rc = asprintf(&extension, "%s.%d", DAEMON_NAME, child_id);
72 } else {
73 rc = asprintf(&extension, "%s", DAEMON_NAME);
75 if (rc == -1) {
76 return;
79 rc = 0;
80 if (lfile == NULL || lfile[0] == '\0') {
81 rc = asprintf(&lfile, "%s/log.%s",
82 get_dyn_LOGFILEBASE(), extension);
83 } else {
84 if (strstr(lfile, extension) == NULL) {
85 if (child_id) {
86 rc = asprintf(&lfile, "%s.%d",
87 lp_logfile(talloc_tos()),
88 child_id);
89 } else {
90 rc = asprintf(&lfile, "%s.%s",
91 lp_logfile(talloc_tos()),
92 extension);
97 if (rc > 0) {
98 lp_set_logfile(lfile);
99 SAFE_FREE(lfile);
102 SAFE_FREE(extension);
104 reopen_logs();
107 static void lsasd_smb_conf_updated(struct messaging_context *msg,
108 void *private_data,
109 uint32_t msg_type,
110 struct server_id server_id,
111 DATA_BLOB *data)
113 struct tevent_context *ev_ctx;
115 DEBUG(10, ("Got message saying smb.conf was updated. Reloading.\n"));
116 ev_ctx = talloc_get_type_abort(private_data, struct tevent_context);
118 change_to_root_user();
119 lp_load_global(get_dyn_CONFIGFILE());
121 lsasd_reopen_logs(lsasd_child_id);
122 if (lsasd_child_id == 0) {
123 pfh_daemon_config(DAEMON_NAME,
124 &pf_lsasd_cfg,
125 &default_pf_lsasd_cfg);
126 pfh_manage_pool(ev_ctx, msg, &pf_lsasd_cfg, lsasd_pool);
130 static void lsasd_sig_term_handler(struct tevent_context *ev,
131 struct tevent_signal *se,
132 int signum,
133 int count,
134 void *siginfo,
135 void *private_data)
137 rpc_netlogon_shutdown();
138 rpc_samr_shutdown();
139 rpc_lsarpc_shutdown();
141 DEBUG(0, ("termination signal\n"));
142 exit(0);
145 static void lsasd_setup_sig_term_handler(struct tevent_context *ev_ctx)
147 struct tevent_signal *se;
149 se = tevent_add_signal(ev_ctx,
150 ev_ctx,
151 SIGTERM, 0,
152 lsasd_sig_term_handler,
153 NULL);
154 if (!se) {
155 DEBUG(0, ("failed to setup SIGTERM handler\n"));
156 exit(1);
160 static void lsasd_sig_hup_handler(struct tevent_context *ev,
161 struct tevent_signal *se,
162 int signum,
163 int count,
164 void *siginfo,
165 void *pvt)
168 change_to_root_user();
169 lp_load_global(get_dyn_CONFIGFILE());
171 lsasd_reopen_logs(lsasd_child_id);
172 pfh_daemon_config(DAEMON_NAME,
173 &pf_lsasd_cfg,
174 &default_pf_lsasd_cfg);
176 /* relay to all children */
177 prefork_send_signal_to_all(lsasd_pool, SIGHUP);
180 static void lsasd_setup_sig_hup_handler(struct tevent_context *ev_ctx)
182 struct tevent_signal *se;
184 se = tevent_add_signal(ev_ctx,
185 ev_ctx,
186 SIGHUP, 0,
187 lsasd_sig_hup_handler,
188 NULL);
189 if (!se) {
190 DEBUG(0, ("failed to setup SIGHUP handler\n"));
191 exit(1);
195 /**********************************************************
196 * Children
197 **********************************************************/
199 static void lsasd_chld_sig_hup_handler(struct tevent_context *ev,
200 struct tevent_signal *se,
201 int signum,
202 int count,
203 void *siginfo,
204 void *pvt)
206 change_to_root_user();
207 lsasd_reopen_logs(lsasd_child_id);
210 static bool lsasd_setup_chld_hup_handler(struct tevent_context *ev_ctx)
212 struct tevent_signal *se;
214 se = tevent_add_signal(ev_ctx,
215 ev_ctx,
216 SIGHUP, 0,
217 lsasd_chld_sig_hup_handler,
218 NULL);
219 if (!se) {
220 DEBUG(1, ("failed to setup SIGHUP handler"));
221 return false;
224 return true;
227 static void parent_ping(struct messaging_context *msg_ctx,
228 void *private_data,
229 uint32_t msg_type,
230 struct server_id server_id,
231 DATA_BLOB *data)
234 /* The fact we received this message is enough to let make the event
235 * loop if it was idle. lsasd_children_main will cycle through
236 * lsasd_next_client at least once. That function will take whatever
237 * action is necessary */
239 DEBUG(10, ("Got message that the parent changed status.\n"));
240 return;
243 static bool lsasd_child_init(struct tevent_context *ev_ctx,
244 int child_id,
245 struct pf_worker_data *pf)
247 NTSTATUS status;
248 struct messaging_context *msg_ctx = server_messaging_context();
249 bool ok;
251 status = reinit_after_fork(msg_ctx, ev_ctx,
252 true);
253 if (!NT_STATUS_IS_OK(status)) {
254 DEBUG(0,("reinit_after_fork() failed\n"));
255 smb_panic("reinit_after_fork() failed");
258 prctl_set_comment("lsasd-child");
260 lsasd_child_id = child_id;
261 lsasd_reopen_logs(child_id);
263 ok = lsasd_setup_chld_hup_handler(ev_ctx);
264 if (!ok) {
265 return false;
268 if (!serverid_register(messaging_server_id(msg_ctx),
269 FLAG_MSG_GENERAL)) {
270 return false;
273 messaging_register(msg_ctx, ev_ctx,
274 MSG_SMB_CONF_UPDATED, lsasd_smb_conf_updated);
275 messaging_register(msg_ctx, ev_ctx,
276 MSG_PREFORK_PARENT_EVENT, parent_ping);
277 id_cache_register_msgs(msg_ctx);
279 status = rpc_lsarpc_init(NULL);
280 if (!NT_STATUS_IS_OK(status)) {
281 DEBUG(0, ("Failed to register lsarpc rpc inteface! (%s)\n",
282 nt_errstr(status)));
283 return false;
286 status = rpc_samr_init(NULL);
287 if (!NT_STATUS_IS_OK(status)) {
288 DEBUG(0, ("Failed to register samr rpc inteface! (%s)\n",
289 nt_errstr(status)));
290 return false;
293 status = rpc_netlogon_init(NULL);
294 if (!NT_STATUS_IS_OK(status)) {
295 DEBUG(0, ("Failed to register netlogon rpc inteface! (%s)\n",
296 nt_errstr(status)));
297 return false;
300 return true;
303 struct lsasd_children_data {
304 struct tevent_context *ev_ctx;
305 struct messaging_context *msg_ctx;
306 struct pf_worker_data *pf;
307 int listen_fd_size;
308 int *listen_fds;
311 static void lsasd_next_client(void *pvt);
313 static int lsasd_children_main(struct tevent_context *ev_ctx,
314 struct messaging_context *msg_ctx,
315 struct pf_worker_data *pf,
316 int child_id,
317 int listen_fd_size,
318 int *listen_fds,
319 void *private_data)
321 struct lsasd_children_data *data;
322 bool ok;
323 int ret = 0;
325 ok = lsasd_child_init(ev_ctx, child_id, pf);
326 if (!ok) {
327 return 1;
330 data = talloc(ev_ctx, struct lsasd_children_data);
331 if (!data) {
332 return 1;
334 data->pf = pf;
335 data->ev_ctx = ev_ctx;
336 data->msg_ctx = msg_ctx;
337 data->listen_fd_size = listen_fd_size;
338 data->listen_fds = listen_fds;
340 /* loop until it is time to exit */
341 while (pf->status != PF_WORKER_EXITING) {
342 /* try to see if it is time to schedule the next client */
343 lsasd_next_client(data);
345 ret = tevent_loop_once(ev_ctx);
346 if (ret != 0) {
347 DEBUG(0, ("tevent_loop_once() exited with %d: %s\n",
348 ret, strerror(errno)));
349 pf->status = PF_WORKER_EXITING;
353 return ret;
356 static void lsasd_client_terminated(void *pvt)
358 struct lsasd_children_data *data;
360 data = talloc_get_type_abort(pvt, struct lsasd_children_data);
362 pfh_client_terminated(data->pf);
364 lsasd_next_client(pvt);
367 struct lsasd_new_client {
368 struct lsasd_children_data *data;
371 static void lsasd_handle_client(struct tevent_req *req);
373 static void lsasd_next_client(void *pvt)
375 struct tevent_req *req;
376 struct lsasd_children_data *data;
377 struct lsasd_new_client *next;
379 data = talloc_get_type_abort(pvt, struct lsasd_children_data);
381 if (!pfh_child_allowed_to_accept(data->pf)) {
382 /* nothing to do for now we are already listening
383 * or we are not allowed to listen further */
384 return;
387 next = talloc_zero(data, struct lsasd_new_client);
388 if (!next) {
389 DEBUG(1, ("Out of memory!?\n"));
390 return;
392 next->data = data;
394 req = prefork_listen_send(next,
395 data->ev_ctx,
396 data->pf,
397 data->listen_fd_size,
398 data->listen_fds);
399 if (!req) {
400 DEBUG(1, ("Failed to make listening request!?\n"));
401 talloc_free(next);
402 return;
404 tevent_req_set_callback(req, lsasd_handle_client, next);
407 static void lsasd_handle_client(struct tevent_req *req)
409 struct lsasd_children_data *data;
410 struct lsasd_new_client *client;
411 const DATA_BLOB ping = data_blob_null;
412 int rc;
413 int sd;
414 TALLOC_CTX *tmp_ctx;
415 struct tsocket_address *srv_addr;
416 struct tsocket_address *cli_addr;
418 client = tevent_req_callback_data(req, struct lsasd_new_client);
419 data = client->data;
421 tmp_ctx = talloc_stackframe();
422 if (tmp_ctx == NULL) {
423 DEBUG(1, ("Failed to allocate stackframe!\n"));
424 return;
427 rc = prefork_listen_recv(req,
428 tmp_ctx,
429 &sd,
430 &srv_addr,
431 &cli_addr);
433 /* this will free the request too */
434 talloc_free(client);
436 if (rc != 0) {
437 DEBUG(6, ("No client connection was available after all!\n"));
438 goto done;
441 /* Warn parent that our status changed */
442 messaging_send(data->msg_ctx, parent_id,
443 MSG_PREFORK_CHILD_EVENT, &ping);
445 DEBUG(2, ("LSASD preforked child %d got client connection!\n",
446 (int)(data->pf->pid)));
448 if (tsocket_address_is_inet(srv_addr, "ip")) {
449 DEBUG(3, ("Got a tcpip client connection from %s on inteface %s\n",
450 tsocket_address_string(cli_addr, tmp_ctx),
451 tsocket_address_string(srv_addr, tmp_ctx)));
453 dcerpc_ncacn_accept(data->ev_ctx,
454 data->msg_ctx,
455 NCACN_IP_TCP,
456 "IP",
457 cli_addr,
458 srv_addr,
460 NULL);
461 } else if (tsocket_address_is_unix(srv_addr)) {
462 const char *p;
463 const char *b;
465 p = tsocket_address_unix_path(srv_addr, tmp_ctx);
466 if (p == NULL) {
467 talloc_free(tmp_ctx);
468 return;
471 b = strrchr(p, '/');
472 if (b != NULL) {
473 b++;
474 } else {
475 b = p;
478 if (strstr(p, "/np/")) {
479 named_pipe_accept_function(data->ev_ctx,
480 data->msg_ctx,
483 lsasd_client_terminated,
484 data);
485 } else {
486 dcerpc_ncacn_accept(data->ev_ctx,
487 data->msg_ctx,
488 NCALRPC,
490 cli_addr,
491 srv_addr,
493 NULL);
495 } else {
496 DEBUG(0, ("ERROR: Unsupported socket!\n"));
499 done:
500 talloc_free(tmp_ctx);
504 * MAIN
507 static void child_ping(struct messaging_context *msg_ctx,
508 void *private_data,
509 uint32_t msg_type,
510 struct server_id server_id,
511 DATA_BLOB *data)
513 struct tevent_context *ev_ctx;
515 ev_ctx = talloc_get_type_abort(private_data, struct tevent_context);
517 DEBUG(10, ("Got message that a child changed status.\n"));
518 pfh_manage_pool(ev_ctx, msg_ctx, &pf_lsasd_cfg, lsasd_pool);
521 static bool lsasd_schedule_check(struct tevent_context *ev_ctx,
522 struct messaging_context *msg_ctx,
523 struct timeval current_time);
525 static void lsasd_check_children(struct tevent_context *ev_ctx,
526 struct tevent_timer *te,
527 struct timeval current_time,
528 void *pvt);
530 static void lsasd_sigchld_handler(struct tevent_context *ev_ctx,
531 struct prefork_pool *pfp,
532 void *pvt)
534 struct messaging_context *msg_ctx;
536 msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);
538 /* run pool management so we can fork/retire or increase
539 * the allowed connections per child based on load */
540 pfh_manage_pool(ev_ctx, msg_ctx, &pf_lsasd_cfg, lsasd_pool);
543 static bool lsasd_setup_children_monitor(struct tevent_context *ev_ctx,
544 struct messaging_context *msg_ctx)
546 bool ok;
548 /* add our oun sigchld callback */
549 prefork_set_sigchld_callback(lsasd_pool, lsasd_sigchld_handler, msg_ctx);
551 ok = lsasd_schedule_check(ev_ctx, msg_ctx, tevent_timeval_current());
553 return ok;
556 static bool lsasd_schedule_check(struct tevent_context *ev_ctx,
557 struct messaging_context *msg_ctx,
558 struct timeval current_time)
560 struct tevent_timer *te;
561 struct timeval next_event;
563 /* check situation again in 10 seconds */
564 next_event = tevent_timeval_current_ofs(10, 0);
566 /* TODO: check when the socket becomes readable, so that children
567 * are checked only when there is some activity ? */
568 te = tevent_add_timer(ev_ctx, lsasd_pool, next_event,
569 lsasd_check_children, msg_ctx);
570 if (!te) {
571 DEBUG(2, ("Failed to set up children monitoring!\n"));
572 return false;
575 return true;
578 static void lsasd_check_children(struct tevent_context *ev_ctx,
579 struct tevent_timer *te,
580 struct timeval current_time,
581 void *pvt)
583 struct messaging_context *msg_ctx;
585 msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);
587 pfh_manage_pool(ev_ctx, msg_ctx, &pf_lsasd_cfg, lsasd_pool);
589 lsasd_schedule_check(ev_ctx, msg_ctx, current_time);
593 * start it up
596 static bool lsasd_create_sockets(struct tevent_context *ev_ctx,
597 struct messaging_context *msg_ctx,
598 int *listen_fd,
599 int *listen_fd_size)
601 struct dcerpc_binding_vector *v, *v_orig;
602 TALLOC_CTX *tmp_ctx;
603 NTSTATUS status;
604 uint32_t i;
605 int fd = -1;
606 int rc;
607 bool ok = false;
609 tmp_ctx = talloc_stackframe();
610 if (tmp_ctx == NULL) {
611 return false;
614 status = dcerpc_binding_vector_new(tmp_ctx, &v_orig);
615 if (!NT_STATUS_IS_OK(status)) {
616 goto done;
619 /* Create only one tcpip listener for all services */
620 status = rpc_create_tcpip_sockets(&ndr_table_lsarpc,
621 v_orig,
623 listen_fd,
624 listen_fd_size);
625 if (!NT_STATUS_IS_OK(status)) {
626 goto done;
629 /* Start to listen on tcpip sockets */
630 for (i = 0; i < *listen_fd_size; i++) {
631 rc = listen(listen_fd[i], pf_lsasd_cfg.max_allowed_clients);
632 if (rc == -1) {
633 DEBUG(0, ("Failed to listen on tcpip socket - %s\n",
634 strerror(errno)));
635 goto done;
639 /* LSARPC */
640 fd = create_named_pipe_socket("lsarpc");
641 if (fd < 0) {
642 goto done;
645 rc = listen(fd, pf_lsasd_cfg.max_allowed_clients);
646 if (rc == -1) {
647 DEBUG(0, ("Failed to listen on lsarpc pipe - %s\n",
648 strerror(errno)));
649 goto done;
651 listen_fd[*listen_fd_size] = fd;
652 (*listen_fd_size)++;
654 fd = create_named_pipe_socket("lsass");
655 if (fd < 0) {
656 goto done;
659 rc = listen(fd, pf_lsasd_cfg.max_allowed_clients);
660 if (rc == -1) {
661 DEBUG(0, ("Failed to listen on lsass pipe - %s\n",
662 strerror(errno)));
663 goto done;
665 listen_fd[*listen_fd_size] = fd;
666 (*listen_fd_size)++;
668 fd = create_dcerpc_ncalrpc_socket("lsarpc");
669 if (fd < 0) {
670 goto done;
673 rc = listen(fd, pf_lsasd_cfg.max_allowed_clients);
674 if (rc == -1) {
675 DEBUG(0, ("Failed to listen on lsarpc ncalrpc - %s\n",
676 strerror(errno)));
677 goto done;
679 listen_fd[*listen_fd_size] = fd;
680 (*listen_fd_size)++;
681 fd = -1;
683 v = dcerpc_binding_vector_dup(tmp_ctx, v_orig);
684 if (v == NULL) {
685 goto done;
688 status = dcerpc_binding_vector_replace_iface(&ndr_table_lsarpc, v);
689 if (!NT_STATUS_IS_OK(status)) {
690 goto done;
693 status = dcerpc_binding_vector_add_np_default(&ndr_table_lsarpc, v);
694 if (!NT_STATUS_IS_OK(status)) {
695 goto done;
698 status = dcerpc_binding_vector_add_unix(&ndr_table_lsarpc, v, "lsarpc");
699 if (!NT_STATUS_IS_OK(status)) {
700 goto done;
703 status = rpc_ep_register(ev_ctx, msg_ctx, &ndr_table_lsarpc, v);
704 if (!NT_STATUS_IS_OK(status)) {
705 goto done;
708 /* SAMR */
709 fd = create_named_pipe_socket("samr");
710 if (fd < 0) {
711 goto done;
714 rc = listen(fd, pf_lsasd_cfg.max_allowed_clients);
715 if (rc == -1) {
716 DEBUG(0, ("Failed to listen on samr pipe - %s\n",
717 strerror(errno)));
718 goto done;
720 listen_fd[*listen_fd_size] = fd;
721 (*listen_fd_size)++;
723 fd = create_dcerpc_ncalrpc_socket("samr");
724 if (fd < 0) {
725 goto done;
728 rc = listen(fd, pf_lsasd_cfg.max_allowed_clients);
729 if (rc == -1) {
730 DEBUG(0, ("Failed to listen on samr ncalrpc - %s\n",
731 strerror(errno)));
732 goto done;
734 listen_fd[*listen_fd_size] = fd;
735 (*listen_fd_size)++;
736 fd = -1;
738 v = dcerpc_binding_vector_dup(tmp_ctx, v_orig);
739 if (v == NULL) {
740 goto done;
743 status = dcerpc_binding_vector_replace_iface(&ndr_table_samr, v);
744 if (!NT_STATUS_IS_OK(status)) {
745 goto done;
748 status = dcerpc_binding_vector_add_np_default(&ndr_table_samr, v);
749 if (!NT_STATUS_IS_OK(status)) {
750 goto done;
753 status = dcerpc_binding_vector_add_unix(&ndr_table_lsarpc, v, "samr");
754 if (!NT_STATUS_IS_OK(status)) {
755 goto done;
758 status = rpc_ep_register(ev_ctx, msg_ctx, &ndr_table_samr, v);
759 if (!NT_STATUS_IS_OK(status)) {
760 goto done;
763 /* NETLOGON */
764 fd = create_named_pipe_socket("netlogon");
765 if (fd < 0) {
766 goto done;
769 rc = listen(fd, pf_lsasd_cfg.max_allowed_clients);
770 if (rc == -1) {
771 DEBUG(0, ("Failed to listen on samr pipe - %s\n",
772 strerror(errno)));
773 goto done;
775 listen_fd[*listen_fd_size] = fd;
776 (*listen_fd_size)++;
778 fd = create_dcerpc_ncalrpc_socket("netlogon");
779 if (fd < 0) {
780 goto done;
783 rc = listen(fd, pf_lsasd_cfg.max_allowed_clients);
784 if (rc == -1) {
785 DEBUG(0, ("Failed to listen on netlogon ncalrpc - %s\n",
786 strerror(errno)));
787 goto done;
789 listen_fd[*listen_fd_size] = fd;
790 (*listen_fd_size)++;
791 fd = -1;
793 v = dcerpc_binding_vector_dup(tmp_ctx, v_orig);
794 if (v == NULL) {
795 goto done;
798 status = dcerpc_binding_vector_replace_iface(&ndr_table_netlogon, v);
799 if (!NT_STATUS_IS_OK(status)) {
800 goto done;
803 status = dcerpc_binding_vector_add_np_default(&ndr_table_netlogon, v);
804 if (!NT_STATUS_IS_OK(status)) {
805 goto done;
808 status = dcerpc_binding_vector_add_unix(&ndr_table_lsarpc, v, "netlogon");
809 if (!NT_STATUS_IS_OK(status)) {
810 goto done;
813 status = rpc_ep_register(ev_ctx, msg_ctx, &ndr_table_netlogon, v);
814 if (!NT_STATUS_IS_OK(status)) {
815 goto done;
818 ok = true;
819 done:
820 if (fd != -1) {
821 close(fd);
823 talloc_free(tmp_ctx);
824 return ok;
827 void start_lsasd(struct tevent_context *ev_ctx,
828 struct messaging_context *msg_ctx)
830 NTSTATUS status;
831 int listen_fd[LSASD_MAX_SOCKETS];
832 int listen_fd_size = 0;
833 pid_t pid;
834 int rc;
835 bool ok;
837 DEBUG(1, ("Forking LSA Service Daemon\n"));
840 * Block signals before forking child as it will have to
841 * set its own handlers. Child will re-enable SIGHUP as
842 * soon as the handlers are set up.
844 BlockSignals(true, SIGTERM);
845 BlockSignals(true, SIGHUP);
847 pid = fork();
848 if (pid == -1) {
849 DEBUG(0, ("Failed to fork LSASD [%s], aborting ...\n",
850 strerror(errno)));
851 exit(1);
854 /* parent or error */
855 if (pid != 0) {
857 /* Re-enable SIGHUP before returnig */
858 BlockSignals(false, SIGTERM);
859 BlockSignals(false, SIGHUP);
861 return;
864 status = smbd_reinit_after_fork(msg_ctx, ev_ctx, true);
865 if (!NT_STATUS_IS_OK(status)) {
866 DEBUG(0,("reinit_after_fork() failed\n"));
867 smb_panic("reinit_after_fork() failed");
870 prctl_set_comment("lsasd-master");
872 /* save the parent process id so the children can use it later */
873 parent_id = messaging_server_id(msg_ctx);
875 lsasd_reopen_logs(0);
876 pfh_daemon_config(DAEMON_NAME,
877 &pf_lsasd_cfg,
878 &default_pf_lsasd_cfg);
880 lsasd_setup_sig_term_handler(ev_ctx);
881 lsasd_setup_sig_hup_handler(ev_ctx);
883 BlockSignals(false, SIGTERM);
884 BlockSignals(false, SIGHUP);
886 ok = lsasd_create_sockets(ev_ctx, msg_ctx, listen_fd, &listen_fd_size);
887 if (!ok) {
888 exit(1);
891 /* start children before any more initialization is done */
892 ok = prefork_create_pool(ev_ctx, /* mem_ctx */
893 ev_ctx,
894 msg_ctx,
895 listen_fd_size,
896 listen_fd,
897 pf_lsasd_cfg.min_children,
898 pf_lsasd_cfg.max_children,
899 &lsasd_children_main,
900 NULL,
901 &lsasd_pool);
902 if (!ok) {
903 exit(1);
906 if (!serverid_register(messaging_server_id(msg_ctx),
907 FLAG_MSG_GENERAL)) {
908 exit(1);
911 messaging_register(msg_ctx,
912 ev_ctx,
913 MSG_SMB_CONF_UPDATED,
914 lsasd_smb_conf_updated);
915 messaging_register(msg_ctx, ev_ctx,
916 MSG_PREFORK_CHILD_EVENT, child_ping);
918 status = rpc_lsarpc_init(NULL);
919 if (!NT_STATUS_IS_OK(status)) {
920 DEBUG(0, ("Failed to register lsarpc rpc inteface in lsasd! (%s)\n",
921 nt_errstr(status)));
922 exit(1);
925 status = rpc_samr_init(NULL);
926 if (!NT_STATUS_IS_OK(status)) {
927 DEBUG(0, ("Failed to register samr rpc inteface in lsasd! (%s)\n",
928 nt_errstr(status)));
929 exit(1);
932 status = rpc_netlogon_init(NULL);
933 if (!NT_STATUS_IS_OK(status)) {
934 DEBUG(0, ("Failed to register netlogon rpc inteface in lsasd! (%s)\n",
935 nt_errstr(status)));
936 exit(1);
939 ok = lsasd_setup_children_monitor(ev_ctx, msg_ctx);
940 if (!ok) {
941 DEBUG(0, ("Failed to setup children monitoring!\n"));
942 exit(1);
945 DEBUG(1, ("LSASD Daemon Started (%u)\n", (unsigned int)getpid()));
947 /* loop forever */
948 rc = tevent_loop_wait(ev_ctx);
950 /* should not be reached */
951 DEBUG(0,("lsasd: tevent_loop_wait() exited with %d - %s\n",
952 rc, (rc == 0) ? "out of events" : strerror(errno)));
953 exit(1);