summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--bounce.c49
1 files changed, 29 insertions, 20 deletions
diff --git a/bounce.c b/bounce.c
index 4e7458c..ff54c98 100644
--- a/bounce.c
+++ b/bounce.c
@@ -23,6 +23,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <sys/file.h>
 #include <sys/socket.h>
 #include <sys/stat.h>
 #include <sysexits.h>
@@ -67,6 +68,33 @@ static void eventRemove(size_t i) {
 	event.clients[i] = event.clients[event.len];
 }
 
+static FILE *saveFile;
+
+static void saveExit(void) {
+	int error = ringSave(saveFile);
+	if (error) warn("fwrite");
+	error = fclose(saveFile);
+	if (error) warn("fclose");
+}
+
+static void saveLoad(const char *path) {
+	umask(0066);
+	saveFile = fopen(path, "a+");
+	if (!saveFile) err(EX_CANTCREAT, "%s", path);
+
+	int error = flock(fileno(saveFile), LOCK_EX | LOCK_NB);
+	if (error && errno != EWOULDBLOCK) err(EX_OSERR, "flock");
+	if (error) errx(EX_CANTCREAT, "%s: lock held by other process", path);
+
+	rewind(saveFile);
+	ringLoad(saveFile);
+
+	error = ftruncate(fileno(saveFile), 0);
+	if (error) err(EX_IOERR, "ftruncate");
+
+	atexit(saveExit);
+}
+
 static char *sensitive(char *arg) {
 	char *value = NULL;
 	if (arg[0] == '@') {
@@ -89,14 +117,6 @@ static char *sensitive(char *arg) {
 	return value;
 }
 
-static FILE *saveFile;
-static void exitSave(void) {
-	int error = ringSave(saveFile);
-	if (error) warn("fwrite");
-	error = fclose(saveFile);
-	if (error) warn("fclose");
-}
-
 int main(int argc, char *argv[]) {
 	const char *localHost = "localhost";
 	const char *localPort = "6697";
@@ -158,18 +178,7 @@ int main(int argc, char *argv[]) {
 	if (!user) user = nick;
 	if (!real) real = nick;
 
-	if (save) {
-		umask(0066);
-		saveFile = fopen(save, "a+");
-		if (!saveFile) err(EX_CANTCREAT, "%s", save);
-
-		rewind(saveFile);
-		ringLoad(saveFile);
-
-		int error = ftruncate(fileno(saveFile), 0);
-		if (error) err(EX_IOERR, "ftruncate");
-		atexit(exitSave);
-	}
+	if (save) saveLoad(save);
 
 	listenConfig(certPath, privPath);
 
?h=3.7.0&id=20e2ef936b182c461b604fa874a556abd07c98e2&follow=1'>import: Add missing scripts/wrap-compiler-for-flag-checkJune McEnroe 2021-05-08Import LibreSSL 3.3.3June McEnroe 2021-04-18build: Remove added x509_verify.3 links 3.3.2June McEnroe 2021-04-18tls: Use EC_KEY_set_ex_dataJune McEnroe 2021-04-18Import LibreSSL 3.3.2June McEnroe 2021-03-05Bump version to 3.3.1p1 3.3.1p1June McEnroe 2021-03-05build: Add OpenSSL includes to libcompatJune McEnroe Some compat sources (getentropy_linux.c for example) require OpenSSL. Reported by Robert Scheck. 2020-12-15Import LibreSSL 3.3.1June McEnroe 2020-11-24Import LibreSSL 3.3.0June McEnroe 2020-10-22Import LibreSSL 3.2.2June McEnroe 2020-09-29Import LibreSSL 3.2.1June McEnroe 2020-09-29import: Add m4/ax_add_fortify_source.m4June McEnroe 2020-08-05build: Add README.7 to EXTRA_DIST 3.2.0June McEnroe 2020-08-03doc: Indicate that only OpenSSL 1.1.1b and newer workJune McEnroe