sbuild
1.5.3
|
00001 /* Copyright © 2005-2007 Roger Leigh <rleigh@debian.org> 00002 * 00003 * schroot is free software: you can redistribute it and/or modify it 00004 * under the terms of the GNU General Public License as published by 00005 * the Free Software Foundation, either version 3 of the License, or 00006 * (at your option) any later version. 00007 * 00008 * schroot is distributed in the hope that it will be useful, but 00009 * WITHOUT ANY WARRANTY; without even the implied warranty of 00010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00011 * General Public License for more details. 00012 * 00013 * You should have received a copy of the GNU General Public License 00014 * along with this program. If not, see 00015 * <http://www.gnu.org/licenses/>. 00016 * 00017 *********************************************************************/ 00018 00019 #ifndef SBUILD_LOCK_H 00020 #define SBUILD_LOCK_H 00021 00022 #include <sbuild/sbuild-lock.h> 00023 #include <sbuild/sbuild-custom-error.h> 00024 00025 #include <string> 00026 00027 #include <sys/time.h> 00028 #include <fcntl.h> 00029 #include <signal.h> 00030 00031 namespace sbuild 00032 { 00033 00038 class lock 00039 { 00040 public: 00042 enum type 00043 { 00044 LOCK_SHARED = F_RDLCK, 00045 LOCK_EXCLUSIVE = F_WRLCK, 00046 LOCK_NONE = F_UNLCK 00047 }; 00048 00050 enum error_code 00051 { 00052 TIMEOUT_HANDLER, 00053 TIMEOUT_SET, 00054 TIMEOUT_CANCEL, 00055 LOCK, 00056 UNLOCK, 00057 LOCK_TIMEOUT, 00058 UNLOCK_TIMEOUT, 00059 DEVICE_LOCK, 00060 DEVICE_LOCK_TIMEOUT, 00061 DEVICE_TEST, 00062 DEVICE_UNLOCK, 00063 DEVICE_UNLOCK_TIMEOUT 00064 }; 00065 00067 typedef custom_error<error_code> error; 00068 00075 virtual void 00076 set_lock (type lock_type, 00077 unsigned int timeout) = 0; 00078 00083 virtual void 00084 unset_lock () = 0; 00085 00086 protected: 00088 lock (); 00090 virtual ~lock (); 00091 00097 void 00098 set_alarm (); 00099 00104 void 00105 clear_alarm (); 00106 00116 void 00117 set_timer (struct itimerval const& timer); 00118 00125 void 00126 unset_timer (); 00127 00128 private: 00130 struct sigaction saved_signals; 00131 }; 00132 00137 class file_lock : public lock 00138 { 00139 public: 00145 file_lock (int fd); 00146 00148 virtual ~file_lock (); 00149 00150 virtual void 00151 set_lock (lock::type lock_type, 00152 unsigned int timeout); 00153 00154 virtual void 00155 unset_lock (); 00156 00157 private: 00159 int fd; 00161 bool locked; 00162 }; 00163 00164 #ifdef SBUILD_FEATURE_DEVLOCK 00165 00171 class device_lock : public lock 00172 { 00173 public: 00179 device_lock (std::string const& device); 00180 00182 virtual ~device_lock (); 00183 00184 virtual void 00185 set_lock (lock::type lock_type, 00186 unsigned int timeout); 00187 00188 virtual void 00189 unset_lock (); 00190 00191 private: 00193 std::string device; 00195 bool locked; 00196 }; 00197 #endif // SBUILD_FEATURE_DEVLOCK 00198 00199 } 00200 00201 #endif /* SBUILD_LOCK_H */ 00202 00203 /* 00204 * Local Variables: 00205 * mode:C++ 00206 * End: 00207 */