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