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 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 {
main(String[] args)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                 } else if (Platform.isBSD()) {
65                     throw new SkippedException("SIGUSR2 can't be tested on BSD");
66                 }
67             }
68         }
69 
70         Path test = Paths.get(System.getProperty("test.nativepath"))
71                          .resolve("sigtest")
72                          .toAbsolutePath();
73         String envVar = Platform.isWindows() ? "PATH" :
74                 (Platform.isOSX() ? "DYLD_LIBRARY_PATH" : "LD_LIBRARY_PATH");
75 
76         List<String> cmd = new ArrayList<>();
77         Collections.addAll(cmd,
78                 test.toString(),
79                 "-sig",
80                 signame,
81                 "-mode",
82                 null, // modeIdx
83                 "-scenario",
84                 null // scenarioIdx
85         );
86         int modeIdx = 4;
87         int scenarioIdx = 6;
88 
89         // add external flags
90         cmd.addAll(vmargs());
91 
92         // add test specific arguments w/o signame
93         cmd.addAll(Arrays.asList(args)
94                          .subList(1, args.length));
95 
96         boolean passed = true;
97 
98         String[] functions = Platform.getOsName().equals("OpenBSD") ? new String[]{"sigaction"} :
99                 new String[]{"sigset", "sigaction"};
100 
101         for (String mode : functions) {
102             for (String scenario : new String[] {"nojvm", "prepre", "prepost", "postpre", "postpost"}) {
103                 cmd.set(modeIdx, mode);
104                 cmd.set(scenarioIdx, scenario);
105                 System.out.printf("START TESTING: SIGNAL = %s, MODE = %s, SCENARIO=%s%n",signame, mode, scenario);
106                 System.out.printf("Do execute: %s%n", cmd.toString());
107 
108                 ProcessBuilder pb = new ProcessBuilder(cmd);
109                 pb.environment().merge(envVar, jvmLibDir().toString(),
110                         (x, y) -> y + File.pathSeparator + x);
111                 pb.environment().put("CLASSPATH", Utils.TEST_CLASS_PATH);
112 
113                 switch (scenario) {
114                     case "postpre":
115                     case "postpost": {
116                         pb.environment().merge("LD_PRELOAD", libjsig().toString(),
117                                 (x, y) -> y + File.pathSeparator + x);
118                     }
119                 }
120 
121                 try {
122                     OutputAnalyzer oa = ProcessTools.executeProcess(pb);
123                     oa.reportDiagnosticSummary();
124                     int exitCode = oa.getExitValue();
125                     if (exitCode == 0) {
126                        System.out.println("PASSED with exit code 0");
127                     } else {
128                         System.out.println("FAILED with exit code " + exitCode);
129                         passed = false;
130                     }
131                 } catch (Exception e) {
132                     throw new Error("execution failed", e);
133                 }
134             }
135         }
136 
137         if (!passed) {
138             throw new Error("test failed");
139         }
140     }
141 
vmargs()142     private static List<String> vmargs() {
143         return Stream.concat(Arrays.stream(Utils.VM_OPTIONS.split(" ")),
144                              Arrays.stream(Utils.JAVA_OPTIONS.split(" ")))
145                      .filter(s -> !s.isEmpty())
146                      .filter(s -> s.startsWith("-X"))
147                      .flatMap(arg -> Stream.of("-vmopt", arg))
148                      .collect(Collectors.toList());
149     }
150 
libjsig()151     private static Path libjsig() {
152         return jvmLibDir().resolve((Platform.isWindows() ? "" : "lib")
153                 + "jsig." + Platform.sharedLibraryExt());
154     }
155 
jvmLibDir()156     private static Path jvmLibDir() {
157         Path dir = Paths.get(Utils.TEST_JDK);
158         if (Platform.isWindows()) {
159             return dir.resolve("bin")
160                       .resolve(variant())
161                       .toAbsolutePath();
162         } else {
163             return dir.resolve("lib")
164                       .resolve(variant())
165                       .toAbsolutePath();
166         }
167     }
168 
variant()169     private static String variant() {
170         if (Platform.isServer()) {
171             return "server";
172         } else if (Platform.isClient()) {
173             return "client";
174         } else if (Platform.isMinimal()) {
175             return "minimal";
176         } else {
177             throw new Error("TESTBUG: unsupported vm variant");
178         }
179     }
180 }
181