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>
49 /* ********************************************************************** */
53 /* the lock code is modified from the dot lock file code from mail.local.c */
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.
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:
68 1. Redistributions qualify as "freeware" or "Open Source Software" under
69 one of the following terms:
71 (a) Redistributions are made at no charge beyond the reasonable cost of
72 materials and delivery.
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.
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.
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."
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.
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:
102 (a) Copyright (c) 1988, 1993 The Regents of the University of
103 California. All rights reserved.
105 (b) Redistribution and use in source and binary forms, with or without
106 modification, are permitted provided that the following conditions
109 (i) Redistributions of source code must retain the above copyright
110 notice, this list of conditions and the following disclaimer.
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.
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.
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.
136 TODO : lock, prefer fcntl() over flock()
137 AND use dotlock code above
140 #define LOCKTO_RM 300 /* timeout for stale lockfile removal */
141 #define LOCKTO_GLOB 400 /* global timeout for lockfile creation */
143 static int lock_common(const char * filename, int fd, short locktype)
145 char lockfilename[PATH_MAX];
155 lock.l_pid = getpid();
156 lock.l_type = locktype;
157 lock.l_whence = SEEK_SET;
159 r = fcntl(fd, F_SETLKW, &lock);
161 /* WARNING POSIX lock could not be applied */
166 if (strlen(filename) + 6 > PATH_MAX) {
171 snprintf(lockfilename, PATH_MAX, "%s.lock", filename);
181 if (now > start + LOCKTO_GLOB) {
186 fd = open(lockfilename, O_WRONLY|O_EXCL|O_CREAT, 0);
188 /* defeat lock checking programs which test pid */
189 if (write(fd, "0", 2) < 0)
195 /* libEtPan! - adds a delay of 5 seconds between each tries */
198 if (stat(lockfilename, &st) < 0) {
199 if (statfailed++ > 5) {
208 if (now < st.st_ctime + LOCKTO_RM)
211 /* try to remove stale lockfile */
212 if (unlink(lockfilename) < 0) {
218 libEtPan! - removes this delay of 5 seconds,
219 maybe it was misplaced ?
231 lock.l_pid = getpid();
232 lock.l_type = F_UNLCK;
233 lock.l_whence = SEEK_SET;
235 r = fcntl(fd, F_SETLK, &lock);
237 /* WARNING POSIX lock could not be applied */
242 static int unlock_common(const char * filename, int fd)
244 char lockfilename[PATH_MAX];
248 if (strlen(filename) + 6 > PATH_MAX)
251 snprintf(lockfilename, PATH_MAX, "%s.lock", filename);
253 unlink(lockfilename);
257 lock.l_pid = getpid();
258 lock.l_type = F_UNLCK;
259 lock.l_whence = SEEK_SET;
261 r = fcntl(fd, F_SETLK, &lock);
263 /* WARNING POSIX lock could not be applied */
269 int maillock_read_lock(const char * filename, int fd)
271 return lock_common(filename, fd, F_RDLCK);
274 int maillock_read_unlock(const char * filename, int fd)
276 return unlock_common(filename, fd);
279 int maillock_write_lock(const char * filename, int fd)
281 return lock_common(filename, fd, F_WRLCK);
284 int maillock_write_unlock(const char * filename, int fd)
286 return unlock_common(filename, fd);