umask(old_umask);
/* Try to claim the pidfile, exiting if we can't */
- if ((pid_file_fd = virPidFileAcquirePath(pid_file, getpid())) < 0) {
+ if ((pid_file_fd = virPidFileAcquirePath(pid_file, false, getpid())) < 0) {
ret = VIR_DAEMON_ERR_PIDFILE;
goto cleanup;
}
/* Re-claim PID file now as we will not be daemonizing */
if (pid_file &&
- (*pid_file_fd = virPidFileAcquirePath(pid_file, getpid())) < 0)
+ (*pid_file_fd = virPidFileAcquirePath(pid_file, false, getpid())) < 0)
goto cleanup;
if (!(lockDaemon = virLockDaemonNewPostExecRestart(object, privileged)))
}
/* If we have a pidfile set, claim it now, exiting if already taken */
- if ((pid_file_fd = virPidFileAcquirePath(pid_file, getpid())) < 0) {
+ if ((pid_file_fd = virPidFileAcquirePath(pid_file, false, getpid())) < 0) {
ret = VIR_LOCK_DAEMON_ERR_PIDFILE;
goto cleanup;
}
* @shared: type of lock to acquire
* @start: byte offset to start lock
* @len: length of lock (0 to acquire entire remaining file from @start)
+ * @waitForLock: wait for previously held lock or not
*
* Attempt to acquire a lock on the file @fd. If @shared
* is true, then a shared lock will be acquired,
* otherwise an exclusive lock will be acquired. If
* the lock cannot be acquired, an error will be
- * returned. This will not wait to acquire the lock if
- * another process already holds it.
+ * returned. If @waitForLock is true, this will wait
+ * for the lock if another process has already acquired it.
*
* The lock will be released when @fd is closed. The lock
* will also be released if *any* other open file descriptor
*
* Returns 0 on success, or -errno otherwise
*/
-int virFileLock(int fd, bool shared, off_t start, off_t len)
+int virFileLock(int fd, bool shared, off_t start, off_t len, bool waitForLock)
{
struct flock fl = {
.l_type = shared ? F_RDLCK : F_WRLCK,
.l_len = len,
};
- if (fcntl(fd, F_SETLK, &fl) < 0)
+ int cmd = waitForLock ? F_SETLKW : F_SETLK;
+
+ if (fcntl(fd, cmd, &fl) < 0)
return -errno;
return 0;
int virFileLock(int fd ATTRIBUTE_UNUSED,
bool shared ATTRIBUTE_UNUSED,
off_t start ATTRIBUTE_UNUSED,
- off_t len ATTRIBUTE_UNUSED)
+ off_t len ATTRIBUTE_UNUSED,
+ bool waitForLock ATTRIBUTE_UNUSED)
{
return -ENOSYS;
}
void virFileWrapperFdFree(virFileWrapperFdPtr dfd);
-int virFileLock(int fd, bool shared, off_t start, off_t len);
+int virFileLock(int fd, bool shared, off_t start, off_t len, bool waitForLock);
int virFileUnlock(int fd, off_t start, off_t len);
typedef int (*virFileRewriteFunc)(int fd, void *opaque);
if (res->flags & VIR_LOCK_SPACE_ACQUIRE_SHARED) {
/* We must upgrade to an exclusive lock to ensure
* no one else still has it before trying to delete */
- if (virFileLock(res->fd, false, 0, 1) < 0) {
+ if (virFileLock(res->fd, false, 0, 1, false) < 0) {
VIR_DEBUG("Could not upgrade shared lease to exclusive, not deleting");
} else {
if (unlink(res->path) < 0 &&
goto error;
}
- if (virFileLock(res->fd, shared, 0, 1) < 0) {
+ if (virFileLock(res->fd, shared, 0, 1, false) < 0) {
if (errno == EACCES || errno == EAGAIN) {
virReportError(VIR_ERR_RESOURCE_BUSY,
_("Lockspace resource '%s' is locked"),
goto error;
}
- if (virFileLock(res->fd, shared, 0, 1) < 0) {
+ if (virFileLock(res->fd, shared, 0, 1, false) < 0) {
if (errno == EACCES || errno == EAGAIN) {
virReportError(VIR_ERR_RESOURCE_BUSY,
_("Lockspace resource '%s' is locked"),
}
int virPidFileAcquirePath(const char *path,
+ bool waitForLock,
pid_t pid)
{
int fd = -1;
return -1;
}
- if (virFileLock(fd, false, 0, 1) < 0) {
+ if (virFileLock(fd, false, 0, 1, waitForLock) < 0) {
virReportSystemError(errno,
_("Failed to acquire pid file '%s'"),
path);
int virPidFileAcquire(const char *dir,
const char *name,
+ bool waitForLock,
pid_t pid)
{
int rc = 0;
goto cleanup;
}
- rc = virPidFileAcquirePath(pidfile, pid);
+ rc = virPidFileAcquirePath(pidfile, waitForLock, pid);
cleanup:
VIR_FREE(pidfile);
int virPidFileAcquirePath(const char *path,
+ bool waitForLock,
pid_t pid) ATTRIBUTE_RETURN_CHECK;
int virPidFileAcquire(const char *dir,
const char *name,
+ bool waitForLock,
pid_t pid) ATTRIBUTE_RETURN_CHECK;
int virPidFileReleasePath(const char *path,