X-Git-Url: http://git.madism.org/?a=blobdiff_plain;f=common%2Fcommon.c;h=758e869a7da225ff3e1a6493a633995ec24adcaf;hb=bcd90daa5d3d303afe13832249fa20cad7303f8c;hp=3688c9d9e758422864c2fcd4f0387e5faf53dde9;hpb=98232498b817536cfd1e11bccb23e2f7fdd5832b;p=apps%2Fpfixtools.git diff --git a/common/common.c b/common/common.c index 3688c9d..758e869 100644 --- a/common/common.c +++ b/common/common.c @@ -229,11 +229,20 @@ int drop_privileges(const char *user, const char *group) int pidfile_open(const char *name) { + struct flock lock; + p_clear(&lock, 1); + lock.l_type = F_WRLCK; if (name) { pidfile = fopen(name, "w"); if (!pidfile) return -1; - fprintf(pidfile, "%d\n", getpid()); + if (fcntl(fileno(pidfile), F_SETLK, &lock) == -1) { + syslog(LOG_ERR, "program already started"); + fclose(pidfile); + pidfile = NULL; + return -1; + } + fprintf(pidfile, "%d\n", getpid()); return fflush(pidfile); } return 0; @@ -252,10 +261,14 @@ int pidfile_refresh(void) static void pidfile_close(void) { + struct flock lock; + p_clear(&lock, 1); + lock.l_type = F_UNLCK; if (pidfile) { rewind(pidfile); ftruncate(fileno(pidfile), 0); - fclose(pidfile); + fcntl(fileno(pidfile), F_SETLK, &lock); + fclose(pidfile); pidfile = NULL; } }