00001 // -*- c++ -*- 00002 //------------------------------------------------------------------------------ 00003 // PidFileLock.h 00004 //------------------------------------------------------------------------------ 00005 // $Id: PidFileLock.h,v 1.6 2005/09/29 02:35:23 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 namespace ASSA { 00027 00033 class PidFileLock : public flock 00034 { 00035 public: 00037 PidFileLock (); 00038 00042 ~PidFileLock (); 00043 00047 bool lock (const string& filename_); 00048 00052 int get_error () const; 00053 00057 const char* get_error_msg () const; 00058 00062 void dump (); 00063 00064 private: 00067 pid_t open_pid_file (const std::string& fname_); 00068 00073 int lock_region (); 00074 00079 int lock_region_exclusive (); 00080 00084 int unlock_region (); 00085 00090 int get_lock_status (); 00091 00092 00096 int write_pid (); 00097 00102 pid_t test_region (); 00103 00106 void log_error (const char* msg_); 00107 00108 private: 00110 string m_filename; 00111 00113 int m_fd; 00114 00116 int m_error; 00117 00119 string m_error_msg; 00120 }; 00121 00122 inline int 00123 PidFileLock:: 00124 get_error () const 00125 { 00126 return m_error; 00127 } 00128 00129 inline const char* 00130 PidFileLock:: 00131 get_error_msg () const 00132 { 00133 return m_error_msg.c_str (); 00134 } 00135 00136 } // end namespace ASSA 00137 00138 #endif /* FILE_LOCK_H */