summary refs log tree commit diff
path: root/bin/cash/libedit
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2019-01-14 16:32:21 -0500
committerJune McEnroe <june@causal.agency>2019-01-15 15:50:56 -0500
commit8b0ad135bc880685f677aee9d5c59433dd2aad50 (patch)
tree016233e763189a7d0ccd0823d53c543ba5e63d6a /bin/cash/libedit
parentAdd sans8x16.psf (diff)
downloadsrc-8b0ad135bc880685f677aee9d5c59433dd2aad50.tar.gz
src-8b0ad135bc880685f677aee9d5c59433dd2aad50.zip
Use flock(2) when loading and saving history
Diffstat (limited to 'bin/cash/libedit')
-rw-r--r--bin/cash/libedit/history.c10
1 files changed, 8 insertions, 2 deletions
diff --git a/bin/cash/libedit/history.c b/bin/cash/libedit/history.c
index 41d1014c..cf6b5789 100644
--- a/bin/cash/libedit/history.c
+++ b/bin/cash/libedit/history.c
@@ -44,6 +44,7 @@ __RCSID("$NetBSD: history.c,v 1.57 2016/04/11 18:56:31 christos Exp $");
 /*
  * hist.c: TYPE(History) access functions
  */
+#include <sys/file.h>
 #include <sys/stat.h>
 #include <stdarg.h>
 #include <stdlib.h>
@@ -768,7 +769,7 @@ static int
 history_load(TYPE(History) *h, const char *fname)
 {
 	FILE *fp;
-	char *line;
+	char *line = NULL;
 	size_t llen;
 	ssize_t sz;
 	size_t max_size;
@@ -782,7 +783,9 @@ history_load(TYPE(History) *h, const char *fname)
 	if ((fp = fopen(fname, "r")) == NULL)
 		return i;
 
-	line = NULL;
+	if (flock(fileno(fp), LOCK_SH) == -1)
+		goto done;
+
 	llen = 0;
 	if ((sz = getline(&line, &llen, fp)) == -1)
 		goto done;
@@ -836,6 +839,8 @@ history_save_fp(TYPE(History) *h, FILE *fp)
 	static ct_buffer_t conv;
 #endif
 
+	if (flock(fileno(fp), LOCK_EX) == -1)
+		goto done;
 	if (fchmod(fileno(fp), S_IRUSR|S_IWUSR) == -1)
 		goto done;
 	if (fputs(hist_cookie, fp) == EOF)
@@ -864,6 +869,7 @@ history_save_fp(TYPE(History) *h, FILE *fp)
 oomem:
 	h_free(ptr);
 done:
+	flock(fileno(fp), LOCK_UN);
 	return i;
 }