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