summary refs log tree commit diff
diff options
context:
space:
mode:
authorC. McEnroe2021-06-11 21:09:46 -0400
committerC. McEnroe2021-06-11 21:15:34 -0400
commitf6e8078c4698d371359ff8827a017535e421496c (patch)
tree2dbe42ed831a8bcbc1a337fe75f2a5e4f764cd42
parentc6009cf13c0fd2a2f458d2081c4ed59a07ca0ef0 (diff)
Exclusively lock save file
Prevents two instances of catgirl from using the same save file and
clobbering each other's data.
-rw-r--r--ui.c6
1 files changed, 6 insertions, 0 deletions
diff --git a/ui.c b/ui.c
index 22b098e..2beba88 100644
--- a/ui.c
+++ b/ui.c
@@ -32,6 +32,7 @@
 #include <curses.h>
 #include <err.h>
 #include <errno.h>
+#include <fcntl.h>
 #include <limits.h>
 #include <signal.h>
 #include <stdarg.h>
@@ -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");