summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorerdgeist <>2013-03-12 19:04:26 +0000
committererdgeist <>2013-03-12 19:04:26 +0000
commit1bb78bf47650bcaccb359ae774554bcbc10934ab (patch)
tree7a935b987a22bb2a1fb281ac9f53a4dc340524ef
parent1bea5673c414dc20a51d33ea123e9e1e17aa09ce (diff)
Use the pidfile library to write the pidfile
-rw-r--r--jaildaemon.c37
1 files changed, 24 insertions, 13 deletions
diff --git a/jaildaemon.c b/jaildaemon.c
index 4b6c923..918f01b 100644
--- a/jaildaemon.c
+++ b/jaildaemon.c
@@ -17,12 +17,15 @@
17#include <signal.h> 17#include <signal.h>
18#include <unistd.h> 18#include <unistd.h>
19#include <syslog.h> 19#include <syslog.h>
20#include <libutil.h>
20 21
21#define IPC_PACKETSIZE 4096 22#define IPC_PACKETSIZE 4096
22#define MAGIC_EXIT_CODE 42 23#define MAGIC_EXIT_CODE 42
23enum { IAM_DAEMON, IAM_CLIENT, IAM_FORKSLAVE }; 24enum { IAM_DAEMON, IAM_CLIENT, IAM_FORKSLAVE };
24static int g_uds; 25static int g_uds;
25static int g_whoami = IAM_CLIENT; 26static int g_whoami = IAM_CLIENT;
27static struct
28 pidfh * g_pidfilehandle;
26static int g_fork_slave_fd; 29static int g_fork_slave_fd;
27static char g_ipc_packet[IPC_PACKETSIZE]; 30static char g_ipc_packet[IPC_PACKETSIZE];
28static int * const g_ipc_packet_int = (int*)g_ipc_packet; 31static int * const g_ipc_packet_int = (int*)g_ipc_packet;
@@ -144,6 +147,8 @@ static int fork_fork_slave( ) {
144 /* I am child, close master's socket fd */ 147 /* I am child, close master's socket fd */
145 close( sockets[0] ); 148 close( sockets[0] );
146 g_whoami = IAM_FORKSLAVE; 149 g_whoami = IAM_FORKSLAVE;
150 pidfile_close( g_pidfilehandle );
151 g_pidfilehandle = NULL;
147 fork_slave( sockets[1] ); /* Never returns */ 152 fork_slave( sockets[1] ); /* Never returns */
148 exit(0); 153 exit(0);
149 default: 154 default:
@@ -285,6 +290,8 @@ static void kill_all_probes( void ) {
285 g_probes_size = 0; 290 g_probes_size = 0;
286 free( g_probes ); 291 free( g_probes );
287 g_probes = 0; 292 g_probes = 0;
293
294 pidfile_remove( g_pidfilehandle );
288} 295}
289 296
290static int add_task_to_kqueue( int kq, daemon_task * t_in ) { 297static int add_task_to_kqueue( int kq, daemon_task * t_in ) {
@@ -394,23 +401,27 @@ int main( int argc, char **argv ) {
394 } 401 }
395 } 402 }
396 403
397 /* Start a fork slave while there is no file descriptors or initialized 404 /* Daemonize and start a fork slave while there is no file descriptors or
398 memory yet. Communicate with this slave via socketpair */ 405 initialized memory yet. Communicate with this slave via socketpair */
399 if( o_daemonize ) { 406 if( o_daemonize ) {
400 if( daemon(1,0) == -1 ) 407 g_pidfilehandle = pidfile_open(o_pidfile, 0600, NULL );
401 exerr( "daemonzing" ); 408
402 g_fork_slave_fd = fork_fork_slave( ); 409 if (!g_pidfilehandle) {
410 if (errno == EEXIST)
411 exerr( "jaildaemon already running." );
412 /* If we cannot create pidfile from other reasons, only warn. */
413 warn( "Cannot open or create pidfile" );
414 }
403 415
404 /* When we're supposed to write a pidfile, just do it */ 416 if( daemon(1,0) == -1 ) {
405 if( o_pidfile ) { 417 pidfile_remove(g_pidfilehandle);
406 FILE *fp = fopen( o_pidfile, "w"); 418 exerr( "daemonzing" );
407 if (!fp)
408 exerr("opening pid file");
409 fprintf(fp, "%d\n", (int)getpid());
410 fclose(fp);
411 } 419 }
420 pidfile_write(g_pidfilehandle);
421
422 g_fork_slave_fd = fork_fork_slave( );
412 423
413 openlog( "jaildaemon", 0, LOG_DAEMON ); 424 openlog( "jaildaemon", 0, LOG_DAEMON );
414 setlogmask(LOG_UPTO(LOG_INFO)); 425 setlogmask(LOG_UPTO(LOG_INFO));
415 g_whoami = IAM_DAEMON; 426 g_whoami = IAM_DAEMON;
416 427