+static qfile_t *locksession_fh = NULL;
+static qboolean locksession_run = false;
+static void Host_InitSession(void)
+{
+ int i;
+ Cvar_RegisterVariable(&sessionid);
+ Cvar_RegisterVariable(&locksession);
+
+ // load the session ID into the read-only cvar
+ if ((i = COM_CheckParm("-sessionid")) && (i + 1 < com_argc))
+ {
+ char vabuf[1024];
+ if(com_argv[i+1][0] == '.')
+ Cvar_SetQuick(&sessionid, com_argv[i+1]);
+ else
+ Cvar_SetQuick(&sessionid, va(vabuf, sizeof(vabuf), ".%s", com_argv[i+1]));
+ }
+}
+void Host_LockSession(void)
+{
+ if(locksession_run)
+ return;
+ locksession_run = true;
+ if(locksession.integer != 0 && !COM_CheckParm("-readonly"))
+ {
+ char vabuf[1024];
+ char *p = va(vabuf, sizeof(vabuf), "%slock%s", *fs_userdir ? fs_userdir : fs_basedir, sessionid.string);
+ FS_CreatePath(p);
+ locksession_fh = FS_SysOpen(p, "wl", false);
+ // TODO maybe write the pid into the lockfile, while we are at it? may help server management tools
+ if(!locksession_fh)
+ {
+ if(locksession.integer == 2)
+ {
+ Con_Printf("WARNING: session lock %s could not be acquired. Please run with -sessionid and an unique session name. Continuing anyway.\n", p);
+ }
+ else
+ {
+ Sys_Error("session lock %s could not be acquired. Please run with -sessionid and an unique session name.\n", p);
+ }
+ }
+ }
+}
+void Host_UnlockSession(void)
+{
+ if(!locksession_run)
+ return;
+ locksession_run = false;
+
+ if(locksession_fh)
+ {
+ FS_Close(locksession_fh);
+ // NOTE: we can NOT unlink the lock here, as doing so would
+ // create a race condition if another process created it
+ // between our close and our unlink
+ locksession_fh = NULL;
+ }
+}
+