From dbf5e7758f297321c0b5260d5cb49c4e5b852858 Mon Sep 17 00:00:00 2001 From: "Rob Swindell (on ChromeOS)" <rob@synchro.net> Date: Sun, 17 Nov 2024 16:35:15 -0800 Subject: [PATCH] Revert "Use fmutex_open for 100% locking and auto-removal upon close/termination" This reverts commit e55183c0953d0b033d9f11796f4290e488596e92. fmutex_open()'s auto-remove feature doesn't work on normal/local Linux file systems: unlink() of a file immediately removes its directory entry allowing subsequent opens of the same file name, defeating the lock. I had tested fmutex_open() on a Samba mount (from Linux) and it worked as I wanted (like the Windows version does). Still searching for a good solution: if a process terminates, gracefully or not, I'd like the file to be removed at that time and until then, other processes or threads can't create or open the same lock file (using O_EXCL and file locking to insure this). --- src/sbbs3/sbbsecho.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/sbbs3/sbbsecho.c b/src/sbbs3/sbbsecho.c index e56eef0e5f..6a0ea1591f 100644 --- a/src/sbbs3/sbbsecho.c +++ b/src/sbbs3/sbbsecho.c @@ -103,6 +103,7 @@ char* text[TOTAL_TEXT]; bool pause_on_exit=false; bool pause_on_abend=false; +bool mtxfile_locked=false; bool terminated=false; str_list_t locked_bso_nodes; @@ -686,7 +687,7 @@ bool bso_lock_node(fidoaddr_t dest) for(unsigned attempt=0;;) { char tmp[128]; time_t t; - if(fmutex_open(fname, program_id(), cfg.bsy_timeout, &t, /* auto-remove: */true) >= 0) + if(fmutex(fname, program_id(), cfg.bsy_timeout, &t)) break; lprintf(LOG_NOTICE, "Node (%s) externally locked via: %s (since %s)", smb_faddrtoa(&dest, NULL), fname, time_as_hhmm(&scfg, t, tmp)); if(++attempt >= cfg.bso_lock_attempts) { @@ -2987,6 +2988,7 @@ const char* area_desc(const char* areatag) void cleanup(void) { char* p; + char path[MAX_PATH+1]; if(bad_areas != NULL) { lprintf(LOG_DEBUG, "Writing %lu areas to %s", (ulong)strListCount(bad_areas), cfg.badareafile); @@ -3009,6 +3011,16 @@ void cleanup(void) } strListFree(&bad_areas); } + while((p=strListPop(&locked_bso_nodes)) != NULL) { + delfile(p, __LINE__); + free(p); + } + + if(mtxfile_locked) { + SAFEPRINTF(path,"%ssbbsecho.bsy", scfg.ctrl_dir); + if(delfile(path, __LINE__)) + mtxfile_locked = false; + } } void bail(int error_level) @@ -6419,10 +6431,11 @@ int main(int argc, char **argv) SAFEPRINTF(path,"%ssbbsecho.bsy", scfg.ctrl_dir); time_t t; - if(fmutex_open(path, program_id(), cfg.bsy_timeout, &t, /* auto-remove: */true) < 0) { + if(!fmutex(path, program_id(), cfg.bsy_timeout, &t)) { lprintf(LOG_WARNING, "Mutex file exists (%s): SBBSecho appears to be already running since %s", path, time_as_hhmm(&scfg, t, str)); bail(1); } + mtxfile_locked = true; atexit(cleanup); if(cfg.max_log_size && ftello(fidologfile) >= cfg.max_log_size) { -- GitLab