From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-1.0 required=5.0 tests=MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham autolearn_force=no version=3.4.2 Received: from primenet.com.au (ns1.primenet.com.au [203.24.36.2]) by inbox.vuxu.org (OpenSMTPD) with ESMTP id aa643d8c for ; Sat, 9 Mar 2019 00:55:35 +0000 (UTC) Received: (qmail 26378 invoked by alias); 9 Mar 2019 00:55:25 -0000 Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Workers List List-Post: List-Help: List-Unsubscribe: X-Seq: 44106 Received: (qmail 17093 invoked by uid 1010); 9 Mar 2019 00:55:25 -0000 X-Qmail-Scanner-Diagnostics: from granite.fifsource.com by f.primenet.com.au (envelope-from , uid 7791) with qmail-scanner-2.11 (clamdscan: 0.100.2/25376. spamassassin: 3.4.2. Clear:RC:0(173.255.216.206):SA:0(-1.9/5.0):. Processed in 0.800258 secs); 09 Mar 2019 00:55:25 -0000 X-Envelope-From: phil+github-commits@fifi.org X-Qmail-Scanner-Mime-Attachments: | X-Qmail-Scanner-Zip-Files: | Received-SPF: pass (ns1.primenet.com.au: SPF record at fifi.org designates 173.255.216.206 as permitted sender) From: Philippe Troin To: Zsh hackers list Cc: Philippe Troin Subject: [PATCH 1/3] Move readhistfile() after flockhistfile(). Date: Fri, 8 Mar 2019 16:54:40 -0800 Message-Id: <20190309005442.944477-1-phil+github-commits@fifi.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <70520d027e2f5ddc09b2c78d543664b52341f450.camel@fifi.org> References: <70520d027e2f5ddc09b2c78d543664b52341f450.camel@fifi.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Qmail-Scanner-2.11: added fake Content-Type header Content-Type: text/plain --- 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