1//===- llvm/Support/Program.h ------------------------------------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file declares the llvm::sys::Program class.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_SUPPORT_PROGRAM_H
15#define LLVM_SUPPORT_PROGRAM_H
16
17#include "llvm/ADT/ArrayRef.h"
18#include "llvm/Support/Path.h"
19#include <system_error>
20
21namespace llvm {
22namespace sys {
23
24  /// This is the OS-specific separator for PATH like environment variables:
25  // a colon on Unix or a semicolon on Windows.
26#if defined(LLVM_ON_UNIX)
27  const char EnvPathSeparator = ':';
28#elif defined (LLVM_ON_WIN32)
29  const char EnvPathSeparator = ';';
30#endif
31
32/// @brief This struct encapsulates information about a process.
33struct ProcessInfo {
34#if defined(LLVM_ON_UNIX)
35  typedef pid_t ProcessId;
36#elif defined(LLVM_ON_WIN32)
37  typedef unsigned long ProcessId; // Must match the type of DWORD on Windows.
38  typedef void * HANDLE; // Must match the type of HANDLE on Windows.
39  /// The handle to the process (available on Windows only).
40  HANDLE ProcessHandle;
41#else
42#error "ProcessInfo is not defined for this platform!"
43#endif
44
45  /// The process identifier.
46  ProcessId Pid;
47
48  /// The return code, set after execution.
49  int ReturnCode;
50
51  ProcessInfo();
52};
53
54  /// This function attempts to locate a program in the operating
55  /// system's file system using some pre-determined set of locations to search
56  /// (e.g. the PATH on Unix). Paths with slashes are returned unmodified.
57  ///
58  /// It does not perform hashing as a shell would but instead stats each PATH
59  /// entry individually so should generally be avoided. Core LLVM library
60  /// functions and options should instead require fully specified paths.
61  ///
62  /// @returns A string containing the path of the program or an empty string if
63  /// the program could not be found.
64  std::string FindProgramByName(const std::string& name);
65
66  // These functions change the specified standard stream (stdin or stdout) to
67  // binary mode. They return errc::success if the specified stream
68  // was changed. Otherwise a platform dependent error is returned.
69  std::error_code ChangeStdinToBinary();
70  std::error_code ChangeStdoutToBinary();
71
72  /// This function executes the program using the arguments provided.  The
73  /// invoked program will inherit the stdin, stdout, and stderr file
74  /// descriptors, the environment and other configuration settings of the
75  /// invoking program.
76  /// This function waits for the program to finish, so should be avoided in
77  /// library functions that aren't expected to block. Consider using
78  /// ExecuteNoWait() instead.
79  /// @returns an integer result code indicating the status of the program.
80  /// A zero or positive value indicates the result code of the program.
81  /// -1 indicates failure to execute
82  /// -2 indicates a crash during execution or timeout
83  int ExecuteAndWait(
84      StringRef Program, ///< Path of the program to be executed. It is
85      /// presumed this is the result of the FindProgramByName method.
86      const char **args, ///< A vector of strings that are passed to the
87      ///< program.  The first element should be the name of the program.
88      ///< The list *must* be terminated by a null char* entry.
89      const char **env = nullptr, ///< An optional vector of strings to use for
90      ///< the program's environment. If not provided, the current program's
91      ///< environment will be used.
92      const StringRef **redirects = nullptr, ///< An optional array of pointers
93      ///< to paths. If the array is null, no redirection is done. The array
94      ///< should have a size of at least three. The inferior process's
95      ///< stdin(0), stdout(1), and stderr(2) will be redirected to the
96      ///< corresponding paths.
97      ///< When an empty path is passed in, the corresponding file
98      ///< descriptor will be disconnected (ie, /dev/null'd) in a portable
99      ///< way.
100      unsigned secondsToWait = 0, ///< If non-zero, this specifies the amount
101      ///< of time to wait for the child process to exit. If the time
102      ///< expires, the child is killed and this call returns. If zero,
103      ///< this function will wait until the child finishes or forever if
104      ///< it doesn't.
105      unsigned memoryLimit = 0, ///< If non-zero, this specifies max. amount
106      ///< of memory can be allocated by process. If memory usage will be
107      ///< higher limit, the child is killed and this call returns. If zero
108      ///< - no memory limit.
109      std::string *ErrMsg = nullptr, ///< If non-zero, provides a pointer to a
110      ///< string instance in which error messages will be returned. If the
111      ///< string is non-empty upon return an error occurred while invoking the
112      ///< program.
113      bool *ExecutionFailed = nullptr);
114
115  /// Similar to ExecuteAndWait, but returns immediately.
116  /// @returns The \see ProcessInfo of the newly launced process.
117  /// \note On Microsoft Windows systems, users will need to either call \see
118  /// Wait until the process finished execution or win32 CloseHandle() API on
119  /// ProcessInfo.ProcessHandle to avoid memory leaks.
120  ProcessInfo
121  ExecuteNoWait(StringRef Program, const char **args, const char **env = nullptr,
122                const StringRef **redirects = nullptr, unsigned memoryLimit = 0,
123                std::string *ErrMsg = nullptr, bool *ExecutionFailed = nullptr);
124
125  /// Return true if the given arguments fit within system-specific
126  /// argument length limits.
127  bool argumentsFitWithinSystemLimits(ArrayRef<const char*> Args);
128
129  /// This function waits for the process specified by \p PI to finish.
130  /// \returns A \see ProcessInfo struct with Pid set to:
131  /// \li The process id of the child process if the child process has changed
132  /// state.
133  /// \li 0 if the child process has not changed state.
134  /// \note Users of this function should always check the ReturnCode member of
135  /// the \see ProcessInfo returned from this function.
136  ProcessInfo Wait(
137      const ProcessInfo &PI, ///< The child process that should be waited on.
138      unsigned SecondsToWait, ///< If non-zero, this specifies the amount of
139      ///< time to wait for the child process to exit. If the time expires, the
140      ///< child is killed and this function returns. If zero, this function
141      ///< will perform a non-blocking wait on the child process.
142      bool WaitUntilTerminates, ///< If true, ignores \p SecondsToWait and waits
143      ///< until child has terminated.
144      std::string *ErrMsg = nullptr ///< If non-zero, provides a pointer to a
145      ///< string instance in which error messages will be returned. If the
146      ///< string is non-empty upon return an error occurred while invoking the
147      ///< program.
148      );
149  }
150}
151
152#endif
153