pidl: If STR_NULLTERM we concider it's a string as well
[Samba/gebeck_regimport.git] / source3 / printing / queue_process.c
blob48e5f771c9fb550d256d1e2658754c567ae40b65
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"
33 /****************************************************************************
34 Notify smbds of new printcap data
35 **************************************************************************/
36 static void reload_pcap_change_notify(struct tevent_context *ev,
37 struct messaging_context *msg_ctx)
39 message_send_all(msg_ctx, MSG_PRINTER_PCAP, NULL, 0, NULL);
42 static bool print_queue_housekeeping(const struct timeval *now, void *pvt)
44 struct messaging_context *msg_ctx =
45 talloc_get_type_abort(pvt, struct messaging_context);
46 time_t printcap_cache_time = (time_t)lp_printcap_cache_time();
47 time_t t = time_mono(NULL);
49 DEBUG(5, ("print queue housekeeping\n"));
51 /* if periodic printcap rescan is enabled,
52 * see if it's time to reload */
53 if ((printcap_cache_time != 0) &&
54 (t >= (last_printer_reload_time + printcap_cache_time))) {
55 DEBUG( 3,( "Printcap cache time expired.\n"));
56 pcap_cache_reload(messaging_event_context(msg_ctx),
57 msg_ctx,
58 &reload_pcap_change_notify);
59 last_printer_reload_time = t;
62 return true;
65 static bool printing_subsystem_queue_tasks(struct tevent_context *ev_ctx,
66 struct messaging_context *msg_ctx)
68 if (!(event_add_idle(ev_ctx, NULL,
69 timeval_set(SMBD_HOUSEKEEPING_INTERVAL, 0),
70 "print_queue_housekeeping",
71 print_queue_housekeeping,
72 msg_ctx))) {
73 DEBUG(0, ("Could not add print_queue_housekeeping event\n"));
74 return false;
77 return true;
80 static void bq_sig_term_handler(struct tevent_context *ev,
81 struct tevent_signal *se,
82 int signum,
83 int count,
84 void *siginfo,
85 void *private_data)
87 exit_server_cleanly("termination signal");
90 static void bq_setup_sig_term_handler(void)
92 struct tevent_signal *se;
94 se = tevent_add_signal(server_event_context(),
95 server_event_context(),
96 SIGTERM, 0,
97 bq_sig_term_handler,
98 NULL);
99 if (!se) {
100 exit_server("failed to setup SIGTERM handler");
104 static void bq_sig_hup_handler(struct tevent_context *ev,
105 struct tevent_signal *se,
106 int signum,
107 int count,
108 void *siginfo,
109 void *pvt)
111 struct messaging_context *msg_ctx;
113 msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);
114 change_to_root_user();
116 DEBUG(1, ("Reloading pcap cache after SIGHUP\n"));
117 pcap_cache_reload(ev, msg_ctx, &reload_pcap_change_notify);
120 static void bq_setup_sig_hup_handler(struct tevent_context *ev,
121 struct messaging_context *msg_ctx)
123 struct tevent_signal *se;
125 se = tevent_add_signal(ev, ev, SIGHUP, 0, bq_sig_hup_handler,
126 msg_ctx);
127 if (!se) {
128 exit_server("failed to setup SIGHUP handler");
132 static void bq_smb_conf_updated(struct messaging_context *msg_ctx,
133 void *private_data,
134 uint32_t msg_type,
135 struct server_id server_id,
136 DATA_BLOB *data)
138 struct tevent_context *ev_ctx =
139 talloc_get_type_abort(private_data, struct tevent_context);
141 DEBUG(10,("smb_conf_updated: Got message saying smb.conf was "
142 "updated. Reloading.\n"));
143 change_to_root_user();
144 pcap_cache_reload(ev_ctx, msg_ctx, &reload_pcap_change_notify);
147 static void printing_pause_fd_handler(struct tevent_context *ev,
148 struct tevent_fd *fde,
149 uint16_t flags,
150 void *private_data)
153 * If pause_pipe[1] is closed it means the parent smbd
154 * and children exited or aborted.
156 exit_server_cleanly(NULL);
159 extern struct child_pid *children;
160 extern int num_children;
162 static void add_child_pid(pid_t pid)
164 struct child_pid *child;
166 child = SMB_MALLOC_P(struct child_pid);
167 if (child == NULL) {
168 DEBUG(0, ("Could not add child struct -- malloc failed\n"));
169 return;
171 child->pid = pid;
172 DLIST_ADD(children, child);
173 num_children += 1;
176 pid_t background_lpq_updater_pid = -1;
178 /****************************************************************************
179 main thread of the background lpq updater
180 ****************************************************************************/
181 static void start_background_queue(struct tevent_context *ev,
182 struct messaging_context *msg_ctx)
184 /* Use local variables for this as we don't
185 * need to save the parent side of this, just
186 * ensure it closes when the process exits.
188 int pause_pipe[2];
190 DEBUG(3,("start_background_queue: Starting background LPQ thread\n"));
192 if (pipe(pause_pipe) == -1) {
193 DEBUG(5,("start_background_queue: cannot create pipe. %s\n", strerror(errno) ));
194 exit(1);
197 background_lpq_updater_pid = sys_fork();
199 if (background_lpq_updater_pid == -1) {
200 DEBUG(5,("start_background_queue: background LPQ thread failed to start. %s\n", strerror(errno) ));
201 exit(1);
204 /* Track the printing pid along with other smbd children */
205 add_child_pid(background_lpq_updater_pid);
207 if(background_lpq_updater_pid == 0) {
208 struct tevent_fd *fde;
209 int ret;
210 NTSTATUS status;
212 /* Child. */
213 DEBUG(5,("start_background_queue: background LPQ thread started\n"));
215 close(pause_pipe[0]);
216 pause_pipe[0] = -1;
218 status = reinit_after_fork(msg_ctx, ev, procid_self(), true);
220 if (!NT_STATUS_IS_OK(status)) {
221 DEBUG(0,("reinit_after_fork() failed\n"));
222 smb_panic("reinit_after_fork() failed");
225 bq_setup_sig_term_handler();
226 bq_setup_sig_hup_handler(ev, msg_ctx);
228 if (!printing_subsystem_queue_tasks(ev, msg_ctx)) {
229 exit(1);
232 if (!serverid_register(procid_self(),
233 FLAG_MSG_GENERAL|FLAG_MSG_SMBD
234 |FLAG_MSG_PRINT_GENERAL)) {
235 exit(1);
238 if (!locking_init()) {
239 exit(1);
242 messaging_register(msg_ctx, ev, MSG_SMB_CONF_UPDATED,
243 bq_smb_conf_updated);
244 messaging_register(msg_ctx, NULL, MSG_PRINTER_UPDATE,
245 print_queue_receive);
247 fde = tevent_add_fd(ev, ev, pause_pipe[1], TEVENT_FD_READ,
248 printing_pause_fd_handler,
249 NULL);
250 if (!fde) {
251 DEBUG(0,("tevent_add_fd() failed for pause_pipe\n"));
252 smb_panic("tevent_add_fd() failed for pause_pipe");
255 DEBUG(5,("start_background_queue: background LPQ thread waiting for messages\n"));
256 ret = tevent_loop_wait(ev);
257 /* should not be reached */
258 DEBUG(0,("background_queue: tevent_loop_wait() exited with %d - %s\n",
259 ret, (ret == 0) ? "out of events" : strerror(errno)));
260 exit(1);
263 close(pause_pipe[1]);
266 static bool use_background_queue;
268 /* Run before the parent forks */
269 bool printing_subsystem_init(struct tevent_context *ev_ctx,
270 struct messaging_context *msg_ctx,
271 bool background_queue)
273 bool ret = true;
275 use_background_queue = background_queue;
277 if (!print_backend_init(msg_ctx)) {
278 return false;
281 /* Publish nt printers, this requires a working winreg pipe */
282 pcap_cache_reload(ev_ctx, msg_ctx, &reload_printers);
284 if (background_queue) {
285 start_background_queue(ev_ctx, msg_ctx);
286 } else {
287 ret = printing_subsystem_queue_tasks(ev_ctx, msg_ctx);
290 return ret;
293 void printing_subsystem_update(struct tevent_context *ev_ctx,
294 struct messaging_context *msg_ctx)
296 if (use_background_queue) return;
298 pcap_cache_reload(ev_ctx, msg_ctx, &reload_pcap_change_notify);