X-Git-Url: http://git.marmaro.de/?p=mmh;a=blobdiff_plain;f=sbr%2Flock_file.c;h=66eab72ca3da9e56461e5145096c688b84fe9fe7;hp=2cb6bdc1543c7b427bb3afcdc39203a44ff2b411;hb=389877bae1fe1a9f7259b8979f6a930744d90fab;hpb=0dc7a85acb242ad851c28001435932526a9a6d29 diff --git a/sbr/lock_file.c b/sbr/lock_file.c index 2cb6bdc..66eab72 100644 --- a/sbr/lock_file.c +++ b/sbr/lock_file.c @@ -3,6 +3,10 @@ * lock.c -- routines to lock/unlock files * * $Id$ + * + * This code is Copyright (c) 2002, by the authors of nmh. See the + * COPYRIGHT file in the root directory of the nmh distribution for + * complete copyright information. */ /* Modified by Ruud de Rooij to support Miquel van Smoorenburg's liblockfile @@ -15,6 +19,7 @@ #include #include +#include #ifdef TIME_WITH_SYS_TIME # include @@ -31,11 +36,6 @@ # include #endif -#ifdef MMDFONLY -# include -# include -#endif /* MMDFONLY */ - #ifdef HAVE_FCNTL_H # include #else @@ -52,8 +52,6 @@ #include #endif -extern int errno; - #ifdef LOCKDIR char *lockdir = LOCKDIR; #endif @@ -197,10 +195,22 @@ lkfopen (char *file, char *mode) if (strcmp (mode, "r") == 0) access = O_RDONLY; - else + else if (strcmp (mode, "r+") == 0) access = O_RDWR; + else if (strcmp (mode, "w") == 0) + access = O_WRONLY | O_CREAT | O_TRUNC; + else if (strcmp (mode, "w+") == 0) + access = O_RDWR | O_CREAT | O_TRUNC; + else if (strcmp (mode, "a") == 0) + access = O_WRONLY | O_CREAT | O_APPEND; + else if (strcmp (mode, "a+") == 0) + access = O_RDWR | O_CREAT | O_APPEND; + else { + errno = EINVAL; + return NULL; + } - if ((fd = lkopen (file, access, 0)) == -1) + if ((fd = lkopen (file, access, 0666)) == -1) return NULL; if ((fp = fdopen (fd, mode)) == NULL) { @@ -310,7 +320,8 @@ lkopen_kernel (char *file, int access, mode_t mode) # endif # ifdef FLOCK_LOCKING - if (flock (fd, LOCK_EX | LOCK_NB) != -1) + if (flock (fd, (((access & 03) == O_RDONLY) ? LOCK_SH : LOCK_EX) + | LOCK_NB) != -1) return fd; # endif @@ -386,6 +397,7 @@ lkopen_dot (char *file, int access, mode_t mode) else sleep (5); } + lockname (file, &lkinfo, 1); } } #else @@ -526,15 +538,11 @@ timerON (char *curlock, int fd) struct lock *lp; size_t len; - if (!(lp = (struct lock *) malloc (sizeof(*lp)))) - return; + lp = (struct lock *) mh_xmalloc (sizeof(*lp)); len = strlen(curlock) + 1; lp->l_fd = fd; - if (!(lp->l_lock = malloc (len))) { - free ((char *) lp); - return; - } + lp->l_lock = mh_xmalloc (len); memcpy (lp->l_lock, curlock, len); lp->l_next = l_top;