libassa  3.5.1
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>
24 using std::string;
25 
26 #if defined(WIN32)
27 struct 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 
36 namespace ASSA {
37 
43 class PidFileLock : public flock
44 {
45 public:
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 
74 private:
77  pid_t open_pid_file (const std::string& fname_);
78 
83  int lock_region ();
84 
89  int lock_region_exclusive ();
90 
94  int unlock_region ();
95 
100  int get_lock_status ();
101 
102 
106  int write_pid ();
107 
112  pid_t test_region ();
113 
116  void log_error (const char* msg_);
117 
118 private:
120  string m_filename;
121 
123  int m_fd;
124 
126  int m_error;
127 
129  string m_error_msg;
130 };
131 
132 inline int
134 get_error () const
135 {
136  return m_error;
137 }
138 
139 inline const char*
141 get_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.
int get_lock_status()
Retrieve lock status.
~PidFileLock()
Destructor.
Definition: PidFileLock.cpp:44
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.
Definition: PidFileLock.h:126
const char * get_error_msg() const
In case of error, return a verbal description of the last error.
Definition: PidFileLock.h:141
PidFileLock()
Constructor.
Definition: PidFileLock.cpp:32
int get_error() const
Return last errno value.
Definition: PidFileLock.h:134
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.
Definition: PidFileLock.h:129
pid_t test_region()
Test if file is unlocked.
string m_filename
Lock file name.
Definition: PidFileLock.h:120
int m_fd
Lock file descriptor.
Definition: PidFileLock.h:123
void dump()
Write the state of the lock to debug file.
bool lock(const string &filename_)
Lock the file.
Definition: PidFileLock.cpp:62
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.
Definition: Acceptor.h:40