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

@ -1,5 +1,5 @@
# #
# Copyright (c) 2011, 2020, Oracle and/or its affiliates. All rights reserved. # Copyright (c) 2011, 2023, Oracle and/or its affiliates. All rights reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
# #
# This code is free software; you can redistribute it and/or modify it # This code is free software; you can redistribute it and/or modify it
@ -50,8 +50,14 @@ $(eval $(call SetupTextFileProcessing, BUILD_VERSION_JAVA, \
@@VENDOR_URL_VM_BUG@@ => $(VENDOR_URL_VM_BUG), \ @@VENDOR_URL_VM_BUG@@ => $(VENDOR_URL_VM_BUG), \
)) ))
TARGETS += $(BUILD_VERSION_JAVA) $(eval $(call SetupTextFileProcessing, BUILD_PLATFORMPROPERTIES_JAVA, \
SOURCE_FILES := $(TOPDIR)/src/java.base/share/classes/jdk/internal/util/OperatingSystemProps.java.template, \
OUTPUT_FILE := $(SUPPORT_OUTPUTDIR)/gensrc/java.base/jdk/internal/util/OperatingSystemProps.java, \
REPLACEMENTS := \
@@OPENJDK_TARGET_OS@@ => $(OPENJDK_TARGET_OS), \
))
TARGETS += $(BUILD_VERSION_JAVA) $(BUILD_PLATFORMPROPERTIES_JAVA)
################################################################################ ################################################################################
ifneq ($(filter $(TOOLCHAIN_TYPE), gcc clang), ) ifneq ($(filter $(TOOLCHAIN_TYPE), gcc clang), )

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -25,6 +25,7 @@
package java.lang; package java.lang;
import jdk.internal.util.OperatingSystem;
import java.io.File; import java.io.File;
import java.io.FileDescriptor; import java.io.FileDescriptor;
import java.io.IOException; import java.io.IOException;
@ -35,7 +36,6 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import jdk.internal.event.ProcessStartEvent; import jdk.internal.event.ProcessStartEvent;
import sun.security.action.GetPropertyAction;
/** /**
* This class is used to create operating system processes. * This class is used to create operating system processes.
@ -469,9 +469,8 @@ public final class ProcessBuilder
* @since 1.7 * @since 1.7
*/ */
public abstract static class Redirect { public abstract static class Redirect {
private static final File NULL_FILE = new File( private static final File NULL_FILE =
(GetPropertyAction.privilegedGetProperty("os.name") new File((OperatingSystem.isWindows() ? "NUL" : "/dev/null")
.startsWith("Windows") ? "NUL" : "/dev/null")
); );
/** /**

View file

@ -63,7 +63,7 @@ import java.util.stream.StreamSupport;
import jdk.internal.access.JavaUtilZipFileAccess; import jdk.internal.access.JavaUtilZipFileAccess;
import jdk.internal.access.JavaUtilJarAccess; import jdk.internal.access.JavaUtilJarAccess;
import jdk.internal.access.SharedSecrets; import jdk.internal.access.SharedSecrets;
import jdk.internal.misc.VM; import jdk.internal.util.OperatingSystem;
import jdk.internal.perf.PerfCounter; import jdk.internal.perf.PerfCounter;
import jdk.internal.ref.CleanerFactory; import jdk.internal.ref.CleanerFactory;
import jdk.internal.vm.annotation.Stable; import jdk.internal.vm.annotation.Stable;
@ -1085,8 +1085,6 @@ public class ZipFile implements ZipConstants, Closeable {
} }
} }
private static boolean isWindows;
static { static {
SharedSecrets.setJavaUtilZipFileAccess( SharedSecrets.setJavaUtilZipFileAccess(
new JavaUtilZipFileAccess() { new JavaUtilZipFileAccess() {
@ -1133,7 +1131,6 @@ public class ZipFile implements ZipConstants, Closeable {
} }
); );
isWindows = VM.getSavedProperty("os.name").contains("Windows");
} }
private static class Source { private static class Source {
@ -1321,7 +1318,7 @@ public class ZipFile implements ZipConstants, Closeable {
this.zc = zc; this.zc = zc;
this.key = key; this.key = key;
if (toDelete) { if (toDelete) {
if (isWindows) { if (OperatingSystem.isWindows()) {
this.zfile = SharedSecrets.getJavaIORandomAccessFileAccess() this.zfile = SharedSecrets.getJavaIORandomAccessFileAccess()
.openAndDelete(key.file, "r"); .openAndDelete(key.file, "r");
} else { } else {

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2020, 2022, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2020, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -25,8 +25,10 @@
*/ */
package jdk.internal.foreign; package jdk.internal.foreign;
import jdk.internal.util.OperatingSystem;
import jdk.internal.util.StaticProperty;
import static java.lang.foreign.ValueLayout.ADDRESS; import static java.lang.foreign.ValueLayout.ADDRESS;
import static sun.security.action.GetPropertyAction.privilegedGetProperty;
public enum CABI { public enum CABI {
SYS_V, SYS_V,
@ -38,32 +40,30 @@ public enum CABI {
private static final CABI ABI; private static final CABI ABI;
private static final String ARCH; private static final String ARCH;
private static final String OS;
private static final long ADDRESS_SIZE; private static final long ADDRESS_SIZE;
static { static {
ARCH = privilegedGetProperty("os.arch"); ARCH = StaticProperty.osArch();
OS = privilegedGetProperty("os.name");
ADDRESS_SIZE = ADDRESS.bitSize(); ADDRESS_SIZE = ADDRESS.bitSize();
// might be running in a 32-bit VM on a 64-bit platform. // might be running in a 32-bit VM on a 64-bit platform.
// addressSize will be correctly 32 // addressSize will be correctly 32
if ((ARCH.equals("amd64") || ARCH.equals("x86_64")) && ADDRESS_SIZE == 64) { if ((ARCH.equals("amd64") || ARCH.equals("x86_64")) && ADDRESS_SIZE == 64) {
if (OS.startsWith("Windows")) { if (OperatingSystem.isWindows()) {
ABI = WIN_64; ABI = WIN_64;
} else { } else {
ABI = SYS_V; ABI = SYS_V;
} }
} else if (ARCH.equals("aarch64")) { } else if (ARCH.equals("aarch64")) {
if (OS.startsWith("Mac")) { if (OperatingSystem.isMacOS()) {
ABI = MAC_OS_AARCH_64; ABI = MAC_OS_AARCH_64;
} else if (OS.startsWith("Windows")) { } else if (OperatingSystem.isWindows()) {
ABI = WIN_AARCH_64; ABI = WIN_AARCH_64;
} else { } else {
// The Linux ABI follows the standard AAPCS ABI // The Linux ABI follows the standard AAPCS ABI
ABI = LINUX_AARCH_64; ABI = LINUX_AARCH_64;
} }
} else if (ARCH.equals("riscv64")) { } else if (ARCH.equals("riscv64")) {
if (OS.startsWith("Linux")) { if (OperatingSystem.isLinux()) {
ABI = LINUX_RISCV_64; ABI = LINUX_RISCV_64;
} else { } else {
// unsupported // unsupported
@ -78,7 +78,8 @@ public enum CABI {
public static CABI current() { public static CABI current() {
if (ABI == null) { if (ABI == null) {
throw new UnsupportedOperationException( throw new UnsupportedOperationException(
"Unsupported os, arch, or address size: " + OS + ", " + ARCH + ", " + ADDRESS_SIZE); "Unsupported os, arch, or address size: " + OperatingSystem.current() +
", " + ARCH + ", " + ADDRESS_SIZE);
} }
return ABI; return ABI;
} }

View file

@ -0,0 +1,124 @@
/*
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.internal.util;
import jdk.internal.util.OperatingSystemProps;
import jdk.internal.vm.annotation.ForceInline;
/**
* Enumeration of operating system types and testing for the current OS.
* The enumeration can be used to dispatch to OS specific code or values.
* Checking if a specific operating system is current uses a simple
* static method for each operating system.
* <p>
* For example,
* {@snippet lang = "java":
* if (OperatingSystem.isWindows()) {
* // Windows only code.
* } else if (OperatingSystem.isLinux()) {
* // Linux only code
* }
*}
*
* Alternatively, compare with the {@linkplain #current() current} operating system.
* For example,
* {@snippet lang = "java":
* if (OperatingSystem.current() == OperatingSystem.WINDOWS) {
* // Windows only code.
* }
*}
* Dispatch based on the current operating system or choose a value.
* For example,
* {@snippet lang = "java":
* int port() {
* return switch(OperatingSystem.current()) {
* case LINUX->32768;
* case AIX->32768;
* case MACOS->49152;
* case WINDOWS->49152;
* };
* }
*}
*/
public enum OperatingSystem {
/**
* Operating systems based on the Linux kernel.
*/
LINUX,
/**
* The Mac OS X Operating system.
*/
MACOS,
/**
* The Windows Operating system.
*/
WINDOWS,
/**
* The AIX Operating system.
*/
AIX,
;
// Cache a copy of the array for lightweight indexing
private static final OperatingSystem[] osValues = OperatingSystem.values();
/**
* {@return {@code true} if built for the Linux operating system}
*/
@ForceInline
public static boolean isLinux() {
return OperatingSystemProps.TARGET_OS_IS_LINUX;
}
/**
* {@return {@code true} if built for the Mac OS X operating system}
*/
@ForceInline
public static boolean isMacOS() {
return OperatingSystemProps.TARGET_OS_IS_MACOSX;
}
/**
* {@return {@code true} if built for the Windows operating system}
*/
@ForceInline
public static boolean isWindows() {
return OperatingSystemProps.TARGET_OS_IS_WINDOWS;
}
/**
* {@return {@code true} if built for the AIX operating system}
*/
@ForceInline
public static boolean isAix() {
return OperatingSystemProps.TARGET_OS_IS_AIX;
}
/**
* {@return the current operating system}
*/
public static OperatingSystem current() {
return osValues[OperatingSystemProps.CURRENT_OS_ORDINAL];
}
}

View file

@ -0,0 +1,46 @@
/*
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.internal.util;
/**
* The corresponding source file is generated by GensrcMisc.gmk for java.base.
* @see OperatingSystem
*/
class OperatingSystemProps {
// Unique integers named to match the build system naming of the build target
// The values must match the ordinals of the respective enum
private static final int TARGET_OS_linux = 0;
private static final int TARGET_OS_macosx = 1;
private static final int TARGET_OS_windows = 2;
private static final int TARGET_OS_aix = 3;
// Index/ordinal of the current OperatingSystem enum as substituted by the build
static final int CURRENT_OS_ORDINAL = TARGET_OS_@@OPENJDK_TARGET_OS@@;
// Precomputed booleans for each Operating System
static final boolean TARGET_OS_IS_LINUX = TARGET_OS_@@OPENJDK_TARGET_OS@@ == TARGET_OS_linux;
static final boolean TARGET_OS_IS_MACOSX = TARGET_OS_@@OPENJDK_TARGET_OS@@ == TARGET_OS_macosx;
static final boolean TARGET_OS_IS_WINDOWS = TARGET_OS_@@OPENJDK_TARGET_OS@@ == TARGET_OS_windows;
static final boolean TARGET_OS_IS_AIX = TARGET_OS_@@OPENJDK_TARGET_OS@@ == TARGET_OS_aix;
}

View file

@ -54,6 +54,8 @@ public final class StaticProperty {
private static final String JAVA_PROPERTIES_DATE; private static final String JAVA_PROPERTIES_DATE;
private static final String SUN_JNU_ENCODING; private static final String SUN_JNU_ENCODING;
private static final String JAVA_LOCALE_USE_OLD_ISO_CODES; private static final String JAVA_LOCALE_USE_OLD_ISO_CODES;
private static final String OS_NAME;
private static final String OS_ARCH;
private StaticProperty() {} private StaticProperty() {}
@ -73,6 +75,8 @@ public final class StaticProperty {
JAVA_PROPERTIES_DATE = getProperty(props, "java.properties.date", null); JAVA_PROPERTIES_DATE = getProperty(props, "java.properties.date", null);
SUN_JNU_ENCODING = getProperty(props, "sun.jnu.encoding"); SUN_JNU_ENCODING = getProperty(props, "sun.jnu.encoding");
JAVA_LOCALE_USE_OLD_ISO_CODES = getProperty(props, "java.locale.useOldISOCodes", ""); JAVA_LOCALE_USE_OLD_ISO_CODES = getProperty(props, "java.locale.useOldISOCodes", "");
OS_NAME = getProperty(props, "os.name");
OS_ARCH = getProperty(props, "os.arch");
} }
private static String getProperty(Properties props, String key) { private static String getProperty(Properties props, String key) {
@ -243,4 +247,22 @@ public final class StaticProperty {
public static String javaLocaleUseOldISOCodes() { public static String javaLocaleUseOldISOCodes() {
return JAVA_LOCALE_USE_OLD_ISO_CODES; return JAVA_LOCALE_USE_OLD_ISO_CODES;
} }
/**
* {@return the {@code os.name} system property}
* <strong>{@link SecurityManager#checkPropertyAccess} is NOT checked
* in this method. This property is not considered security sensitive.</strong>
*/
public static String osName() {
return OS_NAME;
}
/**
* {@return the {@code os.arch} system property}
* <strong>{@link SecurityManager#checkPropertyAccess} is NOT checked
* in this method. This property is not considered security sensitive.</strong>
*/
public static String osArch() {
return OS_ARCH;
}
} }

View file

@ -77,6 +77,7 @@ import java.util.jar.Manifest;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
import jdk.internal.util.OperatingSystem;
import jdk.internal.misc.VM; import jdk.internal.misc.VM;
import jdk.internal.module.ModuleBootstrap; import jdk.internal.module.ModuleBootstrap;
import jdk.internal.module.Modules; import jdk.internal.module.Modules;
@ -168,7 +169,7 @@ public final class LauncherHelper {
printLocale(); printLocale();
break; break;
case "system": case "system":
if (System.getProperty("os.name").contains("Linux")) { if (OperatingSystem.isLinux()) {
printSystemMetrics(); printSystemMetrics();
break; break;
} }
@ -176,7 +177,7 @@ public final class LauncherHelper {
printVmSettings(initialHeapSize, maxHeapSize, stackSize); printVmSettings(initialHeapSize, maxHeapSize, stackSize);
printProperties(); printProperties();
printLocale(); printLocale();
if (System.getProperty("os.name").contains("Linux")) { if (OperatingSystem.isLinux()) {
printSystemMetrics(); printSystemMetrics();
} }
break; break;
@ -532,7 +533,7 @@ public final class LauncherHelper {
initOutput(printToStderr); initOutput(printToStderr);
ostream.println(getLocalizedMessage("java.launcher.X.usage", ostream.println(getLocalizedMessage("java.launcher.X.usage",
File.pathSeparator)); File.pathSeparator));
if (System.getProperty("os.name").contains("OS X")) { if (OperatingSystem.isMacOS()) {
ostream.println(getLocalizedMessage("java.launcher.X.macosx.usage", ostream.println(getLocalizedMessage("java.launcher.X.macosx.usage",
File.pathSeparator)); File.pathSeparator));
} }
@ -745,7 +746,7 @@ public final class LauncherHelper {
Class<?> c = null; Class<?> c = null;
try { try {
c = Class.forName(m, mainClass); c = Class.forName(m, mainClass);
if (c == null && System.getProperty("os.name", "").contains("OS X") if (c == null && OperatingSystem.isMacOS()
&& Normalizer.isNormalized(mainClass, Normalizer.Form.NFD)) { && Normalizer.isNormalized(mainClass, Normalizer.Form.NFD)) {
String cn = Normalizer.normalize(mainClass, Normalizer.Form.NFC); String cn = Normalizer.normalize(mainClass, Normalizer.Form.NFC);
@ -789,7 +790,7 @@ public final class LauncherHelper {
try { try {
mainClass = Class.forName(cn, false, scl); mainClass = Class.forName(cn, false, scl);
} catch (NoClassDefFoundError | ClassNotFoundException cnfe) { } catch (NoClassDefFoundError | ClassNotFoundException cnfe) {
if (System.getProperty("os.name", "").contains("OS X") if (OperatingSystem.isMacOS()
&& Normalizer.isNormalized(cn, Normalizer.Form.NFD)) { && Normalizer.isNormalized(cn, Normalizer.Form.NFD)) {
try { try {
// On Mac OS X since all names with diacritical marks are // On Mac OS X since all names with diacritical marks are

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2010, 2020, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2010, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -30,7 +30,7 @@ import java.io.FileDescriptor;
import jdk.internal.access.SharedSecrets; import jdk.internal.access.SharedSecrets;
import jdk.internal.access.JavaIOFileDescriptorAccess; import jdk.internal.access.JavaIOFileDescriptorAccess;
import sun.security.action.GetPropertyAction; import jdk.internal.util.OperatingSystem;
/** /**
@ -39,8 +39,7 @@ import sun.security.action.GetPropertyAction;
*/ */
public final class SdpSupport { public final class SdpSupport {
private static final String os = GetPropertyAction.privilegedGetProperty("os.name"); private static final boolean isSupported = OperatingSystem.isLinux();
private static final boolean isSupported = os.equals("Linux");
private static final JavaIOFileDescriptorAccess fdAccess = private static final JavaIOFileDescriptorAccess fdAccess =
SharedSecrets.getJavaIOFileDescriptorAccess(); SharedSecrets.getJavaIOFileDescriptorAccess();

View file

@ -37,19 +37,17 @@ import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.Arrays; import java.util.Arrays;
import java.util.EnumSet;
import java.util.Locale; import java.util.Locale;
import java.util.Set;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock; import java.util.concurrent.locks.ReentrantLock;
import java.security.AccessController; import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.PrivilegedActionException; import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction; import java.security.PrivilegedExceptionAction;
import jdk.internal.access.JavaIOFileDescriptorAccess; import jdk.internal.access.JavaIOFileDescriptorAccess;
import jdk.internal.access.SharedSecrets; import jdk.internal.access.SharedSecrets;
import jdk.internal.util.OperatingSystem;
import jdk.internal.util.StaticProperty; import jdk.internal.util.StaticProperty;
import sun.security.action.GetPropertyAction; import sun.security.action.GetPropertyAction;
@ -92,65 +90,39 @@ final class ProcessImpl extends Process {
VFORK VFORK
} }
private static enum Platform { /**
* {@return the default or requested launch mechanism}
LINUX(LaunchMechanism.POSIX_SPAWN, LaunchMechanism.VFORK, LaunchMechanism.FORK), * @throws Error if the requested launch mechanism is not found or valid
*/
BSD(LaunchMechanism.POSIX_SPAWN, LaunchMechanism.FORK), private static LaunchMechanism launchMechanism() {
String s = GetPropertyAction.privilegedGetProperty("jdk.lang.Process.launchMechanism");
AIX(LaunchMechanism.POSIX_SPAWN, LaunchMechanism.FORK); if (s == null) {
return LaunchMechanism.POSIX_SPAWN;
final LaunchMechanism defaultLaunchMechanism;
final Set<LaunchMechanism> validLaunchMechanisms;
Platform(LaunchMechanism ... launchMechanisms) {
this.defaultLaunchMechanism = launchMechanisms[0];
this.validLaunchMechanisms =
EnumSet.copyOf(Arrays.asList(launchMechanisms));
} }
@SuppressWarnings("removal") try {
LaunchMechanism launchMechanism() { // Should be value of a LaunchMechanism enum
return AccessController.doPrivileged( LaunchMechanism lm = LaunchMechanism.valueOf(s.toUpperCase(Locale.ROOT));
(PrivilegedAction<LaunchMechanism>) () -> { switch (OperatingSystem.current()) {
String s = System.getProperty( case LINUX:
"jdk.lang.Process.launchMechanism"); return lm; // All options are valid for Linux
LaunchMechanism lm; case AIX:
if (s == null) { case MACOS:
lm = defaultLaunchMechanism; if (lm != LaunchMechanism.VFORK) {
s = lm.name().toLowerCase(Locale.ROOT); return lm; // All but VFORK are valid
} else {
try {
lm = LaunchMechanism.valueOf(
s.toUpperCase(Locale.ROOT));
} catch (IllegalArgumentException e) {
lm = null;
}
} }
if (lm == null || !validLaunchMechanisms.contains(lm)) { break;
throw new Error( case WINDOWS:
s + " is not a supported " + // fall through to throw to Error
"process launch mechanism on this platform." }
); } catch (IllegalArgumentException e) {
}
return lm;
}
);
} }
static Platform get() { throw new Error(s + " is not a supported " +
String osName = GetPropertyAction.privilegedGetProperty("os.name"); "process launch mechanism on this platform: " + OperatingSystem.current());
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.");
}
} }
private static final Platform platform = Platform.get(); private static final LaunchMechanism launchMechanism = launchMechanism();
private static final LaunchMechanism launchMechanism = platform.launchMechanism();
private static final byte[] helperpath = toCString(StaticProperty.javaHome() + "/lib/jspawnhelper"); private static final byte[] helperpath = toCString(StaticProperty.javaHome() + "/lib/jspawnhelper");
private static byte[] toCString(String s) { private static byte[] toCString(String s) {
@ -354,9 +326,9 @@ final class ProcessImpl extends Process {
* @throws IOException * @throws IOException
*/ */
void initStreams(int[] fds, boolean forceNullOutputStream) throws IOException { void initStreams(int[] fds, boolean forceNullOutputStream) throws IOException {
switch (platform) { switch (OperatingSystem.current()) {
case LINUX: case LINUX:
case BSD: case MACOS:
stdin = (fds[0] == -1) ? stdin = (fds[0] == -1) ?
ProcessBuilder.NullOutputStream.INSTANCE : ProcessBuilder.NullOutputStream.INSTANCE :
new ProcessPipeOutputStream(fds[0]); new ProcessPipeOutputStream(fds[0]);
@ -428,7 +400,9 @@ final class ProcessImpl extends Process {
}); });
break; 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) { private void destroy(boolean force) {
switch (platform) { switch (OperatingSystem.current()) {
case LINUX: case LINUX:
case BSD: case MACOS:
case AIX: case AIX:
// There is a risk that pid will be recycled, causing us to // There is a risk that pid will be recycled, causing us to
// kill the wrong process! So we only terminate processes // kill the wrong process! So we only terminate processes
@ -506,7 +480,7 @@ final class ProcessImpl extends Process {
try { stderr.close(); } catch (IOException ignored) {} try { stderr.close(); } catch (IOException ignored) {}
break; 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; package sun.net;
import sun.security.action.GetPropertyAction; import jdk.internal.util.OperatingSystem;
/** /**
* Determines the ephemeral port range in use on this system. * Determines the ephemeral port range in use on this system.
@ -42,24 +42,26 @@ public final class PortConfig {
static { static {
jdk.internal.loader.BootLoader.loadLibrary("net"); jdk.internal.loader.BootLoader.loadLibrary("net");
String os = GetPropertyAction.privilegedGetProperty("os.name"); switch (OperatingSystem.current()) {
if (os.startsWith("Linux")) { case LINUX:
defaultLower = 32768; defaultLower = 32768;
defaultUpper = 61000; defaultUpper = 61000;
} else if (os.contains("OS X")) { break;
defaultLower = 49152; case MACOS:
defaultUpper = 65535; defaultLower = 49152;
} else if (os.startsWith("AIX")) { defaultUpper = 65535;
// The ephemeral port is OS version dependent on AIX: break;
// http://publib.boulder.ibm.com/infocenter/aix/v7r1/topic/com.ibm.aix.rsct315.admin/bl503_ephport.htm case AIX:
// However, on AIX 5.3 / 6.1 / 7.1 we always see the // The ephemeral port is OS version dependent on AIX:
// settings below by using: // http://publib.boulder.ibm.com/infocenter/aix/v7r1/topic/com.ibm.aix.rsct315.admin/bl503_ephport.htm
// /usr/sbin/no -a | fgrep ephemeral // However, on AIX 5.3 / 6.1 / 7.1 we always see the
defaultLower = 32768; // settings below by using:
defaultUpper = 65535; // /usr/sbin/no -a | fgrep ephemeral
} else { defaultLower = 32768;
throw new InternalError( defaultUpper = 65535;
"sun.net.PortConfig: unknown OS"); break;
default:
throw new InternalError("sun.net.PortConfig: unsupported OS: " + OperatingSystem.current());
} }
int v = getLower0(); int v = getLower0();

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2022, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -24,8 +24,8 @@
/* /*
* @test * @test
* @enablePreview * @enablePreview
* * @summary test with unknown architecture override
* @run testng/othervm -Dos.arch=unknown -Dos.name=unknown --enable-native-access=ALL-UNNAMED TestUnsupportedLinker * @run testng/othervm -Dos.arch=unknown --enable-native-access=ALL-UNNAMED TestUnsupportedLinker
*/ */
import java.lang.foreign.Linker; import java.lang.foreign.Linker;

View file

@ -0,0 +1,86 @@
/*
* Copyright (c) 2022, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.util.Locale;
import java.util.stream.Stream;
import jdk.internal.util.OperatingSystem;
import static jdk.internal.util.OperatingSystem.AIX;
import static jdk.internal.util.OperatingSystem.LINUX;
import static jdk.internal.util.OperatingSystem.MACOS;
import static jdk.internal.util.OperatingSystem.WINDOWS;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
/**
* @test
* @summary test OperatingSystem enum
* @modules java.base/jdk.internal.util
* @run junit OSTest
*/
public class OSTest {
/**
* Test consistency of System property "os.name" with OperatingSystem.current().
*/
@Test
public void os_nameVsCurrent() {
String osName = System.getProperty("os.name").substring(0, 3).toLowerCase(Locale.ROOT);
OperatingSystem os = switch (osName) {
case "win" -> WINDOWS;
case "lin" -> LINUX;
case "mac" -> MACOS;
case "aix" -> AIX;
default -> fail("Unknown os.name: " + osName);
};
assertEquals(OperatingSystem.current(), os, "mismatch in OperatingSystem.current vs " + osName);
}
/**
* Test various OperatingSystem enum values vs boolean isXXX() methods.
* @return a stream of arguments for parameterized test
*/
private static Stream<Arguments> osParams() {
return Stream.of(
Arguments.of(LINUX, OperatingSystem.isLinux()),
Arguments.of(WINDOWS, OperatingSystem.isWindows()),
Arguments.of(MACOS, OperatingSystem.isMacOS()),
Arguments.of(AIX, OperatingSystem.isAix())
);
}
@ParameterizedTest
@MethodSource("osParams")
public void isXXX(OperatingSystem os, boolean isXXX) {
OperatingSystem current = OperatingSystem.current();
assertEquals(os == current, isXXX,
"Mismatch " + os + " == " + current + " vs is" + os);
}
}