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