s4 dns: Implement RFC-compatible update prescan
[Samba/gebeck_regimport.git] / source3 / printing / queue_process.c
blobce4047cf1697d6f12ef1f6a61c2b0632e5f76311
1 /*
2 Unix SMB/Netbios implementation.
3 Version 3.0
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/>.
23 #include "includes.h"
24 #include "smbd/globals.h"
25 #include "include/messages.h"
26 #include "printing.h"
27 #include "printing/pcap.h"
28 #include "printing/queue_process.h"
29 #include "serverid.h"
30 #include "locking/proto.h"
31 #include "smbd/proto.h"
32 #include "rpc_server/rpc_config.h"
33 #include "printing/load.h"
35 extern pid_t start_spoolssd(struct event_context *ev_ctx,
36 struct messaging_context *msg_ctx);
38 /****************************************************************************
39 Notify smbds of new printcap data
40 **************************************************************************/
41 static void reload_pcap_change_notify(struct tevent_context *ev,
42 struct messaging_context *msg_ctx)
44 message_send_all(msg_ctx, MSG_PRINTER_PCAP, NULL, 0, NULL);
47 static bool print_queue_housekeeping(const struct timeval *now, void *pvt)
49 struct messaging_context *msg_ctx =
50 talloc_get_type_abort(pvt, struct messaging_context);
51 time_t printcap_cache_time = (time_t)lp_printcap_cache_time();
52 time_t t = time_mono(NULL);
54 DEBUG(5, ("print queue housekeeping\n"));
56 /* if periodic printcap rescan is enabled,
57 * see if it's time to reload */
58 if ((printcap_cache_time != 0) &&
59 (t >= (last_printer_reload_time + printcap_cache_time))) {
60 DEBUG( 3,( "Printcap cache time expired.\n"));
61 pcap_cache_reload(messaging_event_context(msg_ctx),
62 msg_ctx,
63 &reload_pcap_change_notify);
64 last_printer_reload_time = t;
67 return true;
70 static bool printing_subsystem_queue_tasks(struct tevent_context *ev_ctx,
71 struct messaging_context *msg_ctx)
73 if (!(event_add_idle(ev_ctx, NULL,
74 timeval_set(SMBD_HOUSEKEEPING_INTERVAL, 0),
75 "print_queue_housekeeping",
76 print_queue_housekeeping,
77 msg_ctx))) {
78 DEBUG(0, ("Could not add print_queue_housekeeping event\n"));
79 return false;
82 return true;
85 static void bq_reopen_logs(char *logfile)
87 if (logfile) {
88 lp_set_logfile(logfile);
90 reopen_logs();
93 static void bq_sig_term_handler(struct tevent_context *ev,
94 struct tevent_signal *se,
95 int signum,
96 int count,
97 void *siginfo,
98 void *private_data)
100 exit_server_cleanly("termination signal");
103 static void bq_setup_sig_term_handler(void)
105 struct tevent_signal *se;
107 se = tevent_add_signal(server_event_context(),
108 server_event_context(),
109 SIGTERM, 0,
110 bq_sig_term_handler,
111 NULL);
112 if (!se) {
113 exit_server("failed to setup SIGTERM handler");
117 static void bq_sig_hup_handler(struct tevent_context *ev,
118 struct tevent_signal *se,
119 int signum,
120 int count,
121 void *siginfo,
122 void *pvt)
124 struct messaging_context *msg_ctx;
126 msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);
127 change_to_root_user();
129 DEBUG(1, ("Reloading pcap cache after SIGHUP\n"));
130 pcap_cache_reload(ev, msg_ctx, &reload_pcap_change_notify);
131 bq_reopen_logs(NULL);
134 static void bq_setup_sig_hup_handler(struct tevent_context *ev,
135 struct messaging_context *msg_ctx)
137 struct tevent_signal *se;
139 se = tevent_add_signal(ev, ev, SIGHUP, 0, bq_sig_hup_handler,
140 msg_ctx);
141 if (!se) {
142 exit_server("failed to setup SIGHUP handler");
146 static void bq_sig_chld_handler(struct tevent_context *ev_ctx,
147 struct tevent_signal *se,
148 int signum, int count,
149 void *siginfo, void *pvt)
151 int status;
152 pid_t pid;
154 pid = sys_waitpid(-1, &status, WNOHANG);
155 if (WIFEXITED(status)) {
156 DEBUG(6, ("Bq child process %d terminated with %d\n",
157 (int)pid, WEXITSTATUS(status)));
158 } else {
159 DEBUG(3, ("Bq child process %d terminated abnormally\n",
160 (int)pid));
164 static void bq_setup_sig_chld_handler(struct tevent_context *ev_ctx)
166 struct tevent_signal *se;
168 se = tevent_add_signal(ev_ctx, ev_ctx, SIGCHLD, 0,
169 bq_sig_chld_handler, NULL);
170 if (!se) {
171 exit_server("failed to setup SIGCHLD handler");
175 static void bq_smb_conf_updated(struct messaging_context *msg_ctx,
176 void *private_data,
177 uint32_t msg_type,
178 struct server_id server_id,
179 DATA_BLOB *data)
181 struct tevent_context *ev_ctx =
182 talloc_get_type_abort(private_data, struct tevent_context);
184 DEBUG(10,("smb_conf_updated: Got message saying smb.conf was "
185 "updated. Reloading.\n"));
186 change_to_root_user();
187 pcap_cache_reload(ev_ctx, msg_ctx, &reload_pcap_change_notify);
190 static void printing_pause_fd_handler(struct tevent_context *ev,
191 struct tevent_fd *fde,
192 uint16_t flags,
193 void *private_data)
196 * If pause_pipe[1] is closed it means the parent smbd
197 * and children exited or aborted.
199 exit_server_cleanly(NULL);
202 /****************************************************************************
203 main thread of the background lpq updater
204 ****************************************************************************/
205 pid_t start_background_queue(struct tevent_context *ev,
206 struct messaging_context *msg_ctx,
207 char *logfile)
209 pid_t pid;
211 /* Use local variables for this as we don't
212 * need to save the parent side of this, just
213 * ensure it closes when the process exits.
215 int pause_pipe[2];
217 DEBUG(3,("start_background_queue: Starting background LPQ thread\n"));
219 if (pipe(pause_pipe) == -1) {
220 DEBUG(5,("start_background_queue: cannot create pipe. %s\n", strerror(errno) ));
221 exit(1);
225 * Block signals before forking child as it will have to
226 * set its own handlers. Child will re-enable SIGHUP as
227 * soon as the handlers are set up.
229 BlockSignals(true, SIGTERM);
230 BlockSignals(true, SIGHUP);
232 pid = sys_fork();
234 /* parent or error */
235 if (pid != 0) {
236 /* Re-enable SIGHUP before returnig */
237 BlockSignals(false, SIGTERM);
238 BlockSignals(false, SIGHUP);
239 return pid;
242 if (pid == -1) {
243 DEBUG(5,("start_background_queue: background LPQ thread failed to start. %s\n", strerror(errno) ));
244 exit(1);
247 if (pid == 0) {
248 struct tevent_fd *fde;
249 int ret;
250 NTSTATUS status;
252 /* Child. */
253 DEBUG(5,("start_background_queue: background LPQ thread started\n"));
255 close(pause_pipe[0]);
256 pause_pipe[0] = -1;
258 status = reinit_after_fork(msg_ctx, ev, true);
260 if (!NT_STATUS_IS_OK(status)) {
261 DEBUG(0,("reinit_after_fork() failed\n"));
262 smb_panic("reinit_after_fork() failed");
265 bq_reopen_logs(logfile);
266 bq_setup_sig_term_handler();
267 bq_setup_sig_hup_handler(ev, msg_ctx);
268 bq_setup_sig_chld_handler(ev);
270 BlockSignals(false, SIGTERM);
271 BlockSignals(false, SIGHUP);
273 if (!printing_subsystem_queue_tasks(ev, msg_ctx)) {
274 exit(1);
277 if (!serverid_register(messaging_server_id(msg_ctx),
278 FLAG_MSG_GENERAL |
279 FLAG_MSG_PRINT_GENERAL)) {
280 exit(1);
283 if (!locking_init()) {
284 exit(1);
286 messaging_register(msg_ctx, ev, MSG_SMB_CONF_UPDATED,
287 bq_smb_conf_updated);
288 messaging_register(msg_ctx, NULL, MSG_PRINTER_UPDATE,
289 print_queue_receive);
291 fde = tevent_add_fd(ev, ev, pause_pipe[1], TEVENT_FD_READ,
292 printing_pause_fd_handler,
293 NULL);
294 if (!fde) {
295 DEBUG(0,("tevent_add_fd() failed for pause_pipe\n"));
296 smb_panic("tevent_add_fd() failed for pause_pipe");
299 pcap_cache_reload(ev, msg_ctx, &reload_pcap_change_notify);
301 DEBUG(5,("start_background_queue: background LPQ thread waiting for messages\n"));
302 ret = tevent_loop_wait(ev);
303 /* should not be reached */
304 DEBUG(0,("background_queue: tevent_loop_wait() exited with %d - %s\n",
305 ret, (ret == 0) ? "out of events" : strerror(errno)));
306 exit(1);
309 close(pause_pipe[1]);
311 return pid;
314 /* Run before the parent forks */
315 bool printing_subsystem_init(struct tevent_context *ev_ctx,
316 struct messaging_context *msg_ctx,
317 bool start_daemons,
318 bool background_queue)
320 pid_t pid = -1;
322 if (!print_backend_init(msg_ctx)) {
323 return false;
326 /* start spoolss daemon */
327 /* start as a separate daemon only if enabled */
328 if (start_daemons && rpc_spoolss_daemon() == RPC_DAEMON_FORK) {
330 pid = start_spoolssd(ev_ctx, msg_ctx);
332 } else if (start_daemons && background_queue) {
334 pid = start_background_queue(ev_ctx, msg_ctx, NULL);
336 } else {
337 bool ret;
339 ret = printing_subsystem_queue_tasks(ev_ctx, msg_ctx);
341 /* Publish nt printers, this requires a working winreg pipe */
342 pcap_cache_reload(ev_ctx, msg_ctx, &delete_and_reload_printers);
344 return ret;
347 if (pid == -1) {
348 return false;
350 background_lpq_updater_pid = pid;
352 return true;
355 void printing_subsystem_update(struct tevent_context *ev_ctx,
356 struct messaging_context *msg_ctx,
357 bool force)
359 if (background_lpq_updater_pid != -1) {
360 if (pcap_cache_loaded()) {
361 load_printers(ev_ctx, msg_ctx);
363 if (force) {
364 /* Send a sighup to the background process.
365 * this will force it to reload printers */
366 kill(background_lpq_updater_pid, SIGHUP);
368 return;
371 pcap_cache_reload(ev_ctx, msg_ctx, &delete_and_reload_printers);