 Chromium Code Reviews
 Chromium Code Reviews Issue 2950153002:
  Improve process launch handle sharing API.  (Closed)
    
  
    Issue 2950153002:
  Improve process launch handle sharing API.  (Closed) 
  | 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> | 
| (...skipping 17 matching lines...) Expand all Loading... | |
| 28 #endif | 28 #endif | 
| 29 | 29 | 
| 30 #if defined(OS_FUCHSIA) | 30 #if defined(OS_FUCHSIA) | 
| 31 #include <magenta/types.h> | 31 #include <magenta/types.h> | 
| 32 #endif | 32 #endif | 
| 33 | 33 | 
| 34 namespace base { | 34 namespace base { | 
| 35 | 35 | 
| 36 class CommandLine; | 36 class CommandLine; | 
| 37 | 37 | 
| 38 #if defined(OS_WIN) | 38 #if defined(OS_POSIX) | 
| 39 typedef std::vector<std::pair<int, int>> FileHandleMappingVector; | |
| 
Joe Mason
2017/07/04 18:30:34
From the CL desc: "Name base::HandleToInheritVecto
 
brettw
2017/07/05 19:13:51
Will update the CL description. I put the vector d
 | |
| 40 #elif defined(OS_WIN) | |
| 39 typedef std::vector<HANDLE> HandlesToInheritVector; | 41 typedef std::vector<HANDLE> HandlesToInheritVector; | 
| 40 #elif defined(OS_FUCHSIA) | 42 #elif defined(OS_FUCHSIA) | 
| 
Joe Mason
2017/07/04 18:26:54
Is OS_POSIX also defined when OS_FUCHSIA is? (I ca
 
brettw
2017/07/05 19:13:51
Looks like it is, I reordered.
 | |
| 41 typedef std::vector<mx_handle_t> HandlesToInheritVector; | 43 typedef std::vector<mx_handle_t> HandlesToInheritVector; | 
| 42 #endif | 44 #endif | 
| 43 // TODO(viettrungluu): Only define this on POSIX? | |
| 44 typedef std::vector<std::pair<int, int> > FileHandleMappingVector; | |
| 45 | 45 | 
| 46 // Options for launching a subprocess that are passed to LaunchProcess(). | 46 // Options for launching a subprocess that are passed to LaunchProcess(). | 
| 47 // The default constructor constructs the object with default options. | 47 // The default constructor constructs the object with default options. | 
| 48 struct BASE_EXPORT LaunchOptions { | 48 struct BASE_EXPORT LaunchOptions { | 
| 49 #if defined(OS_POSIX) | 49 #if defined(OS_POSIX) | 
| 50 // Delegate to be run in between fork and exec in the subprocess (see | 50 // Delegate to be run in between fork and exec in the subprocess (see | 
| 51 // pre_exec_delegate below) | 51 // pre_exec_delegate below) | 
| 52 class BASE_EXPORT PreExecDelegate { | 52 class BASE_EXPORT PreExecDelegate { | 
| 53 public: | 53 public: | 
| 54 PreExecDelegate() {} | 54 PreExecDelegate() {} | 
| (...skipping 15 matching lines...) Expand all Loading... | |
| 70 | 70 | 
| 71 // If true, wait for the process to complete. | 71 // If true, wait for the process to complete. | 
| 72 bool wait = false; | 72 bool wait = false; | 
| 73 | 73 | 
| 74 // If not empty, change to this directory before executing the new process. | 74 // If not empty, change to this directory before executing the new process. | 
| 75 base::FilePath current_directory; | 75 base::FilePath current_directory; | 
| 76 | 76 | 
| 77 #if defined(OS_WIN) | 77 #if defined(OS_WIN) | 
| 78 bool start_hidden = false; | 78 bool start_hidden = false; | 
| 79 | 79 | 
| 80 // If non-null, inherit exactly the list of handles in this vector (these | 80 // Windows can inherit handles when it launches child processes. | 
| 81 // handles must be inheritable). | 81 // See https://blogs.msdn.microsoft.com/oldnewthing/20111216-00/?p=8873 | 
| 82 HandlesToInheritVector* handles_to_inherit = nullptr; | 82 // for a good overview of Windows handle inheritance. | 
| 83 // | |
| 84 // Implementation note: it might be nice to implement in terms of | |
| 85 // base::Optional<>, but then the natural default state (vector not present) | |
| 86 // would be "all inheritable handles" while we want "no inheritance." | |
| 87 enum class Inherit { | |
| 88 // Only those handles in |handles_to_inherit| vector are inherited. If the | |
| 89 // vector is empty, no handles are inherited. The handles in the vector must | |
| 90 // all be inheritable. | |
| 91 kSpecific, | |
| 83 | 92 | 
| 84 // If true, the new process inherits handles from the parent. In production | 93 // All handles in the current process which are inheritable are inherited. | 
| 85 // code this flag should be used only when running short-lived, trusted | 94 // In production code this flag should be used only when running | 
| 86 // binaries, because open handles from other libraries and subsystems will | 95 // short-lived, trusted binaries, because open handles from other libraries | 
| 87 // leak to the child process, causing errors such as open socket hangs. | 96 // and subsystems will leak to the child process, causing errors such as | 
| 88 // Note: If |handles_to_inherit| is non-null, this flag is ignored and only | 97 // open socket hangs. There are also race conditions that can cause handle | 
| 89 // those handles will be inherited. | 98 // over-sharing. | 
| 90 bool inherit_handles = false; | 99 // | 
| 100 // |handles_to_inherit| must be null. | |
| 101 kAll | |
| 102 }; | |
| 103 Inherit inherit_mode = Inherit::kSpecific; | |
| 104 HandlesToInheritVector handles_to_inherit; | |
| 91 | 105 | 
| 92 // If non-null, runs as if the user represented by the token had launched it. | 106 // If non-null, runs as if the user represented by the token had launched it. | 
| 93 // Whether the application is visible on the interactive desktop depends on | 107 // Whether the application is visible on the interactive desktop depends on | 
| 94 // the token belonging to an interactive logon session. | 108 // the token belonging to an interactive logon session. | 
| 95 // | 109 // | 
| 96 // To avoid hard to diagnose problems, when specified this loads the | 110 // To avoid hard to diagnose problems, when specified this loads the | 
| 97 // environment variables associated with the user and if this operation fails | 111 // environment variables associated with the user and if this operation fails | 
| 98 // the entire call fails as well. | 112 // the entire call fails as well. | 
| 99 UserTokenHandle as_user = nullptr; | 113 UserTokenHandle as_user = nullptr; | 
| 100 | 114 | 
| 101 // If true, use an empty string for the desktop name. | 115 // If true, use an empty string for the desktop name. | 
| 102 bool empty_desktop_name = false; | 116 bool empty_desktop_name = false; | 
| 103 | 117 | 
| 104 // If non-null, launches the application in that job object. The process will | 118 // If non-null, launches the application in that job object. The process will | 
| 105 // be terminated immediately and LaunchProcess() will fail if assignment to | 119 // be terminated immediately and LaunchProcess() will fail if assignment to | 
| 106 // the job object fails. | 120 // the job object fails. | 
| 107 HANDLE job_handle = nullptr; | 121 HANDLE job_handle = nullptr; | 
| 108 | 122 | 
| 109 // Handles for the redirection of stdin, stdout and stderr. The handles must | 123 // Handles for the redirection of stdin, stdout and stderr. The caller should | 
| 110 // be inheritable. Caller should either set all three of them or none (i.e. | 124 // either set all three of them or none (i.e. there is no way to redirect | 
| 111 // there is no way to redirect stderr without redirecting stdin). The | 125 // stderr without redirecting stdin). | 
| 112 // |inherit_handles| flag must be set to true when redirecting stdio stream. | 126 // | 
| 127 // The handles must be inheritable. Pseudo handles are used when stdout and | |
| 128 // stderr redirect to the console. In that case, GetFileType() will return | |
| 129 // FILE_TYPE_CHAR and they're automatically inherited by child processes. See | |
| 130 // https://msdn.microsoft.com/en-us/library/windows/desktop/ms682075.aspx | |
| 131 // Otherwise, the caller must ensure that the |inherit_mode| and/or | |
| 132 // |handles_to_inherit| set so that the handles are inherited. | |
| 113 HANDLE stdin_handle = nullptr; | 133 HANDLE stdin_handle = nullptr; | 
| 114 HANDLE stdout_handle = nullptr; | 134 HANDLE stdout_handle = nullptr; | 
| 115 HANDLE stderr_handle = nullptr; | 135 HANDLE stderr_handle = nullptr; | 
| 116 | 136 | 
| 117 // If set to true, ensures that the child process is launched with the | 137 // If set to true, ensures that the child process is launched with the | 
| 118 // CREATE_BREAKAWAY_FROM_JOB flag which allows it to breakout of the parent | 138 // CREATE_BREAKAWAY_FROM_JOB flag which allows it to breakout of the parent | 
| 119 // job if any. | 139 // job if any. | 
| 120 bool force_breakaway_from_job_ = false; | 140 bool force_breakaway_from_job_ = false; | 
| 121 #else // !defined(OS_WIN) | 141 #else // !defined(OS_WIN) | 
| 122 // Set/unset environment variables. These are applied on top of the parent | 142 // Set/unset environment variables. These are applied on top of the parent | 
| 123 // process environment. Empty (the default) means to inherit the same | 143 // process environment. Empty (the default) means to inherit the same | 
| 124 // environment. See AlterEnvironment(). | 144 // environment. See AlterEnvironment(). | 
| 125 EnvironmentMap environ; | 145 EnvironmentMap environ; | 
| 126 | 146 | 
| 127 // Clear the environment for the new process before processing changes from | 147 // Clear the environment for the new process before processing changes from | 
| 128 // |environ|. | 148 // |environ|. | 
| 129 bool clear_environ = false; | 149 bool clear_environ = false; | 
| 130 | 150 | 
| 131 // If non-null, remap file descriptors according to the mapping of | 151 // Remap file descriptors according to the mapping of src_fd->dest_fd to | 
| 132 // src fd->dest fd to propagate FDs into the child process. | 152 // propagate FDs into the child process. | 
| 133 // This pointer is owned by the caller and must live through the | 153 FileHandleMappingVector fds_to_remap; | 
| 134 // call to LaunchProcess(). | |
| 135 const FileHandleMappingVector* fds_to_remap = nullptr; | |
| 136 | 154 | 
| 137 // Each element is an RLIMIT_* constant that should be raised to its | 155 // Each element is an RLIMIT_* constant that should be raised to its | 
| 138 // rlim_max. This pointer is owned by the caller and must live through | 156 // rlim_max. This pointer is owned by the caller and must live through | 
| 139 // the call to LaunchProcess(). | 157 // the call to LaunchProcess(). | 
| 140 const std::vector<int>* maximize_rlimits = nullptr; | 158 const std::vector<int>* maximize_rlimits = nullptr; | 
| 141 | 159 | 
| 142 // If true, start the process in a new process group, instead of | 160 // If true, start the process in a new process group, instead of | 
| 143 // inheriting the parent's process group. The pgid of the child process | 161 // inheriting the parent's process group. The pgid of the child process | 
| 144 // will be the same as its pid. | 162 // will be the same as its pid. | 
| 145 bool new_process_group = false; | 163 bool new_process_group = false; | 
| (...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 326 // multiple threads are running, since at the time the fork happened, the | 344 // multiple threads are running, since at the time the fork happened, the | 
| 327 // threads could have been in any state (potentially holding locks, etc.). | 345 // threads could have been in any state (potentially holding locks, etc.). | 
| 328 // Callers should most likely call execve() in the child soon after calling | 346 // Callers should most likely call execve() in the child soon after calling | 
| 329 // this. | 347 // this. | 
| 330 BASE_EXPORT pid_t ForkWithFlags(unsigned long flags, pid_t* ptid, pid_t* ctid); | 348 BASE_EXPORT pid_t ForkWithFlags(unsigned long flags, pid_t* ptid, pid_t* ctid); | 
| 331 #endif | 349 #endif | 
| 332 | 350 | 
| 333 } // namespace base | 351 } // namespace base | 
| 334 | 352 | 
| 335 #endif // BASE_PROCESS_LAUNCH_H_ | 353 #endif // BASE_PROCESS_LAUNCH_H_ | 
| OLD | NEW |