/
PpidChecker.java
473 lines (422 loc) · 17.1 KB
/
PpidChecker.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
package org.apache.maven.surefire.booter;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import org.apache.maven.surefire.api.booter.DumpErrorSingleton;
import javax.annotation.Nonnull;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Queue;
import java.util.Scanner;
import java.util.TimeZone;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static java.lang.Integer.parseInt;
import static java.lang.Long.parseLong;
import static java.util.concurrent.TimeUnit.DAYS;
import static java.util.concurrent.TimeUnit.HOURS;
import static java.util.concurrent.TimeUnit.MINUTES;
import static java.util.regex.Pattern.compile;
import static org.apache.maven.surefire.shared.io.IOUtils.closeQuietly;
import static org.apache.maven.surefire.shared.lang3.StringUtils.isNotBlank;
import static org.apache.maven.surefire.shared.lang3.SystemUtils.IS_OS_HP_UX;
import static org.apache.maven.surefire.shared.lang3.SystemUtils.IS_OS_LINUX;
import static org.apache.maven.surefire.shared.lang3.SystemUtils.IS_OS_UNIX;
import static org.apache.maven.surefire.shared.lang3.SystemUtils.IS_OS_WINDOWS;
import static org.apache.maven.surefire.booter.ProcessInfo.unixProcessInfo;
import static org.apache.maven.surefire.booter.ProcessInfo.windowsProcessInfo;
import static org.apache.maven.surefire.booter.ProcessInfo.ERR_PROCESS_INFO;
import static org.apache.maven.surefire.booter.ProcessInfo.INVALID_PROCESS_INFO;
import static org.apache.maven.surefire.api.util.internal.StringUtils.NL;
/**
* Recognizes PID of Plugin process and determines lifetime.
*
* @author <a href="mailto:tibordigana@apache.org">Tibor Digana (tibor17)</a>
* @since 2.20.1
*/
final class PpidChecker
{
private static final long MINUTES_TO_MILLIS = 60L * 1000L;
// 25 chars https://superuser.com/questions/937380/get-creation-time-of-file-in-milliseconds/937401#937401
private static final int WMIC_CREATION_DATE_VALUE_LENGTH = 25;
private static final int WMIC_CREATION_DATE_TIMESTAMP_LENGTH = 18;
private static final SimpleDateFormat WMIC_CREATION_DATE_FORMAT =
IS_OS_WINDOWS ? createWindowsCreationDateFormat() : null;
private static final String WMIC_CREATION_DATE = "CreationDate";
private static final String WINDOWS_SYSTEM_ROOT_ENV = "SystemRoot";
private static final String RELATIVE_PATH_TO_WMIC = "System32\\Wbem";
private static final String SYSTEM_PATH_TO_WMIC =
"%" + WINDOWS_SYSTEM_ROOT_ENV + "%\\" + RELATIVE_PATH_TO_WMIC + "\\";
private static final String PS_ETIME_HEADER = "ELAPSED";
private static final String PS_PID_HEADER = "PID";
private final Queue<Process> destroyableCommands = new ConcurrentLinkedQueue<>();
/**
* The etime is in the form of [[dd-]hh:]mm:ss on Unix like systems.
* See the workaround https://issues.apache.org/jira/browse/SUREFIRE-1451.
*/
static final Pattern UNIX_CMD_OUT_PATTERN = compile( "^(((\\d+)-)?(\\d{1,2}):)?(\\d{1,2}):(\\d{1,2})\\s+(\\d+)$" );
static final Pattern BUSYBOX_CMD_OUT_PATTERN = compile( "^(\\d+)[hH](\\d{1,2})\\s+(\\d+)$" );
private final String ppid;
private volatile ProcessInfo parentProcessInfo;
private volatile boolean stopped;
PpidChecker( @Nonnull String ppid )
{
this.ppid = ppid;
}
boolean canUse()
{
if ( isStopped() )
{
return false;
}
final ProcessInfo ppi = parentProcessInfo;
return ppi == null ? IS_OS_WINDOWS || IS_OS_UNIX && canExecuteUnixPs() : ppi.canUse();
}
/**
* This method can be called only after {@link #canUse()} has returned {@code true}.
*
* @return {@code true} if parent process is alive; {@code false} otherwise
* @throws IllegalStateException if {@link #canUse()} returns {@code false}, error to read process
* or this object has been {@link #destroyActiveCommands() destroyed}
* @throws NullPointerException if extracted e-time is null
*/
boolean isProcessAlive()
{
if ( !canUse() )
{
throw new IllegalStateException( "irrelevant to call isProcessAlive()" );
}
final ProcessInfo previousInfo = parentProcessInfo;
if ( IS_OS_WINDOWS )
{
parentProcessInfo = windows();
checkProcessInfo();
// let's compare creation time, should be same unless killed or PID is reused by OS into another process
return !parentProcessInfo.isInvalid()
&& ( previousInfo == null || parentProcessInfo.isTimeEqualTo( previousInfo ) );
}
else if ( IS_OS_UNIX )
{
parentProcessInfo = unix();
checkProcessInfo();
// let's compare elapsed time, should be greater or equal if parent process is the same and still alive
return !parentProcessInfo.isInvalid()
&& ( previousInfo == null || !parentProcessInfo.isTimeBefore( previousInfo ) );
}
parentProcessInfo = ERR_PROCESS_INFO;
throw new IllegalStateException( "unknown platform or you did not call canUse() before isProcessAlive()" );
}
private void checkProcessInfo()
{
if ( isStopped() )
{
throw new IllegalStateException( "error [STOPPED] to read process " + ppid );
}
if ( !parentProcessInfo.canUse() )
{
throw new IllegalStateException( "Cannot use PPID " + ppid + " process information. "
+ "Going to use NOOP events." );
}
}
// https://www.freebsd.org/cgi/man.cgi?ps(1)
// etimes elapsed running time, in decimal integer seconds
// http://manpages.ubuntu.com/manpages/xenial/man1/ps.1.html
// etimes elapsed time since the process was started, in seconds.
// http://hg.openjdk.java.net/jdk7/jdk7/jdk/file/9b8c96f96a0f/test/java/lang/ProcessBuilder/Basic.java#L167
ProcessInfo unix()
{
ProcessInfoConsumer reader = new ProcessInfoConsumer( Charset.defaultCharset().name() )
{
@Override
@Nonnull
ProcessInfo consumeLine( String line, ProcessInfo previousOutputLine )
{
if ( previousOutputLine.isInvalid() )
{
if ( hasHeader )
{
Matcher matcher = UNIX_CMD_OUT_PATTERN.matcher( line );
if ( matcher.matches() && ppid.equals( fromPID( matcher ) ) )
{
long pidUptime = fromDays( matcher )
+ fromHours( matcher )
+ fromMinutes( matcher )
+ fromSeconds( matcher );
return unixProcessInfo( ppid, pidUptime );
}
matcher = BUSYBOX_CMD_OUT_PATTERN.matcher( line );
if ( matcher.matches() && ppid.equals( fromBusyboxPID( matcher ) ) )
{
long pidUptime = fromBusyboxHours( matcher ) + fromBusyboxMinutes( matcher );
return unixProcessInfo( ppid, pidUptime );
}
}
else
{
hasHeader = line.contains( PS_ETIME_HEADER ) && line.contains( PS_PID_HEADER );
}
}
return previousOutputLine;
}
};
String cmd = unixPathToPS() + " -o etime,pid " + ( IS_OS_LINUX ? "" : "-p " ) + ppid;
return reader.execute( "/bin/sh", "-c", cmd );
}
ProcessInfo windows()
{
ProcessInfoConsumer reader = new ProcessInfoConsumer( "US-ASCII" )
{
@Override
@Nonnull
ProcessInfo consumeLine( String line, ProcessInfo previousProcessInfo ) throws Exception
{
if ( previousProcessInfo.isInvalid() && !line.isEmpty() )
{
if ( hasHeader )
{
// now the line is CreationDate, e.g. 20180406142327.741074+120
if ( line.length() != WMIC_CREATION_DATE_VALUE_LENGTH )
{
throw new IllegalStateException( "WMIC CreationDate should have 25 characters "
+ line );
}
String startTimestamp = line.substring( 0, WMIC_CREATION_DATE_TIMESTAMP_LENGTH );
int indexOfTimeZone = WMIC_CREATION_DATE_VALUE_LENGTH - 4;
long startTimestampMillisUTC =
WMIC_CREATION_DATE_FORMAT.parse( startTimestamp ).getTime()
- parseInt( line.substring( indexOfTimeZone ) ) * MINUTES_TO_MILLIS;
return windowsProcessInfo( ppid, startTimestampMillisUTC );
}
else
{
hasHeader = WMIC_CREATION_DATE.equals( line );
}
}
return previousProcessInfo;
}
};
String wmicPath = hasWmicStandardSystemPath() ? SYSTEM_PATH_TO_WMIC : "";
return reader.execute( "CMD", "/A", "/X", "/C",
wmicPath + "wmic process where (ProcessId=" + ppid + ") get " + WMIC_CREATION_DATE
);
}
void destroyActiveCommands()
{
stopped = true;
for ( Process p = destroyableCommands.poll(); p != null; p = destroyableCommands.poll() )
{
p.destroy();
}
}
boolean isStopped()
{
return stopped;
}
private static String unixPathToPS()
{
return canExecuteLocalUnixPs() ? "/usr/bin/ps" : "/bin/ps";
}
static boolean canExecuteUnixPs()
{
return canExecuteLocalUnixPs() || canExecuteStandardUnixPs();
}
private static boolean canExecuteLocalUnixPs()
{
try
{
return new File( "/usr/bin/ps" ).canExecute();
}
catch ( SecurityException e )
{
return false;
}
}
private static boolean canExecuteStandardUnixPs()
{
try
{
return new File( "/bin/ps" ).canExecute();
}
catch ( SecurityException e )
{
return false;
}
}
private static boolean hasWmicStandardSystemPath()
{
String systemRoot = System.getenv( WINDOWS_SYSTEM_ROOT_ENV );
return isNotBlank( systemRoot ) && new File( systemRoot, RELATIVE_PATH_TO_WMIC + "\\wmic.exe" ).isFile();
}
static long fromDays( Matcher matcher )
{
String s = matcher.group( 3 );
return s == null ? 0L : DAYS.toSeconds( parseLong( s ) );
}
static long fromHours( Matcher matcher )
{
String s = matcher.group( 4 );
return s == null ? 0L : HOURS.toSeconds( parseLong( s ) );
}
static long fromMinutes( Matcher matcher )
{
String s = matcher.group( 5 );
return s == null ? 0L : MINUTES.toSeconds( parseLong( s ) );
}
static long fromSeconds( Matcher matcher )
{
String s = matcher.group( 6 );
return s == null ? 0L : parseLong( s );
}
static String fromPID( Matcher matcher )
{
return matcher.group( 7 );
}
static long fromBusyboxHours( Matcher matcher )
{
String s = matcher.group( 1 );
return s == null ? 0L : HOURS.toSeconds( parseLong( s ) );
}
static long fromBusyboxMinutes( Matcher matcher )
{
String s = matcher.group( 2 );
return s == null ? 0L : MINUTES.toSeconds( parseLong( s ) );
}
static String fromBusyboxPID( Matcher matcher )
{
return matcher.group( 3 );
}
private static void checkValid( Scanner scanner )
throws IOException
{
IOException exception = scanner.ioException();
if ( exception != null )
{
throw exception;
}
}
/**
* The beginning part of Windows WMIC format yyyymmddHHMMSS.xxx <br>
* https://technet.microsoft.com/en-us/library/ee198928.aspx <br>
* We use UTC time zone which avoids DST changes, see SUREFIRE-1512.
*
* @return Windows WMIC format yyyymmddHHMMSS.xxx
*/
private static SimpleDateFormat createWindowsCreationDateFormat()
{
SimpleDateFormat formatter = new SimpleDateFormat( "yyyyMMddHHmmss'.'SSS" );
formatter.setTimeZone( TimeZone.getTimeZone( "UTC" ) );
return formatter;
}
public void stop()
{
stopped = true;
}
/**
* Reads standard output from {@link Process}.
* <br>
* The artifact maven-shared-utils has non-daemon Threads which is an issue in Surefire to satisfy System.exit.
* This implementation is taylor made without using any Thread.
* It's easy to destroy Process from other Thread.
*/
private abstract class ProcessInfoConsumer
{
private final String charset;
boolean hasHeader;
ProcessInfoConsumer( String charset )
{
this.charset = charset;
}
abstract @Nonnull ProcessInfo consumeLine( String line, ProcessInfo previousProcessInfo ) throws Exception;
ProcessInfo execute( String... command )
{
ProcessBuilder processBuilder = new ProcessBuilder( command );
processBuilder.redirectErrorStream( true );
Process process = null;
ProcessInfo processInfo = INVALID_PROCESS_INFO;
StringBuilder out = new StringBuilder( 64 );
try
{
if ( IS_OS_HP_UX ) // force to run shell commands in UNIX Standard mode on HP-UX
{
processBuilder.environment().put( "UNIX95", "1" );
}
process = processBuilder.start();
destroyableCommands.add( process );
Scanner scanner = new Scanner( process.getInputStream(), charset );
while ( scanner.hasNextLine() )
{
String line = scanner.nextLine();
out.append( line ).append( NL );
processInfo = consumeLine( line.trim(), processInfo );
}
checkValid( scanner );
int exitCode = process.waitFor();
if ( exitCode != 0 || isStopped() )
{
out.append( "<<exit>> <<" ).append( exitCode ).append( ">>" ).append( NL );
DumpErrorSingleton.getSingleton()
.dumpText( out.toString() );
}
return isStopped() ? ERR_PROCESS_INFO : exitCode == 0 ? processInfo : INVALID_PROCESS_INFO;
}
catch ( Exception e )
{
if ( !( e instanceof InterruptedException ) && !( e.getCause() instanceof InterruptedException ) )
{
DumpErrorSingleton.getSingleton()
.dumpText( out.toString() );
DumpErrorSingleton.getSingleton()
.dumpException( e );
}
return ERR_PROCESS_INFO;
}
finally
{
if ( process != null )
{
destroyableCommands.remove( process );
process.destroy();
closeQuietly( process.getInputStream() );
closeQuietly( process.getErrorStream() );
closeQuietly( process.getOutputStream() );
}
}
}
}
@Override
public String toString()
{
String args = "ppid=" + ppid
+ ", stopped=" + stopped;
ProcessInfo processInfo = parentProcessInfo;
if ( processInfo != null )
{
args += ", invalid=" + processInfo.isInvalid()
+ ", error=" + processInfo.isError();
}
if ( IS_OS_UNIX )
{
args += ", canExecuteLocalUnixPs=" + canExecuteLocalUnixPs()
+ ", canExecuteStandardUnixPs=" + canExecuteStandardUnixPs();
}
return "PpidChecker{" + args + '}';
}
}