]> git.openfabrics.org - ~ardavis/dapl.git/commitdiff
Catch SIGINT/SIGTERM and cleanup the lock file.
authorJianxin Xiong <jianxin.xiong@intel.com>
Fri, 28 Sep 2012 03:28:42 +0000 (20:28 -0700)
committerJianxin Xiong <jianxin.xiong@intel.com>
Fri, 28 Sep 2012 04:23:40 +0000 (21:23 -0700)
dapl/svc/mpxyd.c

index 6899f0453a7f26bf7596905c2168a4ca7411e534..2c2aeeae9e890a5c0f3f5c572bec65a3458c7afd 100644 (file)
@@ -45,6 +45,7 @@
 #include <stdarg.h>
 #include <getopt.h>
 #include <fcntl.h>
+#include <signal.h>
 #include <scif.h>
 #include <arpa/inet.h>
 #include <infiniband/verbs.h>
@@ -534,9 +535,10 @@ static void mpxy_log_options(void)
        mlog(0, "mcm msg completion signal rate %d\n", mcm_signal);
 }
 
+static int lock_fd;
 static int mpxy_open_lock_file(void)
 {
-       int lock_fd, rc;
+       int rc;
        char pid[16];
 
        lock_fd = open(lock_file, O_RDWR | O_CREAT | O_EXCL, 0640);
@@ -561,6 +563,13 @@ static int mpxy_open_lock_file(void)
        return 0;
 }
 
+static void mpxyd_release_lock_file( void )
+{
+       lockf(lock_fd, F_ULOCK, 0);
+       close(lock_fd);
+       unlink(lock_file);
+}
+
 static void daemonize(void)
 {
        id_t pid, sid;
@@ -3825,6 +3834,13 @@ retry:
 }
 
 
+static volatile int finished = 0;
+void sig_handler( int signum )
+{
+       mlog(0, "Killed by signal %d.\n", signum);
+       finished = 1;
+}
+
 /*
  * MPXY server will listen on both a IB UD QP for fabric CM messages
  * and a SCIF port for inter-bus MCM operation messages to/from MIC MCM clients.
@@ -3848,6 +3864,15 @@ static void mpxy_server(void)
        struct mcm_cq *m_cq;
        struct mcm_qp *m_qp;
        int time_ms, ret, data = 0, real_data;
+       struct sigaction act, oldact;
+
+       act.sa_handler = sig_handler;
+       sigemptyset(&act.sa_mask);
+       act.sa_flags = 0;
+       if (sigaction(SIGINT, &act, &oldact))
+               mlog(0, "sigaction: %s\n", strerror(errno));
+       if (sigaction(SIGTERM, &act, &oldact))
+               mlog(0, "sigaction: %s\n", strerror(errno));
 
        /* FD array */
        set = mcm_alloc_fd_set();
@@ -3856,7 +3881,7 @@ static void mpxy_server(void)
 
        mlog(0, "server started\n");
 
-       while (1) {
+       while (!finished) {
                if (data)
                        time_ms = 0;  /* no time to sleep */
                else
@@ -4024,5 +4049,7 @@ int main(int argc, char **argv)
 
        mlog(0, "shutdown complete\n");
        fclose(logfile);
+
+       mpxyd_release_lock_file();
        return 0;
 }