|
rpm-build |
0fba15 |
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
/***
|
|
rpm-build |
0fba15 |
This file is part of systemd.
|
|
rpm-build |
0fba15 |
Now copied into libglnx:
|
|
rpm-build |
0fba15 |
- Use GError
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
Copyright 2010 Lennart Poettering
|
|
rpm-build |
0fba15 |
Copyright 2015 Colin Walters <walters@verbum.org>
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
systemd is free software; you can redistribute it and/or modify it
|
|
rpm-build |
0fba15 |
under the terms of the GNU Lesser General Public License as published by
|
|
rpm-build |
0fba15 |
the Free Software Foundation; either version 2.1 of the License, or
|
|
rpm-build |
0fba15 |
(at your option) any later version.
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
systemd is distributed in the hope that it will be useful, but
|
|
rpm-build |
0fba15 |
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
rpm-build |
0fba15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
rpm-build |
0fba15 |
Lesser General Public License for more details.
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
You should have received a copy of the GNU Lesser General Public License
|
|
rpm-build |
0fba15 |
along with systemd; If not, see <http://www.gnu.org/licenses/>.
|
|
rpm-build |
0fba15 |
***/
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
#include "config.h"
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
#include <stdlib.h>
|
|
rpm-build |
0fba15 |
#include <stdbool.h>
|
|
rpm-build |
0fba15 |
#include <errno.h>
|
|
rpm-build |
0fba15 |
#include <string.h>
|
|
rpm-build |
0fba15 |
#include <stdio.h>
|
|
rpm-build |
0fba15 |
#include <limits.h>
|
|
rpm-build |
0fba15 |
#include <unistd.h>
|
|
rpm-build |
0fba15 |
#include <sys/types.h>
|
|
rpm-build |
0fba15 |
#include <sys/file.h>
|
|
rpm-build |
0fba15 |
#include <sys/stat.h>
|
|
rpm-build |
0fba15 |
#include <fcntl.h>
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
#include "glnx-lockfile.h"
|
|
rpm-build |
0fba15 |
#include "glnx-errors.h"
|
|
rpm-build |
0fba15 |
#include "glnx-fdio.h"
|
|
rpm-build |
0fba15 |
#include "glnx-backport-autocleanups.h"
|
|
rpm-build |
0fba15 |
#include "glnx-local-alloc.h"
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
#define newa(t, n) ((t*) alloca(sizeof(t)*(n)))
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
/**
|
|
rpm-build |
0fba15 |
* glnx_make_lock_file:
|
|
rpm-build |
0fba15 |
* @dfd: Directory file descriptor (if not `AT_FDCWD`, must have lifetime `>=` @out_lock)
|
|
rpm-build |
0fba15 |
* @p: Path
|
|
rpm-build |
0fba15 |
* @operation: one of `LOCK_SH`, `LOCK_EX`, `LOCK_UN`, as passed to flock()
|
|
rpm-build |
0fba15 |
* @out_lock: (out) (caller allocates): Return location for lock
|
|
rpm-build |
0fba15 |
* @error: Error
|
|
rpm-build |
0fba15 |
*
|
|
rpm-build |
0fba15 |
* Block until a lock file named @p (relative to @dfd) can be created,
|
|
rpm-build |
0fba15 |
* using the flags in @operation, returning the lock data in the
|
|
rpm-build |
0fba15 |
* caller-allocated location @out_lock.
|
|
rpm-build |
0fba15 |
*
|
|
rpm-build |
0fba15 |
* This API wraps new-style process locking if available, otherwise
|
|
rpm-build |
0fba15 |
* falls back to BSD locks.
|
|
rpm-build |
0fba15 |
*/
|
|
rpm-build |
0fba15 |
gboolean
|
|
rpm-build |
0fba15 |
glnx_make_lock_file(int dfd, const char *p, int operation, GLnxLockFile *out_lock, GError **error) {
|
|
rpm-build |
0fba15 |
glnx_autofd int fd = -1;
|
|
rpm-build |
0fba15 |
g_autofree char *t = NULL;
|
|
rpm-build |
0fba15 |
int r;
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
/*
|
|
rpm-build |
0fba15 |
* We use UNPOSIX locks if they are available. They have nice
|
|
rpm-build |
0fba15 |
* semantics, and are mostly compatible with NFS. However,
|
|
rpm-build |
0fba15 |
* they are only available on new kernels. When we detect we
|
|
rpm-build |
0fba15 |
* are running on an older kernel, then we fall back to good
|
|
rpm-build |
0fba15 |
* old BSD locks. They also have nice semantics, but are
|
|
rpm-build |
0fba15 |
* slightly problematic on NFS, where they are upgraded to
|
|
rpm-build |
0fba15 |
* POSIX locks, even though locally they are orthogonal to
|
|
rpm-build |
0fba15 |
* POSIX locks.
|
|
rpm-build |
0fba15 |
*/
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
t = g_strdup(p);
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
for (;;) {
|
|
rpm-build |
0fba15 |
#ifdef F_OFD_SETLK
|
|
rpm-build |
0fba15 |
struct flock fl = {
|
|
rpm-build |
0fba15 |
.l_type = (operation & ~LOCK_NB) == LOCK_EX ? F_WRLCK : F_RDLCK,
|
|
rpm-build |
0fba15 |
.l_whence = SEEK_SET,
|
|
rpm-build |
0fba15 |
};
|
|
rpm-build |
0fba15 |
#endif
|
|
rpm-build |
0fba15 |
struct stat st;
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
fd = openat(dfd, p, O_CREAT|O_RDWR|O_NOFOLLOW|O_CLOEXEC|O_NOCTTY, 0600);
|
|
rpm-build |
0fba15 |
if (fd < 0)
|
|
rpm-build |
0fba15 |
return glnx_throw_errno(error);
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
/* Unfortunately, new locks are not in RHEL 7.1 glibc */
|
|
rpm-build |
0fba15 |
#ifdef F_OFD_SETLK
|
|
rpm-build |
0fba15 |
r = fcntl(fd, (operation & LOCK_NB) ? F_OFD_SETLK : F_OFD_SETLKW, &fl);
|
|
rpm-build |
0fba15 |
#else
|
|
rpm-build |
0fba15 |
r = -1;
|
|
rpm-build |
0fba15 |
errno = EINVAL;
|
|
rpm-build |
0fba15 |
#endif
|
|
rpm-build |
0fba15 |
if (r < 0) {
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
/* If the kernel is too old, use good old BSD locks */
|
|
rpm-build |
0fba15 |
if (errno == EINVAL)
|
|
rpm-build |
0fba15 |
r = flock(fd, operation);
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
if (r < 0)
|
|
rpm-build |
0fba15 |
return glnx_throw_errno_prefix (error, "flock");
|
|
rpm-build |
0fba15 |
}
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
/* If we acquired the lock, let's check if the file
|
|
rpm-build |
0fba15 |
* still exists in the file system. If not, then the
|
|
rpm-build |
0fba15 |
* previous exclusive owner removed it and then closed
|
|
rpm-build |
0fba15 |
* it. In such a case our acquired lock is worthless,
|
|
rpm-build |
0fba15 |
* hence try again. */
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
if (!glnx_fstat (fd, &st, error))
|
|
rpm-build |
0fba15 |
return FALSE;
|
|
rpm-build |
0fba15 |
if (st.st_nlink > 0)
|
|
rpm-build |
0fba15 |
break;
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
glnx_close_fd (&fd;;
|
|
rpm-build |
0fba15 |
}
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
/* Note that if this is not AT_FDCWD, the caller takes responsibility
|
|
rpm-build |
0fba15 |
* for the fd's lifetime being >= that of the lock.
|
|
rpm-build |
0fba15 |
*/
|
|
rpm-build |
0fba15 |
out_lock->initialized = TRUE;
|
|
rpm-build |
0fba15 |
out_lock->dfd = dfd;
|
|
rpm-build |
0fba15 |
out_lock->path = g_steal_pointer (&t);
|
|
rpm-build |
0fba15 |
out_lock->fd = glnx_steal_fd (&fd;;
|
|
rpm-build |
0fba15 |
out_lock->operation = operation;
|
|
rpm-build |
0fba15 |
return TRUE;
|
|
rpm-build |
0fba15 |
}
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
void glnx_release_lock_file(GLnxLockFile *f) {
|
|
rpm-build |
0fba15 |
int r;
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
if (!(f && f->initialized))
|
|
rpm-build |
0fba15 |
return;
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
if (f->path) {
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
/* If we are the exclusive owner we can safely delete
|
|
rpm-build |
0fba15 |
* the lock file itself. If we are not the exclusive
|
|
rpm-build |
0fba15 |
* owner, we can try becoming it. */
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
if (f->fd >= 0 &&
|
|
rpm-build |
0fba15 |
(f->operation & ~LOCK_NB) == LOCK_SH) {
|
|
rpm-build |
0fba15 |
#ifdef F_OFD_SETLK
|
|
rpm-build |
0fba15 |
static const struct flock fl = {
|
|
rpm-build |
0fba15 |
.l_type = F_WRLCK,
|
|
rpm-build |
0fba15 |
.l_whence = SEEK_SET,
|
|
rpm-build |
0fba15 |
};
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
r = fcntl(f->fd, F_OFD_SETLK, &fl);
|
|
rpm-build |
0fba15 |
#else
|
|
rpm-build |
0fba15 |
r = -1;
|
|
rpm-build |
0fba15 |
errno = EINVAL;
|
|
rpm-build |
0fba15 |
#endif
|
|
rpm-build |
0fba15 |
if (r < 0 && errno == EINVAL)
|
|
rpm-build |
0fba15 |
r = flock(f->fd, LOCK_EX|LOCK_NB);
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
if (r >= 0)
|
|
rpm-build |
0fba15 |
f->operation = LOCK_EX|LOCK_NB;
|
|
rpm-build |
0fba15 |
}
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
if ((f->operation & ~LOCK_NB) == LOCK_EX) {
|
|
rpm-build |
0fba15 |
(void) unlinkat(f->dfd, f->path, 0);
|
|
rpm-build |
0fba15 |
}
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
g_free(f->path);
|
|
rpm-build |
0fba15 |
f->path = NULL;
|
|
rpm-build |
0fba15 |
}
|
|
rpm-build |
0fba15 |
|
|
rpm-build |
0fba15 |
glnx_close_fd (&f->fd);
|
|
rpm-build |
0fba15 |
f->operation = 0;
|
|
rpm-build |
0fba15 |
f->initialized = FALSE;
|
|
rpm-build |
0fba15 |
}
|