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
40 #include <sys/types.h>
51 /* ********************************************************************** */
55 /* the lock code is modified from the dot lock file code from mail.local.c */
60 The following license terms and conditions apply, unless a different
61 license is obtained from Sendmail, Inc., 6425 Christie Ave, Fourth Floor,
62 Emeryville, CA 94608, or by electronic mail at license@sendmail.com.
66 Use, Modification and Redistribution (including distribution of any
67 modified or derived work) in source and binary forms is permitted only if
68 each of the following conditions is met:
70 1. Redistributions qualify as "freeware" or "Open Source Software" under
71 one of the following terms:
73 (a) Redistributions are made at no charge beyond the reasonable cost of
74 materials and delivery.
76 (b) Redistributions are accompanied by a copy of the Source Code or by an
77 irrevocable offer to provide a copy of the Source Code for up to three
78 years at the cost of materials and delivery. Such redistributions
79 must allow further use, modification, and redistribution of the Source
80 Code under substantially the same terms as this license. For the
81 purposes of redistribution "Source Code" means the complete compilable
82 and linkable source code of sendmail including all modifications.
84 2. Redistributions of source code must retain the copyright notices as they
85 appear in each source code file, these license terms, and the
86 disclaimer/limitation of liability set forth as paragraph 6 below.
88 3. Redistributions in binary form must reproduce the Copyright Notice,
89 these license terms, and the disclaimer/limitation of liability set
90 forth as paragraph 6 below, in the documentation and/or other materials
91 provided with the distribution. For the purposes of binary distribution
92 the "Copyright Notice" refers to the following language:
93 "Copyright (c) 1998-2002 Sendmail, Inc. All rights reserved."
95 4. Neither the name of Sendmail, Inc. nor the University of California nor
96 the names of their contributors may be used to endorse or promote
97 products derived from this software without specific prior written
98 permission. The name "sendmail" is a trademark of Sendmail, Inc.
100 5. All redistributions must comply with the conditions imposed by the
101 University of California on certain embedded code, whose copyright
102 notice and conditions for redistribution are as follows:
104 (a) Copyright (c) 1988, 1993 The Regents of the University of
105 California. All rights reserved.
107 (b) Redistribution and use in source and binary forms, with or without
108 modification, are permitted provided that the following conditions
111 (i) Redistributions of source code must retain the above copyright
112 notice, this list of conditions and the following disclaimer.
114 (ii) Redistributions in binary form must reproduce the above
115 copyright notice, this list of conditions and the following
116 disclaimer in the documentation and/or other materials provided
117 with the distribution.
119 (iii) Neither the name of the University nor the names of its
120 contributors may be used to endorse or promote products derived
121 from this software without specific prior written permission.
123 6. Disclaimer/Limitation of Liability: THIS SOFTWARE IS PROVIDED BY
124 SENDMAIL, INC. AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
125 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
126 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
127 NO EVENT SHALL SENDMAIL, INC., THE REGENTS OF THE UNIVERSITY OF
128 CALIFORNIA OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
129 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
130 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
131 USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
132 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
133 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
134 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
138 TODO : lock, prefer fcntl() over flock()
139 AND use dotlock code above
142 #define LOCKTO_RM 300 /* timeout for stale lockfile removal */
143 #define LOCKTO_GLOB 400 /* global timeout for lockfile creation */
145 static int lock_common(const char * filename
, int fd
, short locktype
)
147 char lockfilename
[PATH_MAX
];
157 lock
.l_pid
= getpid();
158 lock
.l_type
= locktype
;
159 lock
.l_whence
= SEEK_SET
;
161 r
= fcntl(fd
, F_SETLKW
, &lock
);
163 /* WARNING POSIX lock could not be applied */
169 if (strlen(filename
) + 6 > PATH_MAX
) {
174 snprintf(lockfilename
, PATH_MAX
, "%s.lock", filename
);
184 if (now
> start
+ LOCKTO_GLOB
) {
189 fd
= open(lockfilename
, O_WRONLY
|O_EXCL
|O_CREAT
, 0);
191 /* defeat lock checking programs which test pid */
192 if (write(fd
, "0", 2) < 0)
193 FILE_OP_ERROR(lockfilename
, "write");
197 FILE_OP_ERROR(lockfilename
, "open");
200 /* libEtPan! - adds a delay of 5 seconds between each tries */
203 if (g_stat(lockfilename
, &st
) < 0) {
204 if (statfailed
++ > 5) {
213 if (now
< st
.st_ctime
+ LOCKTO_RM
)
216 /* try to remove stale lockfile */
217 if (unlink(lockfilename
) < 0) {
223 libEtPan! - removes this delay of 5 seconds,
224 maybe it was misplaced ?
236 lock
.l_pid
= getpid();
237 lock
.l_type
= F_UNLCK
;
238 lock
.l_whence
= SEEK_SET
;
240 r
= fcntl(fd
, F_SETLK
, &lock
);
242 /* WARNING POSIX lock could not be applied */
248 static int unlock_common(const char * filename
, int fd
)
250 char lockfilename
[PATH_MAX
];
254 if (strlen(filename
) + 6 > PATH_MAX
)
257 snprintf(lockfilename
, PATH_MAX
, "%s.lock", filename
);
259 unlink(lockfilename
);
263 lock
.l_pid
= getpid();
264 lock
.l_type
= F_UNLCK
;
265 lock
.l_whence
= SEEK_SET
;
267 r
= fcntl(fd
, F_SETLK
, &lock
);
269 /* WARNING POSIX lock could not be applied */
275 int maillock_read_lock(const char * filename
, int fd
)
277 return lock_common(filename
, fd
, F_RDLCK
);
280 int maillock_read_unlock(const char * filename
, int fd
)
282 return unlock_common(filename
, fd
);
285 int maillock_write_lock(const char * filename
, int fd
)
287 return lock_common(filename
, fd
, F_WRLCK
);
290 int maillock_write_unlock(const char * filename
, int fd
)
292 return unlock_common(filename
, fd
);