s3:printing/spoolssd: avoid using server_messaging_context()
[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         load_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         delete_and_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 static void spoolss_chld_sig_hup_handler(struct tevent_context *ev,
228                                          struct tevent_signal *se,
229                                          int signum,
230                                          int count,
231                                          void *siginfo,
232                                          void *pvt)
233 {
234         struct messaging_context *msg_ctx;
235
236         msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);
237
238         change_to_root_user();
239         DEBUG(1,("Reloading printers after SIGHUP\n"));
240         load_printers(ev, msg_ctx);
241         spoolss_reopen_logs(spoolss_child_id);
242 }
243
244 static bool spoolss_setup_chld_hup_handler(struct tevent_context *ev_ctx,
245                                            struct messaging_context *msg_ctx,
246                                            struct pf_worker_data *pf)
247 {
248         struct tevent_signal *se;
249
250         se = tevent_add_signal(ev_ctx,
251                                ev_ctx,
252                                SIGHUP, 0,
253                                spoolss_chld_sig_hup_handler,
254                                msg_ctx);
255         if (!se) {
256                 DEBUG(1, ("failed to setup SIGHUP handler"));
257                 return false;
258         }
259
260         return true;
261 }
262
263 static void parent_ping(struct messaging_context *msg_ctx,
264                         void *private_data,
265                         uint32_t msg_type,
266                         struct server_id server_id,
267                         DATA_BLOB *data)
268 {
269
270         /* The fact we received this message is enough to let make the event
271          * loop if it was idle. spoolss_children_main will cycle through
272          * spoolss_next_client at least once. That function will take whatever
273          * action is necessary */
274
275         DEBUG(10, ("Got message that the parent changed status.\n"));
276         return;
277 }
278
279 static bool spoolss_child_init(struct tevent_context *ev_ctx,
280                                int child_id, struct pf_worker_data *pf)
281 {
282         NTSTATUS status;
283         struct rpc_srv_callbacks spoolss_cb;
284         struct messaging_context *msg_ctx = messaging_init(NULL, ev_ctx);
285         bool ok;
286
287         status = reinit_after_fork(msg_ctx, ev_ctx,
288                                    true);
289         if (!NT_STATUS_IS_OK(status)) {
290                 DEBUG(0,("reinit_after_fork() failed\n"));
291                 smb_panic("reinit_after_fork() failed");
292         }
293
294         spoolss_child_id = child_id;
295         spoolss_reopen_logs(child_id);
296
297         ok = spoolss_setup_chld_hup_handler(ev_ctx, msg_ctx, pf);
298         if (!ok) {
299                 return false;
300         }
301
302         if (!serverid_register(procid_self(),
303                                 FLAG_MSG_GENERAL |
304                                 FLAG_MSG_PRINT_GENERAL)) {
305                 return false;
306         }
307
308         if (!locking_init()) {
309                 return false;
310         }
311
312         messaging_register(msg_ctx, ev_ctx,
313                            MSG_SMB_CONF_UPDATED, smb_conf_updated);
314         messaging_register(msg_ctx, ev_ctx, MSG_PRINTER_PCAP,
315                            pcap_updated);
316         messaging_register(msg_ctx, ev_ctx,
317                            MSG_PREFORK_PARENT_EVENT, parent_ping);
318
319         /* As soon as messaging is up check if pcap has been loaded already.
320          * If so then we probably missed a message and should load_printers()
321          * ourselves. If pcap has not been loaded yet, then ignore, we will get
322          * a message as soon as the bq process completes the reload. */
323         if (pcap_cache_loaded()) {
324                 load_printers(ev_ctx, msg_ctx);
325         }
326
327         /* try to reinit rpc queues */
328         spoolss_cb.init = spoolss_init_cb;
329         spoolss_cb.shutdown = spoolss_shutdown_cb;
330         spoolss_cb.private_data = msg_ctx;
331
332         status = rpc_winreg_init(NULL);
333         if (!NT_STATUS_IS_OK(status)) {
334                 DEBUG(0, ("Failed to register winreg rpc inteface! (%s)\n",
335                           nt_errstr(status)));
336                 return false;
337         }
338
339         status = rpc_spoolss_init(&spoolss_cb);
340         if (!NT_STATUS_IS_OK(status)) {
341                 DEBUG(0, ("Failed to register spoolss rpc inteface! (%s)\n",
342                           nt_errstr(status)));
343                 return false;
344         }
345
346         return true;
347 }
348
349 struct spoolss_children_data {
350         struct tevent_context *ev_ctx;
351         struct messaging_context *msg_ctx;
352         struct pf_worker_data *pf;
353         int listen_fd_size;
354         int *listen_fds;
355 };
356
357 static void spoolss_next_client(void *pvt);
358
359 static int spoolss_children_main(struct tevent_context *ev_ctx,
360                                  struct messaging_context *msg_ctx,
361                                  struct pf_worker_data *pf,
362                                  int child_id,
363                                  int listen_fd_size,
364                                  int *listen_fds,
365                                  void *private_data)
366 {
367         struct spoolss_children_data *data;
368         bool ok;
369         int ret;
370
371         ok = spoolss_child_init(ev_ctx, child_id, pf);
372         if (!ok) {
373                 return 1;
374         }
375
376         data = talloc(ev_ctx, struct spoolss_children_data);
377         if (!data) {
378                 return 1;
379         }
380         data->pf = pf;
381         data->ev_ctx = ev_ctx;
382         data->msg_ctx = msg_ctx;
383         data->listen_fd_size = listen_fd_size;
384         data->listen_fds = listen_fds;
385
386         /* loop until it is time to exit */
387         while (pf->status != PF_WORKER_EXITING) {
388                 /* try to see if it is time to schedule the next client */
389                 spoolss_next_client(data);
390
391                 ret = tevent_loop_once(ev_ctx);
392                 if (ret != 0) {
393                         DEBUG(0, ("tevent_loop_once() exited with %d: %s\n",
394                                   ret, strerror(errno)));
395                         pf->status = PF_WORKER_EXITING;
396                 }
397         }
398
399         return ret;
400 }
401
402 static void spoolss_client_terminated(void *pvt)
403 {
404         struct spoolss_children_data *data;
405
406         data = talloc_get_type_abort(pvt, struct spoolss_children_data);
407
408         pfh_client_terminated(data->pf);
409
410         spoolss_next_client(pvt);
411 }
412
413 struct spoolss_new_client {
414         struct spoolss_children_data *data;
415         struct tsocket_address *srv_addr;
416         struct tsocket_address *cli_addr;
417 };
418
419 static void spoolss_handle_client(struct tevent_req *req);
420
421 static void spoolss_next_client(void *pvt)
422 {
423         struct tevent_req *req;
424         struct spoolss_children_data *data;
425         struct spoolss_new_client *next;
426
427         data = talloc_get_type_abort(pvt, struct spoolss_children_data);
428
429         if (!pfh_child_allowed_to_accept(data->pf)) {
430                 /* nothing to do for now we are already listening
431                  * or we are not allowed to listen further */
432                 return;
433         }
434
435         next = talloc_zero(data, struct spoolss_new_client);
436         if (!next) {
437                 DEBUG(1, ("Out of memory!?\n"));
438                 return;
439         }
440         next->data = data;
441
442         req = prefork_listen_send(next, data->ev_ctx, data->pf,
443                                   data->listen_fd_size,
444                                   data->listen_fds);
445         if (!req) {
446                 DEBUG(1, ("Failed to make listening request!?\n"));
447                 talloc_free(next);
448                 return;
449         }
450         tevent_req_set_callback(req, spoolss_handle_client, next);
451 }
452
453 static void spoolss_handle_client(struct tevent_req *req)
454 {
455         struct spoolss_children_data *data;
456         struct spoolss_new_client *client;
457         const DATA_BLOB ping = data_blob_null;
458         int ret;
459         int sd;
460
461         client = tevent_req_callback_data(req, struct spoolss_new_client);
462         data = client->data;
463
464         ret = prefork_listen_recv(req, client, &sd,
465                                   &client->srv_addr, &client->cli_addr);
466
467         /* this will free the request too */
468         talloc_free(client);
469
470         if (ret != 0) {
471                 DEBUG(6, ("No client connection was available after all!\n"));
472                 return;
473         }
474
475         /* Warn parent that our status changed */
476         messaging_send(data->msg_ctx, parent_id,
477                         MSG_PREFORK_CHILD_EVENT, &ping);
478
479         DEBUG(2, ("Spoolss preforked child %d got client connection!\n",
480                   (int)(data->pf->pid)));
481
482         named_pipe_accept_function(data->ev_ctx, data->msg_ctx,
483                                    SPOOLSS_PIPE_NAME, sd,
484                                    spoolss_client_terminated, data);
485 }
486
487 /* ==== Main Process Functions ==== */
488
489 extern pid_t background_lpq_updater_pid;
490 static char *bq_logfile;
491
492 static void check_updater_child(struct tevent_context *ev_ctx,
493                                 struct messaging_context *msg_ctx)
494 {
495         int status;
496         pid_t pid;
497
498         if (background_lpq_updater_pid == -1) {
499                 return;
500         }
501
502         pid = sys_waitpid(background_lpq_updater_pid, &status, WNOHANG);
503         if (pid > 0) {
504                 DEBUG(2, ("The background queue child died... Restarting!\n"));
505                 pid = start_background_queue(ev_ctx, msg_ctx, bq_logfile);
506                 background_lpq_updater_pid = pid;
507         }
508 }
509
510 static void child_ping(struct messaging_context *msg_ctx,
511                         void *private_data,
512                         uint32_t msg_type,
513                         struct server_id server_id,
514                         DATA_BLOB *data)
515 {
516         struct tevent_context *ev_ctx;
517
518         ev_ctx = talloc_get_type_abort(private_data, struct tevent_context);
519
520         DEBUG(10, ("Got message that a child changed status.\n"));
521         pfh_manage_pool(ev_ctx, msg_ctx, &pf_spoolss_cfg, spoolss_pool);
522 }
523
524 static bool spoolssd_schedule_check(struct tevent_context *ev_ctx,
525                                     struct messaging_context *msg_ctx,
526                                     struct timeval current_time);
527 static void spoolssd_check_children(struct tevent_context *ev_ctx,
528                                     struct tevent_timer *te,
529                                     struct timeval current_time,
530                                     void *pvt);
531
532 static void spoolssd_sigchld_handler(struct tevent_context *ev_ctx,
533                                      struct prefork_pool *pfp,
534                                      void *pvt)
535 {
536         struct messaging_context *msg_ctx;
537
538         msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);
539
540         /* run pool management so we can fork/retire or increase
541          * the allowed connections per child based on load */
542         pfh_manage_pool(ev_ctx, msg_ctx, &pf_spoolss_cfg, spoolss_pool);
543
544         /* also check if the updater child is alive and well */
545         check_updater_child(ev_ctx, msg_ctx);
546 }
547
548 static bool spoolssd_setup_children_monitor(struct tevent_context *ev_ctx,
549                                             struct messaging_context *msg_ctx)
550 {
551         bool ok;
552
553         /* add our oun sigchld callback */
554         prefork_set_sigchld_callback(spoolss_pool,
555                                      spoolssd_sigchld_handler, msg_ctx);
556
557         ok = spoolssd_schedule_check(ev_ctx, msg_ctx,
558                                      tevent_timeval_current());
559         return ok;
560 }
561
562 static bool spoolssd_schedule_check(struct tevent_context *ev_ctx,
563                                     struct messaging_context *msg_ctx,
564                                     struct timeval current_time)
565 {
566         struct tevent_timer *te;
567         struct timeval next_event;
568
569         /* check situation again in 10 seconds */
570         next_event = tevent_timeval_current_ofs(10, 0);
571
572         /* TODO: check when the socket becomes readable, so that children
573          * are checked only when there is some activity ? */
574         te = tevent_add_timer(ev_ctx, spoolss_pool, next_event,
575                                 spoolssd_check_children, msg_ctx);
576         if (!te) {
577                 DEBUG(2, ("Failed to set up children monitoring!\n"));
578                 return false;
579         }
580
581         return true;
582 }
583
584 static void spoolssd_check_children(struct tevent_context *ev_ctx,
585                                     struct tevent_timer *te,
586                                     struct timeval current_time,
587                                     void *pvt)
588 {
589         struct messaging_context *msg_ctx;
590
591         msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);
592
593         pfh_manage_pool(ev_ctx, msg_ctx, &pf_spoolss_cfg, spoolss_pool);
594
595         spoolssd_schedule_check(ev_ctx, msg_ctx, current_time);
596 }
597
598 static void print_queue_forward(struct messaging_context *msg,
599                                 void *private_data,
600                                 uint32_t msg_type,
601                                 struct server_id server_id,
602                                 DATA_BLOB *data)
603 {
604         messaging_send_buf(msg, pid_to_procid(background_lpq_updater_pid),
605                            MSG_PRINTER_UPDATE, data->data, data->length);
606 }
607
608 static char *get_bq_logfile(void)
609 {
610         char *lfile = lp_logfile();
611         int rc;
612
613         if (lfile == NULL || lfile[0] == '\0') {
614                 rc = asprintf(&lfile, "%s/log.%s.bq",
615                                         get_dyn_LOGFILEBASE(), DAEMON_NAME);
616         } else {
617                 rc = asprintf(&lfile, "%s.bq", lp_logfile());
618         }
619         if (rc == -1) {
620                 lfile = NULL;
621         }
622         return lfile;
623 }
624
625 pid_t start_spoolssd(struct tevent_context *ev_ctx,
626                     struct messaging_context *msg_ctx)
627 {
628         struct rpc_srv_callbacks spoolss_cb;
629         struct dcerpc_binding_vector *v;
630         TALLOC_CTX *mem_ctx;
631         pid_t pid;
632         NTSTATUS status;
633         int listen_fd;
634         int ret;
635         bool ok;
636
637         DEBUG(1, ("Forking SPOOLSS Daemon\n"));
638
639         /*
640          * Block signals before forking child as it will have to
641          * set its own handlers. Child will re-enable SIGHUP as
642          * soon as the handlers are set up.
643          */
644         BlockSignals(true, SIGTERM);
645         BlockSignals(true, SIGHUP);
646
647         pid = sys_fork();
648
649         if (pid == -1) {
650                 DEBUG(0, ("Failed to fork SPOOLSS [%s]\n",
651                            strerror(errno)));
652         }
653
654         /* parent or error */
655         if (pid != 0) {
656
657                 /* Re-enable SIGHUP before returnig */
658                 BlockSignals(false, SIGTERM);
659                 BlockSignals(false, SIGHUP);
660                 return pid;
661         }
662
663         /* child */
664         close_low_fds(false);
665
666         status = reinit_after_fork(msg_ctx,
667                                    ev_ctx,
668                                    true);
669         if (!NT_STATUS_IS_OK(status)) {
670                 DEBUG(0,("reinit_after_fork() failed\n"));
671                 smb_panic("reinit_after_fork() failed");
672         }
673
674         /* save the parent process id so the children can use it later */
675         parent_id = procid_self();
676
677         spoolss_reopen_logs(0);
678         pfh_daemon_config(DAEMON_NAME,
679                           &pf_spoolss_cfg,
680                           &default_pf_spoolss_cfg);
681
682         spoolss_setup_sig_term_handler(ev_ctx);
683         spoolss_setup_sig_hup_handler(ev_ctx, msg_ctx);
684
685         BlockSignals(false, SIGTERM);
686         BlockSignals(false, SIGHUP);
687
688         /* always start the backgroundqueue listner in spoolssd */
689         bq_logfile = get_bq_logfile();
690         pid = start_background_queue(ev_ctx, msg_ctx, bq_logfile);
691         if (pid > 0) {
692                 background_lpq_updater_pid = pid;
693         }
694
695         /* the listening fd must be created before the children are actually
696          * forked out. */
697         listen_fd = create_named_pipe_socket(SPOOLSS_PIPE_NAME);
698         if (listen_fd == -1) {
699                 exit(1);
700         }
701
702         ret = listen(listen_fd, pf_spoolss_cfg.max_allowed_clients);
703         if (ret == -1) {
704                 DEBUG(0, ("Failed to listen on spoolss pipe - %s\n",
705                           strerror(errno)));
706                 exit(1);
707         }
708
709         /* start children before any more initialization is done */
710         ok = prefork_create_pool(ev_ctx, /* mem_ctx */
711                                  ev_ctx, msg_ctx,
712                                  1, &listen_fd,
713                                  pf_spoolss_cfg.min_children,
714                                  pf_spoolss_cfg.max_children,
715                                  &spoolss_children_main, NULL,
716                                  &spoolss_pool);
717         if (!ok) {
718                 exit(1);
719         }
720
721         if (!serverid_register(procid_self(),
722                                 FLAG_MSG_GENERAL |
723                                 FLAG_MSG_PRINT_GENERAL)) {
724                 exit(1);
725         }
726
727         if (!locking_init()) {
728                 exit(1);
729         }
730
731         messaging_register(msg_ctx, ev_ctx,
732                            MSG_SMB_CONF_UPDATED, smb_conf_updated);
733         messaging_register(msg_ctx, NULL, MSG_PRINTER_UPDATE,
734                            print_queue_forward);
735         messaging_register(msg_ctx, ev_ctx, MSG_PRINTER_PCAP,
736                            pcap_updated);
737         messaging_register(msg_ctx, ev_ctx,
738                            MSG_PREFORK_CHILD_EVENT, child_ping);
739
740         /* As soon as messaging is up check if pcap has been loaded already.
741          * If so then we probably missed a message and should load_printers()
742          * ourselves. If pcap has not been loaded yet, then ignore, we will get
743          * a message as soon as the bq process completes the reload. */
744         if (pcap_cache_loaded()) {
745                 load_printers(ev_ctx, msg_ctx);
746         }
747
748         mem_ctx = talloc_new(NULL);
749         if (mem_ctx == NULL) {
750                 exit(1);
751         }
752
753         /*
754          * Initialize spoolss with an init function to convert printers first.
755          * static_init_rpc will try to initialize the spoolss server too but you
756          * can't register it twice.
757          */
758         spoolss_cb.init = spoolss_init_cb;
759         spoolss_cb.shutdown = spoolss_shutdown_cb;
760         spoolss_cb.private_data = msg_ctx;
761
762         status = rpc_winreg_init(NULL);
763         if (!NT_STATUS_IS_OK(status)) {
764                 DEBUG(0, ("Failed to register winreg rpc inteface! (%s)\n",
765                           nt_errstr(status)));
766                 exit(1);
767         }
768
769         status = rpc_spoolss_init(&spoolss_cb);
770         if (!NT_STATUS_IS_OK(status)) {
771                 DEBUG(0, ("Failed to register spoolss rpc inteface! (%s)\n",
772                           nt_errstr(status)));
773                 exit(1);
774         }
775
776         status = dcerpc_binding_vector_new(mem_ctx, &v);
777         if (!NT_STATUS_IS_OK(status)) {
778                 DEBUG(0, ("Failed to create binding vector (%s)\n",
779                           nt_errstr(status)));
780                 exit(1);
781         }
782
783         status = dcerpc_binding_vector_add_np_default(&ndr_table_spoolss, v);
784         if (!NT_STATUS_IS_OK(status)) {
785                 DEBUG(0, ("Failed to add np to binding vector (%s)\n",
786                           nt_errstr(status)));
787                 exit(1);
788         }
789
790         status = rpc_ep_register(ev_ctx, msg_ctx, &ndr_table_spoolss, v);
791         if (!NT_STATUS_IS_OK(status)) {
792                 DEBUG(0, ("Failed to register spoolss endpoint! (%s)\n",
793                           nt_errstr(status)));
794                 exit(1);
795         }
796
797         talloc_free(mem_ctx);
798
799         ok = spoolssd_setup_children_monitor(ev_ctx, msg_ctx);
800         if (!ok) {
801                 DEBUG(0, ("Failed to setup children monitoring!\n"));
802                 exit(1);
803         }
804
805         DEBUG(1, ("SPOOLSS Daemon Started (%d)\n", getpid()));
806
807         pfh_manage_pool(ev_ctx, msg_ctx, &pf_spoolss_cfg, spoolss_pool);
808
809         /* loop forever */
810         ret = tevent_loop_wait(ev_ctx);
811
812         /* should not be reached */
813         DEBUG(0,("spoolssd tevent_loop_wait() exited with %d - %s\n",
814                  ret, (ret == 0) ? "out of events" : strerror(errno)));
815         exit(1);
816 }