Uses a lock file instead than a pid file.

It's more appropriate for a not daemon.
This commit is contained in:
Andrea Mazzoleni 2013-06-18 21:06:31 +02:00
parent bc2a837953
commit fceaa8a4cf
7 changed files with 46 additions and 70 deletions

40
util.c
View file

@ -867,16 +867,14 @@ void randomize(void* void_ptr, unsigned size)
}
/****************************************************************************/
/* pid */
/* lock */
#if HAVE_PIDFILE
int pid_lock(const char* file)
#if HAVE_LOCKFILE
int lock_lock(const char* file)
{
char buffer[64];
ssize_t len;
int f;
f = open(file, O_CREAT | O_WRONLY, 0600);
f = open(file, O_CREAT | O_TRUNC | O_WRONLY, 0600);
if (f == -1) {
return -1;
}
@ -887,38 +885,18 @@ int pid_lock(const char* file)
return -1;
}
/* write the pid into the file */
snprintf(buffer, sizeof(buffer), "%ld", (long)getpid());
len = strlen(buffer);
if (write(f, buffer, len) != len) {
close(f);
return -1;
}
if (ftruncate(f, len) == -1) {
close(f);
return -1;
}
return 0;
}
#endif
#if HAVE_PIDFILE
int pid_unlock(int f, const char* file)
#if HAVE_LOCKFILE
int lock_unlock(int f)
{
/*
* Remove file before closing it.
* This opens a race course in case another process already opened it.
* but has not yet failed the next flock() call.
* But removing it after the close, opens a similar race course.
* It seems that deleting a pid file is something just impossible to do it right.
* Intentionally don't remove file the lock file.
* Removing it just introduces race course with other process
* that could have already opened it.
*/
if (remove(file) == -1) {
close(f);
return -1;
}
if (close(f) == -1) {
return -1;