2011-08-02 00:50:51 +04:00
/*
Unix SMB / Netbios implementation .
Version 3.0
printing backend routines
Copyright ( C ) Andrew Tridgell 1992 - 2000
Copyright ( C ) Jeremy Allison 2002
Copyright ( C ) Simo Sorce 2011
This program is free software ; you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation ; either version 3 of the License , or
( at your option ) any later version .
This program is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
You should have received a copy of the GNU General Public License
along with this program . If not , see < http : //www.gnu.org/licenses/>.
*/
# include "includes.h"
# include "smbd/globals.h"
# include "include/messages.h"
# include "printing.h"
# include "printing/pcap.h"
2011-08-03 14:59:17 +04:00
# include "printing/queue_process.h"
2011-08-02 00:50:51 +04:00
# include "serverid.h"
# include "locking/proto.h"
# include "smbd/proto.h"
2011-05-18 19:24:30 +04:00
# include "rpc_server/rpc_service_setup.h"
2011-08-10 17:27:24 +04:00
# include "printing/load.h"
2011-05-18 19:24:30 +04:00
extern pid_t start_spoolssd ( struct event_context * ev_ctx ,
struct messaging_context * msg_ctx ) ;
2011-08-02 00:50:51 +04:00
/****************************************************************************
Notify smbds of new printcap data
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static void reload_pcap_change_notify ( struct tevent_context * ev ,
struct messaging_context * msg_ctx )
{
message_send_all ( msg_ctx , MSG_PRINTER_PCAP , NULL , 0 , NULL ) ;
}
static bool print_queue_housekeeping ( const struct timeval * now , void * pvt )
{
struct messaging_context * msg_ctx =
talloc_get_type_abort ( pvt , struct messaging_context ) ;
time_t printcap_cache_time = ( time_t ) lp_printcap_cache_time ( ) ;
time_t t = time_mono ( NULL ) ;
DEBUG ( 5 , ( " print queue housekeeping \n " ) ) ;
/* if periodic printcap rescan is enabled,
* see if it ' s time to reload */
if ( ( printcap_cache_time ! = 0 ) & &
( t > = ( last_printer_reload_time + printcap_cache_time ) ) ) {
DEBUG ( 3 , ( " Printcap cache time expired. \n " ) ) ;
pcap_cache_reload ( messaging_event_context ( msg_ctx ) ,
msg_ctx ,
& reload_pcap_change_notify ) ;
last_printer_reload_time = t ;
}
return true ;
}
static bool printing_subsystem_queue_tasks ( struct tevent_context * ev_ctx ,
struct messaging_context * msg_ctx )
{
if ( ! ( event_add_idle ( ev_ctx , NULL ,
timeval_set ( SMBD_HOUSEKEEPING_INTERVAL , 0 ) ,
" print_queue_housekeeping " ,
print_queue_housekeeping ,
msg_ctx ) ) ) {
DEBUG ( 0 , ( " Could not add print_queue_housekeeping event \n " ) ) ;
return false ;
}
return true ;
}
2011-08-05 23:14:26 +04:00
static void bq_reopen_logs ( char * logfile )
{
if ( logfile ) {
lp_set_logfile ( logfile ) ;
}
reopen_logs ( ) ;
}
2011-08-02 00:50:51 +04:00
static void bq_sig_term_handler ( struct tevent_context * ev ,
struct tevent_signal * se ,
int signum ,
int count ,
void * siginfo ,
void * private_data )
{
exit_server_cleanly ( " termination signal " ) ;
}
2011-08-03 14:59:17 +04:00
static void bq_setup_sig_term_handler ( void )
2011-08-02 00:50:51 +04:00
{
struct tevent_signal * se ;
se = tevent_add_signal ( server_event_context ( ) ,
server_event_context ( ) ,
SIGTERM , 0 ,
bq_sig_term_handler ,
NULL ) ;
if ( ! se ) {
exit_server ( " failed to setup SIGTERM handler " ) ;
}
}
static void bq_sig_hup_handler ( struct tevent_context * ev ,
struct tevent_signal * se ,
int signum ,
int count ,
void * siginfo ,
void * pvt )
{
struct messaging_context * msg_ctx ;
msg_ctx = talloc_get_type_abort ( pvt , struct messaging_context ) ;
change_to_root_user ( ) ;
DEBUG ( 1 , ( " Reloading pcap cache after SIGHUP \n " ) ) ;
pcap_cache_reload ( ev , msg_ctx , & reload_pcap_change_notify ) ;
2011-08-05 23:14:26 +04:00
bq_reopen_logs ( NULL ) ;
2011-08-02 00:50:51 +04:00
}
static void bq_setup_sig_hup_handler ( struct tevent_context * ev ,
struct messaging_context * msg_ctx )
{
struct tevent_signal * se ;
se = tevent_add_signal ( ev , ev , SIGHUP , 0 , bq_sig_hup_handler ,
msg_ctx ) ;
if ( ! se ) {
exit_server ( " failed to setup SIGHUP handler " ) ;
}
}
2011-08-10 16:59:44 +04:00
static void bq_sig_chld_handler ( struct tevent_context * ev_ctx ,
struct tevent_signal * se ,
int signum , int count ,
void * siginfo , void * pvt )
{
int status ;
pid_t pid ;
pid = sys_waitpid ( - 1 , & status , WNOHANG ) ;
if ( WIFEXITED ( status ) ) {
DEBUG ( 6 , ( " Bq child process %d terminated with %d \n " ,
( int ) pid , WEXITSTATUS ( status ) ) ) ;
} else {
DEBUG ( 3 , ( " Bq child process %d terminated abnormally \n " ,
( int ) pid ) ) ;
}
}
static void bq_setup_sig_chld_handler ( struct tevent_context * ev_ctx )
{
struct tevent_signal * se ;
se = tevent_add_signal ( ev_ctx , ev_ctx , SIGCHLD , 0 ,
bq_sig_chld_handler , NULL ) ;
if ( ! se ) {
exit_server ( " failed to setup SIGCHLD handler " ) ;
}
}
2011-08-02 00:50:51 +04:00
static void bq_smb_conf_updated ( struct messaging_context * msg_ctx ,
void * private_data ,
uint32_t msg_type ,
struct server_id server_id ,
DATA_BLOB * data )
{
struct tevent_context * ev_ctx =
talloc_get_type_abort ( private_data , struct tevent_context ) ;
DEBUG ( 10 , ( " smb_conf_updated: Got message saying smb.conf was "
" updated. Reloading. \n " ) ) ;
change_to_root_user ( ) ;
pcap_cache_reload ( ev_ctx , msg_ctx , & reload_pcap_change_notify ) ;
}
static void printing_pause_fd_handler ( struct tevent_context * ev ,
struct tevent_fd * fde ,
uint16_t flags ,
void * private_data )
{
/*
* If pause_pipe [ 1 ] is closed it means the parent smbd
* and children exited or aborted .
*/
exit_server_cleanly ( NULL ) ;
}
/****************************************************************************
main thread of the background lpq updater
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2011-08-04 01:04:50 +04:00
pid_t start_background_queue ( struct tevent_context * ev ,
2011-08-05 23:14:26 +04:00
struct messaging_context * msg_ctx ,
char * logfile )
2011-08-02 00:50:51 +04:00
{
2011-08-04 01:04:50 +04:00
pid_t pid ;
2011-08-02 00:50:51 +04:00
/* Use local variables for this as we don't
* need to save the parent side of this , just
* ensure it closes when the process exits .
*/
int pause_pipe [ 2 ] ;
DEBUG ( 3 , ( " start_background_queue: Starting background LPQ thread \n " ) ) ;
if ( pipe ( pause_pipe ) = = - 1 ) {
DEBUG ( 5 , ( " start_background_queue: cannot create pipe. %s \n " , strerror ( errno ) ) ) ;
exit ( 1 ) ;
}
2011-08-08 18:08:46 +04:00
/*
* Block signals before forking child as it will have to
* set its own handlers . Child will re - enable SIGHUP as
* soon as the handlers are set up .
*/
BlockSignals ( true , SIGTERM ) ;
BlockSignals ( true , SIGHUP ) ;
2011-08-04 01:04:50 +04:00
pid = sys_fork ( ) ;
2011-08-02 00:50:51 +04:00
2011-08-08 18:08:46 +04:00
/* parent or error */
if ( pid ! = 0 ) {
/* Re-enable SIGHUP before returnig */
BlockSignals ( false , SIGTERM ) ;
BlockSignals ( false , SIGHUP ) ;
return pid ;
}
2011-08-04 01:04:50 +04:00
if ( pid = = - 1 ) {
2011-08-02 00:50:51 +04:00
DEBUG ( 5 , ( " start_background_queue: background LPQ thread failed to start. %s \n " , strerror ( errno ) ) ) ;
exit ( 1 ) ;
}
2011-08-04 01:04:50 +04:00
if ( pid = = 0 ) {
2011-08-02 00:50:51 +04:00
struct tevent_fd * fde ;
int ret ;
NTSTATUS status ;
/* Child. */
DEBUG ( 5 , ( " start_background_queue: background LPQ thread started \n " ) ) ;
close ( pause_pipe [ 0 ] ) ;
pause_pipe [ 0 ] = - 1 ;
status = reinit_after_fork ( msg_ctx , ev , procid_self ( ) , true ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
DEBUG ( 0 , ( " reinit_after_fork() failed \n " ) ) ;
smb_panic ( " reinit_after_fork() failed " ) ;
}
2011-08-05 23:14:26 +04:00
bq_reopen_logs ( logfile ) ;
2011-08-02 00:50:51 +04:00
bq_setup_sig_term_handler ( ) ;
bq_setup_sig_hup_handler ( ev , msg_ctx ) ;
2011-08-10 16:59:44 +04:00
bq_setup_sig_chld_handler ( ev ) ;
2011-08-02 00:50:51 +04:00
2011-08-08 18:08:46 +04:00
BlockSignals ( false , SIGTERM ) ;
BlockSignals ( false , SIGHUP ) ;
2011-08-02 00:50:51 +04:00
if ( ! printing_subsystem_queue_tasks ( ev , msg_ctx ) ) {
exit ( 1 ) ;
}
if ( ! serverid_register ( procid_self ( ) ,
FLAG_MSG_GENERAL | FLAG_MSG_SMBD
| FLAG_MSG_PRINT_GENERAL ) ) {
exit ( 1 ) ;
}
if ( ! locking_init ( ) ) {
exit ( 1 ) ;
}
messaging_register ( msg_ctx , ev , MSG_SMB_CONF_UPDATED ,
bq_smb_conf_updated ) ;
messaging_register ( msg_ctx , NULL , MSG_PRINTER_UPDATE ,
print_queue_receive ) ;
fde = tevent_add_fd ( ev , ev , pause_pipe [ 1 ] , TEVENT_FD_READ ,
printing_pause_fd_handler ,
NULL ) ;
if ( ! fde ) {
DEBUG ( 0 , ( " tevent_add_fd() failed for pause_pipe \n " ) ) ;
smb_panic ( " tevent_add_fd() failed for pause_pipe " ) ;
}
2011-08-10 16:59:44 +04:00
pcap_cache_reload ( ev , msg_ctx , & reload_pcap_change_notify ) ;
2011-08-02 00:50:51 +04:00
DEBUG ( 5 , ( " start_background_queue: background LPQ thread waiting for messages \n " ) ) ;
ret = tevent_loop_wait ( ev ) ;
/* should not be reached */
DEBUG ( 0 , ( " background_queue: tevent_loop_wait() exited with %d - %s \n " ,
ret , ( ret = = 0 ) ? " out of events " : strerror ( errno ) ) ) ;
exit ( 1 ) ;
}
close ( pause_pipe [ 1 ] ) ;
2011-08-04 01:04:50 +04:00
return pid ;
}
2011-08-02 00:50:51 +04:00
/* Run before the parent forks */
bool printing_subsystem_init ( struct tevent_context * ev_ctx ,
struct messaging_context * msg_ctx ,
2011-05-18 19:24:30 +04:00
bool start_daemons ,
2011-08-02 00:50:51 +04:00
bool background_queue )
{
2011-05-18 19:24:30 +04:00
enum rpc_service_mode_e spoolss_mode = rpc_spoolss_mode ( ) ;
2011-08-04 01:04:50 +04:00
pid_t pid = - 1 ;
2011-08-02 00:50:51 +04:00
2011-08-02 18:06:31 +04:00
if ( ! print_backend_init ( msg_ctx ) ) {
return false ;
}
2011-05-18 19:24:30 +04:00
/* start spoolss daemon */
/* start as a separate daemon only if enabled */
if ( start_daemons & & spoolss_mode = = RPC_SERVICE_MODE_DAEMON ) {
pid = start_spoolssd ( ev_ctx , msg_ctx ) ;
} else if ( start_daemons & & background_queue ) {
2011-08-04 01:04:50 +04:00
2011-08-05 23:14:26 +04:00
pid = start_background_queue ( ev_ctx , msg_ctx , NULL ) ;
2011-08-04 01:04:50 +04:00
2011-08-02 00:50:51 +04:00
} else {
2011-08-04 01:04:50 +04:00
bool ret ;
2011-08-02 00:50:51 +04:00
ret = printing_subsystem_queue_tasks ( ev_ctx , msg_ctx ) ;
2011-08-04 01:04:50 +04:00
/* Publish nt printers, this requires a working winreg pipe */
pcap_cache_reload ( ev_ctx , msg_ctx , & reload_printers ) ;
return ret ;
2011-08-02 00:50:51 +04:00
}
2011-08-04 01:04:50 +04:00
if ( pid = = - 1 ) {
return false ;
}
background_lpq_updater_pid = pid ;
return true ;
2011-08-02 00:50:51 +04:00
}
void printing_subsystem_update ( struct tevent_context * ev_ctx ,
2011-08-04 16:56:01 +04:00
struct messaging_context * msg_ctx ,
bool force )
2011-08-02 00:50:51 +04:00
{
2011-08-04 16:56:01 +04:00
if ( background_lpq_updater_pid ! = - 1 ) {
2011-08-10 17:27:24 +04:00
if ( pcap_cache_loaded ( ) ) {
load_printers ( ev_ctx , msg_ctx ) ;
}
2011-08-04 16:56:01 +04:00
if ( force ) {
/* Send a sighup to the background process.
* this will force it to reload printers */
kill ( background_lpq_updater_pid , SIGHUP ) ;
}
return ;
}
2011-08-02 00:50:51 +04:00
2011-08-10 17:27:24 +04:00
pcap_cache_reload ( ev_ctx , msg_ctx , & reload_printers ) ;
2011-08-02 00:50:51 +04:00
}