diff options
author | erdgeist <> | 2013-03-12 21:21:51 +0000 |
---|---|---|
committer | erdgeist <> | 2013-03-12 21:21:51 +0000 |
commit | f265d201c8430d8bf353a633ce81744c09ee790b (patch) | |
tree | 53976e1f5f4be504fd59529bd63f019dabe5f705 | |
parent | 696c99c61c51895465666b23ad1ee768e5237bc3 (diff) |
Fix check for working command channel. Workaround bug in pidfile_open by providing an unused pid_t*
-rw-r--r-- | jaildaemon.c | 22 |
1 files 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 { | |||
46 | /* Forward declarations */ | 46 | /* Forward declarations */ |
47 | static void term_handler( int signal ); | 47 | static void term_handler( int signal ); |
48 | static void kill_all_probes( void ); | 48 | static void kill_all_probes( void ); |
49 | static void remove_pidfile( void ); | ||
49 | static int check_for_jail( int jid ); | 50 | static int check_for_jail( int jid ); |
50 | static int copy_daemontask( daemon_task ** out, daemon_task * const in ); | 51 | static int copy_daemontask( daemon_task ** out, daemon_task * const in ); |
51 | static int add_task_to_kqueue( int kq, daemon_task * task_in ); | 52 | static int add_task_to_kqueue( int kq, daemon_task * task_in ); |
@@ -290,7 +291,9 @@ static void kill_all_probes( void ) { | |||
290 | g_probes_size = 0; | 291 | g_probes_size = 0; |
291 | free( g_probes ); | 292 | free( g_probes ); |
292 | g_probes = 0; | 293 | g_probes = 0; |
294 | } | ||
293 | 295 | ||
296 | static void remove_pidfile( void ) { | ||
294 | pidfile_remove( g_pidfilehandle ); | 297 | pidfile_remove( g_pidfilehandle ); |
295 | } | 298 | } |
296 | 299 | ||
@@ -388,7 +391,7 @@ int main( int argc, char **argv ) { | |||
388 | 391 | ||
389 | i=1; | 392 | i=1; |
390 | while(i) { | 393 | while(i) { |
391 | switch( getopt( argc, argv, "Drt:c:j:p:f:" ) ) { | 394 | switch( getopt( argc, argv, "DFrt:c:j:p:f:" ) ) { |
392 | case -1: i=0; break; | 395 | case -1: i=0; break; |
393 | case 'D': o_daemonize = 1; break; | 396 | case 'D': o_daemonize = 1; break; |
394 | case 'r': o_respawn = 1; break; | 397 | case 'r': o_respawn = 1; break; |
@@ -397,6 +400,7 @@ int main( int argc, char **argv ) { | |||
397 | case 'j': o_jid = strtol( optarg, 0, 0 ); break; | 400 | case 'j': o_jid = strtol( optarg, 0, 0 ); break; |
398 | case 'p': o_pidfile = optarg; break; | 401 | case 'p': o_pidfile = optarg; break; |
399 | case 'f': o_uds_path = optarg; break; | 402 | case 'f': o_uds_path = optarg; break; |
403 | case 'F': o_force_daemon = 1; break; | ||
400 | case '?': usage( argv[0]); exit(0); break; | 404 | case '?': usage( argv[0]); exit(0); break; |
401 | } | 405 | } |
402 | } | 406 | } |
@@ -404,13 +408,14 @@ int main( int argc, char **argv ) { | |||
404 | /* Daemonize and start a fork slave while there is no file descriptors or | 408 | /* Daemonize and start a fork slave while there is no file descriptors or |
405 | initialized memory yet. Communicate with this slave via socketpair */ | 409 | initialized memory yet. Communicate with this slave via socketpair */ |
406 | if( o_daemonize ) { | 410 | if( o_daemonize ) { |
407 | g_pidfilehandle = pidfile_open(o_pidfile, 0600, NULL ); | 411 | pid_t second_pid; |
412 | g_pidfilehandle = pidfile_open(o_pidfile, 0600, &second_pid ); | ||
408 | 413 | ||
409 | if (!g_pidfilehandle) { | 414 | if (!g_pidfilehandle) { |
410 | if (errno == EEXIST) | 415 | if (errno == EEXIST) |
411 | exerr( "jaildaemon already running." ); | 416 | exerr( "jaildaemon already running." ); |
412 | /* If we cannot create pidfile from other reasons, only warn. */ | 417 | /* If we cannot create pidfile from other reasons, only warn. */ |
413 | warn( "Cannot open or create pidfile" ); | 418 | warn( "Cannot open or create pidfile" ); |
414 | } | 419 | } |
415 | 420 | ||
416 | if( daemon(1,0) == -1 ) { | 421 | if( daemon(1,0) == -1 ) { |
@@ -421,6 +426,7 @@ int main( int argc, char **argv ) { | |||
421 | 426 | ||
422 | g_fork_slave_fd = fork_fork_slave( ); | 427 | g_fork_slave_fd = fork_fork_slave( ); |
423 | 428 | ||
429 | atexit( remove_pidfile ); | ||
424 | openlog( "jaildaemon", 0, LOG_DAEMON ); | 430 | openlog( "jaildaemon", 0, LOG_DAEMON ); |
425 | setlogmask(LOG_UPTO(LOG_INFO)); | 431 | setlogmask(LOG_UPTO(LOG_INFO)); |
426 | g_whoami = IAM_DAEMON; | 432 | g_whoami = IAM_DAEMON; |
@@ -488,12 +494,12 @@ int main( int argc, char **argv ) { | |||
488 | /* Send test DGRAM through the unix domain socket. If this succeeds, there | 494 | /* Send test DGRAM through the unix domain socket. If this succeeds, there |
489 | likely is another daemon already listening. You have to force the daemon | 495 | likely is another daemon already listening. You have to force the daemon |
490 | to start in this case */ | 496 | to start in this case */ |
491 | if( sendto( g_uds, g_ipc_packet, IPC_PACKETSIZE, 0, | 497 | if( sendto( g_uds, g_ipc_packet, 0, 0, |
492 | (struct sockaddr*)&addr, sizeof(addr) ) == 0 ) { | 498 | (struct sockaddr*)&addr, sizeof(addr) ) == 0 ) { |
493 | /* TODO: Force not implemented yet */ | 499 | |
494 | if( !o_force_daemon ) | 500 | if( !o_force_daemon ) |
495 | exerr( "Found command channel. Refusing to overwrite a working one." | 501 | exerr( "Found command channel. Refusing to overwrite a working one." |
496 | " Another server may be running. Force with -f."); | 502 | " Another server may be running. Force with -F."); |
497 | else | 503 | else |
498 | warn( "Forcing start of daemon despite working command channel." ); | 504 | warn( "Forcing start of daemon despite working command channel." ); |
499 | } | 505 | } |