thermferm/lock.c

changeset 607
021338fc4e4f
parent 268
dda91dfa4aa8
--- a/thermferm/lock.c	Mon May 18 11:03:40 2020 +0200
+++ b/thermferm/lock.c	Sat Jun 27 19:28:10 2020 +0200
@@ -1,5 +1,5 @@
 /*****************************************************************************
- * Copyright (C) 2014
+ * Copyright (C) 2014-2020
  *   
  * Michiel Broek <mbroek at mbse dot eu>
  *
@@ -39,8 +39,7 @@
     snprintf(lockfile, PATH_MAX, "/var/run/%s.pid", name);
 
     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;
@@ -56,16 +55,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);
@@ -74,20 +71,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\n", 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);
@@ -108,13 +104,13 @@
     snprintf(lockfile, PATH_MAX, "/var/run/%s.pid", 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