# HG changeset patch # User Michiel Broek # Date 1399916711 -7200 # Node ID 38e3e3a49320e4c8e329dfa6aa367cd6b7f6b8e0 # Parent 2357e8636a6077aae203dbcd5220fb094be05e88 Added lock/pid file diff -r 2357e8636a60 -r 38e3e3a49320 coolers/coolers.c --- a/coolers/coolers.c Fri May 09 22:29:42 2014 +0200 +++ b/coolers/coolers.c Mon May 12 19:45:11 2014 +0200 @@ -241,6 +241,11 @@ rc_switch *tmp2, *old2; int rc, run = 0, temp; + if (lockprog((char *)"coolers")) { + syslog(LOG_NOTICE, "Can't lock"); + return 1; + } + my_mosquitto_init(); rc = piThreadCreate(my_sensors_loop); @@ -323,6 +328,8 @@ stopLCD(); disableTransmit(); + ulockprog((char *)"coolers"); + if (debug) fprintf(stdout, "Goodbye\n"); diff -r 2357e8636a60 -r 38e3e3a49320 lib/Makefile --- a/lib/Makefile Fri May 09 22:29:42 2014 +0200 +++ b/lib/Makefile Mon May 12 19:45:11 2014 +0200 @@ -3,9 +3,9 @@ include ../Makefile.global SRCS = xutil.c rdconfig.c lcd-pcf8574.c rc-switch.c dht11.c \ - logger.c + logger.c lock.c OBJS = xutil.o rdconfig.o lcd-pcf8574.o rc-switch.o dht11.o \ - logger.o + logger.o lock.o HDRS = mbselib.h TARGET = libmbse.a @@ -52,4 +52,5 @@ rc-switch.o: ../config.h mbselib.h dht11.o: ../config.h mbselib.h logger.o: ../config.h mbselib.h +lock.o: ../config.h mbselib.h # End of generated dependencies diff -r 2357e8636a60 -r 38e3e3a49320 lib/lock.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/lock.c Mon May 12 19:45:11 2014 +0200 @@ -0,0 +1,133 @@ +/***************************************************************************** + * Copyright (C) 2014 + * + * Michiel Broek + * + * This file is part of the mbsePi-apps + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2, or (at your option) any + * later version. + * + * mbsePi-apps is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with EC-65K; see the file COPYING. If not, write to the Free + * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + *****************************************************************************/ + +#include "../config.h" +#include "mbselib.h" + +/* + * Put a lock on this program. + */ +int lockprog(char *name) +{ + char *tempfile, *lockfile; + FILE *fp; + pid_t oldpid; + + 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); + + if ((fp = fopen(tempfile, "w")) == NULL) { + perror(name); + printf("Can't create lockfile \"%s\"\n", tempfile); + free(tempfile); + free(lockfile); + return 1; + } + fprintf(fp, "%10u\n", getpid()); + fclose(fp); + + while (TRUE) { + if (link(tempfile, lockfile) == 0) { + unlink(tempfile); + free(tempfile); + free(lockfile); + return 0; + } + if ((fp = fopen(lockfile, "r")) == NULL) { + perror(name); + printf("Can't open lockfile \"%s\"\n", tempfile); + 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); + fclose(fp); + unlink(tempfile); + free(tempfile); + free(lockfile); + return 1; + } + fclose(fp); + if (kill(oldpid,0) == -1) { + if (errno == ESRCH) { + printf("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); + unlink(tempfile); + free(tempfile); + free(lockfile); + return 1; + } + } else { + printf("Another %s is already running, pid=%u\n", name, oldpid); + unlink(tempfile); + free(tempfile); + free(lockfile); + return 1; + } + } +} + + + +void ulockprog(char *name) +{ + char *lockfile; + pid_t oldpid; + FILE *fp; + + lockfile = calloc(PATH_MAX, sizeof(char)); + snprintf(lockfile, PATH_MAX, "/var/run/%s.pid", name); + + if ((fp = fopen(lockfile, "r")) == NULL) { + syslog(LOG_NOTICE, "Can't open lockfile \"%s\"", lockfile); + free(lockfile); + return; + } + + if (fscanf(fp, "%u", &oldpid) != 1) { + syslog(LOG_NOTICE, "Can't read old pid from \"%s\"", lockfile); + fclose(fp); + unlink(lockfile); + free(lockfile); + return; + } + + fclose(fp); + + if (oldpid == getpid()) { + (void)unlink(lockfile); + } + + free(lockfile); +} + + diff -r 2357e8636a60 -r 38e3e3a49320 lib/mbselib.h --- a/lib/mbselib.h Fri May 09 22:29:42 2014 +0200 +++ b/lib/mbselib.h Mon May 12 19:45:11 2014 +0200 @@ -18,6 +18,8 @@ #include #include #include +#include + /* mosquitto */ #include @@ -74,11 +76,15 @@ } sys_config; - void killconfig(void); int rdconfig(char *); +/* lock.c */ +int lockprog(char *); +void ulockprog(char *); + + /* xutil.c */ char *xmalloc(size_t); char *xstrcpy(char *); @@ -109,7 +115,6 @@ /* rc-switch.c */ - int toggleSwitch(char *); int toggleTypeA(char *, char *, bool); int toggleTypeB(int, int, bool);