sbuild  1.6.5
sbuild-lock.h
1 /* Copyright © 2005-2007 Roger Leigh <rleigh@debian.org>
2  *
3  * schroot is free software: you can redistribute it and/or modify it
4  * under the terms of the GNU General Public License as published by
5  * the Free Software Foundation, either version 3 of the License, or
6  * (at your option) any later version.
7  *
8  * schroot is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see
15  * <http://www.gnu.org/licenses/>.
16  *
17  *********************************************************************/
18 
19 #ifndef SBUILD_LOCK_H
20 #define SBUILD_LOCK_H
21 
22 #include <sbuild/sbuild-lock.h>
23 #include <sbuild/sbuild-custom-error.h>
24 
25 #include <string>
26 
27 #include <sys/time.h>
28 #include <fcntl.h>
29 #include <signal.h>
30 
31 namespace sbuild
32 {
33 
38  class lock
39  {
40  public:
42  enum type
43  {
44  LOCK_SHARED = F_RDLCK,
45  LOCK_EXCLUSIVE = F_WRLCK,
46  LOCK_NONE = F_UNLCK
47  };
48 
51  {
55  LOCK,
64  };
65 
68 
75  virtual void
76  set_lock (type lock_type,
77  unsigned int timeout) = 0;
78 
83  virtual void
84  unset_lock () = 0;
85 
86  protected:
88  lock ();
90  virtual ~lock ();
91 
97  void
98  set_alarm ();
99 
104  void
105  clear_alarm ();
106 
116  void
117  set_timer (struct itimerval const& timer);
118 
125  void
126  unset_timer ();
127 
128  private:
130  struct sigaction saved_signals;
131  };
132 
137  class file_lock : public lock
138  {
139  public:
145  file_lock (int fd);
146 
148  virtual ~file_lock ();
149 
150  virtual void
151  set_lock (lock::type lock_type,
152  unsigned int timeout);
153 
154  virtual void
155  unset_lock ();
156 
157  private:
159  int fd;
161  bool locked;
162  };
163 
164 #ifdef SBUILD_FEATURE_DEVLOCK
165 
171  class device_lock : public lock
172  {
173  public:
179  device_lock (std::string const& device);
180 
182  virtual ~device_lock ();
183 
184  virtual void
185  set_lock (lock::type lock_type,
186  unsigned int timeout);
187 
188  virtual void
189  unset_lock ();
190 
191  private:
193  std::string device;
195  bool locked;
196  };
197 #endif // SBUILD_FEATURE_DEVLOCK
198 
199 }
200 
201 #endif /* SBUILD_LOCK_H */
202 
203 /*
204  * Local Variables:
205  * mode:C++
206  * End:
207  */
A shared (read) lock.
Definition: sbuild-lock.h:44
Failed to lock file (timed out).
Definition: sbuild-lock.h:57
struct sigaction saved_signals
Signals saved during timeout.
Definition: sbuild-lock.h:130
Failed to unlock device (timed out)
Definition: sbuild-lock.h:63
virtual void set_lock(lock::type lock_type, unsigned int timeout)
Acquire a lock.
Definition: sbuild-lock.cc:267
Failed to unlock file.
Definition: sbuild-lock.h:56
void unset_timer()
Remove any itimer currently set up.
Definition: sbuild-lock.cc:140
virtual void unset_lock()
Release a lock.
Definition: sbuild-lock.cc:236
No lock.
Definition: sbuild-lock.h:46
virtual void unset_lock()=0
Release a lock.
Failed to lock device.
Definition: sbuild-lock.h:59
void set_timer(struct itimerval const &timer)
Set up an itimer for future expiry.
Definition: sbuild-lock.cc:128
File lock.
Definition: sbuild-lock.h:137
Advisory locking.
Definition: sbuild-lock.h:38
virtual ~device_lock()
The destructor.
Definition: sbuild-lock.cc:255
An exclusive (write) lock.
Definition: sbuild-lock.h:45
custom_error< error_code > error
Exception type.
Definition: sbuild-lock.h:67
error_code
Error codes.
Definition: sbuild-lock.h:50
Failed to unlock device.
Definition: sbuild-lock.h:62
bool locked
Is the file locked?
Definition: sbuild-lock.h:195
virtual void set_lock(type lock_type, unsigned int timeout)=0
Acquire a lock.
int fd
The file descriptor to lock.
Definition: sbuild-lock.h:159
bool locked
Is the file locked?
Definition: sbuild-lock.h:161
virtual void unset_lock()
Release a lock.
Definition: sbuild-lock.cc:345
Custom error.
Definition: sbuild-custom-error.h:32
device_lock(std::string const &device)
The constructor.
Definition: sbuild-lock.cc:248
Failed to set timeout.
Definition: sbuild-lock.h:53
Failed to cancel timeout.
Definition: sbuild-lock.h:54
Failed to set timeout handler.
Definition: sbuild-lock.h:52
virtual ~lock()
The destructor.
Definition: sbuild-lock.cc:104
void set_alarm()
Set the SIGALARM handler.
Definition: sbuild-lock.cc:109
std::string device
The device to lock.
Definition: sbuild-lock.h:193
virtual void set_lock(lock::type lock_type, unsigned int timeout)
Acquire a lock.
Definition: sbuild-lock.cc:182
file_lock(int fd)
The constructor.
Definition: sbuild-lock.cc:155
virtual ~file_lock()
The destructor.
Definition: sbuild-lock.cc:162
Failed to test device lock.
Definition: sbuild-lock.h:61
void clear_alarm()
Restore the state of SIGALRM prior to starting lock acquisition.
Definition: sbuild-lock.cc:121
Failed to lock device (timed out).
Definition: sbuild-lock.h:60
lock()
The constructor.
Definition: sbuild-lock.cc:99
Failed to unlock file (timed out).
Definition: sbuild-lock.h:58
Failed to lock file.
Definition: sbuild-lock.h:55
type
Lock type.
Definition: sbuild-lock.h:42
Device lock.
Definition: sbuild-lock.h:171