2016-09-06 17:08:46 +00:00
|
|
|
/**
|
2016-09-11 20:20:15 +00:00
|
|
|
* Author......: See docs/credits.txt
|
2016-09-06 17:08:46 +00:00
|
|
|
* License.....: MIT
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "common.h"
|
2016-10-09 20:41:55 +00:00
|
|
|
#include "types.h"
|
|
|
|
#include "event.h"
|
2016-09-06 17:08:46 +00:00
|
|
|
#include "locking.h"
|
|
|
|
|
2016-09-07 20:29:57 +00:00
|
|
|
#if defined (F_SETLKW)
|
2016-09-06 17:08:46 +00:00
|
|
|
|
2016-10-09 20:41:55 +00:00
|
|
|
int lock_file (FILE *fp)
|
2016-09-06 17:08:46 +00:00
|
|
|
{
|
|
|
|
struct flock lock;
|
|
|
|
|
|
|
|
memset (&lock, 0, sizeof (struct flock));
|
|
|
|
|
|
|
|
lock.l_type = F_WRLCK;
|
|
|
|
|
2016-11-05 10:33:29 +00:00
|
|
|
/* Needs this loop because a signal may interrupt a wait for lock */
|
|
|
|
while (fcntl (fileno (fp), F_SETLKW, &lock))
|
|
|
|
{
|
|
|
|
if (errno != EINTR) return -1;
|
|
|
|
}
|
2016-09-06 17:08:46 +00:00
|
|
|
|
2016-10-09 20:41:55 +00:00
|
|
|
return 0;
|
2016-09-06 17:08:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void unlock_file (FILE *fp)
|
|
|
|
{
|
|
|
|
struct flock lock;
|
|
|
|
|
|
|
|
memset (&lock, 0, sizeof (struct flock));
|
|
|
|
|
|
|
|
lock.l_type = F_UNLCK;
|
|
|
|
|
|
|
|
fcntl (fileno (fp), F_SETLK, &lock);
|
|
|
|
}
|
|
|
|
|
2016-11-07 14:02:34 +00:00
|
|
|
#else
|
|
|
|
|
|
|
|
int lock_file (MAYBE_UNUSED FILE *fp)
|
|
|
|
{
|
|
|
|
// we should put windows specific code here
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void unlock_file (MAYBE_UNUSED FILE *fp)
|
|
|
|
{
|
|
|
|
// we should put windows specific code here
|
|
|
|
}
|
|
|
|
|
2016-09-06 17:08:46 +00:00
|
|
|
#endif // F_SETLKW
|