# HG changeset patch # User Michiel Broek # Date 1593278890 -7200 # Node ID 021338fc4e4fe941369f5bb63d98b5a1e06f5c0a # Parent 798dd0c4fd001aa8821ac9173f7cdec810366f8d Better logging for locking the daemon and fixed stale lock recovery. diff -r 798dd0c4fd00 -r 021338fc4e4f thermferm/lock.c --- 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 * @@ -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);