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_ALL_FINE 0x00
46 #define SPOOLSS_NEW_MAX 0x01
47 #define SPOLLSS_ENOSPC 0x02
49 static int spoolss_min_children
;
50 static int spoolss_max_children
;
51 static int spoolss_spawn_rate
;
52 static int spoolss_prefork_status
;
54 static void spoolss_prefork_config(void)
56 static int spoolss_prefork_config_init
= false;
57 const char *prefork_str
;
59 bool use_defaults
= false;
62 if (!spoolss_prefork_config_init
) {
63 spoolss_prefork_status
= SPOOLSS_ALL_FINE
;
64 spoolss_min_children
= 0;
65 spoolss_max_children
= 0;
66 spoolss_spawn_rate
= 0;
67 spoolss_prefork_config_init
= true;
70 prefork_str
= lp_parm_const_string(GLOBAL_SECTION_SNUM
,
71 "spoolssd", "prefork", "none");
72 if (strcmp(prefork_str
, "none") == 0) {
75 ret
= sscanf(prefork_str
, "%d:%d:%d", &min
, &max
, &rate
);
77 DEBUG(0, ("invalid format for spoolssd:prefork!\n"));
83 min
= SPOOLSS_MIN_CHILDREN
;
84 max
= SPOOLSS_MAX_CHILDREN
;
85 rate
= SPOOLSS_SPAWN_RATE
;
88 if (max
> spoolss_max_children
&& spoolss_max_children
!= 0) {
89 spoolss_prefork_status
|= SPOOLSS_NEW_MAX
;
92 spoolss_min_children
= min
;
93 spoolss_max_children
= max
;
94 spoolss_spawn_rate
= rate
;
97 static void spoolss_reopen_logs(void)
99 char *lfile
= lp_logfile();
102 if (lfile
== NULL
|| lfile
[0] == '\0') {
103 rc
= asprintf(&lfile
, "%s/log.%s", get_dyn_LOGFILEBASE(), DAEMON_NAME
);
105 lp_set_logfile(lfile
);
109 if (strstr(lfile
, DAEMON_NAME
) == NULL
) {
110 rc
= asprintf(&lfile
, "%s.%s", lp_logfile(), DAEMON_NAME
);
112 lp_set_logfile(lfile
);
121 static void update_conf(struct tevent_context
*ev
,
122 struct messaging_context
*msg
)
124 change_to_root_user();
125 lp_load(get_dyn_CONFIGFILE(), true, false, false, true);
126 reload_printers(ev
, msg
);
128 spoolss_reopen_logs();
129 spoolss_prefork_config();
132 static void smb_conf_updated(struct messaging_context
*msg
,
135 struct server_id server_id
,
138 struct tevent_context
*ev_ctx
= talloc_get_type_abort(private_data
,
139 struct tevent_context
);
141 DEBUG(10, ("Got message saying smb.conf was updated. Reloading.\n"));
142 update_conf(ev_ctx
, msg
);
145 static void spoolss_sig_term_handler(struct tevent_context
*ev
,
146 struct tevent_signal
*se
,
152 exit_server_cleanly("termination signal");
155 static void spoolss_setup_sig_term_handler(struct tevent_context
*ev_ctx
)
157 struct tevent_signal
*se
;
159 se
= tevent_add_signal(ev_ctx
,
162 spoolss_sig_term_handler
,
165 exit_server("failed to setup SIGTERM handler");
169 struct spoolss_hup_ctx
{
170 struct messaging_context
*msg_ctx
;
171 struct prefork_pool
*pfp
;
174 static void spoolss_sig_hup_handler(struct tevent_context
*ev
,
175 struct tevent_signal
*se
,
181 struct spoolss_hup_ctx
*hup_ctx
;
183 hup_ctx
= talloc_get_type_abort(pvt
, struct spoolss_hup_ctx
);
185 DEBUG(1,("Reloading printers after SIGHUP\n"));
186 update_conf(ev
, hup_ctx
->msg_ctx
);
188 /* relay to all children */
189 prefork_send_signal_to_all(hup_ctx
->pfp
, SIGHUP
);
192 static void spoolss_setup_sig_hup_handler(struct tevent_context
*ev_ctx
,
193 struct prefork_pool
*pfp
,
194 struct messaging_context
*msg_ctx
)
196 struct spoolss_hup_ctx
*hup_ctx
;
197 struct tevent_signal
*se
;
199 hup_ctx
= talloc(ev_ctx
, struct spoolss_hup_ctx
);
201 exit_server("failed to setup SIGHUP handler");
204 hup_ctx
->msg_ctx
= msg_ctx
;
206 se
= tevent_add_signal(ev_ctx
,
209 spoolss_sig_hup_handler
,
212 exit_server("failed to setup SIGHUP handler");
216 static bool spoolss_init_cb(void *ptr
)
218 struct messaging_context
*msg_ctx
= talloc_get_type_abort(
219 ptr
, struct messaging_context
);
221 return nt_printing_tdb_migrate(msg_ctx
);
224 static bool spoolss_shutdown_cb(void *ptr
)
226 srv_spoolss_cleanup();
233 struct spoolss_chld_sig_hup_ctx
{
234 struct messaging_context
*msg_ctx
;
235 struct pf_worker_data
*pf
;
238 static void spoolss_chld_sig_hup_handler(struct tevent_context
*ev
,
239 struct tevent_signal
*se
,
245 struct spoolss_chld_sig_hup_ctx
*shc
;
247 shc
= talloc_get_type_abort(pvt
, struct spoolss_chld_sig_hup_ctx
);
249 /* avoid wasting CPU cycles if we are going to exit soon anyways */
250 if (shc
->pf
!= NULL
&&
251 shc
->pf
->cmds
== PF_SRV_MSG_EXIT
) {
255 change_to_root_user();
256 DEBUG(1,("Reloading printers after SIGHUP\n"));
257 reload_printers(ev
, shc
->msg_ctx
);
258 spoolss_reopen_logs();
261 static bool spoolss_setup_chld_hup_handler(struct tevent_context
*ev_ctx
,
262 struct pf_worker_data
*pf
,
263 struct messaging_context
*msg_ctx
)
265 struct spoolss_chld_sig_hup_ctx
*shc
;
266 struct tevent_signal
*se
;
268 shc
= talloc(ev_ctx
, struct spoolss_chld_sig_hup_ctx
);
270 DEBUG(1, ("failed to setup SIGHUP handler"));
274 shc
->msg_ctx
= msg_ctx
;
276 se
= tevent_add_signal(ev_ctx
,
279 spoolss_chld_sig_hup_handler
,
282 DEBUG(1, ("failed to setup SIGHUP handler"));
289 static bool spoolss_child_init(struct tevent_context
*ev_ctx
,
290 struct pf_worker_data
*pf
)
293 struct rpc_srv_callbacks spoolss_cb
;
294 struct messaging_context
*msg_ctx
= server_messaging_context();
297 status
= reinit_after_fork(msg_ctx
, ev_ctx
,
298 procid_self(), true);
299 if (!NT_STATUS_IS_OK(status
)) {
300 DEBUG(0,("reinit_after_fork() failed\n"));
301 smb_panic("reinit_after_fork() failed");
304 spoolss_reopen_logs();
306 ok
= spoolss_setup_chld_hup_handler(ev_ctx
, pf
, msg_ctx
);
311 if (!serverid_register(procid_self(), FLAG_MSG_GENERAL
)) {
315 if (!locking_init()) {
319 messaging_register(msg_ctx
, ev_ctx
,
320 MSG_SMB_CONF_UPDATED
, smb_conf_updated
);
322 /* try to reinit rpc queues */
323 spoolss_cb
.init
= spoolss_init_cb
;
324 spoolss_cb
.shutdown
= spoolss_shutdown_cb
;
325 spoolss_cb
.private_data
= msg_ctx
;
327 status
= rpc_winreg_init(NULL
);
328 if (!NT_STATUS_IS_OK(status
)) {
329 DEBUG(0, ("Failed to register winreg rpc inteface! (%s)\n",
334 status
= rpc_spoolss_init(&spoolss_cb
);
335 if (!NT_STATUS_IS_OK(status
)) {
336 DEBUG(0, ("Failed to register spoolss rpc inteface! (%s)\n",
341 reload_printers(ev_ctx
, msg_ctx
);
346 struct spoolss_children_data
{
347 struct tevent_context
*ev_ctx
;
348 struct messaging_context
*msg_ctx
;
349 struct pf_worker_data
*pf
;
357 static void spoolss_next_client(void *pvt
);
359 static int spoolss_children_main(struct tevent_context
*ev_ctx
,
360 struct pf_worker_data
*pf
,
366 struct messaging_context
*msg_ctx
= server_messaging_context();
367 struct spoolss_children_data
*data
;
371 ok
= spoolss_child_init(ev_ctx
, pf
);
376 data
= talloc(ev_ctx
, struct spoolss_children_data
);
381 data
->ev_ctx
= ev_ctx
;
382 data
->msg_ctx
= msg_ctx
;
383 data
->lock_fd
= lock_fd
;
384 data
->listen_fd_size
= listen_fd_size
;
385 data
->listen_fds
= listen_fds
;
386 data
->listening
= false;
388 /* loop until it is time to exit */
389 while (pf
->status
!= PF_WORKER_EXITING
) {
390 /* try to see if it is time to schedule the next client */
391 spoolss_next_client(data
);
393 ret
= tevent_loop_once(ev_ctx
);
395 DEBUG(0, ("tevent_loop_once() exited with %d: %s\n",
396 ret
, strerror(errno
)));
397 pf
->status
= PF_WORKER_EXITING
;
404 static void spoolss_client_terminated(void *pvt
)
406 struct spoolss_children_data
*data
;
408 data
= talloc_get_type_abort(pvt
, struct spoolss_children_data
);
410 if (data
->pf
->num_clients
) {
411 data
->pf
->num_clients
--;
413 DEBUG(2, ("Invalid num clients, aborting!\n"));
414 data
->pf
->status
= PF_WORKER_EXITING
;
418 spoolss_next_client(pvt
);
421 struct spoolss_new_client
{
422 struct spoolss_children_data
*data
;
423 struct sockaddr_un sunaddr
;
427 static void spoolss_handle_client(struct tevent_req
*req
);
429 static void spoolss_next_client(void *pvt
)
431 struct tevent_req
*req
;
432 struct spoolss_children_data
*data
;
433 struct spoolss_new_client
*next
;
435 data
= talloc_get_type_abort(pvt
, struct spoolss_children_data
);
437 if (data
->pf
->num_clients
== 0) {
438 data
->pf
->status
= PF_WORKER_IDLE
;
441 if (data
->pf
->cmds
== PF_SRV_MSG_EXIT
) {
442 DEBUG(2, ("Parent process commands we terminate!\n"));
446 if (data
->listening
||
447 data
->pf
->num_clients
>= data
->pf
->allowed_clients
) {
448 /* nothing to do for now we are already listening
449 * or reached the number of clients we are allowed
450 * to handle in parallel */
454 next
= talloc_zero(data
, struct spoolss_new_client
);
456 DEBUG(1, ("Out of memory!?\n"));
460 next
->addrlen
= sizeof(next
->sunaddr
);
462 req
= prefork_listen_send(next
, data
->ev_ctx
, data
->pf
,
463 data
->listen_fd_size
,
466 (struct sockaddr
*)&next
->sunaddr
,
469 DEBUG(1, ("Failed to make listening request!?\n"));
473 tevent_req_set_callback(req
, spoolss_handle_client
, next
);
475 data
->listening
= true;
478 static void spoolss_handle_client(struct tevent_req
*req
)
480 struct spoolss_children_data
*data
;
481 struct spoolss_new_client
*client
;
485 client
= tevent_req_callback_data(req
, struct spoolss_new_client
);
488 ret
= prefork_listen_recv(req
, &sd
);
490 /* this will free the request too */
492 /* we are done listening */
493 data
->listening
= false;
496 DEBUG(1, ("Failed to accept client connection!\n"));
497 /* bail out if we are not serving any other client */
498 if (data
->pf
->num_clients
== 0) {
499 data
->pf
->status
= PF_WORKER_EXITING
;
505 DEBUG(1, ("Server asks us to die!\n"));
506 data
->pf
->status
= PF_WORKER_EXITING
;
510 DEBUG(2, ("Spoolss preforked child %d got client connection!\n",
511 (int)(data
->pf
->pid
)));
513 named_pipe_accept_function(data
->ev_ctx
, data
->msg_ctx
,
514 SPOOLSS_PIPE_NAME
, sd
,
515 spoolss_client_terminated
, data
);
518 /* ==== Main Process Functions ==== */
520 extern pid_t background_lpq_updater_pid
;
522 static void check_updater_child(void)
527 if (background_lpq_updater_pid
== -1) {
531 pid
= sys_waitpid(background_lpq_updater_pid
, &status
, WNOHANG
);
533 DEBUG(2, ("The background queue child died... Restarting!\n"));
534 pid
= start_background_queue(server_event_context(),
535 server_messaging_context());
536 background_lpq_updater_pid
= pid
;
540 static void spoolssd_sig_chld_handler(struct tevent_context
*ev_ctx
,
541 struct tevent_signal
*se
,
542 int signum
, int count
,
543 void *siginfo
, void *pvt
)
545 struct prefork_pool
*pfp
;
549 pfp
= talloc_get_type_abort(pvt
, struct prefork_pool
);
551 /* run the cleanup function to make sure all dead children are
552 * properly and timely retired. */
553 prefork_cleanup_loop(pfp
);
555 /* now check we do not descend below the minimum */
556 active
= prefork_count_active_children(pfp
, &total
);
559 if (total
< spoolss_min_children
) {
560 n
= total
- spoolss_min_children
;
561 } else if (total
- active
< (total
/ 4)) {
562 n
= spoolss_min_children
;
566 r
= prefork_add_children(ev_ctx
, pfp
, n
);
568 DEBUG(10, ("Tried to start %d children but only,"
569 "%d were actually started.!\n", n
, r
));
573 /* also check if the updater child is alive and well */
574 check_updater_child();
577 static bool spoolssd_setup_sig_chld_handler(struct tevent_context
*ev_ctx
,
578 struct prefork_pool
*pfp
)
580 struct tevent_signal
*se
;
582 se
= tevent_add_signal(ev_ctx
, ev_ctx
, SIGCHLD
, 0,
583 spoolssd_sig_chld_handler
, pfp
);
585 DEBUG(0, ("Failed to setup SIGCHLD handler!\n"));
592 static bool spoolssd_schedule_check(struct tevent_context
*ev_ctx
,
593 struct prefork_pool
*pfp
,
594 struct timeval current_time
);
595 static void spoolssd_check_children(struct tevent_context
*ev_ctx
,
596 struct tevent_timer
*te
,
597 struct timeval current_time
,
600 static bool spoolssd_setup_children_monitor(struct tevent_context
*ev_ctx
,
601 struct prefork_pool
*pfp
)
605 ok
= spoolssd_setup_sig_chld_handler(ev_ctx
, pfp
);
610 ok
= spoolssd_schedule_check(ev_ctx
, pfp
, tevent_timeval_current());
614 static bool spoolssd_schedule_check(struct tevent_context
*ev_ctx
,
615 struct prefork_pool
*pfp
,
616 struct timeval current_time
)
618 struct tevent_timer
*te
;
619 struct timeval next_event
;
621 /* check situation again in 10 seconds */
622 next_event
= tevent_timeval_current_ofs(10, 0);
624 /* TODO: check when the socket becomes readable, so that children
625 * are checked only when there is some activity ? */
626 te
= tevent_add_timer(ev_ctx
, pfp
, next_event
,
627 spoolssd_check_children
, pfp
);
629 DEBUG(2, ("Failed to set up children monitoring!\n"));
636 static void spoolssd_check_children(struct tevent_context
*ev_ctx
,
637 struct tevent_timer
*te
,
638 struct timeval current_time
,
641 struct prefork_pool
*pfp
;
645 pfp
= talloc_get_type_abort(pvt
, struct prefork_pool
);
647 if ((spoolss_prefork_status
& SPOOLSS_NEW_MAX
) &&
648 !(spoolss_prefork_status
& SPOLLSS_ENOSPC
)) {
649 ret
= prefork_expand_pool(pfp
, spoolss_max_children
);
651 spoolss_prefork_status
|= SPOLLSS_ENOSPC
;
653 spoolss_prefork_status
&= ~SPOOLSS_NEW_MAX
;
656 active
= prefork_count_active_children(pfp
, &total
);
658 if (total
- active
< spoolss_spawn_rate
) {
659 n
= prefork_add_children(ev_ctx
, pfp
, spoolss_spawn_rate
);
660 if (n
< spoolss_spawn_rate
) {
661 DEBUG(10, ("Tried to start 5 children but only,"
662 "%d were actually started.!\n", n
));
666 if (total
- active
> spoolss_min_children
) {
667 if ((total
- spoolss_min_children
) >= spoolss_spawn_rate
) {
668 prefork_retire_children(pfp
, spoolss_spawn_rate
,
669 time(NULL
) - SPOOLSS_MIN_LIFE
);
673 ret
= spoolssd_schedule_check(ev_ctx
, pfp
, current_time
);
676 static void print_queue_forward(struct messaging_context
*msg
,
679 struct server_id server_id
,
682 messaging_send_buf(msg
, pid_to_procid(background_lpq_updater_pid
),
683 MSG_PRINTER_UPDATE
, data
->data
, data
->length
);
686 pid_t
start_spoolssd(struct tevent_context
*ev_ctx
,
687 struct messaging_context
*msg_ctx
)
689 struct prefork_pool
*pool
;
690 struct rpc_srv_callbacks spoolss_cb
;
691 struct dcerpc_binding_vector
*v
;
699 DEBUG(1, ("Forking SPOOLSS Daemon\n"));
704 DEBUG(0, ("Failed to fork SPOOLSS [%s]\n",
708 /* parent or error */
713 close_low_fds(false);
715 status
= reinit_after_fork(msg_ctx
,
717 procid_self(), true);
718 if (!NT_STATUS_IS_OK(status
)) {
719 DEBUG(0,("reinit_after_fork() failed\n"));
720 smb_panic("reinit_after_fork() failed");
723 spoolss_reopen_logs();
724 spoolss_prefork_config();
726 /* Publish nt printers, this requires a working winreg pipe */
727 pcap_cache_reload(ev_ctx
, msg_ctx
, &reload_printers
);
729 /* always start the backgroundqueue listner in spoolssd */
730 pid
= start_background_queue(ev_ctx
, msg_ctx
);
732 background_lpq_updater_pid
= pid
;
735 /* the listening fd must be created before the children are actually
737 listen_fd
= create_named_pipe_socket(SPOOLSS_PIPE_NAME
);
738 if (listen_fd
== -1) {
742 ret
= listen(listen_fd
, spoolss_max_children
);
744 DEBUG(0, ("Failed to listen on spoolss pipe - %s\n",
750 /* start children before any more initialization is done */
751 ok
= prefork_create_pool(ev_ctx
, ev_ctx
, 1, &listen_fd
,
752 spoolss_min_children
,
753 spoolss_max_children
,
754 &spoolss_children_main
, NULL
,
757 spoolss_setup_sig_term_handler(ev_ctx
);
758 spoolss_setup_sig_hup_handler(ev_ctx
, pool
, msg_ctx
);
760 if (!serverid_register(procid_self(),
761 FLAG_MSG_GENERAL
|FLAG_MSG_SMBD
762 |FLAG_MSG_PRINT_GENERAL
)) {
766 if (!locking_init()) {
770 messaging_register(msg_ctx
, ev_ctx
,
771 MSG_SMB_CONF_UPDATED
, smb_conf_updated
);
772 messaging_register(msg_ctx
, NULL
, MSG_PRINTER_UPDATE
,
773 print_queue_forward
);
775 mem_ctx
= talloc_new(NULL
);
776 if (mem_ctx
== NULL
) {
781 * Initialize spoolss with an init function to convert printers first.
782 * static_init_rpc will try to initialize the spoolss server too but you
783 * can't register it twice.
785 spoolss_cb
.init
= spoolss_init_cb
;
786 spoolss_cb
.shutdown
= spoolss_shutdown_cb
;
787 spoolss_cb
.private_data
= msg_ctx
;
789 status
= rpc_winreg_init(NULL
);
790 if (!NT_STATUS_IS_OK(status
)) {
791 DEBUG(0, ("Failed to register winreg rpc inteface! (%s)\n",
796 status
= rpc_spoolss_init(&spoolss_cb
);
797 if (!NT_STATUS_IS_OK(status
)) {
798 DEBUG(0, ("Failed to register spoolss rpc inteface! (%s)\n",
803 status
= dcerpc_binding_vector_new(mem_ctx
, &v
);
804 if (!NT_STATUS_IS_OK(status
)) {
805 DEBUG(0, ("Failed to create binding vector (%s)\n",
810 status
= dcerpc_binding_vector_add_np_default(&ndr_table_spoolss
, v
);
811 if (!NT_STATUS_IS_OK(status
)) {
812 DEBUG(0, ("Failed to add np to binding vector (%s)\n",
817 status
= rpc_ep_register(ev_ctx
, msg_ctx
, &ndr_table_spoolss
, v
);
818 if (!NT_STATUS_IS_OK(status
)) {
819 DEBUG(0, ("Failed to register spoolss endpoint! (%s)\n",
824 talloc_free(mem_ctx
);
826 ok
= spoolssd_setup_children_monitor(ev_ctx
, pool
);
828 DEBUG(0, ("Failed to setup children monitoring!\n"));
832 DEBUG(1, ("SPOOLSS Daemon Started (%d)\n", getpid()));
835 ret
= tevent_loop_wait(ev_ctx
);
837 /* should not be reached */
838 DEBUG(0,("background_queue: tevent_loop_wait() exited with %d - %s\n",
839 ret
, (ret
== 0) ? "out of events" : strerror(errno
)));