libassa 3.5.1
Loading...
Searching...
No Matches
PidFileLock.h
Go to the documentation of this file.
1// -*- c++ -*-
2//------------------------------------------------------------------------------
3// PidFileLock.h
4//------------------------------------------------------------------------------
5// $Id: PidFileLock.h,v 1.7 2006/07/20 02:30:54 vlg Exp $
6//------------------------------------------------------------------------------
7// Copyright (C) 1997-2002,2005 Vladislav Grinchenko
8//
9// This library is free software; you can redistribute it and/or
10// modify it under the terms of the GNU Library General Public
11// License as published by the Free Software Foundation; either
12// version 2 of the License, or (at your option) any later version.
13//------------------------------------------------------------------------------
14#ifndef FILE_LOCK_H
15#define FILE_LOCK_H
16
17//System Includes
18#include <sys/types.h>
19
20#include "assa/Assure.h"
21
22//STL
23#include <string>
24using std::string;
25
26#if defined(WIN32)
27struct flock {
28 off_t l_start;
29 off_t l_len;
30 pid_t l_pid;
31 short l_type;
32 short l_whence;
33};
34#endif
35
36namespace ASSA {
37
43class PidFileLock : public flock
44{
45public:
47 PidFileLock ();
48
52 ~PidFileLock ();
53
57 bool lock (const string& filename_);
58
62 int get_error () const;
63
67 const char* get_error_msg () const;
68
72 void dump ();
73
74private:
77 pid_t open_pid_file (const std::string& fname_);
78
83 int lock_region ();
84
90
94 int unlock_region ();
95
100 int get_lock_status ();
101
102
106 int write_pid ();
107
113
116 void log_error (const char* msg_);
117
118private:
121
123 int m_fd;
124
127
130};
131
132inline int
134get_error () const
135{
136 return m_error;
137}
138
139inline const char*
141get_error_msg () const
142{
143 return m_error_msg.c_str ();
144}
145
146} // end namespace ASSA
147
148#endif /* FILE_LOCK_H */
A collection of assert function wrappers.
A wrapper class to provide AutoPtr with reference semantics.
Definition AutoPtr.h:32
int get_lock_status()
Retrieve lock status.
~PidFileLock()
Destructor.
int write_pid()
Write our process pid to the lock file.
pid_t open_pid_file(const std::string &fname_)
Open pid file in a cross-platform way.
int m_error
Last system call error.
const char * get_error_msg() const
In case of error, return a verbal description of the last error.
PidFileLock()
Constructor.
int get_error() const
Return last errno value.
void log_error(const char *msg_)
Log an error message to the log file and set internal error to errno.
string m_error_msg
Error explanation.
pid_t test_region()
Test if file is unlocked.
string m_filename
Lock file name.
int m_fd
Lock file descriptor.
void dump()
Write the state of the lock to debug file.
bool lock(const string &filename_)
Lock the file.
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.