Prevent running multiple hpcore instances from the same directory (#207)

This commit is contained in:
Chalith Desaman
2021-01-05 11:18:23 +05:30
committed by GitHub
parent 99e4296746
commit e8a3882176
5 changed files with 106 additions and 3 deletions

View File

@@ -355,4 +355,33 @@ namespace util
return 0;
}
/**
* Create a record lock for the file descriptor. Lock is associated with the process (Not for forked child processes).
* @param fd File descriptor to be locked.
* @param lock File lock.
* @param is_rwlock Whether the record lock is a write lock.
* @param start Starting offset for the lock.
* @param len Number of bytes to lock.
* @return Returns 0 if lock is successfully aquired, -1 on error.
*/
int set_lock(const int fd, struct flock &lock, const bool is_rwlock, const off_t start, const off_t len)
{
lock.l_type = is_rwlock ? F_WRLCK : F_RDLCK;
lock.l_whence = SEEK_SET;
lock.l_start = start,
lock.l_len = len;
return fcntl(fd, F_SETLK, &lock);
}
/**
* Releases the lock on file descriptor.
* @param fd File descriptor to be released.
* @param lock File lock.
* @return Returns 0 if lock is successfully released, -1 on error.
*/
int release_lock(const int fd, struct flock &lock)
{
lock.l_type = F_UNLCK;
return fcntl(fd, F_SETLKW, &lock);
}
} // namespace util