Compare commits
10 commits
0b8cf633ee
...
92fb02b0bf
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
92fb02b0bf | ||
|
|
4aa0a648f9 | ||
|
|
6a88ffdd03 | ||
|
|
eaae46a452 | ||
|
|
ee48ef241f | ||
|
|
2582da5def | ||
|
|
e74c74b909 | ||
|
|
2deaff99f7 | ||
|
|
18a7fcc0b3 | ||
|
|
5871e1b152 |
6 changed files with 80 additions and 76 deletions
|
|
@ -17,8 +17,13 @@ compile:
|
||||||
|
|
||||||
tests:
|
tests:
|
||||||
stage: test
|
stage: test
|
||||||
script: "./gradlew test"
|
script:
|
||||||
|
- "./certs-gen.sh"
|
||||||
|
- "./gradlew test"
|
||||||
|
|
||||||
package:
|
package:
|
||||||
stage: deploy
|
stage: deploy
|
||||||
script: "./package.sh"
|
script:
|
||||||
|
- "./certs-gen.sh"
|
||||||
|
- "./gradlew build"
|
||||||
|
- "./package.sh"
|
||||||
|
|
|
||||||
5
buildwrapper.sh
Executable file
5
buildwrapper.sh
Executable file
|
|
@ -0,0 +1,5 @@
|
||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
./certs-gen.sh
|
||||||
|
./gradlew clean build
|
||||||
|
./package.sh
|
||||||
|
|
@ -13,6 +13,7 @@ if [ -z "$CLTNAME" ]; then
|
||||||
CLTNAME=localhost
|
CLTNAME=localhost
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|
||||||
SERVER_CA_CN=jobserv-server-ca
|
SERVER_CA_CN=jobserv-server-ca
|
||||||
SERVER_PATH=resources/server
|
SERVER_PATH=resources/server
|
||||||
CLIENT_CA_CN=jobserv-client-ca
|
CLIENT_CA_CN=jobserv-client-ca
|
||||||
|
|
@ -95,3 +96,4 @@ echo "[+] Converting private keys to X.509"
|
||||||
openssl pkcs8 -topk8 -nocrypt -in $CLIENT_PATH/private.key -out $CLIENT_PATH/private.pem
|
openssl pkcs8 -topk8 -nocrypt -in $CLIENT_PATH/private.key -out $CLIENT_PATH/private.pem
|
||||||
openssl pkcs8 -topk8 -nocrypt -in $SERVER_PATH/private.key -out $SERVER_PATH/private.pem
|
openssl pkcs8 -topk8 -nocrypt -in $SERVER_PATH/private.key -out $SERVER_PATH/private.pem
|
||||||
openssl pkcs8 -topk8 -nocrypt -in $TEST_PATH/private.key -out $TEST_PATH/private.pem
|
openssl pkcs8 -topk8 -nocrypt -in $TEST_PATH/private.key -out $TEST_PATH/private.pem
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -8,11 +8,14 @@
|
||||||
|
|
||||||
package JobServ;
|
package JobServ;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.util.concurrent.locks.Lock;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* ProcessController
|
* ProcessController
|
||||||
|
|
@ -33,18 +36,23 @@ class ProcessController {
|
||||||
private BufferedReader reader;
|
private BufferedReader reader;
|
||||||
|
|
||||||
private Process process;
|
private Process process;
|
||||||
|
|
||||||
private Boolean killedManually = false;
|
private Boolean killedManually = false;
|
||||||
|
|
||||||
|
private Lock lock;
|
||||||
|
private int lockTimeout; // seconds
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Constructor
|
* Constructor
|
||||||
* Takes a command and spawns it in a new process
|
* Takes a command and spawns it in a new process
|
||||||
* Redirects IO streams and assigns a fake PID
|
* Redirects IO streams and assigns a fake PID
|
||||||
*/
|
*/
|
||||||
public ProcessController(String command) throws IOException {
|
public ProcessController(String command, int lockTimeout) throws IOException {
|
||||||
this.pid = ProcessController.nextPid;
|
this.pid = ProcessController.nextPid;
|
||||||
ProcessController.nextPid += 1;
|
ProcessController.nextPid += 1;
|
||||||
|
|
||||||
|
this.lock = new ReentrantLock();
|
||||||
|
this.lockTimeout = lockTimeout;
|
||||||
|
|
||||||
this.process = Runtime.getRuntime().exec(command);
|
this.process = Runtime.getRuntime().exec(command);
|
||||||
this.output = this.process.getOutputStream();
|
this.output = this.process.getOutputStream();
|
||||||
this.input = this.process.getInputStream();
|
this.input = this.process.getInputStream();
|
||||||
|
|
@ -54,6 +62,28 @@ class ProcessController {
|
||||||
JobServServer.logger.write("Job " + String.valueOf(this.pid) + ": " + command);
|
JobServServer.logger.write("Job " + String.valueOf(this.pid) + ": " + command);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* getLock()
|
||||||
|
* attempts to get the lock for lockTimeout seconds
|
||||||
|
* or throws exceptions if interrupted
|
||||||
|
*/
|
||||||
|
public boolean getLock() throws InterruptedException {
|
||||||
|
return this.lock.tryLock(this.lockTimeout, TimeUnit.SECONDS);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* releaseLock()
|
||||||
|
* releases lock on process
|
||||||
|
*/
|
||||||
|
public void releaseLock() {
|
||||||
|
try {
|
||||||
|
this.lock.unlock();
|
||||||
|
|
||||||
|
} catch (IllegalMonitorStateException e) {
|
||||||
|
JobServServer.logger.write("Thread tried to release a lock it didnt have! " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* getPid()
|
* getPid()
|
||||||
* returns translated pid of this process
|
* returns translated pid of this process
|
||||||
|
|
|
||||||
|
|
@ -40,7 +40,6 @@ class ProcessManager {
|
||||||
* processMap
|
* processMap
|
||||||
*/
|
*/
|
||||||
protected ConcurrentHashMap<Integer, ProcessController> processMap;
|
protected ConcurrentHashMap<Integer, ProcessController> processMap;
|
||||||
protected ConcurrentHashMap<Integer, Boolean> lockMap;
|
|
||||||
private ExecutorService threadPool = Executors.newCachedThreadPool();
|
private ExecutorService threadPool = Executors.newCachedThreadPool();
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
@ -49,7 +48,6 @@ class ProcessManager {
|
||||||
*/
|
*/
|
||||||
public ProcessManager() {
|
public ProcessManager() {
|
||||||
processMap = new ConcurrentHashMap<Integer, ProcessController>();
|
processMap = new ConcurrentHashMap<Integer, ProcessController>();
|
||||||
lockMap = new ConcurrentHashMap<Integer, Boolean>();
|
|
||||||
/* TODO: In a long running server over a large period of time
|
/* TODO: In a long running server over a large period of time
|
||||||
* It is possible that the streams used to redirect IO in the
|
* It is possible that the streams used to redirect IO in the
|
||||||
* Processes may become a significant use of resources.
|
* Processes may become a significant use of resources.
|
||||||
|
|
@ -68,12 +66,8 @@ class ProcessManager {
|
||||||
public int newProcess(String command) {
|
public int newProcess(String command) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
ProcessController newProc = new ProcessController(command);
|
ProcessController newProc = new ProcessController(command, this.LOCK_TIMEOUT);
|
||||||
// we dont need to lock the map yet
|
|
||||||
this.lockMap.put(newProc.getPid(), true);
|
|
||||||
this.processMap.put(newProc.getPid(), newProc);
|
this.processMap.put(newProc.getPid(), newProc);
|
||||||
|
|
||||||
this.releaseLock(newProc.getPid());
|
|
||||||
return newProc.getPid();
|
return newProc.getPid();
|
||||||
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
|
@ -95,14 +89,13 @@ class ProcessManager {
|
||||||
public int getProcessStatus(int pid) {
|
public int getProcessStatus(int pid) {
|
||||||
try {
|
try {
|
||||||
if(!this.getLock(pid)) {
|
if(!this.getLock(pid)) {
|
||||||
return 3;
|
// lock could not be grabbed before timeout
|
||||||
|
JobServServer.logger.write("Timeout getting process status: " + String.valueOf(pid));
|
||||||
|
return 4;
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (TimeoutException e) {
|
} catch (IndexOutOfBoundsException e) {
|
||||||
// lock could not be grabbed before timeout
|
return 3;
|
||||||
JobServServer.logger.write("Timeout getting process " +
|
|
||||||
String.valueOf(pid) + " status: " + e.getMessage());
|
|
||||||
return 4;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ProcessController candidate = this.processMap.get(pid);
|
ProcessController candidate = this.processMap.get(pid);
|
||||||
|
|
@ -123,13 +116,12 @@ class ProcessManager {
|
||||||
public int getProcessReturn(int pid) {
|
public int getProcessReturn(int pid) {
|
||||||
try {
|
try {
|
||||||
if(!this.getLock(pid)) {
|
if(!this.getLock(pid)) {
|
||||||
return 258;
|
JobServServer.logger.write("Timeout getting process return: " + String.valueOf(pid));
|
||||||
|
return 259;
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (TimeoutException e) {
|
} catch (IndexOutOfBoundsException e) {
|
||||||
JobServServer.logger.write("Timeout getting process " +
|
return 258;
|
||||||
String.valueOf(pid) + " return: " + e.getMessage());
|
|
||||||
return 259;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ProcessController candidate = this.processMap.get(pid);
|
ProcessController candidate = this.processMap.get(pid);
|
||||||
|
|
@ -146,13 +138,12 @@ class ProcessManager {
|
||||||
public String getProcessOutput(int pid, int lines) {
|
public String getProcessOutput(int pid, int lines) {
|
||||||
try {
|
try {
|
||||||
if(!this.getLock(pid)) {
|
if(!this.getLock(pid)) {
|
||||||
return "[-] SERVER: Process not found";
|
JobServServer.logger.write("Timeout getting process output: " + String.valueOf(pid));
|
||||||
|
return "[-] SERVER: Timeout grabbing lock to access process information";
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (TimeoutException e) {
|
} catch (IndexOutOfBoundsException e) {
|
||||||
JobServServer.logger.write("Timeout getting process " +
|
return "[-] SERVER: Process not found";
|
||||||
String.valueOf(pid) + " output: " + e.getMessage());
|
|
||||||
return "[-] SERVER: Timeout grabbing lock to access process information";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ProcessController candidate = this.processMap.get(pid);
|
ProcessController candidate = this.processMap.get(pid);
|
||||||
|
|
@ -172,13 +163,13 @@ class ProcessManager {
|
||||||
public int killProcess(int pid) {
|
public int killProcess(int pid) {
|
||||||
try {
|
try {
|
||||||
if(!this.getLock(pid)) {
|
if(!this.getLock(pid)) {
|
||||||
return 2;
|
JobServServer.logger.write("Timeout killing process: " + String.valueOf(pid));
|
||||||
|
return 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (TimeoutException e) {
|
} catch (IndexOutOfBoundsException e) {
|
||||||
JobServServer.logger.write("Timeout killing process " +
|
|
||||||
String.valueOf(pid) + ": " + e.getMessage());
|
return 2;
|
||||||
return 3;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ProcessController candidate = this.processMap.get(pid);
|
ProcessController candidate = this.processMap.get(pid);
|
||||||
|
|
@ -194,45 +185,20 @@ class ProcessManager {
|
||||||
* Function is synchronized to prevent multiple threads accessing the same lock at once
|
* Function is synchronized to prevent multiple threads accessing the same lock at once
|
||||||
* (ConcurrentHashMap will report whatever lock value was last to successfully update)
|
* (ConcurrentHashMap will report whatever lock value was last to successfully update)
|
||||||
*/
|
*/
|
||||||
protected synchronized Boolean getLock(int pid) throws TimeoutException {
|
protected synchronized Boolean getLock(int pid) throws IndexOutOfBoundsException {
|
||||||
if (!lockMap.containsKey(pid)) {
|
ProcessController candidate = this.processMap.get(pid);
|
||||||
return false;
|
if (candidate == null) {
|
||||||
|
throw new IndexOutOfBoundsException();
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<Object> future = this.threadPool.submit(
|
|
||||||
new Callable<Object>() {
|
|
||||||
public Object call() {
|
|
||||||
while(lockMap.get(pid)) {
|
|
||||||
continue; // spin!
|
|
||||||
}
|
|
||||||
|
|
||||||
lockMap.replace(pid, true);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
future.get(this.LOCK_TIMEOUT, TimeUnit.SECONDS);
|
Boolean success = candidate.getLock();
|
||||||
|
return success;
|
||||||
|
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
JobServServer.logger.write("[!] Couldnt get lock " +
|
JobServServer.logger.write("[!] Couldnt get lock " +
|
||||||
String.valueOf(pid) + ": "+ e.getMessage());
|
String.valueOf(pid) + ": "+ e.getMessage());
|
||||||
future.cancel(true);
|
|
||||||
|
|
||||||
// in case lock was grabbed after exception
|
|
||||||
this.releaseLock(pid);
|
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
} catch (ExecutionException e) {
|
|
||||||
JobServServer.logger.write("[!] Couldnt get lock " +
|
|
||||||
String.valueOf(pid) + ": "+ e.getMessage());
|
|
||||||
future.cancel(true);
|
|
||||||
|
|
||||||
// in case lock was grabbed after exception
|
|
||||||
this.releaseLock(pid);
|
|
||||||
return false;
|
|
||||||
|
|
||||||
// cancel the attempt to grab the lock
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
@ -247,8 +213,6 @@ class ProcessManager {
|
||||||
* mediate access to the ProcessManager
|
* mediate access to the ProcessManager
|
||||||
* object for fresh calls as well.
|
* object for fresh calls as well.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
@ -256,7 +220,13 @@ class ProcessManager {
|
||||||
* releases mutex so other threads can operate on processqueue
|
* releases mutex so other threads can operate on processqueue
|
||||||
*/
|
*/
|
||||||
protected void releaseLock(int pid) {
|
protected void releaseLock(int pid) {
|
||||||
this.lockMap.put(pid, false);
|
ProcessController candidate = this.processMap.get(pid);
|
||||||
|
if (candidate == null) {
|
||||||
|
JobServServer.logger.write("Tried to release lock of process that doesnt exist!");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
candidate.releaseLock();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
|
||||||
|
|
@ -27,18 +27,10 @@ class ProcessManagerTestImplementation extends ProcessManager {
|
||||||
|
|
||||||
super.releaseLock(pid);
|
super.releaseLock(pid);
|
||||||
|
|
||||||
} catch (TimeoutException e) {
|
|
||||||
System.err.println("[!!] Long Call wasnt able to grab lock!");
|
|
||||||
return;
|
|
||||||
|
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
super.releaseLock(pid); // this doesnt happen, dont cancel this task
|
super.releaseLock(pid); // this doesnt happen, dont cancel this task
|
||||||
System.err.println("[3] Released lock: interrupted");
|
System.err.println("[3] Released lock: interrupted");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Boolean reportLockState(int pid) {
|
|
||||||
return super.lockMap.get(pid);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue