Added program locking.

Sun, 20 Dec 2015 20:52:37 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Sun, 20 Dec 2015 20:52:37 +0100
changeset 465
3aac3276689d
parent 464
4a624c071ca9
child 466
49a5318256f0

Added program locking.

brewco/brewco.c file | annotate | diff | comparison | revisions
brewco/lock.c file | annotate | diff | comparison | revisions
brewco/rdconfig.c file | annotate | diff | comparison | revisions
--- a/brewco/brewco.c	Sun Dec 20 20:37:40 2015 +0100
+++ b/brewco/brewco.c	Sun Dec 20 20:52:37 2015 +0100
@@ -282,10 +282,10 @@
     slcdCharDef(slcdHandle, 6, RevHeatONOFF);
     slcdCharDef(slcdHandle, 7, Language);
 
-//    if (lockprog((char *)"brewco")) {
-//	syslog(LOG_NOTICE, "Can't lock");
-//	return 1;
-//    }
+    if (lockprog((char *)"brewco")) {
+	syslog(LOG_NOTICE, "Can't lock");
+	return 1;
+    }
 
     if (debug)
 	fprintf(stdout, "Begin server()\n");
@@ -558,7 +558,7 @@
     wrrecipes();
     wrconfig();
 
-//    ulockprog((char *)"brewco");
+    ulockprog((char *)"brewco");
     return 0;
 }
 
@@ -609,7 +609,6 @@
     mkdirs(varpath, 0755);
     mkdirs(etcpath, 0755);
 
-fprintf(stdout, "home: %s etc=%s var=%s\n", homepath, etcpath, varpath);
     if (rdconfig()) {
 	fprintf(stderr, "Error reading configuration\n");
 	syslog(LOG_NOTICE, "Error reading configuration: halted");
--- a/brewco/lock.c	Sun Dec 20 20:37:40 2015 +0100
+++ b/brewco/lock.c	Sun Dec 20 20:52:37 2015 +0100
@@ -23,6 +23,9 @@
 #include "lock.h"
 #include "brewco.h"
 
+extern char	*varpath;
+
+
 /*
  *  Put a lock on this program.
  */
@@ -35,8 +38,8 @@
     tempfile = calloc(PATH_MAX, sizeof(char));
     lockfile = calloc(PATH_MAX, sizeof(char));
 
-    snprintf(tempfile, PATH_MAX, "/var/run/%s.tmp", name);
-    snprintf(lockfile, PATH_MAX, "/var/run/%s.pid", name);
+    snprintf(tempfile, PATH_MAX, "%s%s.tmp", varpath, name);
+    snprintf(lockfile, PATH_MAX, "%s%s.pid", varpath, name);
 
     if ((fp = fopen(tempfile, "w")) == NULL) {
 	perror(name);
@@ -105,7 +108,7 @@
     FILE	    *fp;
 
     lockfile = calloc(PATH_MAX, sizeof(char));
-    snprintf(lockfile, PATH_MAX, "/var/run/%s.pid", name);
+    snprintf(lockfile, PATH_MAX, "%s%s.pid", varpath, name);
 
     if ((fp = fopen(lockfile, "r")) == NULL) {
 	syslog(LOG_NOTICE, "Can't open lockfile \"%s\"", lockfile);
--- a/brewco/rdconfig.c	Sun Dec 20 20:37:40 2015 +0100
+++ b/brewco/rdconfig.c	Sun Dec 20 20:52:37 2015 +0100
@@ -1284,12 +1284,12 @@
     /*
      * Make upto 10 backups of the configuration.
      */
-    fpath = calloc(1024, sizeof(char));
-    tpath = calloc(1024, sizeof(char));
+    fpath = calloc(PATH_MAX, sizeof(char));
+    tpath = calloc(PATH_MAX, sizeof(char));
 
     for (i = 9; i > 0; i--) {
-	snprintf(tpath, 1023, "%s.%d", mypath, i+1);
-	snprintf(fpath, 1023, "%s.%d", mypath, i);
+	snprintf(tpath, PATH_MAX, "%s.%d", mypath, i+1);
+	snprintf(fpath, PATH_MAX, "%s.%d", mypath, i);
 	if (file_exist(fpath, R_OK) == 0) {
 	    unlink(tpath);
 	    if (file_cp(fpath, tpath)) {
@@ -1297,8 +1297,8 @@
 	    }
 	}
     }
-    snprintf(tpath, 1023, "%s.1", mypath);
-    snprintf(fpath, 1023, "%s", mypath);
+    snprintf(tpath, PATH_MAX, "%s.1", mypath);
+    snprintf(fpath, PATH_MAX, "%s", mypath);
     if (file_exist(fpath, R_OK) == 0) {
     	unlink(tpath);
     	if (file_cp(fpath, tpath)) {

mercurial