ldb_tdb: Use braces in ltdb_dn_list_find_val()
[Samba.git] / source3 / printing / queue_process.c
blobe07aca036fad52740f72b13f7e35b77630b82731
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"
36 #include "auth.h"
37 #include "nt_printing.h"
39 extern pid_t start_spoolssd(struct tevent_context *ev_ctx,
40 struct messaging_context *msg_ctx);
42 /**
43 * @brief Purge stale printers and reload from pre-populated pcap cache.
45 * This function should normally only be called as a callback on a successful
46 * pcap_cache_reload().
48 * This function can cause DELETION of printers and drivers from our registry,
49 * so calling it on a failed pcap reload may REMOVE permanently all printers
50 * and drivers.
52 * @param[in] ev The event context.
54 * @param[in] msg_ctx The messaging context.
56 static void delete_and_reload_printers_full(struct tevent_context *ev,
57 struct messaging_context *msg_ctx)
59 struct auth_session_info *session_info = NULL;
60 struct spoolss_PrinterInfo2 *pinfo2 = NULL;
61 int n_services;
62 int pnum;
63 int snum;
64 const char *pname;
65 const char *sname;
66 NTSTATUS status;
68 n_services = lp_numservices();
69 pnum = lp_servicenumber(PRINTERS_NAME);
71 status = make_session_info_system(talloc_tos(), &session_info);
72 if (!NT_STATUS_IS_OK(status)) {
73 DEBUG(3, ("reload_printers: "
74 "Could not create system session_info\n"));
75 /* can't remove stale printers before we
76 * are fully initilized */
77 return;
81 * Add default config for printers added to smb.conf file and remove
82 * stale printers
84 for (snum = 0; snum < n_services; snum++) {
85 /* avoid removing PRINTERS_NAME */
86 if (snum == pnum) {
87 continue;
90 /* skip no-printer services */
91 if (!snum_is_shared_printer(snum)) {
92 continue;
95 sname = lp_const_servicename(snum);
96 pname = lp_printername(session_info, snum);
98 /* check printer, but avoid removing non-autoloaded printers */
99 if (lp_autoloaded(snum) && !pcap_printername_ok(pname)) {
100 DEBUG(3, ("removing stale printer %s\n", pname));
102 if (is_printer_published(session_info, session_info,
103 msg_ctx,
104 NULL,
105 lp_servicename(session_info,
106 snum),
107 &pinfo2)) {
108 nt_printer_publish(session_info,
109 session_info,
110 msg_ctx,
111 pinfo2,
112 DSPRINT_UNPUBLISH);
113 TALLOC_FREE(pinfo2);
115 nt_printer_remove(session_info, session_info, msg_ctx,
116 pname);
117 } else {
118 DEBUG(8, ("Adding default registry entry for printer "
119 "[%s], if it doesn't exist.\n", sname));
120 nt_printer_add(session_info, session_info, msg_ctx,
121 sname);
125 /* finally, purge old snums */
126 delete_and_reload_printers(ev, msg_ctx);
128 TALLOC_FREE(session_info);
132 /****************************************************************************
133 Notify smbds of new printcap data
134 **************************************************************************/
135 static void reload_pcap_change_notify(struct tevent_context *ev,
136 struct messaging_context *msg_ctx)
139 * Reload the printers first in the background process so that
140 * newly added printers get default values created in the registry.
142 * This will block the process for some time (~1 sec per printer), but
143 * it doesn't block smbd's servering clients.
145 delete_and_reload_printers_full(ev, msg_ctx);
147 message_send_all(msg_ctx, MSG_PRINTER_PCAP, NULL, 0, NULL);
150 struct bq_state {
151 struct tevent_context *ev;
152 struct messaging_context *msg;
153 struct idle_event *housekeep;
156 static bool print_queue_housekeeping(const struct timeval *now, void *pvt)
158 struct bq_state *state;
160 state = talloc_get_type_abort(pvt, struct bq_state);
162 DEBUG(5, ("print queue housekeeping\n"));
163 pcap_cache_reload(state->ev, state->msg, &reload_pcap_change_notify);
165 return true;
168 static bool printing_subsystem_queue_tasks(struct bq_state *state)
170 uint32_t housekeeping_period = lp_printcap_cache_time();
172 /* cancel any existing housekeeping event */
173 TALLOC_FREE(state->housekeep);
175 if (housekeeping_period == 0) {
176 DEBUG(4, ("background print queue housekeeping disabled\n"));
177 return true;
180 state->housekeep = event_add_idle(state->ev, NULL,
181 timeval_set(housekeeping_period, 0),
182 "print_queue_housekeeping",
183 print_queue_housekeeping, state);
184 if (state->housekeep == NULL) {
185 DEBUG(0,("Could not add print_queue_housekeeping event\n"));
186 return false;
189 return true;
192 static void bq_reopen_logs(char *logfile)
194 if (logfile) {
195 lp_set_logfile(logfile);
197 reopen_logs();
200 static void bq_sig_term_handler(struct tevent_context *ev,
201 struct tevent_signal *se,
202 int signum,
203 int count,
204 void *siginfo,
205 void *private_data)
207 exit_server_cleanly("termination signal");
210 static void bq_setup_sig_term_handler(void)
212 struct tevent_signal *se;
214 se = tevent_add_signal(server_event_context(),
215 server_event_context(),
216 SIGTERM, 0,
217 bq_sig_term_handler,
218 NULL);
219 if (!se) {
220 exit_server("failed to setup SIGTERM handler");
224 static void bq_sig_hup_handler(struct tevent_context *ev,
225 struct tevent_signal *se,
226 int signum,
227 int count,
228 void *siginfo,
229 void *pvt)
231 struct bq_state *state;
233 state = talloc_get_type_abort(pvt, struct bq_state);
234 change_to_root_user();
236 DEBUG(1, ("Reloading pcap cache after SIGHUP\n"));
237 pcap_cache_reload(state->ev, state->msg,
238 &reload_pcap_change_notify);
239 printing_subsystem_queue_tasks(state);
240 bq_reopen_logs(NULL);
243 static void bq_setup_sig_hup_handler(struct bq_state *state)
245 struct tevent_signal *se;
247 se = tevent_add_signal(state->ev, state->ev, SIGHUP, 0,
248 bq_sig_hup_handler, state);
249 if (!se) {
250 exit_server("failed to setup SIGHUP handler");
254 static void bq_sig_chld_handler(struct tevent_context *ev_ctx,
255 struct tevent_signal *se,
256 int signum, int count,
257 void *siginfo, void *pvt)
259 int status;
260 pid_t pid;
262 pid = waitpid(-1, &status, WNOHANG);
263 if (WIFEXITED(status)) {
264 DEBUG(6, ("Bq child process %d terminated with %d\n",
265 (int)pid, WEXITSTATUS(status)));
266 } else {
267 DEBUG(3, ("Bq child process %d terminated abnormally\n",
268 (int)pid));
272 static void bq_setup_sig_chld_handler(struct tevent_context *ev_ctx)
274 struct tevent_signal *se;
276 se = tevent_add_signal(ev_ctx, ev_ctx, SIGCHLD, 0,
277 bq_sig_chld_handler, NULL);
278 if (!se) {
279 exit_server("failed to setup SIGCHLD handler");
283 static void bq_smb_conf_updated(struct messaging_context *msg_ctx,
284 void *private_data,
285 uint32_t msg_type,
286 struct server_id server_id,
287 DATA_BLOB *data)
289 struct bq_state *state;
291 state = talloc_get_type_abort(private_data, struct bq_state);
293 DEBUG(10,("smb_conf_updated: Got message saying smb.conf was "
294 "updated. Reloading.\n"));
295 change_to_root_user();
296 pcap_cache_reload(state->ev, msg_ctx, &reload_pcap_change_notify);
297 printing_subsystem_queue_tasks(state);
300 static void printing_pause_fd_handler(struct tevent_context *ev,
301 struct tevent_fd *fde,
302 uint16_t flags,
303 void *private_data)
306 * If pause_pipe[1] is closed it means the parent smbd
307 * and children exited or aborted.
309 exit_server_cleanly(NULL);
312 /****************************************************************************
313 main thread of the background lpq updater
314 ****************************************************************************/
315 pid_t start_background_queue(struct tevent_context *ev,
316 struct messaging_context *msg_ctx,
317 char *logfile)
319 pid_t pid;
320 struct bq_state *state;
322 /* Use local variables for this as we don't
323 * need to save the parent side of this, just
324 * ensure it closes when the process exits.
326 int pause_pipe[2];
328 DEBUG(3,("start_background_queue: Starting background LPQ thread\n"));
330 if (pipe(pause_pipe) == -1) {
331 DEBUG(5,("start_background_queue: cannot create pipe. %s\n", strerror(errno) ));
332 exit(1);
336 * Block signals before forking child as it will have to
337 * set its own handlers. Child will re-enable SIGHUP as
338 * soon as the handlers are set up.
340 BlockSignals(true, SIGTERM);
341 BlockSignals(true, SIGHUP);
343 pid = fork();
345 /* parent or error */
346 if (pid != 0) {
347 /* Re-enable SIGHUP before returnig */
348 BlockSignals(false, SIGTERM);
349 BlockSignals(false, SIGHUP);
350 return pid;
353 if (pid == -1) {
354 DEBUG(5,("start_background_queue: background LPQ thread failed to start. %s\n", strerror(errno) ));
355 exit(1);
358 if (pid == 0) {
359 struct tevent_fd *fde;
360 int ret;
361 NTSTATUS status;
363 /* Child. */
364 DEBUG(5,("start_background_queue: background LPQ thread started\n"));
366 close(pause_pipe[0]);
367 pause_pipe[0] = -1;
369 status = smbd_reinit_after_fork(msg_ctx, ev, true, "lpqd");
371 if (!NT_STATUS_IS_OK(status)) {
372 DEBUG(0,("reinit_after_fork() failed\n"));
373 smb_panic("reinit_after_fork() failed");
376 state = talloc_zero(NULL, struct bq_state);
377 if (state == NULL) {
378 exit(1);
380 state->ev = ev;
381 state->msg = msg_ctx;
383 bq_reopen_logs(logfile);
384 bq_setup_sig_term_handler();
385 bq_setup_sig_hup_handler(state);
386 bq_setup_sig_chld_handler(ev);
388 BlockSignals(false, SIGTERM);
389 BlockSignals(false, SIGHUP);
391 if (!printing_subsystem_queue_tasks(state)) {
392 exit(1);
395 if (!serverid_register(messaging_server_id(msg_ctx),
396 FLAG_MSG_GENERAL |
397 FLAG_MSG_PRINT_GENERAL)) {
398 exit(1);
401 if (!locking_init()) {
402 exit(1);
404 messaging_register(msg_ctx, state, MSG_SMB_CONF_UPDATED,
405 bq_smb_conf_updated);
406 messaging_register(msg_ctx, NULL, MSG_PRINTER_UPDATE,
407 print_queue_receive);
408 /* Remove previous forwarder message set in parent. */
409 messaging_deregister(msg_ctx, MSG_PRINTER_DRVUPGRADE, NULL);
411 messaging_register(msg_ctx, NULL, MSG_PRINTER_DRVUPGRADE,
412 do_drv_upgrade_printer);
414 fde = tevent_add_fd(ev, ev, pause_pipe[1], TEVENT_FD_READ,
415 printing_pause_fd_handler,
416 NULL);
417 if (!fde) {
418 DEBUG(0,("tevent_add_fd() failed for pause_pipe\n"));
419 smb_panic("tevent_add_fd() failed for pause_pipe");
422 pcap_cache_reload(ev, msg_ctx, &reload_pcap_change_notify);
424 DEBUG(5,("start_background_queue: background LPQ thread waiting for messages\n"));
425 ret = tevent_loop_wait(ev);
426 /* should not be reached */
427 DEBUG(0,("background_queue: tevent_loop_wait() exited with %d - %s\n",
428 ret, (ret == 0) ? "out of events" : strerror(errno)));
429 exit(1);
432 close(pause_pipe[1]);
434 return pid;
437 /* Run before the parent forks */
438 bool printing_subsystem_init(struct tevent_context *ev_ctx,
439 struct messaging_context *msg_ctx,
440 bool start_daemons,
441 bool background_queue)
443 pid_t pid = -1;
445 if (!print_backend_init(msg_ctx)) {
446 return false;
449 /* start spoolss daemon */
450 /* start as a separate daemon only if enabled */
451 if (start_daemons && rpc_spoolss_daemon() == RPC_DAEMON_FORK) {
453 pid = start_spoolssd(ev_ctx, msg_ctx);
455 } else if (start_daemons && background_queue) {
457 pid = start_background_queue(ev_ctx, msg_ctx, NULL);
459 } else {
460 bool ret;
461 struct bq_state *state;
463 state = talloc_zero(NULL, struct bq_state);
464 if (state == NULL) {
465 exit(1);
467 state->ev = ev_ctx;
468 state->msg = msg_ctx;
470 ret = printing_subsystem_queue_tasks(state);
472 /* Publish nt printers, this requires a working winreg pipe */
473 pcap_cache_reload(ev_ctx, msg_ctx,
474 &delete_and_reload_printers_full);
476 return ret;
479 if (pid == -1) {
480 return false;
482 background_lpq_updater_pid = pid;
484 return true;
487 void printing_subsystem_update(struct tevent_context *ev_ctx,
488 struct messaging_context *msg_ctx,
489 bool force)
491 if (background_lpq_updater_pid != -1) {
492 if (pcap_cache_loaded(NULL)) {
493 load_printers(ev_ctx, msg_ctx);
495 if (force) {
496 /* Send a sighup to the background process.
497 * this will force it to reload printers */
498 kill(background_lpq_updater_pid, SIGHUP);
500 return;
503 pcap_cache_reload(ev_ctx, msg_ctx,
504 &delete_and_reload_printers_full);