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"
23 #include "lib/util/util_process.h"
26 #include "include/printing.h"
27 #include "printing/nt_printing_migrate_internal.h"
28 #include "printing/queue_process.h"
29 #include "printing/pcap.h"
30 #include "printing/load.h"
32 #include "librpc/gen_ndr/srv_winreg.h"
33 #include "librpc/gen_ndr/srv_spoolss.h"
34 #include "rpc_server/rpc_server.h"
35 #include "rpc_server/rpc_ep_register.h"
36 #include "rpc_server/rpc_config.h"
37 #include "rpc_server/spoolss/srv_spoolss_nt.h"
38 #include "librpc/rpc/dcerpc_ep.h"
39 #include "lib/server_prefork.h"
40 #include "lib/server_prefork_util.h"
42 #define SPOOLSS_PIPE_NAME "spoolss"
43 #define DAEMON_NAME "spoolssd"
45 static struct server_id parent_id
;
46 static struct prefork_pool
*spoolss_pool
= NULL
;
47 static int spoolss_child_id
= 0;
49 static struct pf_daemon_config default_pf_spoolss_cfg
= {
50 .prefork_status
= PFH_INIT
,
54 .max_allowed_clients
= 100,
55 .child_min_life
= 60 /* 1 minute minimum life time */
57 static struct pf_daemon_config pf_spoolss_cfg
= { 0 };
59 pid_t
start_spoolssd(struct tevent_context
*ev_ctx
,
60 struct messaging_context
*msg_ctx
);
62 static void spoolss_reopen_logs(int child_id
)
64 char *lfile
= lp_logfile(talloc_tos());
69 rc
= asprintf(&ext
, "%s.%d", DAEMON_NAME
, child_id
);
71 rc
= asprintf(&ext
, "%s", DAEMON_NAME
);
79 if (lfile
== NULL
|| lfile
[0] == '\0') {
80 rc
= asprintf(&lfile
, "%s/log.%s",
81 get_dyn_LOGFILEBASE(), ext
);
83 if (strstr(lfile
, ext
) == NULL
) {
85 rc
= asprintf(&lfile
, "%s.%d",
86 lp_logfile(talloc_tos()),
89 rc
= asprintf(&lfile
, "%s.%s",
90 lp_logfile(talloc_tos()),
97 lp_set_logfile(lfile
);
106 static void update_conf(struct tevent_context
*ev
,
107 struct messaging_context
*msg
)
109 change_to_root_user();
110 lp_load(get_dyn_CONFIGFILE(), true, false, false, true);
111 load_printers(ev
, msg
);
113 spoolss_reopen_logs(spoolss_child_id
);
114 if (spoolss_child_id
== 0) {
115 pfh_daemon_config(DAEMON_NAME
,
117 &default_pf_spoolss_cfg
);
118 pfh_manage_pool(ev
, msg
, &pf_spoolss_cfg
, spoolss_pool
);
122 static void smb_conf_updated(struct messaging_context
*msg
,
125 struct server_id server_id
,
128 struct tevent_context
*ev_ctx
= talloc_get_type_abort(private_data
,
129 struct tevent_context
);
131 DEBUG(10, ("Got message saying smb.conf was updated. Reloading.\n"));
132 update_conf(ev_ctx
, msg
);
135 static void update_pcap(struct tevent_context
*ev_ctx
,
136 struct messaging_context
*msg_ctx
)
138 change_to_root_user();
139 delete_and_reload_printers(ev_ctx
, msg_ctx
);
142 static void pcap_updated(struct messaging_context
*msg
,
145 struct server_id server_id
,
148 struct tevent_context
*ev_ctx
;
150 ev_ctx
= talloc_get_type_abort(private_data
, struct tevent_context
);
152 DEBUG(10, ("Got message that pcap updated. Reloading.\n"));
153 update_pcap(ev_ctx
, msg
);
156 static void spoolss_sig_term_handler(struct tevent_context
*ev
,
157 struct tevent_signal
*se
,
163 exit_server_cleanly("termination signal");
166 static void spoolss_setup_sig_term_handler(struct tevent_context
*ev_ctx
)
168 struct tevent_signal
*se
;
170 se
= tevent_add_signal(ev_ctx
,
173 spoolss_sig_term_handler
,
176 exit_server("failed to setup SIGTERM handler");
180 static void spoolss_sig_hup_handler(struct tevent_context
*ev
,
181 struct tevent_signal
*se
,
187 struct messaging_context
*msg_ctx
;
189 msg_ctx
= talloc_get_type_abort(pvt
, struct messaging_context
);
191 DEBUG(1,("Reloading printers after SIGHUP\n"));
192 update_conf(ev
, msg_ctx
);
194 /* relay to all children */
196 prefork_send_signal_to_all(spoolss_pool
, SIGHUP
);
200 static void spoolss_setup_sig_hup_handler(struct tevent_context
*ev_ctx
,
201 struct messaging_context
*msg_ctx
)
203 struct tevent_signal
*se
;
205 se
= tevent_add_signal(ev_ctx
,
208 spoolss_sig_hup_handler
,
211 exit_server("failed to setup SIGHUP handler");
215 static bool spoolss_init_cb(void *ptr
)
217 struct messaging_context
*msg_ctx
= talloc_get_type_abort(
218 ptr
, struct messaging_context
);
220 return nt_printing_tdb_migrate(msg_ctx
);
223 static bool spoolss_shutdown_cb(void *ptr
)
225 srv_spoolss_cleanup();
232 static void spoolss_chld_sig_hup_handler(struct tevent_context
*ev
,
233 struct tevent_signal
*se
,
239 struct messaging_context
*msg_ctx
;
241 msg_ctx
= talloc_get_type_abort(pvt
, struct messaging_context
);
243 change_to_root_user();
244 DEBUG(1,("Reloading printers after SIGHUP\n"));
245 load_printers(ev
, msg_ctx
);
246 spoolss_reopen_logs(spoolss_child_id
);
249 static bool spoolss_setup_chld_hup_handler(struct tevent_context
*ev_ctx
,
250 struct messaging_context
*msg_ctx
,
251 struct pf_worker_data
*pf
)
253 struct tevent_signal
*se
;
255 se
= tevent_add_signal(ev_ctx
,
258 spoolss_chld_sig_hup_handler
,
261 DEBUG(1, ("failed to setup SIGHUP handler"));
268 static void parent_ping(struct messaging_context
*msg_ctx
,
271 struct server_id server_id
,
275 /* The fact we received this message is enough to let make the event
276 * loop if it was idle. spoolss_children_main will cycle through
277 * spoolss_next_client at least once. That function will take whatever
278 * action is necessary */
280 DEBUG(10, ("Got message that the parent changed status.\n"));
284 static bool spoolss_child_init(struct tevent_context
*ev_ctx
,
285 int child_id
, struct pf_worker_data
*pf
)
288 struct rpc_srv_callbacks spoolss_cb
;
289 struct messaging_context
*msg_ctx
= server_messaging_context();
292 status
= reinit_after_fork(msg_ctx
, ev_ctx
,
294 if (!NT_STATUS_IS_OK(status
)) {
295 DEBUG(0,("reinit_after_fork() failed\n"));
296 smb_panic("reinit_after_fork() failed");
299 prctl_set_comment("spoolssd-child");
301 spoolss_child_id
= child_id
;
302 spoolss_reopen_logs(child_id
);
304 ok
= spoolss_setup_chld_hup_handler(ev_ctx
, msg_ctx
, pf
);
309 if (!serverid_register(messaging_server_id(msg_ctx
),
311 FLAG_MSG_PRINT_GENERAL
)) {
315 if (!locking_init()) {
319 messaging_register(msg_ctx
, ev_ctx
,
320 MSG_SMB_CONF_UPDATED
, smb_conf_updated
);
321 messaging_register(msg_ctx
, ev_ctx
, MSG_PRINTER_PCAP
,
323 messaging_register(msg_ctx
, ev_ctx
,
324 MSG_PREFORK_PARENT_EVENT
, parent_ping
);
326 /* As soon as messaging is up check if pcap has been loaded already.
327 * If so then we probably missed a message and should load_printers()
328 * ourselves. If pcap has not been loaded yet, then ignore, we will get
329 * a message as soon as the bq process completes the reload. */
330 if (pcap_cache_loaded()) {
331 load_printers(ev_ctx
, msg_ctx
);
334 /* try to reinit rpc queues */
335 spoolss_cb
.init
= spoolss_init_cb
;
336 spoolss_cb
.shutdown
= spoolss_shutdown_cb
;
337 spoolss_cb
.private_data
= msg_ctx
;
339 status
= rpc_winreg_init(NULL
);
340 if (!NT_STATUS_IS_OK(status
)) {
341 DEBUG(0, ("Failed to register winreg rpc inteface! (%s)\n",
346 status
= rpc_spoolss_init(&spoolss_cb
);
347 if (!NT_STATUS_IS_OK(status
)) {
348 DEBUG(0, ("Failed to register spoolss rpc inteface! (%s)\n",
356 struct spoolss_children_data
{
357 struct tevent_context
*ev_ctx
;
358 struct messaging_context
*msg_ctx
;
359 struct pf_worker_data
*pf
;
364 static void spoolss_next_client(void *pvt
);
366 static int spoolss_children_main(struct tevent_context
*ev_ctx
,
367 struct messaging_context
*msg_ctx
,
368 struct pf_worker_data
*pf
,
374 struct spoolss_children_data
*data
;
378 ok
= spoolss_child_init(ev_ctx
, child_id
, pf
);
383 data
= talloc(ev_ctx
, struct spoolss_children_data
);
388 data
->ev_ctx
= ev_ctx
;
389 data
->msg_ctx
= msg_ctx
;
390 data
->listen_fd_size
= listen_fd_size
;
391 data
->listen_fds
= listen_fds
;
393 /* loop until it is time to exit */
394 while (pf
->status
!= PF_WORKER_EXITING
) {
395 /* try to see if it is time to schedule the next client */
396 spoolss_next_client(data
);
398 ret
= tevent_loop_once(ev_ctx
);
400 DEBUG(0, ("tevent_loop_once() exited with %d: %s\n",
401 ret
, strerror(errno
)));
402 pf
->status
= PF_WORKER_EXITING
;
409 static void spoolss_client_terminated(void *pvt
)
411 struct spoolss_children_data
*data
;
413 data
= talloc_get_type_abort(pvt
, struct spoolss_children_data
);
415 pfh_client_terminated(data
->pf
);
417 spoolss_next_client(pvt
);
420 struct spoolss_new_client
{
421 struct spoolss_children_data
*data
;
422 struct tsocket_address
*srv_addr
;
423 struct tsocket_address
*cli_addr
;
426 static void spoolss_handle_client(struct tevent_req
*req
);
428 static void spoolss_next_client(void *pvt
)
430 struct tevent_req
*req
;
431 struct spoolss_children_data
*data
;
432 struct spoolss_new_client
*next
;
434 data
= talloc_get_type_abort(pvt
, struct spoolss_children_data
);
436 if (!pfh_child_allowed_to_accept(data
->pf
)) {
437 /* nothing to do for now we are already listening
438 * or we are not allowed to listen further */
442 next
= talloc_zero(data
, struct spoolss_new_client
);
444 DEBUG(1, ("Out of memory!?\n"));
449 req
= prefork_listen_send(next
, data
->ev_ctx
, data
->pf
,
450 data
->listen_fd_size
,
453 DEBUG(1, ("Failed to make listening request!?\n"));
457 tevent_req_set_callback(req
, spoolss_handle_client
, next
);
460 static void spoolss_handle_client(struct tevent_req
*req
)
462 struct spoolss_children_data
*data
;
463 struct spoolss_new_client
*client
;
464 const DATA_BLOB ping
= data_blob_null
;
468 client
= tevent_req_callback_data(req
, struct spoolss_new_client
);
471 ret
= prefork_listen_recv(req
, client
, &sd
,
472 &client
->srv_addr
, &client
->cli_addr
);
474 /* this will free the request too */
478 DEBUG(6, ("No client connection was available after all!\n"));
482 /* Warn parent that our status changed */
483 messaging_send(data
->msg_ctx
, parent_id
,
484 MSG_PREFORK_CHILD_EVENT
, &ping
);
486 DEBUG(2, ("Spoolss preforked child %d got client connection!\n",
487 (int)(data
->pf
->pid
)));
489 named_pipe_accept_function(data
->ev_ctx
, data
->msg_ctx
,
490 SPOOLSS_PIPE_NAME
, sd
,
491 spoolss_client_terminated
, data
);
494 /* ==== Main Process Functions ==== */
496 extern pid_t background_lpq_updater_pid
;
497 static char *bq_logfile
;
499 static void check_updater_child(struct tevent_context
*ev_ctx
,
500 struct messaging_context
*msg_ctx
)
505 if (background_lpq_updater_pid
== -1) {
509 pid
= sys_waitpid(background_lpq_updater_pid
, &status
, WNOHANG
);
511 DEBUG(2, ("The background queue child died... Restarting!\n"));
512 pid
= start_background_queue(ev_ctx
, msg_ctx
, bq_logfile
);
513 background_lpq_updater_pid
= pid
;
517 static void child_ping(struct messaging_context
*msg_ctx
,
520 struct server_id server_id
,
523 struct tevent_context
*ev_ctx
;
525 ev_ctx
= talloc_get_type_abort(private_data
, struct tevent_context
);
527 DEBUG(10, ("Got message that a child changed status.\n"));
528 pfh_manage_pool(ev_ctx
, msg_ctx
, &pf_spoolss_cfg
, spoolss_pool
);
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(ev_ctx
, msg_ctx
);
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(talloc_tos());
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(talloc_tos()));
632 pid_t
start_spoolssd(struct tevent_context
*ev_ctx
,
633 struct messaging_context
*msg_ctx
)
635 enum rpc_service_mode_e epm_mode
= rpc_epmapper_mode();
636 struct rpc_srv_callbacks spoolss_cb
;
637 struct dcerpc_binding_vector
*v
;
645 DEBUG(1, ("Forking SPOOLSS Daemon\n"));
648 * Block signals before forking child as it will have to
649 * set its own handlers. Child will re-enable SIGHUP as
650 * soon as the handlers are set up.
652 BlockSignals(true, SIGTERM
);
653 BlockSignals(true, SIGHUP
);
658 DEBUG(0, ("Failed to fork SPOOLSS [%s]\n",
662 /* parent or error */
665 /* Re-enable SIGHUP before returnig */
666 BlockSignals(false, SIGTERM
);
667 BlockSignals(false, SIGHUP
);
671 status
= reinit_after_fork(msg_ctx
,
674 if (!NT_STATUS_IS_OK(status
)) {
675 DEBUG(0,("reinit_after_fork() failed\n"));
676 smb_panic("reinit_after_fork() failed");
679 prctl_set_comment("spoolssd-master");
681 /* save the parent process id so the children can use it later */
682 parent_id
= messaging_server_id(msg_ctx
);
684 spoolss_reopen_logs(0);
685 pfh_daemon_config(DAEMON_NAME
,
687 &default_pf_spoolss_cfg
);
689 spoolss_setup_sig_term_handler(ev_ctx
);
690 spoolss_setup_sig_hup_handler(ev_ctx
, msg_ctx
);
692 BlockSignals(false, SIGTERM
);
693 BlockSignals(false, SIGHUP
);
695 /* always start the backgroundqueue listner in spoolssd */
696 bq_logfile
= get_bq_logfile();
697 pid
= start_background_queue(ev_ctx
, msg_ctx
, bq_logfile
);
699 background_lpq_updater_pid
= pid
;
702 /* the listening fd must be created before the children are actually
704 listen_fd
= create_named_pipe_socket(SPOOLSS_PIPE_NAME
);
705 if (listen_fd
== -1) {
709 ret
= listen(listen_fd
, pf_spoolss_cfg
.max_allowed_clients
);
711 DEBUG(0, ("Failed to listen on spoolss pipe - %s\n",
716 /* start children before any more initialization is done */
717 ok
= prefork_create_pool(ev_ctx
, /* mem_ctx */
720 pf_spoolss_cfg
.min_children
,
721 pf_spoolss_cfg
.max_children
,
722 &spoolss_children_main
, NULL
,
728 if (!serverid_register(messaging_server_id(msg_ctx
),
730 FLAG_MSG_PRINT_GENERAL
)) {
734 if (!locking_init()) {
738 messaging_register(msg_ctx
, ev_ctx
,
739 MSG_SMB_CONF_UPDATED
, smb_conf_updated
);
740 messaging_register(msg_ctx
, NULL
, MSG_PRINTER_UPDATE
,
741 print_queue_forward
);
742 messaging_register(msg_ctx
, ev_ctx
, MSG_PRINTER_PCAP
,
744 messaging_register(msg_ctx
, ev_ctx
,
745 MSG_PREFORK_CHILD_EVENT
, child_ping
);
747 /* As soon as messaging is up check if pcap has been loaded already.
748 * If so then we probably missed a message and should load_printers()
749 * ourselves. If pcap has not been loaded yet, then ignore, we will get
750 * a message as soon as the bq process completes the reload. */
751 if (pcap_cache_loaded()) {
752 load_printers(ev_ctx
, msg_ctx
);
755 mem_ctx
= talloc_new(NULL
);
756 if (mem_ctx
== NULL
) {
761 * Initialize spoolss with an init function to convert printers first.
762 * static_init_rpc will try to initialize the spoolss server too but you
763 * can't register it twice.
765 spoolss_cb
.init
= spoolss_init_cb
;
766 spoolss_cb
.shutdown
= spoolss_shutdown_cb
;
767 spoolss_cb
.private_data
= msg_ctx
;
769 status
= rpc_winreg_init(NULL
);
770 if (!NT_STATUS_IS_OK(status
)) {
771 DEBUG(0, ("Failed to register winreg rpc inteface! (%s)\n",
776 status
= rpc_spoolss_init(&spoolss_cb
);
777 if (!NT_STATUS_IS_OK(status
)) {
778 DEBUG(0, ("Failed to register spoolss rpc inteface! (%s)\n",
783 if (epm_mode
!= RPC_SERVICE_MODE_DISABLED
&&
784 (lp_parm_bool(-1, "rpc_server", "register_embedded_np", false))) {
785 status
= dcerpc_binding_vector_new(mem_ctx
, &v
);
786 if (!NT_STATUS_IS_OK(status
)) {
787 DEBUG(0, ("Failed to create binding vector (%s)\n",
792 status
= dcerpc_binding_vector_add_np_default(&ndr_table_spoolss
, v
);
793 if (!NT_STATUS_IS_OK(status
)) {
794 DEBUG(0, ("Failed to add np to binding vector (%s)\n",
799 status
= rpc_ep_register(ev_ctx
, msg_ctx
, &ndr_table_spoolss
, v
);
800 if (!NT_STATUS_IS_OK(status
)) {
801 DEBUG(0, ("Failed to register spoolss endpoint! (%s)\n",
807 talloc_free(mem_ctx
);
809 ok
= spoolssd_setup_children_monitor(ev_ctx
, msg_ctx
);
811 DEBUG(0, ("Failed to setup children monitoring!\n"));
815 DEBUG(1, ("SPOOLSS Daemon Started (%u)\n", (unsigned int)getpid()));
817 pfh_manage_pool(ev_ctx
, msg_ctx
, &pf_spoolss_cfg
, spoolss_pool
);
820 ret
= tevent_loop_wait(ev_ctx
);
822 /* should not be reached */
823 DEBUG(0,("spoolssd tevent_loop_wait() exited with %d - %s\n",
824 ret
, (ret
== 0) ? "out of events" : strerror(errno
)));