examples: fix build on AIX6
[Samba/gebeck_regimport.git] / source3 / printing / queue_process.c
blob3d142dd7693d93c38e54822735834e2cf7c7befa
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"
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)
45 * Reload the printers first in the background process so that
46 * newly added printers get default values created in the registry.
48 * This will block the process for some time (~1 sec per printer), but
49 * it doesn't block smbd's servering clients.
51 delete_and_reload_printers(ev, msg_ctx);
53 message_send_all(msg_ctx, MSG_PRINTER_PCAP, NULL, 0, NULL);
56 struct printing_queue_housekeeping_state {
57 struct tevent_context *ev;
58 struct messaging_context *msg;
61 static bool print_queue_housekeeping(const struct timeval *now, void *pvt)
63 struct printing_queue_housekeeping_state *state =
64 talloc_get_type_abort(pvt,
65 struct printing_queue_housekeeping_state);
66 time_t printcap_cache_time = (time_t)lp_printcap_cache_time();
67 time_t t = time_mono(NULL);
69 DEBUG(5, ("print queue housekeeping\n"));
71 /* if periodic printcap rescan is enabled,
72 * see if it's time to reload */
73 if ((printcap_cache_time != 0) &&
74 (t >= (last_printer_reload_time + printcap_cache_time))) {
75 DEBUG( 3,( "Printcap cache time expired.\n"));
76 pcap_cache_reload(state->ev, state->msg,
77 &reload_pcap_change_notify);
78 last_printer_reload_time = t;
81 return true;
84 static bool printing_subsystem_queue_tasks(struct tevent_context *ev_ctx,
85 struct messaging_context *msg_ctx)
87 struct printing_queue_housekeeping_state *state;
89 state = talloc_zero(ev_ctx, struct printing_queue_housekeeping_state);
90 if (state == NULL) {
91 DEBUG(0,("Could not talloc printing_queue_housekeeping_state\n"));
92 return false;
94 state->ev = ev_ctx;
95 state->msg = msg_ctx;
97 if (!(event_add_idle(ev_ctx, NULL,
98 timeval_set(SMBD_HOUSEKEEPING_INTERVAL, 0),
99 "print_queue_housekeeping",
100 print_queue_housekeeping,
101 state))) {
102 DEBUG(0,("Could not add print_queue_housekeeping event\n"));
103 return false;
106 return true;
109 static void bq_reopen_logs(char *logfile)
111 if (logfile) {
112 lp_set_logfile(logfile);
114 reopen_logs();
117 static void bq_sig_term_handler(struct tevent_context *ev,
118 struct tevent_signal *se,
119 int signum,
120 int count,
121 void *siginfo,
122 void *private_data)
124 exit_server_cleanly("termination signal");
127 static void bq_setup_sig_term_handler(void)
129 struct tevent_signal *se;
131 se = tevent_add_signal(server_event_context(),
132 server_event_context(),
133 SIGTERM, 0,
134 bq_sig_term_handler,
135 NULL);
136 if (!se) {
137 exit_server("failed to setup SIGTERM handler");
141 static void bq_sig_hup_handler(struct tevent_context *ev,
142 struct tevent_signal *se,
143 int signum,
144 int count,
145 void *siginfo,
146 void *pvt)
148 struct messaging_context *msg_ctx;
150 msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);
151 change_to_root_user();
153 DEBUG(1, ("Reloading pcap cache after SIGHUP\n"));
154 pcap_cache_reload(ev, msg_ctx, &reload_pcap_change_notify);
155 bq_reopen_logs(NULL);
158 static void bq_setup_sig_hup_handler(struct tevent_context *ev,
159 struct messaging_context *msg_ctx)
161 struct tevent_signal *se;
163 se = tevent_add_signal(ev, ev, SIGHUP, 0, bq_sig_hup_handler,
164 msg_ctx);
165 if (!se) {
166 exit_server("failed to setup SIGHUP handler");
170 static void bq_sig_chld_handler(struct tevent_context *ev_ctx,
171 struct tevent_signal *se,
172 int signum, int count,
173 void *siginfo, void *pvt)
175 int status;
176 pid_t pid;
178 pid = sys_waitpid(-1, &status, WNOHANG);
179 if (WIFEXITED(status)) {
180 DEBUG(6, ("Bq child process %d terminated with %d\n",
181 (int)pid, WEXITSTATUS(status)));
182 } else {
183 DEBUG(3, ("Bq child process %d terminated abnormally\n",
184 (int)pid));
188 static void bq_setup_sig_chld_handler(struct tevent_context *ev_ctx)
190 struct tevent_signal *se;
192 se = tevent_add_signal(ev_ctx, ev_ctx, SIGCHLD, 0,
193 bq_sig_chld_handler, NULL);
194 if (!se) {
195 exit_server("failed to setup SIGCHLD handler");
199 static void bq_smb_conf_updated(struct messaging_context *msg_ctx,
200 void *private_data,
201 uint32_t msg_type,
202 struct server_id server_id,
203 DATA_BLOB *data)
205 struct tevent_context *ev_ctx =
206 talloc_get_type_abort(private_data, struct tevent_context);
208 DEBUG(10,("smb_conf_updated: Got message saying smb.conf was "
209 "updated. Reloading.\n"));
210 change_to_root_user();
211 pcap_cache_reload(ev_ctx, msg_ctx, &reload_pcap_change_notify);
214 static void printing_pause_fd_handler(struct tevent_context *ev,
215 struct tevent_fd *fde,
216 uint16_t flags,
217 void *private_data)
220 * If pause_pipe[1] is closed it means the parent smbd
221 * and children exited or aborted.
223 exit_server_cleanly(NULL);
226 /****************************************************************************
227 main thread of the background lpq updater
228 ****************************************************************************/
229 pid_t start_background_queue(struct tevent_context *ev,
230 struct messaging_context *msg_ctx,
231 char *logfile)
233 pid_t pid;
235 /* Use local variables for this as we don't
236 * need to save the parent side of this, just
237 * ensure it closes when the process exits.
239 int pause_pipe[2];
241 DEBUG(3,("start_background_queue: Starting background LPQ thread\n"));
243 if (pipe(pause_pipe) == -1) {
244 DEBUG(5,("start_background_queue: cannot create pipe. %s\n", strerror(errno) ));
245 exit(1);
249 * Block signals before forking child as it will have to
250 * set its own handlers. Child will re-enable SIGHUP as
251 * soon as the handlers are set up.
253 BlockSignals(true, SIGTERM);
254 BlockSignals(true, SIGHUP);
256 pid = fork();
258 /* parent or error */
259 if (pid != 0) {
260 /* Re-enable SIGHUP before returnig */
261 BlockSignals(false, SIGTERM);
262 BlockSignals(false, SIGHUP);
263 return pid;
266 if (pid == -1) {
267 DEBUG(5,("start_background_queue: background LPQ thread failed to start. %s\n", strerror(errno) ));
268 exit(1);
271 if (pid == 0) {
272 struct tevent_fd *fde;
273 int ret;
274 NTSTATUS status;
276 /* Child. */
277 DEBUG(5,("start_background_queue: background LPQ thread started\n"));
279 close(pause_pipe[0]);
280 pause_pipe[0] = -1;
282 status = reinit_after_fork(msg_ctx, ev, true);
284 if (!NT_STATUS_IS_OK(status)) {
285 DEBUG(0,("reinit_after_fork() failed\n"));
286 smb_panic("reinit_after_fork() failed");
289 bq_reopen_logs(logfile);
290 bq_setup_sig_term_handler();
291 bq_setup_sig_hup_handler(ev, msg_ctx);
292 bq_setup_sig_chld_handler(ev);
294 BlockSignals(false, SIGTERM);
295 BlockSignals(false, SIGHUP);
297 if (!printing_subsystem_queue_tasks(ev, msg_ctx)) {
298 exit(1);
301 if (!serverid_register(messaging_server_id(msg_ctx),
302 FLAG_MSG_GENERAL |
303 FLAG_MSG_PRINT_GENERAL)) {
304 exit(1);
307 if (!locking_init()) {
308 exit(1);
310 messaging_register(msg_ctx, ev, MSG_SMB_CONF_UPDATED,
311 bq_smb_conf_updated);
312 messaging_register(msg_ctx, NULL, MSG_PRINTER_UPDATE,
313 print_queue_receive);
315 fde = tevent_add_fd(ev, ev, pause_pipe[1], TEVENT_FD_READ,
316 printing_pause_fd_handler,
317 NULL);
318 if (!fde) {
319 DEBUG(0,("tevent_add_fd() failed for pause_pipe\n"));
320 smb_panic("tevent_add_fd() failed for pause_pipe");
323 pcap_cache_reload(ev, msg_ctx, &reload_pcap_change_notify);
325 DEBUG(5,("start_background_queue: background LPQ thread waiting for messages\n"));
326 ret = tevent_loop_wait(ev);
327 /* should not be reached */
328 DEBUG(0,("background_queue: tevent_loop_wait() exited with %d - %s\n",
329 ret, (ret == 0) ? "out of events" : strerror(errno)));
330 exit(1);
333 close(pause_pipe[1]);
335 return pid;
338 /* Run before the parent forks */
339 bool printing_subsystem_init(struct tevent_context *ev_ctx,
340 struct messaging_context *msg_ctx,
341 bool start_daemons,
342 bool background_queue)
344 pid_t pid = -1;
346 if (!print_backend_init(msg_ctx)) {
347 return false;
350 /* start spoolss daemon */
351 /* start as a separate daemon only if enabled */
352 if (start_daemons && rpc_spoolss_daemon() == RPC_DAEMON_FORK) {
354 pid = start_spoolssd(ev_ctx, msg_ctx);
356 } else if (start_daemons && background_queue) {
358 pid = start_background_queue(ev_ctx, msg_ctx, NULL);
360 } else {
361 bool ret;
363 ret = printing_subsystem_queue_tasks(ev_ctx, msg_ctx);
365 /* Publish nt printers, this requires a working winreg pipe */
366 pcap_cache_reload(ev_ctx, msg_ctx, &delete_and_reload_printers);
368 return ret;
371 if (pid == -1) {
372 return false;
374 background_lpq_updater_pid = pid;
376 return true;
379 void printing_subsystem_update(struct tevent_context *ev_ctx,
380 struct messaging_context *msg_ctx,
381 bool force)
383 if (background_lpq_updater_pid != -1) {
384 if (pcap_cache_loaded()) {
385 load_printers(ev_ctx, msg_ctx);
387 if (force) {
388 /* Send a sighup to the background process.
389 * this will force it to reload printers */
390 kill(background_lpq_updater_pid, SIGHUP);
392 return;
395 pcap_cache_reload(ev_ctx, msg_ctx, &delete_and_reload_printers);