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