2 Unix SMB/Netbios implementation.
4 printing backend routines
5 Copyright (C) Andrew Tridgell 1992-2000
6 Copyright (C) Jeremy Allison 2002
7 Copyright (C) Simo Sorce 2011
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "smbd/globals.h"
25 #include "include/messages.h"
26 #include "lib/util/util_process.h"
28 #include "printing/pcap.h"
29 #include "printing/queue_process.h"
31 #include "locking/proto.h"
32 #include "smbd/smbd.h"
33 #include "rpc_server/rpc_config.h"
34 #include "printing/load.h"
36 extern pid_t
start_spoolssd(struct tevent_context
*ev_ctx
,
37 struct messaging_context
*msg_ctx
);
39 /****************************************************************************
40 Notify smbds of new printcap data
41 **************************************************************************/
42 static void reload_pcap_change_notify(struct tevent_context
*ev
,
43 struct messaging_context
*msg_ctx
)
46 * Reload the printers first in the background process so that
47 * newly added printers get default values created in the registry.
49 * This will block the process for some time (~1 sec per printer), but
50 * it doesn't block smbd's servering clients.
52 delete_and_reload_printers(ev
, msg_ctx
);
54 message_send_all(msg_ctx
, MSG_PRINTER_PCAP
, NULL
, 0, NULL
);
57 struct printing_queue_housekeeping_state
{
58 struct tevent_context
*ev
;
59 struct messaging_context
*msg
;
62 static bool print_queue_housekeeping(const struct timeval
*now
, void *pvt
)
64 struct printing_queue_housekeeping_state
*state
=
65 talloc_get_type_abort(pvt
,
66 struct printing_queue_housekeeping_state
);
67 time_t printcap_cache_time
= (time_t)lp_printcap_cache_time();
68 time_t t
= time_mono(NULL
);
70 DEBUG(5, ("print queue housekeeping\n"));
72 /* if periodic printcap rescan is enabled,
73 * see if it's time to reload */
74 if ((printcap_cache_time
!= 0) &&
75 (t
>= (last_printer_reload_time
+ printcap_cache_time
))) {
76 DEBUG( 3,( "Printcap cache time expired.\n"));
77 pcap_cache_reload(state
->ev
, state
->msg
,
78 &reload_pcap_change_notify
);
79 last_printer_reload_time
= t
;
85 static bool printing_subsystem_queue_tasks(struct tevent_context
*ev_ctx
,
86 struct messaging_context
*msg_ctx
)
88 struct printing_queue_housekeeping_state
*state
;
90 state
= talloc_zero(ev_ctx
, struct printing_queue_housekeeping_state
);
92 DEBUG(0,("Could not talloc printing_queue_housekeeping_state\n"));
98 if (!(event_add_idle(ev_ctx
, NULL
,
99 timeval_set(SMBD_HOUSEKEEPING_INTERVAL
, 0),
100 "print_queue_housekeeping",
101 print_queue_housekeeping
,
103 DEBUG(0,("Could not add print_queue_housekeeping event\n"));
110 static void bq_reopen_logs(char *logfile
)
113 lp_set_logfile(logfile
);
118 static void bq_sig_term_handler(struct tevent_context
*ev
,
119 struct tevent_signal
*se
,
125 exit_server_cleanly("termination signal");
128 static void bq_setup_sig_term_handler(void)
130 struct tevent_signal
*se
;
132 se
= tevent_add_signal(server_event_context(),
133 server_event_context(),
138 exit_server("failed to setup SIGTERM handler");
142 static void bq_sig_hup_handler(struct tevent_context
*ev
,
143 struct tevent_signal
*se
,
149 struct messaging_context
*msg_ctx
;
151 msg_ctx
= talloc_get_type_abort(pvt
, struct messaging_context
);
152 change_to_root_user();
154 DEBUG(1, ("Reloading pcap cache after SIGHUP\n"));
155 pcap_cache_reload(ev
, msg_ctx
, &reload_pcap_change_notify
);
156 bq_reopen_logs(NULL
);
159 static void bq_setup_sig_hup_handler(struct tevent_context
*ev
,
160 struct messaging_context
*msg_ctx
)
162 struct tevent_signal
*se
;
164 se
= tevent_add_signal(ev
, ev
, SIGHUP
, 0, bq_sig_hup_handler
,
167 exit_server("failed to setup SIGHUP handler");
171 static void bq_sig_chld_handler(struct tevent_context
*ev_ctx
,
172 struct tevent_signal
*se
,
173 int signum
, int count
,
174 void *siginfo
, void *pvt
)
179 pid
= sys_waitpid(-1, &status
, WNOHANG
);
180 if (WIFEXITED(status
)) {
181 DEBUG(6, ("Bq child process %d terminated with %d\n",
182 (int)pid
, WEXITSTATUS(status
)));
184 DEBUG(3, ("Bq child process %d terminated abnormally\n",
189 static void bq_setup_sig_chld_handler(struct tevent_context
*ev_ctx
)
191 struct tevent_signal
*se
;
193 se
= tevent_add_signal(ev_ctx
, ev_ctx
, SIGCHLD
, 0,
194 bq_sig_chld_handler
, NULL
);
196 exit_server("failed to setup SIGCHLD handler");
200 static void bq_smb_conf_updated(struct messaging_context
*msg_ctx
,
203 struct server_id server_id
,
206 struct tevent_context
*ev_ctx
=
207 talloc_get_type_abort(private_data
, struct tevent_context
);
209 DEBUG(10,("smb_conf_updated: Got message saying smb.conf was "
210 "updated. Reloading.\n"));
211 change_to_root_user();
212 pcap_cache_reload(ev_ctx
, msg_ctx
, &reload_pcap_change_notify
);
215 static void printing_pause_fd_handler(struct tevent_context
*ev
,
216 struct tevent_fd
*fde
,
221 * If pause_pipe[1] is closed it means the parent smbd
222 * and children exited or aborted.
224 exit_server_cleanly(NULL
);
227 /****************************************************************************
228 main thread of the background lpq updater
229 ****************************************************************************/
230 pid_t
start_background_queue(struct tevent_context
*ev
,
231 struct messaging_context
*msg_ctx
,
236 /* Use local variables for this as we don't
237 * need to save the parent side of this, just
238 * ensure it closes when the process exits.
242 DEBUG(3,("start_background_queue: Starting background LPQ thread\n"));
244 if (pipe(pause_pipe
) == -1) {
245 DEBUG(5,("start_background_queue: cannot create pipe. %s\n", strerror(errno
) ));
250 * Block signals before forking child as it will have to
251 * set its own handlers. Child will re-enable SIGHUP as
252 * soon as the handlers are set up.
254 BlockSignals(true, SIGTERM
);
255 BlockSignals(true, SIGHUP
);
259 /* parent or error */
261 /* Re-enable SIGHUP before returnig */
262 BlockSignals(false, SIGTERM
);
263 BlockSignals(false, SIGHUP
);
268 DEBUG(5,("start_background_queue: background LPQ thread failed to start. %s\n", strerror(errno
) ));
273 struct tevent_fd
*fde
;
278 DEBUG(5,("start_background_queue: background LPQ thread started\n"));
280 close(pause_pipe
[0]);
283 status
= reinit_after_fork(msg_ctx
, ev
, true);
285 if (!NT_STATUS_IS_OK(status
)) {
286 DEBUG(0,("reinit_after_fork() failed\n"));
287 smb_panic("reinit_after_fork() failed");
290 prctl_set_comment("lpqd");
292 bq_reopen_logs(logfile
);
293 bq_setup_sig_term_handler();
294 bq_setup_sig_hup_handler(ev
, msg_ctx
);
295 bq_setup_sig_chld_handler(ev
);
297 BlockSignals(false, SIGTERM
);
298 BlockSignals(false, SIGHUP
);
300 if (!printing_subsystem_queue_tasks(ev
, msg_ctx
)) {
304 if (!serverid_register(messaging_server_id(msg_ctx
),
306 FLAG_MSG_PRINT_GENERAL
)) {
310 if (!locking_init()) {
313 messaging_register(msg_ctx
, ev
, MSG_SMB_CONF_UPDATED
,
314 bq_smb_conf_updated
);
315 messaging_register(msg_ctx
, NULL
, MSG_PRINTER_UPDATE
,
316 print_queue_receive
);
318 fde
= tevent_add_fd(ev
, ev
, pause_pipe
[1], TEVENT_FD_READ
,
319 printing_pause_fd_handler
,
322 DEBUG(0,("tevent_add_fd() failed for pause_pipe\n"));
323 smb_panic("tevent_add_fd() failed for pause_pipe");
326 pcap_cache_reload(ev
, msg_ctx
, &reload_pcap_change_notify
);
328 DEBUG(5,("start_background_queue: background LPQ thread waiting for messages\n"));
329 ret
= tevent_loop_wait(ev
);
330 /* should not be reached */
331 DEBUG(0,("background_queue: tevent_loop_wait() exited with %d - %s\n",
332 ret
, (ret
== 0) ? "out of events" : strerror(errno
)));
336 close(pause_pipe
[1]);
341 /* Run before the parent forks */
342 bool printing_subsystem_init(struct tevent_context
*ev_ctx
,
343 struct messaging_context
*msg_ctx
,
345 bool background_queue
)
349 if (!print_backend_init(msg_ctx
)) {
353 /* start spoolss daemon */
354 /* start as a separate daemon only if enabled */
355 if (start_daemons
&& rpc_spoolss_daemon() == RPC_DAEMON_FORK
) {
357 pid
= start_spoolssd(ev_ctx
, msg_ctx
);
359 } else if (start_daemons
&& background_queue
) {
361 pid
= start_background_queue(ev_ctx
, msg_ctx
, NULL
);
366 ret
= printing_subsystem_queue_tasks(ev_ctx
, msg_ctx
);
368 /* Publish nt printers, this requires a working winreg pipe */
369 pcap_cache_reload(ev_ctx
, msg_ctx
, &delete_and_reload_printers
);
377 background_lpq_updater_pid
= pid
;
382 void printing_subsystem_update(struct tevent_context
*ev_ctx
,
383 struct messaging_context
*msg_ctx
,
386 if (background_lpq_updater_pid
!= -1) {
387 if (pcap_cache_loaded()) {
388 load_printers(ev_ctx
, msg_ctx
);
391 /* Send a sighup to the background process.
392 * this will force it to reload printers */
393 kill(background_lpq_updater_pid
, SIGHUP
);
398 pcap_cache_reload(ev_ctx
, msg_ctx
, &delete_and_reload_printers
);