1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.tradefed.sandbox; 17 18 import com.android.tradefed.config.ConfigurationException; 19 import com.android.tradefed.config.GlobalConfiguration; 20 import com.android.tradefed.config.IConfiguration; 21 import com.android.tradefed.log.LogUtil.CLog; 22 import com.android.tradefed.sandbox.SandboxConfigDump.DumpCmd; 23 import com.android.tradefed.util.CommandResult; 24 import com.android.tradefed.util.CommandStatus; 25 import com.android.tradefed.util.FileUtil; 26 import com.android.tradefed.util.IRunUtil; 27 import com.android.tradefed.util.IRunUtil.EnvPriority; 28 import com.android.tradefed.util.SystemUtil; 29 import com.android.tradefed.util.TimeUtil; 30 31 import com.google.common.base.Strings; 32 33 import java.io.File; 34 import java.io.IOException; 35 import java.util.ArrayList; 36 import java.util.List; 37 import java.util.Set; 38 39 /** A utility class for managing {@link IConfiguration} when doing sandboxing. */ 40 public class SandboxConfigUtil { 41 42 private static final long DUMP_TIMEOUT = 5 * 60 * 1000; // 5 minutes 43 44 /** 45 * Create a subprocess based on the Tf jars from any version, and dump the xml {@link 46 * IConfiguration} based on the command line args. 47 * 48 * @param classpath the classpath to use to run the sandbox. 49 * @param runUtil the {@link IRunUtil} to use to run the command. 50 * @param args the command line args. 51 * @param dump the {@link DumpCmd} driving some of the outputs. 52 * @param globalConfig the file describing the global configuration to be used. 53 * @return A {@link File} containing the xml dump from the command line. 54 * @throws SandboxConfigurationException if the dump is not successful. 55 */ dumpConfigForVersion( String classpath, IRunUtil runUtil, String[] args, DumpCmd dump, File globalConfig)56 public static File dumpConfigForVersion( 57 String classpath, IRunUtil runUtil, String[] args, DumpCmd dump, File globalConfig) 58 throws SandboxConfigurationException, IOException { 59 if (Strings.isNullOrEmpty(classpath)) { 60 throw new SandboxConfigurationException( 61 "Something went wrong with the sandbox setup, classpath was empty."); 62 } 63 runUtil.unsetEnvVariable(GlobalConfiguration.GLOBAL_CONFIG_VARIABLE); 64 runUtil.unsetEnvVariable(GlobalConfiguration.GLOBAL_CONFIG_SERVER_CONFIG_VARIABLE); 65 File destination = null; 66 try { 67 destination = FileUtil.createTempFile("config-container", ".xml"); 68 if (globalConfig != null) { 69 runUtil.setEnvVariable( 70 GlobalConfiguration.GLOBAL_CONFIG_VARIABLE, globalConfig.getAbsolutePath()); 71 runUtil.setEnvVariablePriority(EnvPriority.SET); 72 } 73 } catch (IOException e) { 74 FileUtil.deleteFile(globalConfig); 75 FileUtil.deleteFile(destination); 76 throw e; 77 } 78 List<String> mCmdArgs = new ArrayList<>(); 79 mCmdArgs.add(SystemUtil.getRunningJavaBinaryPath().getAbsolutePath()); 80 mCmdArgs.add("-cp"); 81 mCmdArgs.add(classpath); 82 mCmdArgs.add(SandboxConfigDump.class.getCanonicalName()); 83 mCmdArgs.add(dump.toString()); 84 mCmdArgs.add(destination.getAbsolutePath()); 85 for (String arg : args) { 86 mCmdArgs.add(arg); 87 } 88 CommandResult result = runUtil.runTimedCmd(DUMP_TIMEOUT, mCmdArgs.toArray(new String[0])); 89 if (CommandStatus.SUCCESS.equals(result.getStatus())) { 90 return destination; 91 } 92 93 if (result.getStderr() != null && !result.getStderr().isEmpty()) { 94 CLog.d("stderr: %s\nstdout: %s", result.getStderr(), result.getStdout()); 95 } 96 97 FileUtil.deleteFile(destination); 98 // Do not delete the global configuration file here in this case, it might still be used. 99 String errorMessage = "Error when dumping the config."; 100 if (CommandStatus.TIMED_OUT.equals(result.getStatus())) { 101 errorMessage += 102 String.format(" Timed out after %s.", TimeUtil.formatElapsedTime(DUMP_TIMEOUT)); 103 } 104 errorMessage += String.format(" stderr: %s", result.getStderr()); 105 throw new SandboxConfigurationException(errorMessage); 106 } 107 108 /** 109 * Create a subprocess based on the Tf jars from any version, and dump the xml {@link 110 * IConfiguration} based on the command line args. 111 * 112 * @param rootDir the directory containing all the jars from TF. 113 * @param runUtil the {@link IRunUtil} to use to run the command. 114 * @param args the command line args. 115 * @param dump the {@link DumpCmd} driving some of the outputs. 116 * @param globalConfig the file describing the global configuration to be used. 117 * @return A {@link File} containing the xml dump from the command line. 118 * @throws ConfigurationException if the dump is not successful. 119 */ dumpConfigForVersion( File rootDir, IRunUtil runUtil, String[] args, DumpCmd dump, File globalConfig)120 public static File dumpConfigForVersion( 121 File rootDir, IRunUtil runUtil, String[] args, DumpCmd dump, File globalConfig) 122 throws ConfigurationException, IOException { 123 // include all jars on the classpath 124 String classpath = ""; 125 Set<String> jarFiles = FileUtil.findFiles(rootDir, ".*.jar"); 126 classpath = String.join(":", jarFiles); 127 return dumpConfigForVersion(classpath, runUtil, args, dump, globalConfig); 128 } 129 130 /** Create a global config with only the keystore to make it available in subprocess. */ dumpFilteredGlobalConfig(Set<String> exclusionPatterns)131 public static File dumpFilteredGlobalConfig(Set<String> exclusionPatterns) throws IOException { 132 return GlobalConfiguration.getInstance().cloneConfigWithFilter(exclusionPatterns); 133 } 134 } 135