masqmail-0.2

view src/dotlock.c @ 123:311d4ac8a2eb

next version will be 0.2.25
author meillo@marmaro.de
date Thu, 01 Jul 2010 13:40:13 +0200
parents 08114f7dcc23
children
line source
1 /* MasqMail
2 Copyright (C) 2001 Oliver Kurth
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
19 #include <glib.h>
20 #include <unistd.h>
21 #include <fcntl.h>
22 #include <sys/stat.h>
23 #include <sys/types.h>
24 #include <syslog.h>
25 #include <errno.h>
26 #include <string.h>
27 #include <time.h>
29 #include "masqmail.h"
30 #include "dotlock.h"
32 gboolean
33 dot_lock(gchar * lock_name, gchar * hitch_name)
34 {
35 gboolean ok = FALSE;
36 int fd;
38 fd = open(hitch_name, O_WRONLY | O_CREAT | O_EXCL, 0);
39 if (fd != -1) {
40 struct stat stat_buf;
42 close(fd);
43 link(hitch_name, lock_name);
44 if (stat(hitch_name, &stat_buf) == 0) {
45 if (stat_buf.st_nlink == 2) {
46 unlink(hitch_name);
47 ok = TRUE;
48 } else {
49 if (stat(lock_name, &stat_buf) == 0) {
50 if ((time(NULL) - stat_buf.st_mtime) > MAX_LOCKAGE) {
51 /* remove lock if uncredibly old */
52 unlink(lock_name);
54 link(hitch_name, lock_name);
55 if (stat(hitch_name, &stat_buf) == 0) {
56 if (stat_buf.st_nlink == 2) {
57 unlink(hitch_name);
58 ok = TRUE;
59 }
60 }
61 }
62 }
63 }
64 }
65 if (!ok) {
66 unlink(hitch_name);
67 }
68 } else
69 logwrite(LOG_WARNING, "could not create lock file %s: %s\n", lock_name, strerror(errno));
71 return ok;
72 }
74 gboolean
75 dot_unlock(gchar * lock_name)
76 {
77 unlink(lock_name);
79 return TRUE;
80 }