2 Unix SMB/Netbios implementation.
4 Copyright (C) Simo Sorce 2010
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.
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.
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/>.
21 #include "smbd/smbd.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 "printing/load.h"
30 #include "librpc/gen_ndr/srv_winreg.h"
31 #include "librpc/gen_ndr/srv_spoolss.h"
32 #include "rpc_server/rpc_server.h"
33 #include "rpc_server/rpc_ep_register.h"
34 #include "rpc_server/spoolss/srv_spoolss_nt.h"
35 #include "librpc/rpc/dcerpc_ep.h"
36 #include "lib/server_prefork.h"
38 #define SPOOLSS_PIPE_NAME "spoolss"
39 #define DAEMON_NAME "spoolssd"
41 #define SPOOLSS_MIN_CHILDREN 5
42 #define SPOOLSS_MAX_CHILDREN 25
43 #define SPOOLSS_SPAWN_RATE 5
44 #define SPOOLSS_MIN_LIFE 60 /* 1 minute minimum life time */
46 #define SPOOLSS_INIT 0x00
47 #define SPOOLSS_NEW_MAX 0x01
48 #define SPOLLSS_ENOSPC 0x02
50 static struct prefork_pool *spoolss_pool;
51 static int spoolss_min_children;
52 static int spoolss_max_children;
53 static int spoolss_spawn_rate;
54 static int spoolss_prefork_status;
55 static int spoolss_child_id = 0;
57 pid_t start_spoolssd(struct tevent_context *ev_ctx,
58 struct messaging_context *msg_ctx);
60 static void spoolss_prefork_config(void)
62 static int spoolss_prefork_config_init = false;
63 const char *prefork_str;
65 bool use_defaults = false;
68 if (!spoolss_prefork_config_init) {
70 spoolss_prefork_status = SPOOLSS_INIT;
71 spoolss_min_children = 0;
72 spoolss_max_children = 0;
73 spoolss_spawn_rate = 0;
74 spoolss_prefork_config_init = true;
77 prefork_str = lp_parm_const_string(GLOBAL_SECTION_SNUM,
78 "spoolssd", "prefork", "none");
79 if (strcmp(prefork_str, "none") == 0) {
82 ret = sscanf(prefork_str, "%d:%d:%d", &min, &max, &rate);
84 DEBUG(0, ("invalid format for spoolssd:prefork!\n"));
90 min = SPOOLSS_MIN_CHILDREN;
91 max = SPOOLSS_MAX_CHILDREN;
92 rate = SPOOLSS_SPAWN_RATE;
95 if (max > spoolss_max_children && spoolss_max_children != 0) {
96 spoolss_prefork_status |= SPOOLSS_NEW_MAX;
99 spoolss_min_children = min;
100 spoolss_max_children = max;
101 spoolss_spawn_rate = rate;
104 static void spoolss_reopen_logs(int child_id)
106 char *lfile = lp_logfile();
111 rc = asprintf(&ext, "%s.%d", DAEMON_NAME, child_id);
113 rc = asprintf(&ext, "%s", DAEMON_NAME);
121 if (lfile == NULL || lfile[0] == '\0') {
122 rc = asprintf(&lfile, "%s/log.%s",
123 get_dyn_LOGFILEBASE(), ext);
125 if (strstr(lfile, ext) == NULL) {
127 rc = asprintf(&lfile, "%s.%d",
128 lp_logfile(), child_id);
130 rc = asprintf(&lfile, "%s.%s",
137 lp_set_logfile(lfile);
146 static void update_conf(struct tevent_context *ev,
147 struct messaging_context *msg)
149 change_to_root_user();
150 lp_load(get_dyn_CONFIGFILE(), true, false, false, true);
151 reload_printers(ev, msg);
153 spoolss_reopen_logs(spoolss_child_id);
154 if (spoolss_child_id == 0) {
155 spoolss_prefork_config();
159 static void smb_conf_updated(struct messaging_context *msg,
162 struct server_id server_id,
165 struct tevent_context *ev_ctx = talloc_get_type_abort(private_data,
166 struct tevent_context);
168 DEBUG(10, ("Got message saying smb.conf was updated. Reloading.\n"));
169 update_conf(ev_ctx, msg);
172 static void update_pcap(struct tevent_context *ev_ctx,
173 struct messaging_context *msg_ctx)
175 change_to_root_user();
176 reload_printers(ev_ctx, msg_ctx);
179 static void pcap_updated(struct messaging_context *msg,
182 struct server_id server_id,
185 struct tevent_context *ev_ctx;
187 ev_ctx = talloc_get_type_abort(private_data, struct tevent_context);
189 DEBUG(10, ("Got message that pcap updated. Reloading.\n"));
190 update_pcap(ev_ctx, msg);
193 static void spoolss_sig_term_handler(struct tevent_context *ev,
194 struct tevent_signal *se,
200 exit_server_cleanly("termination signal");
203 static void spoolss_setup_sig_term_handler(struct tevent_context *ev_ctx)
205 struct tevent_signal *se;
207 se = tevent_add_signal(ev_ctx,
210 spoolss_sig_term_handler,
213 exit_server("failed to setup SIGTERM handler");
217 static void spoolss_sig_hup_handler(struct tevent_context *ev,
218 struct tevent_signal *se,
224 struct messaging_context *msg_ctx;
226 msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);
228 DEBUG(1,("Reloading printers after SIGHUP\n"));
229 update_conf(ev, msg_ctx);
231 /* relay to all children */
233 prefork_send_signal_to_all(spoolss_pool, SIGHUP);
237 static void spoolss_setup_sig_hup_handler(struct tevent_context *ev_ctx,
238 struct messaging_context *msg_ctx)
240 struct tevent_signal *se;
242 se = tevent_add_signal(ev_ctx,
245 spoolss_sig_hup_handler,
248 exit_server("failed to setup SIGHUP handler");
252 static bool spoolss_init_cb(void *ptr)
254 struct messaging_context *msg_ctx = talloc_get_type_abort(
255 ptr, struct messaging_context);
257 return nt_printing_tdb_migrate(msg_ctx);
260 static bool spoolss_shutdown_cb(void *ptr)
262 srv_spoolss_cleanup();
269 struct spoolss_chld_sig_hup_ctx {
270 struct messaging_context *msg_ctx;
271 struct pf_worker_data *pf;
274 static void spoolss_chld_sig_hup_handler(struct tevent_context *ev,
275 struct tevent_signal *se,
281 struct spoolss_chld_sig_hup_ctx *shc;
283 shc = talloc_get_type_abort(pvt, struct spoolss_chld_sig_hup_ctx);
285 /* avoid wasting CPU cycles if we are going to exit soon anyways */
286 if (shc->pf != NULL &&
287 shc->pf->cmds == PF_SRV_MSG_EXIT) {
291 change_to_root_user();
292 DEBUG(1,("Reloading printers after SIGHUP\n"));
293 reload_printers(ev, shc->msg_ctx);
294 spoolss_reopen_logs(spoolss_child_id);
297 static bool spoolss_setup_chld_hup_handler(struct tevent_context *ev_ctx,
298 struct messaging_context *msg_ctx,
299 struct pf_worker_data *pf)
301 struct spoolss_chld_sig_hup_ctx *shc;
302 struct tevent_signal *se;
304 shc = talloc(ev_ctx, struct spoolss_chld_sig_hup_ctx);
306 DEBUG(1, ("failed to setup SIGHUP handler"));
310 shc->msg_ctx = msg_ctx;
312 se = tevent_add_signal(ev_ctx,
315 spoolss_chld_sig_hup_handler,
318 DEBUG(1, ("failed to setup SIGHUP handler"));
325 static bool spoolss_child_init(struct tevent_context *ev_ctx,
326 int child_id, struct pf_worker_data *pf)
329 struct rpc_srv_callbacks spoolss_cb;
330 struct messaging_context *msg_ctx = server_messaging_context();
333 status = reinit_after_fork(msg_ctx, ev_ctx,
334 procid_self(), true);
335 if (!NT_STATUS_IS_OK(status)) {
336 DEBUG(0,("reinit_after_fork() failed\n"));
337 smb_panic("reinit_after_fork() failed");
340 spoolss_child_id = child_id;
341 spoolss_reopen_logs(child_id);
343 ok = spoolss_setup_chld_hup_handler(ev_ctx, msg_ctx, pf);
348 if (!serverid_register(procid_self(),
350 FLAG_MSG_PRINT_GENERAL)) {
354 if (!locking_init()) {
358 messaging_register(msg_ctx, ev_ctx,
359 MSG_SMB_CONF_UPDATED, smb_conf_updated);
360 messaging_register(msg_ctx, ev_ctx, MSG_PRINTER_PCAP,
363 /* As soon as messaging is up check if pcap has been loaded already.
364 * If so then we probably missed a message and should load_printers()
365 * ourselves. If pcap has not been loaded yet, then ignore, we will get
366 * a message as soon as the bq process completes the reload. */
367 if (pcap_cache_loaded()) {
368 load_printers(ev_ctx, msg_ctx);
371 /* try to reinit rpc queues */
372 spoolss_cb.init = spoolss_init_cb;
373 spoolss_cb.shutdown = spoolss_shutdown_cb;
374 spoolss_cb.private_data = msg_ctx;
376 status = rpc_winreg_init(NULL);
377 if (!NT_STATUS_IS_OK(status)) {
378 DEBUG(0, ("Failed to register winreg rpc inteface! (%s)\n",
383 status = rpc_spoolss_init(&spoolss_cb);
384 if (!NT_STATUS_IS_OK(status)) {
385 DEBUG(0, ("Failed to register spoolss rpc inteface! (%s)\n",
393 struct spoolss_children_data {
394 struct tevent_context *ev_ctx;
395 struct messaging_context *msg_ctx;
396 struct pf_worker_data *pf;
404 static void spoolss_next_client(void *pvt);
406 static int spoolss_children_main(struct tevent_context *ev_ctx,
407 struct messaging_context *msg_ctx,
408 struct pf_worker_data *pf,
415 struct spoolss_children_data *data;
419 ok = spoolss_child_init(ev_ctx, child_id, pf);
424 data = talloc(ev_ctx, struct spoolss_children_data);
429 data->ev_ctx = ev_ctx;
430 data->msg_ctx = msg_ctx;
431 data->lock_fd = lock_fd;
432 data->listen_fd_size = listen_fd_size;
433 data->listen_fds = listen_fds;
434 data->listening = false;
436 /* loop until it is time to exit */
437 while (pf->status != PF_WORKER_EXITING) {
438 /* try to see if it is time to schedule the next client */
439 spoolss_next_client(data);
441 ret = tevent_loop_once(ev_ctx);
443 DEBUG(0, ("tevent_loop_once() exited with %d: %s\n",
444 ret, strerror(errno)));
445 pf->status = PF_WORKER_EXITING;
452 static void spoolss_client_terminated(void *pvt)
454 struct spoolss_children_data *data;
456 data = talloc_get_type_abort(pvt, struct spoolss_children_data);
458 if (data->pf->num_clients) {
459 data->pf->num_clients--;
461 DEBUG(2, ("Invalid num clients, aborting!\n"));
462 data->pf->status = PF_WORKER_EXITING;
466 spoolss_next_client(pvt);
469 struct spoolss_new_client {
470 struct spoolss_children_data *data;
471 struct tsocket_address *srv_addr;
472 struct tsocket_address *cli_addr;
475 static void spoolss_handle_client(struct tevent_req *req);
477 static void spoolss_next_client(void *pvt)
479 struct tevent_req *req;
480 struct spoolss_children_data *data;
481 struct spoolss_new_client *next;
483 data = talloc_get_type_abort(pvt, struct spoolss_children_data);
485 if (data->pf->num_clients == 0) {
486 data->pf->status = PF_WORKER_IDLE;
489 if (data->pf->cmds == PF_SRV_MSG_EXIT) {
490 DEBUG(2, ("Parent process commands we terminate!\n"));
494 if (data->listening ||
495 data->pf->num_clients >= data->pf->allowed_clients) {
496 /* nothing to do for now we are already listening
497 * or reached the number of clients we are allowed
498 * to handle in parallel */
502 next = talloc_zero(data, struct spoolss_new_client);
504 DEBUG(1, ("Out of memory!?\n"));
509 req = prefork_listen_send(next, data->ev_ctx, data->pf,
510 data->listen_fd_size,
514 DEBUG(1, ("Failed to make listening request!?\n"));
518 tevent_req_set_callback(req, spoolss_handle_client, next);
520 data->listening = true;
523 static void spoolss_handle_client(struct tevent_req *req)
525 struct spoolss_children_data *data;
526 struct spoolss_new_client *client;
530 client = tevent_req_callback_data(req, struct spoolss_new_client);
533 ret = prefork_listen_recv(req, client, &sd,
534 &client->srv_addr, &client->cli_addr);
536 /* this will free the request too */
538 /* we are done listening */
539 data->listening = false;
542 DEBUG(1, ("Failed to accept client connection!\n"));
543 /* bail out if we are not serving any other client */
544 if (data->pf->num_clients == 0) {
545 data->pf->status = PF_WORKER_EXITING;
551 DEBUG(1, ("Server asks us to die!\n"));
552 data->pf->status = PF_WORKER_EXITING;
556 DEBUG(2, ("Spoolss preforked child %d got client connection!\n",
557 (int)(data->pf->pid)));
559 named_pipe_accept_function(data->ev_ctx, data->msg_ctx,
560 SPOOLSS_PIPE_NAME, sd,
561 spoolss_client_terminated, data);
564 /* ==== Main Process Functions ==== */
566 extern pid_t background_lpq_updater_pid;
567 static char *bq_logfile;
569 static void check_updater_child(void)
574 if (background_lpq_updater_pid == -1) {
578 pid = sys_waitpid(background_lpq_updater_pid, &status, WNOHANG);
580 DEBUG(2, ("The background queue child died... Restarting!\n"));
581 pid = start_background_queue(server_event_context(),
582 server_messaging_context(),
584 background_lpq_updater_pid = pid;
588 static bool spoolssd_schedule_check(struct tevent_context *ev_ctx,
589 struct messaging_context *msg_ctx,
590 struct timeval current_time);
591 static void spoolssd_check_children(struct tevent_context *ev_ctx,
592 struct tevent_timer *te,
593 struct timeval current_time,
596 static void spoolssd_sigchld_handler(struct tevent_context *ev_ctx,
597 struct prefork_pool *pfp,
600 struct messaging_context *msg_ctx;
604 msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);
606 /* now check we do not descend below the minimum */
607 active = prefork_count_active_children(pfp, &total);
610 if (total < spoolss_min_children) {
611 n = total - spoolss_min_children;
612 } else if (total - active < (total / 4)) {
613 n = spoolss_min_children;
617 r = prefork_add_children(ev_ctx, msg_ctx, pfp, n);
619 DEBUG(10, ("Tried to start %d children but only,"
620 "%d were actually started.!\n", n, r));
624 /* also check if the updater child is alive and well */
625 check_updater_child();
628 static bool spoolssd_setup_children_monitor(struct tevent_context *ev_ctx,
629 struct messaging_context *msg_ctx)
633 /* add our oun sigchld callback */
634 prefork_set_sigchld_callback(spoolss_pool,
635 spoolssd_sigchld_handler, msg_ctx);
637 ok = spoolssd_schedule_check(ev_ctx, msg_ctx,
638 tevent_timeval_current());
642 static bool spoolssd_schedule_check(struct tevent_context *ev_ctx,
643 struct messaging_context *msg_ctx,
644 struct timeval current_time)
646 struct tevent_timer *te;
647 struct timeval next_event;
649 /* check situation again in 10 seconds */
650 next_event = tevent_timeval_current_ofs(10, 0);
652 /* TODO: check when the socket becomes readable, so that children
653 * are checked only when there is some activity ? */
654 te = tevent_add_timer(ev_ctx, spoolss_pool, next_event,
655 spoolssd_check_children, msg_ctx);
657 DEBUG(2, ("Failed to set up children monitoring!\n"));
664 static void spoolssd_check_children(struct tevent_context *ev_ctx,
665 struct tevent_timer *te,
666 struct timeval current_time,
669 struct messaging_context *msg_ctx;
670 time_t now = time(NULL);
674 msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);
676 if ((spoolss_prefork_status & SPOOLSS_NEW_MAX) &&
677 !(spoolss_prefork_status & SPOLLSS_ENOSPC)) {
678 ret = prefork_expand_pool(spoolss_pool, spoolss_max_children);
680 spoolss_prefork_status |= SPOLLSS_ENOSPC;
682 spoolss_prefork_status &= ~SPOOLSS_NEW_MAX;
685 active = prefork_count_active_children(spoolss_pool, &total);
687 if (total - active < spoolss_spawn_rate) {
688 n = prefork_add_children(ev_ctx, msg_ctx,
689 spoolss_pool, spoolss_spawn_rate);
690 if (n < spoolss_spawn_rate) {
691 DEBUG(10, ("Tried to start 5 children but only,"
692 "%d were actually started.!\n", n));
696 if (total - active > spoolss_min_children) {
697 if ((total - spoolss_min_children) >= spoolss_spawn_rate) {
698 prefork_retire_children(spoolss_pool,
700 now - SPOOLSS_MIN_LIFE);
704 ret = spoolssd_schedule_check(ev_ctx, msg_ctx, current_time);
707 static void print_queue_forward(struct messaging_context *msg,
710 struct server_id server_id,
713 messaging_send_buf(msg, pid_to_procid(background_lpq_updater_pid),
714 MSG_PRINTER_UPDATE, data->data, data->length);
717 static char *get_bq_logfile(void)
719 char *lfile = lp_logfile();
722 if (lfile == NULL || lfile[0] == '\0') {
723 rc = asprintf(&lfile, "%s/log.%s.bq",
724 get_dyn_LOGFILEBASE(), DAEMON_NAME);
726 rc = asprintf(&lfile, "%s.bq", lp_logfile());
734 pid_t start_spoolssd(struct tevent_context *ev_ctx,
735 struct messaging_context *msg_ctx)
737 struct rpc_srv_callbacks spoolss_cb;
738 struct dcerpc_binding_vector *v;
746 DEBUG(1, ("Forking SPOOLSS Daemon\n"));
749 * Block signals before forking child as it will have to
750 * set its own handlers. Child will re-enable SIGHUP as
751 * soon as the handlers are set up.
753 BlockSignals(true, SIGTERM);
754 BlockSignals(true, SIGHUP);
759 DEBUG(0, ("Failed to fork SPOOLSS [%s]\n",
763 /* parent or error */
766 /* Re-enable SIGHUP before returnig */
767 BlockSignals(false, SIGTERM);
768 BlockSignals(false, SIGHUP);
773 close_low_fds(false);
775 status = reinit_after_fork(msg_ctx,
777 procid_self(), true);
778 if (!NT_STATUS_IS_OK(status)) {
779 DEBUG(0,("reinit_after_fork() failed\n"));
780 smb_panic("reinit_after_fork() failed");
783 spoolss_reopen_logs(0);
784 spoolss_prefork_config();
786 spoolss_setup_sig_term_handler(ev_ctx);
787 spoolss_setup_sig_hup_handler(ev_ctx, msg_ctx);
789 BlockSignals(false, SIGTERM);
790 BlockSignals(false, SIGHUP);
792 /* always start the backgroundqueue listner in spoolssd */
793 bq_logfile = get_bq_logfile();
794 pid = start_background_queue(ev_ctx, msg_ctx, bq_logfile);
796 background_lpq_updater_pid = pid;
799 /* the listening fd must be created before the children are actually
801 listen_fd = create_named_pipe_socket(SPOOLSS_PIPE_NAME);
802 if (listen_fd == -1) {
806 ret = listen(listen_fd, spoolss_max_children);
808 DEBUG(0, ("Failed to listen on spoolss pipe - %s\n",
813 /* start children before any more initialization is done */
814 ok = prefork_create_pool(ev_ctx, /* mem_ctx */
817 spoolss_min_children,
818 spoolss_max_children,
819 &spoolss_children_main, NULL,
825 if (!serverid_register(procid_self(),
827 FLAG_MSG_PRINT_GENERAL)) {
831 if (!locking_init()) {
835 messaging_register(msg_ctx, ev_ctx,
836 MSG_SMB_CONF_UPDATED, smb_conf_updated);
837 messaging_register(msg_ctx, NULL, MSG_PRINTER_UPDATE,
838 print_queue_forward);
839 messaging_register(msg_ctx, ev_ctx, MSG_PRINTER_PCAP,
842 /* As soon as messaging is up check if pcap has been loaded already.
843 * If so then we probably missed a message and should load_printers()
844 * ourselves. If pcap has not been loaded yet, then ignore, we will get
845 * a message as soon as the bq process completes the reload. */
846 if (pcap_cache_loaded()) {
847 load_printers(ev_ctx, msg_ctx);
850 mem_ctx = talloc_new(NULL);
851 if (mem_ctx == NULL) {
856 * Initialize spoolss with an init function to convert printers first.
857 * static_init_rpc will try to initialize the spoolss server too but you
858 * can't register it twice.
860 spoolss_cb.init = spoolss_init_cb;
861 spoolss_cb.shutdown = spoolss_shutdown_cb;
862 spoolss_cb.private_data = msg_ctx;
864 status = rpc_winreg_init(NULL);
865 if (!NT_STATUS_IS_OK(status)) {
866 DEBUG(0, ("Failed to register winreg rpc inteface! (%s)\n",
871 status = rpc_spoolss_init(&spoolss_cb);
872 if (!NT_STATUS_IS_OK(status)) {
873 DEBUG(0, ("Failed to register spoolss rpc inteface! (%s)\n",
878 status = dcerpc_binding_vector_new(mem_ctx, &v);
879 if (!NT_STATUS_IS_OK(status)) {
880 DEBUG(0, ("Failed to create binding vector (%s)\n",
885 status = dcerpc_binding_vector_add_np_default(&ndr_table_spoolss, v);
886 if (!NT_STATUS_IS_OK(status)) {
887 DEBUG(0, ("Failed to add np to binding vector (%s)\n",
892 status = rpc_ep_register(ev_ctx, msg_ctx, &ndr_table_spoolss, v);
893 if (!NT_STATUS_IS_OK(status)) {
894 DEBUG(0, ("Failed to register spoolss endpoint! (%s)\n",
899 talloc_free(mem_ctx);
901 ok = spoolssd_setup_children_monitor(ev_ctx, msg_ctx);
903 DEBUG(0, ("Failed to setup children monitoring!\n"));
907 DEBUG(1, ("SPOOLSS Daemon Started (%d)\n", getpid()));
910 ret = tevent_loop_wait(ev_ctx);
912 /* should not be reached */
913 DEBUG(0,("spoolssd tevent_loop_wait() exited with %d - %s\n",
914 ret, (ret == 0) ? "out of events" : strerror(errno)));