Diffstat (limited to 'kmicromail/libetpan/tools/maillock.c') (more/less context) (ignore whitespace changes)
-rw-r--r-- | kmicromail/libetpan/tools/maillock.c | 286 |
1 files changed, 286 insertions, 0 deletions
diff --git a/kmicromail/libetpan/tools/maillock.c b/kmicromail/libetpan/tools/maillock.c new file mode 100644 index 0000000..6128f34 --- a/dev/null +++ b/kmicromail/libetpan/tools/maillock.c | |||
@@ -0,0 +1,286 @@ | |||
1 | /* | ||
2 | * libEtPan! -- a mail stuff library | ||
3 | * | ||
4 | * Copyright (C) 2001, 2002 - DINH Viet Hoa | ||
5 | * All rights reserved. | ||
6 | * | ||
7 | * Redistribution and use in source and binary forms, with or without | ||
8 | * modification, are permitted provided that the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. Neither the name of the libEtPan! project nor the names of its | ||
16 | * contributors may be used to endorse or promote products derived | ||
17 | * from this software without specific prior written permission. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
29 | * SUCH DAMAGE. | ||
30 | */ | ||
31 | |||
32 | /* | ||
33 | * $Id $ | ||
34 | */ | ||
35 | |||
36 | #include "maillock.h" | ||
37 | |||
38 | #include "libetpan-config.h" | ||
39 | |||
40 | #include <sys/types.h> | ||
41 | #include <sys/stat.h> | ||
42 | #include <fcntl.h> | ||
43 | #include <unistd.h> | ||
44 | #include <stdio.h> | ||
45 | #include <time.h> | ||
46 | #include <string.h> | ||
47 | |||
48 | /* ********************************************************************** */ | ||
49 | |||
50 | /* lock primitives */ | ||
51 | |||
52 | /* the lock code is modified from the dot lock file code from mail.local.c */ | ||
53 | |||
54 | /* | ||
55 | SENDMAIL LICENSE | ||
56 | |||
57 | The following license terms and conditions apply, unless a different | ||
58 | license is obtained from Sendmail, Inc., 6425 Christie Ave, Fourth Floor, | ||
59 | Emeryville, CA 94608, or by electronic mail at license@sendmail.com. | ||
60 | |||
61 | License Terms: | ||
62 | |||
63 | Use, Modification and Redistribution (including distribution of any | ||
64 | modified or derived work) in source and binary forms is permitted only if | ||
65 | each of the following conditions is met: | ||
66 | |||
67 | 1. Redistributions qualify as "freeware" or "Open Source Software" under | ||
68 | one of the following terms: | ||
69 | |||
70 | (a) Redistributions are made at no charge beyond the reasonable cost of | ||
71 | materials and delivery. | ||
72 | |||
73 | (b) Redistributions are accompanied by a copy of the Source Code or by an | ||
74 | irrevocable offer to provide a copy of the Source Code for up to three | ||
75 | years at the cost of materials and delivery. Such redistributions | ||
76 | must allow further use, modification, and redistribution of the Source | ||
77 | Code under substantially the same terms as this license. For the | ||
78 | purposes of redistribution "Source Code" means the complete compilable | ||
79 | and linkable source code of sendmail including all modifications. | ||
80 | |||
81 | 2. Redistributions of source code must retain the copyright notices as they | ||
82 | appear in each source code file, these license terms, and the | ||
83 | disclaimer/limitation of liability set forth as paragraph 6 below. | ||
84 | |||
85 | 3. Redistributions in binary form must reproduce the Copyright Notice, | ||
86 | these license terms, and the disclaimer/limitation of liability set | ||
87 | forth as paragraph 6 below, in the documentation and/or other materials | ||
88 | provided with the distribution. For the purposes of binary distribution | ||
89 | the "Copyright Notice" refers to the following language: | ||
90 | "Copyright (c) 1998-2002 Sendmail, Inc. All rights reserved." | ||
91 | |||
92 | 4. Neither the name of Sendmail, Inc. nor the University of California nor | ||
93 | the names of their contributors may be used to endorse or promote | ||
94 | products derived from this software without specific prior written | ||
95 | permission. The name "sendmail" is a trademark of Sendmail, Inc. | ||
96 | |||
97 | 5. All redistributions must comply with the conditions imposed by the | ||
98 | University of California on certain embedded code, whose copyright | ||
99 | notice and conditions for redistribution are as follows: | ||
100 | |||
101 | (a) Copyright (c) 1988, 1993 The Regents of the University of | ||
102 | California. All rights reserved. | ||
103 | |||
104 | (b) Redistribution and use in source and binary forms, with or without | ||
105 | modification, are permitted provided that the following conditions | ||
106 | are met: | ||
107 | |||
108 | (i) Redistributions of source code must retain the above copyright | ||
109 | notice, this list of conditions and the following disclaimer. | ||
110 | |||
111 | (ii) Redistributions in binary form must reproduce the above | ||
112 | copyright notice, this list of conditions and the following | ||
113 | disclaimer in the documentation and/or other materials provided | ||
114 | with the distribution. | ||
115 | |||
116 | (iii) Neither the name of the University nor the names of its | ||
117 | contributors may be used to endorse or promote products derived | ||
118 | from this software without specific prior written permission. | ||
119 | |||
120 | 6. Disclaimer/Limitation of Liability: THIS SOFTWARE IS PROVIDED BY | ||
121 | SENDMAIL, INC. AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED | ||
122 | WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | ||
123 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN | ||
124 | NO EVENT SHALL SENDMAIL, INC., THE REGENTS OF THE UNIVERSITY OF | ||
125 | CALIFORNIA OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
126 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
127 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF | ||
128 | USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON | ||
129 | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
130 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | ||
131 | THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. | ||
132 | */ | ||
133 | |||
134 | /* | ||
135 | TODO : lock, prefer fcntl() over flock() | ||
136 | AND use dotlock code above | ||
137 | */ | ||
138 | |||
139 | #define LOCKTO_RM 300/* timeout for stale lockfile removal */ | ||
140 | #define LOCKTO_GLOB 400/* global timeout for lockfile creation */ | ||
141 | |||
142 | static int lock_common(const char * filename, int fd, short locktype) | ||
143 | { | ||
144 | char lockfilename[PATH_MAX]; | ||
145 | struct flock lock; | ||
146 | /* dot lock file */ | ||
147 | int statfailed = 0; | ||
148 | time_t start; | ||
149 | int r; | ||
150 | int res; | ||
151 | |||
152 | lock.l_start = 0; | ||
153 | lock.l_len = 0; | ||
154 | lock.l_pid = getpid(); | ||
155 | lock.l_type = locktype; | ||
156 | lock.l_whence = SEEK_SET; | ||
157 | |||
158 | r = fcntl(fd, F_SETLKW, &lock); | ||
159 | if (r < 0) { | ||
160 | /* WARNING POSIX lock could not be applied */ | ||
161 | } | ||
162 | |||
163 | /* dot lock file */ | ||
164 | |||
165 | if (strlen(filename) + 6 > PATH_MAX) { | ||
166 | res = -1; | ||
167 | goto unlock; | ||
168 | } | ||
169 | |||
170 | snprintf(lockfilename, PATH_MAX, "%s.lock", filename); | ||
171 | |||
172 | time(&start); | ||
173 | while (1) { | ||
174 | int fd; | ||
175 | struct stat st; | ||
176 | time_t now; | ||
177 | |||
178 | /* global timeout */ | ||
179 | time(&now); | ||
180 | if (now > start + LOCKTO_GLOB) { | ||
181 | res = -1; | ||
182 | goto unlock; | ||
183 | } | ||
184 | |||
185 | fd = open(lockfilename, O_WRONLY|O_EXCL|O_CREAT, 0); | ||
186 | if (fd >= 0) { | ||
187 | /* defeat lock checking programs which test pid */ | ||
188 | write(fd, "0", 2); | ||
189 | close(fd); | ||
190 | break; | ||
191 | } | ||
192 | |||
193 | /* libEtPan! - adds a delay of 5 seconds between each tries */ | ||
194 | sleep(5); | ||
195 | |||
196 | if (stat(lockfilename, &st) < 0) { | ||
197 | if (statfailed++ > 5) { | ||
198 | res = -1; | ||
199 | goto unlock; | ||
200 | } | ||
201 | continue; | ||
202 | } | ||
203 | statfailed = 0; | ||
204 | time(&now); | ||
205 | |||
206 | if (now < st.st_ctime + LOCKTO_RM) | ||
207 | continue; | ||
208 | |||
209 | /* try to remove stale lockfile */ | ||
210 | if (unlink(lockfilename) < 0) { | ||
211 | res = -1; | ||
212 | goto unlock; | ||
213 | } | ||
214 | |||
215 | /* | ||
216 | libEtPan! - removes this delay of 5 seconds, | ||
217 | maybe it was misplaced ? | ||
218 | */ | ||
219 | #if 0 | ||
220 | sleep(5); | ||
221 | #endif | ||
222 | } | ||
223 | |||
224 | return 0; | ||
225 | |||
226 | unlock: | ||
227 | lock.l_start = 0; | ||
228 | lock.l_len = 0; | ||
229 | lock.l_pid = getpid(); | ||
230 | lock.l_type = F_UNLCK; | ||
231 | lock.l_whence = SEEK_SET; | ||
232 | |||
233 | r = fcntl(fd, F_SETLK, &lock); | ||
234 | if (r < 0) { | ||
235 | /* WARNING POSIX lock could not be applied */ | ||
236 | } | ||
237 | err: | ||
238 | return res; | ||
239 | } | ||
240 | |||
241 | static int unlock_common(const char * filename, int fd) | ||
242 | { | ||
243 | char lockfilename[PATH_MAX]; | ||
244 | struct flock lock; | ||
245 | int r; | ||
246 | |||
247 | if (strlen(filename) + 6 > PATH_MAX) | ||
248 | return -1; | ||
249 | |||
250 | snprintf(lockfilename, PATH_MAX, "%s.lock", filename); | ||
251 | |||
252 | unlink(lockfilename); | ||
253 | |||
254 | lock.l_start = 0; | ||
255 | lock.l_len = 0; | ||
256 | lock.l_pid = getpid(); | ||
257 | lock.l_type = F_UNLCK; | ||
258 | lock.l_whence = SEEK_SET; | ||
259 | |||
260 | r = fcntl(fd, F_SETLK, &lock); | ||
261 | if (r < 0) { | ||
262 | /* WARNING POSIX lock could not be applied */ | ||
263 | } | ||
264 | |||
265 | return 0; | ||
266 | } | ||
267 | |||
268 | int maillock_read_lock(const char * filename, int fd) | ||
269 | { | ||
270 | return lock_common(filename, fd, F_RDLCK); | ||
271 | } | ||
272 | |||
273 | int maillock_read_unlock(const char * filename, int fd) | ||
274 | { | ||
275 | return unlock_common(filename, fd); | ||
276 | } | ||
277 | |||
278 | int maillock_write_lock(const char * filename, int fd) | ||
279 | { | ||
280 | return lock_common(filename, fd, F_WRLCK); | ||
281 | } | ||
282 | |||
283 | int maillock_write_unlock(const char * filename, int fd) | ||
284 | { | ||
285 | return unlock_common(filename, fd); | ||
286 | } | ||