printing: return last change time with pcap_cache_loaded()
[sfrench/samba-autobuild/.git] / source3 / printing / queue_process.c
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
8
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.
13
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.
18
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/>.
21 */
22
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
37 extern pid_t start_spoolssd(struct tevent_context *ev_ctx,
38                             struct messaging_context *msg_ctx);
39
40 /****************************************************************************
41  Notify smbds of new printcap data
42 **************************************************************************/
43 static void reload_pcap_change_notify(struct tevent_context *ev,
44                                struct messaging_context *msg_ctx)
45 {
46         /*
47          * Reload the printers first in the background process so that
48          * newly added printers get default values created in the registry.
49          *
50          * This will block the process for some time (~1 sec per printer), but
51          * it doesn't block smbd's servering clients.
52          */
53         delete_and_reload_printers(ev, msg_ctx);
54
55         message_send_all(msg_ctx, MSG_PRINTER_PCAP, NULL, 0, NULL);
56 }
57
58 struct printing_queue_housekeeping_state {
59         struct tevent_context *ev;
60         struct messaging_context *msg;
61 };
62
63 static bool print_queue_housekeeping(const struct timeval *now, void *pvt)
64 {
65         struct printing_queue_housekeeping_state *state =
66                 talloc_get_type_abort(pvt,
67                 struct printing_queue_housekeeping_state);
68         time_t printcap_cache_time = (time_t)lp_printcap_cache_time();
69         time_t t = time_mono(NULL);
70
71         DEBUG(5, ("print queue housekeeping\n"));
72
73         /* if periodic printcap rescan is enabled,
74          * see if it's time to reload */
75         if ((printcap_cache_time != 0) &&
76             (t >= (last_printer_reload_time + printcap_cache_time))) {
77                 DEBUG( 3,( "Printcap cache time expired.\n"));
78                 pcap_cache_reload(state->ev, state->msg,
79                                   &reload_pcap_change_notify);
80                 last_printer_reload_time = t;
81         }
82
83         return true;
84 }
85
86 static bool printing_subsystem_queue_tasks(struct tevent_context *ev_ctx,
87                                            struct messaging_context *msg_ctx)
88 {
89         struct printing_queue_housekeeping_state *state;
90
91         state = talloc_zero(ev_ctx, struct printing_queue_housekeeping_state);
92         if (state == NULL) {
93                 DEBUG(0,("Could not talloc printing_queue_housekeeping_state\n"));
94                 return false;
95         }
96         state->ev = ev_ctx;
97         state->msg = msg_ctx;
98
99         if (!(event_add_idle(ev_ctx, NULL,
100                              timeval_set(SMBD_HOUSEKEEPING_INTERVAL, 0),
101                              "print_queue_housekeeping",
102                              print_queue_housekeeping,
103                              state))) {
104                 DEBUG(0,("Could not add print_queue_housekeeping event\n"));
105                 return false;
106         }
107
108         return true;
109 }
110
111 static void bq_reopen_logs(char *logfile)
112 {
113         if (logfile) {
114                 lp_set_logfile(logfile);
115         }
116         reopen_logs();
117 }
118
119 static void bq_sig_term_handler(struct tevent_context *ev,
120                                 struct tevent_signal *se,
121                                 int signum,
122                                 int count,
123                                 void *siginfo,
124                                 void *private_data)
125 {
126         exit_server_cleanly("termination signal");
127 }
128
129 static void bq_setup_sig_term_handler(void)
130 {
131         struct tevent_signal *se;
132
133         se = tevent_add_signal(server_event_context(),
134                                server_event_context(),
135                                SIGTERM, 0,
136                                bq_sig_term_handler,
137                                NULL);
138         if (!se) {
139                 exit_server("failed to setup SIGTERM handler");
140         }
141 }
142
143 static void bq_sig_hup_handler(struct tevent_context *ev,
144                                 struct tevent_signal *se,
145                                 int signum,
146                                 int count,
147                                 void *siginfo,
148                                 void *pvt)
149 {
150         struct messaging_context *msg_ctx;
151
152         msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);
153         change_to_root_user();
154
155         DEBUG(1, ("Reloading pcap cache after SIGHUP\n"));
156         pcap_cache_reload(ev, msg_ctx, &reload_pcap_change_notify);
157         bq_reopen_logs(NULL);
158 }
159
160 static void bq_setup_sig_hup_handler(struct tevent_context *ev,
161                                      struct messaging_context *msg_ctx)
162 {
163         struct tevent_signal *se;
164
165         se = tevent_add_signal(ev, ev, SIGHUP, 0, bq_sig_hup_handler,
166                                msg_ctx);
167         if (!se) {
168                 exit_server("failed to setup SIGHUP handler");
169         }
170 }
171
172 static void bq_sig_chld_handler(struct tevent_context *ev_ctx,
173                                 struct tevent_signal *se,
174                                 int signum, int count,
175                                 void *siginfo, void *pvt)
176 {
177         int status;
178         pid_t pid;
179
180         pid = sys_waitpid(-1, &status, WNOHANG);
181         if (WIFEXITED(status)) {
182                 DEBUG(6, ("Bq child process %d terminated with %d\n",
183                           (int)pid, WEXITSTATUS(status)));
184         } else {
185                 DEBUG(3, ("Bq child process %d terminated abnormally\n",
186                           (int)pid));
187         }
188 }
189
190 static void bq_setup_sig_chld_handler(struct tevent_context *ev_ctx)
191 {
192         struct tevent_signal *se;
193
194         se = tevent_add_signal(ev_ctx, ev_ctx, SIGCHLD, 0,
195                                 bq_sig_chld_handler, NULL);
196         if (!se) {
197                 exit_server("failed to setup SIGCHLD handler");
198         }
199 }
200
201 static void bq_smb_conf_updated(struct messaging_context *msg_ctx,
202                                 void *private_data,
203                                 uint32_t msg_type,
204                                 struct server_id server_id,
205                                 DATA_BLOB *data)
206 {
207         struct tevent_context *ev_ctx =
208                 talloc_get_type_abort(private_data, struct tevent_context);
209
210         DEBUG(10,("smb_conf_updated: Got message saying smb.conf was "
211                   "updated. Reloading.\n"));
212         change_to_root_user();
213         pcap_cache_reload(ev_ctx, msg_ctx, &reload_pcap_change_notify);
214 }
215
216 static void printing_pause_fd_handler(struct tevent_context *ev,
217                                       struct tevent_fd *fde,
218                                       uint16_t flags,
219                                       void *private_data)
220 {
221         /*
222          * If pause_pipe[1] is closed it means the parent smbd
223          * and children exited or aborted.
224          */
225         exit_server_cleanly(NULL);
226 }
227
228 /****************************************************************************
229 main thread of the background lpq updater
230 ****************************************************************************/
231 pid_t start_background_queue(struct tevent_context *ev,
232                              struct messaging_context *msg_ctx,
233                              char *logfile)
234 {
235         pid_t pid;
236
237         /* Use local variables for this as we don't
238          * need to save the parent side of this, just
239          * ensure it closes when the process exits.
240          */
241         int pause_pipe[2];
242
243         DEBUG(3,("start_background_queue: Starting background LPQ thread\n"));
244
245         if (pipe(pause_pipe) == -1) {
246                 DEBUG(5,("start_background_queue: cannot create pipe. %s\n", strerror(errno) ));
247                 exit(1);
248         }
249
250         /*
251          * Block signals before forking child as it will have to
252          * set its own handlers. Child will re-enable SIGHUP as
253          * soon as the handlers are set up.
254          */
255         BlockSignals(true, SIGTERM);
256         BlockSignals(true, SIGHUP);
257
258         pid = fork();
259
260         /* parent or error */
261         if (pid != 0) {
262                 /* Re-enable SIGHUP before returnig */
263                 BlockSignals(false, SIGTERM);
264                 BlockSignals(false, SIGHUP);
265                 return pid;
266         }
267
268         if (pid == -1) {
269                 DEBUG(5,("start_background_queue: background LPQ thread failed to start. %s\n", strerror(errno) ));
270                 exit(1);
271         }
272
273         if (pid == 0) {
274                 struct tevent_fd *fde;
275                 int ret;
276                 NTSTATUS status;
277
278                 /* Child. */
279                 DEBUG(5,("start_background_queue: background LPQ thread started\n"));
280
281                 close(pause_pipe[0]);
282                 pause_pipe[0] = -1;
283
284                 status = reinit_after_fork(msg_ctx, ev, true);
285
286                 if (!NT_STATUS_IS_OK(status)) {
287                         DEBUG(0,("reinit_after_fork() failed\n"));
288                         smb_panic("reinit_after_fork() failed");
289                 }
290
291                 prctl_set_comment("lpqd");
292
293                 bq_reopen_logs(logfile);
294                 bq_setup_sig_term_handler();
295                 bq_setup_sig_hup_handler(ev, msg_ctx);
296                 bq_setup_sig_chld_handler(ev);
297
298                 BlockSignals(false, SIGTERM);
299                 BlockSignals(false, SIGHUP);
300
301                 if (!printing_subsystem_queue_tasks(ev, msg_ctx)) {
302                         exit(1);
303                 }
304
305                 if (!serverid_register(messaging_server_id(msg_ctx),
306                                        FLAG_MSG_GENERAL |
307                                        FLAG_MSG_PRINT_GENERAL)) {
308                         exit(1);
309                 }
310
311                 if (!locking_init()) {
312                         exit(1);
313                 }
314                 messaging_register(msg_ctx, ev, MSG_SMB_CONF_UPDATED,
315                                    bq_smb_conf_updated);
316                 messaging_register(msg_ctx, NULL, MSG_PRINTER_UPDATE,
317                                    print_queue_receive);
318                 /* Remove previous forwarder message set in parent. */
319                 messaging_deregister(msg_ctx, MSG_PRINTER_DRVUPGRADE, NULL);
320
321                 messaging_register(msg_ctx, NULL, MSG_PRINTER_DRVUPGRADE,
322                                    do_drv_upgrade_printer);
323
324                 fde = tevent_add_fd(ev, ev, pause_pipe[1], TEVENT_FD_READ,
325                                     printing_pause_fd_handler,
326                                     NULL);
327                 if (!fde) {
328                         DEBUG(0,("tevent_add_fd() failed for pause_pipe\n"));
329                         smb_panic("tevent_add_fd() failed for pause_pipe");
330                 }
331
332                 pcap_cache_reload(ev, msg_ctx, &reload_pcap_change_notify);
333
334                 DEBUG(5,("start_background_queue: background LPQ thread waiting for messages\n"));
335                 ret = tevent_loop_wait(ev);
336                 /* should not be reached */
337                 DEBUG(0,("background_queue: tevent_loop_wait() exited with %d - %s\n",
338                          ret, (ret == 0) ? "out of events" : strerror(errno)));
339                 exit(1);
340         }
341
342         close(pause_pipe[1]);
343
344         return pid;
345 }
346
347 /* Run before the parent forks */
348 bool printing_subsystem_init(struct tevent_context *ev_ctx,
349                              struct messaging_context *msg_ctx,
350                              bool start_daemons,
351                              bool background_queue)
352 {
353         pid_t pid = -1;
354
355         if (!print_backend_init(msg_ctx)) {
356                 return false;
357         }
358
359         /* start spoolss daemon */
360         /* start as a separate daemon only if enabled */
361         if (start_daemons && rpc_spoolss_daemon() == RPC_DAEMON_FORK) {
362
363                 pid = start_spoolssd(ev_ctx, msg_ctx);
364
365         } else if (start_daemons && background_queue) {
366
367                 pid = start_background_queue(ev_ctx, msg_ctx, NULL);
368
369         } else {
370                 bool ret;
371
372                 ret = printing_subsystem_queue_tasks(ev_ctx, msg_ctx);
373
374                 /* Publish nt printers, this requires a working winreg pipe */
375                 pcap_cache_reload(ev_ctx, msg_ctx, &delete_and_reload_printers);
376
377                 return ret;
378         }
379
380         if (pid == -1) {
381                 return false;
382         }
383         background_lpq_updater_pid = pid;
384
385         return true;
386 }
387
388 void printing_subsystem_update(struct tevent_context *ev_ctx,
389                                struct messaging_context *msg_ctx,
390                                bool force)
391 {
392         if (background_lpq_updater_pid != -1) {
393                 if (pcap_cache_loaded(NULL)) {
394                         load_printers(ev_ctx, msg_ctx);
395                 }
396                 if (force) {
397                         /* Send a sighup to the background process.
398                          * this will force it to reload printers */
399                         kill(background_lpq_updater_pid, SIGHUP);
400                 }
401                 return;
402         }
403
404         pcap_cache_reload(ev_ctx, msg_ctx, &delete_and_reload_printers);
405 }