| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // This file contains functions for launching subprocesses. | 5 // This file contains functions for launching subprocesses. |
| 6 | 6 |
| 7 #ifndef BASE_PROCESS_LAUNCH_H_ | 7 #ifndef BASE_PROCESS_LAUNCH_H_ |
| 8 #define BASE_PROCESS_LAUNCH_H_ | 8 #define BASE_PROCESS_LAUNCH_H_ |
| 9 | 9 |
| 10 #include <stddef.h> | 10 #include <stddef.h> |
| 11 | 11 |
| 12 #include <string> | 12 #include <string> |
| 13 #include <utility> | 13 #include <utility> |
| 14 #include <vector> | 14 #include <vector> |
| 15 | 15 |
| 16 #include "base/base_export.h" | 16 #include "base/base_export.h" |
| 17 #include "base/environment.h" | 17 #include "base/environment.h" |
| 18 #include "base/macros.h" | 18 #include "base/macros.h" |
| 19 #include "base/process/process.h" | 19 #include "base/process/process.h" |
| 20 #include "base/process/process_handle.h" | 20 #include "base/process/process_handle.h" |
| 21 #include "base/strings/string_piece.h" | 21 #include "base/strings/string_piece.h" |
| 22 #include "build/build_config.h" | 22 #include "build/build_config.h" |
| 23 | 23 |
| 24 #if defined(OS_POSIX) | 24 #if defined(OS_POSIX) || defined(OS_FUCHSIA) |
| 25 #include "base/posix/file_descriptor_shuffle.h" | 25 #include "base/posix/file_descriptor_shuffle.h" |
| 26 #elif defined(OS_WIN) | 26 #elif defined(OS_WIN) |
| 27 #include <windows.h> | 27 #include <windows.h> |
| 28 #endif | 28 #endif |
| 29 | 29 |
| 30 namespace base { | 30 namespace base { |
| 31 | 31 |
| 32 class CommandLine; | 32 class CommandLine; |
| 33 | 33 |
| 34 #if defined(OS_WIN) | 34 #if defined(OS_WIN) |
| 35 typedef std::vector<HANDLE> HandlesToInheritVector; | 35 typedef std::vector<HANDLE> HandlesToInheritVector; |
| 36 #endif | 36 #endif |
| 37 // TODO(viettrungluu): Only define this on POSIX? | 37 // TODO(viettrungluu): Only define this on POSIX? |
| 38 typedef std::vector<std::pair<int, int> > FileHandleMappingVector; | 38 typedef std::vector<std::pair<int, int> > FileHandleMappingVector; |
| 39 | 39 |
| 40 // Options for launching a subprocess that are passed to LaunchProcess(). | 40 // Options for launching a subprocess that are passed to LaunchProcess(). |
| 41 // The default constructor constructs the object with default options. | 41 // The default constructor constructs the object with default options. |
| 42 struct BASE_EXPORT LaunchOptions { | 42 struct BASE_EXPORT LaunchOptions { |
| 43 #if defined(OS_POSIX) | 43 #if defined(OS_POSIX) || defined(OS_FUCHSIA) |
| 44 // Delegate to be run in between fork and exec in the subprocess (see | 44 // Delegate to be run in between fork and exec in the subprocess (see |
| 45 // pre_exec_delegate below) | 45 // pre_exec_delegate below) |
| 46 class BASE_EXPORT PreExecDelegate { | 46 class BASE_EXPORT PreExecDelegate { |
| 47 public: | 47 public: |
| 48 PreExecDelegate() {} | 48 PreExecDelegate() {} |
| 49 virtual ~PreExecDelegate() {} | 49 virtual ~PreExecDelegate() {} |
| 50 | 50 |
| 51 // Since this is to be run between fork and exec, and fork may have happened | 51 // Since this is to be run between fork and exec, and fork may have happened |
| 52 // while multiple threads were running, this function needs to be async | 52 // while multiple threads were running, this function needs to be async |
| 53 // safe. | 53 // safe. |
| 54 virtual void RunAsyncSafe() = 0; | 54 virtual void RunAsyncSafe() = 0; |
| 55 | 55 |
| 56 private: | 56 private: |
| 57 DISALLOW_COPY_AND_ASSIGN(PreExecDelegate); | 57 DISALLOW_COPY_AND_ASSIGN(PreExecDelegate); |
| 58 }; | 58 }; |
| 59 #endif // defined(OS_POSIX) | 59 #endif // defined(OS_POSIX) || defined(OS_FUCHSIA) |
| 60 | 60 |
| 61 LaunchOptions(); | 61 LaunchOptions(); |
| 62 LaunchOptions(const LaunchOptions&); | 62 LaunchOptions(const LaunchOptions&); |
| 63 ~LaunchOptions(); | 63 ~LaunchOptions(); |
| 64 | 64 |
| 65 // If true, wait for the process to complete. | 65 // If true, wait for the process to complete. |
| 66 bool wait = false; | 66 bool wait = false; |
| 67 | 67 |
| 68 // If not empty, change to this directory before executing the new process. | 68 // If not empty, change to this directory before executing the new process. |
| 69 base::FilePath current_directory; | 69 base::FilePath current_directory; |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 int clone_flags = 0; | 146 int clone_flags = 0; |
| 147 | 147 |
| 148 // By default, child processes will have the PR_SET_NO_NEW_PRIVS bit set. If | 148 // By default, child processes will have the PR_SET_NO_NEW_PRIVS bit set. If |
| 149 // true, then this bit will not be set in the new child process. | 149 // true, then this bit will not be set in the new child process. |
| 150 bool allow_new_privs = false; | 150 bool allow_new_privs = false; |
| 151 | 151 |
| 152 // Sets parent process death signal to SIGKILL. | 152 // Sets parent process death signal to SIGKILL. |
| 153 bool kill_on_parent_death = false; | 153 bool kill_on_parent_death = false; |
| 154 #endif // defined(OS_LINUX) | 154 #endif // defined(OS_LINUX) |
| 155 | 155 |
| 156 #if defined(OS_POSIX) | 156 #if defined(OS_POSIX) || defined(OS_FUCHSIA) |
| 157 // If not empty, launch the specified executable instead of | 157 // If not empty, launch the specified executable instead of |
| 158 // cmdline.GetProgram(). This is useful when it is necessary to pass a custom | 158 // cmdline.GetProgram(). This is useful when it is necessary to pass a custom |
| 159 // argv[0]. | 159 // argv[0]. |
| 160 base::FilePath real_path; | 160 base::FilePath real_path; |
| 161 | 161 |
| 162 // If non-null, a delegate to be run immediately prior to executing the new | 162 // If non-null, a delegate to be run immediately prior to executing the new |
| 163 // program in the child process. | 163 // program in the child process. |
| 164 // | 164 // |
| 165 // WARNING: If LaunchProcess is called in the presence of multiple threads, | 165 // WARNING: If LaunchProcess is called in the presence of multiple threads, |
| 166 // code running in this delegate essentially needs to be async-signal safe | 166 // code running in this delegate essentially needs to be async-signal safe |
| 167 // (see man 7 signal for a list of allowed functions). | 167 // (see man 7 signal for a list of allowed functions). |
| 168 PreExecDelegate* pre_exec_delegate = nullptr; | 168 PreExecDelegate* pre_exec_delegate = nullptr; |
| 169 #endif // defined(OS_POSIX) | 169 #endif // defined(OS_POSIX) || defined(OS_FUCHSIA) |
| 170 | 170 |
| 171 #if defined(OS_CHROMEOS) | 171 #if defined(OS_CHROMEOS) |
| 172 // If non-negative, the specified file descriptor will be set as the launched | 172 // If non-negative, the specified file descriptor will be set as the launched |
| 173 // process' controlling terminal. | 173 // process' controlling terminal. |
| 174 int ctrl_terminal_fd = -1; | 174 int ctrl_terminal_fd = -1; |
| 175 #endif // defined(OS_CHROMEOS) | 175 #endif // defined(OS_CHROMEOS) |
| 176 #endif // !defined(OS_WIN) | 176 #endif // !defined(OS_WIN) |
| 177 }; | 177 }; |
| 178 | 178 |
| 179 // Launch a process via the command line |cmdline|. | 179 // Launch a process via the command line |cmdline|. |
| (...skipping 27 matching lines...) Expand all Loading... |
| 207 const LaunchOptions& options); | 207 const LaunchOptions& options); |
| 208 | 208 |
| 209 // Launches a process with elevated privileges. This does not behave exactly | 209 // Launches a process with elevated privileges. This does not behave exactly |
| 210 // like LaunchProcess as it uses ShellExecuteEx instead of CreateProcess to | 210 // like LaunchProcess as it uses ShellExecuteEx instead of CreateProcess to |
| 211 // create the process. This means the process will have elevated privileges | 211 // create the process. This means the process will have elevated privileges |
| 212 // and thus some common operations like OpenProcess will fail. Currently the | 212 // and thus some common operations like OpenProcess will fail. Currently the |
| 213 // only supported LaunchOptions are |start_hidden| and |wait|. | 213 // only supported LaunchOptions are |start_hidden| and |wait|. |
| 214 BASE_EXPORT Process LaunchElevatedProcess(const CommandLine& cmdline, | 214 BASE_EXPORT Process LaunchElevatedProcess(const CommandLine& cmdline, |
| 215 const LaunchOptions& options); | 215 const LaunchOptions& options); |
| 216 | 216 |
| 217 #elif defined(OS_POSIX) | 217 #elif defined(OS_POSIX) || defined(OS_FUCHSIA) |
| 218 // A POSIX-specific version of LaunchProcess that takes an argv array | 218 // A POSIX-specific version of LaunchProcess that takes an argv array |
| 219 // instead of a CommandLine. Useful for situations where you need to | 219 // instead of a CommandLine. Useful for situations where you need to |
| 220 // control the command line arguments directly, but prefer the | 220 // control the command line arguments directly, but prefer the |
| 221 // CommandLine version if launching Chrome itself. | 221 // CommandLine version if launching Chrome itself. |
| 222 BASE_EXPORT Process LaunchProcess(const std::vector<std::string>& argv, | 222 BASE_EXPORT Process LaunchProcess(const std::vector<std::string>& argv, |
| 223 const LaunchOptions& options); | 223 const LaunchOptions& options); |
| 224 | 224 |
| 225 // Close all file descriptors, except those which are a destination in the | 225 // Close all file descriptors, except those which are a destination in the |
| 226 // given multimap. Only call this function in a child process where you know | 226 // given multimap. Only call this function in a child process where you know |
| 227 // that there aren't any other threads. | 227 // that there aren't any other threads. |
| 228 BASE_EXPORT void CloseSuperfluousFds(const InjectiveMultimap& saved_map); | 228 BASE_EXPORT void CloseSuperfluousFds(const InjectiveMultimap& saved_map); |
| 229 #endif // defined(OS_POSIX) | 229 #endif // defined(OS_POSIX) || defined(OS_FUCHSIA) |
| 230 | 230 |
| 231 #if defined(OS_WIN) | 231 #if defined(OS_WIN) |
| 232 // Set |job_object|'s JOBOBJECT_EXTENDED_LIMIT_INFORMATION | 232 // Set |job_object|'s JOBOBJECT_EXTENDED_LIMIT_INFORMATION |
| 233 // BasicLimitInformation.LimitFlags to |limit_flags|. | 233 // BasicLimitInformation.LimitFlags to |limit_flags|. |
| 234 BASE_EXPORT bool SetJobObjectLimitFlags(HANDLE job_object, DWORD limit_flags); | 234 BASE_EXPORT bool SetJobObjectLimitFlags(HANDLE job_object, DWORD limit_flags); |
| 235 | 235 |
| 236 // Output multi-process printf, cout, cerr, etc to the cmd.exe console that ran | 236 // Output multi-process printf, cout, cerr, etc to the cmd.exe console that ran |
| 237 // chrome. This is not thread-safe: only call from main thread. | 237 // chrome. This is not thread-safe: only call from main thread. |
| 238 BASE_EXPORT void RouteStdioToConsole(bool create_console_if_not_found); | 238 BASE_EXPORT void RouteStdioToConsole(bool create_console_if_not_found); |
| 239 #endif // defined(OS_WIN) | 239 #endif // defined(OS_WIN) |
| 240 | 240 |
| 241 // Executes the application specified by |cl| and wait for it to exit. Stores | 241 // Executes the application specified by |cl| and wait for it to exit. Stores |
| 242 // the output (stdout) in |output|. Redirects stderr to /dev/null. Returns true | 242 // the output (stdout) in |output|. Redirects stderr to /dev/null. Returns true |
| 243 // on success (application launched and exited cleanly, with exit code | 243 // on success (application launched and exited cleanly, with exit code |
| 244 // indicating success). | 244 // indicating success). |
| 245 BASE_EXPORT bool GetAppOutput(const CommandLine& cl, std::string* output); | 245 BASE_EXPORT bool GetAppOutput(const CommandLine& cl, std::string* output); |
| 246 | 246 |
| 247 // Like GetAppOutput, but also includes stderr. | 247 // Like GetAppOutput, but also includes stderr. |
| 248 BASE_EXPORT bool GetAppOutputAndError(const CommandLine& cl, | 248 BASE_EXPORT bool GetAppOutputAndError(const CommandLine& cl, |
| 249 std::string* output); | 249 std::string* output); |
| 250 | 250 |
| 251 #if defined(OS_WIN) | 251 #if defined(OS_WIN) |
| 252 // A Windows-specific version of GetAppOutput that takes a command line string | 252 // A Windows-specific version of GetAppOutput that takes a command line string |
| 253 // instead of a CommandLine object. Useful for situations where you need to | 253 // instead of a CommandLine object. Useful for situations where you need to |
| 254 // control the command line arguments directly. | 254 // control the command line arguments directly. |
| 255 BASE_EXPORT bool GetAppOutput(const StringPiece16& cl, std::string* output); | 255 BASE_EXPORT bool GetAppOutput(const StringPiece16& cl, std::string* output); |
| 256 #endif | 256 #endif |
| 257 | 257 |
| 258 #if defined(OS_POSIX) | 258 #if defined(OS_POSIX) || defined(OS_FUCHSIA) |
| 259 // A POSIX-specific version of GetAppOutput that takes an argv array | 259 // A POSIX-specific version of GetAppOutput that takes an argv array |
| 260 // instead of a CommandLine. Useful for situations where you need to | 260 // instead of a CommandLine. Useful for situations where you need to |
| 261 // control the command line arguments directly. | 261 // control the command line arguments directly. |
| 262 BASE_EXPORT bool GetAppOutput(const std::vector<std::string>& argv, | 262 BASE_EXPORT bool GetAppOutput(const std::vector<std::string>& argv, |
| 263 std::string* output); | 263 std::string* output); |
| 264 | 264 |
| 265 // A version of |GetAppOutput()| which also returns the exit code of the | 265 // A version of |GetAppOutput()| which also returns the exit code of the |
| 266 // executed command. Returns true if the application runs and exits cleanly. If | 266 // executed command. Returns true if the application runs and exits cleanly. If |
| 267 // this is the case the exit code of the application is available in | 267 // this is the case the exit code of the application is available in |
| 268 // |*exit_code|. | 268 // |*exit_code|. |
| 269 BASE_EXPORT bool GetAppOutputWithExitCode(const CommandLine& cl, | 269 BASE_EXPORT bool GetAppOutputWithExitCode(const CommandLine& cl, |
| 270 std::string* output, int* exit_code); | 270 std::string* output, int* exit_code); |
| 271 #endif // defined(OS_POSIX) | 271 #endif // defined(OS_POSIX) || defined(OS_FUCHSIA) |
| 272 | 272 |
| 273 // If supported on the platform, and the user has sufficent rights, increase | 273 // If supported on the platform, and the user has sufficent rights, increase |
| 274 // the current process's scheduling priority to a high priority. | 274 // the current process's scheduling priority to a high priority. |
| 275 BASE_EXPORT void RaiseProcessToHighPriority(); | 275 BASE_EXPORT void RaiseProcessToHighPriority(); |
| 276 | 276 |
| 277 #if defined(OS_MACOSX) | 277 #if defined(OS_MACOSX) |
| 278 // An implementation of LaunchProcess() that uses posix_spawn() instead of | 278 // An implementation of LaunchProcess() that uses posix_spawn() instead of |
| 279 // fork()+exec(). This does not support the |pre_exec_delegate| and | 279 // fork()+exec(). This does not support the |pre_exec_delegate| and |
| 280 // |current_directory| options. | 280 // |current_directory| options. |
| 281 Process LaunchProcessPosixSpawn(const std::vector<std::string>& argv, | 281 Process LaunchProcessPosixSpawn(const std::vector<std::string>& argv, |
| (...skipping 28 matching lines...) Expand all Loading... |
| 310 // multiple threads are running, since at the time the fork happened, the | 310 // multiple threads are running, since at the time the fork happened, the |
| 311 // threads could have been in any state (potentially holding locks, etc.). | 311 // threads could have been in any state (potentially holding locks, etc.). |
| 312 // Callers should most likely call execve() in the child soon after calling | 312 // Callers should most likely call execve() in the child soon after calling |
| 313 // this. | 313 // this. |
| 314 BASE_EXPORT pid_t ForkWithFlags(unsigned long flags, pid_t* ptid, pid_t* ctid); | 314 BASE_EXPORT pid_t ForkWithFlags(unsigned long flags, pid_t* ptid, pid_t* ctid); |
| 315 #endif | 315 #endif |
| 316 | 316 |
| 317 } // namespace base | 317 } // namespace base |
| 318 | 318 |
| 319 #endif // BASE_PROCESS_LAUNCH_H_ | 319 #endif // BASE_PROCESS_LAUNCH_H_ |
| OLD | NEW |