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"
|
2016-09-06 17:08:46 +00:00
|
|
|
#include "locking.h"
|
2019-06-30 23:30:24 +00:00
|
|
|
#include "shared.h"
|
2016-09-06 17:08:46 +00:00
|
|
|
|
2016-09-07 20:29:57 +00:00
|
|
|
#if defined (F_SETLKW)
|
2016-09-06 17:08:46 +00:00
|
|
|
|
2019-06-30 23:30:24 +00:00
|
|
|
int hc_lockfile (HCFILE *fp)
|
2016-09-06 17:08:46 +00:00
|
|
|
{
|
2019-07-01 15:27:08 +00:00
|
|
|
if (fp == NULL) return -1;
|
2019-06-26 17:06:46 +00:00
|
|
|
|
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 */
|
2019-06-30 23:30:24 +00:00
|
|
|
while (fcntl (hc_fileno (fp), F_SETLKW, &lock))
|
2016-11-05 10:33:29 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-06-30 23:30:24 +00:00
|
|
|
int hc_unlockfile (HCFILE *fp)
|
2016-09-06 17:08:46 +00:00
|
|
|
{
|
2019-07-01 15:27:08 +00:00
|
|
|
if (fp == NULL) return -1;
|
2019-06-26 17:06:46 +00:00
|
|
|
|
2016-09-06 17:08:46 +00:00
|
|
|
struct flock lock;
|
|
|
|
|
|
|
|
memset (&lock, 0, sizeof (struct flock));
|
|
|
|
|
|
|
|
lock.l_type = F_UNLCK;
|
|
|
|
|
2019-07-01 15:27:08 +00:00
|
|
|
if (fcntl (hc_fileno (fp), F_SETLK, &lock)) return -1;
|
2017-02-14 14:15:50 +00:00
|
|
|
|
|
|
|
return 0;
|
2016-09-06 17:08:46 +00:00
|
|
|
}
|
|
|
|
|
2016-11-07 14:02:34 +00:00
|
|
|
#else
|
|
|
|
|
2019-06-30 23:30:24 +00:00
|
|
|
int hc_lockfile (MAYBE_UNUSED HCFILE *fp)
|
2016-11-07 14:02:34 +00:00
|
|
|
{
|
|
|
|
// we should put windows specific code here
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-06-30 23:30:24 +00:00
|
|
|
int hc_unlockfile (MAYBE_UNUSED HCFILE *fp)
|
2016-11-07 14:02:34 +00:00
|
|
|
{
|
|
|
|
// we should put windows specific code here
|
2017-02-14 15:53:01 +00:00
|
|
|
|
|
|
|
return 0;
|
2016-11-07 14:02:34 +00:00
|
|
|
}
|
|
|
|
|
2016-09-06 17:08:46 +00:00
|
|
|
#endif // F_SETLKW
|