X-Git-Url: https://code.citadel.org/?p=citadel.git;a=blobdiff_plain;f=citadel%2Fserver_main.c;h=061273a6336c034c2357039bf40ad3db4802a952;hp=45312588ee614e90f17c836fd2b05e95f7c5578c;hb=e6499f44a49d449e41ba605a0c7476019da432b4;hpb=ab71006c986efb39298cf0b9e5eed3988e8e2a74 diff --git a/citadel/server_main.c b/citadel/server_main.c index 45312588e..061273a63 100644 --- a/citadel/server_main.c +++ b/citadel/server_main.c @@ -14,6 +14,7 @@ #include #include #include +#include #include #include "citserver.h" @@ -34,6 +35,42 @@ const char *CitadelServiceTCP="citadel-TCP"; void go_threading(void); + + +/* + * Create or remove a lock file, so we only have one Citadel Server running at a time. + */ +void ctdl_lockfile(int yo) { + static char lockfilename[SIZ]; + static FILE *fp; + + + if (yo) { + syslog(LOG_DEBUG, "Creating lockfile"); + snprintf(lockfilename, sizeof lockfilename, "%s/citadel.lock", ctdl_run_dir); + fp = fopen(lockfilename, "w"); + if (!fp) { + syslog(LOG_ERR, "Cannot open or create %s", lockfilename); + exit(CTDLEXIT_DB); + } + if (flock(fileno(fp), (LOCK_EX|LOCK_NB)) != 0) { + syslog(LOG_ERR, "Cannot lock %s , is another citserver running?", lockfilename); + exit(CTDLEXIT_DB); + } + return; + } + + syslog(LOG_DEBUG, "Removing lockfile"); + unlink(lockfilename); + flock(fileno(fp), LOCK_UN); + fclose(fp); +} + + + + + + /* * Here's where it all begins. */ @@ -232,6 +269,8 @@ int main(int argc, char **argv) #endif + ctdl_lockfile(1); + /* Initialize... */ init_sysdep(); @@ -362,6 +401,7 @@ int main(int argc, char **argv) go_threading(); - master_cleanup(exit_signal); - return(0); + int exit_code = master_cleanup(exit_signal); + ctdl_lockfile(0); + return(exit_code); }