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 prefork_pool
*spoolss_pool
= NULL
;
43 static int spoolss_child_id
= 0;
45 static struct pf_daemon_config default_pf_spoolss_cfg
= {
46 .prefork_status
= PFH_INIT
,
50 .max_allowed_clients
= 100,
51 .child_min_life
= 60 /* 1 minute minimum life time */
53 static struct pf_daemon_config pf_spoolss_cfg
= { 0 };
55 pid_t
start_spoolssd(struct tevent_context
*ev_ctx
,
56 struct messaging_context
*msg_ctx
);
58 static void spoolss_reopen_logs(int child_id
)
60 char *lfile
= lp_logfile();
65 rc
= asprintf(&ext
, "%s.%d", DAEMON_NAME
, child_id
);
67 rc
= asprintf(&ext
, "%s", DAEMON_NAME
);
75 if (lfile
== NULL
|| lfile
[0] == '\0') {
76 rc
= asprintf(&lfile
, "%s/log.%s",
77 get_dyn_LOGFILEBASE(), ext
);
79 if (strstr(lfile
, ext
) == NULL
) {
81 rc
= asprintf(&lfile
, "%s.%d",
82 lp_logfile(), child_id
);
84 rc
= asprintf(&lfile
, "%s.%s",
91 lp_set_logfile(lfile
);
100 static void update_conf(struct tevent_context
*ev
,
101 struct messaging_context
*msg
)
103 change_to_root_user();
104 lp_load(get_dyn_CONFIGFILE(), true, false, false, true);
105 reload_printers(ev
, msg
);
107 spoolss_reopen_logs(spoolss_child_id
);
108 if (spoolss_child_id
== 0) {
109 pfh_daemon_config(DAEMON_NAME
,
111 &default_pf_spoolss_cfg
);
112 pfh_manage_pool(ev
, msg
, &pf_spoolss_cfg
, spoolss_pool
);
116 static void smb_conf_updated(struct messaging_context
*msg
,
119 struct server_id server_id
,
122 struct tevent_context
*ev_ctx
= talloc_get_type_abort(private_data
,
123 struct tevent_context
);
125 DEBUG(10, ("Got message saying smb.conf was updated. Reloading.\n"));
126 update_conf(ev_ctx
, msg
);
129 static void update_pcap(struct tevent_context
*ev_ctx
,
130 struct messaging_context
*msg_ctx
)
132 change_to_root_user();
133 reload_printers(ev_ctx
, msg_ctx
);
136 static void pcap_updated(struct messaging_context
*msg
,
139 struct server_id server_id
,
142 struct tevent_context
*ev_ctx
;
144 ev_ctx
= talloc_get_type_abort(private_data
, struct tevent_context
);
146 DEBUG(10, ("Got message that pcap updated. Reloading.\n"));
147 update_pcap(ev_ctx
, msg
);
150 static void spoolss_sig_term_handler(struct tevent_context
*ev
,
151 struct tevent_signal
*se
,
157 exit_server_cleanly("termination signal");
160 static void spoolss_setup_sig_term_handler(struct tevent_context
*ev_ctx
)
162 struct tevent_signal
*se
;
164 se
= tevent_add_signal(ev_ctx
,
167 spoolss_sig_term_handler
,
170 exit_server("failed to setup SIGTERM handler");
174 static void spoolss_sig_hup_handler(struct tevent_context
*ev
,
175 struct tevent_signal
*se
,
181 struct messaging_context
*msg_ctx
;
183 msg_ctx
= talloc_get_type_abort(pvt
, struct messaging_context
);
185 DEBUG(1,("Reloading printers after SIGHUP\n"));
186 update_conf(ev
, msg_ctx
);
188 /* relay to all children */
190 prefork_send_signal_to_all(spoolss_pool
, SIGHUP
);
194 static void spoolss_setup_sig_hup_handler(struct tevent_context
*ev_ctx
,
195 struct messaging_context
*msg_ctx
)
197 struct tevent_signal
*se
;
199 se
= tevent_add_signal(ev_ctx
,
202 spoolss_sig_hup_handler
,
205 exit_server("failed to setup SIGHUP handler");
209 static bool spoolss_init_cb(void *ptr
)
211 struct messaging_context
*msg_ctx
= talloc_get_type_abort(
212 ptr
, struct messaging_context
);
214 return nt_printing_tdb_migrate(msg_ctx
);
217 static bool spoolss_shutdown_cb(void *ptr
)
219 srv_spoolss_cleanup();
226 struct spoolss_chld_sig_hup_ctx
{
227 struct messaging_context
*msg_ctx
;
228 struct pf_worker_data
*pf
;
231 static void spoolss_chld_sig_hup_handler(struct tevent_context
*ev
,
232 struct tevent_signal
*se
,
238 struct spoolss_chld_sig_hup_ctx
*shc
;
240 shc
= talloc_get_type_abort(pvt
, struct spoolss_chld_sig_hup_ctx
);
242 /* avoid wasting CPU cycles if we are going to exit soon anyways */
243 if (shc
->pf
!= NULL
&&
244 shc
->pf
->cmds
== PF_SRV_MSG_EXIT
) {
248 change_to_root_user();
249 DEBUG(1,("Reloading printers after SIGHUP\n"));
250 reload_printers(ev
, shc
->msg_ctx
);
251 spoolss_reopen_logs(spoolss_child_id
);
254 static bool spoolss_setup_chld_hup_handler(struct tevent_context
*ev_ctx
,
255 struct messaging_context
*msg_ctx
,
256 struct pf_worker_data
*pf
)
258 struct spoolss_chld_sig_hup_ctx
*shc
;
259 struct tevent_signal
*se
;
261 shc
= talloc(ev_ctx
, struct spoolss_chld_sig_hup_ctx
);
263 DEBUG(1, ("failed to setup SIGHUP handler"));
267 shc
->msg_ctx
= msg_ctx
;
269 se
= tevent_add_signal(ev_ctx
,
272 spoolss_chld_sig_hup_handler
,
275 DEBUG(1, ("failed to setup SIGHUP handler"));
282 static bool spoolss_child_init(struct tevent_context
*ev_ctx
,
283 int child_id
, struct pf_worker_data
*pf
)
286 struct rpc_srv_callbacks spoolss_cb
;
287 struct messaging_context
*msg_ctx
= server_messaging_context();
290 status
= reinit_after_fork(msg_ctx
, ev_ctx
,
291 procid_self(), true);
292 if (!NT_STATUS_IS_OK(status
)) {
293 DEBUG(0,("reinit_after_fork() failed\n"));
294 smb_panic("reinit_after_fork() failed");
297 spoolss_child_id
= child_id
;
298 spoolss_reopen_logs(child_id
);
300 ok
= spoolss_setup_chld_hup_handler(ev_ctx
, msg_ctx
, pf
);
305 if (!serverid_register(procid_self(),
307 FLAG_MSG_PRINT_GENERAL
)) {
311 if (!locking_init()) {
315 messaging_register(msg_ctx
, ev_ctx
,
316 MSG_SMB_CONF_UPDATED
, smb_conf_updated
);
317 messaging_register(msg_ctx
, ev_ctx
, MSG_PRINTER_PCAP
,
320 /* As soon as messaging is up check if pcap has been loaded already.
321 * If so then we probably missed a message and should load_printers()
322 * ourselves. If pcap has not been loaded yet, then ignore, we will get
323 * a message as soon as the bq process completes the reload. */
324 if (pcap_cache_loaded()) {
325 load_printers(ev_ctx
, msg_ctx
);
328 /* try to reinit rpc queues */
329 spoolss_cb
.init
= spoolss_init_cb
;
330 spoolss_cb
.shutdown
= spoolss_shutdown_cb
;
331 spoolss_cb
.private_data
= msg_ctx
;
333 status
= rpc_winreg_init(NULL
);
334 if (!NT_STATUS_IS_OK(status
)) {
335 DEBUG(0, ("Failed to register winreg rpc inteface! (%s)\n",
340 status
= rpc_spoolss_init(&spoolss_cb
);
341 if (!NT_STATUS_IS_OK(status
)) {
342 DEBUG(0, ("Failed to register spoolss rpc inteface! (%s)\n",
350 struct spoolss_children_data
{
351 struct tevent_context
*ev_ctx
;
352 struct messaging_context
*msg_ctx
;
353 struct pf_worker_data
*pf
;
360 static void spoolss_next_client(void *pvt
);
362 static int spoolss_children_main(struct tevent_context
*ev_ctx
,
363 struct messaging_context
*msg_ctx
,
364 struct pf_worker_data
*pf
,
370 struct spoolss_children_data
*data
;
374 ok
= spoolss_child_init(ev_ctx
, child_id
, pf
);
379 data
= talloc(ev_ctx
, struct spoolss_children_data
);
384 data
->ev_ctx
= ev_ctx
;
385 data
->msg_ctx
= msg_ctx
;
386 data
->listen_fd_size
= listen_fd_size
;
387 data
->listen_fds
= listen_fds
;
388 data
->listening
= false;
390 /* loop until it is time to exit */
391 while (pf
->status
!= PF_WORKER_EXITING
) {
392 /* try to see if it is time to schedule the next client */
393 spoolss_next_client(data
);
395 ret
= tevent_loop_once(ev_ctx
);
397 DEBUG(0, ("tevent_loop_once() exited with %d: %s\n",
398 ret
, strerror(errno
)));
399 pf
->status
= PF_WORKER_EXITING
;
406 static void spoolss_client_terminated(void *pvt
)
408 struct spoolss_children_data
*data
;
410 data
= talloc_get_type_abort(pvt
, struct spoolss_children_data
);
412 if (data
->pf
->num_clients
) {
413 data
->pf
->num_clients
--;
415 DEBUG(2, ("Invalid num clients, aborting!\n"));
416 data
->pf
->status
= PF_WORKER_EXITING
;
420 spoolss_next_client(pvt
);
423 struct spoolss_new_client
{
424 struct spoolss_children_data
*data
;
425 struct tsocket_address
*srv_addr
;
426 struct tsocket_address
*cli_addr
;
429 static void spoolss_handle_client(struct tevent_req
*req
);
431 static void spoolss_next_client(void *pvt
)
433 struct tevent_req
*req
;
434 struct spoolss_children_data
*data
;
435 struct spoolss_new_client
*next
;
437 data
= talloc_get_type_abort(pvt
, struct spoolss_children_data
);
439 if (data
->pf
->num_clients
== 0) {
440 data
->pf
->status
= PF_WORKER_IDLE
;
443 if (data
->pf
->cmds
== PF_SRV_MSG_EXIT
) {
444 DEBUG(2, ("Parent process commands we terminate!\n"));
448 if (data
->listening
||
449 data
->pf
->num_clients
>= data
->pf
->allowed_clients
) {
450 /* nothing to do for now we are already listening
451 * or reached the number of clients we are allowed
452 * to handle in parallel */
456 next
= talloc_zero(data
, struct spoolss_new_client
);
458 DEBUG(1, ("Out of memory!?\n"));
463 req
= prefork_listen_send(next
, data
->ev_ctx
, data
->pf
,
464 data
->listen_fd_size
,
467 DEBUG(1, ("Failed to make listening request!?\n"));
471 tevent_req_set_callback(req
, spoolss_handle_client
, next
);
473 data
->listening
= true;
476 static void spoolss_handle_client(struct tevent_req
*req
)
478 struct spoolss_children_data
*data
;
479 struct spoolss_new_client
*client
;
483 client
= tevent_req_callback_data(req
, struct spoolss_new_client
);
486 ret
= prefork_listen_recv(req
, client
, &sd
,
487 &client
->srv_addr
, &client
->cli_addr
);
489 /* this will free the request too */
491 /* we are done listening */
492 data
->listening
= false;
495 DEBUG(6, ("No client connection was available after all!\n"));
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 bool spoolssd_schedule_check(struct tevent_context
*ev_ctx
,
532 struct messaging_context
*msg_ctx
,
533 struct timeval current_time
);
534 static void spoolssd_check_children(struct tevent_context
*ev_ctx
,
535 struct tevent_timer
*te
,
536 struct timeval current_time
,
539 static void spoolssd_sigchld_handler(struct tevent_context
*ev_ctx
,
540 struct prefork_pool
*pfp
,
543 struct messaging_context
*msg_ctx
;
545 msg_ctx
= talloc_get_type_abort(pvt
, struct messaging_context
);
547 /* run pool management so we can fork/retire or increase
548 * the allowed connections per child based on load */
549 pfh_manage_pool(ev_ctx
, msg_ctx
, &pf_spoolss_cfg
, spoolss_pool
);
551 /* also check if the updater child is alive and well */
552 check_updater_child();
555 static bool spoolssd_setup_children_monitor(struct tevent_context
*ev_ctx
,
556 struct messaging_context
*msg_ctx
)
560 /* add our oun sigchld callback */
561 prefork_set_sigchld_callback(spoolss_pool
,
562 spoolssd_sigchld_handler
, msg_ctx
);
564 ok
= spoolssd_schedule_check(ev_ctx
, msg_ctx
,
565 tevent_timeval_current());
569 static bool spoolssd_schedule_check(struct tevent_context
*ev_ctx
,
570 struct messaging_context
*msg_ctx
,
571 struct timeval current_time
)
573 struct tevent_timer
*te
;
574 struct timeval next_event
;
576 /* check situation again in 10 seconds */
577 next_event
= tevent_timeval_current_ofs(10, 0);
579 /* TODO: check when the socket becomes readable, so that children
580 * are checked only when there is some activity ? */
581 te
= tevent_add_timer(ev_ctx
, spoolss_pool
, next_event
,
582 spoolssd_check_children
, msg_ctx
);
584 DEBUG(2, ("Failed to set up children monitoring!\n"));
591 static void spoolssd_check_children(struct tevent_context
*ev_ctx
,
592 struct tevent_timer
*te
,
593 struct timeval current_time
,
596 struct messaging_context
*msg_ctx
;
598 msg_ctx
= talloc_get_type_abort(pvt
, struct messaging_context
);
600 pfh_manage_pool(ev_ctx
, msg_ctx
, &pf_spoolss_cfg
, spoolss_pool
);
602 spoolssd_schedule_check(ev_ctx
, msg_ctx
, current_time
);
605 static void print_queue_forward(struct messaging_context
*msg
,
608 struct server_id server_id
,
611 messaging_send_buf(msg
, pid_to_procid(background_lpq_updater_pid
),
612 MSG_PRINTER_UPDATE
, data
->data
, data
->length
);
615 static char *get_bq_logfile(void)
617 char *lfile
= lp_logfile();
620 if (lfile
== NULL
|| lfile
[0] == '\0') {
621 rc
= asprintf(&lfile
, "%s/log.%s.bq",
622 get_dyn_LOGFILEBASE(), DAEMON_NAME
);
624 rc
= asprintf(&lfile
, "%s.bq", lp_logfile());
632 pid_t
start_spoolssd(struct tevent_context
*ev_ctx
,
633 struct messaging_context
*msg_ctx
)
635 struct rpc_srv_callbacks spoolss_cb
;
636 struct dcerpc_binding_vector
*v
;
644 DEBUG(1, ("Forking SPOOLSS Daemon\n"));
647 * Block signals before forking child as it will have to
648 * set its own handlers. Child will re-enable SIGHUP as
649 * soon as the handlers are set up.
651 BlockSignals(true, SIGTERM
);
652 BlockSignals(true, SIGHUP
);
657 DEBUG(0, ("Failed to fork SPOOLSS [%s]\n",
661 /* parent or error */
664 /* Re-enable SIGHUP before returnig */
665 BlockSignals(false, SIGTERM
);
666 BlockSignals(false, SIGHUP
);
671 close_low_fds(false);
673 status
= reinit_after_fork(msg_ctx
,
675 procid_self(), true);
676 if (!NT_STATUS_IS_OK(status
)) {
677 DEBUG(0,("reinit_after_fork() failed\n"));
678 smb_panic("reinit_after_fork() failed");
681 spoolss_reopen_logs(0);
682 pfh_daemon_config(DAEMON_NAME
,
684 &default_pf_spoolss_cfg
);
686 spoolss_setup_sig_term_handler(ev_ctx
);
687 spoolss_setup_sig_hup_handler(ev_ctx
, msg_ctx
);
689 BlockSignals(false, SIGTERM
);
690 BlockSignals(false, SIGHUP
);
692 /* always start the backgroundqueue listner in spoolssd */
693 bq_logfile
= get_bq_logfile();
694 pid
= start_background_queue(ev_ctx
, msg_ctx
, bq_logfile
);
696 background_lpq_updater_pid
= pid
;
699 /* the listening fd must be created before the children are actually
701 listen_fd
= create_named_pipe_socket(SPOOLSS_PIPE_NAME
);
702 if (listen_fd
== -1) {
706 ret
= listen(listen_fd
, pf_spoolss_cfg
.max_allowed_clients
);
708 DEBUG(0, ("Failed to listen on spoolss pipe - %s\n",
713 /* start children before any more initialization is done */
714 ok
= prefork_create_pool(ev_ctx
, /* mem_ctx */
717 pf_spoolss_cfg
.min_children
,
718 pf_spoolss_cfg
.max_children
,
719 &spoolss_children_main
, NULL
,
725 if (!serverid_register(procid_self(),
727 FLAG_MSG_PRINT_GENERAL
)) {
731 if (!locking_init()) {
735 messaging_register(msg_ctx
, ev_ctx
,
736 MSG_SMB_CONF_UPDATED
, smb_conf_updated
);
737 messaging_register(msg_ctx
, NULL
, MSG_PRINTER_UPDATE
,
738 print_queue_forward
);
739 messaging_register(msg_ctx
, ev_ctx
, MSG_PRINTER_PCAP
,
742 /* As soon as messaging is up check if pcap has been loaded already.
743 * If so then we probably missed a message and should load_printers()
744 * ourselves. If pcap has not been loaded yet, then ignore, we will get
745 * a message as soon as the bq process completes the reload. */
746 if (pcap_cache_loaded()) {
747 load_printers(ev_ctx
, msg_ctx
);
750 mem_ctx
= talloc_new(NULL
);
751 if (mem_ctx
== NULL
) {
756 * Initialize spoolss with an init function to convert printers first.
757 * static_init_rpc will try to initialize the spoolss server too but you
758 * can't register it twice.
760 spoolss_cb
.init
= spoolss_init_cb
;
761 spoolss_cb
.shutdown
= spoolss_shutdown_cb
;
762 spoolss_cb
.private_data
= msg_ctx
;
764 status
= rpc_winreg_init(NULL
);
765 if (!NT_STATUS_IS_OK(status
)) {
766 DEBUG(0, ("Failed to register winreg rpc inteface! (%s)\n",
771 status
= rpc_spoolss_init(&spoolss_cb
);
772 if (!NT_STATUS_IS_OK(status
)) {
773 DEBUG(0, ("Failed to register spoolss rpc inteface! (%s)\n",
778 status
= dcerpc_binding_vector_new(mem_ctx
, &v
);
779 if (!NT_STATUS_IS_OK(status
)) {
780 DEBUG(0, ("Failed to create binding vector (%s)\n",
785 status
= dcerpc_binding_vector_add_np_default(&ndr_table_spoolss
, v
);
786 if (!NT_STATUS_IS_OK(status
)) {
787 DEBUG(0, ("Failed to add np to binding vector (%s)\n",
792 status
= rpc_ep_register(ev_ctx
, msg_ctx
, &ndr_table_spoolss
, v
);
793 if (!NT_STATUS_IS_OK(status
)) {
794 DEBUG(0, ("Failed to register spoolss endpoint! (%s)\n",
799 talloc_free(mem_ctx
);
801 ok
= spoolssd_setup_children_monitor(ev_ctx
, msg_ctx
);
803 DEBUG(0, ("Failed to setup children monitoring!\n"));
807 DEBUG(1, ("SPOOLSS Daemon Started (%d)\n", getpid()));
809 pfh_manage_pool(ev_ctx
, msg_ctx
, &pf_spoolss_cfg
, spoolss_pool
);
812 ret
= tevent_loop_wait(ev_ctx
);
814 /* should not be reached */
815 DEBUG(0,("spoolssd tevent_loop_wait() exited with %d - %s\n",
816 ret
, (ret
== 0) ? "out of events" : strerror(errno
)));