From 8b0ad135bc880685f677aee9d5c59433dd2aad50 Mon Sep 17 00:00:00 2001 From: Curtis McEnroe Date: Mon, 14 Jan 2019 16:32:21 -0500 Subject: Use flock(2) when loading and saving history --- bin/cash/libedit/history.c | 10 ++++++++-- 1 file 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 #include #include #include @@ -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; } -- cgit 1.4.1