< prev index next >

test/hotspot/jtreg/serviceability/sa/ClhsdbLauncher.java

Print this page

        

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.

@@ -23,49 +23,56 @@
 
 import java.io.IOException;
 import java.io.OutputStream;
 import java.util.List;
 import java.util.Map;
+import java.util.Arrays;
 
 import jdk.test.lib.apps.LingeredApp;
 import jdk.test.lib.Platform;
 import jdk.test.lib.JDKToolLauncher;
 import jdk.test.lib.JDKToolFinder;
 import jdk.test.lib.process.OutputAnalyzer;
+import jdk.test.lib.SA.SATestUtils;
+
 
 /**
  * This is a framework to run 'jhsdb clhsdb' commands.
  * See open/test/hotspot/jtreg/serviceability/sa/ClhsdbLongConstant.java for
  * an example of how to write a test.
  */
 
 public class ClhsdbLauncher {
 
     private Process toolProcess;
+    private boolean needPrivileges;
 
     public ClhsdbLauncher() {
         toolProcess = null;
+        needPrivileges = false;
     }
 
     /**
      *
      * Launches 'jhsdb clhsdb' and attaches to the Lingered App process.
      * @param lingeredAppPid  - pid of the Lingered App or one its sub-classes.
      */
     private void attach(long lingeredAppPid)
         throws IOException {
-
         JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jhsdb");
         launcher.addToolArg("clhsdb");
         if (lingeredAppPid != -1) {
             launcher.addToolArg("--pid=" + Long.toString(lingeredAppPid));
             System.out.println("Starting clhsdb against " + lingeredAppPid);
         }
 
-        ProcessBuilder processBuilder = new ProcessBuilder(launcher.getCommand());
+        List<String> cmdStringList = Arrays.asList(launcher.getCommand());
+        if (needPrivileges) {
+            cmdStringList = SATestUtils.addPrivileges(cmdStringList);
+        }
+        ProcessBuilder processBuilder = new ProcessBuilder(cmdStringList);
         processBuilder.redirectError(ProcessBuilder.Redirect.INHERIT);
-
         toolProcess = processBuilder.start();
     }
 
     /**
      *

@@ -123,10 +130,11 @@
             toolProcess.destroyForcibly();
             throw new Error("Problem awaiting the child process: " + ie);
         }
 
         oa.shouldHaveExitValue(0);
+
         output = oa.getOutput();
         System.out.println(output);
 
         String[] parts = output.split("hsdb>");
         for (String cmd : commands) {

@@ -171,13 +179,26 @@
                       Map<String, List<String>> expectedStrMap,
                       Map<String, List<String>> unExpectedStrMap)
         throws IOException, InterruptedException {
 
         if (!Platform.shouldSAAttach()) {
-            // Silently skip the test if we don't have enough permissions to attach
+            if (Platform.isOSX()) {
+                // If we are on OSX and not 'root', we try to run the command
+                // by appending 'sudo' to it. Check now if sudo passes in this
+                // environment with a simple 'echo' command.
+                if (!SATestUtils.canAddPrivileges()) {
+                   // Skip the test if we don't have enough permissions to attach
+                   // and cannot add privileges.
             System.out.println("SA attach not expected to work - test skipped.");
             return null;
+               } else {
+                   needPrivileges = true;
+               }
+            } else {
+                System.out.println("SA attach not expected to work. Insufficient privileges.");
+                throw new Error("Cannot attach.");
+            }
         }
 
         attach(lingeredAppPid);
         return runCmd(commands, expectedStrMap, unExpectedStrMap);
     }

@@ -198,15 +219,9 @@
                             List<String> commands,
                             Map<String, List<String>> expectedStrMap,
                             Map<String, List<String>> unExpectedStrMap)
         throws IOException, InterruptedException {
 
-        if (!Platform.shouldSAAttach()) {
-            // Silently skip the test if we don't have enough permissions to attach
-            System.out.println("SA attach not expected to work - test skipped.");
-            return null;
-        }
-
         loadCore(coreFileName);
         return runCmd(commands, expectedStrMap, unExpectedStrMap);
     }
 }
< prev index next >