1 /*
   2  * Copyright (c) 2007, 2018, 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 jdk.test.lib.Platform;
  25 import jdk.test.lib.Utils;
  26 import jdk.test.lib.process.OutputAnalyzer;
  27 import jdk.test.lib.process.ProcessTools;
  28 
  29 import java.io.File;
  30 import java.nio.file.Path;
  31 import java.nio.file.Paths;
  32 import java.util.ArrayList;
  33 import java.util.Arrays;
  34 import java.util.Collections;
  35 import java.util.List;
  36 import java.util.stream.Collectors;
  37 import java.util.stream.Stream;
  38 
  39 public class SigTestDriver {
  40     public static void main(String[] args) {
  41         // No signal tests on Windows yet; so setting to no-op
  42         if (Platform.isWindows()) {
  43             System.out.println("SKIPPED: no signal tests on Windows, ignore.");
  44             return;
  45         }
  46 
  47         String signame = args[0];
  48         switch (signame) {
  49             case "SIGWAITING":
  50             case "SIGKILL":
  51             case "SIGSTOP": {
  52                 System.out.println("SKIPPED: signals SIGWAITING, SIGKILL and SIGSTOP can't be tested, ignore.");
  53                 return;
  54             }
  55             case "SIGUSR2": {
  56                 if (Platform.isLinux()) {
  57                     System.out.println("SKIPPED: SIGUSR2 can't be tested on Linux, ignore.");
  58                     return;
  59                 } else if (Platform.isOSX()) {
  60                     System.out.println("SKIPPED: SIGUSR2 can't be tested on OS X, ignore.");
  61                     return;
  62                 }
  63             }
  64         }
  65 
  66         Path test = Paths.get(System.getProperty("test.nativepath"))
  67                          .resolve("sigtest")
  68                          .toAbsolutePath();
  69         String envVar = Platform.isWindows() ? "PATH" :
  70                 (Platform.isOSX() ? "DYLD_LIBRARY_PATH" : "LD_LIBRARY_PATH");
  71 
  72         List<String> cmd = new ArrayList<>();
  73         Collections.addAll(cmd,
  74                 test.toString(),
  75                 "-sig",
  76                 signame,
  77                 "-mode",
  78                 null, // modeIdx
  79                 "-scenario",
  80                 null // scenarioIdx
  81         );
  82         int modeIdx = 4;
  83         int scenarioIdx = 6;
  84 
  85         // add external flags
  86         cmd.addAll(vmargs());
  87 
  88         // add test specific arguments w/o signame
  89         cmd.addAll(Arrays.asList(args)
  90                          .subList(1, args.length));
  91 
  92         boolean passed = true;
  93 
  94         for (String mode : new String[]{"sigset", "sigaction"}) {
  95             for (String scenario : new String[] {"nojvm", "prepre", "prepost", "postpre", "postpost"}) {
  96                 cmd.set(modeIdx, mode);
  97                 cmd.set(scenarioIdx, scenario);
  98                 System.out.printf("START TESTING: SIGNAL = %s, MODE = %s, SCENARIO=%s%n",signame, mode, scenario);
  99                 System.out.printf("Do execute: %s%n", cmd.toString());
 100 
 101                 ProcessBuilder pb = new ProcessBuilder(cmd);
 102                 pb.environment().merge(envVar, jvmLibDir().toString(),
 103                         (x, y) -> y + File.pathSeparator + x);
 104                 pb.environment().put("CLASSPATH", Utils.TEST_CLASS_PATH);
 105 
 106                 switch (scenario) {
 107                     case "postpre":
 108                     case "postpost": {
 109                         pb.environment().merge("LD_PRELOAD", libjsig().toString(),
 110                                 (x, y) -> y + File.pathSeparator + x);
 111                     }
 112                 }
 113 
 114                 try {
 115                     OutputAnalyzer oa = ProcessTools.executeProcess(pb);
 116                     oa.reportDiagnosticSummary();
 117                     int exitCode = oa.getExitValue();
 118                     if (exitCode == 0) {
 119                        System.out.println("PASSED with exit code 0");
 120                     } else {
 121                         System.out.println("FAILED with exit code " + exitCode);
 122                         passed = false;
 123                     }
 124                 } catch (Exception e) {
 125                     throw new Error("execution failed", e);
 126                 }
 127             }
 128         }
 129 
 130         if (!passed) {
 131             throw new Error("test failed");
 132         }
 133     }
 134 
 135     private static List<String> vmargs() {
 136         return Stream.concat(Arrays.stream(Utils.VM_OPTIONS.split(" ")),
 137                              Arrays.stream(Utils.JAVA_OPTIONS.split(" ")))
 138                      .filter(s -> !s.isEmpty())
 139                      .filter(s -> s.startsWith("-X"))
 140                      .flatMap(arg -> Stream.of("-vmopt", arg))
 141                      .collect(Collectors.toList());
 142     }
 143 
 144     private static Path libjsig() {
 145         return jvmLibDir().resolve((Platform.isWindows() ? "" : "lib")
 146                 + "jsig." + Platform.sharedLibraryExt());
 147     }
 148 
 149     private static Path jvmLibDir() {
 150         Path dir = Paths.get(Utils.TEST_JDK);
 151         if (Platform.isWindows()) {
 152             return dir.resolve("bin")
 153                       .resolve(variant())
 154                       .toAbsolutePath();
 155         } else {
 156             return dir.resolve("lib")
 157                       .resolve(variant())
 158                       .toAbsolutePath();
 159         }
 160     }
 161 
 162     private static String variant() {
 163         if (Platform.isServer()) {
 164             return "server";
 165         } else if (Platform.isClient()) {
 166             return "client";
 167         } else if (Platform.isMinimal()) {
 168             return "minimal";
 169         } else {
 170             throw new Error("TESTBUG: unsupported vm variant");
 171         }
 172     }
 173 }