file-dotlock.c revision 892f02d4ab8f764f86015009aaf7437349398286
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync/* Copyright (C) 2003 Timo Sirainen */
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync/* 0.1 .. 0.2msec */
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync#define LOCK_RANDOM_USLEEP_TIME (100000 + (unsigned int)rand() % 100000)
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync /* read line */
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync /* fix the string */
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync /* it should contain pid:host */
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync /* host must be ours */
1b33c96954667ba382fa595baf7b31290bfdd517vboxsyncstatic int check_lock(time_t now, struct lock_info *lock_info)
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync i_error("lstat(%s) failed: %m", lock_info->lock_path);
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync /* either our first check or someone else got the lock file.
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync check if it contains a pid whose existence we can verify */
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync lock_info->pid = read_local_pid(lock_info->lock_path);
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync /* we've local PID. Check if it exists. */
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync if (kill(lock_info->pid, 0) == 0 || errno != ESRCH)
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync /* doesn't exist - go ahead and delete */
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync if (unlink(lock_info->lock_path) < 0 && errno != ENOENT) {
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync i_error("unlink(%s) failed: %m", lock_info->lock_path);
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync /* see if the file we're locking is being modified */
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync /* file doesn't exist. treat it as if
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync it hasn't changed */
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync if (now > lock_info->last_change + (time_t)lock_info->stale_timeout) {
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync /* no changes for a while, assume stale lock */
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync if (unlink(lock_info->lock_path) < 0 && errno != ENOENT) {
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync i_error("unlink(%s) failed: %m", lock_info->lock_path);
1b33c96954667ba382fa595baf7b31290bfdd517vboxsyncstatic int create_temp_file(const char *prefix, const char **path_r)
1b33c96954667ba382fa595baf7b31290bfdd517vboxsync fd = open(*path_r, O_RDWR | O_EXCL | O_CREAT, 0644);
const char *str, *p;
if (p != NULL) {
void *context),
void *context)
const char *lock_path;
unsigned int stale_notify_threshold;
if (do_wait) {
if (ret < 0)
if (checkonly)
if (ret != 0)
t_push();
t_pop();
if (ret <= 0) {
if (ret == 0)
return ret;
void *context),
return ret;
const char *lock_path;
void *context),
void *context)
if (ret <= 0)
return fd;
const char *lock_path;
if (verify_owner) {
if (verify_owner) {