[pisa-src] r1194 - in trunk/pairing: management.c management.h

  • From: Jan Marten <jan.marten@xxxxxxxxxxxxxx>
  • To: pisa-src@xxxxxxxxxxxxx
  • Date: Thu, 15 Oct 2009 19:08:38 +0200

Author: marten
Date: Thu Oct 15 19:08:38 2009
New Revision: 1194

Log:
Readded lock-file creation

Modified:
   trunk/pairing/management.c
   trunk/pairing/management.h

Modified: trunk/pairing/management.c
==============================================================================
--- trunk/pairing/management.c  Thu Oct 15 18:46:37 2009        (r1193)
+++ trunk/pairing/management.c  Thu Oct 15 19:08:38 2009        (r1194)
@@ -10,9 +10,6 @@
  * @date Aug. 2009
  */
 
-/* TODO JAN : insert code for locking
- */
-
 #include <stdio.h>
 #include <unistd.h>
 #include <time.h>
@@ -20,6 +17,7 @@
 #include <search.h>
 #include <signal.h>
 #include <getopt.h>
+#include <fcntl.h>
 
 #include "global.h" /* it must be included before all other header files */
 #include "config.h"
@@ -342,6 +340,34 @@
     return ul;
 }
 
+/* Tries to create a lock file to let only one instance of the program run
+ * @return 0 -> An instance is running
+ * @return 1 -> Lock file was created successfully
+ */
+static int get_lock(void)
+{
+       int fdlock;
+       struct flock fl;
+
+       fl.l_type = F_WRLCK;
+       fl.l_whence = SEEK_SET;
+       fl.l_start = 0;
+       fl.l_len = 1;
+
+       if((fdlock = open("pisamgmt.lock", O_WRONLY|O_CREAT, 666)) == -1)
+       {
+               return 0;
+       }
+
+       if(fcntl(fdlock, F_SETLK, &fl) == -1)
+       {
+               return 0;
+       }
+
+       return 1;
+}
+
+
 /** Main program that will be run on the user's computer.
  */
 int main(int argc, char *argv[])
@@ -357,6 +383,12 @@
     pisa_userhitlist_entry *uhlentry = NULL;
     pisa_userhitlist_entry new_entry = {};
 
+       if(!get_lock())
+       {
+               PISA_ERROR("A management instance is already running!");
+               return ERROR_ALREADY_RUNNING;
+       }
+
     pisa_mgmt_conf_init(&managementconf);
 
     new_entry.active = NO_CHANGE;

Modified: trunk/pairing/management.h
==============================================================================
--- trunk/pairing/management.h  Thu Oct 15 18:46:37 2009        (r1193)
+++ trunk/pairing/management.h  Thu Oct 15 19:08:38 2009        (r1194)
@@ -25,6 +25,7 @@
 #define ERROR_PARSING_COMMANDLINE 4
 #define ERROR_WRITING_CONFIG 5
 #define ERROR_GENERAL 6
+#define ERROR_ALREADY_RUNNING 7
 
 #define DEACTIVATED 0
 #define ACTIVATED 1

Other related posts:

  • » [pisa-src] r1194 - in trunk/pairing: management.c management.h - Jan Marten