Apache Mesos
exec.hpp
Go to the documentation of this file.
1 // Licensed under the Apache License, Version 2.0 (the "License");
2 // you may not use this file except in compliance with the License.
3 // You may obtain a copy of the License at
4 //
5 // http://www.apache.org/licenses/LICENSE-2.0
6 //
7 // Unless required by applicable law or agreed to in writing, software
8 // distributed under the License is distributed on an "AS IS" BASIS,
9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 // See the License for the specific language governing permissions and
11 // limitations under the License.
12 
13 #ifndef __STOUT_OS_EXEC_HPP__
14 #define __STOUT_OS_EXEC_HPP__
15 
16 // For readability, we minimize the number of #ifdef blocks in the code by
17 // splitting platform specific system calls into separate directories.
18 #ifdef __WINDOWS__
20 #else
21 #include <stout/os/posix/exec.hpp>
22 #endif // __WINDOWS__
23 
24 namespace os {
25 
26 // Forks a subprocess and executes the provided file with the provided
27 // arguments.
28 //
29 // Blocks until the subprocess terminates and returns the exit code of
30 // the subprocess, or `None` if an error occurred (e.g., fork / exec /
31 // waitpid or the Windows equivalents failed).
32 //
33 // POSIX: this function is async signal safe. We return an
34 // `Option<int>` instead of a `Try<int>`, because although `Try`
35 // does not dynamically allocate, `Error` uses `std::string`,
36 // which is not async signal safe.
37 //
38 // Windows: Note that on Windows, processes are started using
39 // a string command line, and each process does its own parsing
40 // of that command line into arguments. This function will quote
41 // and escape the arguments compatible with any programs that
42 // use `CommandLineToArgvW` (this is the most common way, and any
43 // programs that use libc-style main with an arguments array will
44 // use this under the covers). However, some programs, notably
45 // cmd.exe have their own approach for parsing quotes / arguments
46 // that are not compatible with CommandLineToArgvW, and therefore
47 // should not be used with this function!
48 //
49 // TODO(bmahler): Add a windows only overload that takes a single
50 // string command line (to support cmd.exe and others with non-
51 // CommandLineToArgvW parsing). See MESOS-10093.
52 inline Option<int> spawn(
53  const std::string& file,
54  const std::vector<std::string>& arguments);
55 
56 
57 // This wrapper allows a caller to call `execvp` on both POSIX
58 // and Windows systems.
59 //
60 // Windows: In order to emulate `execvp`, this function forks
61 // another subprocess to execute with the provided arguments,
62 // and once the subprocess terminates, the parent process will
63 // in turn exit with the same exit code. Note that on Windows,
64 // processes are started using a string command line, and each
65 // process does its own parsing of that command line into
66 // arguments. This function will quote and escape the arguments
67 // compatible with any programs that use `CommandLineToArgvW`
68 // (this is the most common way, and any programs that use
69 // libc-style main with an arguments array will use this under
70 // the covers). However, some programs, notably cmd.exe have
71 // their own approach for parsing quotes / arguments that are
72 // not compatible with CommandLineToArgvW, and therefore should
73 // not be used with this function!
74 //
75 // TODO(bmahler): Probably we shouldn't provide this windows
76 // emulation and should instead have the caller use windows
77 // subprocess functions directly?
78 inline int execvp(const char* file, char* const argv[]);
79 
80 
81 // This function is a portable version of execvpe ('p' means searching
82 // executable from PATH and 'e' means setting environments). We add
83 // this function because it is not available on all POSIX systems.
84 //
85 // POSIX: This function is not thread safe. It is supposed to be used
86 // only after fork (when there is only one thread). This function is
87 // async signal safe.
88 //
89 // Windows: In order to emulate `execvpe`, this function forks
90 // another subprocess to execute with the provided arguments,
91 // and once the subprocess terminates, the parent process will
92 // in turn exit with the same exit code. Note that on Windows,
93 // processes are started using a string command line, and each
94 // process does its own parsing of that command line into
95 // arguments. This function will quote and escape the arguments
96 // compatible with any programs that use `CommandLineToArgvW`
97 // (this is the most common way, and any programs that use
98 // libc-style main with an arguments array will use this under
99 // the covers). However, some programs, notably cmd.exe have
100 // their own approach for parsing quotes / arguments that are
101 // not compatible with CommandLineToArgvW, and therefore should
102 // not be used with this function!
103 //
104 // NOTE: This function can accept `Argv` and `Envp` constructs
105 // via their implicit type conversions, but on Windows, it cannot
106 // accept the os::raw forms.
107 //
108 // TODO(bmahler): Probably we shouldn't provide this windows
109 // emulation and should instead have the caller use windows
110 // subprocess functions directly?
111 inline int execvpe(const char* file, char** argv, char** envp);
112 
113 } // namespace os {
114 
115 #endif // __STOUT_OS_EXEC_HPP__
Definition: posix_signalhandler.hpp:23
int execvp(const char *file, char *const argv[])
Definition: exec.hpp:58
URI file(const std::string &path)
Creates a file URI with the given path on the local host.
Definition: file.hpp:33
int execvpe(const char *file, char **argv, char **envp)
Definition: exec.hpp:64
Option< int > spawn(const std::string &file, const std::vector< std::string > &arguments)
Definition: exec.hpp:32