masqmail

annotate src/dotlock.c @ 351:4c046561582a

Minor refactoring
author markus schnalke <meillo@marmaro.de>
date Thu, 14 Jul 2011 14:12:32 +0200
parents 08114f7dcc23
children 41958685480d
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@10 32 gboolean
meillo@10 33 dot_lock(gchar * lock_name, gchar * hitch_name)
meillo@0 34 {
meillo@10 35 gboolean ok = FALSE;
meillo@10 36 int fd;
meillo@0 37
meillo@10 38 fd = open(hitch_name, O_WRONLY | O_CREAT | O_EXCL, 0);
meillo@10 39 if (fd != -1) {
meillo@10 40 struct stat stat_buf;
meillo@0 41
meillo@10 42 close(fd);
meillo@10 43 link(hitch_name, lock_name);
meillo@10 44 if (stat(hitch_name, &stat_buf) == 0) {
meillo@10 45 if (stat_buf.st_nlink == 2) {
meillo@10 46 unlink(hitch_name);
meillo@10 47 ok = TRUE;
meillo@10 48 } else {
meillo@10 49 if (stat(lock_name, &stat_buf) == 0) {
meillo@10 50 if ((time(NULL) - stat_buf.st_mtime) > MAX_LOCKAGE) {
meillo@10 51 /* remove lock if uncredibly old */
meillo@10 52 unlink(lock_name);
meillo@0 53
meillo@10 54 link(hitch_name, lock_name);
meillo@10 55 if (stat(hitch_name, &stat_buf) == 0) {
meillo@10 56 if (stat_buf.st_nlink == 2) {
meillo@10 57 unlink(hitch_name);
meillo@10 58 ok = TRUE;
meillo@10 59 }
meillo@10 60 }
meillo@10 61 }
meillo@10 62 }
meillo@10 63 }
meillo@10 64 }
meillo@10 65 if (!ok) {
meillo@10 66 unlink(hitch_name);
meillo@10 67 }
meillo@10 68 } else
meillo@10 69 logwrite(LOG_WARNING, "could not create lock file %s: %s\n", lock_name, strerror(errno));
meillo@0 70
meillo@10 71 return ok;
meillo@0 72 }
meillo@0 73
meillo@10 74 gboolean
meillo@10 75 dot_unlock(gchar * lock_name)
meillo@0 76 {
meillo@10 77 unlink(lock_name);
meillo@0 78
meillo@10 79 return TRUE;
meillo@0 80 }