masqmail-0.2

view src/dotlock.c @ 0:08114f7dcc23

this is masqmail-0.2.21 from oliver kurth
author meillo@marmaro.de
date Fri, 26 Sep 2008 17:05:23 +0200
parents
children 26e34ae9a3e3
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 dot_lock(gchar *lock_name, gchar *hitch_name)
33 {
34 gboolean ok = FALSE;
35 int fd;
37 fd = open(hitch_name, O_WRONLY | O_CREAT | O_EXCL, 0);
38 if(fd != -1){
39 struct stat stat_buf;
41 close(fd);
42 link(hitch_name, lock_name);
43 if(stat(hitch_name, &stat_buf) == 0){
44 if(stat_buf.st_nlink == 2){
45 unlink(hitch_name);
46 ok = TRUE;
47 }
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",
70 lock_name, strerror(errno));
72 return ok;
73 }
75 gboolean dot_unlock(gchar *lock_name)
76 {
77 unlink(lock_name);
79 return TRUE;
80 }