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