20 #ifdef HAVE_SYS_FILE_H
31 #include <sys/locking.h>
69 log_info(
LD_FS,
"Locking \"%s\"", filename);
72 log_warn(
LD_FS,
"Couldn't open \"%s\" for locking: %s", filename,
78 _lseek(fd, 0, SEEK_SET);
79 if (_locking(fd, blocking ? _LK_LOCK : _LK_NBLCK, 1) < 0) {
80 if (errno != EACCES && errno != EDEADLOCK)
81 log_warn(
LD_FS,
"Couldn't lock \"%s\": %s", filename, strerror(errno));
87 #elif defined(HAVE_FLOCK)
88 if (flock(fd, LOCK_EX|(blocking ? 0 : LOCK_NB)) < 0) {
89 if (errno != EWOULDBLOCK)
90 log_warn(
LD_FS,
"Couldn't lock \"%s\": %s", filename, strerror(errno));
99 memset(&lock, 0,
sizeof(lock));
100 lock.l_type = F_WRLCK;
101 lock.l_whence = SEEK_SET;
102 if (fcntl(fd, blocking ? F_SETLKW : F_SETLK, &lock) < 0) {
103 if (errno != EACCES && errno != EAGAIN)
104 log_warn(
LD_FS,
"Couldn't lock \"%s\": %s", filename, strerror(errno));
114 result->
filename = tor_strdup(filename);
128 if (_locking(
lockfile->
fd, _LK_UNLCK, 1) < 0) {
132 #elif defined(HAVE_FLOCK)
Wrappers for reading and writing data to files on disk.
int tor_open_cloexec(const char *path, int flags, unsigned mode)
tor_lockfile_t * tor_lockfile_lock(const char *filename, int blocking, int *locked_out)
void tor_lockfile_unlock(tor_lockfile_t *lockfile)
static tor_lockfile_t * lockfile
Headers for util_malloc.c.
Macros to manage assertions, fatal and non-fatal.