8303392: Runtime.exec and ProcessBuilder.start should use System logger

Reviewed-by: stuefe, alanb, mullan
This commit is contained in:
Roger Riggs 2023-03-29 17:32:46 +00:00
parent be764a711c
commit d063b8964f
6 changed files with 272 additions and 0 deletions

View file

@ -190,6 +190,9 @@ import jdk.internal.event.ProcessStartEvent;
public final class ProcessBuilder
{
// Lazily and racy initialize when needed, racy is ok, any logger is ok
private static System.Logger LOGGER;
private List<String> command;
private File directory;
private Map<String,String> environment;
@ -1067,6 +1070,19 @@ public final class ProcessBuilder
*
* @throws IOException if an I/O error occurs
*
* @implNote
* In the reference implementation, logging of the command, arguments, directory,
* stack trace, and process id can be enabled.
* The logged information may contain sensitive security information and the potential exposure
* of the information should be carefully reviewed.
* Logging of the information is enabled when the logging level of the
* {@linkplain System#getLogger(String) system logger} named {@code java.lang.ProcessBuilder}
* is {@link System.Logger.Level#DEBUG Level.DEBUG} or {@link System.Logger.Level#TRACE Level.TRACE}.
* When enabled for {@code Level.DEBUG} only the process id, directory, command, and stack trace
* are logged.
* When enabled for {@code Level.TRACE} the arguments are included with the process id,
* directory, command, and stack trace.
*
* @see Runtime#exec(String[], String[], java.io.File)
*/
public Process start() throws IOException {
@ -1119,6 +1135,21 @@ public final class ProcessBuilder
event.pid = process.pid();
event.commit();
}
// Racy initialization for logging; errors in configuration may throw exceptions
System.Logger logger = LOGGER;
if (logger == null) {
LOGGER = logger = System.getLogger("java.lang.ProcessBuilder");
}
if (logger.isLoggable(System.Logger.Level.DEBUG)) {
boolean detail = logger.isLoggable(System.Logger.Level.TRACE);
var level = (detail) ? System.Logger.Level.TRACE : System.Logger.Level.DEBUG;
var cmdargs = (detail) ? String.join("\" \"", cmdarray) : cmdarray[0];
RuntimeException stackTraceEx = new RuntimeException("ProcessBuilder.start() debug");
LOGGER.log(level, "ProcessBuilder.start(): pid: " + process.pid() +
", dir: " + dir +
", cmd: \"" + cmdargs + "\"",
stackTraceEx);
}
return process;
} catch (IOException | IllegalArgumentException e) {
String exceptionInfo = ": " + e.getMessage();
@ -1263,6 +1294,10 @@ public final class ProcessBuilder
* @throws UnsupportedOperationException
* If the operating system does not support the creation of processes
*
* @implNote
* In the reference implementation, logging of each process created can be enabled,
* see {@link ProcessBuilder#start()} for details.
*
* @throws IOException if an I/O error occurs
* @since 9
*/

View file

@ -349,6 +349,10 @@ public class Runtime {
* @throws IllegalArgumentException
* If {@code command} is empty
*
* @implNote
* In the reference implementation, logging of the created process can be enabled,
* see {@link ProcessBuilder#start()} for details.
*
* @see #exec(String[], String[], File)
* @see ProcessBuilder
*/
@ -397,6 +401,10 @@ public class Runtime {
* @throws IllegalArgumentException
* If {@code command} is empty
*
* @implNote
* In the reference implementation, logging of the created process can be enabled,
* see {@link ProcessBuilder#start()} for details.
*
* @see #exec(String[], String[], File)
* @see ProcessBuilder
*/
@ -458,6 +466,10 @@ public class Runtime {
* @throws IllegalArgumentException
* If {@code command} is empty
*
* @implNote
* In the reference implementation, logging of the created process can be enabled,
* see {@link ProcessBuilder#start()} for details.
*
* @see ProcessBuilder
* @since 1.3
*/
@ -503,6 +515,10 @@ public class Runtime {
* If {@code cmdarray} is an empty array
* (has length {@code 0})
*
* @implNote
* In the reference implementation, logging of the created process can be enabled,
* see {@link ProcessBuilder#start()} for details.
*
* @see ProcessBuilder
*/
public Process exec(String[] cmdarray) throws IOException {
@ -546,6 +562,10 @@ public class Runtime {
* If {@code cmdarray} is an empty array
* (has length {@code 0})
*
* @implNote
* In the reference implementation, logging of the created process can be enabled,
* see {@link ProcessBuilder#start()} for details.
*
* @see ProcessBuilder
*/
public Process exec(String[] cmdarray, String[] envp) throws IOException {
@ -641,6 +661,10 @@ public class Runtime {
* If {@code cmdarray} is an empty array
* (has length {@code 0})
*
* @implNote
* In the reference implementation, logging of the created process can be enabled,
* see {@link ProcessBuilder#start()} for details.
*
* @see ProcessBuilder
* @since 1.3
*/