1
0
Fork 0

make file locking a bit more sensible (#5012)

This commit is contained in:
Jan 2018-04-13 11:06:47 +02:00 committed by GitHub
parent 5324aaf6b3
commit 0e9fa1a08f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 18 additions and 31 deletions

View File

@ -1154,20 +1154,9 @@ int TRI_VerifyLockFile(char const* filename) {
sizeof(buffer)); // not really necessary, but this shuts up valgrind
ssize_t n = TRI_READ(fd, buffer, static_cast<TRI_read_t>(sizeof(buffer)));
if (n < 0) {
TRI_TRACKED_CLOSE_FILE(fd);
return TRI_ERROR_NO_ERROR;
}
TRI_DEFER(TRI_TRACKED_CLOSE_FILE(fd));
// pid too long
if (n == sizeof(buffer)) {
TRI_TRACKED_CLOSE_FILE(fd);
return TRI_ERROR_NO_ERROR;
}
// file empty
if (n == 0) {
TRI_TRACKED_CLOSE_FILE(fd);
if (n <= 0 || n == sizeof(buffer)) {
return TRI_ERROR_NO_ERROR;
}
@ -1175,25 +1164,27 @@ int TRI_VerifyLockFile(char const* filename) {
int res = TRI_errno();
if (res != TRI_ERROR_NO_ERROR) {
TRI_TRACKED_CLOSE_FILE(fd);
// invalid pid value
return TRI_ERROR_NO_ERROR;
}
TRI_pid_t pid = fc;
if (pid == Thread::currentProcessId()) {
TRI_TRACKED_CLOSE_FILE(fd);
return TRI_ERROR_NO_ERROR;
}
// check for the existence of previous process via kill command
// from man 2 kill:
// If sig is 0, then no signal is sent, but existence and permission checks are still performed;
// this can be used to check for the existence of a process ID or process group ID that the caller
// is permitted to signal.
if (kill(pid, 0) == -1) {
TRI_TRACKED_CLOSE_FILE(fd);
return TRI_ERROR_NO_ERROR;
LOG_TOPIC(WARN, arangodb::Logger::FIXME) << "found existing lockfile '" << filename << "' of previous process with pid " << pid << ", but that process seems to be dead already";
} else {
LOG_TOPIC(WARN, arangodb::Logger::FIXME) << "found existing lockfile '" << filename << "' of previous process with pid " << pid << ", and that process seems to be still running";
}
#ifdef TRI_HAVE_SETLK
struct flock lock;
lock.l_start = 0;
lock.l_len = 0;
lock.l_type = F_WRLCK;
@ -1203,21 +1194,19 @@ int TRI_VerifyLockFile(char const* filename) {
// file was not yet locked; could be locked
if (canLock == 0) {
lock.l_type = F_UNLCK;
//lock.l_type = F_UNLCK;
res = fcntl(fd, F_GETLK, &lock);
if (res != TRI_ERROR_NO_ERROR) {
canLock = errno;
TRI_set_errno(TRI_ERROR_SYS_ERROR);
LOG_TOPIC(WARN, arangodb::Logger::FIXME) << "fcntl on lockfile '" << filename
<< "' failed: " << TRI_errno_string(canLock)
<< ". a possible reason is that the filesystem does not support file-locking";
<< "' failed: " << TRI_last_error();
}
TRI_TRACKED_CLOSE_FILE(fd);
return TRI_ERROR_NO_ERROR;
}
// error!
canLock = errno;
TRI_set_errno(TRI_ERROR_SYS_ERROR);
@ -1230,8 +1219,6 @@ int TRI_VerifyLockFile(char const* filename) {
}
#endif
TRI_TRACKED_CLOSE_FILE(fd);
return TRI_ERROR_ARANGO_DATADIR_LOCKED;
}