smbd: split printer reload processing
[Samba.git] / source3 / printing / queue_process.c
blobc4c577e5a7c08dcc06fb4152b22d98fd663fcebe
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/smbd.h"
32 #include "rpc_server/rpc_config.h"
33 #include "printing/load.h"
34 #include "rpc_server/spoolss/srv_spoolss_nt.h"
35 #include "auth.h"
36 #include "nt_printing.h"
38 extern pid_t start_spoolssd(struct event_context *ev_ctx,
39 struct messaging_context *msg_ctx);
41 /**
42 * @brief Purge stale printers and reload from pre-populated pcap cache.
44 * This function should normally only be called as a callback on a successful
45 * pcap_cache_reload().
47 * This function can cause DELETION of printers and drivers from our registry,
48 * so calling it on a failed pcap reload may REMOVE permanently all printers
49 * and drivers.
51 * @param[in] ev The event context.
53 * @param[in] msg_ctx The messaging context.
55 static void delete_and_reload_printers_full(struct tevent_context *ev,
56 struct messaging_context *msg_ctx)
58 struct auth_session_info *session_info = NULL;
59 struct spoolss_PrinterInfo2 *pinfo2 = NULL;
60 int n_services;
61 int pnum;
62 int snum;
63 const char *pname;
64 const char *sname;
65 NTSTATUS status;
67 n_services = lp_numservices();
68 pnum = lp_servicenumber(PRINTERS_NAME);
70 status = make_session_info_system(talloc_tos(), &session_info);
71 if (!NT_STATUS_IS_OK(status)) {
72 DEBUG(3, ("reload_printers: "
73 "Could not create system session_info\n"));
74 /* can't remove stale printers before we
75 * are fully initilized */
76 return;
80 * Add default config for printers added to smb.conf file and remove
81 * stale printers
83 for (snum = 0; snum < n_services; snum++) {
84 /* avoid removing PRINTERS_NAME */
85 if (snum == pnum) {
86 continue;
89 /* skip no-printer services */
90 if (!snum_is_shared_printer(snum)) {
91 continue;
94 sname = lp_const_servicename(snum);
95 pname = lp_printername(session_info, snum);
97 /* check printer, but avoid removing non-autoloaded printers */
98 if (lp_autoloaded(snum) && !pcap_printername_ok(pname)) {
99 DEBUG(3, ("removing stale printer %s\n", pname));
101 if (is_printer_published(session_info, session_info,
102 msg_ctx,
103 NULL,
104 lp_servicename(session_info,
105 snum),
106 &pinfo2)) {
107 nt_printer_publish(session_info,
108 session_info,
109 msg_ctx,
110 pinfo2,
111 DSPRINT_UNPUBLISH);
112 TALLOC_FREE(pinfo2);
114 nt_printer_remove(session_info, session_info, msg_ctx,
115 pname);
116 } else {
117 DEBUG(8, ("Adding default registry entry for printer "
118 "[%s], if it doesn't exist.\n", sname));
119 nt_printer_add(session_info, session_info, msg_ctx,
120 sname);
124 /* finally, purge old snums */
125 delete_and_reload_printers(ev, msg_ctx);
127 TALLOC_FREE(session_info);
131 /****************************************************************************
132 Notify smbds of new printcap data
133 **************************************************************************/
134 static void reload_pcap_change_notify(struct tevent_context *ev,
135 struct messaging_context *msg_ctx)
138 * Reload the printers first in the background process so that
139 * newly added printers get default values created in the registry.
141 * This will block the process for some time (~1 sec per printer), but
142 * it doesn't block smbd's servering clients.
144 delete_and_reload_printers_full(ev, msg_ctx);
146 message_send_all(msg_ctx, MSG_PRINTER_PCAP, NULL, 0, NULL);
149 struct printing_queue_housekeeping_state {
150 struct tevent_context *ev;
151 struct messaging_context *msg;
154 static bool print_queue_housekeeping(const struct timeval *now, void *pvt)
156 struct printing_queue_housekeeping_state *state =
157 talloc_get_type_abort(pvt,
158 struct printing_queue_housekeeping_state);
159 time_t printcap_cache_time = (time_t)lp_printcap_cache_time();
160 time_t t = time_mono(NULL);
162 DEBUG(5, ("print queue housekeeping\n"));
164 /* if periodic printcap rescan is enabled,
165 * see if it's time to reload */
166 if ((printcap_cache_time != 0) &&
167 (t >= (last_printer_reload_time + printcap_cache_time))) {
168 DEBUG( 3,( "Printcap cache time expired.\n"));
169 pcap_cache_reload(state->ev, state->msg,
170 &reload_pcap_change_notify);
171 last_printer_reload_time = t;
174 return true;
177 static bool printing_subsystem_queue_tasks(struct tevent_context *ev_ctx,
178 struct messaging_context *msg_ctx)
180 struct printing_queue_housekeeping_state *state;
182 state = talloc_zero(ev_ctx, struct printing_queue_housekeeping_state);
183 if (state == NULL) {
184 DEBUG(0,("Could not talloc printing_queue_housekeeping_state\n"));
185 return false;
187 state->ev = ev_ctx;
188 state->msg = msg_ctx;
190 if (!(event_add_idle(ev_ctx, NULL,
191 timeval_set(SMBD_HOUSEKEEPING_INTERVAL, 0),
192 "print_queue_housekeeping",
193 print_queue_housekeeping,
194 state))) {
195 DEBUG(0,("Could not add print_queue_housekeeping event\n"));
196 return false;
199 return true;
202 static void bq_reopen_logs(char *logfile)
204 if (logfile) {
205 lp_set_logfile(logfile);
207 reopen_logs();
210 static void bq_sig_term_handler(struct tevent_context *ev,
211 struct tevent_signal *se,
212 int signum,
213 int count,
214 void *siginfo,
215 void *private_data)
217 exit_server_cleanly("termination signal");
220 static void bq_setup_sig_term_handler(void)
222 struct tevent_signal *se;
224 se = tevent_add_signal(server_event_context(),
225 server_event_context(),
226 SIGTERM, 0,
227 bq_sig_term_handler,
228 NULL);
229 if (!se) {
230 exit_server("failed to setup SIGTERM handler");
234 static void bq_sig_hup_handler(struct tevent_context *ev,
235 struct tevent_signal *se,
236 int signum,
237 int count,
238 void *siginfo,
239 void *pvt)
241 struct messaging_context *msg_ctx;
243 msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);
244 change_to_root_user();
246 DEBUG(1, ("Reloading pcap cache after SIGHUP\n"));
247 pcap_cache_reload(ev, msg_ctx, &reload_pcap_change_notify);
248 bq_reopen_logs(NULL);
251 static void bq_setup_sig_hup_handler(struct tevent_context *ev,
252 struct messaging_context *msg_ctx)
254 struct tevent_signal *se;
256 se = tevent_add_signal(ev, ev, SIGHUP, 0, bq_sig_hup_handler,
257 msg_ctx);
258 if (!se) {
259 exit_server("failed to setup SIGHUP handler");
263 static void bq_sig_chld_handler(struct tevent_context *ev_ctx,
264 struct tevent_signal *se,
265 int signum, int count,
266 void *siginfo, void *pvt)
268 int status;
269 pid_t pid;
271 pid = sys_waitpid(-1, &status, WNOHANG);
272 if (WIFEXITED(status)) {
273 DEBUG(6, ("Bq child process %d terminated with %d\n",
274 (int)pid, WEXITSTATUS(status)));
275 } else {
276 DEBUG(3, ("Bq child process %d terminated abnormally\n",
277 (int)pid));
281 static void bq_setup_sig_chld_handler(struct tevent_context *ev_ctx)
283 struct tevent_signal *se;
285 se = tevent_add_signal(ev_ctx, ev_ctx, SIGCHLD, 0,
286 bq_sig_chld_handler, NULL);
287 if (!se) {
288 exit_server("failed to setup SIGCHLD handler");
292 static void bq_smb_conf_updated(struct messaging_context *msg_ctx,
293 void *private_data,
294 uint32_t msg_type,
295 struct server_id server_id,
296 DATA_BLOB *data)
298 struct tevent_context *ev_ctx =
299 talloc_get_type_abort(private_data, struct tevent_context);
301 DEBUG(10,("smb_conf_updated: Got message saying smb.conf was "
302 "updated. Reloading.\n"));
303 change_to_root_user();
304 pcap_cache_reload(ev_ctx, msg_ctx, &reload_pcap_change_notify);
307 static void printing_pause_fd_handler(struct tevent_context *ev,
308 struct tevent_fd *fde,
309 uint16_t flags,
310 void *private_data)
313 * If pause_pipe[1] is closed it means the parent smbd
314 * and children exited or aborted.
316 exit_server_cleanly(NULL);
319 /****************************************************************************
320 main thread of the background lpq updater
321 ****************************************************************************/
322 pid_t start_background_queue(struct tevent_context *ev,
323 struct messaging_context *msg_ctx,
324 char *logfile)
326 pid_t pid;
328 /* Use local variables for this as we don't
329 * need to save the parent side of this, just
330 * ensure it closes when the process exits.
332 int pause_pipe[2];
334 DEBUG(3,("start_background_queue: Starting background LPQ thread\n"));
336 if (pipe(pause_pipe) == -1) {
337 DEBUG(5,("start_background_queue: cannot create pipe. %s\n", strerror(errno) ));
338 exit(1);
342 * Block signals before forking child as it will have to
343 * set its own handlers. Child will re-enable SIGHUP as
344 * soon as the handlers are set up.
346 BlockSignals(true, SIGTERM);
347 BlockSignals(true, SIGHUP);
349 pid = fork();
351 /* parent or error */
352 if (pid != 0) {
353 /* Re-enable SIGHUP before returnig */
354 BlockSignals(false, SIGTERM);
355 BlockSignals(false, SIGHUP);
356 return pid;
359 if (pid == -1) {
360 DEBUG(5,("start_background_queue: background LPQ thread failed to start. %s\n", strerror(errno) ));
361 exit(1);
364 if (pid == 0) {
365 struct tevent_fd *fde;
366 int ret;
367 NTSTATUS status;
369 /* Child. */
370 DEBUG(5,("start_background_queue: background LPQ thread started\n"));
372 close(pause_pipe[0]);
373 pause_pipe[0] = -1;
375 status = reinit_after_fork(msg_ctx, ev, true);
377 if (!NT_STATUS_IS_OK(status)) {
378 DEBUG(0,("reinit_after_fork() failed\n"));
379 smb_panic("reinit_after_fork() failed");
382 bq_reopen_logs(logfile);
383 bq_setup_sig_term_handler();
384 bq_setup_sig_hup_handler(ev, msg_ctx);
385 bq_setup_sig_chld_handler(ev);
387 BlockSignals(false, SIGTERM);
388 BlockSignals(false, SIGHUP);
390 if (!printing_subsystem_queue_tasks(ev, msg_ctx)) {
391 exit(1);
394 if (!serverid_register(messaging_server_id(msg_ctx),
395 FLAG_MSG_GENERAL |
396 FLAG_MSG_PRINT_GENERAL)) {
397 exit(1);
400 if (!locking_init()) {
401 exit(1);
403 messaging_register(msg_ctx, ev, MSG_SMB_CONF_UPDATED,
404 bq_smb_conf_updated);
405 messaging_register(msg_ctx, NULL, MSG_PRINTER_UPDATE,
406 print_queue_receive);
407 /* Remove previous forwarder message set in parent. */
408 messaging_deregister(msg_ctx, MSG_PRINTER_DRVUPGRADE, NULL);
410 messaging_register(msg_ctx, NULL, MSG_PRINTER_DRVUPGRADE,
411 do_drv_upgrade_printer);
413 fde = tevent_add_fd(ev, ev, pause_pipe[1], TEVENT_FD_READ,
414 printing_pause_fd_handler,
415 NULL);
416 if (!fde) {
417 DEBUG(0,("tevent_add_fd() failed for pause_pipe\n"));
418 smb_panic("tevent_add_fd() failed for pause_pipe");
421 pcap_cache_reload(ev, msg_ctx, &reload_pcap_change_notify);
423 DEBUG(5,("start_background_queue: background LPQ thread waiting for messages\n"));
424 ret = tevent_loop_wait(ev);
425 /* should not be reached */
426 DEBUG(0,("background_queue: tevent_loop_wait() exited with %d - %s\n",
427 ret, (ret == 0) ? "out of events" : strerror(errno)));
428 exit(1);
431 close(pause_pipe[1]);
433 return pid;
436 /* Run before the parent forks */
437 bool printing_subsystem_init(struct tevent_context *ev_ctx,
438 struct messaging_context *msg_ctx,
439 bool start_daemons,
440 bool background_queue)
442 pid_t pid = -1;
444 if (!print_backend_init(msg_ctx)) {
445 return false;
448 /* start spoolss daemon */
449 /* start as a separate daemon only if enabled */
450 if (start_daemons && rpc_spoolss_daemon() == RPC_DAEMON_FORK) {
452 pid = start_spoolssd(ev_ctx, msg_ctx);
454 } else if (start_daemons && background_queue) {
456 pid = start_background_queue(ev_ctx, msg_ctx, NULL);
458 } else {
459 bool ret;
461 ret = printing_subsystem_queue_tasks(ev_ctx, msg_ctx);
463 /* Publish nt printers, this requires a working winreg pipe */
464 pcap_cache_reload(ev_ctx, msg_ctx,
465 &delete_and_reload_printers_full);
467 return ret;
470 if (pid == -1) {
471 return false;
473 background_lpq_updater_pid = pid;
475 return true;
478 void printing_subsystem_update(struct tevent_context *ev_ctx,
479 struct messaging_context *msg_ctx,
480 bool force)
482 if (background_lpq_updater_pid != -1) {
483 if (pcap_cache_loaded(NULL)) {
484 load_printers(ev_ctx, msg_ctx);
486 if (force) {
487 /* Send a sighup to the background process.
488 * this will force it to reload printers */
489 kill(background_lpq_updater_pid, SIGHUP);
491 return;
494 pcap_cache_reload(ev_ctx, msg_ctx,
495 &delete_and_reload_printers_full);