libsmb: Make cli_ntcreate cancellable
[Samba.git] / source3 / printing / queue_process.c
blobaa0d0fb6a894ada96be70a5f52547f85dec3e751
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"
35 #include "rpc_server/spoolss/srv_spoolss_nt.h"
37 extern pid_t start_spoolssd(struct tevent_context *ev_ctx,
38 struct messaging_context *msg_ctx);
40 /****************************************************************************
41 Notify smbds of new printcap data
42 **************************************************************************/
43 static void reload_pcap_change_notify(struct tevent_context *ev,
44 struct messaging_context *msg_ctx)
47 * Reload the printers first in the background process so that
48 * newly added printers get default values created in the registry.
50 * This will block the process for some time (~1 sec per printer), but
51 * it doesn't block smbd's servering clients.
53 delete_and_reload_printers(ev, msg_ctx);
55 message_send_all(msg_ctx, MSG_PRINTER_PCAP, NULL, 0, NULL);
58 struct printing_queue_housekeeping_state {
59 struct tevent_context *ev;
60 struct messaging_context *msg;
63 static bool print_queue_housekeeping(const struct timeval *now, void *pvt)
65 struct printing_queue_housekeeping_state *state =
66 talloc_get_type_abort(pvt,
67 struct printing_queue_housekeeping_state);
68 time_t printcap_cache_time = (time_t)lp_printcap_cache_time();
69 time_t t = time_mono(NULL);
71 DEBUG(5, ("print queue housekeeping\n"));
73 /* if periodic printcap rescan is enabled,
74 * see if it's time to reload */
75 if ((printcap_cache_time != 0) &&
76 (t >= (last_printer_reload_time + printcap_cache_time))) {
77 DEBUG( 3,( "Printcap cache time expired.\n"));
78 pcap_cache_reload(state->ev, state->msg,
79 &reload_pcap_change_notify);
80 last_printer_reload_time = t;
83 return true;
86 static bool printing_subsystem_queue_tasks(struct tevent_context *ev_ctx,
87 struct messaging_context *msg_ctx)
89 struct printing_queue_housekeeping_state *state;
91 state = talloc_zero(ev_ctx, struct printing_queue_housekeeping_state);
92 if (state == NULL) {
93 DEBUG(0,("Could not talloc printing_queue_housekeeping_state\n"));
94 return false;
96 state->ev = ev_ctx;
97 state->msg = msg_ctx;
99 if (!(event_add_idle(ev_ctx, NULL,
100 timeval_set(SMBD_HOUSEKEEPING_INTERVAL, 0),
101 "print_queue_housekeeping",
102 print_queue_housekeeping,
103 state))) {
104 DEBUG(0,("Could not add print_queue_housekeeping event\n"));
105 return false;
108 return true;
111 static void bq_reopen_logs(char *logfile)
113 if (logfile) {
114 lp_set_logfile(logfile);
116 reopen_logs();
119 static void bq_sig_term_handler(struct tevent_context *ev,
120 struct tevent_signal *se,
121 int signum,
122 int count,
123 void *siginfo,
124 void *private_data)
126 exit_server_cleanly("termination signal");
129 static void bq_setup_sig_term_handler(void)
131 struct tevent_signal *se;
133 se = tevent_add_signal(server_event_context(),
134 server_event_context(),
135 SIGTERM, 0,
136 bq_sig_term_handler,
137 NULL);
138 if (!se) {
139 exit_server("failed to setup SIGTERM handler");
143 static void bq_sig_hup_handler(struct tevent_context *ev,
144 struct tevent_signal *se,
145 int signum,
146 int count,
147 void *siginfo,
148 void *pvt)
150 struct messaging_context *msg_ctx;
152 msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);
153 change_to_root_user();
155 DEBUG(1, ("Reloading pcap cache after SIGHUP\n"));
156 pcap_cache_reload(ev, msg_ctx, &reload_pcap_change_notify);
157 bq_reopen_logs(NULL);
160 static void bq_setup_sig_hup_handler(struct tevent_context *ev,
161 struct messaging_context *msg_ctx)
163 struct tevent_signal *se;
165 se = tevent_add_signal(ev, ev, SIGHUP, 0, bq_sig_hup_handler,
166 msg_ctx);
167 if (!se) {
168 exit_server("failed to setup SIGHUP handler");
172 static void bq_sig_chld_handler(struct tevent_context *ev_ctx,
173 struct tevent_signal *se,
174 int signum, int count,
175 void *siginfo, void *pvt)
177 int status;
178 pid_t pid;
180 pid = sys_waitpid(-1, &status, WNOHANG);
181 if (WIFEXITED(status)) {
182 DEBUG(6, ("Bq child process %d terminated with %d\n",
183 (int)pid, WEXITSTATUS(status)));
184 } else {
185 DEBUG(3, ("Bq child process %d terminated abnormally\n",
186 (int)pid));
190 static void bq_setup_sig_chld_handler(struct tevent_context *ev_ctx)
192 struct tevent_signal *se;
194 se = tevent_add_signal(ev_ctx, ev_ctx, SIGCHLD, 0,
195 bq_sig_chld_handler, NULL);
196 if (!se) {
197 exit_server("failed to setup SIGCHLD handler");
201 static void bq_smb_conf_updated(struct messaging_context *msg_ctx,
202 void *private_data,
203 uint32_t msg_type,
204 struct server_id server_id,
205 DATA_BLOB *data)
207 struct tevent_context *ev_ctx =
208 talloc_get_type_abort(private_data, struct tevent_context);
210 DEBUG(10,("smb_conf_updated: Got message saying smb.conf was "
211 "updated. Reloading.\n"));
212 change_to_root_user();
213 pcap_cache_reload(ev_ctx, msg_ctx, &reload_pcap_change_notify);
216 static void printing_pause_fd_handler(struct tevent_context *ev,
217 struct tevent_fd *fde,
218 uint16_t flags,
219 void *private_data)
222 * If pause_pipe[1] is closed it means the parent smbd
223 * and children exited or aborted.
225 exit_server_cleanly(NULL);
228 /****************************************************************************
229 main thread of the background lpq updater
230 ****************************************************************************/
231 pid_t start_background_queue(struct tevent_context *ev,
232 struct messaging_context *msg_ctx,
233 char *logfile)
235 pid_t pid;
237 /* Use local variables for this as we don't
238 * need to save the parent side of this, just
239 * ensure it closes when the process exits.
241 int pause_pipe[2];
243 DEBUG(3,("start_background_queue: Starting background LPQ thread\n"));
245 if (pipe(pause_pipe) == -1) {
246 DEBUG(5,("start_background_queue: cannot create pipe. %s\n", strerror(errno) ));
247 exit(1);
251 * Block signals before forking child as it will have to
252 * set its own handlers. Child will re-enable SIGHUP as
253 * soon as the handlers are set up.
255 BlockSignals(true, SIGTERM);
256 BlockSignals(true, SIGHUP);
258 pid = fork();
260 /* parent or error */
261 if (pid != 0) {
262 /* Re-enable SIGHUP before returnig */
263 BlockSignals(false, SIGTERM);
264 BlockSignals(false, SIGHUP);
265 return pid;
268 if (pid == -1) {
269 DEBUG(5,("start_background_queue: background LPQ thread failed to start. %s\n", strerror(errno) ));
270 exit(1);
273 if (pid == 0) {
274 struct tevent_fd *fde;
275 int ret;
276 NTSTATUS status;
278 /* Child. */
279 DEBUG(5,("start_background_queue: background LPQ thread started\n"));
281 close(pause_pipe[0]);
282 pause_pipe[0] = -1;
284 status = reinit_after_fork(msg_ctx, ev, true);
286 if (!NT_STATUS_IS_OK(status)) {
287 DEBUG(0,("reinit_after_fork() failed\n"));
288 smb_panic("reinit_after_fork() failed");
291 prctl_set_comment("lpqd");
293 bq_reopen_logs(logfile);
294 bq_setup_sig_term_handler();
295 bq_setup_sig_hup_handler(ev, msg_ctx);
296 bq_setup_sig_chld_handler(ev);
298 BlockSignals(false, SIGTERM);
299 BlockSignals(false, SIGHUP);
301 if (!printing_subsystem_queue_tasks(ev, msg_ctx)) {
302 exit(1);
305 if (!serverid_register(messaging_server_id(msg_ctx),
306 FLAG_MSG_GENERAL |
307 FLAG_MSG_PRINT_GENERAL)) {
308 exit(1);
311 if (!locking_init()) {
312 exit(1);
314 messaging_register(msg_ctx, ev, MSG_SMB_CONF_UPDATED,
315 bq_smb_conf_updated);
316 messaging_register(msg_ctx, NULL, MSG_PRINTER_UPDATE,
317 print_queue_receive);
318 /* Remove previous forwarder message set in parent. */
319 messaging_deregister(msg_ctx, MSG_PRINTER_DRVUPGRADE, NULL);
321 messaging_register(msg_ctx, NULL, MSG_PRINTER_DRVUPGRADE,
322 do_drv_upgrade_printer);
324 fde = tevent_add_fd(ev, ev, pause_pipe[1], TEVENT_FD_READ,
325 printing_pause_fd_handler,
326 NULL);
327 if (!fde) {
328 DEBUG(0,("tevent_add_fd() failed for pause_pipe\n"));
329 smb_panic("tevent_add_fd() failed for pause_pipe");
332 pcap_cache_reload(ev, msg_ctx, &reload_pcap_change_notify);
334 DEBUG(5,("start_background_queue: background LPQ thread waiting for messages\n"));
335 ret = tevent_loop_wait(ev);
336 /* should not be reached */
337 DEBUG(0,("background_queue: tevent_loop_wait() exited with %d - %s\n",
338 ret, (ret == 0) ? "out of events" : strerror(errno)));
339 exit(1);
342 close(pause_pipe[1]);
344 return pid;
347 /* Run before the parent forks */
348 bool printing_subsystem_init(struct tevent_context *ev_ctx,
349 struct messaging_context *msg_ctx,
350 bool start_daemons,
351 bool background_queue)
353 pid_t pid = -1;
355 if (!print_backend_init(msg_ctx)) {
356 return false;
359 /* start spoolss daemon */
360 /* start as a separate daemon only if enabled */
361 if (start_daemons && rpc_spoolss_daemon() == RPC_DAEMON_FORK) {
363 pid = start_spoolssd(ev_ctx, msg_ctx);
365 } else if (start_daemons && background_queue) {
367 pid = start_background_queue(ev_ctx, msg_ctx, NULL);
369 } else {
370 bool ret;
372 ret = printing_subsystem_queue_tasks(ev_ctx, msg_ctx);
374 /* Publish nt printers, this requires a working winreg pipe */
375 pcap_cache_reload(ev_ctx, msg_ctx, &delete_and_reload_printers);
377 return ret;
380 if (pid == -1) {
381 return false;
383 background_lpq_updater_pid = pid;
385 return true;
388 void printing_subsystem_update(struct tevent_context *ev_ctx,
389 struct messaging_context *msg_ctx,
390 bool force)
392 if (background_lpq_updater_pid != -1) {
393 if (pcap_cache_loaded()) {
394 load_printers(ev_ctx, msg_ctx);
396 if (force) {
397 /* Send a sighup to the background process.
398 * this will force it to reload printers */
399 kill(background_lpq_updater_pid, SIGHUP);
401 return;
404 pcap_cache_reload(ev_ctx, msg_ctx, &delete_and_reload_printers);