00001 // -*- c++ -*- 00002 //------------------------------------------------------------------------------ 00003 // PidFileLock.h 00004 //------------------------------------------------------------------------------ 00005 // $Id: PidFileLock.h,v 1.7 2006/07/20 02:30:54 vlg Exp $ 00006 //------------------------------------------------------------------------------ 00007 // Copyright (C) 1997-2002,2005 Vladislav Grinchenko 00008 // 00009 // This library is free software; you can redistribute it and/or 00010 // modify it under the terms of the GNU Library General Public 00011 // License as published by the Free Software Foundation; either 00012 // version 2 of the License, or (at your option) any later version. 00013 //------------------------------------------------------------------------------ 00014 #ifndef FILE_LOCK_H 00015 #define FILE_LOCK_H 00016 00017 //System Includes 00018 #include <sys/types.h> 00019 00020 #include "assa/Assure.h" 00021 00022 //STL 00023 #include <string> 00024 using std::string; 00025 00026 #if defined(WIN32) 00027 struct flock { 00028 off_t l_start; 00029 off_t l_len; 00030 pid_t l_pid; 00031 short l_type; 00032 short l_whence; 00033 }; 00034 #endif 00035 00036 namespace ASSA { 00037 00043 class PidFileLock : public flock 00044 { 00045 public: 00047 PidFileLock (); 00048 00052 ~PidFileLock (); 00053 00057 bool lock (const string& filename_); 00058 00062 int get_error () const; 00063 00067 const char* get_error_msg () const; 00068 00072 void dump (); 00073 00074 private: 00077 pid_t open_pid_file (const std::string& fname_); 00078 00083 int lock_region (); 00084 00089 int lock_region_exclusive (); 00090 00094 int unlock_region (); 00095 00100 int get_lock_status (); 00101 00102 00106 int write_pid (); 00107 00112 pid_t test_region (); 00113 00116 void log_error (const char* msg_); 00117 00118 private: 00120 string m_filename; 00121 00123 int m_fd; 00124 00126 int m_error; 00127 00129 string m_error_msg; 00130 }; 00131 00132 inline int 00133 PidFileLock:: 00134 get_error () const 00135 { 00136 return m_error; 00137 } 00138 00139 inline const char* 00140 PidFileLock:: 00141 get_error_msg () const 00142 { 00143 return m_error_msg.c_str (); 00144 } 00145 00146 } // end namespace ASSA 00147 00148 #endif /* FILE_LOCK_H */