s3-printing: Add child handler to bq process
[mat/samba.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 "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"
33
34 extern pid_t start_spoolssd(struct event_context *ev_ctx,
35                             struct messaging_context *msg_ctx);
36
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)
42 {
43         message_send_all(msg_ctx, MSG_PRINTER_PCAP, NULL, 0, NULL);
44 }
45
46 static bool print_queue_housekeeping(const struct timeval *now, void *pvt)
47 {
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);
52
53         DEBUG(5, ("print queue housekeeping\n"));
54
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;
64         }
65
66         return true;
67 }
68
69 static bool printing_subsystem_queue_tasks(struct tevent_context *ev_ctx,
70                                            struct messaging_context *msg_ctx)
71 {
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;
79         }
80
81         return true;
82 }
83
84 static void bq_reopen_logs(char *logfile)
85 {
86         if (logfile) {
87                 lp_set_logfile(logfile);
88         }
89         reopen_logs();
90 }
91
92 static void bq_sig_term_handler(struct tevent_context *ev,
93                                 struct tevent_signal *se,
94                                 int signum,
95                                 int count,
96                                 void *siginfo,
97                                 void *private_data)
98 {
99         exit_server_cleanly("termination signal");
100 }
101
102 static void bq_setup_sig_term_handler(void)
103 {
104         struct tevent_signal *se;
105
106         se = tevent_add_signal(server_event_context(),
107                                server_event_context(),
108                                SIGTERM, 0,
109                                bq_sig_term_handler,
110                                NULL);
111         if (!se) {
112                 exit_server("failed to setup SIGTERM handler");
113         }
114 }
115
116 static void bq_sig_hup_handler(struct tevent_context *ev,
117                                 struct tevent_signal *se,
118                                 int signum,
119                                 int count,
120                                 void *siginfo,
121                                 void *pvt)
122 {
123         struct messaging_context *msg_ctx;
124
125         msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);
126         change_to_root_user();
127
128         DEBUG(1, ("Reloading pcap cache after SIGHUP\n"));
129         pcap_cache_reload(ev, msg_ctx, &reload_pcap_change_notify);
130         bq_reopen_logs(NULL);
131 }
132
133 static void bq_setup_sig_hup_handler(struct tevent_context *ev,
134                                      struct messaging_context *msg_ctx)
135 {
136         struct tevent_signal *se;
137
138         se = tevent_add_signal(ev, ev, SIGHUP, 0, bq_sig_hup_handler,
139                                msg_ctx);
140         if (!se) {
141                 exit_server("failed to setup SIGHUP handler");
142         }
143 }
144
145 static void bq_sig_chld_handler(struct tevent_context *ev_ctx,
146                                 struct tevent_signal *se,
147                                 int signum, int count,
148                                 void *siginfo, void *pvt)
149 {
150         int status;
151         pid_t pid;
152
153         pid = sys_waitpid(-1, &status, WNOHANG);
154         if (WIFEXITED(status)) {
155                 DEBUG(6, ("Bq child process %d terminated with %d\n",
156                           (int)pid, WEXITSTATUS(status)));
157         } else {
158                 DEBUG(3, ("Bq child process %d terminated abnormally\n",
159                           (int)pid));
160         }
161 }
162
163 static void bq_setup_sig_chld_handler(struct tevent_context *ev_ctx)
164 {
165         struct tevent_signal *se;
166
167         se = tevent_add_signal(ev_ctx, ev_ctx, SIGCHLD, 0,
168                                 bq_sig_chld_handler, NULL);
169         if (!se) {
170                 exit_server("failed to setup SIGCHLD handler");
171         }
172 }
173
174 static void bq_smb_conf_updated(struct messaging_context *msg_ctx,
175                                 void *private_data,
176                                 uint32_t msg_type,
177                                 struct server_id server_id,
178                                 DATA_BLOB *data)
179 {
180         struct tevent_context *ev_ctx =
181                 talloc_get_type_abort(private_data, struct tevent_context);
182
183         DEBUG(10,("smb_conf_updated: Got message saying smb.conf was "
184                   "updated. Reloading.\n"));
185         change_to_root_user();
186         pcap_cache_reload(ev_ctx, msg_ctx, &reload_pcap_change_notify);
187 }
188
189 static void printing_pause_fd_handler(struct tevent_context *ev,
190                                       struct tevent_fd *fde,
191                                       uint16_t flags,
192                                       void *private_data)
193 {
194         /*
195          * If pause_pipe[1] is closed it means the parent smbd
196          * and children exited or aborted.
197          */
198         exit_server_cleanly(NULL);
199 }
200
201 /****************************************************************************
202 main thread of the background lpq updater
203 ****************************************************************************/
204 pid_t start_background_queue(struct tevent_context *ev,
205                              struct messaging_context *msg_ctx,
206                              char *logfile)
207 {
208         pid_t pid;
209
210         /* Use local variables for this as we don't
211          * need to save the parent side of this, just
212          * ensure it closes when the process exits.
213          */
214         int pause_pipe[2];
215
216         DEBUG(3,("start_background_queue: Starting background LPQ thread\n"));
217
218         if (pipe(pause_pipe) == -1) {
219                 DEBUG(5,("start_background_queue: cannot create pipe. %s\n", strerror(errno) ));
220                 exit(1);
221         }
222
223         /*
224          * Block signals before forking child as it will have to
225          * set its own handlers. Child will re-enable SIGHUP as
226          * soon as the handlers are set up.
227          */
228         BlockSignals(true, SIGTERM);
229         BlockSignals(true, SIGHUP);
230
231         pid = sys_fork();
232
233         /* parent or error */
234         if (pid != 0) {
235                 /* Re-enable SIGHUP before returnig */
236                 BlockSignals(false, SIGTERM);
237                 BlockSignals(false, SIGHUP);
238                 return pid;
239         }
240
241         if (pid == -1) {
242                 DEBUG(5,("start_background_queue: background LPQ thread failed to start. %s\n", strerror(errno) ));
243                 exit(1);
244         }
245
246         if (pid == 0) {
247                 struct tevent_fd *fde;
248                 int ret;
249                 NTSTATUS status;
250
251                 /* Child. */
252                 DEBUG(5,("start_background_queue: background LPQ thread started\n"));
253
254                 close(pause_pipe[0]);
255                 pause_pipe[0] = -1;
256
257                 status = reinit_after_fork(msg_ctx, ev, procid_self(), true);
258
259                 if (!NT_STATUS_IS_OK(status)) {
260                         DEBUG(0,("reinit_after_fork() failed\n"));
261                         smb_panic("reinit_after_fork() failed");
262                 }
263
264                 bq_reopen_logs(logfile);
265                 bq_setup_sig_term_handler();
266                 bq_setup_sig_hup_handler(ev, msg_ctx);
267                 bq_setup_sig_chld_handler(ev);
268
269                 BlockSignals(false, SIGTERM);
270                 BlockSignals(false, SIGHUP);
271
272                 if (!printing_subsystem_queue_tasks(ev, msg_ctx)) {
273                         exit(1);
274                 }
275
276                 if (!serverid_register(procid_self(),
277                                        FLAG_MSG_GENERAL|FLAG_MSG_SMBD
278                                        |FLAG_MSG_PRINT_GENERAL)) {
279                         exit(1);
280                 }
281
282                 if (!locking_init()) {
283                         exit(1);
284                 }
285                 messaging_register(msg_ctx, ev, MSG_SMB_CONF_UPDATED,
286                                    bq_smb_conf_updated);
287                 messaging_register(msg_ctx, NULL, MSG_PRINTER_UPDATE,
288                                    print_queue_receive);
289
290                 fde = tevent_add_fd(ev, ev, pause_pipe[1], TEVENT_FD_READ,
291                                     printing_pause_fd_handler,
292                                     NULL);
293                 if (!fde) {
294                         DEBUG(0,("tevent_add_fd() failed for pause_pipe\n"));
295                         smb_panic("tevent_add_fd() failed for pause_pipe");
296                 }
297
298                 pcap_cache_reload(ev, msg_ctx, &reload_pcap_change_notify);
299
300                 DEBUG(5,("start_background_queue: background LPQ thread waiting for messages\n"));
301                 ret = tevent_loop_wait(ev);
302                 /* should not be reached */
303                 DEBUG(0,("background_queue: tevent_loop_wait() exited with %d - %s\n",
304                          ret, (ret == 0) ? "out of events" : strerror(errno)));
305                 exit(1);
306         }
307
308         close(pause_pipe[1]);
309
310         return pid;
311 }
312
313 /* Run before the parent forks */
314 bool printing_subsystem_init(struct tevent_context *ev_ctx,
315                              struct messaging_context *msg_ctx,
316                              bool start_daemons,
317                              bool background_queue)
318 {
319         enum rpc_service_mode_e spoolss_mode = rpc_spoolss_mode();
320         pid_t pid = -1;
321
322         if (!print_backend_init(msg_ctx)) {
323                 return false;
324         }
325
326         /* start spoolss daemon */
327         /* start as a separate daemon only if enabled */
328         if (start_daemons && spoolss_mode == RPC_SERVICE_MODE_DAEMON) {
329
330                 pid = start_spoolssd(ev_ctx, msg_ctx);
331
332         } else if (start_daemons && background_queue) {
333
334                 pid = start_background_queue(ev_ctx, msg_ctx, NULL);
335
336         } else {
337                 bool ret;
338
339                 ret = printing_subsystem_queue_tasks(ev_ctx, msg_ctx);
340
341                 /* Publish nt printers, this requires a working winreg pipe */
342                 pcap_cache_reload(ev_ctx, msg_ctx, &reload_printers);
343
344                 return ret;
345         }
346
347         if (pid == -1) {
348                 return false;
349         }
350         background_lpq_updater_pid = pid;
351
352         return true;
353 }
354
355 void printing_subsystem_update(struct tevent_context *ev_ctx,
356                                struct messaging_context *msg_ctx,
357                                bool force)
358 {
359         if (background_lpq_updater_pid != -1) {
360                 if (force) {
361                         /* Send a sighup to the background process.
362                          * this will force it to reload printers */
363                         kill(background_lpq_updater_pid, SIGHUP);
364                 }
365                 return;
366         }
367
368         pcap_cache_reload(ev_ctx, msg_ctx, &reload_pcap_change_notify);
369 }