2008-02-27 20:56:47 +03:00
|
|
|
/*
|
|
|
|
* libcsync -- a library to sync a directory with another
|
|
|
|
*
|
|
|
|
* Copyright (c) 2006 by Andreas Schneider <mail@cynapses.org>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* vim: ts=2 sw=2 et cindent
|
|
|
|
*/
|
|
|
|
|
2008-06-09 19:19:12 +04:00
|
|
|
#ifndef _GNU_SOURCE
|
2008-02-27 20:56:47 +03:00
|
|
|
#define _GNU_SOURCE
|
2008-06-09 19:19:12 +04:00
|
|
|
#endif
|
|
|
|
|
2008-02-27 20:56:47 +03:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "c_lib.h"
|
|
|
|
#include "csync_lock.h"
|
|
|
|
|
|
|
|
#define CSYNC_LOG_CATEGORY_NAME "csync.lock"
|
|
|
|
#include "csync_log.h"
|
|
|
|
|
2008-06-02 17:11:45 +04:00
|
|
|
static int _csync_lock_create(const char *lockfile) {
|
2008-02-27 20:56:47 +03:00
|
|
|
int fd, pid, rc = -1;
|
2008-03-03 16:11:14 +03:00
|
|
|
char *ctmpfile = NULL;
|
2008-02-27 20:56:47 +03:00
|
|
|
char *dir = NULL;
|
|
|
|
char *buf = NULL;
|
|
|
|
|
|
|
|
pid = getpid();
|
|
|
|
|
|
|
|
dir = c_dirname(lockfile);
|
|
|
|
if (dir == NULL) {
|
|
|
|
rc = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2008-03-03 16:11:14 +03:00
|
|
|
if (asprintf(&ctmpfile, "%s/tmp_lock_XXXXXX", dir) < 0) {
|
2008-02-27 20:56:47 +03:00
|
|
|
rc = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2008-03-03 16:11:14 +03:00
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "Create temporary lock file: %s", ctmpfile);
|
|
|
|
if ((fd = mkstemp(ctmpfile)) < 0) {
|
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR, "Unable to create temporary lock file: %s - %s", ctmpfile, strerror(errno));
|
2008-02-27 20:56:47 +03:00
|
|
|
rc = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2008-03-03 16:11:14 +03:00
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "Write pid (%d) to temporary lock file: %s", pid, ctmpfile);
|
2008-02-27 20:56:47 +03:00
|
|
|
pid = asprintf(&buf, "%d\n", pid);
|
|
|
|
if (write(fd, buf, pid) == pid) {
|
|
|
|
/* Create lock file */
|
2008-03-03 16:11:14 +03:00
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "Create a hardlink from %s to %s.", ctmpfile, lockfile);
|
|
|
|
if (link(ctmpfile, lockfile) < 0 ) {
|
2008-02-27 20:56:47 +03:00
|
|
|
/* Oops, alredy locked */
|
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_INFO, "Already locked: %s - %s", lockfile, strerror(errno));
|
|
|
|
rc = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
} else {
|
2008-03-03 16:11:14 +03:00
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR, "Can't create %s - %s", ctmpfile, strerror(errno));
|
2008-02-27 20:56:47 +03:00
|
|
|
rc = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = 0;
|
|
|
|
|
|
|
|
out:
|
|
|
|
close(fd);
|
2008-03-03 16:11:14 +03:00
|
|
|
unlink(ctmpfile);
|
2008-02-27 20:56:47 +03:00
|
|
|
|
|
|
|
SAFE_FREE(buf);
|
|
|
|
SAFE_FREE(dir);
|
2008-03-03 16:11:14 +03:00
|
|
|
SAFE_FREE(ctmpfile);
|
2008-02-27 20:56:47 +03:00
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2008-06-02 17:11:45 +04:00
|
|
|
static pid_t _csync_lock_read(const char *lockfile) {
|
2008-02-27 20:56:47 +03:00
|
|
|
char buf[8] = {0};
|
|
|
|
int fd, pid;
|
|
|
|
|
|
|
|
/* Read PID from existing lock */
|
|
|
|
if ((fd = open(lockfile, O_RDONLY)) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
pid = read(fd, buf, sizeof(buf));
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
if (pid <= 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf[sizeof(buf) - 1] = '\0';
|
|
|
|
pid = strtol(buf, NULL, 10);
|
|
|
|
if (!pid || errno == ERANGE) {
|
|
|
|
/* Broken lock file */
|
|
|
|
if (unlink(lockfile) < 0) {
|
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR, "Unable to remove broken lock %s - %s", lockfile, strerror(errno));
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if process is still alive */
|
|
|
|
if (kill(pid, 0) < 0 && errno == ESRCH) {
|
|
|
|
/* Process is dead. Remove stale lock. */
|
|
|
|
if (unlink(lockfile) < 0) {
|
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR, "Unable to remove stale lock %s - %s", lockfile, strerror(errno));
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pid;
|
|
|
|
}
|
|
|
|
|
|
|
|
int csync_lock(const char *lockfile) {
|
|
|
|
/* Check if lock already exists. */
|
2008-06-02 17:11:45 +04:00
|
|
|
if (_csync_lock_read(lockfile) > 0) {
|
2008-02-27 20:56:47 +03:00
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR, "Aborting, another synchronization process is running.");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_INFO, "Creating lock file: %s", lockfile);
|
|
|
|
|
2008-06-02 17:11:45 +04:00
|
|
|
return _csync_lock_create(lockfile);
|
2008-02-27 20:56:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void csync_lock_remove(const char *lockfile) {
|
|
|
|
/* You can't remove the lock if it is from another process */
|
2008-06-02 17:11:45 +04:00
|
|
|
if (_csync_lock_read(lockfile) == getpid()) {
|
2008-02-27 20:56:47 +03:00
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG, "Removing lock file: %s", lockfile);
|
|
|
|
if (unlink(lockfile) < 0) {
|
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR, "Unable to remove lock %s - %s", lockfile, strerror(errno));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|