[pisa-src] r1141 - in trunk: include/global.h include/util.h libpisa/util.c pisacd/cdmain.c pisand/ndmain.c pisasd/sdmain.c

  • From: Thomas Jansen <mithi@xxxxxxxxx>
  • To: pisa-src@xxxxxxxxxxxxx
  • Date: Wed, 14 Oct 2009 15:21:45 +0200

Author: tjansen
Date: Wed Oct 14 15:21:45 2009
New Revision: 1141

Log:
Pisa{s,c,n}d now create and honor a lock file.

This prevents the daemons from being started twice by accident, which happens
easily if the first one is started by the initscript and the second one by
hand.

The functionality was shamelessly copied from HIPL before, but not used in the
daemons. If you have to copy from another source, at least rename the
functions consistently or - even better - write the code yourself!

Modified:
   trunk/include/global.h
   trunk/include/util.h
   trunk/libpisa/util.c
   trunk/pisacd/cdmain.c
   trunk/pisand/ndmain.c
   trunk/pisasd/sdmain.c

Modified: trunk/include/global.h
==============================================================================
--- trunk/include/global.h      Wed Oct 14 14:42:01 2009        (r1140)
+++ trunk/include/global.h      Wed Oct 14 15:21:45 2009        (r1141)
@@ -175,6 +175,10 @@
 #define PISAND_CONFFILE                "pisand.conf"
 #define PISAND_CONFFILE_FULLPATH       PISA_CONFDIR_PATH "/" PISAND_CONFFILE
 
+#define PISACD_LOCK_FILE       "/var/lock/pisacd.lock"
+#define PISASD_LOCK_FILE       "/var/lock/pisasd.lock"
+#define PISAND_LOCK_FILE       "/var/lock/pisand.lock"
+
 #define PISA_AUTHORIZED_HOSTS_CONFFILE         "authorized_hosts.cfg"
 #define PISA_AUTHORIZED_HOSTS_CONFFILE_FULLPATH        PISA_CONFDIR_PATH "/" 
PISA_AUTHORIZED_HOSTS_CONFFILE
 

Modified: trunk/include/util.h
==============================================================================
--- trunk/include/util.h        Wed Oct 14 14:42:01 2009        (r1140)
+++ trunk/include/util.h        Wed Oct 14 15:21:45 2009        (r1141)
@@ -139,8 +139,8 @@
 int pisa_make_hipd_run(void);
 int pisa_check_if_hipd_runs(void);
 
-int hip_create_lock_file(char *filename, int killold);
-int hip_remove_lock_file(char *filename);
+int pisa_create_lock_file(char *filename, int killold);
+int pisa_remove_lock_file(char *filename);
 
 void trim_trailing_spaces(char *instr);
 

Modified: trunk/libpisa/util.c
==============================================================================
--- trunk/libpisa/util.c        Wed Oct 14 14:42:01 2009        (r1140)
+++ trunk/libpisa/util.c        Wed Oct 14 15:21:45 2009        (r1141)
@@ -614,8 +614,7 @@
        if (lockf(fd, F_TLOCK, 0) < 0)
        {
                PISA_IFEL(!killold, -12,
-                        "\nHIP daemon already running with pid %d\n"
-                        "Give: -k option to kill old daemon.\n", old_pid);
+                        "PISA daemon already running with pid %d\n", old_pid);
 
                PISA_INFO("\nDaemon is already running with pID %d\n"
                         "-k option given, terminating old one...\n", old_pid);

Modified: trunk/pisacd/cdmain.c
==============================================================================
--- trunk/pisacd/cdmain.c       Wed Oct 14 14:42:01 2009        (r1140)
+++ trunk/pisacd/cdmain.c       Wed Oct 14 15:21:45 2009        (r1141)
@@ -160,7 +160,6 @@
 static void cd_init(int argc, char *argv[])
 {
        /* Set signal handler for each signal */
-
        signal(SIGTERM, cd_quit);
        signal(SIGINT, cd_quit);
        signal(SIGQUIT, cd_quit);
@@ -172,6 +171,9 @@
        signal(SIGALRM, cd_handle_sigalarm);
 #endif /* REMOVE_PREAUTH_CODE */
 
+       /* Check and create lockfile */
+       pisa_create_lock_file(PISACD_LOCK_FILE, 0);
+
        /* Set default values in context and config */
        cdctx_init(&cd_ctx);
        cdconf_init(&cd_cfg);
@@ -300,6 +302,9 @@
 
        pisa_ctrlhandler_cleanup(&cd_ctx.ctrlhandlers);
 
+       /* Remove lockfile */
+       pisa_remove_lock_file(PISACD_LOCK_FILE);
+
 #ifdef CONFIG_PISA_PERFORMANCE
        cd_perf_destroy();
 #endif

Modified: trunk/pisand/ndmain.c
==============================================================================
--- trunk/pisand/ndmain.c       Wed Oct 14 14:42:01 2009        (r1140)
+++ trunk/pisand/ndmain.c       Wed Oct 14 15:21:45 2009        (r1141)
@@ -125,7 +125,6 @@
 static void nd_init(int argc, char *argv[])
 {
        /* Set signal handler for each signal */
-
        signal(SIGKILL, nd_quit);
        signal(SIGTERM, nd_quit);
        signal(SIGINT, nd_quit);
@@ -137,6 +136,9 @@
        signal(SIGSEGV, nd_quit);
        signal(SIGALRM, nd_handle_sigalarm);
 
+       /* Check and create lockfile */
+       pisa_create_lock_file(PISAND_LOCK_FILE, 0);
+
        /* Set default values in context and config */
        ndctx_init(&nd_ctx);
        ndconf_init(&nd_cfg);
@@ -715,6 +717,9 @@
        close(nd_ctx.fd_nesrv);
        close(nd_ctx.fd_necli);
 
+       /* Remove lockfile */
+       pisa_remove_lock_file(PISAND_LOCK_FILE);
+
        nd_perf_destroy();
 
        exit(EXIT_FAILURE);

Modified: trunk/pisasd/sdmain.c
==============================================================================
--- trunk/pisasd/sdmain.c       Wed Oct 14 14:42:01 2009        (r1140)
+++ trunk/pisasd/sdmain.c       Wed Oct 14 15:21:45 2009        (r1141)
@@ -135,6 +135,9 @@
        signal(SIGBUS, sd_quit);
        signal(SIGALRM, sd_handle_sigalarm);
 
+       /* Check and create lockfile */
+       pisa_create_lock_file(PISASD_LOCK_FILE, 0);
+
        /* Set default values in context and config */
        sdctx_init(&sd_ctx);
        sdconf_init(&sd_cfg);
@@ -270,6 +273,9 @@
        /* Disable IPv4 forwarding */
        pisa_forwarding_stop();
 
+       /* Remove lockfile */
+       pisa_remove_lock_file(PISASD_LOCK_FILE);
+
        /* TODO clear iptables (at least/most from nat stuff) */
 
 #ifdef CONFIG_PISA_LOGGING

Other related posts:

  • » [pisa-src] r1141 - in trunk: include/global.h include/util.h libpisa/util.c pisacd/cdmain.c pisand/ndmain.c pisasd/sdmain.c - Thomas Jansen