1*0b57cec5SDimitry Andric //===-- Program.cpp - Implement OS Program Concept --------------*- C++ -*-===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric //
9*0b57cec5SDimitry Andric // This file implements the operating system Program concept.
10*0b57cec5SDimitry Andric //
11*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
12*0b57cec5SDimitry Andric
13*0b57cec5SDimitry Andric #include "llvm/Support/Program.h"
14*0b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h"
15*0b57cec5SDimitry Andric #include "llvm/Config/llvm-config.h"
16*0b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
17*0b57cec5SDimitry Andric using namespace llvm;
18*0b57cec5SDimitry Andric using namespace sys;
19*0b57cec5SDimitry Andric
20*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
21*0b57cec5SDimitry Andric //=== WARNING: Implementation here must contain only TRULY operating system
22*0b57cec5SDimitry Andric //=== independent code.
23*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
24*0b57cec5SDimitry Andric
25*0b57cec5SDimitry Andric static bool Execute(ProcessInfo &PI, StringRef Program,
26*0b57cec5SDimitry Andric ArrayRef<StringRef> Args,
27*0b57cec5SDimitry Andric std::optional<ArrayRef<StringRef>> Env,
28*0b57cec5SDimitry Andric ArrayRef<std::optional<StringRef>> Redirects,
29*0b57cec5SDimitry Andric unsigned MemoryLimit, std::string *ErrMsg,
30*0b57cec5SDimitry Andric BitVector *AffinityMask);
31*0b57cec5SDimitry Andric
ExecuteAndWait(StringRef Program,ArrayRef<StringRef> Args,std::optional<ArrayRef<StringRef>> Env,ArrayRef<std::optional<StringRef>> Redirects,unsigned SecondsToWait,unsigned MemoryLimit,std::string * ErrMsg,bool * ExecutionFailed,std::optional<ProcessStatistics> * ProcStat,BitVector * AffinityMask)32*0b57cec5SDimitry Andric int sys::ExecuteAndWait(StringRef Program, ArrayRef<StringRef> Args,
33*0b57cec5SDimitry Andric std::optional<ArrayRef<StringRef>> Env,
34*0b57cec5SDimitry Andric ArrayRef<std::optional<StringRef>> Redirects,
35*0b57cec5SDimitry Andric unsigned SecondsToWait, unsigned MemoryLimit,
36*0b57cec5SDimitry Andric std::string *ErrMsg, bool *ExecutionFailed,
37*0b57cec5SDimitry Andric std::optional<ProcessStatistics> *ProcStat,
38*0b57cec5SDimitry Andric BitVector *AffinityMask) {
39*0b57cec5SDimitry Andric assert(Redirects.empty() || Redirects.size() == 3);
40*0b57cec5SDimitry Andric ProcessInfo PI;
41*0b57cec5SDimitry Andric if (Execute(PI, Program, Args, Env, Redirects, MemoryLimit, ErrMsg,
42*0b57cec5SDimitry Andric AffinityMask)) {
43*0b57cec5SDimitry Andric if (ExecutionFailed)
44*0b57cec5SDimitry Andric *ExecutionFailed = false;
45*0b57cec5SDimitry Andric ProcessInfo Result = Wait(
46*0b57cec5SDimitry Andric PI, SecondsToWait == 0 ? std::nullopt : std::optional(SecondsToWait),
47*0b57cec5SDimitry Andric ErrMsg, ProcStat);
48*0b57cec5SDimitry Andric return Result.ReturnCode;
49*0b57cec5SDimitry Andric }
50*0b57cec5SDimitry Andric
51*0b57cec5SDimitry Andric if (ExecutionFailed)
52*0b57cec5SDimitry Andric *ExecutionFailed = true;
53*0b57cec5SDimitry Andric
54*0b57cec5SDimitry Andric return -1;
55*0b57cec5SDimitry Andric }
56*0b57cec5SDimitry Andric
ExecuteNoWait(StringRef Program,ArrayRef<StringRef> Args,std::optional<ArrayRef<StringRef>> Env,ArrayRef<std::optional<StringRef>> Redirects,unsigned MemoryLimit,std::string * ErrMsg,bool * ExecutionFailed,BitVector * AffinityMask)57*0b57cec5SDimitry Andric ProcessInfo sys::ExecuteNoWait(StringRef Program, ArrayRef<StringRef> Args,
58*0b57cec5SDimitry Andric std::optional<ArrayRef<StringRef>> Env,
59*0b57cec5SDimitry Andric ArrayRef<std::optional<StringRef>> Redirects,
60*0b57cec5SDimitry Andric unsigned MemoryLimit, std::string *ErrMsg,
61*0b57cec5SDimitry Andric bool *ExecutionFailed, BitVector *AffinityMask) {
62*0b57cec5SDimitry Andric assert(Redirects.empty() || Redirects.size() == 3);
63*0b57cec5SDimitry Andric ProcessInfo PI;
64*0b57cec5SDimitry Andric if (ExecutionFailed)
65*0b57cec5SDimitry Andric *ExecutionFailed = false;
66*0b57cec5SDimitry Andric if (!Execute(PI, Program, Args, Env, Redirects, MemoryLimit, ErrMsg,
67*0b57cec5SDimitry Andric AffinityMask))
68*0b57cec5SDimitry Andric if (ExecutionFailed)
69*0b57cec5SDimitry Andric *ExecutionFailed = true;
70*0b57cec5SDimitry Andric
71*0b57cec5SDimitry Andric return PI;
72*0b57cec5SDimitry Andric }
73*0b57cec5SDimitry Andric
commandLineFitsWithinSystemLimits(StringRef Program,ArrayRef<const char * > Args)74*0b57cec5SDimitry Andric bool sys::commandLineFitsWithinSystemLimits(StringRef Program,
75*0b57cec5SDimitry Andric ArrayRef<const char *> Args) {
76*0b57cec5SDimitry Andric SmallVector<StringRef, 8> StringRefArgs;
77*0b57cec5SDimitry Andric StringRefArgs.reserve(Args.size());
78*0b57cec5SDimitry Andric for (const char *A : Args)
79*0b57cec5SDimitry Andric StringRefArgs.emplace_back(A);
80*0b57cec5SDimitry Andric return commandLineFitsWithinSystemLimits(Program, StringRefArgs);
81*0b57cec5SDimitry Andric }
82*0b57cec5SDimitry Andric
printArg(raw_ostream & OS,StringRef Arg,bool Quote)83 void sys::printArg(raw_ostream &OS, StringRef Arg, bool Quote) {
84 const bool Escape = Arg.find_first_of(" \"\\$") != StringRef::npos;
85
86 if (!Quote && !Escape) {
87 OS << Arg;
88 return;
89 }
90
91 // Quote and escape. This isn't really complete, but good enough.
92 OS << '"';
93 for (const auto c : Arg) {
94 if (c == '"' || c == '\\' || c == '$')
95 OS << '\\';
96 OS << c;
97 }
98 OS << '"';
99 }
100
101 // Include the platform-specific parts of this class.
102 #ifdef LLVM_ON_UNIX
103 #include "Unix/Program.inc"
104 #endif
105 #ifdef _WIN32
106 #include "Windows/Program.inc"
107 #endif
108