From: Philippe Troin <phil+github-commits@fifi.org>
To: Zsh hackers list <zsh-workers@zsh.org>
Cc: Philippe Troin <phil+github-commits@fifi.org>
Subject: [PATCH 1/3] Move readhistfile() after flockhistfile().
Date: Fri, 8 Mar 2019 16:54:40 -0800 [thread overview]
Message-ID: <20190309005442.944477-1-phil+github-commits@fifi.org> (raw)
In-Reply-To: <70520d027e2f5ddc09b2c78d543664b52341f450.camel@fifi.org>
---
Src/hist.c | 96 +++++++++++++++++++++++++++---------------------------
1 file changed, 48 insertions(+), 48 deletions(-)
diff --git a/Src/hist.c b/Src/hist.c
index f7e53de74..4e1f24afe 100644
--- a/Src/hist.c
+++ b/Src/hist.c
@@ -2525,6 +2525,54 @@ readhistline(int start, char **bufp, int *bufsiz, FILE *in)
return 0;
}
+#ifdef HAVE_FCNTL_H
+static int flock_fd = -1;
+
+/*
+ * Lock file using fcntl(). Return 0 on success, 1 on failure of
+ * locking mechanism, 2 on permanent failure (e.g. permission).
+ */
+
+static int
+flockhistfile(char *fn, int keep_trying)
+{
+ struct flock lck;
+ long sleep_us = 0x10000; /* about 67 ms */
+ time_t end_time;
+
+ if (flock_fd >= 0)
+ return 0; /* already locked */
+
+ if ((flock_fd = open(unmeta(fn), O_RDWR | O_NOCTTY)) < 0)
+ return errno == ENOENT ? 0 : 2; /* "successfully" locked missing file */
+
+ lck.l_type = F_WRLCK;
+ lck.l_whence = SEEK_SET;
+ lck.l_start = 0;
+ lck.l_len = 0; /* lock the whole file */
+
+ /*
+ * Timeout is ten seconds.
+ */
+ end_time = time(NULL) + (time_t)10;
+ while (fcntl(flock_fd, F_SETLKW, &lck) == -1) {
+ if (!keep_trying || time(NULL) >= end_time ||
+ /*
+ * Randomise wait to minimise clashes with shells exiting at
+ * the same time.
+ */
+ !zsleep_random(sleep_us, end_time)) {
+ close(flock_fd);
+ flock_fd = -1;
+ return 1;
+ }
+ sleep_us <<= 1;
+ }
+
+ return 0;
+}
+#endif
+
/**/
void
readhistfile(char *fn, int err, int readflags)
@@ -2721,54 +2769,6 @@ readhistfile(char *fn, int err, int readflags)
zleentry(ZLE_CMD_SET_HIST_LINE, curhist);
}
-#ifdef HAVE_FCNTL_H
-static int flock_fd = -1;
-
-/*
- * Lock file using fcntl(). Return 0 on success, 1 on failure of
- * locking mechanism, 2 on permanent failure (e.g. permission).
- */
-
-static int
-flockhistfile(char *fn, int keep_trying)
-{
- struct flock lck;
- long sleep_us = 0x10000; /* about 67 ms */
- time_t end_time;
-
- if (flock_fd >= 0)
- return 0; /* already locked */
-
- if ((flock_fd = open(unmeta(fn), O_RDWR | O_NOCTTY)) < 0)
- return errno == ENOENT ? 0 : 2; /* "successfully" locked missing file */
-
- lck.l_type = F_WRLCK;
- lck.l_whence = SEEK_SET;
- lck.l_start = 0;
- lck.l_len = 0; /* lock the whole file */
-
- /*
- * Timeout is ten seconds.
- */
- end_time = time(NULL) + (time_t)10;
- while (fcntl(flock_fd, F_SETLKW, &lck) == -1) {
- if (!keep_trying || time(NULL) >= end_time ||
- /*
- * Randomise wait to minimise clashes with shells exiting at
- * the same time.
- */
- !zsleep_random(sleep_us, end_time)) {
- close(flock_fd);
- flock_fd = -1;
- return 1;
- }
- sleep_us <<= 1;
- }
-
- return 0;
-}
-#endif
-
/**/
void
savehistfile(char *fn, int err, int writeflags)
--
2.20.1
next prev parent reply other threads:[~2019-03-09 0:55 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-02-27 18:30 Issues with fcntl() history file locking Philippe Troin
2019-02-27 21:27 ` Bart Schaefer
2019-02-28 6:36 ` Philippe Troin
2019-03-09 0:53 ` Philippe Troin
2019-03-09 0:54 ` Philippe Troin [this message]
2019-03-09 0:54 ` [PATCH 2/3] Factorize the code that unlock the fcntl() lock into funlockhistfile() Philippe Troin
2019-03-09 0:54 ` [PATCH 3/3] Delay closing the history file until the fcntl-lock is released Philippe Troin
[not found] ` <CAH+w=7aUD11M_GYy-FOC5MPGpGXb+o9O_q855OTC32fnSnpshQ@mail.gmail.com>
[not found] ` <82f4a6db638fbfce396e64a45029424185863068.camel@fifi.org>
[not found] ` <CAH+w=7ZS=ke8xHuBaO+hu0-RTtW=GYnG-0MENfBtTsyyp9joyg@mail.gmail.com>
[not found] ` <3228a3e68f2580fc25a9fda9bf7ccf5ce9a73689.camel@fifi.org>
2019-03-10 1:19 ` [PATCH 1/3] Move readhistfile() after flockhistfile() Bart Schaefer
2019-03-11 21:04 ` Jason L Tibbitts III
2019-03-12 7:55 ` Jun T
2019-03-12 9:52 ` Peter Stephenson
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20190309005442.944477-1-phil+github-commits@fifi.org \
--to=phil+github-commits@fifi.org \
--cc=zsh-workers@zsh.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).