From d8bf5919a86a15da4385fcf1f049a3ec0cc47dcf Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Thu, 21 Jan 2016 17:42:18 +0100 Subject: Add client locking, preventing multiple client runs --- client.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/client.js b/client.js index f6c2278..6a111a3 100755 --- a/client.js +++ b/client.js @@ -11,6 +11,30 @@ var fs=require("fs"), var HOSTNAME=process.argv[2]||"tomsmeding.nl",HTTPPORT=42420; console.log("Using server "+HOSTNAME); + +var lockfiledesc; +var lockfilename="/tmp/.lock_serverstore_client_"+HOSTNAME; +try {lockfiledesc=fs.openSync(lockfilename,"wx");} +catch(e){ + var lockedpid=+fs.readFileSync(lockfilename); + var canoverwrite=false; + if(isNaN(lockedpid))canoverwrite=true; + try {process.kill(lockedpid,0);} + catch(e){canoverwrite=true;} + if(!canoverwrite){ + console.log("Another copy of the serverstore client with the same hostname seems to be running."); + console.log("If this is not the case, or intentional, delete the file "+lockfilename+" and try again."); + process.exit(1); + } + lockfiledesc=fs.openSync(lockfilename,"w"); +} +fs.write(lockfiledesc,process.pid+"\n"); +process.on("exit",function(){ + fs.closeSync(lockfiledesc); + fs.unlinkSync(lockfilename); +}); + + var userid="0",password="-"; -- cgit v1.2.3-70-g09d2