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 
lock_common(const char * filename,int fd,short locktype)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     perror("lock");
163   }
164 
165   /* dot lock file */
166 
167   if (strlen(filename) + 6 > PATH_MAX) {
168     res = -1;
169     goto unlock;
170   }
171 
172   snprintf(lockfilename, PATH_MAX, "%s.lock", filename);
173 
174   time(&start);
175   while (1) {
176     int fd;
177     GStatBuf st;
178     time_t now;
179 
180     /* global timeout */
181     time(&now);
182     if (now > start + LOCKTO_GLOB) {
183       res = -1;
184       goto unlock;
185     }
186 
187     fd = open(lockfilename, O_WRONLY|O_EXCL|O_CREAT, 0);
188     if (fd >= 0) {
189       /* defeat lock checking programs which test pid */
190       if (write(fd, "0", 2) < 0)
191           FILE_OP_ERROR(lockfilename, "write");
192       close(fd);
193       break;
194     } else {
195       FILE_OP_ERROR(lockfilename, "open");
196     }
197 
198     /* libEtPan! - adds a delay of 5 seconds between each tries */
199     sleep(5);
200 
201     if (g_stat(lockfilename, &st) < 0) {
202       if (statfailed++ > 5) {
203 	res = -1;
204 	goto unlock;
205       }
206       continue;
207     }
208     statfailed = 0;
209     time(&now);
210 
211     if (now < st.st_ctime + LOCKTO_RM)
212       continue;
213 
214     /* try to remove stale lockfile */
215     if (unlink(lockfilename) < 0) {
216       res = -1;
217       goto unlock;
218     }
219 
220     /*
221       libEtPan! - removes this delay of 5 seconds,
222        maybe it was misplaced ?
223     */
224 #if 0
225     sleep(5);
226 #endif
227   }
228 
229   return 0;
230 
231  unlock:
232   lock.l_start = 0;
233   lock.l_len = 0;
234   lock.l_pid = getpid();
235   lock.l_type = F_UNLCK;
236   lock.l_whence = SEEK_SET;
237 
238   r = fcntl(fd, F_SETLK, &lock);
239   if (r < 0) {
240     /* WARNING POSIX lock could not be applied */
241     perror("lock");
242   }
243   return res;
244 }
245 
unlock_common(const char * filename,int fd)246 static int unlock_common(const char * filename, int fd)
247 {
248   char lockfilename[PATH_MAX];
249   struct flock lock;
250   int r;
251 
252   if (strlen(filename) + 6 > PATH_MAX)
253     return -1;
254 
255   snprintf(lockfilename, PATH_MAX, "%s.lock", filename);
256 
257   unlink(lockfilename);
258 
259   lock.l_start = 0;
260   lock.l_len = 0;
261   lock.l_pid = getpid();
262   lock.l_type = F_UNLCK;
263   lock.l_whence = SEEK_SET;
264 
265   r = fcntl(fd, F_SETLK, &lock);
266   if (r < 0) {
267     /* WARNING POSIX lock could not be applied */
268   }
269 
270   return 0;
271 }
272 
maillock_read_lock(const char * filename,int fd)273 int maillock_read_lock(const char * filename, int fd)
274 {
275   return lock_common(filename, fd, F_RDLCK);
276 }
277 
maillock_read_unlock(const char * filename,int fd)278 int maillock_read_unlock(const char * filename, int fd)
279 {
280   return unlock_common(filename, fd);
281 }
282 
maillock_write_lock(const char * filename,int fd)283 int maillock_write_lock(const char * filename, int fd)
284 {
285   return lock_common(filename, fd, F_WRLCK);
286 }
287 
maillock_write_unlock(const char * filename,int fd)288 int maillock_write_unlock(const char * filename, int fd)
289 {
290   return unlock_common(filename, fd);
291 }
292