libassa 3.5.1
|
#include <PidFileLock.h>
Public Member Functions | |
PidFileLock () | |
Constructor. | |
~PidFileLock () | |
Destructor. | |
bool | lock (const string &filename_) |
Lock the file. | |
int | get_error () const |
Return last errno value. | |
const char * | get_error_msg () const |
In case of error, return a verbal description of the last error. | |
void | dump () |
Write the state of the lock to debug file. | |
Private Member Functions | |
pid_t | open_pid_file (const std::string &fname_) |
Open pid file in a cross-platform way. | |
int | lock_region () |
Lock the entire file. | |
int | lock_region_exclusive () |
Lock the entire file (only under Cygwin). | |
int | unlock_region () |
Unlock the entire file. | |
int | get_lock_status () |
Retrieve lock status. | |
int | write_pid () |
Write our process pid to the lock file. | |
pid_t | test_region () |
Test if file is unlocked. | |
void | log_error (const char *msg_) |
Log an error message to the log file and set internal error to errno. | |
Private Attributes | |
string | m_filename |
Lock file name. | |
int | m_fd |
Lock file descriptor. | |
int | m_error |
Last system call error. | |
string | m_error_msg |
Error explanation. | |
Definition at line 43 of file PidFileLock.h.
PidFileLock::PidFileLock | ( | ) |
Constructor.
Definition at line 31 of file PidFileLock.cpp.
References ASSA::PIDFLOCK, and trace_with_mask.
PidFileLock::~PidFileLock | ( | ) |
Destructor.
If process is holds the lock on the file, file is removed from the file system.
Definition at line 43 of file PidFileLock.cpp.
References DL, m_fd, m_filename, ASSA::PIDFLOCK, trace_with_mask, and unlock_region().
Write the state of the lock to debug file.
m_fd = -1
indicates that lock file is not opened.
Definition at line 383 of file PidFileLock.cpp.
References DL, get_error(), get_error_msg(), m_fd, m_filename, ASSA::PIDFLOCK, test_region(), and trace_with_mask.
|
inline |
In case of error, return a verbal description of the last error.
Definition at line 140 of file PidFileLock.h.
References m_error_msg.
Referenced by dump(), and ASSA::GenServer::init_internals().
|
private |
Retrieve lock status.
Read the file descriptor's flags.
On POSIX-compliant systems, on input to fcntl(F_GETLK), the l_type
describes a lock we would like to place on the file. If the lock could be placed, fcntl() does not actually place it, but returns F_UNLCK in the l_type and leaves the other fields of the structure unchanged. If, however, one or more incompatable locks would prevent this lock being placed, then fcntl() returns details about one of these locks in the l_type/l_whence/l_start/l_len fields and sets l_pid to be the PID of the process holding that lock. A lock can be removed explicitly with F_UNLCK or released automatically when the process terminates or if it closes any file descriptor referring to a file on which locks are held.
CYGWIN port does not support F_GETLK command with fcntl() because:
1) LockFileEx() is not implemented on 9x/ME 2) Lock requests given to LockFileEx() may not overlap existing locked regions of the file. 3) There is not nearly a functionality similar to F_GETLK in the Win32 API.
Instead, we try to set a lock. We might fail even if we already hold the lock ourselves. If we fail, try to unlock the file, and if that fails, then we know that file is locked by someone else. This method is not reliable, of course, but that's the best we can do.
Definition at line 293 of file PidFileLock.cpp.
References DL, EL, lock_region_exclusive(), m_fd, ASSA::PIDFLOCK, trace_with_mask, and unlock_region().
Referenced by test_region().
Lock the file.
Now that we have the lock, truncate file to zero length
Store our PID in the file
Set close-on-exec flag
Definition at line 61 of file PidFileLock.cpp.
References DL, get_error(), log_error(), m_error, m_fd, m_filename, open_pid_file(), ASSA::PIDFLOCK, ASSA::Utils::strenv(), trace_with_mask, and write_pid().
Referenced by ASSA::GenServer::init_internals().
|
private |
Lock the entire file.
Definition at line 177 of file PidFileLock.cpp.
References DL, m_fd, ASSA::PIDFLOCK, and trace_with_mask.
Referenced by open_pid_file(), and write_pid().
|
private |
Lock the entire file (only under Cygwin).
Definition at line 211 of file PidFileLock.cpp.
References DL, m_fd, ASSA::PIDFLOCK, and trace_with_mask.
Referenced by get_lock_status(), and write_pid().
Log an error message to the log file and set internal error to errno.
Definition at line 420 of file PidFileLock.cpp.
References ASSA::ASSAERR, EL, and m_error.
Referenced by lock(), and open_pid_file().
Open pid file in a cross-platform way.
Cygwin doesn't implement file locking via fcntl() - for it we test in one step.
If we cannot get lock status, or already have a lock, or if PID file is already locked by another process, then terminate. Otherwise (file is unlocked), proceed with locking.
Try to set a write lock on the entire file
Definition at line 434 of file PidFileLock.cpp.
References lock_region(), log_error(), m_error, m_fd, ASSA::PIDFLOCK, test_region(), and trace_with_mask.
Referenced by lock().
|
private |
Test if file is unlocked.
Test to see if file is locked by some other process.
If it is locked by us, return 0. If it is locked by some other process, return lock owner's PID.
Definition at line 354 of file PidFileLock.cpp.
References DL, get_lock_status(), ASSA::PIDFLOCK, and trace_with_mask.
Referenced by dump(), and open_pid_file().
|
private |
Unlock the entire file.
Definition at line 238 of file PidFileLock.cpp.
References DL, m_fd, ASSA::PIDFLOCK, and trace_with_mask.
Referenced by get_lock_status(), write_pid(), and ~PidFileLock().
|
private |
Write our process pid to the lock file.
Cygwin does not have POSIX semantics for locks.
We need to remove shared lock and then get an exclusive lock to write our PID. Then remove the exclusive lock and replace it with the shared lock. This leave two chances for another process to steal the lock from us, but this is the best we can do.
An exclusive lock under Cygwin prevents other processes to even open a file for read-only operations!
Definition at line 131 of file PidFileLock.cpp.
References DL, lock_region(), lock_region_exclusive(), m_fd, ASSA::PIDFLOCK, trace_with_mask, and unlock_region().
Referenced by lock().
|
private |
Last system call error.
Definition at line 126 of file PidFileLock.h.
Referenced by get_error(), lock(), log_error(), and open_pid_file().
|
private |
|
private |
Lock file descriptor.
Definition at line 123 of file PidFileLock.h.
Referenced by dump(), get_lock_status(), lock(), lock_region(), lock_region_exclusive(), open_pid_file(), unlock_region(), write_pid(), and ~PidFileLock().
|
private |
Lock file name.
Definition at line 120 of file PidFileLock.h.
Referenced by dump(), lock(), and ~PidFileLock().