From 82a21b6f3cddf8ab048dcb02c080ea9797c73c5a Mon Sep 17 00:00:00 2001 From: Ken Hornstein Date: Fri, 5 Sep 2003 18:36:36 +0000 Subject: [PATCH] Merge in locking fixes into the main line. --- sbr/context_read.c | 4 ++-- sbr/context_save.c | 4 ++-- sbr/lock_file.c | 20 +++++++++++++++++--- sbr/seq_read.c | 6 +++--- sbr/seq_save.c | 6 +++--- 5 files changed, 27 insertions(+), 13 deletions(-) diff --git a/sbr/context_read.c b/sbr/context_read.c index e6cdfff..d2524c7 100644 --- a/sbr/context_read.c +++ b/sbr/context_read.c @@ -124,9 +124,9 @@ context_read (void) ctxpath = getcpy (m_maildir (cp)); - if ((ib = fopen (ctxpath, "r"))) { + if ((ib = lkfopen (ctxpath, "r"))) { readconfig ((struct node **) 0, ib, cp, 1); - fclose (ib); + lkfclose (ib); } return; diff --git a/sbr/context_save.c b/sbr/context_save.c index 58d1745..4fa60ea 100644 --- a/sbr/context_save.c +++ b/sbr/context_save.c @@ -41,12 +41,12 @@ context_save (void) sigaddset (&set, SIGTERM); SIGPROCMASK (SIG_BLOCK, &set, &oset); - if (!(out = fopen (ctxpath, "w"))) + if (!(out = lkfopen (ctxpath, "w"))) adios (ctxpath, "unable to write"); for (np = m_defs; np; np = np->n_next) if (np->n_context) fprintf (out, "%s: %s\n", np->n_name, np->n_field); - fclose (out); + lkfclose (out, ctxpath); SIGPROCMASK (SIG_SETMASK, &oset, &set); /* reset the signal mask */ diff --git a/sbr/lock_file.c b/sbr/lock_file.c index 7280464..5cd1872 100644 --- a/sbr/lock_file.c +++ b/sbr/lock_file.c @@ -201,10 +201,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) { @@ -314,7 +326,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 @@ -329,6 +342,7 @@ lkopen_kernel (char *file, int access, mode_t mode) j = errno; close (fd); + admonish (file, "lock did not succeed, waiting"); sleep (5); } diff --git a/sbr/seq_read.c b/sbr/seq_read.c index d085d53..f36edb5 100644 --- a/sbr/seq_read.c +++ b/sbr/seq_read.c @@ -22,7 +22,7 @@ static void seq_private (struct msgs *); /* * Get the sequence information for this folder from - * .mh_sequence (or equivalent specified in .mh_profile) + * .mh_sequences (or equivalent specified in .mh_profile) * or context file (for private sequences). */ @@ -73,7 +73,7 @@ seq_public (struct msgs *mp) /* get filename of sequence file */ snprintf (seqfile, sizeof(seqfile), "%s/%s", mp->foldpath, mh_seq); - if ((fp = fopen (seqfile, "r")) == NULL) + if ((fp = lkfopen (seqfile, "r")) == NULL) return; /* Use m_getfld to scan sequence file */ @@ -111,7 +111,7 @@ seq_public (struct msgs *mp) break; /* break from for loop */ } - fclose (fp); + lkfclose (fp, seqfile); } diff --git a/sbr/seq_save.c b/sbr/seq_save.c index f8de9ed..84775b9 100644 --- a/sbr/seq_save.c +++ b/sbr/seq_save.c @@ -76,9 +76,9 @@ priv: * If that fails (probably because folder is * readonly), then make sequence private. */ - if ((fp = fopen (seqfile, "w")) == NULL + if ((fp = lkfopen (seqfile, "w")) == NULL && (unlink (seqfile) == -1 || - (fp = fopen (seqfile, "w")) == NULL)) { + (fp = lkfopen (seqfile, "w")) == NULL)) { admonish (attr, "unable to write"); goto priv; } @@ -96,7 +96,7 @@ priv: } if (fp) { - fclose (fp); + lkfclose (fp, seqfile); SIGPROCMASK (SIG_SETMASK, &oset, &set); /* reset signal mask */ } else { /* -- 1.7.10.4