s3:libsmb: make use of smb1cli_tcon_set_values()
[mat/samba.git] / source3 / printing / spoolssd.c
index e7bee0e0ebd379b9c27d606367c1b55ce09413d4..8f182f824c7c384a3b0eceb5250cf7b01eb4714e 100644 (file)
@@ -1,7 +1,7 @@
 /*
    Unix SMB/Netbios implementation.
    SPOOLSS Daemon
-   Copyright (C) Simo Sorce 2010
+   Copyright (C) Simo Sorce <idra@samba.org> 2010-2011
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
 #include "serverid.h"
 #include "smbd/smbd.h"
 
+#include "lib/util/util_process.h"
+
 #include "messages.h"
 #include "include/printing.h"
 #include "printing/nt_printing_migrate_internal.h"
+#include "printing/queue_process.h"
+#include "printing/pcap.h"
+#include "printing/load.h"
 #include "ntdomain.h"
 #include "librpc/gen_ndr/srv_winreg.h"
 #include "librpc/gen_ndr/srv_spoolss.h"
 #include "rpc_server/spoolss/srv_spoolss_nt.h"
 #include "librpc/rpc/dcerpc_ep.h"
 #include "lib/server_prefork.h"
+#include "lib/server_prefork_util.h"
 
 #define SPOOLSS_PIPE_NAME "spoolss"
 #define DAEMON_NAME "spoolssd"
 
-#define SPOOLSS_MIN_CHILDREN 5
-#define SPOOLSS_MAX_CHILDREN 25
-#define SPOOLSS_SPAWN_RATE 5
-#define SPOOLSS_MIN_LIFE 60 /* 1 minute minimum life time */
+static struct server_id parent_id;
+static struct prefork_pool *spoolss_pool = NULL;
+static int spoolss_child_id = 0;
+
+static struct pf_daemon_config default_pf_spoolss_cfg = {
+       .prefork_status = PFH_INIT,
+       .min_children = 5,
+       .max_children = 25,
+       .spawn_rate = 5,
+       .max_allowed_clients = 100,
+       .child_min_life = 60 /* 1 minute minimum life time */
+};
+static struct pf_daemon_config pf_spoolss_cfg = { 0 };
 
-void start_spoolssd(struct tevent_context *ev_ctx,
-                   struct messaging_context *msg_ctx);
+pid_t start_spoolssd(struct tevent_context *ev_ctx,
+                    struct messaging_context *msg_ctx);
 
-static void spoolss_reopen_logs(void)
+static void spoolss_reopen_logs(int child_id)
 {
-       char *lfile = lp_logfile();
+       char *lfile = lp_logfile(talloc_tos());
+       char *ext;
        int rc;
 
+       if (child_id) {
+               rc = asprintf(&ext, "%s.%d", DAEMON_NAME, child_id);
+       } else {
+               rc = asprintf(&ext, "%s", DAEMON_NAME);
+       }
+
+       if (rc == -1) {
+               return;
+       }
+
+       rc = 0;
        if (lfile == NULL || lfile[0] == '\0') {
-               rc = asprintf(&lfile, "%s/log.%s", get_dyn_LOGFILEBASE(), DAEMON_NAME);
-               if (rc > 0) {
-                       lp_set_logfile(lfile);
-                       SAFE_FREE(lfile);
-               }
+               rc = asprintf(&lfile, "%s/log.%s",
+                             get_dyn_LOGFILEBASE(), ext);
        } else {
-               if (strstr(lfile, DAEMON_NAME) == NULL) {
-                       rc = asprintf(&lfile, "%s.%s", lp_logfile(), DAEMON_NAME);
-                       if (rc > 0) {
-                               lp_set_logfile(lfile);
-                               SAFE_FREE(lfile);
+               if (strstr(lfile, ext) == NULL) {
+                       if (child_id) {
+                               rc = asprintf(&lfile, "%s.%d",
+                                             lp_logfile(talloc_tos()),
+                                             child_id);
+                       } else {
+                               rc = asprintf(&lfile, "%s.%s",
+                                             lp_logfile(talloc_tos()),
+                                             ext);
                        }
                }
        }
 
+       if (rc > 0) {
+               lp_set_logfile(lfile);
+               SAFE_FREE(lfile);
+       }
+
+       SAFE_FREE(ext);
+
        reopen_logs();
 }
 
+static void update_conf(struct tevent_context *ev,
+                       struct messaging_context *msg)
+{
+       change_to_root_user();
+       lp_load(get_dyn_CONFIGFILE(), true, false, false, true);
+       load_printers(ev, msg);
+
+       spoolss_reopen_logs(spoolss_child_id);
+       if (spoolss_child_id == 0) {
+               pfh_daemon_config(DAEMON_NAME,
+                                 &pf_spoolss_cfg,
+                                 &default_pf_spoolss_cfg);
+               pfh_manage_pool(ev, msg, &pf_spoolss_cfg, spoolss_pool);
+       }
+}
+
 static void smb_conf_updated(struct messaging_context *msg,
                             void *private_data,
                             uint32_t msg_type,
@@ -77,9 +128,28 @@ static void smb_conf_updated(struct messaging_context *msg,
                                                             struct tevent_context);
 
        DEBUG(10, ("Got message saying smb.conf was updated. Reloading.\n"));
+       update_conf(ev_ctx, msg);
+}
+
+static void update_pcap(struct tevent_context *ev_ctx,
+                       struct messaging_context *msg_ctx)
+{
        change_to_root_user();
-       reload_printers(ev_ctx, msg);
-       spoolss_reopen_logs();
+       delete_and_reload_printers(ev_ctx, msg_ctx);
+}
+
+static void pcap_updated(struct messaging_context *msg,
+                        void *private_data,
+                        uint32_t msg_type,
+                        struct server_id server_id,
+                        DATA_BLOB *data)
+{
+       struct tevent_context *ev_ctx;
+
+       ev_ctx = talloc_get_type_abort(private_data, struct tevent_context);
+
+       DEBUG(10, ("Got message that pcap updated. Reloading.\n"));
+       update_pcap(ev_ctx, msg);
 }
 
 static void spoolss_sig_term_handler(struct tevent_context *ev,
@@ -117,10 +187,13 @@ static void spoolss_sig_hup_handler(struct tevent_context *ev,
 
        msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);
 
-       change_to_root_user();
        DEBUG(1,("Reloading printers after SIGHUP\n"));
-       reload_printers(ev, msg_ctx);
-       spoolss_reopen_logs();
+       update_conf(ev, msg_ctx);
+
+       /* relay to all children */
+       if (spoolss_pool) {
+               prefork_send_signal_to_all(spoolss_pool, SIGHUP);
+       }
 }
 
 static void spoolss_setup_sig_hup_handler(struct tevent_context *ev_ctx,
@@ -153,12 +226,7 @@ static bool spoolss_shutdown_cb(void *ptr)
        return true;
 }
 
-/* Childrens */
-
-struct spoolss_chld_sig_hup_ctx {
-       struct messaging_context *msg_ctx;
-       struct pf_worker_data *pf;
-};
+/* Children */
 
 static void spoolss_chld_sig_hup_handler(struct tevent_context *ev,
                                         struct tevent_signal *se,
@@ -167,42 +235,27 @@ static void spoolss_chld_sig_hup_handler(struct tevent_context *ev,
                                         void *siginfo,
                                         void *pvt)
 {
-       struct spoolss_chld_sig_hup_ctx *shc;
-
-       shc = talloc_get_type_abort(pvt, struct spoolss_chld_sig_hup_ctx);
+       struct messaging_context *msg_ctx;
 
-       /* avoid wasting CPU cycles if we are going to exit soon anyways */
-       if (shc->pf != NULL &&
-           shc->pf->cmds == PF_SRV_MSG_EXIT) {
-               return;
-       }
+       msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);
 
        change_to_root_user();
        DEBUG(1,("Reloading printers after SIGHUP\n"));
-       reload_printers(ev, shc->msg_ctx);
-       spoolss_reopen_logs();
+       load_printers(ev, msg_ctx);
+       spoolss_reopen_logs(spoolss_child_id);
 }
 
 static bool spoolss_setup_chld_hup_handler(struct tevent_context *ev_ctx,
-                                          struct pf_worker_data *pf,
-                                          struct messaging_context *msg_ctx)
+                                          struct messaging_context *msg_ctx,
+                                          struct pf_worker_data *pf)
 {
-       struct spoolss_chld_sig_hup_ctx *shc;
        struct tevent_signal *se;
 
-       shc = talloc(ev_ctx, struct spoolss_chld_sig_hup_ctx);
-       if (!shc) {
-               DEBUG(1, ("failed to setup SIGHUP handler"));
-               return false;
-       }
-       shc->pf = pf;
-       shc->msg_ctx = msg_ctx;
-
        se = tevent_add_signal(ev_ctx,
                               ev_ctx,
                               SIGHUP, 0,
                               spoolss_chld_sig_hup_handler,
-                              shc);
+                              msg_ctx);
        if (!se) {
                DEBUG(1, ("failed to setup SIGHUP handler"));
                return false;
@@ -211,29 +264,50 @@ static bool spoolss_setup_chld_hup_handler(struct tevent_context *ev_ctx,
        return true;
 }
 
+static void parent_ping(struct messaging_context *msg_ctx,
+                       void *private_data,
+                       uint32_t msg_type,
+                       struct server_id server_id,
+                       DATA_BLOB *data)
+{
+
+       /* The fact we received this message is enough to let make the event
+        * loop if it was idle. spoolss_children_main will cycle through
+        * spoolss_next_client at least once. That function will take whatever
+        * action is necessary */
+
+       DEBUG(10, ("Got message that the parent changed status.\n"));
+       return;
+}
+
 static bool spoolss_child_init(struct tevent_context *ev_ctx,
-                                       struct pf_worker_data *pf)
+                              int child_id, struct pf_worker_data *pf)
 {
        NTSTATUS status;
        struct rpc_srv_callbacks spoolss_cb;
-       struct messaging_context *msg_ctx = server_messaging_context();
+       struct messaging_context *msg_ctx = messaging_init(NULL, ev_ctx);
        bool ok;
 
        status = reinit_after_fork(msg_ctx, ev_ctx,
-                                  procid_self(), true);
+                                  true);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(0,("reinit_after_fork() failed\n"));
                smb_panic("reinit_after_fork() failed");
        }
 
-       spoolss_reopen_logs();
+       prctl_set_comment("spoolssd-child");
+
+       spoolss_child_id = child_id;
+       spoolss_reopen_logs(child_id);
 
-       ok = spoolss_setup_chld_hup_handler(ev_ctx, pf, msg_ctx);
+       ok = spoolss_setup_chld_hup_handler(ev_ctx, msg_ctx, pf);
        if (!ok) {
                return false;
        }
 
-       if (!serverid_register(procid_self(), FLAG_MSG_GENERAL)) {
+       if (!serverid_register(messaging_server_id(msg_ctx),
+                               FLAG_MSG_GENERAL |
+                               FLAG_MSG_PRINT_GENERAL)) {
                return false;
        }
 
@@ -243,6 +317,18 @@ static bool spoolss_child_init(struct tevent_context *ev_ctx,
 
        messaging_register(msg_ctx, ev_ctx,
                           MSG_SMB_CONF_UPDATED, smb_conf_updated);
+       messaging_register(msg_ctx, ev_ctx, MSG_PRINTER_PCAP,
+                          pcap_updated);
+       messaging_register(msg_ctx, ev_ctx,
+                          MSG_PREFORK_PARENT_EVENT, parent_ping);
+
+       /* As soon as messaging is up check if pcap has been loaded already.
+        * If so then we probably missed a message and should load_printers()
+        * ourselves. If pcap has not been loaded yet, then ignore, we will get
+        * a message as soon as the bq process completes the reload. */
+       if (pcap_cache_loaded()) {
+               load_printers(ev_ctx, msg_ctx);
+       }
 
        /* try to reinit rpc queues */
        spoolss_cb.init = spoolss_init_cb;
@@ -263,8 +349,6 @@ static bool spoolss_child_init(struct tevent_context *ev_ctx,
                return false;
        }
 
-       reload_printers(ev_ctx, msg_ctx);
-
        return true;
 }
 
@@ -272,26 +356,25 @@ struct spoolss_children_data {
        struct tevent_context *ev_ctx;
        struct messaging_context *msg_ctx;
        struct pf_worker_data *pf;
-       int listen_fd;
-       int lock_fd;
+       int listen_fd_size;
+       int *listen_fds;
 };
 
-static void spoolss_schedule_loop(void *pvt);
-static void spoolss_children_loop(struct tevent_context *ev_ctx,
-                                 struct tevent_immediate *im,
-                                 void *pvt);
+static void spoolss_next_client(void *pvt);
 
 static int spoolss_children_main(struct tevent_context *ev_ctx,
+                                struct messaging_context *msg_ctx,
                                 struct pf_worker_data *pf,
-                                int listen_fd, int lock_fd,
+                                int child_id,
+                                int listen_fd_size,
+                                int *listen_fds,
                                 void *private_data)
 {
-       struct messaging_context *msg_ctx = server_messaging_context();
        struct spoolss_children_data *data;
        bool ok;
        int ret;
 
-       ok = spoolss_child_init(ev_ctx, pf);
+       ok = spoolss_child_init(ev_ctx, child_id, pf);
        if (!ok) {
                return 1;
        }
@@ -303,13 +386,14 @@ static int spoolss_children_main(struct tevent_context *ev_ctx,
        data->pf = pf;
        data->ev_ctx = ev_ctx;
        data->msg_ctx = msg_ctx;
-       data->lock_fd = lock_fd;
-       data->listen_fd = listen_fd;
-
-       spoolss_schedule_loop(data);
+       data->listen_fd_size = listen_fd_size;
+       data->listen_fds = listen_fds;
 
        /* loop until it is time to exit */
        while (pf->status != PF_WORKER_EXITING) {
+               /* try to see if it is time to schedule the next client */
+               spoolss_next_client(data);
+
                ret = tevent_loop_once(ev_ctx);
                if (ret != 0) {
                        DEBUG(0, ("tevent_loop_once() exited with %d: %s\n",
@@ -327,72 +411,78 @@ static void spoolss_client_terminated(void *pvt)
 
        data = talloc_get_type_abort(pvt, struct spoolss_children_data);
 
-       if (data->pf->num_clients) {
-               data->pf->num_clients--;
-       } else {
-               DEBUG(2, ("Invalid num clients, aborting!\n"));
-               data->pf->status = PF_WORKER_EXITING;
-               return;
-       }
+       pfh_client_terminated(data->pf);
 
-       spoolss_schedule_loop(pvt);
+       spoolss_next_client(pvt);
 }
 
-static void spoolss_schedule_loop(void *pvt)
+struct spoolss_new_client {
+       struct spoolss_children_data *data;
+       struct tsocket_address *srv_addr;
+       struct tsocket_address *cli_addr;
+};
+
+static void spoolss_handle_client(struct tevent_req *req);
+
+static void spoolss_next_client(void *pvt)
 {
+       struct tevent_req *req;
        struct spoolss_children_data *data;
-       struct tevent_immediate *im;
+       struct spoolss_new_client *next;
 
        data = talloc_get_type_abort(pvt, struct spoolss_children_data);
 
-       if (data->pf->num_clients == 0) {
-               data->pf->status = PF_WORKER_IDLE;
+       if (!pfh_child_allowed_to_accept(data->pf)) {
+               /* nothing to do for now we are already listening
+                * or we are not allowed to listen further */
+               return;
        }
 
-       if (data->pf->cmds == PF_SRV_MSG_EXIT) {
-               DEBUG(2, ("Parent process commands we terminate!\n"));
+       next = talloc_zero(data, struct spoolss_new_client);
+       if (!next) {
+               DEBUG(1, ("Out of memory!?\n"));
                return;
        }
+       next->data = data;
 
-       im = tevent_create_immediate(data);
-       if (!im) {
-               DEBUG(1, ("Failed to create immediate event!\n"));
+       req = prefork_listen_send(next, data->ev_ctx, data->pf,
+                                 data->listen_fd_size,
+                                 data->listen_fds);
+       if (!req) {
+               DEBUG(1, ("Failed to make listening request!?\n"));
+               talloc_free(next);
                return;
        }
-
-       tevent_schedule_immediate(im, data->ev_ctx,
-                                 spoolss_children_loop, data);
+       tevent_req_set_callback(req, spoolss_handle_client, next);
 }
 
-static void spoolss_children_loop(struct tevent_context *ev_ctx,
-                                 struct tevent_immediate *im,
-                                 void *pvt)
+static void spoolss_handle_client(struct tevent_req *req)
 {
        struct spoolss_children_data *data;
-       struct sockaddr_un sunaddr;
-       socklen_t addrlen = sizeof(sunaddr);
+       struct spoolss_new_client *client;
+       const DATA_BLOB ping = data_blob_null;
        int ret;
        int sd;
 
-       data = talloc_get_type_abort(pvt, struct spoolss_children_data);
+       client = tevent_req_callback_data(req, struct spoolss_new_client);
+       data = client->data;
 
+       ret = prefork_listen_recv(req, client, &sd,
+                                 &client->srv_addr, &client->cli_addr);
 
-       /* FIXME: this call is blocking. */
-       ret = prefork_wait_for_client(data->pf, data->lock_fd, data->listen_fd,
-                                       (struct sockaddr *)(void *)&sunaddr,
-                                       &addrlen, &sd);
-       if (ret > 0) {
-               DEBUG(1, ("Failed to accept connection!\n"));
-               return;
-       }
+       /* this will free the request too */
+       talloc_free(client);
 
-       if (ret == -2) {
-               DEBUG(1, ("Server asks us to die!\n"));
-               data->pf->status = PF_WORKER_EXITING;
+       if (ret != 0) {
+               DEBUG(6, ("No client connection was available after all!\n"));
                return;
        }
 
-       DEBUG(2, ("Spoolss preforked child %d activated!\n",
+       /* Warn parent that our status changed */
+       messaging_send(data->msg_ctx, parent_id,
+                       MSG_PREFORK_CHILD_EVENT, &ping);
+
+       DEBUG(2, ("Spoolss preforked child %d got client connection!\n",
                  (int)(data->pf->pid)));
 
        named_pipe_accept_function(data->ev_ctx, data->msg_ctx,
@@ -402,88 +492,81 @@ static void spoolss_children_loop(struct tevent_context *ev_ctx,
 
 /* ==== Main Process Functions ==== */
 
-static void spoolssd_sig_chld_handler(struct tevent_context *ev_ctx,
-                                     struct tevent_signal *se,
-                                     int signum, int count,
-                                     void *siginfo, void *pvt)
+extern pid_t background_lpq_updater_pid;
+static char *bq_logfile;
+
+static void check_updater_child(struct tevent_context *ev_ctx,
+                               struct messaging_context *msg_ctx)
 {
-       struct prefork_pool *pfp;
-       pid_t pid;
        int status;
-       bool ok;
-       int active, total;
-       int n, r;
-
-       pfp = talloc_get_type_abort(pvt, struct prefork_pool);
-
-       while ((pid = sys_waitpid(-1, &status, WNOHANG)) > 0) {
-               ok = prefork_mark_pid_dead(pfp, pid);
-               if (!ok) {
-                       DEBUG(1, ("Pid %d was not found in children pool!\n",
-                                 (int)pid));
-               }
-       }
-
-       /* now check we do not descent below the minimum */
-       active = prefork_count_active_children(pfp, &total);
+       pid_t pid;
 
-       n = 0;
-       if (total < SPOOLSS_MIN_CHILDREN) {
-               n = total - SPOOLSS_MIN_CHILDREN;
-       } else if (total - active < (total / 4)) {
-               n = SPOOLSS_MIN_CHILDREN;
+       if (background_lpq_updater_pid == -1) {
+               return;
        }
 
-       if (n > 0) {
-               r = prefork_add_children(ev_ctx, pfp, n);
-               if (r < n) {
-                       DEBUG(10, ("Tried to start %d children but only,"
-                                  "%d were actually started.!\n", n, r));
-               }
+       pid = sys_waitpid(background_lpq_updater_pid, &status, WNOHANG);
+       if (pid > 0) {
+               DEBUG(2, ("The background queue child died... Restarting!\n"));
+               pid = start_background_queue(ev_ctx, msg_ctx, bq_logfile);
+               background_lpq_updater_pid = pid;
        }
-
-
 }
 
-static bool spoolssd_setup_sig_chld_handler(struct tevent_context *ev_ctx,
-                                           struct prefork_pool *pfp)
+static void child_ping(struct messaging_context *msg_ctx,
+                       void *private_data,
+                       uint32_t msg_type,
+                       struct server_id server_id,
+                       DATA_BLOB *data)
 {
-       struct tevent_signal *se;
+       struct tevent_context *ev_ctx;
 
-       se = tevent_add_signal(ev_ctx, ev_ctx, SIGCHLD, 0,
-                               spoolssd_sig_chld_handler, pfp);
-       if (!se) {
-               DEBUG(0, ("Failed to setup SIGCHLD handler!\n"));
-               return false;
-       }
+       ev_ctx = talloc_get_type_abort(private_data, struct tevent_context);
 
-       return true;
+       DEBUG(10, ("Got message that a child changed status.\n"));
+       pfh_manage_pool(ev_ctx, msg_ctx, &pf_spoolss_cfg, spoolss_pool);
 }
 
 static bool spoolssd_schedule_check(struct tevent_context *ev_ctx,
-                                   struct prefork_pool *pfp,
+                                   struct messaging_context *msg_ctx,
                                    struct timeval current_time);
 static void spoolssd_check_children(struct tevent_context *ev_ctx,
                                    struct tevent_timer *te,
                                    struct timeval current_time,
                                    void *pvt);
 
+static void spoolssd_sigchld_handler(struct tevent_context *ev_ctx,
+                                    struct prefork_pool *pfp,
+                                    void *pvt)
+{
+       struct messaging_context *msg_ctx;
+
+       msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);
+
+       /* run pool management so we can fork/retire or increase
+        * the allowed connections per child based on load */
+       pfh_manage_pool(ev_ctx, msg_ctx, &pf_spoolss_cfg, spoolss_pool);
+
+       /* also check if the updater child is alive and well */
+       check_updater_child(ev_ctx, msg_ctx);
+}
+
 static bool spoolssd_setup_children_monitor(struct tevent_context *ev_ctx,
-                                           struct prefork_pool *pfp)
+                                           struct messaging_context *msg_ctx)
 {
        bool ok;
 
-       ok = spoolssd_setup_sig_chld_handler(ev_ctx, pfp);
-       if (!ok) {
-               return false;
-       }
+       /* add our oun sigchld callback */
+       prefork_set_sigchld_callback(spoolss_pool,
+                                    spoolssd_sigchld_handler, msg_ctx);
 
-       ok = spoolssd_schedule_check(ev_ctx, pfp, tevent_timeval_current());
+       ok = spoolssd_schedule_check(ev_ctx, msg_ctx,
+                                    tevent_timeval_current());
        return ok;
 }
 
 static bool spoolssd_schedule_check(struct tevent_context *ev_ctx,
-                                   struct prefork_pool *pfp,
+                                   struct messaging_context *msg_ctx,
                                    struct timeval current_time)
 {
        struct tevent_timer *te;
@@ -492,10 +575,10 @@ static bool spoolssd_schedule_check(struct tevent_context *ev_ctx,
        /* check situation again in 10 seconds */
        next_event = tevent_timeval_current_ofs(10, 0);
 
-       /* check when the socket becomes readable, so that children
-        * are checked only when there is some activity */
-       te = tevent_add_timer(ev_ctx, pfp, next_event,
-                               spoolssd_check_children, pfp);
+       /* TODO: check when the socket becomes readable, so that children
+        * are checked only when there is some activity */
+       te = tevent_add_timer(ev_ctx, spoolss_pool, next_event,
+                               spoolssd_check_children, msg_ctx);
        if (!te) {
                DEBUG(2, ("Failed to set up children monitoring!\n"));
                return false;
@@ -509,36 +592,45 @@ static void spoolssd_check_children(struct tevent_context *ev_ctx,
                                    struct timeval current_time,
                                    void *pvt)
 {
-       struct prefork_pool *pfp;
-       int active, total;
-       int ret, n;
+       struct messaging_context *msg_ctx;
 
-       pfp = talloc_get_type_abort(pvt, struct prefork_pool);
+       msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);
 
-       active = prefork_count_active_children(pfp, &total);
+       pfh_manage_pool(ev_ctx, msg_ctx, &pf_spoolss_cfg, spoolss_pool);
 
-       if (total - active < SPOOLSS_SPAWN_RATE) {
-               n = prefork_add_children(ev_ctx, pfp, SPOOLSS_SPAWN_RATE);
-               if (n < SPOOLSS_SPAWN_RATE) {
-                       DEBUG(10, ("Tried to start 5 children but only,"
-                                  "%d were actually started.!\n", n));
-               }
-       }
+       spoolssd_schedule_check(ev_ctx, msg_ctx, current_time);
+}
 
-       if (total - active > SPOOLSS_MIN_CHILDREN) {
-               if ((total - SPOOLSS_MIN_CHILDREN) >= SPOOLSS_SPAWN_RATE) {
-                       prefork_retire_children(pfp, SPOOLSS_SPAWN_RATE,
-                                               time(NULL) - SPOOLSS_MIN_LIFE);
-               }
-       }
+static void print_queue_forward(struct messaging_context *msg,
+                               void *private_data,
+                               uint32_t msg_type,
+                               struct server_id server_id,
+                               DATA_BLOB *data)
+{
+       messaging_send_buf(msg, pid_to_procid(background_lpq_updater_pid),
+                          MSG_PRINTER_UPDATE, data->data, data->length);
+}
 
-       ret = spoolssd_schedule_check(ev_ctx, pfp, current_time);
+static char *get_bq_logfile(void)
+{
+       char *lfile = lp_logfile(talloc_tos());
+       int rc;
+
+       if (lfile == NULL || lfile[0] == '\0') {
+               rc = asprintf(&lfile, "%s/log.%s.bq",
+                                       get_dyn_LOGFILEBASE(), DAEMON_NAME);
+       } else {
+               rc = asprintf(&lfile, "%s.bq", lp_logfile(talloc_tos()));
+       }
+       if (rc == -1) {
+               lfile = NULL;
+       }
+       return lfile;
 }
 
-void start_spoolssd(struct tevent_context *ev_ctx,
+pid_t start_spoolssd(struct tevent_context *ev_ctx,
                    struct messaging_context *msg_ctx)
 {
-       struct prefork_pool *pool;
        struct rpc_srv_callbacks spoolss_cb;
        struct dcerpc_binding_vector *v;
        TALLOC_CTX *mem_ctx;
@@ -550,31 +642,60 @@ void start_spoolssd(struct tevent_context *ev_ctx,
 
        DEBUG(1, ("Forking SPOOLSS Daemon\n"));
 
-       pid = sys_fork();
+       /*
+        * Block signals before forking child as it will have to
+        * set its own handlers. Child will re-enable SIGHUP as
+        * soon as the handlers are set up.
+        */
+       BlockSignals(true, SIGTERM);
+       BlockSignals(true, SIGHUP);
+
+       pid = fork();
 
        if (pid == -1) {
-               DEBUG(0, ("Failed to fork SPOOLSS [%s], aborting ...\n",
+               DEBUG(0, ("Failed to fork SPOOLSS [%s]\n",
                           strerror(errno)));
-               exit(1);
        }
 
-       if (pid) {
-               /* parent */
-               return;
-       }
+       /* parent or error */
+       if (pid != 0) {
 
-       /* child */
-       close_low_fds(false);
+               /* Re-enable SIGHUP before returnig */
+               BlockSignals(false, SIGTERM);
+               BlockSignals(false, SIGHUP);
+               return pid;
+       }
 
        status = reinit_after_fork(msg_ctx,
                                   ev_ctx,
-                                  procid_self(), true);
+                                  true);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(0,("reinit_after_fork() failed\n"));
                smb_panic("reinit_after_fork() failed");
        }
 
-       spoolss_reopen_logs();
+       prctl_set_comment("spoolssd-master");
+
+       /* save the parent process id so the children can use it later */
+       parent_id = messaging_server_id(msg_ctx);
+
+       spoolss_reopen_logs(0);
+       pfh_daemon_config(DAEMON_NAME,
+                         &pf_spoolss_cfg,
+                         &default_pf_spoolss_cfg);
+
+       spoolss_setup_sig_term_handler(ev_ctx);
+       spoolss_setup_sig_hup_handler(ev_ctx, msg_ctx);
+
+       BlockSignals(false, SIGTERM);
+       BlockSignals(false, SIGHUP);
+
+       /* always start the backgroundqueue listner in spoolssd */
+       bq_logfile = get_bq_logfile();
+       pid = start_background_queue(ev_ctx, msg_ctx, bq_logfile);
+       if (pid > 0) {
+               background_lpq_updater_pid = pid;
+       }
 
        /* the listening fd must be created before the children are actually
         * forked out. */
@@ -583,27 +704,28 @@ void start_spoolssd(struct tevent_context *ev_ctx,
                exit(1);
        }
 
-       ret = listen(listen_fd, SPOOLSS_MAX_CHILDREN);
+       ret = listen(listen_fd, pf_spoolss_cfg.max_allowed_clients);
        if (ret == -1) {
                DEBUG(0, ("Failed to listen on spoolss pipe - %s\n",
                          strerror(errno)));
                exit(1);
        }
 
-
        /* start children before any more initialization is done */
-       ok = prefork_create_pool(ev_ctx, ev_ctx, listen_fd,
-                                SPOOLSS_MIN_CHILDREN,
-                                SPOOLSS_MAX_CHILDREN,
+       ok = prefork_create_pool(ev_ctx, /* mem_ctx */
+                                ev_ctx, msg_ctx,
+                                1, &listen_fd,
+                                pf_spoolss_cfg.min_children,
+                                pf_spoolss_cfg.max_children,
                                 &spoolss_children_main, NULL,
-                                &pool);
-
-       spoolss_setup_sig_term_handler(ev_ctx);
-       spoolss_setup_sig_hup_handler(ev_ctx, msg_ctx);
+                                &spoolss_pool);
+       if (!ok) {
+               exit(1);
+       }
 
-       if (!serverid_register(procid_self(),
-                               FLAG_MSG_GENERAL|FLAG_MSG_SMBD
-                               |FLAG_MSG_PRINT_GENERAL)) {
+       if (!serverid_register(messaging_server_id(msg_ctx),
+                               FLAG_MSG_GENERAL |
+                               FLAG_MSG_PRINT_GENERAL)) {
                exit(1);
        }
 
@@ -611,10 +733,22 @@ void start_spoolssd(struct tevent_context *ev_ctx,
                exit(1);
        }
 
-       messaging_register(msg_ctx, NULL,
-                          MSG_PRINTER_UPDATE, print_queue_receive);
        messaging_register(msg_ctx, ev_ctx,
                           MSG_SMB_CONF_UPDATED, smb_conf_updated);
+       messaging_register(msg_ctx, NULL, MSG_PRINTER_UPDATE,
+                          print_queue_forward);
+       messaging_register(msg_ctx, ev_ctx, MSG_PRINTER_PCAP,
+                          pcap_updated);
+       messaging_register(msg_ctx, ev_ctx,
+                          MSG_PREFORK_CHILD_EVENT, child_ping);
+
+       /* As soon as messaging is up check if pcap has been loaded already.
+        * If so then we probably missed a message and should load_printers()
+        * ourselves. If pcap has not been loaded yet, then ignore, we will get
+        * a message as soon as the bq process completes the reload. */
+       if (pcap_cache_loaded()) {
+               load_printers(ev_ctx, msg_ctx);
+       }
 
        mem_ctx = talloc_new(NULL);
        if (mem_ctx == NULL) {
@@ -667,21 +801,21 @@ void start_spoolssd(struct tevent_context *ev_ctx,
 
        talloc_free(mem_ctx);
 
-       ok = spoolssd_setup_children_monitor(ev_ctx, pool);
+       ok = spoolssd_setup_children_monitor(ev_ctx, msg_ctx);
        if (!ok) {
                DEBUG(0, ("Failed to setup children monitoring!\n"));
                exit(1);
        }
 
-       reload_printers(ev_ctx, msg_ctx);
+       DEBUG(1, ("SPOOLSS Daemon Started (%u)\n", (unsigned int)getpid()));
 
-       DEBUG(1, ("SPOOLSS Daemon Started (%d)\n", getpid()));
+       pfh_manage_pool(ev_ctx, msg_ctx, &pf_spoolss_cfg, spoolss_pool);
 
        /* loop forever */
        ret = tevent_loop_wait(ev_ctx);
 
        /* should not be reached */
-       DEBUG(0,("background_queue: tevent_loop_wait() exited with %d - %s\n",
+       DEBUG(0,("spoolssd tevent_loop_wait() exited with %d - %s\n",
                 ret, (ret == 0) ? "out of events" : strerror(errno)));
        exit(1);
 }