Better logging for locking the daemon and fixed stale lock recovery.

Sat, 27 Jun 2020 15:41:53 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Sat, 27 Jun 2020 15:41:53 +0200
changeset 704
62a1d2baec75
parent 703
faeede125639
child 705
d77b723f7b35

Better logging for locking the daemon and fixed stale lock recovery.

README.design file | annotate | diff | comparison | revisions
bmsd/lock.c file | annotate | diff | comparison | revisions
--- a/README.design	Mon Jun 15 20:26:46 2020 +0200
+++ b/README.design	Sat Jun 27 15:41:53 2020 +0200
@@ -12,6 +12,9 @@
   van de vergisting. Gaat vooral fout met de Saison.
   NOOT: experimentele wijziging is toegevoegd op 5-dec-2019. New Cubic van seanterrill.
 
+  Alle gisten checken op POF+ vlag. WLP staat op de website per gist. Alle overige niet.
+
+
 Extra:
 
 Gisten: = Cultures?
--- a/bmsd/lock.c	Mon Jun 15 20:26:46 2020 +0200
+++ b/bmsd/lock.c	Sat Jun 27 15:41:53 2020 +0200
@@ -1,5 +1,5 @@
 /*****************************************************************************
- * Copyright (C) 2017
+ * Copyright (C) 2017-2020
  *   
  * Michiel Broek <mbroek at mbse dot eu>
  *
@@ -44,15 +44,14 @@
     snprintf(lockfile, PATH_MAX, "%s/run/%s.pid", Private_Path, name);
 
     if (mkdirs(tempfile, 0755) == FALSE) {
-	printf("Can't create path \"%s\"\n", tempfile);
+	syslog(LOG_NOTICE, "Can't create path `%s': %s", tempfile, strerror(errno));
 	free(tempfile);
 	free(lockfile);
 	return 1;
     }
 
     if ((fp = fopen(tempfile, "w")) == NULL) {
-	perror(name);
-	printf("Can't create lockfile \"%s\"\n", tempfile);
+	syslog(LOG_NOTICE, "Can't create lockfile `%s': %s", tempfile, strerror(errno));
 	free(tempfile);
 	free(lockfile);
 	return 1;
@@ -68,16 +67,14 @@
 	    return 0;
 	}
 	if ((fp = fopen(lockfile, "r")) == NULL) {
-	    perror(name);
-	    printf("Can't open lockfile \"%s\"\n", tempfile);
+	    syslog(LOG_NOTICE, "Can't open lockfile `%s': %s", tempfile, strerror(errno));
 	    unlink(tempfile);
 	    free(tempfile);
 	    free(lockfile);
 	    return 1;
 	}
 	if (fscanf(fp, "%u", &oldpid) != 1) {
-	    perror(name);
-	    printf("Can't read old pid from \"%s\"\n", tempfile);
+	    syslog(LOG_NOTICE, "Can't read old pid from `%s'", tempfile);
 	    fclose(fp);
 	    unlink(tempfile);
 	    free(tempfile);
@@ -86,20 +83,19 @@
 	}
 	fclose(fp);
 	if (kill(oldpid,0) == -1) {
-	    if (errno == ESRCH) {
-		printf("Stale lock found for pid %u\n", oldpid);
+	    if (errno == ESRCH || errno == EPERM) {
+		syslog(LOG_NOTICE, "Stale lock found for pid %u", oldpid);
 		unlink(lockfile);
 		/* no return, try lock again */  
 	    } else {
-		perror(name);
-		printf("Kill for %u failed\n",oldpid);
+		syslog(LOG_NOTICE, "Kill for %u failed: %s",oldpid, strerror(errno));
 		unlink(tempfile);
 		free(tempfile);
 		free(lockfile);
 		return 1;
 	    }
 	} else {
-	    printf("Another %s is already running, pid=%u\n", name, oldpid);
+	    syslog(LOG_NOTICE, "Another %s is already running, pid=%u", name, oldpid);
 	    unlink(tempfile);
 	    free(tempfile);
 	    free(lockfile);
@@ -120,13 +116,13 @@
     snprintf(lockfile, PATH_MAX, "%s/run/%s.pid", Private_Path, name);
 
     if ((fp = fopen(lockfile, "r")) == NULL) {
-	syslog(LOG_NOTICE, "Can't open lockfile \"%s\"", lockfile);
+	syslog(LOG_NOTICE, "Can't open lockfile `%s': %s", lockfile, strerror(errno));
 	free(lockfile);
 	return;
     }
 
     if (fscanf(fp, "%u", &oldpid) != 1) {
-	syslog(LOG_NOTICE, "Can't read old pid from \"%s\"", lockfile);
+	syslog(LOG_NOTICE, "Can't read old pid from `%s'", lockfile);
 	fclose(fp);
 	unlink(lockfile);
 	free(lockfile);

mercurial