s3:spoolssd Start spoolssd from printing_subsystem_init
[Samba.git] / source3 / printing / queue_process.c
blob5d18a08d63e0585e3c2eae83422b40ca369a7dea
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/proto.h"
32 #include "rpc_server/rpc_service_setup.h"
34 extern pid_t start_spoolssd(struct event_context *ev_ctx,
35 struct messaging_context *msg_ctx);
37 /****************************************************************************
38 Notify smbds of new printcap data
39 **************************************************************************/
40 static void reload_pcap_change_notify(struct tevent_context *ev,
41 struct messaging_context *msg_ctx)
43 message_send_all(msg_ctx, MSG_PRINTER_PCAP, NULL, 0, NULL);
46 static bool print_queue_housekeeping(const struct timeval *now, void *pvt)
48 struct messaging_context *msg_ctx =
49 talloc_get_type_abort(pvt, struct messaging_context);
50 time_t printcap_cache_time = (time_t)lp_printcap_cache_time();
51 time_t t = time_mono(NULL);
53 DEBUG(5, ("print queue housekeeping\n"));
55 /* if periodic printcap rescan is enabled,
56 * see if it's time to reload */
57 if ((printcap_cache_time != 0) &&
58 (t >= (last_printer_reload_time + printcap_cache_time))) {
59 DEBUG( 3,( "Printcap cache time expired.\n"));
60 pcap_cache_reload(messaging_event_context(msg_ctx),
61 msg_ctx,
62 &reload_pcap_change_notify);
63 last_printer_reload_time = t;
66 return true;
69 static bool printing_subsystem_queue_tasks(struct tevent_context *ev_ctx,
70 struct messaging_context *msg_ctx)
72 if (!(event_add_idle(ev_ctx, NULL,
73 timeval_set(SMBD_HOUSEKEEPING_INTERVAL, 0),
74 "print_queue_housekeeping",
75 print_queue_housekeeping,
76 msg_ctx))) {
77 DEBUG(0, ("Could not add print_queue_housekeeping event\n"));
78 return false;
81 return true;
84 static void bq_sig_term_handler(struct tevent_context *ev,
85 struct tevent_signal *se,
86 int signum,
87 int count,
88 void *siginfo,
89 void *private_data)
91 exit_server_cleanly("termination signal");
94 static void bq_setup_sig_term_handler(void)
96 struct tevent_signal *se;
98 se = tevent_add_signal(server_event_context(),
99 server_event_context(),
100 SIGTERM, 0,
101 bq_sig_term_handler,
102 NULL);
103 if (!se) {
104 exit_server("failed to setup SIGTERM handler");
108 static void bq_sig_hup_handler(struct tevent_context *ev,
109 struct tevent_signal *se,
110 int signum,
111 int count,
112 void *siginfo,
113 void *pvt)
115 struct messaging_context *msg_ctx;
117 msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);
118 change_to_root_user();
120 DEBUG(1, ("Reloading pcap cache after SIGHUP\n"));
121 pcap_cache_reload(ev, msg_ctx, &reload_pcap_change_notify);
124 static void bq_setup_sig_hup_handler(struct tevent_context *ev,
125 struct messaging_context *msg_ctx)
127 struct tevent_signal *se;
129 se = tevent_add_signal(ev, ev, SIGHUP, 0, bq_sig_hup_handler,
130 msg_ctx);
131 if (!se) {
132 exit_server("failed to setup SIGHUP handler");
136 static void bq_smb_conf_updated(struct messaging_context *msg_ctx,
137 void *private_data,
138 uint32_t msg_type,
139 struct server_id server_id,
140 DATA_BLOB *data)
142 struct tevent_context *ev_ctx =
143 talloc_get_type_abort(private_data, struct tevent_context);
145 DEBUG(10,("smb_conf_updated: Got message saying smb.conf was "
146 "updated. Reloading.\n"));
147 change_to_root_user();
148 pcap_cache_reload(ev_ctx, msg_ctx, &reload_pcap_change_notify);
151 static void printing_pause_fd_handler(struct tevent_context *ev,
152 struct tevent_fd *fde,
153 uint16_t flags,
154 void *private_data)
157 * If pause_pipe[1] is closed it means the parent smbd
158 * and children exited or aborted.
160 exit_server_cleanly(NULL);
163 /****************************************************************************
164 main thread of the background lpq updater
165 ****************************************************************************/
166 pid_t start_background_queue(struct tevent_context *ev,
167 struct messaging_context *msg_ctx)
169 pid_t pid;
171 /* Use local variables for this as we don't
172 * need to save the parent side of this, just
173 * ensure it closes when the process exits.
175 int pause_pipe[2];
177 DEBUG(3,("start_background_queue: Starting background LPQ thread\n"));
179 if (pipe(pause_pipe) == -1) {
180 DEBUG(5,("start_background_queue: cannot create pipe. %s\n", strerror(errno) ));
181 exit(1);
184 pid = sys_fork();
186 if (pid == -1) {
187 DEBUG(5,("start_background_queue: background LPQ thread failed to start. %s\n", strerror(errno) ));
188 exit(1);
191 if (pid == 0) {
192 struct tevent_fd *fde;
193 int ret;
194 NTSTATUS status;
196 /* Child. */
197 DEBUG(5,("start_background_queue: background LPQ thread started\n"));
199 close(pause_pipe[0]);
200 pause_pipe[0] = -1;
202 status = reinit_after_fork(msg_ctx, ev, procid_self(), true);
204 if (!NT_STATUS_IS_OK(status)) {
205 DEBUG(0,("reinit_after_fork() failed\n"));
206 smb_panic("reinit_after_fork() failed");
209 bq_setup_sig_term_handler();
210 bq_setup_sig_hup_handler(ev, msg_ctx);
212 if (!pcap_cache_loaded()) {
213 pcap_cache_reload(ev, msg_ctx, &reload_printers);
216 if (!printing_subsystem_queue_tasks(ev, msg_ctx)) {
217 exit(1);
220 if (!serverid_register(procid_self(),
221 FLAG_MSG_GENERAL|FLAG_MSG_SMBD
222 |FLAG_MSG_PRINT_GENERAL)) {
223 exit(1);
226 if (!locking_init()) {
227 exit(1);
229 messaging_register(msg_ctx, ev, MSG_SMB_CONF_UPDATED,
230 bq_smb_conf_updated);
231 messaging_register(msg_ctx, NULL, MSG_PRINTER_UPDATE,
232 print_queue_receive);
234 fde = tevent_add_fd(ev, ev, pause_pipe[1], TEVENT_FD_READ,
235 printing_pause_fd_handler,
236 NULL);
237 if (!fde) {
238 DEBUG(0,("tevent_add_fd() failed for pause_pipe\n"));
239 smb_panic("tevent_add_fd() failed for pause_pipe");
242 DEBUG(5,("start_background_queue: background LPQ thread waiting for messages\n"));
243 ret = tevent_loop_wait(ev);
244 /* should not be reached */
245 DEBUG(0,("background_queue: tevent_loop_wait() exited with %d - %s\n",
246 ret, (ret == 0) ? "out of events" : strerror(errno)));
247 exit(1);
250 close(pause_pipe[1]);
252 return pid;
255 /* Run before the parent forks */
256 bool printing_subsystem_init(struct tevent_context *ev_ctx,
257 struct messaging_context *msg_ctx,
258 bool start_daemons,
259 bool background_queue)
261 enum rpc_service_mode_e spoolss_mode = rpc_spoolss_mode();
262 pid_t pid = -1;
264 if (!print_backend_init(msg_ctx)) {
265 return false;
268 /* start spoolss daemon */
269 /* start as a separate daemon only if enabled */
270 if (start_daemons && spoolss_mode == RPC_SERVICE_MODE_DAEMON) {
272 pid = start_spoolssd(ev_ctx, msg_ctx);
274 } else if (start_daemons && background_queue) {
276 pid = start_background_queue(ev_ctx, msg_ctx);
278 } else {
279 bool ret;
281 ret = printing_subsystem_queue_tasks(ev_ctx, msg_ctx);
283 /* Publish nt printers, this requires a working winreg pipe */
284 pcap_cache_reload(ev_ctx, msg_ctx, &reload_printers);
286 return ret;
289 if (pid == -1) {
290 return false;
292 background_lpq_updater_pid = pid;
294 return true;
297 void printing_subsystem_update(struct tevent_context *ev_ctx,
298 struct messaging_context *msg_ctx)
300 if (background_lpq_updater_pid != -1) return;
302 pcap_cache_reload(ev_ctx, msg_ctx, &reload_pcap_change_notify);