2 * libEtPan! -- a mail stuff library
4 * Copyright (C) 2001, 2002 - DINH Viet Hoa
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
38 #include <sys/types.h>
47 /* ********************************************************************** */
51 /* the lock code is modified from the dot lock file code from mail.local.c */
56 The following license terms and conditions apply, unless a different
57 license is obtained from Sendmail, Inc., 6425 Christie Ave, Fourth Floor,
58 Emeryville, CA 94608, or by electronic mail at license@sendmail.com.
62 Use, Modification and Redistribution (including distribution of any
63 modified or derived work) in source and binary forms is permitted only if
64 each of the following conditions is met:
66 1. Redistributions qualify as "freeware" or "Open Source Software" under
67 one of the following terms:
69 (a) Redistributions are made at no charge beyond the reasonable cost of
70 materials and delivery.
72 (b) Redistributions are accompanied by a copy of the Source Code or by an
73 irrevocable offer to provide a copy of the Source Code for up to three
74 years at the cost of materials and delivery. Such redistributions
75 must allow further use, modification, and redistribution of the Source
76 Code under substantially the same terms as this license. For the
77 purposes of redistribution "Source Code" means the complete compilable
78 and linkable source code of sendmail including all modifications.
80 2. Redistributions of source code must retain the copyright notices as they
81 appear in each source code file, these license terms, and the
82 disclaimer/limitation of liability set forth as paragraph 6 below.
84 3. Redistributions in binary form must reproduce the Copyright Notice,
85 these license terms, and the disclaimer/limitation of liability set
86 forth as paragraph 6 below, in the documentation and/or other materials
87 provided with the distribution. For the purposes of binary distribution
88 the "Copyright Notice" refers to the following language:
89 "Copyright (c) 1998-2002 Sendmail, Inc. All rights reserved."
91 4. Neither the name of Sendmail, Inc. nor the University of California nor
92 the names of their contributors may be used to endorse or promote
93 products derived from this software without specific prior written
94 permission. The name "sendmail" is a trademark of Sendmail, Inc.
96 5. All redistributions must comply with the conditions imposed by the
97 University of California on certain embedded code, whose copyright
98 notice and conditions for redistribution are as follows:
100 (a) Copyright (c) 1988, 1993 The Regents of the University of
101 California. All rights reserved.
103 (b) Redistribution and use in source and binary forms, with or without
104 modification, are permitted provided that the following conditions
107 (i) Redistributions of source code must retain the above copyright
108 notice, this list of conditions and the following disclaimer.
110 (ii) Redistributions in binary form must reproduce the above
111 copyright notice, this list of conditions and the following
112 disclaimer in the documentation and/or other materials provided
113 with the distribution.
115 (iii) Neither the name of the University nor the names of its
116 contributors may be used to endorse or promote products derived
117 from this software without specific prior written permission.
119 6. Disclaimer/Limitation of Liability: THIS SOFTWARE IS PROVIDED BY
120 SENDMAIL, INC. AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
121 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
122 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
123 NO EVENT SHALL SENDMAIL, INC., THE REGENTS OF THE UNIVERSITY OF
124 CALIFORNIA OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
125 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
126 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
127 USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
128 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
129 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
130 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
134 TODO : lock, prefer fcntl() over flock()
135 AND use dotlock code above
138 #define LOCKTO_RM 300 /* timeout for stale lockfile removal */
139 #define LOCKTO_GLOB 400 /* global timeout for lockfile creation */
141 static int lock_common(const char * filename, int fd, short locktype)
143 char lockfilename[PATH_MAX];
153 lock.l_pid = getpid();
154 lock.l_type = locktype;
155 lock.l_whence = SEEK_SET;
157 r = fcntl(fd, F_SETLKW, &lock);
159 /* WARNING POSIX lock could not be applied */
164 if (strlen(filename) + 6 > PATH_MAX) {
169 snprintf(lockfilename, PATH_MAX, "%s.lock", filename);
179 if (now > start + LOCKTO_GLOB) {
184 fd = open(lockfilename, O_WRONLY|O_EXCL|O_CREAT, 0);
186 /* defeat lock checking programs which test pid */
192 /* libEtPan! - adds a delay of 5 seconds between each tries */
195 if (stat(lockfilename, &st) < 0) {
196 if (statfailed++ > 5) {
205 if (now < st.st_ctime + LOCKTO_RM)
208 /* try to remove stale lockfile */
209 if (unlink(lockfilename) < 0) {
215 libEtPan! - removes this delay of 5 seconds,
216 maybe it was misplaced ?
228 lock.l_pid = getpid();
229 lock.l_type = F_UNLCK;
230 lock.l_whence = SEEK_SET;
232 r = fcntl(fd, F_SETLK, &lock);
234 /* WARNING POSIX lock could not be applied */
239 static int unlock_common(const char * filename, int fd)
241 char lockfilename[PATH_MAX];
245 if (strlen(filename) + 6 > PATH_MAX)
248 snprintf(lockfilename, PATH_MAX, "%s.lock", filename);
250 unlink(lockfilename);
254 lock.l_pid = getpid();
255 lock.l_type = F_UNLCK;
256 lock.l_whence = SEEK_SET;
258 r = fcntl(fd, F_SETLK, &lock);
260 /* WARNING POSIX lock could not be applied */
266 int maillock_read_lock(const char * filename, int fd)
268 return lock_common(filename, fd, F_RDLCK);
271 int maillock_read_unlock(const char * filename, int fd)
273 return unlock_common(filename, fd);
276 int maillock_write_lock(const char * filename, int fd)
278 return lock_common(filename, fd, F_WRLCK);
281 int maillock_write_unlock(const char * filename, int fd)
283 return unlock_common(filename, fd);