masqmail

annotate src/dotlock.c @ 414:309935f59820

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