s3-lsasd: Use prefrok utils to manage children
[amitay/samba.git] / source3 / rpc_server / lsasd.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *
4  *  LSA service daemon
5  *
6  *  Copyright (c) 2011      Andreas Schneider <asn@samba.org>
7  *
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.
12  *
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.
17  *
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/>.
20  */
21
22 #include "includes.h"
23 #include "serverid.h"
24 #include "messages.h"
25 #include "ntdomain.h"
26
27 #include "../lib/tsocket/tsocket.h"
28 #include "lib/server_prefork.h"
29 #include "lib/server_prefork_util.h"
30 #include "librpc/rpc/dcerpc_ep.h"
31
32 #include "rpc_server/rpc_server.h"
33 #include "rpc_server/rpc_ep_register.h"
34 #include "rpc_server/rpc_sock_helper.h"
35
36 #include "librpc/gen_ndr/srv_lsa.h"
37 #include "librpc/gen_ndr/srv_samr.h"
38 #include "librpc/gen_ndr/srv_netlogon.h"
39
40 #define DAEMON_NAME "lsasd"
41 #define LSASD_MAX_SOCKETS 64
42
43 static struct prefork_pool *lsasd_pool = NULL;
44 static int lsasd_child_id = 0;
45
46 static struct pf_daemon_config default_pf_lsasd_cfg = {
47         .prefork_status = PFH_INIT,
48         .min_children = 5,
49         .max_children = 25,
50         .spawn_rate = 5,
51         .max_allowed_clients = 100,
52         .child_min_life = 60 /* 1 minute minimum life time */
53 };
54 static struct pf_daemon_config pf_lsasd_cfg = { 0 };
55
56 void start_lsasd(struct tevent_context *ev_ctx,
57                  struct messaging_context *msg_ctx);
58
59 static void lsasd_reopen_logs(int child_id)
60 {
61         char *lfile = lp_logfile();
62         char *extension;
63         int rc;
64
65         if (child_id) {
66                 rc = asprintf(&extension, "%s.%d", DAEMON_NAME, child_id);
67         } else {
68                 rc = asprintf(&extension, "%s", DAEMON_NAME);
69         }
70         if (rc == -1) {
71                 return;
72         }
73
74         rc = 0;
75         if (lfile == NULL || lfile[0] == '\0') {
76                 rc = asprintf(&lfile, "%s/log.%s",
77                               get_dyn_LOGFILEBASE(), extension);
78         } else {
79                 if (strstr(lfile, extension) == NULL) {
80                         if (child_id) {
81                                 rc = asprintf(&lfile, "%s.%d",
82                                                 lp_logfile(),
83                                                 child_id);
84                         } else {
85                                 rc = asprintf(&lfile, "%s.%s",
86                                                 lp_logfile(),
87                                                 extension);
88                         }
89                 }
90         }
91
92         if (rc > 0) {
93                 lp_set_logfile(lfile);
94                 SAFE_FREE(lfile);
95         }
96
97         SAFE_FREE(extension);
98
99         reopen_logs();
100 }
101
102 static void lsasd_smb_conf_updated(struct messaging_context *msg,
103                                   void *private_data,
104                                   uint32_t msg_type,
105                                   struct server_id server_id,
106                                   DATA_BLOB *data)
107 {
108         struct tevent_context *ev_ctx;
109
110         DEBUG(10, ("Got message saying smb.conf was updated. Reloading.\n"));
111         ev_ctx = talloc_get_type_abort(private_data, struct tevent_context);
112
113         change_to_root_user();
114         lp_load(get_dyn_CONFIGFILE(), true, false, false, true);
115
116         lsasd_reopen_logs(lsasd_child_id);
117         if (lsasd_child_id == 0) {
118                 pfh_daemon_config(DAEMON_NAME,
119                                   &pf_lsasd_cfg,
120                                   &default_pf_lsasd_cfg);
121                 pfh_manage_pool(ev_ctx, msg, &pf_lsasd_cfg, lsasd_pool);
122         }
123 }
124
125 static void lsasd_sig_term_handler(struct tevent_context *ev,
126                                   struct tevent_signal *se,
127                                   int signum,
128                                   int count,
129                                   void *siginfo,
130                                   void *private_data)
131 {
132         rpc_netlogon_shutdown();
133         rpc_samr_shutdown();
134         rpc_lsarpc_shutdown();
135
136         DEBUG(0, ("termination signal\n"));
137         exit(0);
138 }
139
140 static void lsasd_setup_sig_term_handler(struct tevent_context *ev_ctx)
141 {
142         struct tevent_signal *se;
143
144         se = tevent_add_signal(ev_ctx,
145                                ev_ctx,
146                                SIGTERM, 0,
147                                lsasd_sig_term_handler,
148                                NULL);
149         if (!se) {
150                 DEBUG(0, ("failed to setup SIGTERM handler\n"));
151                 exit(1);
152         }
153 }
154
155 static void lsasd_sig_hup_handler(struct tevent_context *ev,
156                                     struct tevent_signal *se,
157                                     int signum,
158                                     int count,
159                                     void *siginfo,
160                                     void *pvt)
161 {
162
163         change_to_root_user();
164         lp_load(get_dyn_CONFIGFILE(), true, false, false, true);
165
166         lsasd_reopen_logs(lsasd_child_id);
167         pfh_daemon_config(DAEMON_NAME,
168                           &pf_lsasd_cfg,
169                           &default_pf_lsasd_cfg);
170
171         /* relay to all children */
172         prefork_send_signal_to_all(lsasd_pool, SIGHUP);
173 }
174
175 static void lsasd_setup_sig_hup_handler(struct tevent_context *ev_ctx)
176 {
177         struct tevent_signal *se;
178
179         se = tevent_add_signal(ev_ctx,
180                                ev_ctx,
181                                SIGHUP, 0,
182                                lsasd_sig_hup_handler,
183                                NULL);
184         if (!se) {
185                 DEBUG(0, ("failed to setup SIGHUP handler\n"));
186                 exit(1);
187         }
188 }
189
190 /**********************************************************
191  * Children
192  **********************************************************/
193
194 struct lsasd_chld_sig_hup_ctx {
195         struct messaging_context *msg_ctx;
196         struct pf_worker_data *pf;
197 };
198
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)
205 {
206         struct pf_worker_data *pf = (struct pf_worker_data *)pvt;
207
208         /* avoid wasting CPU cycles if we are going to exit soon anyways */
209         if (pf->cmds == PF_SRV_MSG_EXIT) {
210                 return;
211         }
212
213         change_to_root_user();
214         lsasd_reopen_logs(lsasd_child_id);
215 }
216
217 static bool lsasd_setup_chld_hup_handler(struct tevent_context *ev_ctx,
218                                          struct pf_worker_data *pf)
219 {
220         struct tevent_signal *se;
221
222         se = tevent_add_signal(ev_ctx,
223                                ev_ctx,
224                                SIGHUP, 0,
225                                lsasd_chld_sig_hup_handler,
226                                pf);
227         if (!se) {
228                 DEBUG(1, ("failed to setup SIGHUP handler"));
229                 return false;
230         }
231
232         return true;
233 }
234
235 static bool lsasd_child_init(struct tevent_context *ev_ctx,
236                              int child_id,
237                              struct pf_worker_data *pf)
238 {
239         NTSTATUS status;
240         struct messaging_context *msg_ctx = server_messaging_context();
241         bool ok;
242
243         status = reinit_after_fork(msg_ctx, ev_ctx,
244                                    procid_self(), true);
245         if (!NT_STATUS_IS_OK(status)) {
246                 DEBUG(0,("reinit_after_fork() failed\n"));
247                 smb_panic("reinit_after_fork() failed");
248         }
249
250         lsasd_child_id = child_id;
251         lsasd_reopen_logs(child_id);
252
253         ok = lsasd_setup_chld_hup_handler(ev_ctx, pf);
254         if (!ok) {
255                 return false;
256         }
257
258         if (!serverid_register(procid_self(), FLAG_MSG_GENERAL)) {
259                 return false;
260         }
261
262         messaging_register(msg_ctx, ev_ctx,
263                            MSG_SMB_CONF_UPDATED, lsasd_smb_conf_updated);
264
265         status = rpc_lsarpc_init(NULL);
266         if (!NT_STATUS_IS_OK(status)) {
267                 DEBUG(0, ("Failed to register lsarpc rpc inteface! (%s)\n",
268                           nt_errstr(status)));
269                 return false;
270         }
271
272         status = rpc_samr_init(NULL);
273         if (!NT_STATUS_IS_OK(status)) {
274                 DEBUG(0, ("Failed to register samr rpc inteface! (%s)\n",
275                           nt_errstr(status)));
276                 return false;
277         }
278
279         status = rpc_netlogon_init(NULL);
280         if (!NT_STATUS_IS_OK(status)) {
281                 DEBUG(0, ("Failed to register netlogon rpc inteface! (%s)\n",
282                           nt_errstr(status)));
283                 return false;
284         }
285
286         return true;
287 }
288
289 struct lsasd_children_data {
290         struct tevent_context *ev_ctx;
291         struct messaging_context *msg_ctx;
292         struct pf_worker_data *pf;
293         int listen_fd_size;
294         int *listen_fds;
295         int lock_fd;
296
297         bool listening;
298 };
299
300 static void lsasd_next_client(void *pvt);
301
302 static int lsasd_children_main(struct tevent_context *ev_ctx,
303                                struct messaging_context *msg_ctx,
304                                struct pf_worker_data *pf,
305                                int child_id,
306                                int listen_fd_size,
307                                int *listen_fds,
308                                int lock_fd,
309                                void *private_data)
310 {
311         struct lsasd_children_data *data;
312         bool ok;
313         int ret;
314
315         ok = lsasd_child_init(ev_ctx, child_id, pf);
316         if (!ok) {
317                 return 1;
318         }
319
320         data = talloc(ev_ctx, struct lsasd_children_data);
321         if (!data) {
322                 return 1;
323         }
324         data->pf = pf;
325         data->ev_ctx = ev_ctx;
326         data->msg_ctx = msg_ctx;
327         data->lock_fd = lock_fd;
328         data->listen_fd_size = listen_fd_size;
329         data->listen_fds = listen_fds;
330         data->listening = false;
331
332         /* loop until it is time to exit */
333         while (pf->status != PF_WORKER_EXITING) {
334                 /* try to see if it is time to schedule the next client */
335                 lsasd_next_client(data);
336
337                 ret = tevent_loop_once(ev_ctx);
338                 if (ret != 0) {
339                         DEBUG(0, ("tevent_loop_once() exited with %d: %s\n",
340                                   ret, strerror(errno)));
341                         pf->status = PF_WORKER_EXITING;
342                 }
343         }
344
345         return ret;
346 }
347
348 static void lsasd_client_terminated(void *pvt)
349 {
350         struct lsasd_children_data *data;
351
352         data = talloc_get_type_abort(pvt, struct lsasd_children_data);
353
354         if (data->pf->num_clients) {
355                 data->pf->num_clients--;
356         } else {
357                 DEBUG(2, ("Invalid num clients, aborting!\n"));
358                 data->pf->status = PF_WORKER_EXITING;
359                 return;
360         }
361
362         lsasd_next_client(pvt);
363 }
364
365 struct lsasd_new_client {
366         struct lsasd_children_data *data;
367 };
368
369 static void lsasd_handle_client(struct tevent_req *req);
370
371 static void lsasd_next_client(void *pvt)
372 {
373         struct tevent_req *req;
374         struct lsasd_children_data *data;
375         struct lsasd_new_client *next;
376
377         data = talloc_get_type_abort(pvt, struct lsasd_children_data);
378
379         if (data->pf->num_clients == 0) {
380                 data->pf->status = PF_WORKER_IDLE;
381         }
382
383         if (data->pf->cmds == PF_SRV_MSG_EXIT) {
384                 DEBUG(2, ("Parent process commands we terminate!\n"));
385                 return;
386         }
387
388         if (data->listening ||
389             data->pf->num_clients >= data->pf->allowed_clients) {
390                 /* nothing to do for now we are already listening
391                  * or reached the number of clients we are allowed
392                  * to handle in parallel */
393                 return;
394         }
395
396         next = talloc_zero(data, struct lsasd_new_client);
397         if (!next) {
398                 DEBUG(1, ("Out of memory!?\n"));
399                 return;
400         }
401         next->data = data;
402
403         req = prefork_listen_send(next,
404                                   data->ev_ctx,
405                                   data->pf,
406                                   data->listen_fd_size,
407                                   data->listen_fds,
408                                   data->lock_fd);
409         if (!req) {
410                 DEBUG(1, ("Failed to make listening request!?\n"));
411                 talloc_free(next);
412                 return;
413         }
414         tevent_req_set_callback(req, lsasd_handle_client, next);
415
416         data->listening = true;
417 }
418
419 static void lsasd_handle_client(struct tevent_req *req)
420 {
421         struct lsasd_children_data *data;
422         struct lsasd_new_client *client;
423         int rc;
424         int sd;
425         TALLOC_CTX *tmp_ctx;
426         struct tsocket_address *srv_addr;
427         struct tsocket_address *cli_addr;
428
429         client = tevent_req_callback_data(req, struct lsasd_new_client);
430         data = client->data;
431
432         tmp_ctx = talloc_stackframe();
433         if (tmp_ctx == NULL) {
434                 DEBUG(1, ("Failed to allocate stackframe!\n"));
435                 return;
436         }
437
438         rc = prefork_listen_recv(req,
439                                  tmp_ctx,
440                                  &sd,
441                                  &srv_addr,
442                                  &cli_addr);
443
444         /* this will free the request too */
445         talloc_free(client);
446         /* we are done listening */
447         data->listening = false;
448
449         if (rc > 0) {
450                 DEBUG(1, ("Failed to accept client connection!\n"));
451                 /* bail out if we are not serving any other client */
452                 if (data->pf->num_clients == 0) {
453                         data->pf->status = PF_WORKER_EXITING;
454                 }
455                 return;
456         }
457
458         if (rc == -2) {
459                 DEBUG(1, ("Server asks us to die!\n"));
460                 data->pf->status = PF_WORKER_EXITING;
461                 return;
462         }
463
464         DEBUG(2, ("LSASD preforked child %d got client connection!\n",
465                   (int)(data->pf->pid)));
466
467         if (tsocket_address_is_inet(srv_addr, "ip")) {
468                 DEBUG(3, ("Got a tcpip client connection from %s on inteface %s\n",
469                            tsocket_address_string(cli_addr, tmp_ctx),
470                            tsocket_address_string(srv_addr, tmp_ctx)));
471
472                 dcerpc_ncacn_accept(data->ev_ctx,
473                                     data->msg_ctx,
474                                     NCACN_IP_TCP,
475                                     "IP",
476                                     cli_addr,
477                                     srv_addr,
478                                     sd,
479                                     NULL);
480         } else if (tsocket_address_is_unix(srv_addr)) {
481                 char *p;
482
483                 p = tsocket_address_unix_path(srv_addr, tmp_ctx);
484                 if (p == NULL) {
485                         talloc_free(tmp_ctx);
486                         return;
487                 }
488
489                 if (strstr(p, "/np/")) {
490                         p = basename(p);
491
492                         named_pipe_accept_function(data->ev_ctx,
493                                                    data->msg_ctx,
494                                                    p,
495                                                    sd,
496                                                    lsasd_client_terminated,
497                                                    data);
498                 } else {
499                         p = basename(p);
500
501                         dcerpc_ncacn_accept(data->ev_ctx,
502                                             data->msg_ctx,
503                                             NCALRPC,
504                                             p,
505                                             cli_addr,
506                                             srv_addr,
507                                             sd,
508                                             NULL);
509                 }
510         } else {
511                 DEBUG(0, ("ERROR: Unsupported socket!\n"));
512         }
513
514         talloc_free(tmp_ctx);
515 }
516
517 /*
518  * MAIN
519  */
520
521 static bool lsasd_schedule_check(struct tevent_context *ev_ctx,
522                                  struct messaging_context *msg_ctx,
523                                  struct timeval current_time);
524
525 static void lsasd_check_children(struct tevent_context *ev_ctx,
526                                     struct tevent_timer *te,
527                                     struct timeval current_time,
528                                     void *pvt);
529
530 static void lsasd_sigchld_handler(struct tevent_context *ev_ctx,
531                                   struct prefork_pool *pfp,
532                                   void *pvt)
533 {
534         struct messaging_context *msg_ctx;
535
536         msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);
537
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);
541 }
542
543 static bool lsasd_setup_children_monitor(struct tevent_context *ev_ctx,
544                                          struct messaging_context *msg_ctx)
545 {
546         bool ok;
547
548         /* add our oun sigchld callback */
549         prefork_set_sigchld_callback(lsasd_pool, lsasd_sigchld_handler, msg_ctx);
550
551         ok = lsasd_schedule_check(ev_ctx, msg_ctx, tevent_timeval_current());
552
553         return ok;
554 }
555
556 static bool lsasd_schedule_check(struct tevent_context *ev_ctx,
557                                  struct messaging_context *msg_ctx,
558                                  struct timeval current_time)
559 {
560         struct tevent_timer *te;
561         struct timeval next_event;
562
563         /* check situation again in 10 seconds */
564         next_event = tevent_timeval_current_ofs(10, 0);
565
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;
573         }
574
575         return true;
576 }
577
578 static void lsasd_check_children(struct tevent_context *ev_ctx,
579                                  struct tevent_timer *te,
580                                  struct timeval current_time,
581                                  void *pvt)
582 {
583         struct messaging_context *msg_ctx;
584
585         msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);
586
587         pfh_manage_pool(ev_ctx, msg_ctx, &pf_lsasd_cfg, lsasd_pool);
588
589         lsasd_schedule_check(ev_ctx, msg_ctx, current_time);
590 }
591
592 /*
593  * start it up
594  */
595
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)
600 {
601         struct dcerpc_binding_vector *v, *v_orig;
602         TALLOC_CTX *tmp_ctx;
603         NTSTATUS status;
604         uint32_t i;
605         int fd;
606         int rc;
607         bool ok = true;
608
609         tmp_ctx = talloc_stackframe();
610         if (tmp_ctx == NULL) {
611                 return false;
612         }
613
614         status = dcerpc_binding_vector_new(tmp_ctx, &v_orig);
615         if (!NT_STATUS_IS_OK(status)) {
616                 ok = false;
617                 goto done;
618         }
619
620         /* Create only one tcpip listener for all services */
621         status = rpc_create_tcpip_sockets(&ndr_table_lsarpc,
622                                           v_orig,
623                                           0,
624                                           listen_fd,
625                                           listen_fd_size);
626         if (!NT_STATUS_IS_OK(status)) {
627                 ok = false;
628                 goto done;
629         }
630
631         /* Start to listen on tcpip sockets */
632         for (i = 0; i < *listen_fd_size; i++) {
633                 rc = listen(listen_fd[i], pf_lsasd_cfg.max_allowed_clients);
634                 if (rc == -1) {
635                         DEBUG(0, ("Failed to listen on tcpip socket - %s\n",
636                                   strerror(errno)));
637                         ok = false;
638                         goto done;
639                 }
640         }
641
642         /* LSARPC */
643         fd = create_named_pipe_socket("lsarpc");
644         if (fd < 0) {
645                 ok = false;
646                 goto done;
647         }
648         listen_fd[*listen_fd_size] = fd;
649         (*listen_fd_size)++;
650
651         rc = listen(fd, pf_lsasd_cfg.max_allowed_clients);
652         if (rc == -1) {
653                 DEBUG(0, ("Failed to listen on lsarpc pipe - %s\n",
654                           strerror(errno)));
655                 ok = false;
656                 goto done;
657         }
658
659         v = dcerpc_binding_vector_dup(tmp_ctx, v_orig);
660         if (v == NULL) {
661                 ok = false;
662                 goto done;
663         }
664
665         status = dcerpc_binding_vector_replace_iface(&ndr_table_lsarpc, v);
666         if (!NT_STATUS_IS_OK(status)) {
667                 return false;
668         }
669
670         status = dcerpc_binding_vector_add_np_default(&ndr_table_lsarpc, v);
671         if (!NT_STATUS_IS_OK(status)) {
672                 ok = false;
673                 goto done;
674         }
675
676         status = rpc_ep_register(ev_ctx, msg_ctx, &ndr_table_lsarpc, v);
677         if (!NT_STATUS_IS_OK(status)) {
678                 ok = false;
679                 goto done;
680         }
681
682         /* SAMR */
683         fd = create_named_pipe_socket("samr");
684         if (fd < 0) {
685                 ok = false;
686                 goto done;
687         }
688
689         rc = listen(fd, pf_lsasd_cfg.max_allowed_clients);
690         if (rc == -1) {
691                 DEBUG(0, ("Failed to listen on samr pipe - %s\n",
692                           strerror(errno)));
693                 ok = false;
694                 goto done;
695         }
696         listen_fd[*listen_fd_size] = fd;
697         (*listen_fd_size)++;
698
699         v = dcerpc_binding_vector_dup(tmp_ctx, v_orig);
700         if (v == NULL) {
701                 ok = false;
702                 goto done;
703         }
704
705         status = dcerpc_binding_vector_replace_iface(&ndr_table_samr, v);
706         if (!NT_STATUS_IS_OK(status)) {
707                 return false;
708         }
709
710         status = dcerpc_binding_vector_add_np_default(&ndr_table_samr, v);
711         if (!NT_STATUS_IS_OK(status)) {
712                 ok = false;
713                 goto done;
714         }
715
716         status = rpc_ep_register(ev_ctx, msg_ctx, &ndr_table_samr, v);
717         if (!NT_STATUS_IS_OK(status)) {
718                 ok = false;
719                 goto done;
720         }
721
722         /* NETLOGON */
723         fd = create_named_pipe_socket("netlogon");
724         if (fd < 0) {
725                 ok = false;
726                 goto done;
727         }
728
729         rc = listen(fd, pf_lsasd_cfg.max_allowed_clients);
730         if (rc == -1) {
731                 DEBUG(0, ("Failed to listen on samr pipe - %s\n",
732                           strerror(errno)));
733                 ok = false;
734                 goto done;
735         }
736         listen_fd[*listen_fd_size] = fd;
737         (*listen_fd_size)++;
738
739         v = dcerpc_binding_vector_dup(tmp_ctx, v_orig);
740         if (v == NULL) {
741                 ok = false;
742                 goto done;
743         }
744
745         status = dcerpc_binding_vector_replace_iface(&ndr_table_netlogon, v);
746         if (!NT_STATUS_IS_OK(status)) {
747                 return false;
748         }
749
750         status = dcerpc_binding_vector_add_np_default(&ndr_table_netlogon, v);
751         if (!NT_STATUS_IS_OK(status)) {
752                 ok = false;
753                 goto done;
754         }
755
756         status = rpc_ep_register(ev_ctx, msg_ctx, &ndr_table_netlogon, v);
757         if (!NT_STATUS_IS_OK(status)) {
758                 ok = false;
759                 goto done;
760         }
761
762 done:
763         talloc_free(tmp_ctx);
764         return ok;
765 }
766
767 void start_lsasd(struct tevent_context *ev_ctx,
768                  struct messaging_context *msg_ctx)
769 {
770         NTSTATUS status;
771         int listen_fd[LSASD_MAX_SOCKETS];
772         int listen_fd_size = 0;
773         pid_t pid;
774         int rc;
775         bool ok;
776
777         DEBUG(1, ("Forking LSA Service Daemon\n"));
778
779         /*
780          * Block signals before forking child as it will have to
781          * set its own handlers. Child will re-enable SIGHUP as
782          * soon as the handlers are set up.
783          */
784         BlockSignals(true, SIGTERM);
785         BlockSignals(true, SIGHUP);
786
787         pid = sys_fork();
788         if (pid == -1) {
789                 DEBUG(0, ("Failed to fork LSASD [%s], aborting ...\n",
790                            strerror(errno)));
791                 exit(1);
792         }
793
794         /* parent or error */
795         if (pid != 0) {
796
797                 /* Re-enable SIGHUP before returnig */
798                 BlockSignals(false, SIGTERM);
799                 BlockSignals(false, SIGHUP);
800
801                 return;
802         }
803
804         /* child */
805         close_low_fds(false);
806
807         status = reinit_after_fork(msg_ctx,
808                                    ev_ctx,
809                                    procid_self(), true);
810         if (!NT_STATUS_IS_OK(status)) {
811                 DEBUG(0,("reinit_after_fork() failed\n"));
812                 smb_panic("reinit_after_fork() failed");
813         }
814
815         lsasd_reopen_logs(0);
816         pfh_daemon_config(DAEMON_NAME,
817                           &pf_lsasd_cfg,
818                           &default_pf_lsasd_cfg);
819
820         lsasd_setup_sig_term_handler(ev_ctx);
821         lsasd_setup_sig_hup_handler(ev_ctx);
822
823         BlockSignals(false, SIGTERM);
824         BlockSignals(false, SIGHUP);
825
826         ok = lsasd_create_sockets(ev_ctx, msg_ctx, listen_fd, &listen_fd_size);
827         if (!ok) {
828                 exit(1);
829         }
830
831         /* start children before any more initialization is done */
832         ok = prefork_create_pool(ev_ctx, /* mem_ctx */
833                                  ev_ctx,
834                                  msg_ctx,
835                                  listen_fd_size,
836                                  listen_fd,
837                                  pf_lsasd_cfg.min_children,
838                                  pf_lsasd_cfg.max_children,
839                                  &lsasd_children_main,
840                                  NULL,
841                                  &lsasd_pool);
842         if (!ok) {
843                 exit(1);
844         }
845
846         if (!serverid_register(procid_self(), FLAG_MSG_GENERAL)) {
847                 exit(1);
848         }
849
850         messaging_register(msg_ctx,
851                            ev_ctx,
852                            MSG_SMB_CONF_UPDATED,
853                            lsasd_smb_conf_updated);
854
855         status = rpc_lsarpc_init(NULL);
856         if (!NT_STATUS_IS_OK(status)) {
857                 DEBUG(0, ("Failed to register winreg rpc inteface! (%s)\n",
858                           nt_errstr(status)));
859                 exit(1);
860         }
861
862         status = rpc_samr_init(NULL);
863         if (!NT_STATUS_IS_OK(status)) {
864                 DEBUG(0, ("Failed to register lsasd rpc inteface! (%s)\n",
865                           nt_errstr(status)));
866                 exit(1);
867         }
868
869         status = rpc_netlogon_init(NULL);
870         if (!NT_STATUS_IS_OK(status)) {
871                 DEBUG(0, ("Failed to register lsasd rpc inteface! (%s)\n",
872                           nt_errstr(status)));
873                 exit(1);
874         }
875
876         ok = lsasd_setup_children_monitor(ev_ctx, msg_ctx);
877         if (!ok) {
878                 DEBUG(0, ("Failed to setup children monitoring!\n"));
879                 exit(1);
880         }
881
882         DEBUG(1, ("LSASD Daemon Started (%d)\n", getpid()));
883
884         /* loop forever */
885         rc = tevent_loop_wait(ev_ctx);
886
887         /* should not be reached */
888         DEBUG(0,("lsasd: tevent_loop_wait() exited with %d - %s\n",
889                  rc, (rc == 0) ? "out of events" : strerror(errno)));
890         exit(1);
891 }