s3-spoolss: Introduce helper function to manage pool.
[idra/samba.git] / source3 / printing / spoolssd.c
index 1297b581293382a3177ad1ab37e407d6f851a753..2cbd6e3603c77b9d32ccbf6e1b45a4e7bf140b7e 100644 (file)
@@ -25,6 +25,7 @@
 #include "printing/nt_printing_migrate_internal.h"
 #include "printing/queue_process.h"
 #include "printing/pcap.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 "ntdomain.h"
 #include "librpc/gen_ndr/srv_winreg.h"
 #include "librpc/gen_ndr/srv_spoolss.h"
 #define SPOOLSS_SPAWN_RATE 5
 #define SPOOLSS_MIN_LIFE 60 /* 1 minute minimum life time */
 
 #define SPOOLSS_SPAWN_RATE 5
 #define SPOOLSS_MIN_LIFE 60 /* 1 minute minimum life time */
 
-#define SPOOLSS_ALL_FINE 0x00
+#define SPOOLSS_INIT     0x00
 #define SPOOLSS_NEW_MAX  0x01
 #define SPOLLSS_ENOSPC   0x02
 
 #define SPOOLSS_NEW_MAX  0x01
 #define SPOLLSS_ENOSPC   0x02
 
+static struct prefork_pool *spoolss_pool;
 static int spoolss_min_children;
 static int spoolss_max_children;
 static int spoolss_spawn_rate;
 static int spoolss_prefork_status;
 static int spoolss_min_children;
 static int spoolss_max_children;
 static int spoolss_spawn_rate;
 static int spoolss_prefork_status;
+static int spoolss_child_id = 0;
+
+pid_t start_spoolssd(struct tevent_context *ev_ctx,
+                    struct messaging_context *msg_ctx);
+static void spoolss_manage_pool(struct tevent_context *ev_ctx,
+                               struct messaging_context *msg_ctx);
 
 static void spoolss_prefork_config(void)
 {
 
 static void spoolss_prefork_config(void)
 {
@@ -60,7 +68,8 @@ static void spoolss_prefork_config(void)
        int ret;
 
        if (!spoolss_prefork_config_init) {
        int ret;
 
        if (!spoolss_prefork_config_init) {
-               spoolss_prefork_status = SPOOLSS_ALL_FINE;
+               spoolss_pool = NULL;
+               spoolss_prefork_status = SPOOLSS_INIT;
                spoolss_min_children = 0;
                spoolss_max_children = 0;
                spoolss_spawn_rate = 0;
                spoolss_min_children = 0;
                spoolss_max_children = 0;
                spoolss_spawn_rate = 0;
@@ -68,13 +77,13 @@ static void spoolss_prefork_config(void)
        }
 
        prefork_str = lp_parm_const_string(GLOBAL_SECTION_SNUM,
        }
 
        prefork_str = lp_parm_const_string(GLOBAL_SECTION_SNUM,
-                                          "spoolssd", "prefork", "none");
+                                          "spoolss", "prefork", "none");
        if (strcmp(prefork_str, "none") == 0) {
                use_defaults = true;
        } else {
                ret = sscanf(prefork_str, "%d:%d:%d", &min, &max, &rate);
                if (ret != 3) {
        if (strcmp(prefork_str, "none") == 0) {
                use_defaults = true;
        } else {
                ret = sscanf(prefork_str, "%d:%d:%d", &min, &max, &rate);
                if (ret != 3) {
-                       DEBUG(0, ("invalid format for spoolssd:prefork!\n"));
+                       DEBUG(0, ("invalid format for spoolss:prefork!\n"));
                        use_defaults = true;
                }
        }
                        use_defaults = true;
                }
        }
@@ -94,27 +103,45 @@ static void spoolss_prefork_config(void)
        spoolss_spawn_rate = rate;
 }
 
        spoolss_spawn_rate = rate;
 }
 
-static void spoolss_reopen_logs(void)
+static void spoolss_reopen_logs(int child_id)
 {
        char *lfile = lp_logfile();
 {
        char *lfile = lp_logfile();
+       char *ext;
        int rc;
 
        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') {
        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 {
        } 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(), child_id);
+                       } else {
+                               rc = asprintf(&lfile, "%s.%s",
+                                             lp_logfile(), ext);
                        }
                }
        }
 
                        }
                }
        }
 
+       if (rc > 0) {
+               lp_set_logfile(lfile);
+               SAFE_FREE(lfile);
+       }
+
+       SAFE_FREE(ext);
+
        reopen_logs();
 }
 
        reopen_logs();
 }
 
@@ -125,8 +152,11 @@ static void update_conf(struct tevent_context *ev,
        lp_load(get_dyn_CONFIGFILE(), true, false, false, true);
        reload_printers(ev, msg);
 
        lp_load(get_dyn_CONFIGFILE(), true, false, false, true);
        reload_printers(ev, msg);
 
-       spoolss_reopen_logs();
-       spoolss_prefork_config();
+       spoolss_reopen_logs(spoolss_child_id);
+       if (spoolss_child_id == 0) {
+               spoolss_prefork_config();
+               spoolss_manage_pool(ev, msg);
+       }
 }
 
 static void smb_conf_updated(struct messaging_context *msg,
 }
 
 static void smb_conf_updated(struct messaging_context *msg,
@@ -142,6 +172,27 @@ static void smb_conf_updated(struct messaging_context *msg,
        update_conf(ev_ctx, msg);
 }
 
        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_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,
                                     struct tevent_signal *se,
                                     int signum,
 static void spoolss_sig_term_handler(struct tevent_context *ev,
                                     struct tevent_signal *se,
                                     int signum,
@@ -166,11 +217,6 @@ static void spoolss_setup_sig_term_handler(struct tevent_context *ev_ctx)
        }
 }
 
        }
 }
 
-struct spoolss_hup_ctx {
-       struct messaging_context *msg_ctx;
-       struct prefork_pool *pfp;
-};
-
 static void spoolss_sig_hup_handler(struct tevent_context *ev,
                                    struct tevent_signal *se,
                                    int signum,
 static void spoolss_sig_hup_handler(struct tevent_context *ev,
                                    struct tevent_signal *se,
                                    int signum,
@@ -178,36 +224,29 @@ static void spoolss_sig_hup_handler(struct tevent_context *ev,
                                    void *siginfo,
                                    void *pvt)
 {
                                    void *siginfo,
                                    void *pvt)
 {
-       struct spoolss_hup_ctx *hup_ctx;
+       struct messaging_context *msg_ctx;
 
 
-       hup_ctx = talloc_get_type_abort(pvt, struct spoolss_hup_ctx);
+       msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);
 
        DEBUG(1,("Reloading printers after SIGHUP\n"));
 
        DEBUG(1,("Reloading printers after SIGHUP\n"));
-       update_conf(ev, hup_ctx->msg_ctx);
+       update_conf(ev, msg_ctx);
 
        /* relay to all children */
 
        /* relay to all children */
-       prefork_send_signal_to_all(hup_ctx->pfp, SIGHUP);
+       if (spoolss_pool) {
+               prefork_send_signal_to_all(spoolss_pool, SIGHUP);
+       }
 }
 
 static void spoolss_setup_sig_hup_handler(struct tevent_context *ev_ctx,
 }
 
 static void spoolss_setup_sig_hup_handler(struct tevent_context *ev_ctx,
-                                         struct prefork_pool *pfp,
                                          struct messaging_context *msg_ctx)
 {
                                          struct messaging_context *msg_ctx)
 {
-       struct spoolss_hup_ctx *hup_ctx;
        struct tevent_signal *se;
 
        struct tevent_signal *se;
 
-       hup_ctx = talloc(ev_ctx, struct spoolss_hup_ctx);
-       if (!hup_ctx) {
-               exit_server("failed to setup SIGHUP handler");
-       }
-       hup_ctx->pfp = pfp;
-       hup_ctx->msg_ctx = msg_ctx;
-
        se = tevent_add_signal(ev_ctx,
                               ev_ctx,
                               SIGHUP, 0,
                               spoolss_sig_hup_handler,
        se = tevent_add_signal(ev_ctx,
                               ev_ctx,
                               SIGHUP, 0,
                               spoolss_sig_hup_handler,
-                              hup_ctx);
+                              msg_ctx);
        if (!se) {
                exit_server("failed to setup SIGHUP handler");
        }
        if (!se) {
                exit_server("failed to setup SIGHUP handler");
        }
@@ -255,12 +294,12 @@ static void spoolss_chld_sig_hup_handler(struct tevent_context *ev,
        change_to_root_user();
        DEBUG(1,("Reloading printers after SIGHUP\n"));
        reload_printers(ev, shc->msg_ctx);
        change_to_root_user();
        DEBUG(1,("Reloading printers after SIGHUP\n"));
        reload_printers(ev, shc->msg_ctx);
-       spoolss_reopen_logs();
+       spoolss_reopen_logs(spoolss_child_id);
 }
 
 static bool spoolss_setup_chld_hup_handler(struct tevent_context *ev_ctx,
 }
 
 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;
 {
        struct spoolss_chld_sig_hup_ctx *shc;
        struct tevent_signal *se;
@@ -287,7 +326,7 @@ static bool spoolss_setup_chld_hup_handler(struct tevent_context *ev_ctx,
 }
 
 static bool spoolss_child_init(struct tevent_context *ev_ctx,
 }
 
 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;
 {
        NTSTATUS status;
        struct rpc_srv_callbacks spoolss_cb;
@@ -301,14 +340,17 @@ static bool spoolss_child_init(struct tevent_context *ev_ctx,
                smb_panic("reinit_after_fork() failed");
        }
 
                smb_panic("reinit_after_fork() failed");
        }
 
-       spoolss_reopen_logs();
+       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 (!ok) {
                return false;
        }
 
-       if (!serverid_register(procid_self(), FLAG_MSG_GENERAL)) {
+       if (!serverid_register(procid_self(),
+                               FLAG_MSG_GENERAL |
+                               FLAG_MSG_PRINT_GENERAL)) {
                return false;
        }
 
                return false;
        }
 
@@ -318,6 +360,16 @@ 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_SMB_CONF_UPDATED, smb_conf_updated);
+       messaging_register(msg_ctx, ev_ctx, MSG_PRINTER_PCAP,
+                          pcap_updated);
+
+       /* 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;
 
        /* try to reinit rpc queues */
        spoolss_cb.init = spoolss_init_cb;
@@ -338,8 +390,6 @@ static bool spoolss_child_init(struct tevent_context *ev_ctx,
                return false;
        }
 
                return false;
        }
 
-       reload_printers(ev_ctx, msg_ctx);
-
        return true;
 }
 
        return true;
 }
 
@@ -359,6 +409,7 @@ 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,
 static int spoolss_children_main(struct tevent_context *ev_ctx,
                                 struct messaging_context *msg_ctx,
                                 struct pf_worker_data *pf,
+                                int child_id,
                                 int listen_fd_size,
                                 int *listen_fds,
                                 int lock_fd,
                                 int listen_fd_size,
                                 int *listen_fds,
                                 int lock_fd,
@@ -368,7 +419,7 @@ static int spoolss_children_main(struct tevent_context *ev_ctx,
        bool ok;
        int ret;
 
        bool ok;
        int ret;
 
-       ok = spoolss_child_init(ev_ctx, pf);
+       ok = spoolss_child_init(ev_ctx, child_id, pf);
        if (!ok) {
                return 1;
        }
        if (!ok) {
                return 1;
        }
@@ -516,6 +567,7 @@ static void spoolss_handle_client(struct tevent_req *req)
 /* ==== Main Process Functions ==== */
 
 extern pid_t background_lpq_updater_pid;
 /* ==== Main Process Functions ==== */
 
 extern pid_t background_lpq_updater_pid;
+static char *bq_logfile;
 
 static void check_updater_child(void)
 {
 
 static void check_updater_child(void)
 {
@@ -530,14 +582,66 @@ static void check_updater_child(void)
        if (pid > 0) {
                DEBUG(2, ("The background queue child died... Restarting!\n"));
                pid = start_background_queue(server_event_context(),
        if (pid > 0) {
                DEBUG(2, ("The background queue child died... Restarting!\n"));
                pid = start_background_queue(server_event_context(),
-                                            server_messaging_context());
+                                            server_messaging_context(),
+                                            bq_logfile);
                background_lpq_updater_pid = pid;
        }
 }
 
                background_lpq_updater_pid = pid;
        }
 }
 
+static void spoolss_manage_pool(struct tevent_context *ev_ctx,
+                               struct messaging_context *msg_ctx)
+{
+       time_t now = time(NULL);
+       int active, total;
+       int ret, n;
+
+       if ((spoolss_prefork_status & SPOOLSS_NEW_MAX) &&
+           !(spoolss_prefork_status & SPOLLSS_ENOSPC)) {
+               ret = prefork_expand_pool(spoolss_pool, spoolss_max_children);
+               if (ret == ENOSPC) {
+                       spoolss_prefork_status |= SPOLLSS_ENOSPC;
+               }
+               spoolss_prefork_status &= ~SPOOLSS_NEW_MAX;
+       }
+
+       active = prefork_count_active_children(spoolss_pool, &total);
+
+       if ((total < spoolss_max_children) &&
+           ((total < spoolss_min_children) ||
+            (total - active < spoolss_spawn_rate))) {
+               n = prefork_add_children(ev_ctx, msg_ctx,
+                                        spoolss_pool, spoolss_spawn_rate);
+               if (n < spoolss_spawn_rate) {
+                       DEBUG(10, ("Tried to start %d children but only,"
+                                  "%d were actually started.!\n",
+                                  spoolss_spawn_rate, n));
+               }
+       }
+
+       if (total - active > spoolss_min_children) {
+               if ((total - spoolss_min_children) >= spoolss_spawn_rate) {
+                       prefork_retire_children(spoolss_pool,
+                                               spoolss_spawn_rate,
+                                               now - SPOOLSS_MIN_LIFE);
+               }
+       }
+
+       n = prefork_count_allowed_connections(spoolss_pool);
+       if (n <= spoolss_spawn_rate) {
+               do {
+                       prefork_increase_allowed_clients(spoolss_pool);
+                       n = prefork_count_allowed_connections(spoolss_pool);
+               } while (n <= spoolss_spawn_rate);
+       } else if (n > spoolss_max_children + spoolss_spawn_rate) {
+               do {
+                       prefork_decrease_allowed_clients(spoolss_pool);
+                       n = prefork_count_allowed_connections(spoolss_pool);
+               } while (n > spoolss_max_children + spoolss_spawn_rate);
+       }
+}
+
 static bool spoolssd_schedule_check(struct tevent_context *ev_ctx,
                                    struct messaging_context *msg_ctx,
 static bool spoolssd_schedule_check(struct tevent_context *ev_ctx,
                                    struct messaging_context *msg_ctx,
-                                   struct prefork_pool *pfp,
                                    struct timeval current_time);
 static void spoolssd_check_children(struct tevent_context *ev_ctx,
                                    struct tevent_timer *te,
                                    struct timeval current_time);
 static void spoolssd_check_children(struct tevent_context *ev_ctx,
                                    struct tevent_timer *te,
@@ -549,82 +653,49 @@ static void spoolssd_sigchld_handler(struct tevent_context *ev_ctx,
                                     void *pvt)
 {
        struct messaging_context *msg_ctx;
                                     void *pvt)
 {
        struct messaging_context *msg_ctx;
-       int active, total;
-       int n, r;
 
        msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);
 
 
        msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);
 
-       /* now check we do not descend below the minimum */
-       active = prefork_count_active_children(pfp, &total);
-
-       n = 0;
-       if (total < spoolss_min_children) {
-               n = total - spoolss_min_children;
-       } else if (total - active < (total / 4)) {
-               n = spoolss_min_children;
-       }
-
-       if (n > 0) {
-               r = prefork_add_children(ev_ctx, msg_ctx, pfp, n);
-               if (r < n) {
-                       DEBUG(10, ("Tried to start %d children but only,"
-                                  "%d were actually started.!\n", n, r));
-               }
-       }
+       /* run pool management so we can fork/retire or increase
+        * the allowed connections per child based on load */
+       spoolss_manage_pool(ev_ctx, msg_ctx);
 
        /* also check if the updater child is alive and well */
        check_updater_child();
 }
 
 static bool spoolssd_setup_children_monitor(struct tevent_context *ev_ctx,
 
        /* also check if the updater child is alive and well */
        check_updater_child();
 }
 
 static bool spoolssd_setup_children_monitor(struct tevent_context *ev_ctx,
-                                           struct messaging_context *msg_ctx,
-                                           struct prefork_pool *pfp)
+                                           struct messaging_context *msg_ctx)
 {
        bool ok;
 
        /* add our oun sigchld callback */
 {
        bool ok;
 
        /* add our oun sigchld callback */
-       prefork_set_sigchld_callback(pfp, spoolssd_sigchld_handler, msg_ctx);
+       prefork_set_sigchld_callback(spoolss_pool,
+                                    spoolssd_sigchld_handler, msg_ctx);
 
 
-       ok = spoolssd_schedule_check(ev_ctx, msg_ctx, pfp,
+       ok = spoolssd_schedule_check(ev_ctx, msg_ctx,
                                     tevent_timeval_current());
        return ok;
 }
 
                                     tevent_timeval_current());
        return ok;
 }
 
-struct schedule_check_state {
-       struct messaging_context *msg_ctx;
-       struct prefork_pool *pfp;
-};
-
 static bool spoolssd_schedule_check(struct tevent_context *ev_ctx,
                                    struct messaging_context *msg_ctx,
 static bool spoolssd_schedule_check(struct tevent_context *ev_ctx,
                                    struct messaging_context *msg_ctx,
-                                   struct prefork_pool *pfp,
                                    struct timeval current_time)
 {
        struct tevent_timer *te;
        struct timeval next_event;
                                    struct timeval current_time)
 {
        struct tevent_timer *te;
        struct timeval next_event;
-       struct schedule_check_state *state;
 
        /* check situation again in 10 seconds */
        next_event = tevent_timeval_current_ofs(10, 0);
 
 
        /* check situation again in 10 seconds */
        next_event = tevent_timeval_current_ofs(10, 0);
 
-       state = talloc(ev_ctx, struct schedule_check_state);
-       if (!state) {
-               DEBUG(0, ("Out of memory!\n"));
-               return false;
-       }
-       state->msg_ctx = msg_ctx;
-       state->pfp = pfp;
-
        /* TODO: check when the socket becomes readable, so that children
         * are checked only when there is some activity ? */
        /* TODO: 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, state);
+       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"));
        if (!te) {
                DEBUG(2, ("Failed to set up children monitoring!\n"));
-               talloc_free(state);
                return false;
        }
                return false;
        }
-       talloc_steal(te, state);
 
        return true;
 }
 
        return true;
 }
@@ -634,41 +705,13 @@ static void spoolssd_check_children(struct tevent_context *ev_ctx,
                                    struct timeval current_time,
                                    void *pvt)
 {
                                    struct timeval current_time,
                                    void *pvt)
 {
-       struct schedule_check_state *state;
-       int active, total;
-       int ret, n;
-
-       state = talloc_get_type_abort(pvt, struct schedule_check_state);
-
-       if ((spoolss_prefork_status & SPOOLSS_NEW_MAX) &&
-           !(spoolss_prefork_status & SPOLLSS_ENOSPC)) {
-               ret = prefork_expand_pool(state->pfp, spoolss_max_children);
-               if (ret == ENOSPC) {
-                       spoolss_prefork_status |= SPOLLSS_ENOSPC;
-               }
-               spoolss_prefork_status &= ~SPOOLSS_NEW_MAX;
-       }
-
-       active = prefork_count_active_children(state->pfp, &total);
+       struct messaging_context *msg_ctx;
 
 
-       if (total - active < spoolss_spawn_rate) {
-               n = prefork_add_children(ev_ctx, state->msg_ctx,
-                                        state->pfp, spoolss_spawn_rate);
-               if (n < spoolss_spawn_rate) {
-                       DEBUG(10, ("Tried to start 5 children but only,"
-                                  "%d were actually started.!\n", n));
-               }
-       }
+       msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);
 
 
-       if (total - active > spoolss_min_children) {
-               if ((total - spoolss_min_children) >= spoolss_spawn_rate) {
-                       prefork_retire_children(state->pfp, spoolss_spawn_rate,
-                                               time(NULL) - SPOOLSS_MIN_LIFE);
-               }
-       }
+       spoolss_manage_pool(ev_ctx, msg_ctx);
 
 
-       ret = spoolssd_schedule_check(ev_ctx, state->msg_ctx,
-                                       state->pfp, current_time);
+       spoolssd_schedule_check(ev_ctx, msg_ctx, current_time);
 }
 
 static void print_queue_forward(struct messaging_context *msg,
 }
 
 static void print_queue_forward(struct messaging_context *msg,
@@ -681,10 +724,26 @@ static void print_queue_forward(struct messaging_context *msg,
                           MSG_PRINTER_UPDATE, data->data, data->length);
 }
 
                           MSG_PRINTER_UPDATE, data->data, data->length);
 }
 
+static char *get_bq_logfile(void)
+{
+       char *lfile = lp_logfile();
+       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());
+       }
+       if (rc == -1) {
+               lfile = NULL;
+       }
+       return lfile;
+}
+
 pid_t 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)
 {
-       struct prefork_pool *pool;
        struct rpc_srv_callbacks spoolss_cb;
        struct dcerpc_binding_vector *v;
        TALLOC_CTX *mem_ctx;
        struct rpc_srv_callbacks spoolss_cb;
        struct dcerpc_binding_vector *v;
        TALLOC_CTX *mem_ctx;
@@ -696,14 +755,27 @@ pid_t start_spoolssd(struct tevent_context *ev_ctx,
 
        DEBUG(1, ("Forking SPOOLSS Daemon\n"));
 
 
        DEBUG(1, ("Forking SPOOLSS Daemon\n"));
 
+       /*
+        * 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 = sys_fork();
 
        if (pid == -1) {
                DEBUG(0, ("Failed to fork SPOOLSS [%s]\n",
                           strerror(errno)));
        }
        pid = sys_fork();
 
        if (pid == -1) {
                DEBUG(0, ("Failed to fork SPOOLSS [%s]\n",
                           strerror(errno)));
        }
+
+       /* parent or error */
        if (pid != 0) {
        if (pid != 0) {
-               /* parent or error */
+
+               /* Re-enable SIGHUP before returnig */
+               BlockSignals(false, SIGTERM);
+               BlockSignals(false, SIGHUP);
                return pid;
        }
 
                return pid;
        }
 
@@ -718,14 +790,18 @@ pid_t start_spoolssd(struct tevent_context *ev_ctx,
                smb_panic("reinit_after_fork() failed");
        }
 
                smb_panic("reinit_after_fork() failed");
        }
 
-       spoolss_reopen_logs();
+       spoolss_reopen_logs(0);
        spoolss_prefork_config();
 
        spoolss_prefork_config();
 
-       /* Publish nt printers, this requires a working winreg pipe */
-       pcap_cache_reload(ev_ctx, msg_ctx, &reload_printers);
+       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 */
 
        /* always start the backgroundqueue listner in spoolssd */
-       pid = start_background_queue(ev_ctx, msg_ctx);
+       bq_logfile = get_bq_logfile();
+       pid = start_background_queue(ev_ctx, msg_ctx, bq_logfile);
        if (pid > 0) {
                background_lpq_updater_pid = pid;
        }
        if (pid > 0) {
                background_lpq_updater_pid = pid;
        }
@@ -744,7 +820,6 @@ pid_t start_spoolssd(struct tevent_context *ev_ctx,
                exit(1);
        }
 
                exit(1);
        }
 
-
        /* start children before any more initialization is done */
        ok = prefork_create_pool(ev_ctx, /* mem_ctx */
                                 ev_ctx, msg_ctx,
        /* start children before any more initialization is done */
        ok = prefork_create_pool(ev_ctx, /* mem_ctx */
                                 ev_ctx, msg_ctx,
@@ -752,14 +827,14 @@ pid_t start_spoolssd(struct tevent_context *ev_ctx,
                                 spoolss_min_children,
                                 spoolss_max_children,
                                 &spoolss_children_main, NULL,
                                 spoolss_min_children,
                                 spoolss_max_children,
                                 &spoolss_children_main, NULL,
-                                &pool);
-
-       spoolss_setup_sig_term_handler(ev_ctx);
-       spoolss_setup_sig_hup_handler(ev_ctx, pool, msg_ctx);
+                                &spoolss_pool);
+       if (!ok) {
+               exit(1);
+       }
 
        if (!serverid_register(procid_self(),
 
        if (!serverid_register(procid_self(),
-                               FLAG_MSG_GENERAL|FLAG_MSG_SMBD
-                               |FLAG_MSG_PRINT_GENERAL)) {
+                               FLAG_MSG_GENERAL |
+                               FLAG_MSG_PRINT_GENERAL)) {
                exit(1);
        }
 
                exit(1);
        }
 
@@ -771,6 +846,16 @@ pid_t start_spoolssd(struct tevent_context *ev_ctx,
                           MSG_SMB_CONF_UPDATED, smb_conf_updated);
        messaging_register(msg_ctx, NULL, MSG_PRINTER_UPDATE,
                           print_queue_forward);
                           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);
+
+       /* 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) {
 
        mem_ctx = talloc_new(NULL);
        if (mem_ctx == NULL) {
@@ -823,7 +908,7 @@ pid_t start_spoolssd(struct tevent_context *ev_ctx,
 
        talloc_free(mem_ctx);
 
 
        talloc_free(mem_ctx);
 
-       ok = spoolssd_setup_children_monitor(ev_ctx, msg_ctx, pool);
+       ok = spoolssd_setup_children_monitor(ev_ctx, msg_ctx);
        if (!ok) {
                DEBUG(0, ("Failed to setup children monitoring!\n"));
                exit(1);
        if (!ok) {
                DEBUG(0, ("Failed to setup children monitoring!\n"));
                exit(1);
@@ -831,11 +916,13 @@ pid_t start_spoolssd(struct tevent_context *ev_ctx,
 
        DEBUG(1, ("SPOOLSS Daemon Started (%d)\n", getpid()));
 
 
        DEBUG(1, ("SPOOLSS Daemon Started (%d)\n", getpid()));
 
+       spoolss_manage_pool(ev_ctx, msg_ctx);
+
        /* loop forever */
        ret = tevent_loop_wait(ev_ctx);
 
        /* should not be reached */
        /* 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);
 }
                 ret, (ret == 0) ? "out of events" : strerror(errno)));
        exit(1);
 }