2011-05-13 12:45:46 +04:00
/*
2012-02-28 22:35:04 +04:00
* Copyright ( C ) 2011 - 2012 Red Hat , Inc .
2011-05-13 12:45:46 +04:00
*
* This copyrighted material is made available to anyone wishing to use ,
* modify , copy , or redistribute it subject to the terms and conditions
* of the GNU Lesser General Public License v .2 .1 .
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program ; if not , write to the Free Software Foundation ,
2016-01-21 13:49:46 +03:00
* Inc . , 51 Franklin Street , Fifth Floor , Boston , MA 02110 - 1301 USA
2011-05-13 12:45:46 +04:00
*/
2018-05-14 12:30:20 +03:00
# include "tools/tool.h"
2015-07-06 19:30:18 +03:00
2012-02-28 22:35:04 +04:00
# include "daemon-server.h"
2012-08-08 11:41:01 +04:00
# include "daemon-log.h"
2024-04-13 01:39:43 +03:00
# include "daemon-stray.h"
2018-12-03 20:14:26 +03:00
# include "libdaemon/client/daemon-io.h"
2012-02-28 22:35:04 +04:00
2011-05-13 12:45:46 +04:00
# include <dlfcn.h>
# include <errno.h>
2022-02-16 02:48:49 +03:00
# include <fcntl.h> /* help musl C */
2011-05-13 12:45:46 +04:00
# include <pthread.h>
# include <sys/stat.h>
# include <sys/wait.h>
# include <sys/resource.h>
2011-05-15 15:02:29 +04:00
# include <netinet/in.h>
# include <sys/un.h>
2011-05-13 12:45:46 +04:00
# include <unistd.h>
# include <signal.h>
2012-08-08 11:41:01 +04:00
# include <syslog.h> /* FIXME. For the global closelog(). */
2011-05-13 12:45:46 +04:00
2011-05-15 15:02:29 +04:00
#if 0
2011-05-13 12:45:46 +04:00
/* Create a device monitoring thread. */
static int _pthread_create ( pthread_t * t , void * ( * fun ) ( void * ) , void * arg , int stacksize )
{
pthread_attr_t attr ;
pthread_attr_init ( & attr ) ;
/*
* We use a smaller stack since it gets preallocated in its entirety
*/
2017-02-11 20:14:00 +03:00
pthread_attr_setstacksize ( & attr , stacksize + getpagesize ( ) ) ;
2011-05-13 12:45:46 +04:00
return pthread_create ( t , & attr , fun , arg ) ;
}
2011-05-15 15:02:29 +04:00
# endif
2011-05-13 12:45:46 +04:00
static volatile sig_atomic_t _shutdown_requested = 0 ;
2012-02-28 17:05:21 +04:00
static int _systemd_activation = 0 ;
2011-05-13 12:45:46 +04:00
static void _exit_handler ( int sig __attribute__ ( ( unused ) ) )
{
_shutdown_requested = 1 ;
}
2013-04-15 11:43:30 +04:00
# define EXIT_ALREADYRUNNING 13
2013-11-13 17:56:29 +04:00
# ifdef __linux__
2012-02-28 17:05:21 +04:00
# include <stddef.h>
/*
* Kernel version 2.6 .36 and higher has
* new OOM killer adjustment interface .
*/
# define OOM_ADJ_FILE_OLD " / proc / self / oom_adj"
# define OOM_ADJ_FILE " / proc / self / oom_score_adj"
2011-05-13 12:45:46 +04:00
/* From linux/oom.h */
2012-02-28 17:05:21 +04:00
/* Old interface */
2011-05-13 12:45:46 +04:00
# define OOM_DISABLE (-17)
# define OOM_ADJUST_MIN (-16)
2012-02-28 17:05:21 +04:00
/* New interface */
# define OOM_SCORE_ADJ_MIN (-1000)
/* Systemd on-demand activation support */
2012-03-14 19:51:51 +04:00
# define SD_ACTIVATION_ENV_VAR_NAME "SD_ACTIVATION"
2012-02-28 17:05:21 +04:00
# define SD_LISTEN_PID_ENV_VAR_NAME "LISTEN_PID"
# define SD_LISTEN_FDS_ENV_VAR_NAME "LISTEN_FDS"
# define SD_LISTEN_FDS_START 3
# define SD_FD_SOCKET_SERVER SD_LISTEN_FDS_START
2015-04-29 17:59:30 +03:00
static int _is_idle ( daemon_state s )
{
2015-05-12 10:37:19 +03:00
return s . idle & & s . idle - > is_idle & & ! s . threads - > next ;
2015-04-29 17:59:30 +03:00
}
2019-04-10 13:50:53 +03:00
static struct timespec * _get_timeout ( daemon_state s )
2015-04-29 17:59:30 +03:00
{
2015-05-12 10:37:19 +03:00
return s . idle ? s . idle - > ptimeout : NULL ;
2015-04-29 17:59:30 +03:00
}
static void _reset_timeout ( daemon_state s )
{
if ( s . idle ) {
s . idle - > ptimeout - > tv_sec = 1 ;
2019-04-10 13:50:53 +03:00
s . idle - > ptimeout - > tv_nsec = 0 ;
2015-04-29 17:59:30 +03:00
}
}
static unsigned _get_max_timeouts ( daemon_state s )
{
return s . idle ? s . idle - > max_timeouts : 0 ;
}
2012-02-28 17:05:21 +04:00
static int _set_oom_adj ( const char * oom_adj_path , int val )
{
FILE * fp ;
if ( ! ( fp = fopen ( oom_adj_path , " w " ) ) ) {
perror ( " oom_adj: fopen failed " ) ;
return 0 ;
}
fprintf ( fp , " %i " , val ) ;
if ( dm_fclose ( fp ) )
perror ( " oom_adj: fclose failed " ) ;
return 1 ;
}
2011-05-13 12:45:46 +04:00
/*
* Protection against OOM killer if kernel supports it
*/
2012-02-28 17:05:21 +04:00
static int _protect_against_oom_killer ( void )
2011-05-13 12:45:46 +04:00
{
struct stat st ;
if ( stat ( OOM_ADJ_FILE , & st ) = = - 1 ) {
2012-02-28 17:05:21 +04:00
if ( errno ! = ENOENT )
2011-05-13 12:45:46 +04:00
perror ( OOM_ADJ_FILE " : stat failed " ) ;
2012-02-28 17:05:21 +04:00
/* Try old oom_adj interface as a fallback */
if ( stat ( OOM_ADJ_FILE_OLD , & st ) = = - 1 ) {
if ( errno = = ENOENT )
perror ( OOM_ADJ_FILE_OLD " not found " ) ;
else
perror ( OOM_ADJ_FILE_OLD " : stat failed " ) ;
return 1 ;
}
return _set_oom_adj ( OOM_ADJ_FILE_OLD , OOM_DISABLE ) | |
_set_oom_adj ( OOM_ADJ_FILE_OLD , OOM_ADJUST_MIN ) ;
2011-05-13 12:45:46 +04:00
}
2012-02-28 17:05:21 +04:00
return _set_oom_adj ( OOM_ADJ_FILE , OOM_SCORE_ADJ_MIN ) ;
}
union sockaddr_union {
struct sockaddr sa ;
struct sockaddr_un un ;
} ;
static int _handle_preloaded_socket ( int fd , const char * path )
{
struct stat st_fd ;
2012-06-21 23:19:28 +04:00
union sockaddr_union sockaddr = { . sa . sa_family = 0 } ;
2012-02-28 17:05:21 +04:00
int type = 0 ;
socklen_t len = sizeof ( type ) ;
size_t path_len = strlen ( path ) ;
if ( fd < 0 )
2011-05-13 12:45:46 +04:00
return 0 ;
2012-02-28 17:05:21 +04:00
if ( fstat ( fd , & st_fd ) < 0 | | ! S_ISSOCK ( st_fd . st_mode ) )
return 0 ;
if ( getsockopt ( fd , SOL_SOCKET , SO_TYPE , & type , & len ) < 0 | |
len ! = sizeof ( type ) | | type ! = SOCK_STREAM )
return 0 ;
len = sizeof ( sockaddr ) ;
if ( getsockname ( fd , & sockaddr . sa , & len ) < 0 | |
len < sizeof ( sa_family_t ) | |
sockaddr . sa . sa_family ! = PF_UNIX )
return 0 ;
if ( ! ( len > = offsetof ( struct sockaddr_un , sun_path ) + path_len + 1 & &
memcmp ( path , sockaddr . un . sun_path , path_len ) = = 0 ) )
return 0 ;
2011-05-13 12:45:46 +04:00
return 1 ;
}
2012-02-28 17:05:21 +04:00
static int _systemd_handover ( struct daemon_state * ds )
{
const char * e ;
char * p ;
unsigned long env_pid , env_listen_fds ;
int r = 0 ;
2012-03-14 19:51:51 +04:00
/* SD_ACTIVATION must be set! */
if ( ! ( e = getenv ( SD_ACTIVATION_ENV_VAR_NAME ) ) | | strcmp ( e , " 1 " ) )
goto out ;
2012-02-28 17:05:21 +04:00
/* LISTEN_PID must be equal to our PID! */
if ( ! ( e = getenv ( SD_LISTEN_PID_ENV_VAR_NAME ) ) )
goto out ;
errno = 0 ;
env_pid = strtoul ( e , & p , 10 ) ;
if ( errno | | ! p | | * p | | env_pid < = 0 | |
getpid ( ) ! = ( pid_t ) env_pid )
2012-03-17 01:30:53 +04:00
goto out ;
2012-02-28 17:05:21 +04:00
/* LISTEN_FDS must be 1 and the fd must be a socket! */
if ( ! ( e = getenv ( SD_LISTEN_FDS_ENV_VAR_NAME ) ) )
goto out ;
errno = 0 ;
env_listen_fds = strtoul ( e , & p , 10 ) ;
if ( errno | | ! p | | * p | | env_listen_fds ! = 1 )
goto out ;
/* Check and handle the socket passed in */
if ( ( r = _handle_preloaded_socket ( SD_FD_SOCKET_SERVER , ds - > socket_path ) ) )
ds - > socket_fd = SD_FD_SOCKET_SERVER ;
out :
2012-03-14 19:51:51 +04:00
unsetenv ( SD_ACTIVATION_ENV_VAR_NAME ) ;
2012-02-28 17:05:21 +04:00
unsetenv ( SD_LISTEN_PID_ENV_VAR_NAME ) ;
unsetenv ( SD_LISTEN_FDS_ENV_VAR_NAME ) ;
return r ;
}
2011-05-13 12:45:46 +04:00
# endif
2011-05-13 13:34:12 +04:00
static int _open_socket ( daemon_state s )
{
2014-08-19 16:19:11 +04:00
int fd ;
2013-11-30 00:56:29 +04:00
int file_created = 0 ;
2012-06-21 23:19:28 +04:00
struct sockaddr_un sockaddr = { . sun_family = AF_UNIX } ;
2013-11-30 00:56:29 +04:00
struct stat buf ;
2011-05-13 13:34:12 +04:00
mode_t old_mask ;
( void ) dm_prepare_selinux_context ( s . socket_path , S_IFSOCK ) ;
old_mask = umask ( 0077 ) ;
/* Open local socket */
fd = socket ( PF_UNIX , SOCK_STREAM , 0 ) ;
if ( fd < 0 ) {
2011-05-15 15:02:29 +04:00
perror ( " Can't create local socket. " ) ;
2011-05-13 13:34:12 +04:00
goto error ;
}
2015-01-30 17:15:24 +03:00
/* Set non-blocking */
2012-06-20 12:49:16 +04:00
if ( fcntl ( fd , F_SETFL , fcntl ( fd , F_GETFL , 0 ) | O_NONBLOCK ) )
fprintf ( stderr , " setting O_NONBLOCK on socket fd %d failed: %s \n " , fd , strerror ( errno ) ) ;
2011-05-13 13:34:12 +04:00
2011-06-27 17:46:45 +04:00
fprintf ( stderr , " [D] creating %s \n " , s . socket_path ) ;
2024-04-04 19:42:24 +03:00
if ( ! _dm_strncpy ( sockaddr . sun_path , s . socket_path , sizeof ( sockaddr . sun_path ) ) ) {
2012-04-27 13:52:33 +04:00
fprintf ( stderr , " %s: daemon socket path too long. \n " , s . socket_path ) ;
goto error ;
}
2011-05-13 13:34:12 +04:00
if ( bind ( fd , ( struct sockaddr * ) & sockaddr , sizeof ( sockaddr ) ) ) {
2013-11-30 00:56:29 +04:00
if ( errno ! = EADDRINUSE ) {
perror ( " can't bind local socket " ) ;
goto error ;
}
/* Socket already exists. If it's stale, remove it. */
2015-05-13 15:42:09 +03:00
if ( lstat ( sockaddr . sun_path , & buf ) ) {
2013-11-30 00:56:29 +04:00
perror ( " stat failed " ) ;
goto error ;
}
2015-05-13 15:42:09 +03:00
if ( ! S_ISSOCK ( buf . st_mode ) ) {
2013-11-30 00:56:29 +04:00
fprintf ( stderr , " %s: not a socket \n " , sockaddr . sun_path ) ;
goto error ;
}
if ( buf . st_uid | | ( buf . st_mode & ( S_IRWXG | S_IRWXO ) ) ) {
fprintf ( stderr , " %s: unrecognised permissions \n " , sockaddr . sun_path ) ;
goto error ;
}
if ( ! connect ( fd , ( struct sockaddr * ) & sockaddr , sizeof ( sockaddr ) ) ) {
fprintf ( stderr , " Socket %s already in use \n " , sockaddr . sun_path ) ;
goto error ;
}
fprintf ( stderr , " removing stale socket %s \n " , sockaddr . sun_path ) ;
if ( unlink ( sockaddr . sun_path ) & & ( errno ! = ENOENT ) ) {
perror ( " unlink failed " ) ;
goto error ;
}
if ( bind ( fd , ( struct sockaddr * ) & sockaddr , sizeof ( sockaddr ) ) ) {
perror ( " local socket bind failed after unlink " ) ;
goto error ;
}
2011-05-13 13:34:12 +04:00
}
2013-11-30 00:56:29 +04:00
file_created = 1 ;
2011-05-13 13:34:12 +04:00
if ( listen ( fd , 1 ) ! = 0 ) {
2011-05-15 15:02:29 +04:00
perror ( " listen local " ) ;
2011-05-13 13:34:12 +04:00
goto error ;
}
out :
umask ( old_mask ) ;
( void ) dm_prepare_selinux_context ( NULL , 0 ) ;
return fd ;
error :
if ( fd > = 0 ) {
2012-01-26 01:30:27 +04:00
if ( close ( fd ) )
perror ( " close failed " ) ;
2013-11-30 00:56:29 +04:00
if ( file_created & & unlink ( s . socket_path ) )
2012-01-26 01:30:27 +04:00
perror ( " unlink failed " ) ;
2011-05-13 13:34:12 +04:00
fd = - 1 ;
}
goto out ;
}
2016-07-15 14:20:35 +03:00
static void _remove_lockfile ( const char * file )
2011-05-13 12:45:46 +04:00
{
if ( unlink ( file ) )
2011-05-15 15:02:29 +04:00
perror ( " unlink failed " ) ;
2011-05-13 12:45:46 +04:00
}
2013-04-15 11:43:30 +04:00
static void _daemonise ( daemon_state s )
2011-05-13 12:45:46 +04:00
{
int child_status ;
2024-04-13 01:39:43 +03:00
int fd ;
2011-05-13 12:45:46 +04:00
pid_t pid ;
struct timeval tval ;
sigset_t my_sigset ;
2024-04-13 01:39:43 +03:00
struct custom_fds custom_fds = {
/* Do not close fds preloaded by systemd! */
. out = ( _systemd_activation ) ? SD_FD_SOCKET_SERVER : - 1
} ;
2011-05-13 12:45:46 +04:00
2017-08-25 12:55:38 +03:00
if ( ( fd = open ( " /dev/null " , O_RDWR ) ) = = - 1 ) {
fprintf ( stderr , " Unable to open /dev/null. \n " ) ;
exit ( EXIT_FAILURE ) ;
}
2011-05-13 12:45:46 +04:00
sigemptyset ( & my_sigset ) ;
if ( sigprocmask ( SIG_SETMASK , & my_sigset , NULL ) < 0 ) {
fprintf ( stderr , " Unable to restore signals. \n " ) ;
exit ( EXIT_FAILURE ) ;
}
signal ( SIGTERM , & _exit_handler ) ;
switch ( pid = fork ( ) ) {
case - 1 :
perror ( " fork failed: " ) ;
exit ( EXIT_FAILURE ) ;
case 0 : /* Child */
break ;
default :
2017-08-25 12:55:38 +03:00
( void ) close ( fd ) ;
2011-05-13 12:45:46 +04:00
/* Wait for response from child */
2011-05-15 15:02:29 +04:00
while ( ! waitpid ( pid , & child_status , WNOHANG ) & & ! _shutdown_requested ) {
2011-05-13 12:45:46 +04:00
tval . tv_sec = 0 ;
tval . tv_usec = 250000 ; /* .25 sec */
select ( 0 , NULL , NULL , NULL , & tval ) ;
}
if ( _shutdown_requested ) /* Child has signaled it is ok - we can exit now */
exit ( 0 ) ;
2013-04-15 11:43:30 +04:00
switch ( WEXITSTATUS ( child_status ) ) {
case EXIT_ALREADYRUNNING :
fprintf ( stderr , " Failed to acquire lock on %s. Already running? \n " , s . pidfile ) ;
break ;
default :
/* Problem with child. Determine what it is by exit code */
fprintf ( stderr , " Child exited with code %d \n " , WEXITSTATUS ( child_status ) ) ;
}
2011-05-13 12:45:46 +04:00
exit ( WEXITSTATUS ( child_status ) ) ;
}
2017-09-06 12:47:53 +03:00
if ( chdir ( " / " ) ) {
perror ( " Cannot chdir to / " ) ;
2011-05-13 12:45:46 +04:00
exit ( 1 ) ;
2017-09-06 12:47:53 +03:00
}
2011-05-13 12:45:46 +04:00
2017-09-06 12:47:53 +03:00
if ( ( dup2 ( fd , STDIN_FILENO ) = = - 1 ) | |
( dup2 ( fd , STDOUT_FILENO ) = = - 1 ) | |
( dup2 ( fd , STDERR_FILENO ) = = - 1 ) ) {
perror ( " Error setting terminal FDs to /dev/null " ) ;
exit ( 2 ) ;
}
2017-08-25 12:55:38 +03:00
2017-09-06 12:47:53 +03:00
if ( ( fd > STDERR_FILENO ) & & close ( fd ) ) {
perror ( " Failed to close /dev/null descriptor " ) ;
exit ( 3 ) ;
}
2017-08-25 12:55:38 +03:00
2024-04-13 01:39:43 +03:00
daemon_close_stray_fds ( s . name , 0 , STDERR_FILENO , & custom_fds ) ;
2011-05-13 12:45:46 +04:00
setsid ( ) ;
2021-09-19 21:21:55 +03:00
/* coverity[leaked_handle] 'fd' handle is not leaking */
2011-05-13 12:45:46 +04:00
}
2011-08-31 16:39:58 +04:00
response daemon_reply_simple ( const char * id , . . . )
2011-06-27 17:58:11 +04:00
{
va_list ap ;
2011-09-17 18:49:18 +04:00
response res = { . cft = NULL } ;
2011-06-27 17:58:11 +04:00
va_start ( ap , id ) ;
2012-10-11 16:17:17 +04:00
buffer_init ( & res . buffer ) ;
if ( ! buffer_append_f ( & res . buffer , " response = %s " , id , NULL ) ) {
2011-06-27 17:58:11 +04:00
res . error = ENOMEM ;
2012-08-11 12:33:53 +04:00
goto end ;
}
2012-10-11 16:17:17 +04:00
if ( ! buffer_append_vf ( & res . buffer , ap ) ) {
2012-08-11 12:33:53 +04:00
res . error = ENOMEM ;
goto end ;
}
2011-09-17 18:49:18 +04:00
2012-08-11 12:33:53 +04:00
end :
va_end ( ap ) ;
2011-06-27 17:58:11 +04:00
return res ;
}
2016-07-15 14:20:35 +03:00
static response _builtin_handler ( daemon_state s , client_handle h , request r )
2012-02-24 03:52:11 +04:00
{
const char * rq = daemon_request_str ( r , " request " , " NONE " ) ;
2012-10-12 12:15:30 +04:00
response res = { . error = EPROTO } ;
2012-02-24 03:52:11 +04:00
if ( ! strcmp ( rq , " hello " ) ) {
return daemon_reply_simple ( " OK " , " protocol = %s " , s . protocol ? : " default " ,
2012-09-09 23:57:59 +04:00
" version = % " PRId64 , ( int64_t ) s . protocol_version , NULL ) ;
2012-02-24 03:52:11 +04:00
}
2012-10-11 16:17:17 +04:00
buffer_init ( & res . buffer ) ;
2012-02-24 03:52:11 +04:00
return res ;
}
2016-07-15 14:20:35 +03:00
static void * _client_thread ( void * state )
2011-06-14 06:34:18 +04:00
{
2014-06-09 03:50:57 +04:00
thread_state * ts = state ;
2012-10-11 16:17:17 +04:00
request req ;
2011-09-17 18:49:18 +04:00
response res ;
2012-10-11 16:17:17 +04:00
buffer_init ( & req . buffer ) ;
2011-06-14 06:34:18 +04:00
while ( 1 ) {
2014-06-09 03:50:57 +04:00
if ( ! buffer_read ( ts - > client . socket_fd , & req . buffer ) )
2011-06-14 06:34:18 +04:00
goto fail ;
2016-09-21 15:59:14 +03:00
req . cft = config_tree_from_string_without_dup_node_check ( req . buffer . mem ) ;
2012-08-23 13:01:00 +04:00
2011-07-18 18:46:54 +04:00
if ( ! req . cft )
2012-10-11 16:17:17 +04:00
fprintf ( stderr , " error parsing request: \n %s \n " , req . buffer . mem ) ;
2012-08-23 13:01:00 +04:00
else
2014-06-09 03:50:57 +04:00
daemon_log_cft ( ts - > s . log , DAEMON_LOG_WIRE , " <- " , req . cft - > root ) ;
2012-02-24 03:52:11 +04:00
2016-07-15 14:20:35 +03:00
res = _builtin_handler ( ts - > s , ts - > client , req ) ;
2012-02-24 03:52:11 +04:00
if ( res . error = = EPROTO ) /* Not a builtin, delegate to the custom handler. */
2014-06-09 03:50:57 +04:00
res = ts - > s . handler ( ts - > s , ts - > client , req ) ;
2011-06-14 06:34:18 +04:00
2012-10-11 16:17:17 +04:00
if ( ! res . buffer . mem ) {
2012-12-15 00:36:27 +04:00
if ( ! dm_config_write_node ( res . cft - > root , buffer_line , & res . buffer ) )
goto fail ;
2012-10-11 16:17:17 +04:00
if ( ! buffer_append ( & res . buffer , " \n \n " ) )
2012-02-28 18:25:37 +04:00
goto fail ;
2011-08-31 15:31:57 +04:00
dm_config_destroy ( res . cft ) ;
2011-06-14 06:34:18 +04:00
}
2012-01-15 14:33:41 +04:00
if ( req . cft )
dm_config_destroy ( req . cft ) ;
2012-10-11 16:17:17 +04:00
buffer_destroy ( & req . buffer ) ;
2012-01-15 14:33:41 +04:00
2014-06-09 03:50:57 +04:00
daemon_log_multi ( ts - > s . log , DAEMON_LOG_WIRE , " -> " , res . buffer . mem ) ;
buffer_write ( ts - > client . socket_fd , & res . buffer ) ;
2011-06-14 06:34:18 +04:00
2012-10-11 16:17:17 +04:00
buffer_destroy ( & res . buffer ) ;
2011-06-14 06:34:18 +04:00
}
fail :
/* TODO what should we really do here? */
2014-06-09 03:50:57 +04:00
if ( close ( ts - > client . socket_fd ) )
2012-03-02 01:12:37 +04:00
perror ( " close " ) ;
2012-10-11 16:17:17 +04:00
buffer_destroy ( & req . buffer ) ;
2014-06-09 03:50:57 +04:00
ts - > active = 0 ;
2011-06-14 06:34:18 +04:00
return NULL ;
}
2017-10-18 17:57:46 +03:00
static int _handle_connect ( daemon_state s )
2011-06-14 06:34:18 +04:00
{
2014-06-09 03:50:57 +04:00
thread_state * ts ;
2011-06-14 06:34:18 +04:00
struct sockaddr_un sockaddr ;
2011-09-17 18:49:18 +04:00
client_handle client = { . thread_id = 0 } ;
2011-06-14 06:34:18 +04:00
socklen_t sl = sizeof ( sockaddr ) ;
2011-09-17 18:49:18 +04:00
client . socket_fd = accept ( s . socket_fd , ( struct sockaddr * ) & sockaddr , & sl ) ;
2016-06-20 21:19:02 +03:00
if ( client . socket_fd < 0 ) {
2019-11-08 15:07:06 +03:00
if ( errno ! = EAGAIN )
2017-07-06 16:53:10 +03:00
ERROR ( & s , " Failed to accept connection: %s. " , strerror ( errno ) ) ;
2019-11-08 15:07:06 +03:00
goto bad ;
}
if ( _shutdown_requested ) {
ERROR ( & s , " Shutdown requested. " ) ;
goto bad ;
2016-06-20 21:19:02 +03:00
}
2011-06-14 06:34:18 +04:00
2019-11-08 15:07:06 +03:00
if ( fcntl ( client . socket_fd , F_SETFD , FD_CLOEXEC ) )
2014-11-12 11:50:59 +03:00
WARN ( & s , " setting CLOEXEC on client socket fd %d failed " , client . socket_fd ) ;
2018-06-08 15:40:53 +03:00
if ( ! ( ts = malloc ( sizeof ( thread_state ) ) ) ) {
2014-06-09 03:50:57 +04:00
ERROR ( & s , " Failed to allocate thread state " ) ;
2019-11-08 15:07:06 +03:00
goto bad ;
2012-12-15 00:31:22 +04:00
}
2011-06-14 06:34:18 +04:00
2014-06-09 03:50:57 +04:00
ts - > next = s . threads - > next ;
s . threads - > next = ts ;
2011-06-14 06:34:18 +04:00
2014-06-09 03:50:57 +04:00
ts - > active = 1 ;
ts - > s = s ;
ts - > client = client ;
2011-09-17 18:49:18 +04:00
2017-07-06 16:53:10 +03:00
if ( ( errno = pthread_create ( & ts - > client . thread_id , NULL , _client_thread , ts ) ) ) {
ERROR ( & s , " Failed to create client thread: %s. " , strerror ( errno ) ) ;
2019-11-08 15:07:06 +03:00
ts - > active = 0 ;
goto bad ;
2016-06-20 21:19:02 +03:00
}
2012-01-15 14:33:41 +04:00
2011-06-14 06:34:18 +04:00
return 1 ;
2019-11-08 15:07:06 +03:00
bad :
if ( ( client . socket_fd > = 0 ) & & close ( client . socket_fd ) )
perror ( " close " ) ;
return 0 ;
2011-06-14 06:34:18 +04:00
}
2016-07-15 14:20:35 +03:00
static void _reap ( daemon_state s , int waiting )
2014-06-09 03:50:57 +04:00
{
thread_state * last = s . threads , * ts = last - > next ;
void * rv ;
while ( ts ) {
2014-06-19 13:33:11 +04:00
if ( waiting | | ! ts - > active ) {
2021-04-01 12:32:47 +03:00
if ( ts - > client . thread_id ) {
if ( ( errno = pthread_kill ( ts - > client . thread_id , SIGTERM ) ) & &
( errno ! = ESRCH ) )
ERROR ( & s , " pthread_kill failed for pid %ld " ,
( long ) ts - > client . thread_id ) ;
if ( ( errno = pthread_join ( ts - > client . thread_id , & rv ) ) )
ERROR ( & s , " pthread_join failed: %s " , strerror ( errno ) ) ;
}
2014-06-09 03:50:57 +04:00
last - > next = ts - > next ;
2018-06-08 15:40:53 +03:00
free ( ts ) ;
2014-06-09 03:50:57 +04:00
} else
last = ts ;
ts = last - > next ;
}
}
2011-05-15 15:02:29 +04:00
void daemon_start ( daemon_state s )
2011-05-13 12:45:46 +04:00
{
2011-05-13 13:34:12 +04:00
int failed = 0 ;
2012-10-12 12:15:30 +04:00
log_state _log = { { 0 } } ;
2014-06-09 03:50:57 +04:00
thread_state _threads = { . next = NULL } ;
2015-04-29 17:59:30 +03:00
unsigned timeout_count = 0 ;
2015-08-18 16:47:07 +03:00
fd_set in ;
2019-04-10 13:50:53 +03:00
sigset_t new_set , old_set ;
int ret ;
2012-10-12 12:15:30 +04:00
2011-05-13 12:45:46 +04:00
/*
* Switch to C locale to avoid reading large locale - archive file used by
* some glibc ( on some distributions it takes over 100 MB ) . Some daemons
* need to use mlockall ( ) .
*/
2013-01-22 14:25:02 +04:00
if ( setenv ( " LC_ALL " , " C " , 1 ) )
perror ( " Cannot set LC_ALL to C " ) ;
2011-05-13 12:45:46 +04:00
2013-11-13 17:56:29 +04:00
# ifdef __linux__
2012-02-28 17:05:21 +04:00
_systemd_activation = _systemd_handover ( & s ) ;
# endif
2011-05-13 12:45:46 +04:00
if ( ! s . foreground )
2013-04-15 11:43:30 +04:00
_daemonise ( s ) ;
2011-05-13 12:45:46 +04:00
2012-08-08 11:41:01 +04:00
s . log = & _log ;
s . log - > name = s . name ;
2014-06-09 03:50:57 +04:00
s . threads = & _threads ;
2012-08-08 11:41:01 +04:00
/* Log important things to syslog by default. */
daemon_log_enable ( s . log , DAEMON_LOG_OUTLET_SYSLOG , DAEMON_LOG_FATAL , 1 ) ;
daemon_log_enable ( s . log , DAEMON_LOG_OUTLET_SYSLOG , DAEMON_LOG_ERROR , 1 ) ;
2011-05-13 12:45:46 +04:00
2012-10-09 22:22:30 +04:00
if ( s . pidfile ) {
( void ) dm_prepare_selinux_context ( s . pidfile , S_IFREG ) ;
2011-05-13 12:45:46 +04:00
2012-10-09 22:22:30 +04:00
/*
* NB . Take care to not keep stale locks around . Best not exit ( . . . )
* after this point .
*/
2015-05-14 16:43:43 +03:00
if ( dm_create_lockfile ( s . pidfile ) = = 0 ) {
ERROR ( & s , " Failed to acquire lock on %s. Already running? \n " , s . pidfile ) ;
2013-04-15 11:43:30 +04:00
exit ( EXIT_ALREADYRUNNING ) ;
2015-05-14 16:43:43 +03:00
}
2011-05-13 12:45:46 +04:00
2012-10-09 22:22:30 +04:00
( void ) dm_prepare_selinux_context ( NULL , 0 ) ;
}
2011-05-13 12:45:46 +04:00
/* Set normal exit signals to request shutdown instead of dying. */
signal ( SIGINT , & _exit_handler ) ;
signal ( SIGHUP , & _exit_handler ) ;
signal ( SIGQUIT , & _exit_handler ) ;
2011-06-14 06:34:18 +04:00
signal ( SIGTERM , & _exit_handler ) ;
2011-08-30 19:42:56 +04:00
signal ( SIGALRM , & _exit_handler ) ;
2011-06-14 06:34:18 +04:00
signal ( SIGPIPE , SIG_IGN ) ;
2011-05-13 12:45:46 +04:00
2013-11-13 17:56:29 +04:00
# ifdef __linux__
2012-02-28 17:05:21 +04:00
/* Systemd has adjusted oom killer for us already */
if ( s . avoid_oom & & ! _systemd_activation & & ! _protect_against_oom_killer ( ) )
2012-08-08 11:41:01 +04:00
ERROR ( & s , " Failed to protect against OOM killer " ) ;
2011-05-13 12:45:46 +04:00
# endif
2012-02-28 17:05:21 +04:00
if ( ! _systemd_activation & & s . socket_path ) {
2011-05-13 13:34:12 +04:00
s . socket_fd = _open_socket ( s ) ;
if ( s . socket_fd < 0 )
failed = 1 ;
}
2015-01-30 17:15:24 +03:00
/* Set Close-on-exec */
2015-05-13 15:42:09 +03:00
if ( ! failed & & fcntl ( s . socket_fd , F_SETFD , 1 ) )
2015-05-14 16:50:16 +03:00
ERROR ( & s , " setting CLOEXEC on socket fd %d failed: %s \n " , s . socket_fd , strerror ( errno ) ) ;
2015-01-30 17:15:24 +03:00
2011-05-13 12:45:46 +04:00
/* Signal parent, letting them know we are ready to go. */
if ( ! s . foreground )
kill ( getppid ( ) , SIGTERM ) ;
2019-04-10 13:50:53 +03:00
/* * Use daemon_main for daemon-specific init and polling, or
2015-06-24 20:16:26 +03:00
* use daemon_init for daemon - specific init and generic lib polling .
*/
2014-12-02 23:08:58 +03:00
if ( s . daemon_main ) {
if ( ! s . daemon_main ( & s ) )
failed = 1 ;
goto out ;
}
2011-07-18 18:46:54 +04:00
if ( s . daemon_init )
2012-10-15 05:06:27 +04:00
if ( ! s . daemon_init ( & s ) )
failed = 1 ;
2011-07-18 18:46:54 +04:00
2019-04-10 13:50:53 +03:00
if ( s . socket_fd > = FD_SETSIZE )
failed = 1 ; /* FD out of available selectable set */
sigfillset ( & new_set ) ;
2019-11-14 18:56:12 +03:00
if ( sigprocmask ( SIG_SETMASK , NULL , & old_set ) )
perror ( " sigprocmask error " ) ;
2019-04-10 13:50:53 +03:00
2021-04-01 12:32:47 +03:00
while ( ! failed & & ! _shutdown_requested ) {
2015-04-29 17:59:30 +03:00
_reset_timeout ( s ) ;
2011-06-14 06:34:18 +04:00
FD_ZERO ( & in ) ;
FD_SET ( s . socket_fd , & in ) ;
2019-04-10 13:50:53 +03:00
2019-11-08 17:42:42 +03:00
if ( sigprocmask ( SIG_SETMASK , & new_set , NULL ) )
perror ( " sigprocmask error " ) ;
2019-04-10 13:50:53 +03:00
ret = pselect ( s . socket_fd + 1 , & in , NULL , NULL , _get_timeout ( s ) , & old_set ) ;
2019-11-08 17:42:42 +03:00
if ( sigprocmask ( SIG_SETMASK , & old_set , NULL ) )
perror ( " sigprocmask error " ) ;
2019-04-10 13:50:53 +03:00
if ( ret < 0 ) {
2020-09-12 14:15:12 +03:00
if ( ( errno ! = EINTR ) & & ( errno ! = EAGAIN ) )
2019-04-10 13:50:53 +03:00
perror ( " select error " ) ;
2021-04-01 12:32:47 +03:00
_reap ( s , 0 ) ;
2019-04-10 13:50:53 +03:00
continue ;
}
2015-04-29 17:59:30 +03:00
if ( FD_ISSET ( s . socket_fd , & in ) ) {
timeout_count = 0 ;
2017-10-18 17:57:46 +03:00
_handle_connect ( s ) ;
2015-04-29 17:59:30 +03:00
}
2016-07-15 14:20:35 +03:00
_reap ( s , 0 ) ;
2015-04-29 17:59:30 +03:00
/* s.idle == NULL equals no shutdown on timeout */
if ( _is_idle ( s ) ) {
DEBUGLOG ( & s , " timeout occured " ) ;
if ( + + timeout_count > = _get_max_timeouts ( s ) ) {
INFO ( & s , " Inactive for %d seconds. Exiting. " , timeout_count ) ;
break ;
}
}
2011-05-13 12:45:46 +04:00
}
2021-04-01 12:32:47 +03:00
if ( _shutdown_requested )
INFO ( & s , " %s shutdown requested " , s . name ) ;
2014-06-09 03:50:57 +04:00
INFO ( & s , " %s waiting for client threads to finish " , s . name ) ;
2016-07-15 14:20:35 +03:00
_reap ( s , 1 ) ;
2014-12-02 23:08:58 +03:00
out :
2012-03-08 18:54:05 +04:00
/* If activated by systemd, do not unlink the socket - systemd takes care of that! */
if ( ! _systemd_activation & & s . socket_fd > = 0 )
2019-06-25 16:11:05 +03:00
if ( s . socket_path & & unlink ( s . socket_path ) )
2012-01-26 01:30:27 +04:00
perror ( " unlink error " ) ;
2011-06-14 06:34:18 +04:00
2016-07-15 16:42:01 +03:00
if ( s . socket_fd > = 0 )
if ( close ( s . socket_fd ) )
2016-07-15 23:55:38 +03:00
perror ( " socket close " ) ;
2016-07-15 16:42:01 +03:00
2011-07-18 18:46:54 +04:00
if ( s . daemon_fini )
2012-10-15 05:06:27 +04:00
if ( ! s . daemon_fini ( & s ) )
failed = 1 ;
2011-07-18 18:46:54 +04:00
2012-08-08 11:41:01 +04:00
INFO ( & s , " %s shutting down " , s . name ) ;
closelog ( ) ; /* FIXME */
2012-10-09 22:22:30 +04:00
if ( s . pidfile )
2016-07-15 14:20:35 +03:00
_remove_lockfile ( s . pidfile ) ;
2011-05-13 13:34:12 +04:00
if ( failed )
exit ( 1 ) ;
2011-05-13 12:45:46 +04:00
}