From 6e42ef0edaba59fd344e89ffd469202ff2bd39ac Mon Sep 17 00:00:00 2001
From: Samo Penic <samo.penic@fe.uni-lj.si>
Date: Tue, 15 Mar 2016 15:40:24 +0000
Subject: [PATCH] File locking into .lock seems to work fine

---
 src/main.c    |    4 ++++
 src/general.c |   14 +++++++-------
 2 files changed, 11 insertions(+), 7 deletions(-)

diff --git a/src/general.c b/src/general.c
index 68b0611..9782e47 100644
--- a/src/general.c
+++ b/src/general.c
@@ -61,7 +61,7 @@
     fd = open(pidFile, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
     if (fd == -1){
         ts_fprintf(stderr,"Could not open PID file %s", pidFile);
-	fatal("Cannot continue",1);
+	fatal("Cannot continue (1)",1);
 }
     if (flags & CPF_CLOEXEC) {
 
@@ -75,13 +75,13 @@
         flags = fcntl(fd, F_GETFD);                     /* Fetch flags */
         if (flags == -1){
             ts_fprintf(stderr,"Could not get flags for PID file %s", pidFile);
-	fatal("Cannot continue",1);
+	fatal("Cannot continue (2)",1);
 }
         flags |= FD_CLOEXEC;                            /* Turn on FD_CLOEXEC */
 
         if (fcntl(fd, F_SETFD, flags) == -1)            /* Update flags */
             ts_fprintf(stderr,"Could not set flags for PID file %s", pidFile);
-		fatal("Cannot continue",1);
+		fatal("Cannot continue (3)",1);
 	    
     }
 
@@ -89,24 +89,24 @@
         if (errno  == EAGAIN || errno == EACCES){
             ts_fprintf(stderr,"PID file '%s' is locked; probably "
                      "'%s' is already running", pidFile, progName);
-		fatal("Cannot continue",1);
+		fatal("Cannot continue (4)",1);
 }
         else{
             ts_fprintf(stderr,"Unable to lock PID file '%s'", pidFile);
-	fatal("Cannot continue",1);
+	fatal("Cannot continue (5)",1);
 }
     }
 
     if (ftruncate(fd, 0) == -1){
         ts_fprintf(stderr,"Could not truncate PID file '%s'", pidFile);
-	fatal("Cannot continue",1);
+	fatal("Cannot continue (6)",1);
 }
 
     snprintf(buf, BUF_SIZE, "%ld\n", (long) getpid());
     if (write(fd, buf, strlen(buf)) != strlen(buf)){
 
         ts_fprintf(stderr,"Writing to PID file '%s'", pidFile);
-	fatal("Cannot continue",1);
+	fatal("Cannot continue (7)",1);
 }
     return fd;
 }
diff --git a/src/main.c b/src/main.c
index 8cf3e92..e4b306f 100644
--- a/src/main.c
+++ b/src/main.c
@@ -17,6 +17,8 @@
 #include "shcomplex.h"
 #include "dumpstate.h"
 #include "restore.h"
+
+#include <fcntl.h>
 /** Entrance function to the program
   * @param argv is a number of parameters used in program call (including the program name
   * @param argc is a pointer to strings (character arrays) which holds the arguments
@@ -28,6 +30,8 @@
 	ts_tape *tape;
 	ts_uint start_iteration=0;
 	force_from_tape=0;
+	/* create lock file */
+	createPidFile("ts_trisurf",".lock",0);
 	parse_args(argv,argc); // sets global variable command_line_args (defined in io.h)
 	ts_fprintf(stdout,"TRISURF-NG v. %s, compiled on: %s %s.\n", TS_VERSION, __DATE__, __TIME__);
 	ts_fprintf(stdout,"Programming done by: Samo Penic and Miha Fosnaric\n");

--
Gitblit v1.9.3