masqmail-0.2

diff 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 diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/dotlock.c	Fri Sep 26 17:05:23 2008 +0200
     1.3 @@ -0,0 +1,80 @@
     1.4 +/*  MasqMail
     1.5 +    Copyright (C) 2001 Oliver Kurth
     1.6 +
     1.7 +    This program is free software; you can redistribute it and/or modify
     1.8 +    it under the terms of the GNU General Public License as published by
     1.9 +    the Free Software Foundation; either version 2 of the License, or
    1.10 +    (at your option) any later version.
    1.11 +
    1.12 +    This program is distributed in the hope that it will be useful,
    1.13 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.15 +    GNU General Public License for more details.
    1.16 +
    1.17 +    You should have received a copy of the GNU General Public License
    1.18 +    along with this program; if not, write to the Free Software
    1.19 +    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
    1.20 +*/
    1.21 +
    1.22 +#include <glib.h>
    1.23 +#include <unistd.h>
    1.24 +#include <fcntl.h>
    1.25 +#include <sys/stat.h>
    1.26 +#include <sys/types.h>
    1.27 +#include <syslog.h>
    1.28 +#include <errno.h>
    1.29 +#include <string.h>
    1.30 +#include <time.h>
    1.31 +
    1.32 +#include "masqmail.h"
    1.33 +#include "dotlock.h"
    1.34 +
    1.35 +gboolean dot_lock(gchar *lock_name, gchar *hitch_name)
    1.36 +{
    1.37 +  gboolean ok = FALSE;
    1.38 +  int fd;
    1.39 +
    1.40 +  fd = open(hitch_name, O_WRONLY | O_CREAT | O_EXCL, 0);
    1.41 +  if(fd != -1){
    1.42 +    struct stat stat_buf;
    1.43 +
    1.44 +    close(fd);
    1.45 +    link(hitch_name, lock_name);
    1.46 +    if(stat(hitch_name, &stat_buf) == 0){
    1.47 +      if(stat_buf.st_nlink == 2){
    1.48 +        unlink(hitch_name);
    1.49 +        ok = TRUE;
    1.50 +      }
    1.51 +      else{
    1.52 +        if(stat(lock_name, &stat_buf) == 0){
    1.53 +          if((time(NULL) - stat_buf.st_mtime) > MAX_LOCKAGE){
    1.54 +            /* remove lock if uncredibly old */
    1.55 +            unlink(lock_name);
    1.56 +
    1.57 +            link(hitch_name, lock_name);
    1.58 +            if(stat(hitch_name, &stat_buf) == 0){
    1.59 +              if(stat_buf.st_nlink == 2){
    1.60 +                unlink(hitch_name);
    1.61 +                ok = TRUE;
    1.62 +              }
    1.63 +            }
    1.64 +          }
    1.65 +        }
    1.66 +      }
    1.67 +    }
    1.68 +    if(!ok){
    1.69 +      unlink(hitch_name);
    1.70 +    }
    1.71 +  }else
    1.72 +    logwrite(LOG_WARNING, "could not create lock file %s: %s\n",
    1.73 +	     lock_name, strerror(errno));
    1.74 +
    1.75 +  return ok;
    1.76 +}
    1.77 +
    1.78 +gboolean dot_unlock(gchar *lock_name)
    1.79 +{
    1.80 +  unlink(lock_name);
    1.81 +
    1.82 +  return TRUE;
    1.83 +}