17
17
package org .polypheny .control .control ;
18
18
19
19
20
- import java .io .BufferedReader ;
21
20
import java .io .IOException ;
22
21
import java .io .InputStream ;
23
- import java .io .InputStreamReader ;
24
22
import java .io .OutputStream ;
25
- import java .security .AccessController ;
26
- import java .security .PrivilegedAction ;
27
23
import java .util .concurrent .TimeUnit ;
28
24
import lombok .extern .slf4j .Slf4j ;
29
25
import lombok .val ;
@@ -110,13 +106,13 @@ private static final class WindowsPolyphenyDbProcess extends PolyphenyDbProcess
110
106
private final WinProcess winProcess ;
111
107
112
108
113
- protected WindowsPolyphenyDbProcess ( final int pid ) {
109
+ private WindowsPolyphenyDbProcess ( final int pid ) {
114
110
super ( null );
115
111
winProcess = new WinProcess ( pid );
116
112
}
117
113
118
114
119
- protected WindowsPolyphenyDbProcess ( final Process process ) {
115
+ private WindowsPolyphenyDbProcess ( final Process process ) {
120
116
super ( process );
121
117
winProcess = new WinProcess ( process );
122
118
}
@@ -171,6 +167,7 @@ public void killForcibly() {
171
167
super .process .destroyForcibly ();
172
168
}
173
169
}
170
+
174
171
}
175
172
176
173
@@ -181,39 +178,22 @@ private static final class UnixPolyphenyDbProcess extends PolyphenyDbProcess {
181
178
182
179
private final int pid ;
183
180
181
+ private final ProcessHandle handle ;
182
+
184
183
185
- protected UnixPolyphenyDbProcess ( final int pid ) {
184
+ private UnixPolyphenyDbProcess ( final int pid ) {
186
185
super ( null );
187
186
this .pid = pid ;
187
+ this .handle = ProcessHandle .of ( pid ).get ();
188
188
}
189
189
190
190
191
- protected UnixPolyphenyDbProcess ( final Process process ) {
191
+ private UnixPolyphenyDbProcess ( final Process process ) {
192
192
super ( process );
193
193
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 ();
217
197
}
218
198
219
199
@@ -225,71 +205,21 @@ public int getPid() {
225
205
226
206
@ Override
227
207
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 ();
248
209
}
249
210
250
211
251
212
@ Override
252
213
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 ();
268
215
}
269
216
270
217
271
218
@ Override
272
219
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 ();
293
221
}
222
+
294
223
}
224
+
295
225
}
0 commit comments