s3-spoolssd: Send a message to the parent when we accept a connection
[mat/samba.git] / source3 / printing / spoolssd.c
1 /*
2    Unix SMB/Netbios implementation.
3    SPOOLSS Daemon
4    Copyright (C) Simo Sorce <idra@samba.org> 2010-2011
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19 #include "includes.h"
20 #include "serverid.h"
21 #include "smbd/smbd.h"
22
23 #include "messages.h"
24 #include "include/printing.h"
25 #include "printing/nt_printing_migrate_internal.h"
26 #include "printing/queue_process.h"
27 #include "printing/pcap.h"
28 #include "printing/load.h"
29 #include "ntdomain.h"
30 #include "librpc/gen_ndr/srv_winreg.h"
31 #include "librpc/gen_ndr/srv_spoolss.h"
32 #include "rpc_server/rpc_server.h"
33 #include "rpc_server/rpc_ep_register.h"
34 #include "rpc_server/spoolss/srv_spoolss_nt.h"
35 #include "librpc/rpc/dcerpc_ep.h"
36 #include "lib/server_prefork.h"
37 #include "lib/server_prefork_util.h"
38
39 #define SPOOLSS_PIPE_NAME "spoolss"
40 #define DAEMON_NAME "spoolssd"
41
42 static struct server_id parent_id;
43 static struct prefork_pool *spoolss_pool = NULL;
44 static int spoolss_child_id = 0;
45
46 static struct pf_daemon_config default_pf_spoolss_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_spoolss_cfg = { 0 };
55
56 pid_t start_spoolssd(struct tevent_context *ev_ctx,
57                      struct messaging_context *msg_ctx);
58
59 static void spoolss_reopen_logs(int child_id)
60 {
61         char *lfile = lp_logfile();
62         char *ext;
63         int rc;
64
65         if (child_id) {
66                 rc = asprintf(&ext, "%s.%d", DAEMON_NAME, child_id);
67         } else {
68                 rc = asprintf(&ext, "%s", DAEMON_NAME);
69         }
70
71         if (rc == -1) {
72                 return;
73         }
74
75         rc = 0;
76         if (lfile == NULL || lfile[0] == '\0') {
77                 rc = asprintf(&lfile, "%s/log.%s",
78                               get_dyn_LOGFILEBASE(), ext);
79         } else {
80                 if (strstr(lfile, ext) == NULL) {
81                         if (child_id) {
82                                 rc = asprintf(&lfile, "%s.%d",
83                                               lp_logfile(), child_id);
84                         } else {
85                                 rc = asprintf(&lfile, "%s.%s",
86                                               lp_logfile(), ext);
87                         }
88                 }
89         }
90
91         if (rc > 0) {
92                 lp_set_logfile(lfile);
93                 SAFE_FREE(lfile);
94         }
95
96         SAFE_FREE(ext);
97
98         reopen_logs();
99 }
100
101 static void update_conf(struct tevent_context *ev,
102                         struct messaging_context *msg)
103 {
104         change_to_root_user();
105         lp_load(get_dyn_CONFIGFILE(), true, false, false, true);
106         reload_printers(ev, msg);
107
108         spoolss_reopen_logs(spoolss_child_id);
109         if (spoolss_child_id == 0) {
110                 pfh_daemon_config(DAEMON_NAME,
111                                   &pf_spoolss_cfg,
112                                   &default_pf_spoolss_cfg);
113                 pfh_manage_pool(ev, msg, &pf_spoolss_cfg, spoolss_pool);
114         }
115 }
116
117 static void smb_conf_updated(struct messaging_context *msg,
118                              void *private_data,
119                              uint32_t msg_type,
120                              struct server_id server_id,
121                              DATA_BLOB *data)
122 {
123         struct tevent_context *ev_ctx = talloc_get_type_abort(private_data,
124                                                              struct tevent_context);
125
126         DEBUG(10, ("Got message saying smb.conf was updated. Reloading.\n"));
127         update_conf(ev_ctx, msg);
128 }
129
130 static void update_pcap(struct tevent_context *ev_ctx,
131                         struct messaging_context *msg_ctx)
132 {
133         change_to_root_user();
134         reload_printers(ev_ctx, msg_ctx);
135 }
136
137 static void pcap_updated(struct messaging_context *msg,
138                          void *private_data,
139                          uint32_t msg_type,
140                          struct server_id server_id,
141                          DATA_BLOB *data)
142 {
143         struct tevent_context *ev_ctx;
144
145         ev_ctx = talloc_get_type_abort(private_data, struct tevent_context);
146
147         DEBUG(10, ("Got message that pcap updated. Reloading.\n"));
148         update_pcap(ev_ctx, msg);
149 }
150
151 static void spoolss_sig_term_handler(struct tevent_context *ev,
152                                      struct tevent_signal *se,
153                                      int signum,
154                                      int count,
155                                      void *siginfo,
156                                      void *private_data)
157 {
158         exit_server_cleanly("termination signal");
159 }
160
161 static void spoolss_setup_sig_term_handler(struct tevent_context *ev_ctx)
162 {
163         struct tevent_signal *se;
164
165         se = tevent_add_signal(ev_ctx,
166                                ev_ctx,
167                                SIGTERM, 0,
168                                spoolss_sig_term_handler,
169                                NULL);
170         if (!se) {
171                 exit_server("failed to setup SIGTERM handler");
172         }
173 }
174
175 static void spoolss_sig_hup_handler(struct tevent_context *ev,
176                                     struct tevent_signal *se,
177                                     int signum,
178                                     int count,
179                                     void *siginfo,
180                                     void *pvt)
181 {
182         struct messaging_context *msg_ctx;
183
184         msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);
185
186         DEBUG(1,("Reloading printers after SIGHUP\n"));
187         update_conf(ev, msg_ctx);
188
189         /* relay to all children */
190         if (spoolss_pool) {
191                 prefork_send_signal_to_all(spoolss_pool, SIGHUP);
192         }
193 }
194
195 static void spoolss_setup_sig_hup_handler(struct tevent_context *ev_ctx,
196                                           struct messaging_context *msg_ctx)
197 {
198         struct tevent_signal *se;
199
200         se = tevent_add_signal(ev_ctx,
201                                ev_ctx,
202                                SIGHUP, 0,
203                                spoolss_sig_hup_handler,
204                                msg_ctx);
205         if (!se) {
206                 exit_server("failed to setup SIGHUP handler");
207         }
208 }
209
210 static bool spoolss_init_cb(void *ptr)
211 {
212         struct messaging_context *msg_ctx = talloc_get_type_abort(
213                 ptr, struct messaging_context);
214
215         return nt_printing_tdb_migrate(msg_ctx);
216 }
217
218 static bool spoolss_shutdown_cb(void *ptr)
219 {
220         srv_spoolss_cleanup();
221
222         return true;
223 }
224
225 /* Children */
226
227 struct spoolss_chld_sig_hup_ctx {
228         struct messaging_context *msg_ctx;
229         struct pf_worker_data *pf;
230 };
231
232 static void spoolss_chld_sig_hup_handler(struct tevent_context *ev,
233                                          struct tevent_signal *se,
234                                          int signum,
235                                          int count,
236                                          void *siginfo,
237                                          void *pvt)
238 {
239         struct spoolss_chld_sig_hup_ctx *shc;
240
241         shc = talloc_get_type_abort(pvt, struct spoolss_chld_sig_hup_ctx);
242
243         /* avoid wasting CPU cycles if we are going to exit soon anyways */
244         if (shc->pf != NULL &&
245             shc->pf->cmds == PF_SRV_MSG_EXIT) {
246                 return;
247         }
248
249         change_to_root_user();
250         DEBUG(1,("Reloading printers after SIGHUP\n"));
251         reload_printers(ev, shc->msg_ctx);
252         spoolss_reopen_logs(spoolss_child_id);
253 }
254
255 static bool spoolss_setup_chld_hup_handler(struct tevent_context *ev_ctx,
256                                            struct messaging_context *msg_ctx,
257                                            struct pf_worker_data *pf)
258 {
259         struct spoolss_chld_sig_hup_ctx *shc;
260         struct tevent_signal *se;
261
262         shc = talloc(ev_ctx, struct spoolss_chld_sig_hup_ctx);
263         if (!shc) {
264                 DEBUG(1, ("failed to setup SIGHUP handler"));
265                 return false;
266         }
267         shc->pf = pf;
268         shc->msg_ctx = msg_ctx;
269
270         se = tevent_add_signal(ev_ctx,
271                                ev_ctx,
272                                SIGHUP, 0,
273                                spoolss_chld_sig_hup_handler,
274                                shc);
275         if (!se) {
276                 DEBUG(1, ("failed to setup SIGHUP handler"));
277                 return false;
278         }
279
280         return true;
281 }
282
283 static bool spoolss_child_init(struct tevent_context *ev_ctx,
284                                int child_id, struct pf_worker_data *pf)
285 {
286         NTSTATUS status;
287         struct rpc_srv_callbacks spoolss_cb;
288         struct messaging_context *msg_ctx = server_messaging_context();
289         bool ok;
290
291         status = reinit_after_fork(msg_ctx, ev_ctx,
292                                    procid_self(), true);
293         if (!NT_STATUS_IS_OK(status)) {
294                 DEBUG(0,("reinit_after_fork() failed\n"));
295                 smb_panic("reinit_after_fork() failed");
296         }
297
298         spoolss_child_id = child_id;
299         spoolss_reopen_logs(child_id);
300
301         ok = spoolss_setup_chld_hup_handler(ev_ctx, msg_ctx, pf);
302         if (!ok) {
303                 return false;
304         }
305
306         if (!serverid_register(procid_self(),
307                                 FLAG_MSG_GENERAL |
308                                 FLAG_MSG_PRINT_GENERAL)) {
309                 return false;
310         }
311
312         if (!locking_init()) {
313                 return false;
314         }
315
316         messaging_register(msg_ctx, ev_ctx,
317                            MSG_SMB_CONF_UPDATED, smb_conf_updated);
318         messaging_register(msg_ctx, ev_ctx, MSG_PRINTER_PCAP,
319                            pcap_updated);
320
321         /* As soon as messaging is up check if pcap has been loaded already.
322          * If so then we probably missed a message and should load_printers()
323          * ourselves. If pcap has not been loaded yet, then ignore, we will get
324          * a message as soon as the bq process completes the reload. */
325         if (pcap_cache_loaded()) {
326                 load_printers(ev_ctx, msg_ctx);
327         }
328
329         /* try to reinit rpc queues */
330         spoolss_cb.init = spoolss_init_cb;
331         spoolss_cb.shutdown = spoolss_shutdown_cb;
332         spoolss_cb.private_data = msg_ctx;
333
334         status = rpc_winreg_init(NULL);
335         if (!NT_STATUS_IS_OK(status)) {
336                 DEBUG(0, ("Failed to register winreg rpc inteface! (%s)\n",
337                           nt_errstr(status)));
338                 return false;
339         }
340
341         status = rpc_spoolss_init(&spoolss_cb);
342         if (!NT_STATUS_IS_OK(status)) {
343                 DEBUG(0, ("Failed to register spoolss rpc inteface! (%s)\n",
344                           nt_errstr(status)));
345                 return false;
346         }
347
348         return true;
349 }
350
351 struct spoolss_children_data {
352         struct tevent_context *ev_ctx;
353         struct messaging_context *msg_ctx;
354         struct pf_worker_data *pf;
355         int listen_fd_size;
356         int *listen_fds;
357 };
358
359 static void spoolss_next_client(void *pvt);
360
361 static int spoolss_children_main(struct tevent_context *ev_ctx,
362                                  struct messaging_context *msg_ctx,
363                                  struct pf_worker_data *pf,
364                                  int child_id,
365                                  int listen_fd_size,
366                                  int *listen_fds,
367                                  void *private_data)
368 {
369         struct spoolss_children_data *data;
370         bool ok;
371         int ret;
372
373         ok = spoolss_child_init(ev_ctx, child_id, pf);
374         if (!ok) {
375                 return 1;
376         }
377
378         data = talloc(ev_ctx, struct spoolss_children_data);
379         if (!data) {
380                 return 1;
381         }
382         data->pf = pf;
383         data->ev_ctx = ev_ctx;
384         data->msg_ctx = msg_ctx;
385         data->listen_fd_size = listen_fd_size;
386         data->listen_fds = listen_fds;
387
388         /* loop until it is time to exit */
389         while (pf->status != PF_WORKER_EXITING) {
390                 /* try to see if it is time to schedule the next client */
391                 spoolss_next_client(data);
392
393                 ret = tevent_loop_once(ev_ctx);
394                 if (ret != 0) {
395                         DEBUG(0, ("tevent_loop_once() exited with %d: %s\n",
396                                   ret, strerror(errno)));
397                         pf->status = PF_WORKER_EXITING;
398                 }
399         }
400
401         return ret;
402 }
403
404 static void spoolss_client_terminated(void *pvt)
405 {
406         struct spoolss_children_data *data;
407
408         data = talloc_get_type_abort(pvt, struct spoolss_children_data);
409
410         pfh_client_terminated(data->pf);
411
412         spoolss_next_client(pvt);
413 }
414
415 struct spoolss_new_client {
416         struct spoolss_children_data *data;
417         struct tsocket_address *srv_addr;
418         struct tsocket_address *cli_addr;
419 };
420
421 static void spoolss_handle_client(struct tevent_req *req);
422
423 static void spoolss_next_client(void *pvt)
424 {
425         struct tevent_req *req;
426         struct spoolss_children_data *data;
427         struct spoolss_new_client *next;
428
429         data = talloc_get_type_abort(pvt, struct spoolss_children_data);
430
431         if (!pfh_child_allowed_to_accept(data->pf)) {
432                 /* nothing to do for now we are already listening
433                  * or we are not allowed to listen further */
434                 return;
435         }
436
437         next = talloc_zero(data, struct spoolss_new_client);
438         if (!next) {
439                 DEBUG(1, ("Out of memory!?\n"));
440                 return;
441         }
442         next->data = data;
443
444         req = prefork_listen_send(next, data->ev_ctx, data->pf,
445                                   data->listen_fd_size,
446                                   data->listen_fds);
447         if (!req) {
448                 DEBUG(1, ("Failed to make listening request!?\n"));
449                 talloc_free(next);
450                 return;
451         }
452         tevent_req_set_callback(req, spoolss_handle_client, next);
453 }
454
455 static void spoolss_handle_client(struct tevent_req *req)
456 {
457         struct spoolss_children_data *data;
458         struct spoolss_new_client *client;
459         const DATA_BLOB ping = data_blob_null;
460         int ret;
461         int sd;
462
463         client = tevent_req_callback_data(req, struct spoolss_new_client);
464         data = client->data;
465
466         ret = prefork_listen_recv(req, client, &sd,
467                                   &client->srv_addr, &client->cli_addr);
468
469         /* this will free the request too */
470         talloc_free(client);
471
472         if (ret != 0) {
473                 DEBUG(6, ("No client connection was available after all!\n"));
474                 return;
475         }
476
477         /* Warn parent that our status changed */
478         messaging_send(data->msg_ctx, parent_id,
479                         MSG_PREFORK_CHILD_EVENT, &ping);
480
481         DEBUG(2, ("Spoolss preforked child %d got client connection!\n",
482                   (int)(data->pf->pid)));
483
484         named_pipe_accept_function(data->ev_ctx, data->msg_ctx,
485                                    SPOOLSS_PIPE_NAME, sd,
486                                    spoolss_client_terminated, data);
487 }
488
489 /* ==== Main Process Functions ==== */
490
491 extern pid_t background_lpq_updater_pid;
492 static char *bq_logfile;
493
494 static void check_updater_child(void)
495 {
496         int status;
497         pid_t pid;
498
499         if (background_lpq_updater_pid == -1) {
500                 return;
501         }
502
503         pid = sys_waitpid(background_lpq_updater_pid, &status, WNOHANG);
504         if (pid > 0) {
505                 DEBUG(2, ("The background queue child died... Restarting!\n"));
506                 pid = start_background_queue(server_event_context(),
507                                              server_messaging_context(),
508                                              bq_logfile);
509                 background_lpq_updater_pid = pid;
510         }
511 }
512
513 static void child_ping(struct messaging_context *msg_ctx,
514                         void *private_data,
515                         uint32_t msg_type,
516                         struct server_id server_id,
517                         DATA_BLOB *data)
518 {
519         struct tevent_context *ev_ctx;
520
521         ev_ctx = talloc_get_type_abort(private_data, struct tevent_context);
522
523         DEBUG(10, ("Got message that a child changed status.\n"));
524         pfh_manage_pool(ev_ctx, msg_ctx, &pf_spoolss_cfg, spoolss_pool);
525 }
526
527 static bool spoolssd_schedule_check(struct tevent_context *ev_ctx,
528                                     struct messaging_context *msg_ctx,
529                                     struct timeval current_time);
530 static void spoolssd_check_children(struct tevent_context *ev_ctx,
531                                     struct tevent_timer *te,
532                                     struct timeval current_time,
533                                     void *pvt);
534
535 static void spoolssd_sigchld_handler(struct tevent_context *ev_ctx,
536                                      struct prefork_pool *pfp,
537                                      void *pvt)
538 {
539         struct messaging_context *msg_ctx;
540
541         msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);
542
543         /* run pool management so we can fork/retire or increase
544          * the allowed connections per child based on load */
545         pfh_manage_pool(ev_ctx, msg_ctx, &pf_spoolss_cfg, spoolss_pool);
546
547         /* also check if the updater child is alive and well */
548         check_updater_child();
549 }
550
551 static bool spoolssd_setup_children_monitor(struct tevent_context *ev_ctx,
552                                             struct messaging_context *msg_ctx)
553 {
554         bool ok;
555
556         /* add our oun sigchld callback */
557         prefork_set_sigchld_callback(spoolss_pool,
558                                      spoolssd_sigchld_handler, msg_ctx);
559
560         ok = spoolssd_schedule_check(ev_ctx, msg_ctx,
561                                      tevent_timeval_current());
562         return ok;
563 }
564
565 static bool spoolssd_schedule_check(struct tevent_context *ev_ctx,
566                                     struct messaging_context *msg_ctx,
567                                     struct timeval current_time)
568 {
569         struct tevent_timer *te;
570         struct timeval next_event;
571
572         /* check situation again in 10 seconds */
573         next_event = tevent_timeval_current_ofs(10, 0);
574
575         /* TODO: check when the socket becomes readable, so that children
576          * are checked only when there is some activity ? */
577         te = tevent_add_timer(ev_ctx, spoolss_pool, next_event,
578                                 spoolssd_check_children, msg_ctx);
579         if (!te) {
580                 DEBUG(2, ("Failed to set up children monitoring!\n"));
581                 return false;
582         }
583
584         return true;
585 }
586
587 static void spoolssd_check_children(struct tevent_context *ev_ctx,
588                                     struct tevent_timer *te,
589                                     struct timeval current_time,
590                                     void *pvt)
591 {
592         struct messaging_context *msg_ctx;
593
594         msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);
595
596         pfh_manage_pool(ev_ctx, msg_ctx, &pf_spoolss_cfg, spoolss_pool);
597
598         spoolssd_schedule_check(ev_ctx, msg_ctx, current_time);
599 }
600
601 static void print_queue_forward(struct messaging_context *msg,
602                                 void *private_data,
603                                 uint32_t msg_type,
604                                 struct server_id server_id,
605                                 DATA_BLOB *data)
606 {
607         messaging_send_buf(msg, pid_to_procid(background_lpq_updater_pid),
608                            MSG_PRINTER_UPDATE, data->data, data->length);
609 }
610
611 static char *get_bq_logfile(void)
612 {
613         char *lfile = lp_logfile();
614         int rc;
615
616         if (lfile == NULL || lfile[0] == '\0') {
617                 rc = asprintf(&lfile, "%s/log.%s.bq",
618                                         get_dyn_LOGFILEBASE(), DAEMON_NAME);
619         } else {
620                 rc = asprintf(&lfile, "%s.bq", lp_logfile());
621         }
622         if (rc == -1) {
623                 lfile = NULL;
624         }
625         return lfile;
626 }
627
628 pid_t start_spoolssd(struct tevent_context *ev_ctx,
629                     struct messaging_context *msg_ctx)
630 {
631         struct rpc_srv_callbacks spoolss_cb;
632         struct dcerpc_binding_vector *v;
633         TALLOC_CTX *mem_ctx;
634         pid_t pid;
635         NTSTATUS status;
636         int listen_fd;
637         int ret;
638         bool ok;
639
640         DEBUG(1, ("Forking SPOOLSS Daemon\n"));
641
642         /*
643          * Block signals before forking child as it will have to
644          * set its own handlers. Child will re-enable SIGHUP as
645          * soon as the handlers are set up.
646          */
647         BlockSignals(true, SIGTERM);
648         BlockSignals(true, SIGHUP);
649
650         pid = sys_fork();
651
652         if (pid == -1) {
653                 DEBUG(0, ("Failed to fork SPOOLSS [%s]\n",
654                            strerror(errno)));
655         }
656
657         /* parent or error */
658         if (pid != 0) {
659
660                 /* Re-enable SIGHUP before returnig */
661                 BlockSignals(false, SIGTERM);
662                 BlockSignals(false, SIGHUP);
663                 return pid;
664         }
665
666         /* child */
667         close_low_fds(false);
668
669         status = reinit_after_fork(msg_ctx,
670                                    ev_ctx,
671                                    procid_self(), true);
672         if (!NT_STATUS_IS_OK(status)) {
673                 DEBUG(0,("reinit_after_fork() failed\n"));
674                 smb_panic("reinit_after_fork() failed");
675         }
676
677         /* save the parent process id so the children can use it later */
678         parent_id = procid_self();
679
680         spoolss_reopen_logs(0);
681         pfh_daemon_config(DAEMON_NAME,
682                           &pf_spoolss_cfg,
683                           &default_pf_spoolss_cfg);
684
685         spoolss_setup_sig_term_handler(ev_ctx);
686         spoolss_setup_sig_hup_handler(ev_ctx, msg_ctx);
687
688         BlockSignals(false, SIGTERM);
689         BlockSignals(false, SIGHUP);
690
691         /* always start the backgroundqueue listner in spoolssd */
692         bq_logfile = get_bq_logfile();
693         pid = start_background_queue(ev_ctx, msg_ctx, bq_logfile);
694         if (pid > 0) {
695                 background_lpq_updater_pid = pid;
696         }
697
698         /* the listening fd must be created before the children are actually
699          * forked out. */
700         listen_fd = create_named_pipe_socket(SPOOLSS_PIPE_NAME);
701         if (listen_fd == -1) {
702                 exit(1);
703         }
704
705         ret = listen(listen_fd, pf_spoolss_cfg.max_allowed_clients);
706         if (ret == -1) {
707                 DEBUG(0, ("Failed to listen on spoolss pipe - %s\n",
708                           strerror(errno)));
709                 exit(1);
710         }
711
712         /* start children before any more initialization is done */
713         ok = prefork_create_pool(ev_ctx, /* mem_ctx */
714                                  ev_ctx, msg_ctx,
715                                  1, &listen_fd,
716                                  pf_spoolss_cfg.min_children,
717                                  pf_spoolss_cfg.max_children,
718                                  &spoolss_children_main, NULL,
719                                  &spoolss_pool);
720         if (!ok) {
721                 exit(1);
722         }
723
724         if (!serverid_register(procid_self(),
725                                 FLAG_MSG_GENERAL |
726                                 FLAG_MSG_PRINT_GENERAL)) {
727                 exit(1);
728         }
729
730         if (!locking_init()) {
731                 exit(1);
732         }
733
734         messaging_register(msg_ctx, ev_ctx,
735                            MSG_SMB_CONF_UPDATED, smb_conf_updated);
736         messaging_register(msg_ctx, NULL, MSG_PRINTER_UPDATE,
737                            print_queue_forward);
738         messaging_register(msg_ctx, ev_ctx, MSG_PRINTER_PCAP,
739                            pcap_updated);
740         messaging_register(msg_ctx, ev_ctx,
741                            MSG_PREFORK_CHILD_EVENT, child_ping);
742
743         /* As soon as messaging is up check if pcap has been loaded already.
744          * If so then we probably missed a message and should load_printers()
745          * ourselves. If pcap has not been loaded yet, then ignore, we will get
746          * a message as soon as the bq process completes the reload. */
747         if (pcap_cache_loaded()) {
748                 load_printers(ev_ctx, msg_ctx);
749         }
750
751         mem_ctx = talloc_new(NULL);
752         if (mem_ctx == NULL) {
753                 exit(1);
754         }
755
756         /*
757          * Initialize spoolss with an init function to convert printers first.
758          * static_init_rpc will try to initialize the spoolss server too but you
759          * can't register it twice.
760          */
761         spoolss_cb.init = spoolss_init_cb;
762         spoolss_cb.shutdown = spoolss_shutdown_cb;
763         spoolss_cb.private_data = msg_ctx;
764
765         status = rpc_winreg_init(NULL);
766         if (!NT_STATUS_IS_OK(status)) {
767                 DEBUG(0, ("Failed to register winreg rpc inteface! (%s)\n",
768                           nt_errstr(status)));
769                 exit(1);
770         }
771
772         status = rpc_spoolss_init(&spoolss_cb);
773         if (!NT_STATUS_IS_OK(status)) {
774                 DEBUG(0, ("Failed to register spoolss rpc inteface! (%s)\n",
775                           nt_errstr(status)));
776                 exit(1);
777         }
778
779         status = dcerpc_binding_vector_new(mem_ctx, &v);
780         if (!NT_STATUS_IS_OK(status)) {
781                 DEBUG(0, ("Failed to create binding vector (%s)\n",
782                           nt_errstr(status)));
783                 exit(1);
784         }
785
786         status = dcerpc_binding_vector_add_np_default(&ndr_table_spoolss, v);
787         if (!NT_STATUS_IS_OK(status)) {
788                 DEBUG(0, ("Failed to add np to binding vector (%s)\n",
789                           nt_errstr(status)));
790                 exit(1);
791         }
792
793         status = rpc_ep_register(ev_ctx, msg_ctx, &ndr_table_spoolss, v);
794         if (!NT_STATUS_IS_OK(status)) {
795                 DEBUG(0, ("Failed to register spoolss endpoint! (%s)\n",
796                           nt_errstr(status)));
797                 exit(1);
798         }
799
800         talloc_free(mem_ctx);
801
802         ok = spoolssd_setup_children_monitor(ev_ctx, msg_ctx);
803         if (!ok) {
804                 DEBUG(0, ("Failed to setup children monitoring!\n"));
805                 exit(1);
806         }
807
808         DEBUG(1, ("SPOOLSS Daemon Started (%d)\n", getpid()));
809
810         pfh_manage_pool(ev_ctx, msg_ctx, &pf_spoolss_cfg, spoolss_pool);
811
812         /* loop forever */
813         ret = tevent_loop_wait(ev_ctx);
814
815         /* should not be reached */
816         DEBUG(0,("spoolssd tevent_loop_wait() exited with %d - %s\n",
817                  ret, (ret == 0) ? "out of events" : strerror(errno)));
818         exit(1);
819 }