lockfile-util.c revision b26fa1a2fbcfee7d03b0c8fd15ec3aa64ae70b9f
/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include "alloc-util.h"
#include "fd-util.h"
#include "fs-util.h"
#include "lockfile-util.h"
#include "macro.h"
#include "path-util.h"
_cleanup_free_ char *t = NULL;
int r;
/*
* We use UNPOSIX locks if they are available. They have nice
* semantics, and are mostly compatible with NFS. However,
* they are only available on new kernels. When we detect we
* are running on an older kernel, then we fall back to good
* old BSD locks. They also have nice semantics, but are
* slightly problematic on NFS, where they are upgraded to
* POSIX locks, even though locally they are orthogonal to
* POSIX locks.
*/
t = strdup(p);
if (!t)
return -ENOMEM;
for (;;) {
};
if (fd < 0)
return -errno;
if (r < 0) {
/* If the kernel is too old, use good old BSD locks */
if (r < 0)
}
/* If we acquired the lock, let's check if the file
* still exists in the file system. If not, then the
* previous exclusive owner removed it and then closed
* it. In such a case our acquired lock is worthless,
* hence try again. */
if (r < 0)
return -errno;
break;
}
fd = -1;
t = NULL;
return r;
}
const char *fn;
char *t;
assert(p);
if (!filename_is_valid(fn))
return -EINVAL;
}
void release_lock_file(LockFile *f) {
int r;
if (!f)
return;
if (f->path) {
/* If we are the exclusive owner we can safely delete
* the lock file itself. If we are not the exclusive
* owner, we can try becoming it. */
if (f->fd >= 0 &&
};
if (r >= 0)
}
unlink_noerrno(f->path);
}
f->operation = 0;
}