< prev index next >

src/java.base/unix/classes/java/lang/ProcessImpl.java

Print this page
rev 14210 : 8154231: Simplify access to System properties from JDK code
Reviewed-by: rriggs


  29 import java.io.BufferedInputStream;
  30 import java.io.BufferedOutputStream;
  31 import java.io.ByteArrayInputStream;
  32 import java.io.FileDescriptor;
  33 import java.io.FileInputStream;
  34 import java.io.FileOutputStream;
  35 import java.io.IOException;
  36 import java.io.InputStream;
  37 import java.io.OutputStream;
  38 import java.util.Arrays;
  39 import java.util.EnumSet;
  40 import java.util.Locale;
  41 import java.util.Set;
  42 import java.util.concurrent.CompletableFuture;
  43 import java.util.concurrent.TimeUnit;
  44 import java.security.AccessController;
  45 import static java.security.AccessController.doPrivileged;
  46 import java.security.PrivilegedAction;
  47 import java.security.PrivilegedActionException;
  48 import java.security.PrivilegedExceptionAction;

  49 import jdk.internal.misc.JavaIOFileDescriptorAccess;
  50 import jdk.internal.misc.SharedSecrets;

  51 
  52 /**
  53  * java.lang.Process subclass in the UNIX environment.
  54  *
  55  * @author Mario Wolczko and Ross Knippel.
  56  * @author Konstantin Kladko (ported to Linux and Bsd)
  57  * @author Martin Buchholz
  58  * @author Volker Simonis (ported to AIX)
  59  * @since   1.5
  60  */
  61 final class ProcessImpl extends Process {
  62     private static final JavaIOFileDescriptorAccess fdAccess
  63         = SharedSecrets.getJavaIOFileDescriptorAccess();
  64 
  65     // Linux platforms support a normal (non-forcible) kill signal.
  66     static final boolean SUPPORTS_NORMAL_TERMINATION = true;
  67 
  68     private final int pid;
  69     private final ProcessHandleImpl processHandle;
  70     private int exitcode;


 106         @SuppressWarnings("fallthrough")
 107         private String helperPath(String javahome, String osArch) {
 108             switch (this) {
 109                 case SOLARIS:
 110                     if (osArch.equals("x86")) { osArch = "i386"; }
 111                     else if (osArch.equals("x86_64")) { osArch = "amd64"; }
 112                     // fall through...
 113                 case LINUX:
 114                 case AIX:
 115                     return javahome + "/lib/" + osArch + "/jspawnhelper";
 116 
 117                 case BSD:
 118                     return javahome + "/lib/jspawnhelper";
 119 
 120                 default:
 121                     throw new AssertionError("Unsupported platform: " + this);
 122             }
 123         }
 124 
 125         String helperPath() {
 126             return AccessController.doPrivileged(
 127                 (PrivilegedAction<String>) () ->
 128                     helperPath(System.getProperty("java.home"),
 129                                System.getProperty("os.arch"))
 130             );
 131         }
 132 
 133         LaunchMechanism launchMechanism() {
 134             return AccessController.doPrivileged(
 135                 (PrivilegedAction<LaunchMechanism>) () -> {
 136                     String s = System.getProperty(
 137                         "jdk.lang.Process.launchMechanism");
 138                     LaunchMechanism lm;
 139                     if (s == null) {
 140                         lm = defaultLaunchMechanism;
 141                         s = lm.name().toLowerCase(Locale.ENGLISH);
 142                     } else {
 143                         try {
 144                             lm = LaunchMechanism.valueOf(
 145                                 s.toUpperCase(Locale.ENGLISH));
 146                         } catch (IllegalArgumentException e) {
 147                             lm = null;
 148                         }
 149                     }
 150                     if (lm == null || !validLaunchMechanisms.contains(lm)) {
 151                         throw new Error(
 152                             s + " is not a supported " +
 153                             "process launch mechanism on this platform."
 154                         );
 155                     }
 156                     return lm;
 157                 }
 158             );
 159         }
 160 
 161         static Platform get() {
 162             String osName = AccessController.doPrivileged(
 163                 (PrivilegedAction<String>) () -> System.getProperty("os.name")
 164             );
 165 
 166             if (osName.equals("Linux")) { return LINUX; }
 167             if (osName.contains("OS X")) { return BSD; }
 168             if (osName.equals("SunOS")) { return SOLARIS; }
 169             if (osName.equals("AIX")) { return AIX; }
 170 
 171             throw new Error(osName + " is not a supported OS platform.");
 172         }
 173     }
 174 
 175     private static final Platform platform = Platform.get();
 176     private static final LaunchMechanism launchMechanism = platform.launchMechanism();
 177     private static final byte[] helperpath = toCString(platform.helperPath());
 178 
 179     private static byte[] toCString(String s) {
 180         if (s == null)
 181             return null;
 182         byte[] bytes = s.getBytes();
 183         byte[] result = new byte[bytes.length + 1];
 184         System.arraycopy(bytes, 0,




  29 import java.io.BufferedInputStream;
  30 import java.io.BufferedOutputStream;
  31 import java.io.ByteArrayInputStream;
  32 import java.io.FileDescriptor;
  33 import java.io.FileInputStream;
  34 import java.io.FileOutputStream;
  35 import java.io.IOException;
  36 import java.io.InputStream;
  37 import java.io.OutputStream;
  38 import java.util.Arrays;
  39 import java.util.EnumSet;
  40 import java.util.Locale;
  41 import java.util.Set;
  42 import java.util.concurrent.CompletableFuture;
  43 import java.util.concurrent.TimeUnit;
  44 import java.security.AccessController;
  45 import static java.security.AccessController.doPrivileged;
  46 import java.security.PrivilegedAction;
  47 import java.security.PrivilegedActionException;
  48 import java.security.PrivilegedExceptionAction;
  49 import java.util.Properties;
  50 import jdk.internal.misc.JavaIOFileDescriptorAccess;
  51 import jdk.internal.misc.SharedSecrets;
  52 import sun.security.action.GetPropertyAction;
  53 
  54 /**
  55  * java.lang.Process subclass in the UNIX environment.
  56  *
  57  * @author Mario Wolczko and Ross Knippel.
  58  * @author Konstantin Kladko (ported to Linux and Bsd)
  59  * @author Martin Buchholz
  60  * @author Volker Simonis (ported to AIX)
  61  * @since   1.5
  62  */
  63 final class ProcessImpl extends Process {
  64     private static final JavaIOFileDescriptorAccess fdAccess
  65         = SharedSecrets.getJavaIOFileDescriptorAccess();
  66 
  67     // Linux platforms support a normal (non-forcible) kill signal.
  68     static final boolean SUPPORTS_NORMAL_TERMINATION = true;
  69 
  70     private final int pid;
  71     private final ProcessHandleImpl processHandle;
  72     private int exitcode;


 108         @SuppressWarnings("fallthrough")
 109         private String helperPath(String javahome, String osArch) {
 110             switch (this) {
 111                 case SOLARIS:
 112                     if (osArch.equals("x86")) { osArch = "i386"; }
 113                     else if (osArch.equals("x86_64")) { osArch = "amd64"; }
 114                     // fall through...
 115                 case LINUX:
 116                 case AIX:
 117                     return javahome + "/lib/" + osArch + "/jspawnhelper";
 118 
 119                 case BSD:
 120                     return javahome + "/lib/jspawnhelper";
 121 
 122                 default:
 123                     throw new AssertionError("Unsupported platform: " + this);
 124             }
 125         }
 126 
 127         String helperPath() {
 128             Properties props = GetPropertyAction.getProperties();
 129             return helperPath(props.getProperty("java.home"),
 130                               props.getProperty("os.arch"));


 131         }
 132 
 133         LaunchMechanism launchMechanism() {
 134             return AccessController.doPrivileged(
 135                 (PrivilegedAction<LaunchMechanism>) () -> {
 136                     String s = System.getProperty(
 137                         "jdk.lang.Process.launchMechanism");
 138                     LaunchMechanism lm;
 139                     if (s == null) {
 140                         lm = defaultLaunchMechanism;
 141                         s = lm.name().toLowerCase(Locale.ENGLISH);
 142                     } else {
 143                         try {
 144                             lm = LaunchMechanism.valueOf(
 145                                 s.toUpperCase(Locale.ENGLISH));
 146                         } catch (IllegalArgumentException e) {
 147                             lm = null;
 148                         }
 149                     }
 150                     if (lm == null || !validLaunchMechanisms.contains(lm)) {
 151                         throw new Error(
 152                             s + " is not a supported " +
 153                             "process launch mechanism on this platform."
 154                         );
 155                     }
 156                     return lm;
 157                 }
 158             );
 159         }
 160 
 161         static Platform get() {
 162             String osName = GetPropertyAction.getProperty("os.name");


 163 
 164             if (osName.equals("Linux")) { return LINUX; }
 165             if (osName.contains("OS X")) { return BSD; }
 166             if (osName.equals("SunOS")) { return SOLARIS; }
 167             if (osName.equals("AIX")) { return AIX; }
 168 
 169             throw new Error(osName + " is not a supported OS platform.");
 170         }
 171     }
 172 
 173     private static final Platform platform = Platform.get();
 174     private static final LaunchMechanism launchMechanism = platform.launchMechanism();
 175     private static final byte[] helperpath = toCString(platform.helperPath());
 176 
 177     private static byte[] toCString(String s) {
 178         if (s == null)
 179             return null;
 180         byte[] bytes = s.getBytes();
 181         byte[] result = new byte[bytes.length + 1];
 182         System.arraycopy(bytes, 0,


< prev index next >