2 Unix SMB/Netbios implementation.
4 printing backend routines
5 Copyright (C) Andrew Tridgell 1992-2000
6 Copyright (C) Jeremy Allison 2002
7 Copyright (C) Simo Sorce 2011
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.
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.
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/>.
25 #include "smbd/globals.h"
26 #include "include/messages.h"
27 #include "lib/util/util_process.h"
28 #include "lib/util/sys_rw.h"
30 #include "printing/pcap.h"
31 #include "printing/printer_list.h"
32 #include "printing/queue_process.h"
33 #include "locking/proto.h"
34 #include "locking/share_mode_lock.h"
35 #include "smbd/smbd.h"
36 #include "rpc_server/rpc_config.h"
37 #include "printing/load.h"
38 #include "rpc_server/spoolss/srv_spoolss_nt.h"
40 #include "nt_printing.h"
41 #include "util_event.h"
42 #include "lib/global_contexts.h"
43 #include "lib/util/pidfile.h"
46 * @brief Purge stale printers and reload from pre-populated pcap cache.
48 * This function should normally only be called as a callback on a successful
49 * pcap_cache_reload().
51 * This function can cause DELETION of printers and drivers from our registry,
52 * so calling it on a failed pcap reload may REMOVE permanently all printers
55 * @param[in] ev The event context.
57 * @param[in] msg_ctx The messaging context.
59 static void delete_and_reload_printers_full(struct tevent_context
*ev
,
60 struct messaging_context
*msg_ctx
)
62 struct auth_session_info
*session_info
= NULL
;
63 struct spoolss_PrinterInfo2
*pinfo2
= NULL
;
64 const struct loadparm_substitution
*lp_sub
=
65 loadparm_s3_global_substitution();
73 n_services
= lp_numservices();
74 pnum
= lp_servicenumber(PRINTERS_NAME
);
76 status
= make_session_info_system(talloc_tos(), &session_info
);
77 if (!NT_STATUS_IS_OK(status
)) {
78 DEBUG(3, ("reload_printers: "
79 "Could not create system session_info\n"));
80 /* can't remove stale printers before we
81 * are fully initialized */
86 * Add default config for printers added to smb.conf file and remove
89 for (snum
= 0; snum
< n_services
; snum
++) {
90 /* avoid removing PRINTERS_NAME */
95 /* skip no-printer services */
96 if (!snum_is_shared_printer(snum
)) {
100 sname
= lp_const_servicename(snum
);
101 pname
= lp_printername(session_info
, lp_sub
, snum
);
103 /* check printer, but avoid removing non-autoloaded printers */
104 if (lp_autoloaded(snum
) &&
105 !printer_list_printername_exists(pname
)) {
106 DEBUG(3, ("removing stale printer %s\n", pname
));
108 if (is_printer_published(session_info
, session_info
,
111 lp_servicename(session_info
,
115 nt_printer_publish(session_info
,
122 nt_printer_remove(session_info
, session_info
, msg_ctx
,
125 DEBUG(8, ("Adding default registry entry for printer "
126 "[%s], if it doesn't exist.\n", sname
));
127 nt_printer_add(session_info
, session_info
, msg_ctx
,
132 /* finally, purge old snums */
133 delete_and_reload_printers();
135 TALLOC_FREE(session_info
);
139 /****************************************************************************
140 Notify smbds of new printcap data
141 **************************************************************************/
142 static void reload_pcap_change_notify(struct tevent_context
*ev
,
143 struct messaging_context
*msg_ctx
)
146 * Reload the printers first in the background process so that
147 * newly added printers get default values created in the registry.
149 * This will block the process for some time (~1 sec per printer), but
150 * it doesn't block smbd's serving clients.
152 delete_and_reload_printers_full(ev
, msg_ctx
);
154 messaging_send_all(msg_ctx
, MSG_PRINTER_PCAP
, NULL
, 0);
158 struct tevent_context
*ev
;
159 struct messaging_context
*msg
;
160 struct idle_event
*housekeep
;
161 struct tevent_signal
*sighup_handler
;
162 struct tevent_signal
*sigchld_handler
;
165 static bool print_queue_housekeeping(const struct timeval
*now
, void *pvt
)
167 struct bq_state
*state
;
169 state
= talloc_get_type_abort(pvt
, struct bq_state
);
171 DEBUG(5, ("print queue housekeeping\n"));
172 pcap_cache_reload(state
->ev
, state
->msg
, reload_pcap_change_notify
);
177 static bool printing_subsystem_queue_tasks(struct bq_state
*state
)
179 uint32_t housekeeping_period
= lp_printcap_cache_time();
181 /* cancel any existing housekeeping event */
182 TALLOC_FREE(state
->housekeep
);
184 if ((housekeeping_period
== 0) || !lp_load_printers()) {
185 DEBUG(4, ("background print queue housekeeping disabled\n"));
189 state
->housekeep
= event_add_idle(state
->ev
, NULL
,
190 timeval_set(housekeeping_period
, 0),
191 "print_queue_housekeeping",
192 print_queue_housekeeping
, state
);
193 if (state
->housekeep
== NULL
) {
194 DEBUG(0,("Could not add print_queue_housekeeping event\n"));
201 static void bq_reopen_logs(char *logfile
)
204 lp_set_logfile(logfile
);
209 static void bq_sig_hup_handler(struct tevent_context
*ev
,
210 struct tevent_signal
*se
,
216 struct bq_state
*state
;
218 state
= talloc_get_type_abort(pvt
, struct bq_state
);
219 change_to_root_user();
221 DEBUG(1, ("Reloading pcap cache after SIGHUP\n"));
222 pcap_cache_reload(state
->ev
, state
->msg
,
223 reload_pcap_change_notify
);
224 printing_subsystem_queue_tasks(state
);
225 bq_reopen_logs(NULL
);
228 static void bq_sig_chld_handler(struct tevent_context
*ev_ctx
,
229 struct tevent_signal
*se
,
230 int signum
, int count
,
231 void *siginfo
, void *pvt
)
238 pid
= waitpid(-1, &status
, WNOHANG
);
239 } while ((pid
== -1) && (errno
== EINTR
));
241 if (WIFEXITED(status
)) {
242 DBG_INFO("Bq child process %d terminated with %d\n",
244 WEXITSTATUS(status
));
246 DBG_NOTICE("Bq child process %d terminated abnormally\n",
252 static void bq_smb_conf_updated(struct messaging_context
*msg_ctx
,
255 struct server_id server_id
,
258 struct bq_state
*state
;
260 state
= talloc_get_type_abort(private_data
, struct bq_state
);
262 DEBUG(10,("smb_conf_updated: Got message saying smb.conf was "
263 "updated. Reloading.\n"));
264 change_to_root_user();
265 pcap_cache_reload(state
->ev
, msg_ctx
, reload_pcap_change_notify
);
266 printing_subsystem_queue_tasks(state
);
269 static int bq_state_destructor(struct bq_state
*s
)
271 struct messaging_context
*msg_ctx
= s
->msg
;
272 TALLOC_FREE(s
->sighup_handler
);
273 TALLOC_FREE(s
->sigchld_handler
);
274 messaging_deregister(msg_ctx
, MSG_PRINTER_DRVUPGRADE
, NULL
);
275 messaging_deregister(msg_ctx
, MSG_PRINTER_UPDATE
, NULL
);
276 messaging_deregister(msg_ctx
, MSG_SMB_CONF_UPDATED
, s
);
280 struct bq_state
*register_printing_bq_handlers(
282 struct messaging_context
*msg_ctx
)
284 struct bq_state
*state
= NULL
;
288 state
= talloc_zero(mem_ctx
, struct bq_state
);
292 state
->ev
= messaging_tevent_context(msg_ctx
);
293 state
->msg
= msg_ctx
;
295 status
= messaging_register(
296 msg_ctx
, state
, MSG_SMB_CONF_UPDATED
, bq_smb_conf_updated
);
297 if (!NT_STATUS_IS_OK(status
)) {
300 status
= messaging_register(
301 msg_ctx
, NULL
, MSG_PRINTER_UPDATE
, print_queue_receive
);
302 if (!NT_STATUS_IS_OK(status
)) {
303 goto fail_dereg_smb_conf_updated
;
305 status
= messaging_register(
306 msg_ctx
, NULL
, MSG_PRINTER_DRVUPGRADE
, do_drv_upgrade_printer
);
307 if (!NT_STATUS_IS_OK(status
)) {
308 goto fail_dereg_printer_update
;
311 state
->sighup_handler
= tevent_add_signal(
312 state
->ev
, state
, SIGHUP
, 0, bq_sig_hup_handler
, state
);
313 if (state
->sighup_handler
== NULL
) {
314 goto fail_dereg_printer_drvupgrade
;
316 state
->sigchld_handler
= tevent_add_signal(
317 state
->ev
, state
, SIGCHLD
, 0, bq_sig_chld_handler
, NULL
);
318 if (state
->sigchld_handler
== NULL
) {
319 goto fail_free_handlers
;
322 /* Initialize the printcap cache as soon as the daemon starts. */
323 pcap_cache_reload(state
->ev
, state
->msg
, reload_pcap_change_notify
);
325 ok
= printing_subsystem_queue_tasks(state
);
327 goto fail_free_handlers
;
330 talloc_set_destructor(state
, bq_state_destructor
);
335 TALLOC_FREE(state
->sighup_handler
);
336 TALLOC_FREE(state
->sigchld_handler
);
337 fail_dereg_printer_drvupgrade
:
338 messaging_deregister(msg_ctx
, MSG_PRINTER_DRVUPGRADE
, NULL
);
339 fail_dereg_printer_update
:
340 messaging_deregister(msg_ctx
, MSG_PRINTER_UPDATE
, NULL
);
341 fail_dereg_smb_conf_updated
:
342 messaging_deregister(msg_ctx
, MSG_SMB_CONF_UPDATED
, state
);
348 /****************************************************************************
349 main thread of the background lpq updater
350 ****************************************************************************/
351 pid_t
start_background_queue(struct tevent_context
*ev
,
352 struct messaging_context
*msg_ctx
,
361 DEBUG(3,("start_background_queue: Starting background LPQ thread\n"));
363 ret
= pipe(ready_fds
);
368 argv
= str_list_make_empty(talloc_tos());
370 &argv
, "%s/samba-bgqd", get_dyn_SAMBA_LIBEXECDIR());
372 &argv
, "--ready-signal-fd=%d", ready_fds
[1]);
374 &argv
, "--parent-watch-fd=%d", 0);
376 &argv
, "--debuglevel=%d", debuglevel_get_class(DBGC_RPC_SRV
));
377 if (!is_default_dyn_CONFIGFILE()) {
379 &argv
, "--configfile=%s", get_dyn_CONFIGFILE());
381 if (!is_default_dyn_LOGFILEBASE()) {
383 &argv
, "--log-basename=%s", get_dyn_LOGFILEBASE());
385 str_list_add_printf(&argv
, "-F");
390 ret
= posix_spawn(&pid
, argv
[0], NULL
, NULL
, argv
, environ
);
398 nread
= sys_read(ready_fds
[0], &pid
, sizeof(pid
));
400 if (nread
!= sizeof(pid
)) {
419 /* Run before the parent forks */
420 bool printing_subsystem_init(struct tevent_context
*ev_ctx
,
421 struct messaging_context
*msg_ctx
,
422 struct dcesrv_context
*dce_ctx
)
426 pid
= start_background_queue(NULL
, NULL
, NULL
);
430 background_lpq_updater_pid
= pid
;
432 if (!print_backend_init(msg_ctx
)) {
439 void send_to_bgqd(struct messaging_context
*msg_ctx
,
444 pid_t bgqd
= pidfile_pid(lp_pid_directory(), "samba-bgqd");
450 msg_ctx
, pid_to_procid(bgqd
), msg_type
, buf
, buflen
);