From f265d201c8430d8bf353a633ce81744c09ee790b Mon Sep 17 00:00:00 2001 From: erdgeist <> Date: Tue, 12 Mar 2013 21:21:51 +0000 Subject: Fix check for working command channel. Workaround bug in pidfile_open by providing an unused pid_t* --- jaildaemon.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/jaildaemon.c b/jaildaemon.c index 918f01b..ef37ed8 100644 --- a/jaildaemon.c +++ b/jaildaemon.c @@ -46,6 +46,7 @@ typedef struct { /* Forward declarations */ static void term_handler( int signal ); static void kill_all_probes( void ); +static void remove_pidfile( void ); static int check_for_jail( int jid ); static int copy_daemontask( daemon_task ** out, daemon_task * const in ); static int add_task_to_kqueue( int kq, daemon_task * task_in ); @@ -290,7 +291,9 @@ static void kill_all_probes( void ) { g_probes_size = 0; free( g_probes ); g_probes = 0; +} +static void remove_pidfile( void ) { pidfile_remove( g_pidfilehandle ); } @@ -388,7 +391,7 @@ int main( int argc, char **argv ) { i=1; while(i) { - switch( getopt( argc, argv, "Drt:c:j:p:f:" ) ) { + switch( getopt( argc, argv, "DFrt:c:j:p:f:" ) ) { case -1: i=0; break; case 'D': o_daemonize = 1; break; case 'r': o_respawn = 1; break; @@ -397,6 +400,7 @@ int main( int argc, char **argv ) { case 'j': o_jid = strtol( optarg, 0, 0 ); break; case 'p': o_pidfile = optarg; break; case 'f': o_uds_path = optarg; break; + case 'F': o_force_daemon = 1; break; case '?': usage( argv[0]); exit(0); break; } } @@ -404,13 +408,14 @@ int main( int argc, char **argv ) { /* Daemonize and start a fork slave while there is no file descriptors or initialized memory yet. Communicate with this slave via socketpair */ if( o_daemonize ) { - g_pidfilehandle = pidfile_open(o_pidfile, 0600, NULL ); + pid_t second_pid; + g_pidfilehandle = pidfile_open(o_pidfile, 0600, &second_pid ); if (!g_pidfilehandle) { - if (errno == EEXIST) + if (errno == EEXIST) exerr( "jaildaemon already running." ); - /* If we cannot create pidfile from other reasons, only warn. */ - warn( "Cannot open or create pidfile" ); + /* If we cannot create pidfile from other reasons, only warn. */ + warn( "Cannot open or create pidfile" ); } if( daemon(1,0) == -1 ) { @@ -421,6 +426,7 @@ int main( int argc, char **argv ) { g_fork_slave_fd = fork_fork_slave( ); + atexit( remove_pidfile ); openlog( "jaildaemon", 0, LOG_DAEMON ); setlogmask(LOG_UPTO(LOG_INFO)); g_whoami = IAM_DAEMON; @@ -488,12 +494,12 @@ int main( int argc, char **argv ) { /* Send test DGRAM through the unix domain socket. If this succeeds, there likely is another daemon already listening. You have to force the daemon to start in this case */ - if( sendto( g_uds, g_ipc_packet, IPC_PACKETSIZE, 0, + if( sendto( g_uds, g_ipc_packet, 0, 0, (struct sockaddr*)&addr, sizeof(addr) ) == 0 ) { - /* TODO: Force not implemented yet */ + if( !o_force_daemon ) exerr( "Found command channel. Refusing to overwrite a working one." - " Another server may be running. Force with -f."); + " Another server may be running. Force with -F."); else warn( "Forcing start of daemon despite working command channel." ); } -- cgit v1.2.3