Skip to content

Commit cc384d9

Browse files
committed
WIP
1 parent f8d2e9e commit cc384d9

File tree

2 files changed

+77
-147
lines changed

2 files changed

+77
-147
lines changed

src/main/java/org/polypheny/control/control/PolyphenyDbProcess.java

+16-86
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,9 @@
1717
package org.polypheny.control.control;
1818

1919

20-
import java.io.BufferedReader;
2120
import java.io.IOException;
2221
import java.io.InputStream;
23-
import java.io.InputStreamReader;
2422
import java.io.OutputStream;
25-
import java.security.AccessController;
26-
import java.security.PrivilegedAction;
2723
import java.util.concurrent.TimeUnit;
2824
import lombok.extern.slf4j.Slf4j;
2925
import lombok.val;
@@ -110,13 +106,13 @@ private static final class WindowsPolyphenyDbProcess extends PolyphenyDbProcess
110106
private final WinProcess winProcess;
111107

112108

113-
protected WindowsPolyphenyDbProcess( final int pid ) {
109+
private WindowsPolyphenyDbProcess( final int pid ) {
114110
super( null );
115111
winProcess = new WinProcess( pid );
116112
}
117113

118114

119-
protected WindowsPolyphenyDbProcess( final Process process ) {
115+
private WindowsPolyphenyDbProcess( final Process process ) {
120116
super( process );
121117
winProcess = new WinProcess( process );
122118
}
@@ -171,6 +167,7 @@ public void killForcibly() {
171167
super.process.destroyForcibly();
172168
}
173169
}
170+
174171
}
175172

176173

@@ -181,39 +178,22 @@ private static final class UnixPolyphenyDbProcess extends PolyphenyDbProcess {
181178

182179
private final int pid;
183180

181+
private final ProcessHandle handle;
182+
184183

185-
protected UnixPolyphenyDbProcess( final int pid ) {
184+
private UnixPolyphenyDbProcess( final int pid ) {
186185
super( null );
187186
this.pid = pid;
187+
this.handle = ProcessHandle.of( pid ).get();
188188
}
189189

190190

191-
protected UnixPolyphenyDbProcess( final Process process ) {
191+
private UnixPolyphenyDbProcess( final Process process ) {
192192
super( process );
193193

194-
long pid = -1;
195-
196-
try {
197-
val pidField = process.getClass().getDeclaredField( "pid" );
198-
199-
AccessController.doPrivileged( (PrivilegedAction<Void>) () -> {
200-
pidField.setAccessible( true );
201-
return null;
202-
} );
203-
204-
pid = pidField.getLong( process );
205-
206-
AccessController.doPrivileged( (PrivilegedAction<Void>) () -> {
207-
pidField.setAccessible( false );
208-
return null;
209-
} );
210-
211-
} catch ( NoSuchFieldException | IllegalAccessException e ) {
212-
log.error( "Exception while extracting the PID from java.lang.Process.", e );
213-
pid = -1;
214-
} finally {
215-
this.pid = (int) pid;
216-
}
194+
log.info( "New process.pid() code" );
195+
this.pid = (int) process.pid();
196+
this.handle = process.toHandle();
217197
}
218198

219199

@@ -225,71 +205,21 @@ public int getPid() {
225205

226206
@Override
227207
public boolean isAlive() {
228-
if ( super.process == null ) {
229-
// "PID Mode"
230-
try {
231-
val process = Runtime.getRuntime().exec( "ps -p " + pid );
232-
try ( val input = new BufferedReader( new InputStreamReader( process.getInputStream() ) ) ) {
233-
String line;
234-
while ( (line = input.readLine()) != null ) {
235-
if ( line.contains( " " + pid + " " ) ) {
236-
return true;
237-
}
238-
}
239-
}
240-
return false;
241-
} catch ( IOException e ) {
242-
log.warn( "IOException while checking if {} is still alive.", pid, e );
243-
return false;
244-
}
245-
} else {
246-
return super.process.isAlive();
247-
}
208+
return handle.isAlive();
248209
}
249210

250211

251212
@Override
252213
public void kill() {
253-
// "PID Mode"
254-
try {
255-
val kill = Runtime.getRuntime().exec( "kill -15 " + pid ); // SIGTERM -- CTRL + C
256-
if ( kill.waitFor( 1, TimeUnit.MINUTES ) ) {
257-
// terminated within the time
258-
if ( kill.exitValue() == 0 ) {
259-
return;
260-
}
261-
}
262-
killForcibly();
263-
} catch ( IOException e ) {
264-
log.error( "Exception while trying to kill <<me:{}>> via PID on Unix.", pid, e );
265-
} catch ( InterruptedException e ) {
266-
log.warn( "Interrupted while waiting for kill to finish." );
267-
}
214+
handle.destroy();
268215
}
269216

270217

271218
@Override
272219
public void killForcibly() {
273-
if ( super.process == null ) {
274-
// "PID Mode"
275-
try {
276-
val kill = Runtime.getRuntime().exec( "kill -9 " + pid ); // SIGKILL
277-
if ( kill.waitFor( 1, TimeUnit.SECONDS ) ) {
278-
// terminated within the time
279-
if ( kill.exitValue() == 0 ) {
280-
return;
281-
}
282-
}
283-
log.warn( "Could not kill {}", pid );
284-
} catch ( IOException e ) {
285-
log.error( "Exception while trying to kill <<me:{}>> via PID on Unix.", pid, e );
286-
} catch ( InterruptedException e ) {
287-
log.warn( "Interrupted while waiting for kill to finish." );
288-
}
289-
290-
} else {
291-
super.process.destroyForcibly();
292-
}
220+
handle.destroyForcibly();
293221
}
222+
294223
}
224+
295225
}

0 commit comments

Comments
 (0)