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 | |
---|
34 | /** |
---|
35 | * Advisory locking. This class defines a simple interface for |
---|
36 | * shared and exclusive locks. |
---|
37 | */ |
---|
38 | class lock |
---|
39 | { |
---|
40 | public: |
---|
41 | /// Lock type. |
---|
42 | enum type |
---|
43 | { |
---|
44 | LOCK_SHARED = F_RDLCK, ///< A shared (read) lock. |
---|
45 | LOCK_EXCLUSIVE = F_WRLCK, ///< An exclusive (write) lock. |
---|
46 | LOCK_NONE = F_UNLCK ///< No lock. |
---|
47 | }; |
---|
48 | |
---|
49 | /// Error codes. |
---|
50 | enum error_code |
---|
51 | { |
---|
52 | TIMEOUT_HANDLER, ///< Failed to set timeout handler. |
---|
53 | TIMEOUT_SET, ///< Failed to set timeout. |
---|
54 | TIMEOUT_CANCEL, ///< Failed to cancel timeout. |
---|
55 | LOCK, ///< Failed to lock file. |
---|
56 | UNLOCK, ///< Failed to unlock file. |
---|
57 | LOCK_TIMEOUT, ///< Failed to lock file (timed out). |
---|
58 | UNLOCK_TIMEOUT, ///< Failed to unlock file (timed out). |
---|
59 | DEVICE_LOCK, ///< Failed to lock device. |
---|
60 | DEVICE_LOCK_TIMEOUT, ///< Failed to lock device (timed out). |
---|
61 | DEVICE_TEST, ///< Failed to test device lock. |
---|
62 | DEVICE_UNLOCK, ///< Failed to unlock device. |
---|
63 | DEVICE_UNLOCK_TIMEOUT ///< Failed to unlock device (timed out) |
---|
64 | }; |
---|
65 | |
---|
66 | /// Exception type. |
---|
67 | typedef custom_error<error_code> error; |
---|
68 | |
---|
69 | /** |
---|
70 | * Acquire a lock. |
---|
71 | * |
---|
72 | * @param lock_type the type of lock to acquire. |
---|
73 | * @param timeout the time in seconds to wait on the lock. |
---|
74 | */ |
---|
75 | virtual void |
---|
76 | set_lock (type lock_type, |
---|
77 | unsigned int timeout) = 0; |
---|
78 | |
---|
79 | /** |
---|
80 | * Release a lock. This is equivalent to set_lock with a |
---|
81 | * lock_type of LOCK_NONE and a timeout of 0. |
---|
82 | */ |
---|
83 | virtual void |
---|
84 | unset_lock () = 0; |
---|
85 | |
---|
86 | protected: |
---|
87 | /// The constructor. |
---|
88 | lock (); |
---|
89 | /// The destructor. |
---|
90 | virtual ~lock (); |
---|
91 | |
---|
92 | /** |
---|
93 | * Set the SIGALARM handler. |
---|
94 | * |
---|
95 | * An error will be thrown on failure. |
---|
96 | */ |
---|
97 | void |
---|
98 | set_alarm (); |
---|
99 | |
---|
100 | /** |
---|
101 | * Restore the state of SIGALRM prior to starting lock |
---|
102 | * acquisition. |
---|
103 | */ |
---|
104 | void |
---|
105 | clear_alarm (); |
---|
106 | |
---|
107 | /** |
---|
108 | * Set up an itimer for future expiry. This is used to interrupt |
---|
109 | * system calls. This will set a handler for SIGALRM as a side |
---|
110 | * effect (using set_alarm). |
---|
111 | * |
---|
112 | * An error will be thrown on failure. |
---|
113 | * |
---|
114 | * @param timer the timeout to set. |
---|
115 | */ |
---|
116 | void |
---|
117 | set_timer (struct itimerval const& timer); |
---|
118 | |
---|
119 | /** |
---|
120 | * Remove any itimer currently set up. This will clear any |
---|
121 | * SIGALRM handler (using clear_alarm). |
---|
122 | * |
---|
123 | * An error will be thrown on failure. |
---|
124 | */ |
---|
125 | void |
---|
126 | unset_timer (); |
---|
127 | |
---|
128 | private: |
---|
129 | /// Signals saved during timeout. |
---|
130 | struct sigaction saved_signals; |
---|
131 | }; |
---|
132 | |
---|
133 | /** |
---|
134 | * File lock. Simple whole-file shared and exclusive advisory |
---|
135 | * locking based upon POSIX fcntl byte region locks. |
---|
136 | */ |
---|
137 | class file_lock : public lock |
---|
138 | { |
---|
139 | public: |
---|
140 | /** |
---|
141 | * The constructor. |
---|
142 | * |
---|
143 | * @param fd the file descriptor to lock. |
---|
144 | */ |
---|
145 | file_lock (int fd); |
---|
146 | |
---|
147 | /// The destructor. |
---|
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: |
---|
158 | /// The file descriptor to lock. |
---|
159 | int fd; |
---|
160 | /// Is the file locked? |
---|
161 | bool locked; |
---|
162 | }; |
---|
163 | |
---|
164 | #ifdef SBUILD_FEATURE_DEVLOCK |
---|
165 | /** |
---|
166 | * Device lock. Set an advisory lock on a device. The lock is |
---|
167 | * acquired using liblockdev lock_dev(). Note that a lock_type of |
---|
168 | * LOCK_SHARED is equivalent to LOCK_EXCLUSIVE, because this lock |
---|
169 | * type does not support shared locks. |
---|
170 | */ |
---|
171 | class device_lock : public lock |
---|
172 | { |
---|
173 | public: |
---|
174 | /** |
---|
175 | * The constructor. |
---|
176 | * |
---|
177 | * @param device the device to lock (full pathname). |
---|
178 | */ |
---|
179 | device_lock (std::string const& device); |
---|
180 | |
---|
181 | /// The destructor. |
---|
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: |
---|
192 | /// The device to lock. |
---|
193 | std::string device; |
---|
194 | /// Is the file locked? |
---|
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 | */ |
---|