add support for Ayatana indicator to Notification plugin
[claws.git] / src / plugins / mailmbox / maillock.c
blob300bebd76d3317bc26ba41d3ec7b7a35e5b425a2
1 /*
2 * libEtPan! -- a mail stuff library
4 * Copyright (C) 2001, 2002 - DINH Viet Hoa
5 * All rights reserved.
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.
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.
33 * $Id $
36 #include "config.h"
38 #include "maillock.h"
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include <fcntl.h>
43 #include <limits.h>
44 #include <unistd.h>
45 #include <stdio.h>
46 #include <time.h>
47 #include <string.h>
49 #include "utils.h"
51 /* ********************************************************************** */
53 /* lock primitives */
55 /* the lock code is modified from the dot lock file code from mail.local.c */
58 SENDMAIL LICENSE
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.
64 License Terms:
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
109 are met:
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];
148 struct flock lock;
149 /* dot lock file */
150 int statfailed = 0;
151 time_t start;
152 int r;
153 int res;
155 lock.l_start = 0;
156 lock.l_len = 0;
157 lock.l_pid = getpid();
158 lock.l_type = locktype;
159 lock.l_whence = SEEK_SET;
161 r = fcntl(fd, F_SETLKW, &lock);
162 if (r < 0) {
163 /* WARNING POSIX lock could not be applied */
164 perror("lock");
167 /* dot lock file */
169 if (strlen(filename) + 6 > PATH_MAX) {
170 res = -1;
171 goto unlock;
174 snprintf(lockfilename, PATH_MAX, "%s.lock", filename);
176 time(&start);
177 while (1) {
178 int fd;
179 GStatBuf st;
180 time_t now;
182 /* global timeout */
183 time(&now);
184 if (now > start + LOCKTO_GLOB) {
185 res = -1;
186 goto unlock;
189 fd = open(lockfilename, O_WRONLY|O_EXCL|O_CREAT, 0);
190 if (fd >= 0) {
191 /* defeat lock checking programs which test pid */
192 if (write(fd, "0", 2) < 0)
193 FILE_OP_ERROR(lockfilename, "write");
194 close(fd);
195 break;
196 } else {
197 FILE_OP_ERROR(lockfilename, "open");
200 /* libEtPan! - adds a delay of 5 seconds between each tries */
201 sleep(5);
203 if (g_stat(lockfilename, &st) < 0) {
204 if (statfailed++ > 5) {
205 res = -1;
206 goto unlock;
208 continue;
210 statfailed = 0;
211 time(&now);
213 if (now < st.st_ctime + LOCKTO_RM)
214 continue;
216 /* try to remove stale lockfile */
217 if (unlink(lockfilename) < 0) {
218 res = -1;
219 goto unlock;
223 libEtPan! - removes this delay of 5 seconds,
224 maybe it was misplaced ?
226 #if 0
227 sleep(5);
228 #endif
231 return 0;
233 unlock:
234 lock.l_start = 0;
235 lock.l_len = 0;
236 lock.l_pid = getpid();
237 lock.l_type = F_UNLCK;
238 lock.l_whence = SEEK_SET;
240 r = fcntl(fd, F_SETLK, &lock);
241 if (r < 0) {
242 /* WARNING POSIX lock could not be applied */
243 perror("lock");
245 return res;
248 static int unlock_common(const char * filename, int fd)
250 char lockfilename[PATH_MAX];
251 struct flock lock;
252 int r;
254 if (strlen(filename) + 6 > PATH_MAX)
255 return -1;
257 snprintf(lockfilename, PATH_MAX, "%s.lock", filename);
259 unlink(lockfilename);
261 lock.l_start = 0;
262 lock.l_len = 0;
263 lock.l_pid = getpid();
264 lock.l_type = F_UNLCK;
265 lock.l_whence = SEEK_SET;
267 r = fcntl(fd, F_SETLK, &lock);
268 if (r < 0) {
269 /* WARNING POSIX lock could not be applied */
272 return 0;
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);