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"
29 #include "librpc/gen_ndr/srv_winreg.h"
30 #include "librpc/gen_ndr/srv_spoolss.h"
31 #include "rpc_server/rpc_server.h"
32 #include "rpc_server/rpc_ep_register.h"
33 #include "rpc_server/spoolss/srv_spoolss_nt.h"
34 #include "librpc/rpc/dcerpc_ep.h"
35 #include "lib/server_prefork.h"
37 #define SPOOLSS_PIPE_NAME "spoolss"
38 #define DAEMON_NAME "spoolssd"
40 #define SPOOLSS_MIN_CHILDREN 5
41 #define SPOOLSS_MAX_CHILDREN 25
42 #define SPOOLSS_SPAWN_RATE 5
43 #define SPOOLSS_MIN_LIFE 60 /* 1 minute minimum life time */
45 #define SPOOLSS_INIT 0x00
46 #define SPOOLSS_NEW_MAX 0x01
47 #define SPOLLSS_ENOSPC 0x02
49 static struct prefork_pool *spoolss_pool;
50 static int spoolss_min_children;
51 static int spoolss_max_children;
52 static int spoolss_spawn_rate;
53 static int spoolss_prefork_status;
55 static void spoolss_prefork_config(void)
57 static int spoolss_prefork_config_init = false;
58 const char *prefork_str;
60 bool use_defaults = false;
63 if (!spoolss_prefork_config_init) {
65 spoolss_prefork_status = SPOOLSS_INIT;
66 spoolss_min_children = 0;
67 spoolss_max_children = 0;
68 spoolss_spawn_rate = 0;
69 spoolss_prefork_config_init = true;
72 prefork_str = lp_parm_const_string(GLOBAL_SECTION_SNUM,
73 "spoolssd", "prefork", "none");
74 if (strcmp(prefork_str, "none") == 0) {
77 ret = sscanf(prefork_str, "%d:%d:%d", &min, &max, &rate);
79 DEBUG(0, ("invalid format for spoolssd:prefork!\n"));
85 min = SPOOLSS_MIN_CHILDREN;
86 max = SPOOLSS_MAX_CHILDREN;
87 rate = SPOOLSS_SPAWN_RATE;
90 if (max > spoolss_max_children && spoolss_max_children != 0) {
91 spoolss_prefork_status |= SPOOLSS_NEW_MAX;
94 spoolss_min_children = min;
95 spoolss_max_children = max;
96 spoolss_spawn_rate = rate;
99 static void spoolss_reopen_logs(int child_id)
101 char *lfile = lp_logfile();
106 rc = asprintf(&ext, ".%s.%d", DAEMON_NAME, child_id);
108 rc = asprintf(&ext, ".%s", DAEMON_NAME);
112 /* if we can't allocate, set it to NULL
113 * and logging will flow in the original file */
118 if (lfile == NULL || lfile[0] == '\0') {
119 rc = asprintf(&lfile, "%s/log%s",
120 get_dyn_LOGFILEBASE(), ext?ext:"");
122 if (ext && strstr(lfile, ext) == NULL) {
123 if (strstr(lfile, DAEMON_NAME) == NULL) {
124 rc = asprintf(&lfile, "%s%s",
125 lp_logfile(), ext?ext:"");
127 rc = asprintf(&lfile, "%s.%d",
128 lp_logfile(), child_id);
134 lp_set_logfile(lfile);
143 static void update_conf(struct tevent_context *ev,
144 struct messaging_context *msg)
146 change_to_root_user();
147 lp_load(get_dyn_CONFIGFILE(), true, false, false, true);
148 reload_printers(ev, msg);
150 spoolss_reopen_logs(0);
151 spoolss_prefork_config();
154 static void smb_conf_updated(struct messaging_context *msg,
157 struct server_id server_id,
160 struct tevent_context *ev_ctx = talloc_get_type_abort(private_data,
161 struct tevent_context);
163 DEBUG(10, ("Got message saying smb.conf was updated. Reloading.\n"));
164 update_conf(ev_ctx, msg);
167 static void spoolss_sig_term_handler(struct tevent_context *ev,
168 struct tevent_signal *se,
174 exit_server_cleanly("termination signal");
177 static void spoolss_setup_sig_term_handler(struct tevent_context *ev_ctx)
179 struct tevent_signal *se;
181 se = tevent_add_signal(ev_ctx,
184 spoolss_sig_term_handler,
187 exit_server("failed to setup SIGTERM handler");
191 static void spoolss_sig_hup_handler(struct tevent_context *ev,
192 struct tevent_signal *se,
198 struct messaging_context *msg_ctx;
200 msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);
202 DEBUG(1,("Reloading printers after SIGHUP\n"));
203 update_conf(ev, msg_ctx);
205 /* relay to all children */
207 prefork_send_signal_to_all(spoolss_pool, SIGHUP);
211 static void spoolss_setup_sig_hup_handler(struct tevent_context *ev_ctx,
212 struct messaging_context *msg_ctx)
214 struct tevent_signal *se;
216 se = tevent_add_signal(ev_ctx,
219 spoolss_sig_hup_handler,
222 exit_server("failed to setup SIGHUP handler");
226 static bool spoolss_init_cb(void *ptr)
228 struct messaging_context *msg_ctx = talloc_get_type_abort(
229 ptr, struct messaging_context);
231 return nt_printing_tdb_migrate(msg_ctx);
234 static bool spoolss_shutdown_cb(void *ptr)
236 srv_spoolss_cleanup();
243 struct spoolss_chld_sig_hup_ctx {
244 struct messaging_context *msg_ctx;
245 struct pf_worker_data *pf;
249 static void spoolss_chld_sig_hup_handler(struct tevent_context *ev,
250 struct tevent_signal *se,
256 struct spoolss_chld_sig_hup_ctx *shc;
258 shc = talloc_get_type_abort(pvt, struct spoolss_chld_sig_hup_ctx);
260 /* avoid wasting CPU cycles if we are going to exit soon anyways */
261 if (shc->pf != NULL &&
262 shc->pf->cmds == PF_SRV_MSG_EXIT) {
266 change_to_root_user();
267 DEBUG(1,("Reloading printers after SIGHUP\n"));
268 reload_printers(ev, shc->msg_ctx);
269 spoolss_reopen_logs(shc->child_id);
272 static bool spoolss_setup_chld_hup_handler(struct tevent_context *ev_ctx,
273 struct messaging_context *msg_ctx,
274 struct pf_worker_data *pf,
277 struct spoolss_chld_sig_hup_ctx *shc;
278 struct tevent_signal *se;
280 shc = talloc(ev_ctx, struct spoolss_chld_sig_hup_ctx);
282 DEBUG(1, ("failed to setup SIGHUP handler"));
285 shc->child_id = child_id;
287 shc->msg_ctx = msg_ctx;
289 se = tevent_add_signal(ev_ctx,
292 spoolss_chld_sig_hup_handler,
295 DEBUG(1, ("failed to setup SIGHUP handler"));
302 static bool spoolss_child_init(struct tevent_context *ev_ctx,
303 int child_id, struct pf_worker_data *pf)
306 struct rpc_srv_callbacks spoolss_cb;
307 struct messaging_context *msg_ctx = server_messaging_context();
310 status = reinit_after_fork(msg_ctx, ev_ctx,
311 procid_self(), true);
312 if (!NT_STATUS_IS_OK(status)) {
313 DEBUG(0,("reinit_after_fork() failed\n"));
314 smb_panic("reinit_after_fork() failed");
317 spoolss_reopen_logs(child_id);
319 ok = spoolss_setup_chld_hup_handler(ev_ctx, msg_ctx, pf, child_id);
324 if (!serverid_register(procid_self(), FLAG_MSG_GENERAL)) {
328 if (!locking_init()) {
332 messaging_register(msg_ctx, ev_ctx,
333 MSG_SMB_CONF_UPDATED, smb_conf_updated);
335 /* try to reinit rpc queues */
336 spoolss_cb.init = spoolss_init_cb;
337 spoolss_cb.shutdown = spoolss_shutdown_cb;
338 spoolss_cb.private_data = msg_ctx;
340 status = rpc_winreg_init(NULL);
341 if (!NT_STATUS_IS_OK(status)) {
342 DEBUG(0, ("Failed to register winreg rpc inteface! (%s)\n",
347 status = rpc_spoolss_init(&spoolss_cb);
348 if (!NT_STATUS_IS_OK(status)) {
349 DEBUG(0, ("Failed to register spoolss rpc inteface! (%s)\n",
354 reload_printers(ev_ctx, msg_ctx);
359 struct spoolss_children_data {
360 struct tevent_context *ev_ctx;
361 struct messaging_context *msg_ctx;
363 struct pf_worker_data *pf;
371 static void spoolss_next_client(void *pvt);
373 static int spoolss_children_main(struct tevent_context *ev_ctx,
374 struct messaging_context *msg_ctx,
375 struct pf_worker_data *pf,
382 struct spoolss_children_data *data;
386 ok = spoolss_child_init(ev_ctx, child_id, pf);
391 data = talloc(ev_ctx, struct spoolss_children_data);
395 data->child_id = child_id;
397 data->ev_ctx = ev_ctx;
398 data->msg_ctx = msg_ctx;
399 data->lock_fd = lock_fd;
400 data->listen_fd_size = listen_fd_size;
401 data->listen_fds = listen_fds;
402 data->listening = false;
404 /* loop until it is time to exit */
405 while (pf->status != PF_WORKER_EXITING) {
406 /* try to see if it is time to schedule the next client */
407 spoolss_next_client(data);
409 ret = tevent_loop_once(ev_ctx);
411 DEBUG(0, ("tevent_loop_once() exited with %d: %s\n",
412 ret, strerror(errno)));
413 pf->status = PF_WORKER_EXITING;
420 static void spoolss_client_terminated(void *pvt)
422 struct spoolss_children_data *data;
424 data = talloc_get_type_abort(pvt, struct spoolss_children_data);
426 if (data->pf->num_clients) {
427 data->pf->num_clients--;
429 DEBUG(2, ("Invalid num clients, aborting!\n"));
430 data->pf->status = PF_WORKER_EXITING;
434 spoolss_next_client(pvt);
437 struct spoolss_new_client {
438 struct spoolss_children_data *data;
439 struct tsocket_address *srv_addr;
440 struct tsocket_address *cli_addr;
443 static void spoolss_handle_client(struct tevent_req *req);
445 static void spoolss_next_client(void *pvt)
447 struct tevent_req *req;
448 struct spoolss_children_data *data;
449 struct spoolss_new_client *next;
451 data = talloc_get_type_abort(pvt, struct spoolss_children_data);
453 if (data->pf->num_clients == 0) {
454 data->pf->status = PF_WORKER_IDLE;
457 if (data->pf->cmds == PF_SRV_MSG_EXIT) {
458 DEBUG(2, ("Parent process commands we terminate!\n"));
462 if (data->listening ||
463 data->pf->num_clients >= data->pf->allowed_clients) {
464 /* nothing to do for now we are already listening
465 * or reached the number of clients we are allowed
466 * to handle in parallel */
470 next = talloc_zero(data, struct spoolss_new_client);
472 DEBUG(1, ("Out of memory!?\n"));
477 req = prefork_listen_send(next, data->ev_ctx, data->pf,
478 data->listen_fd_size,
482 DEBUG(1, ("Failed to make listening request!?\n"));
486 tevent_req_set_callback(req, spoolss_handle_client, next);
488 data->listening = true;
491 static void spoolss_handle_client(struct tevent_req *req)
493 struct spoolss_children_data *data;
494 struct spoolss_new_client *client;
498 client = tevent_req_callback_data(req, struct spoolss_new_client);
501 ret = prefork_listen_recv(req, client, &sd,
502 &client->srv_addr, &client->cli_addr);
504 /* this will free the request too */
506 /* we are done listening */
507 data->listening = false;
510 DEBUG(1, ("Failed to accept client connection!\n"));
511 /* bail out if we are not serving any other client */
512 if (data->pf->num_clients == 0) {
513 data->pf->status = PF_WORKER_EXITING;
519 DEBUG(1, ("Server asks us to die!\n"));
520 data->pf->status = PF_WORKER_EXITING;
524 DEBUG(2, ("Spoolss preforked child %d got client connection!\n",
525 (int)(data->pf->pid)));
527 named_pipe_accept_function(data->ev_ctx, data->msg_ctx,
528 SPOOLSS_PIPE_NAME, sd,
529 spoolss_client_terminated, data);
532 /* ==== Main Process Functions ==== */
534 extern pid_t background_lpq_updater_pid;
535 static char *bq_logfile;
537 static void check_updater_child(void)
542 if (background_lpq_updater_pid == -1) {
546 pid = sys_waitpid(background_lpq_updater_pid, &status, WNOHANG);
548 DEBUG(2, ("The background queue child died... Restarting!\n"));
549 pid = start_background_queue(server_event_context(),
550 server_messaging_context(),
552 background_lpq_updater_pid = pid;
556 static bool spoolssd_schedule_check(struct tevent_context *ev_ctx,
557 struct messaging_context *msg_ctx,
558 struct timeval current_time);
559 static void spoolssd_check_children(struct tevent_context *ev_ctx,
560 struct tevent_timer *te,
561 struct timeval current_time,
564 static void spoolssd_sigchld_handler(struct tevent_context *ev_ctx,
565 struct prefork_pool *pfp,
568 struct messaging_context *msg_ctx;
572 msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);
574 /* now check we do not descend below the minimum */
575 active = prefork_count_active_children(pfp, &total);
578 if (total < spoolss_min_children) {
579 n = total - spoolss_min_children;
580 } else if (total - active < (total / 4)) {
581 n = spoolss_min_children;
585 r = prefork_add_children(ev_ctx, msg_ctx, pfp, n);
587 DEBUG(10, ("Tried to start %d children but only,"
588 "%d were actually started.!\n", n, r));
592 /* also check if the updater child is alive and well */
593 check_updater_child();
596 static bool spoolssd_setup_children_monitor(struct tevent_context *ev_ctx,
597 struct messaging_context *msg_ctx)
601 /* add our oun sigchld callback */
602 prefork_set_sigchld_callback(spoolss_pool,
603 spoolssd_sigchld_handler, msg_ctx);
605 ok = spoolssd_schedule_check(ev_ctx, msg_ctx,
606 tevent_timeval_current());
610 static bool spoolssd_schedule_check(struct tevent_context *ev_ctx,
611 struct messaging_context *msg_ctx,
612 struct timeval current_time)
614 struct tevent_timer *te;
615 struct timeval next_event;
617 /* check situation again in 10 seconds */
618 next_event = tevent_timeval_current_ofs(10, 0);
620 /* TODO: check when the socket becomes readable, so that children
621 * are checked only when there is some activity ? */
622 te = tevent_add_timer(ev_ctx, spoolss_pool, next_event,
623 spoolssd_check_children, msg_ctx);
625 DEBUG(2, ("Failed to set up children monitoring!\n"));
632 static void spoolssd_check_children(struct tevent_context *ev_ctx,
633 struct tevent_timer *te,
634 struct timeval current_time,
637 struct messaging_context *msg_ctx;
638 time_t now = time(NULL);
642 msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);
644 if ((spoolss_prefork_status & SPOOLSS_NEW_MAX) &&
645 !(spoolss_prefork_status & SPOLLSS_ENOSPC)) {
646 ret = prefork_expand_pool(spoolss_pool, spoolss_max_children);
648 spoolss_prefork_status |= SPOLLSS_ENOSPC;
650 spoolss_prefork_status &= ~SPOOLSS_NEW_MAX;
653 active = prefork_count_active_children(spoolss_pool, &total);
655 if (total - active < spoolss_spawn_rate) {
656 n = prefork_add_children(ev_ctx, msg_ctx,
657 spoolss_pool, spoolss_spawn_rate);
658 if (n < spoolss_spawn_rate) {
659 DEBUG(10, ("Tried to start 5 children but only,"
660 "%d were actually started.!\n", n));
664 if (total - active > spoolss_min_children) {
665 if ((total - spoolss_min_children) >= spoolss_spawn_rate) {
666 prefork_retire_children(spoolss_pool,
668 now - SPOOLSS_MIN_LIFE);
672 ret = spoolssd_schedule_check(ev_ctx, msg_ctx, current_time);
675 static void print_queue_forward(struct messaging_context *msg,
678 struct server_id server_id,
681 messaging_send_buf(msg, pid_to_procid(background_lpq_updater_pid),
682 MSG_PRINTER_UPDATE, data->data, data->length);
685 char *get_bq_logfile(void)
687 char *lfile = lp_logfile();
690 if (lfile == NULL || lfile[0] == '\0') {
691 rc = asprintf(&lfile, "%s/log.%s.bq",
692 get_dyn_LOGFILEBASE(), DAEMON_NAME);
694 rc = asprintf(&lfile, "%s.bq", lp_logfile());
702 pid_t start_spoolssd(struct tevent_context *ev_ctx,
703 struct messaging_context *msg_ctx)
705 struct rpc_srv_callbacks spoolss_cb;
706 struct dcerpc_binding_vector *v;
714 DEBUG(1, ("Forking SPOOLSS Daemon\n"));
719 DEBUG(0, ("Failed to fork SPOOLSS [%s]\n",
723 /* parent or error */
728 close_low_fds(false);
730 status = reinit_after_fork(msg_ctx,
732 procid_self(), true);
733 if (!NT_STATUS_IS_OK(status)) {
734 DEBUG(0,("reinit_after_fork() failed\n"));
735 smb_panic("reinit_after_fork() failed");
738 spoolss_reopen_logs(0);
739 spoolss_prefork_config();
741 /* Publish nt printers, this requires a working winreg pipe */
742 pcap_cache_reload(ev_ctx, msg_ctx, &reload_printers);
744 /* always start the backgroundqueue listner in spoolssd */
745 bq_logfile = get_bq_logfile();
746 pid = start_background_queue(ev_ctx, msg_ctx, bq_logfile);
748 background_lpq_updater_pid = pid;
751 /* the listening fd must be created before the children are actually
753 listen_fd = create_named_pipe_socket(SPOOLSS_PIPE_NAME);
754 if (listen_fd == -1) {
758 ret = listen(listen_fd, spoolss_max_children);
760 DEBUG(0, ("Failed to listen on spoolss pipe - %s\n",
765 /* start children before any more initialization is done */
766 ok = prefork_create_pool(ev_ctx, /* mem_ctx */
769 spoolss_min_children,
770 spoolss_max_children,
771 &spoolss_children_main, NULL,
774 spoolss_setup_sig_term_handler(ev_ctx);
775 spoolss_setup_sig_hup_handler(ev_ctx, msg_ctx);
777 if (!serverid_register(procid_self(),
778 FLAG_MSG_GENERAL|FLAG_MSG_SMBD
779 |FLAG_MSG_PRINT_GENERAL)) {
783 if (!locking_init()) {
787 messaging_register(msg_ctx, ev_ctx,
788 MSG_SMB_CONF_UPDATED, smb_conf_updated);
789 messaging_register(msg_ctx, NULL, MSG_PRINTER_UPDATE,
790 print_queue_forward);
792 mem_ctx = talloc_new(NULL);
793 if (mem_ctx == NULL) {
798 * Initialize spoolss with an init function to convert printers first.
799 * static_init_rpc will try to initialize the spoolss server too but you
800 * can't register it twice.
802 spoolss_cb.init = spoolss_init_cb;
803 spoolss_cb.shutdown = spoolss_shutdown_cb;
804 spoolss_cb.private_data = msg_ctx;
806 status = rpc_winreg_init(NULL);
807 if (!NT_STATUS_IS_OK(status)) {
808 DEBUG(0, ("Failed to register winreg rpc inteface! (%s)\n",
813 status = rpc_spoolss_init(&spoolss_cb);
814 if (!NT_STATUS_IS_OK(status)) {
815 DEBUG(0, ("Failed to register spoolss rpc inteface! (%s)\n",
820 status = dcerpc_binding_vector_new(mem_ctx, &v);
821 if (!NT_STATUS_IS_OK(status)) {
822 DEBUG(0, ("Failed to create binding vector (%s)\n",
827 status = dcerpc_binding_vector_add_np_default(&ndr_table_spoolss, v);
828 if (!NT_STATUS_IS_OK(status)) {
829 DEBUG(0, ("Failed to add np to binding vector (%s)\n",
834 status = rpc_ep_register(ev_ctx, msg_ctx, &ndr_table_spoolss, v);
835 if (!NT_STATUS_IS_OK(status)) {
836 DEBUG(0, ("Failed to register spoolss endpoint! (%s)\n",
841 talloc_free(mem_ctx);
843 ok = spoolssd_setup_children_monitor(ev_ctx, msg_ctx);
845 DEBUG(0, ("Failed to setup children monitoring!\n"));
849 DEBUG(1, ("SPOOLSS Daemon Started (%d)\n", getpid()));
852 ret = tevent_loop_wait(ev_ctx);
854 /* should not be reached */
855 DEBUG(0,("background_queue: tevent_loop_wait() exited with %d - %s\n",
856 ret, (ret == 0) ? "out of events" : strerror(errno)));