From f6e8078c4698d371359ff8827a017535e421496c Mon Sep 17 00:00:00 2001 From: "C. McEnroe" Date: Fri, 11 Jun 2021 21:09:46 -0400 Subject: Exclusively lock save file Prevents two instances of catgirl from using the same save file and clobbering each other's data. --- ui.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/ui.c b/ui.c index 22b098e..2beba88 100644 --- a/ui.c +++ b/ui.c @@ -32,6 +32,7 @@ #include #include #include +#include #include #include #include @@ -1183,6 +1184,11 @@ void uiLoad(const char *name) { if (!saveFile) exit(EX_CANTCREAT); rewind(saveFile); + int error = flock(fileno(saveFile), LOCK_EX | LOCK_NB); + if (error && errno == EWOULDBLOCK) { + errx(EX_CANTCREAT, "%s: save file in use", name); + } + time_t signature; fread(&signature, sizeof(signature), 1, saveFile); if (ferror(saveFile)) err(EX_IOERR, "fread"); -- cgit 1.4.1