s3-spoolssd: Listen on parent messages
[ambi/samba-autobuild/.git] / source3 / printing / spoolssd.c
index 09ffc05caf1edcbf8c1d4858e362e3f0bb468b94..9a806f09a8bd1460e12c1362a5d814aa16c66e48 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 "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 */
-
-#define SPOOLSS_INIT     0x00
-#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 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 };
+
 pid_t start_spoolssd(struct tevent_context *ev_ctx,
                     struct messaging_context *msg_ctx);
 
-static void spoolss_prefork_config(void)
-{
-       static int spoolss_prefork_config_init = false;
-       const char *prefork_str;
-       int min, max, rate;
-       bool use_defaults = false;
-       int ret;
-
-       if (!spoolss_prefork_config_init) {
-               spoolss_pool = NULL;
-               spoolss_prefork_status = SPOOLSS_INIT;
-               spoolss_min_children = 0;
-               spoolss_max_children = 0;
-               spoolss_spawn_rate = 0;
-               spoolss_prefork_config_init = true;
-       }
-
-       prefork_str = lp_parm_const_string(GLOBAL_SECTION_SNUM,
-                                          "spoolssd", "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) {
-                       DEBUG(0, ("invalid format for spoolssd:prefork!\n"));
-                       use_defaults = true;
-               }
-       }
-
-       if (use_defaults) {
-               min = SPOOLSS_MIN_CHILDREN;
-               max = SPOOLSS_MAX_CHILDREN;
-               rate = SPOOLSS_SPAWN_RATE;
-       }
-
-       if (max > spoolss_max_children && spoolss_max_children != 0) {
-               spoolss_prefork_status |= SPOOLSS_NEW_MAX;
-       }
-
-       spoolss_min_children = min;
-       spoolss_max_children = max;
-       spoolss_spawn_rate = rate;
-}
-
 static void spoolss_reopen_logs(int child_id)
 {
        char *lfile = lp_logfile();
@@ -108,29 +63,27 @@ static void spoolss_reopen_logs(int child_id)
        int rc;
 
        if (child_id) {
-               rc = asprintf(&ext, ".%s.%d", DAEMON_NAME, child_id);
+               rc = asprintf(&ext, "%s.%d", DAEMON_NAME, child_id);
        } else {
-               rc = asprintf(&ext, ".%s", DAEMON_NAME);
+               rc = asprintf(&ext, "%s", DAEMON_NAME);
        }
 
        if (rc == -1) {
-               /* if we can't allocate, set it to NULL
-                * and logging will flow in the original file */
-               ext = NULL;
+               return;
        }
 
        rc = 0;
        if (lfile == NULL || lfile[0] == '\0') {
-               rc = asprintf(&lfile, "%s/log%s",
-                             get_dyn_LOGFILEBASE(), ext?ext:"");
+               rc = asprintf(&lfile, "%s/log.%s",
+                             get_dyn_LOGFILEBASE(), ext);
        } else {
-               if (ext && strstr(lfile, ext) == NULL) {
-                       if (strstr(lfile, DAEMON_NAME) == NULL) {
-                               rc = asprintf(&lfile, "%s%s",
-                                             lp_logfile(), ext?ext:"");
-                       } else {
+               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);
                        }
                }
        }
@@ -154,7 +107,10 @@ static void update_conf(struct tevent_context *ev,
 
        spoolss_reopen_logs(spoolss_child_id);
        if (spoolss_child_id == 0) {
-               spoolss_prefork_config();
+               pfh_daemon_config(DAEMON_NAME,
+                                 &pf_spoolss_cfg,
+                                 &default_pf_spoolss_cfg);
+               pfh_manage_pool(ev, msg, &pf_spoolss_cfg, spoolss_pool);
        }
 }
 
@@ -324,6 +280,22 @@ 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,
                               int child_id, struct pf_worker_data *pf)
 {
@@ -361,6 +333,8 @@ static bool spoolss_child_init(struct tevent_context *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()
@@ -398,9 +372,6 @@ struct spoolss_children_data {
        struct pf_worker_data *pf;
        int listen_fd_size;
        int *listen_fds;
-       int lock_fd;
-
-       bool listening;
 };
 
 static void spoolss_next_client(void *pvt);
@@ -411,7 +382,6 @@ static int spoolss_children_main(struct tevent_context *ev_ctx,
                                 int child_id,
                                 int listen_fd_size,
                                 int *listen_fds,
-                                int lock_fd,
                                 void *private_data)
 {
        struct spoolss_children_data *data;
@@ -430,10 +400,8 @@ 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_size = listen_fd_size;
        data->listen_fds = listen_fds;
-       data->listening = false;
 
        /* loop until it is time to exit */
        while (pf->status != PF_WORKER_EXITING) {
@@ -457,13 +425,7 @@ 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_next_client(pvt);
 }
@@ -484,20 +446,9 @@ static void spoolss_next_client(void *pvt)
 
        data = talloc_get_type_abort(pvt, struct spoolss_children_data);
 
-       if (data->pf->num_clients == 0) {
-               data->pf->status = PF_WORKER_IDLE;
-       }
-
-       if (data->pf->cmds == PF_SRV_MSG_EXIT) {
-               DEBUG(2, ("Parent process commands we terminate!\n"));
-               return;
-       }
-
-       if (data->listening ||
-           data->pf->num_clients >= data->pf->allowed_clients) {
+       if (!pfh_child_allowed_to_accept(data->pf)) {
                /* nothing to do for now we are already listening
-                * or reached the number of clients we are allowed
-                * to handle in parallel */
+                * or we are not allowed to listen further */
                return;
        }
 
@@ -510,22 +461,20 @@ static void spoolss_next_client(void *pvt)
 
        req = prefork_listen_send(next, data->ev_ctx, data->pf,
                                  data->listen_fd_size,
-                                 data->listen_fds,
-                                 data->lock_fd);
+                                 data->listen_fds);
        if (!req) {
                DEBUG(1, ("Failed to make listening request!?\n"));
                talloc_free(next);
                return;
        }
        tevent_req_set_callback(req, spoolss_handle_client, next);
-
-       data->listening = true;
 }
 
 static void spoolss_handle_client(struct tevent_req *req)
 {
        struct spoolss_children_data *data;
        struct spoolss_new_client *client;
+       const DATA_BLOB ping = data_blob_null;
        int ret;
        int sd;
 
@@ -537,24 +486,16 @@ static void spoolss_handle_client(struct tevent_req *req)
 
        /* this will free the request too */
        talloc_free(client);
-       /* we are done listening */
-       data->listening = false;
-
-       if (ret > 0) {
-               DEBUG(1, ("Failed to accept client connection!\n"));
-               /* bail out if we are not serving any other client */
-               if (data->pf->num_clients == 0) {
-                       data->pf->status = PF_WORKER_EXITING;
-               }
-               return;
-       }
 
-       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;
        }
 
+       /* 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)));
 
@@ -587,6 +528,20 @@ static void check_updater_child(void)
        }
 }
 
+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_context *ev_ctx;
+
+       ev_ctx = talloc_get_type_abort(private_data, struct tevent_context);
+
+       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 messaging_context *msg_ctx,
                                    struct timeval current_time);
@@ -600,28 +555,12 @@ static void spoolssd_sigchld_handler(struct tevent_context *ev_ctx,
                                     void *pvt)
 {
        struct messaging_context *msg_ctx;
-       int active, total;
-       int n, r;
 
        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 */
+       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();
@@ -669,41 +608,12 @@ static void spoolssd_check_children(struct tevent_context *ev_ctx,
                                    void *pvt)
 {
        struct messaging_context *msg_ctx;
-       time_t now = time(NULL);
-       int active, total;
-       int ret, n;
 
        msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);
 
-       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);
+       pfh_manage_pool(ev_ctx, msg_ctx, &pf_spoolss_cfg, spoolss_pool);
 
-       if (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 5 children but only,"
-                                  "%d were actually started.!\n", 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);
-               }
-       }
-
-       ret = spoolssd_schedule_check(ev_ctx, msg_ctx, current_time);
+       spoolssd_schedule_check(ev_ctx, msg_ctx, current_time);
 }
 
 static void print_queue_forward(struct messaging_context *msg,
@@ -782,8 +692,13 @@ pid_t start_spoolssd(struct tevent_context *ev_ctx,
                smb_panic("reinit_after_fork() failed");
        }
 
+       /* save the parent process id so the children can use it later */
+       parent_id = procid_self();
+
        spoolss_reopen_logs(0);
-       spoolss_prefork_config();
+       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);
@@ -805,7 +720,7 @@ pid_t 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)));
@@ -816,10 +731,13 @@ pid_t start_spoolssd(struct tevent_context *ev_ctx,
        ok = prefork_create_pool(ev_ctx, /* mem_ctx */
                                 ev_ctx, msg_ctx,
                                 1, &listen_fd,
-                                spoolss_min_children,
-                                spoolss_max_children,
+                                pf_spoolss_cfg.min_children,
+                                pf_spoolss_cfg.max_children,
                                 &spoolss_children_main, NULL,
                                 &spoolss_pool);
+       if (!ok) {
+               exit(1);
+       }
 
        if (!serverid_register(procid_self(),
                                FLAG_MSG_GENERAL |
@@ -837,6 +755,8 @@ pid_t start_spoolssd(struct tevent_context *ev_ctx,
                           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()
@@ -905,11 +825,13 @@ pid_t start_spoolssd(struct tevent_context *ev_ctx,
 
        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);
 }