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