2 Unix SMB/Netbios implementation.
4 Copyright (C) Simo Sorce <idra@samba.org> 2010-2011
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"
37 #include "lib/server_prefork_util.h"
39 #define SPOOLSS_PIPE_NAME "spoolss"
40 #define DAEMON_NAME "spoolssd"
42 static struct server_id parent_id
;
43 static struct prefork_pool
*spoolss_pool
= NULL
;
44 static int spoolss_child_id
= 0;
46 static struct pf_daemon_config default_pf_spoolss_cfg
= {
47 .prefork_status
= PFH_INIT
,
51 .max_allowed_clients
= 100,
52 .child_min_life
= 60 /* 1 minute minimum life time */
54 static struct pf_daemon_config pf_spoolss_cfg
= { 0 };
56 pid_t
start_spoolssd(struct tevent_context
*ev_ctx
,
57 struct messaging_context
*msg_ctx
);
59 static void spoolss_reopen_logs(int child_id
)
61 char *lfile
= lp_logfile();
66 rc
= asprintf(&ext
, "%s.%d", DAEMON_NAME
, child_id
);
68 rc
= asprintf(&ext
, "%s", DAEMON_NAME
);
76 if (lfile
== NULL
|| lfile
[0] == '\0') {
77 rc
= asprintf(&lfile
, "%s/log.%s",
78 get_dyn_LOGFILEBASE(), ext
);
80 if (strstr(lfile
, ext
) == NULL
) {
82 rc
= asprintf(&lfile
, "%s.%d",
83 lp_logfile(), child_id
);
85 rc
= asprintf(&lfile
, "%s.%s",
92 lp_set_logfile(lfile
);
101 static void update_conf(struct tevent_context
*ev
,
102 struct messaging_context
*msg
)
104 change_to_root_user();
105 lp_load(get_dyn_CONFIGFILE(), true, false, false, true);
106 reload_printers(ev
, msg
);
108 spoolss_reopen_logs(spoolss_child_id
);
109 if (spoolss_child_id
== 0) {
110 pfh_daemon_config(DAEMON_NAME
,
112 &default_pf_spoolss_cfg
);
113 pfh_manage_pool(ev
, msg
, &pf_spoolss_cfg
, spoolss_pool
);
117 static void smb_conf_updated(struct messaging_context
*msg
,
120 struct server_id server_id
,
123 struct tevent_context
*ev_ctx
= talloc_get_type_abort(private_data
,
124 struct tevent_context
);
126 DEBUG(10, ("Got message saying smb.conf was updated. Reloading.\n"));
127 update_conf(ev_ctx
, msg
);
130 static void update_pcap(struct tevent_context
*ev_ctx
,
131 struct messaging_context
*msg_ctx
)
133 change_to_root_user();
134 reload_printers(ev_ctx
, msg_ctx
);
137 static void pcap_updated(struct messaging_context
*msg
,
140 struct server_id server_id
,
143 struct tevent_context
*ev_ctx
;
145 ev_ctx
= talloc_get_type_abort(private_data
, struct tevent_context
);
147 DEBUG(10, ("Got message that pcap updated. Reloading.\n"));
148 update_pcap(ev_ctx
, msg
);
151 static void spoolss_sig_term_handler(struct tevent_context
*ev
,
152 struct tevent_signal
*se
,
158 exit_server_cleanly("termination signal");
161 static void spoolss_setup_sig_term_handler(struct tevent_context
*ev_ctx
)
163 struct tevent_signal
*se
;
165 se
= tevent_add_signal(ev_ctx
,
168 spoolss_sig_term_handler
,
171 exit_server("failed to setup SIGTERM handler");
175 static void spoolss_sig_hup_handler(struct tevent_context
*ev
,
176 struct tevent_signal
*se
,
182 struct messaging_context
*msg_ctx
;
184 msg_ctx
= talloc_get_type_abort(pvt
, struct messaging_context
);
186 DEBUG(1,("Reloading printers after SIGHUP\n"));
187 update_conf(ev
, msg_ctx
);
189 /* relay to all children */
191 prefork_send_signal_to_all(spoolss_pool
, SIGHUP
);
195 static void spoolss_setup_sig_hup_handler(struct tevent_context
*ev_ctx
,
196 struct messaging_context
*msg_ctx
)
198 struct tevent_signal
*se
;
200 se
= tevent_add_signal(ev_ctx
,
203 spoolss_sig_hup_handler
,
206 exit_server("failed to setup SIGHUP handler");
210 static bool spoolss_init_cb(void *ptr
)
212 struct messaging_context
*msg_ctx
= talloc_get_type_abort(
213 ptr
, struct messaging_context
);
215 return nt_printing_tdb_migrate(msg_ctx
);
218 static bool spoolss_shutdown_cb(void *ptr
)
220 srv_spoolss_cleanup();
227 struct spoolss_chld_sig_hup_ctx
{
228 struct messaging_context
*msg_ctx
;
229 struct pf_worker_data
*pf
;
232 static void spoolss_chld_sig_hup_handler(struct tevent_context
*ev
,
233 struct tevent_signal
*se
,
239 struct spoolss_chld_sig_hup_ctx
*shc
;
241 shc
= talloc_get_type_abort(pvt
, struct spoolss_chld_sig_hup_ctx
);
243 /* avoid wasting CPU cycles if we are going to exit soon anyways */
244 if (shc
->pf
!= NULL
&&
245 shc
->pf
->cmds
== PF_SRV_MSG_EXIT
) {
249 change_to_root_user();
250 DEBUG(1,("Reloading printers after SIGHUP\n"));
251 reload_printers(ev
, shc
->msg_ctx
);
252 spoolss_reopen_logs(spoolss_child_id
);
255 static bool spoolss_setup_chld_hup_handler(struct tevent_context
*ev_ctx
,
256 struct messaging_context
*msg_ctx
,
257 struct pf_worker_data
*pf
)
259 struct spoolss_chld_sig_hup_ctx
*shc
;
260 struct tevent_signal
*se
;
262 shc
= talloc(ev_ctx
, struct spoolss_chld_sig_hup_ctx
);
264 DEBUG(1, ("failed to setup SIGHUP handler"));
268 shc
->msg_ctx
= msg_ctx
;
270 se
= tevent_add_signal(ev_ctx
,
273 spoolss_chld_sig_hup_handler
,
276 DEBUG(1, ("failed to setup SIGHUP handler"));
283 static void parent_ping(struct messaging_context
*msg_ctx
,
286 struct server_id server_id
,
290 /* The fact we received this message is enough to let make the event
291 * loop if it was idle. spoolss_children_main will cycle through
292 * spoolss_next_client at least once. That function will take whatever
293 * action is necessary */
295 DEBUG(10, ("Got message that the parent changed status.\n"));
299 static bool spoolss_child_init(struct tevent_context
*ev_ctx
,
300 int child_id
, struct pf_worker_data
*pf
)
303 struct rpc_srv_callbacks spoolss_cb
;
304 struct messaging_context
*msg_ctx
= server_messaging_context();
307 status
= reinit_after_fork(msg_ctx
, ev_ctx
,
308 procid_self(), true);
309 if (!NT_STATUS_IS_OK(status
)) {
310 DEBUG(0,("reinit_after_fork() failed\n"));
311 smb_panic("reinit_after_fork() failed");
314 spoolss_child_id
= child_id
;
315 spoolss_reopen_logs(child_id
);
317 ok
= spoolss_setup_chld_hup_handler(ev_ctx
, msg_ctx
, pf
);
322 if (!serverid_register(procid_self(),
324 FLAG_MSG_PRINT_GENERAL
)) {
328 if (!locking_init()) {
332 messaging_register(msg_ctx
, ev_ctx
,
333 MSG_SMB_CONF_UPDATED
, smb_conf_updated
);
334 messaging_register(msg_ctx
, ev_ctx
, MSG_PRINTER_PCAP
,
336 messaging_register(msg_ctx
, ev_ctx
,
337 MSG_PREFORK_PARENT_EVENT
, parent_ping
);
339 /* As soon as messaging is up check if pcap has been loaded already.
340 * If so then we probably missed a message and should load_printers()
341 * ourselves. If pcap has not been loaded yet, then ignore, we will get
342 * a message as soon as the bq process completes the reload. */
343 if (pcap_cache_loaded()) {
344 load_printers(ev_ctx
, msg_ctx
);
347 /* try to reinit rpc queues */
348 spoolss_cb
.init
= spoolss_init_cb
;
349 spoolss_cb
.shutdown
= spoolss_shutdown_cb
;
350 spoolss_cb
.private_data
= msg_ctx
;
352 status
= rpc_winreg_init(NULL
);
353 if (!NT_STATUS_IS_OK(status
)) {
354 DEBUG(0, ("Failed to register winreg rpc inteface! (%s)\n",
359 status
= rpc_spoolss_init(&spoolss_cb
);
360 if (!NT_STATUS_IS_OK(status
)) {
361 DEBUG(0, ("Failed to register spoolss rpc inteface! (%s)\n",
369 struct spoolss_children_data
{
370 struct tevent_context
*ev_ctx
;
371 struct messaging_context
*msg_ctx
;
372 struct pf_worker_data
*pf
;
377 static void spoolss_next_client(void *pvt
);
379 static int spoolss_children_main(struct tevent_context
*ev_ctx
,
380 struct messaging_context
*msg_ctx
,
381 struct pf_worker_data
*pf
,
387 struct spoolss_children_data
*data
;
391 ok
= spoolss_child_init(ev_ctx
, child_id
, pf
);
396 data
= talloc(ev_ctx
, struct spoolss_children_data
);
401 data
->ev_ctx
= ev_ctx
;
402 data
->msg_ctx
= msg_ctx
;
403 data
->listen_fd_size
= listen_fd_size
;
404 data
->listen_fds
= listen_fds
;
406 /* loop until it is time to exit */
407 while (pf
->status
!= PF_WORKER_EXITING
) {
408 /* try to see if it is time to schedule the next client */
409 spoolss_next_client(data
);
411 ret
= tevent_loop_once(ev_ctx
);
413 DEBUG(0, ("tevent_loop_once() exited with %d: %s\n",
414 ret
, strerror(errno
)));
415 pf
->status
= PF_WORKER_EXITING
;
422 static void spoolss_client_terminated(void *pvt
)
424 struct spoolss_children_data
*data
;
426 data
= talloc_get_type_abort(pvt
, struct spoolss_children_data
);
428 pfh_client_terminated(data
->pf
);
430 spoolss_next_client(pvt
);
433 struct spoolss_new_client
{
434 struct spoolss_children_data
*data
;
435 struct tsocket_address
*srv_addr
;
436 struct tsocket_address
*cli_addr
;
439 static void spoolss_handle_client(struct tevent_req
*req
);
441 static void spoolss_next_client(void *pvt
)
443 struct tevent_req
*req
;
444 struct spoolss_children_data
*data
;
445 struct spoolss_new_client
*next
;
447 data
= talloc_get_type_abort(pvt
, struct spoolss_children_data
);
449 if (!pfh_child_allowed_to_accept(data
->pf
)) {
450 /* nothing to do for now we are already listening
451 * or we are not allowed to listen further */
455 next
= talloc_zero(data
, struct spoolss_new_client
);
457 DEBUG(1, ("Out of memory!?\n"));
462 req
= prefork_listen_send(next
, data
->ev_ctx
, data
->pf
,
463 data
->listen_fd_size
,
466 DEBUG(1, ("Failed to make listening request!?\n"));
470 tevent_req_set_callback(req
, spoolss_handle_client
, next
);
473 static void spoolss_handle_client(struct tevent_req
*req
)
475 struct spoolss_children_data
*data
;
476 struct spoolss_new_client
*client
;
477 const DATA_BLOB ping
= data_blob_null
;
481 client
= tevent_req_callback_data(req
, struct spoolss_new_client
);
484 ret
= prefork_listen_recv(req
, client
, &sd
,
485 &client
->srv_addr
, &client
->cli_addr
);
487 /* this will free the request too */
491 DEBUG(6, ("No client connection was available after all!\n"));
495 /* Warn parent that our status changed */
496 messaging_send(data
->msg_ctx
, parent_id
,
497 MSG_PREFORK_CHILD_EVENT
, &ping
);
499 DEBUG(2, ("Spoolss preforked child %d got client connection!\n",
500 (int)(data
->pf
->pid
)));
502 named_pipe_accept_function(data
->ev_ctx
, data
->msg_ctx
,
503 SPOOLSS_PIPE_NAME
, sd
,
504 spoolss_client_terminated
, data
);
507 /* ==== Main Process Functions ==== */
509 extern pid_t background_lpq_updater_pid
;
510 static char *bq_logfile
;
512 static void check_updater_child(void)
517 if (background_lpq_updater_pid
== -1) {
521 pid
= sys_waitpid(background_lpq_updater_pid
, &status
, WNOHANG
);
523 DEBUG(2, ("The background queue child died... Restarting!\n"));
524 pid
= start_background_queue(server_event_context(),
525 server_messaging_context(),
527 background_lpq_updater_pid
= pid
;
531 static void child_ping(struct messaging_context
*msg_ctx
,
534 struct server_id server_id
,
537 struct tevent_context
*ev_ctx
;
539 ev_ctx
= talloc_get_type_abort(private_data
, struct tevent_context
);
541 DEBUG(10, ("Got message that a child changed status.\n"));
542 pfh_manage_pool(ev_ctx
, msg_ctx
, &pf_spoolss_cfg
, spoolss_pool
);
545 static bool spoolssd_schedule_check(struct tevent_context
*ev_ctx
,
546 struct messaging_context
*msg_ctx
,
547 struct timeval current_time
);
548 static void spoolssd_check_children(struct tevent_context
*ev_ctx
,
549 struct tevent_timer
*te
,
550 struct timeval current_time
,
553 static void spoolssd_sigchld_handler(struct tevent_context
*ev_ctx
,
554 struct prefork_pool
*pfp
,
557 struct messaging_context
*msg_ctx
;
559 msg_ctx
= talloc_get_type_abort(pvt
, struct messaging_context
);
561 /* run pool management so we can fork/retire or increase
562 * the allowed connections per child based on load */
563 pfh_manage_pool(ev_ctx
, msg_ctx
, &pf_spoolss_cfg
, spoolss_pool
);
565 /* also check if the updater child is alive and well */
566 check_updater_child();
569 static bool spoolssd_setup_children_monitor(struct tevent_context
*ev_ctx
,
570 struct messaging_context
*msg_ctx
)
574 /* add our oun sigchld callback */
575 prefork_set_sigchld_callback(spoolss_pool
,
576 spoolssd_sigchld_handler
, msg_ctx
);
578 ok
= spoolssd_schedule_check(ev_ctx
, msg_ctx
,
579 tevent_timeval_current());
583 static bool spoolssd_schedule_check(struct tevent_context
*ev_ctx
,
584 struct messaging_context
*msg_ctx
,
585 struct timeval current_time
)
587 struct tevent_timer
*te
;
588 struct timeval next_event
;
590 /* check situation again in 10 seconds */
591 next_event
= tevent_timeval_current_ofs(10, 0);
593 /* TODO: check when the socket becomes readable, so that children
594 * are checked only when there is some activity ? */
595 te
= tevent_add_timer(ev_ctx
, spoolss_pool
, next_event
,
596 spoolssd_check_children
, msg_ctx
);
598 DEBUG(2, ("Failed to set up children monitoring!\n"));
605 static void spoolssd_check_children(struct tevent_context
*ev_ctx
,
606 struct tevent_timer
*te
,
607 struct timeval current_time
,
610 struct messaging_context
*msg_ctx
;
612 msg_ctx
= talloc_get_type_abort(pvt
, struct messaging_context
);
614 pfh_manage_pool(ev_ctx
, msg_ctx
, &pf_spoolss_cfg
, spoolss_pool
);
616 spoolssd_schedule_check(ev_ctx
, msg_ctx
, current_time
);
619 static void print_queue_forward(struct messaging_context
*msg
,
622 struct server_id server_id
,
625 messaging_send_buf(msg
, pid_to_procid(background_lpq_updater_pid
),
626 MSG_PRINTER_UPDATE
, data
->data
, data
->length
);
629 static char *get_bq_logfile(void)
631 char *lfile
= lp_logfile();
634 if (lfile
== NULL
|| lfile
[0] == '\0') {
635 rc
= asprintf(&lfile
, "%s/log.%s.bq",
636 get_dyn_LOGFILEBASE(), DAEMON_NAME
);
638 rc
= asprintf(&lfile
, "%s.bq", lp_logfile());
646 pid_t
start_spoolssd(struct tevent_context
*ev_ctx
,
647 struct messaging_context
*msg_ctx
)
649 struct rpc_srv_callbacks spoolss_cb
;
650 struct dcerpc_binding_vector
*v
;
658 DEBUG(1, ("Forking SPOOLSS Daemon\n"));
661 * Block signals before forking child as it will have to
662 * set its own handlers. Child will re-enable SIGHUP as
663 * soon as the handlers are set up.
665 BlockSignals(true, SIGTERM
);
666 BlockSignals(true, SIGHUP
);
671 DEBUG(0, ("Failed to fork SPOOLSS [%s]\n",
675 /* parent or error */
678 /* Re-enable SIGHUP before returnig */
679 BlockSignals(false, SIGTERM
);
680 BlockSignals(false, SIGHUP
);
685 close_low_fds(false);
687 status
= reinit_after_fork(msg_ctx
,
689 procid_self(), true);
690 if (!NT_STATUS_IS_OK(status
)) {
691 DEBUG(0,("reinit_after_fork() failed\n"));
692 smb_panic("reinit_after_fork() failed");
695 /* save the parent process id so the children can use it later */
696 parent_id
= procid_self();
698 spoolss_reopen_logs(0);
699 pfh_daemon_config(DAEMON_NAME
,
701 &default_pf_spoolss_cfg
);
703 spoolss_setup_sig_term_handler(ev_ctx
);
704 spoolss_setup_sig_hup_handler(ev_ctx
, msg_ctx
);
706 BlockSignals(false, SIGTERM
);
707 BlockSignals(false, SIGHUP
);
709 /* always start the backgroundqueue listner in spoolssd */
710 bq_logfile
= get_bq_logfile();
711 pid
= start_background_queue(ev_ctx
, msg_ctx
, bq_logfile
);
713 background_lpq_updater_pid
= pid
;
716 /* the listening fd must be created before the children are actually
718 listen_fd
= create_named_pipe_socket(SPOOLSS_PIPE_NAME
);
719 if (listen_fd
== -1) {
723 ret
= listen(listen_fd
, pf_spoolss_cfg
.max_allowed_clients
);
725 DEBUG(0, ("Failed to listen on spoolss pipe - %s\n",
730 /* start children before any more initialization is done */
731 ok
= prefork_create_pool(ev_ctx
, /* mem_ctx */
734 pf_spoolss_cfg
.min_children
,
735 pf_spoolss_cfg
.max_children
,
736 &spoolss_children_main
, NULL
,
742 if (!serverid_register(procid_self(),
744 FLAG_MSG_PRINT_GENERAL
)) {
748 if (!locking_init()) {
752 messaging_register(msg_ctx
, ev_ctx
,
753 MSG_SMB_CONF_UPDATED
, smb_conf_updated
);
754 messaging_register(msg_ctx
, NULL
, MSG_PRINTER_UPDATE
,
755 print_queue_forward
);
756 messaging_register(msg_ctx
, ev_ctx
, MSG_PRINTER_PCAP
,
758 messaging_register(msg_ctx
, ev_ctx
,
759 MSG_PREFORK_CHILD_EVENT
, child_ping
);
761 /* As soon as messaging is up check if pcap has been loaded already.
762 * If so then we probably missed a message and should load_printers()
763 * ourselves. If pcap has not been loaded yet, then ignore, we will get
764 * a message as soon as the bq process completes the reload. */
765 if (pcap_cache_loaded()) {
766 load_printers(ev_ctx
, msg_ctx
);
769 mem_ctx
= talloc_new(NULL
);
770 if (mem_ctx
== NULL
) {
775 * Initialize spoolss with an init function to convert printers first.
776 * static_init_rpc will try to initialize the spoolss server too but you
777 * can't register it twice.
779 spoolss_cb
.init
= spoolss_init_cb
;
780 spoolss_cb
.shutdown
= spoolss_shutdown_cb
;
781 spoolss_cb
.private_data
= msg_ctx
;
783 status
= rpc_winreg_init(NULL
);
784 if (!NT_STATUS_IS_OK(status
)) {
785 DEBUG(0, ("Failed to register winreg rpc inteface! (%s)\n",
790 status
= rpc_spoolss_init(&spoolss_cb
);
791 if (!NT_STATUS_IS_OK(status
)) {
792 DEBUG(0, ("Failed to register spoolss rpc inteface! (%s)\n",
797 status
= dcerpc_binding_vector_new(mem_ctx
, &v
);
798 if (!NT_STATUS_IS_OK(status
)) {
799 DEBUG(0, ("Failed to create binding vector (%s)\n",
804 status
= dcerpc_binding_vector_add_np_default(&ndr_table_spoolss
, v
);
805 if (!NT_STATUS_IS_OK(status
)) {
806 DEBUG(0, ("Failed to add np to binding vector (%s)\n",
811 status
= rpc_ep_register(ev_ctx
, msg_ctx
, &ndr_table_spoolss
, v
);
812 if (!NT_STATUS_IS_OK(status
)) {
813 DEBUG(0, ("Failed to register spoolss endpoint! (%s)\n",
818 talloc_free(mem_ctx
);
820 ok
= spoolssd_setup_children_monitor(ev_ctx
, msg_ctx
);
822 DEBUG(0, ("Failed to setup children monitoring!\n"));
826 DEBUG(1, ("SPOOLSS Daemon Started (%d)\n", getpid()));
828 pfh_manage_pool(ev_ctx
, msg_ctx
, &pf_spoolss_cfg
, spoolss_pool
);
831 ret
= tevent_loop_wait(ev_ctx
);
833 /* should not be reached */
834 DEBUG(0,("spoolssd tevent_loop_wait() exited with %d - %s\n",
835 ret
, (ret
== 0) ? "out of events" : strerror(errno
)));