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>
46 /* ********************************************************************** */
50 /* the lock code is modified from the dot lock file code from mail.local.c */
55 The following license terms and conditions apply, unless a different
56 license is obtained from Sendmail, Inc., 6425 Christie Ave, Fourth Floor,
57 Emeryville, CA 94608, or by electronic mail at license@sendmail.com.
61 Use, Modification and Redistribution (including distribution of any
62 modified or derived work) in source and binary forms is permitted only if
63 each of the following conditions is met:
65 1. Redistributions qualify as "freeware" or "Open Source Software" under
66 one of the following terms:
68 (a) Redistributions are made at no charge beyond the reasonable cost of
69 materials and delivery.
71 (b) Redistributions are accompanied by a copy of the Source Code or by an
72 irrevocable offer to provide a copy of the Source Code for up to three
73 years at the cost of materials and delivery. Such redistributions
74 must allow further use, modification, and redistribution of the Source
75 Code under substantially the same terms as this license. For the
76 purposes of redistribution "Source Code" means the complete compilable
77 and linkable source code of sendmail including all modifications.
79 2. Redistributions of source code must retain the copyright notices as they
80 appear in each source code file, these license terms, and the
81 disclaimer/limitation of liability set forth as paragraph 6 below.
83 3. Redistributions in binary form must reproduce the Copyright Notice,
84 these license terms, and the disclaimer/limitation of liability set
85 forth as paragraph 6 below, in the documentation and/or other materials
86 provided with the distribution. For the purposes of binary distribution
87 the "Copyright Notice" refers to the following language:
88 "Copyright (c) 1998-2002 Sendmail, Inc. All rights reserved."
90 4. Neither the name of Sendmail, Inc. nor the University of California nor
91 the names of their contributors may be used to endorse or promote
92 products derived from this software without specific prior written
93 permission. The name "sendmail" is a trademark of Sendmail, Inc.
95 5. All redistributions must comply with the conditions imposed by the
96 University of California on certain embedded code, whose copyright
97 notice and conditions for redistribution are as follows:
99 (a) Copyright (c) 1988, 1993 The Regents of the University of
100 California. All rights reserved.
102 (b) Redistribution and use in source and binary forms, with or without
103 modification, are permitted provided that the following conditions
106 (i) Redistributions of source code must retain the above copyright
107 notice, this list of conditions and the following disclaimer.
109 (ii) Redistributions in binary form must reproduce the above
110 copyright notice, this list of conditions and the following
111 disclaimer in the documentation and/or other materials provided
112 with the distribution.
114 (iii) Neither the name of the University nor the names of its
115 contributors may be used to endorse or promote products derived
116 from this software without specific prior written permission.
118 6. Disclaimer/Limitation of Liability: THIS SOFTWARE IS PROVIDED BY
119 SENDMAIL, INC. AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
120 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
121 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
122 NO EVENT SHALL SENDMAIL, INC., THE REGENTS OF THE UNIVERSITY OF
123 CALIFORNIA OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
124 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
125 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
126 USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
127 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
128 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
129 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
133 TODO : lock, prefer fcntl() over flock()
134 AND use dotlock code above
137 #define LOCKTO_RM 300 /* timeout for stale lockfile removal */
138 #define LOCKTO_GLOB 400 /* global timeout for lockfile creation */
140 static int lock_common(char * filename, int fd, short locktype)
142 char lockfilename[PATH_MAX];
152 lock.l_pid = getpid();
153 lock.l_type = locktype;
154 lock.l_whence = SEEK_SET;
156 r = fcntl(fd, F_SETLKW, &lock);
158 /* WARNING POSIX lock could not be applied */
163 if (strlen(filename) + 6 > PATH_MAX) {
168 snprintf(lockfilename, PATH_MAX, "%s.lock", filename);
178 if (now > start + LOCKTO_GLOB) {
183 fd = open(lockfilename, O_WRONLY|O_EXCL|O_CREAT, 0);
185 /* defeat lock checking programs which test pid */
191 if (stat(lockfilename, &st) < 0) {
192 if (statfailed++ > 5) {
201 if (now < st.st_ctime + LOCKTO_RM)
204 /* try to remove stale lockfile */
205 if (unlink(lockfilename) < 0) {
218 lock.l_pid = getpid();
219 lock.l_type = F_UNLCK;
220 lock.l_whence = SEEK_SET;
222 r = fcntl(fd, F_SETLK, &lock);
224 /* WARNING POSIX lock could not be applied */
230 static int unlock_common(char * filename, int fd)
232 char lockfilename[PATH_MAX];
236 if (strlen(filename) + 6 > PATH_MAX)
239 snprintf(lockfilename, PATH_MAX, "%s.lock", filename);
241 unlink(lockfilename);
245 lock.l_pid = getpid();
246 lock.l_type = F_UNLCK;
247 lock.l_whence = SEEK_SET;
249 r = fcntl(fd, F_SETLK, &lock);
251 /* WARNING POSIX lock could not be applied */
257 int maillock_read_lock(char * filename, int fd)
259 return lock_common(filename, fd, F_RDLCK);
262 int maillock_read_unlock(char * filename, int fd)
264 return unlock_common(filename, fd);
267 int maillock_write_lock(char * filename, int fd)
269 return lock_common(filename, fd, F_WRLCK);
272 int maillock_write_unlock(char * filename, int fd)
274 return unlock_common(filename, fd);