summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortomsmeding <tom.smeding@gmail.com>2016-01-21 17:42:18 +0100
committertomsmeding <tom.smeding@gmail.com>2016-01-21 17:42:18 +0100
commitd8bf5919a86a15da4385fcf1f049a3ec0cc47dcf (patch)
tree28955cc4f9470c521f434f5a96cccc1c7b2a2695
parent29e461d2b0cdd1b0fa867c4f4b902d393610b909 (diff)
Add client locking, preventing multiple client runs
-rwxr-xr-xclient.js24
1 files changed, 24 insertions, 0 deletions
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="-";