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