1 /*
   2  * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.lang.ProcessBuilder.Redirect;
  25 import java.nio.file.Files;
  26 import java.nio.file.Path;
  27 import java.nio.file.Paths;
  28 import java.nio.file.StandardCopyOption;
  29 import java.util.Arrays;
  30 import java.util.List;
  31 import jdk.test.lib.Asserts;
  32 
  33 /*
  34  * @test
  35  * @key cte_test
  36  * @bug 4345157
  37  * @summary JDK 1.3.0 alters thread signal mask
  38  * @requires (vm.arch == "sparcv9")
  39  * @modules java.base/jdk.internal.misc
  40  * @library /testlibrary
  41  * @compile Prog.java
  42  * @run main/native ThreadSignalMask
  43  */
  44 public class ThreadSignalMask {
  45 
  46     public static void main(String args[]) throws Exception {
  47 
  48         String testClasses = getSystemProperty("test.classes");
  49 
  50         String testNativePath = getSystemProperty("test.nativepath");
  51 
  52         String testJdk = getSystemProperty("test.jdk");
  53 
  54         Path currentDirPath = Paths.get(".");
  55 
  56         Path classFilePath = Paths.get(testClasses,
  57                 Prog.class.getSimpleName() + ".class");
  58 
  59         // copy Prog.class file to be invoked from native
  60         Files.copy(classFilePath,
  61                 currentDirPath.resolve(Prog.class.getSimpleName() + ".class"),
  62                 StandardCopyOption.REPLACE_EXISTING);
  63 
  64         Path executableFilePath = Paths.get(testNativePath,
  65                 ThreadSignalMask.class.getSimpleName());
  66 
  67         Path executableFileLocalPath = currentDirPath.resolve(
  68                 ThreadSignalMask.class.getSimpleName());
  69 
  70         // copy compiled native executable ThreadSignalMask
  71         Files.copy(executableFilePath,
  72                 executableFileLocalPath,
  73                 StandardCopyOption.REPLACE_EXISTING);
  74 
  75         executableFileLocalPath.toFile().setExecutable(true);
  76 
  77         long[] intervalsArray = {2000, 5000, 10000, 20000};
  78 
  79         List<String> processArgs = Arrays.asList(
  80                 executableFileLocalPath.toString(),
  81                 testJdk);
  82         ProcessBuilder pb = new ProcessBuilder(processArgs);
  83         pb.redirectOutput(Redirect.INHERIT);
  84         pb.redirectError(Redirect.INHERIT);
  85         int result = 0;
  86         for (long interval : intervalsArray) {
  87             Process p = pb.start();
  88 
  89             // sleep for a specified period of time to let native run
  90             sleep(interval);
  91             p.destroy();
  92 
  93             // wait for process to finish, get exit value and validate it
  94             result = p.waitFor();
  95             System.out.println("Result = " + result);
  96             if (result == 0) {
  97                 break;
  98             }
  99         }
 100 
 101         Asserts.assertEquals(result, 0);
 102     }
 103 
 104     // Utility method to handle Thread.sleep
 105     private static void sleep(long millis) throws InterruptedException {
 106         System.out.println("Sleep for " + millis);
 107         Thread.sleep(millis);
 108     }
 109 
 110     // Utility method to retrieve and validate system properties
 111     private static String getSystemProperty(String propertyName) throws Error {
 112         String systemProperty = System.getProperty(propertyName, "").trim();
 113         System.out.println(propertyName + " = " + systemProperty);
 114         if (systemProperty.isEmpty()) {
 115             throw new Error("TESTBUG: property " + propertyName + " is empty");
 116         }
 117         return systemProperty;
 118     }
 119 }