masqmail-0.2

annotate src/dotlock.c @ 8:7e01b9096bb6

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