8303485: Replacing os.name for operating system customization

Reviewed-by: naoto, erikj, alanb
This commit is contained in:
Roger Riggs 2023-03-27 17:45:20 +00:00
parent 87b314a985
commit 6c3b10fb1d
13 changed files with 371 additions and 114 deletions

View file

@ -37,19 +37,17 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.Locale;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import jdk.internal.access.JavaIOFileDescriptorAccess;
import jdk.internal.access.SharedSecrets;
import jdk.internal.util.OperatingSystem;
import jdk.internal.util.StaticProperty;
import sun.security.action.GetPropertyAction;
@ -92,65 +90,39 @@ final class ProcessImpl extends Process {
VFORK
}
private static enum Platform {
LINUX(LaunchMechanism.POSIX_SPAWN, LaunchMechanism.VFORK, LaunchMechanism.FORK),
BSD(LaunchMechanism.POSIX_SPAWN, LaunchMechanism.FORK),
AIX(LaunchMechanism.POSIX_SPAWN, LaunchMechanism.FORK);
final LaunchMechanism defaultLaunchMechanism;
final Set<LaunchMechanism> validLaunchMechanisms;
Platform(LaunchMechanism ... launchMechanisms) {
this.defaultLaunchMechanism = launchMechanisms[0];
this.validLaunchMechanisms =
EnumSet.copyOf(Arrays.asList(launchMechanisms));
/**
* {@return the default or requested launch mechanism}
* @throws Error if the requested launch mechanism is not found or valid
*/
private static LaunchMechanism launchMechanism() {
String s = GetPropertyAction.privilegedGetProperty("jdk.lang.Process.launchMechanism");
if (s == null) {
return LaunchMechanism.POSIX_SPAWN;
}
@SuppressWarnings("removal")
LaunchMechanism launchMechanism() {
return AccessController.doPrivileged(
(PrivilegedAction<LaunchMechanism>) () -> {
String s = System.getProperty(
"jdk.lang.Process.launchMechanism");
LaunchMechanism lm;
if (s == null) {
lm = defaultLaunchMechanism;
s = lm.name().toLowerCase(Locale.ROOT);
} else {
try {
lm = LaunchMechanism.valueOf(
s.toUpperCase(Locale.ROOT));
} catch (IllegalArgumentException e) {
lm = null;
}
try {
// Should be value of a LaunchMechanism enum
LaunchMechanism lm = LaunchMechanism.valueOf(s.toUpperCase(Locale.ROOT));
switch (OperatingSystem.current()) {
case LINUX:
return lm; // All options are valid for Linux
case AIX:
case MACOS:
if (lm != LaunchMechanism.VFORK) {
return lm; // All but VFORK are valid
}
if (lm == null || !validLaunchMechanisms.contains(lm)) {
throw new Error(
s + " is not a supported " +
"process launch mechanism on this platform."
);
}
return lm;
}
);
break;
case WINDOWS:
// fall through to throw to Error
}
} catch (IllegalArgumentException e) {
}
static Platform get() {
String osName = GetPropertyAction.privilegedGetProperty("os.name");
if (osName.equals("Linux")) { return LINUX; }
if (osName.contains("OS X")) { return BSD; }
if (osName.equals("AIX")) { return AIX; }
throw new Error(osName + " is not a supported OS platform.");
}
throw new Error(s + " is not a supported " +
"process launch mechanism on this platform: " + OperatingSystem.current());
}
private static final Platform platform = Platform.get();
private static final LaunchMechanism launchMechanism = platform.launchMechanism();
private static final LaunchMechanism launchMechanism = launchMechanism();
private static final byte[] helperpath = toCString(StaticProperty.javaHome() + "/lib/jspawnhelper");
private static byte[] toCString(String s) {
@ -354,9 +326,9 @@ final class ProcessImpl extends Process {
* @throws IOException
*/
void initStreams(int[] fds, boolean forceNullOutputStream) throws IOException {
switch (platform) {
switch (OperatingSystem.current()) {
case LINUX:
case BSD:
case MACOS:
stdin = (fds[0] == -1) ?
ProcessBuilder.NullOutputStream.INSTANCE :
new ProcessPipeOutputStream(fds[0]);
@ -428,7 +400,9 @@ final class ProcessImpl extends Process {
});
break;
default: throw new AssertionError("Unsupported platform: " + platform);
default:
throw new AssertionError("Unsupported platform: " +
OperatingSystem.current());
}
}
@ -484,9 +458,9 @@ final class ProcessImpl extends Process {
}
private void destroy(boolean force) {
switch (platform) {
switch (OperatingSystem.current()) {
case LINUX:
case BSD:
case MACOS:
case AIX:
// There is a risk that pid will be recycled, causing us to
// kill the wrong process! So we only terminate processes
@ -506,7 +480,7 @@ final class ProcessImpl extends Process {
try { stderr.close(); } catch (IOException ignored) {}
break;
default: throw new AssertionError("Unsupported platform: " + platform);
default: throw new AssertionError("Unsupported platform: " + OperatingSystem.current());
}
}

View file

@ -25,7 +25,7 @@
package sun.net;
import sun.security.action.GetPropertyAction;
import jdk.internal.util.OperatingSystem;
/**
* Determines the ephemeral port range in use on this system.
@ -42,24 +42,26 @@ public final class PortConfig {
static {
jdk.internal.loader.BootLoader.loadLibrary("net");
String os = GetPropertyAction.privilegedGetProperty("os.name");
if (os.startsWith("Linux")) {
defaultLower = 32768;
defaultUpper = 61000;
} else if (os.contains("OS X")) {
defaultLower = 49152;
defaultUpper = 65535;
} else if (os.startsWith("AIX")) {
// The ephemeral port is OS version dependent on AIX:
// http://publib.boulder.ibm.com/infocenter/aix/v7r1/topic/com.ibm.aix.rsct315.admin/bl503_ephport.htm
// However, on AIX 5.3 / 6.1 / 7.1 we always see the
// settings below by using:
// /usr/sbin/no -a | fgrep ephemeral
defaultLower = 32768;
defaultUpper = 65535;
} else {
throw new InternalError(
"sun.net.PortConfig: unknown OS");
switch (OperatingSystem.current()) {
case LINUX:
defaultLower = 32768;
defaultUpper = 61000;
break;
case MACOS:
defaultLower = 49152;
defaultUpper = 65535;
break;
case AIX:
// The ephemeral port is OS version dependent on AIX:
// http://publib.boulder.ibm.com/infocenter/aix/v7r1/topic/com.ibm.aix.rsct315.admin/bl503_ephport.htm
// However, on AIX 5.3 / 6.1 / 7.1 we always see the
// settings below by using:
// /usr/sbin/no -a | fgrep ephemeral
defaultLower = 32768;
defaultUpper = 65535;
break;
default:
throw new InternalError("sun.net.PortConfig: unsupported OS: " + OperatingSystem.current());
}
int v = getLower0();