schannel_store.tdb: make it schannel_store.ntdb if 'use ntdb'.
[Samba/id10ts.git] / source3 / printing / queue_process.c
blobbc6c33f9a8fcddc0cdd8ec912c6c75ee1f798f32
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 "lib/util/util_process.h"
27 #include "printing.h"
28 #include "printing/pcap.h"
29 #include "printing/queue_process.h"
30 #include "serverid.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;
82 return true;
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);
91 if (state == NULL) {
92 DEBUG(0,("Could not talloc printing_queue_housekeeping_state\n"));
93 return false;
95 state->ev = ev_ctx;
96 state->msg = msg_ctx;
98 if (!(event_add_idle(ev_ctx, NULL,
99 timeval_set(SMBD_HOUSEKEEPING_INTERVAL, 0),
100 "print_queue_housekeeping",
101 print_queue_housekeeping,
102 state))) {
103 DEBUG(0,("Could not add print_queue_housekeeping event\n"));
104 return false;
107 return true;
110 static void bq_reopen_logs(char *logfile)
112 if (logfile) {
113 lp_set_logfile(logfile);
115 reopen_logs();
118 static void bq_sig_term_handler(struct tevent_context *ev,
119 struct tevent_signal *se,
120 int signum,
121 int count,
122 void *siginfo,
123 void *private_data)
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(),
134 SIGTERM, 0,
135 bq_sig_term_handler,
136 NULL);
137 if (!se) {
138 exit_server("failed to setup SIGTERM handler");
142 static void bq_sig_hup_handler(struct tevent_context *ev,
143 struct tevent_signal *se,
144 int signum,
145 int count,
146 void *siginfo,
147 void *pvt)
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,
165 msg_ctx);
166 if (!se) {
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)
176 int status;
177 pid_t pid;
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)));
183 } else {
184 DEBUG(3, ("Bq child process %d terminated abnormally\n",
185 (int)pid));
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);
195 if (!se) {
196 exit_server("failed to setup SIGCHLD handler");
200 static void bq_smb_conf_updated(struct messaging_context *msg_ctx,
201 void *private_data,
202 uint32_t msg_type,
203 struct server_id server_id,
204 DATA_BLOB *data)
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,
217 uint16_t flags,
218 void *private_data)
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,
232 char *logfile)
234 pid_t pid;
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.
240 int pause_pipe[2];
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) ));
246 exit(1);
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);
257 pid = fork();
259 /* parent or error */
260 if (pid != 0) {
261 /* Re-enable SIGHUP before returnig */
262 BlockSignals(false, SIGTERM);
263 BlockSignals(false, SIGHUP);
264 return pid;
267 if (pid == -1) {
268 DEBUG(5,("start_background_queue: background LPQ thread failed to start. %s\n", strerror(errno) ));
269 exit(1);
272 if (pid == 0) {
273 struct tevent_fd *fde;
274 int ret;
275 NTSTATUS status;
277 /* Child. */
278 DEBUG(5,("start_background_queue: background LPQ thread started\n"));
280 close(pause_pipe[0]);
281 pause_pipe[0] = -1;
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)) {
301 exit(1);
304 if (!serverid_register(messaging_server_id(msg_ctx),
305 FLAG_MSG_GENERAL |
306 FLAG_MSG_PRINT_GENERAL)) {
307 exit(1);
310 if (!locking_init()) {
311 exit(1);
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,
320 NULL);
321 if (!fde) {
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)));
333 exit(1);
336 close(pause_pipe[1]);
338 return pid;
341 /* Run before the parent forks */
342 bool printing_subsystem_init(struct tevent_context *ev_ctx,
343 struct messaging_context *msg_ctx,
344 bool start_daemons,
345 bool background_queue)
347 pid_t pid = -1;
349 if (!print_backend_init(msg_ctx)) {
350 return false;
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);
363 } else {
364 bool ret;
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);
371 return ret;
374 if (pid == -1) {
375 return false;
377 background_lpq_updater_pid = pid;
379 return true;
382 void printing_subsystem_update(struct tevent_context *ev_ctx,
383 struct messaging_context *msg_ctx,
384 bool force)
386 if (background_lpq_updater_pid != -1) {
387 if (pcap_cache_loaded()) {
388 load_printers(ev_ctx, msg_ctx);
390 if (force) {
391 /* Send a sighup to the background process.
392 * this will force it to reload printers */
393 kill(background_lpq_updater_pid, SIGHUP);
395 return;
398 pcap_cache_reload(ev_ctx, msg_ctx, &delete_and_reload_printers);