2016-04-06 04:45:04 +08:00
|
|
|
//===- llvm/Support/Unix/Program.cpp -----------------------------*- C++ -*-===//
|
2009-07-18 04:38:17 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2009-07-18 04:38:17 +08:00
|
|
|
//
|
2004-08-30 03:20:41 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the Unix specific portion of the Program class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//=== WARNING: Implementation here must contain only generic UNIX code that
|
|
|
|
//=== is guaranteed to work on *all* UNIX variants.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Unix.h"
|
2014-11-04 09:29:29 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2015-03-24 02:07:13 +08:00
|
|
|
#include "llvm/Config/config.h"
|
2013-02-04 15:03:24 +08:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2015-06-14 01:23:04 +08:00
|
|
|
#include "llvm/Support/Errc.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2015-03-24 02:07:13 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2018-06-13 01:43:52 +08:00
|
|
|
#include "llvm/Support/StringSaver.h"
|
2014-09-04 04:02:00 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2004-12-27 14:16:52 +08:00
|
|
|
#if HAVE_SYS_STAT_H
|
2004-08-30 03:20:41 +08:00
|
|
|
#include <sys/stat.h>
|
2004-12-27 14:16:52 +08:00
|
|
|
#endif
|
2007-02-17 03:11:07 +08:00
|
|
|
#if HAVE_SYS_RESOURCE_H
|
|
|
|
#include <sys/resource.h>
|
|
|
|
#endif
|
2016-04-06 04:45:04 +08:00
|
|
|
#if HAVE_SIGNAL_H
|
|
|
|
#include <signal.h>
|
|
|
|
#endif
|
2004-12-27 14:16:52 +08:00
|
|
|
#if HAVE_FCNTL_H
|
2004-08-30 03:20:41 +08:00
|
|
|
#include <fcntl.h>
|
|
|
|
#endif
|
2013-04-11 22:06:34 +08:00
|
|
|
#if HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
2010-04-18 12:14:37 +08:00
|
|
|
#ifdef HAVE_POSIX_SPAWN
|
|
|
|
#include <spawn.h>
|
2015-02-20 03:50:52 +08:00
|
|
|
|
2015-01-30 08:10:39 +08:00
|
|
|
#if defined(__APPLE__)
|
|
|
|
#include <TargetConditionals.h>
|
|
|
|
#endif
|
2015-02-20 03:50:52 +08:00
|
|
|
|
|
|
|
#if defined(__APPLE__) && !(defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE)
|
|
|
|
#define USE_NSGETENVIRON 1
|
|
|
|
#else
|
|
|
|
#define USE_NSGETENVIRON 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !USE_NSGETENVIRON
|
2010-04-18 15:07:48 +08:00
|
|
|
extern char **environ;
|
2010-04-18 17:16:04 +08:00
|
|
|
#else
|
|
|
|
#include <crt_externs.h> // _NSGetEnviron
|
2010-04-18 15:07:48 +08:00
|
|
|
#endif
|
2010-04-18 12:14:37 +08:00
|
|
|
#endif
|
2004-08-30 03:20:41 +08:00
|
|
|
|
|
|
|
namespace llvm {
|
2014-06-13 05:46:39 +08:00
|
|
|
|
2004-08-30 03:20:41 +08:00
|
|
|
using namespace sys;
|
|
|
|
|
2013-10-01 22:28:18 +08:00
|
|
|
ProcessInfo::ProcessInfo() : Pid(0), ReturnCode(0) {}
|
|
|
|
|
2014-11-04 09:29:29 +08:00
|
|
|
ErrorOr<std::string> sys::findProgramByName(StringRef Name,
|
|
|
|
ArrayRef<StringRef> Paths) {
|
|
|
|
assert(!Name.empty() && "Must have a name!");
|
|
|
|
// Use the given path verbatim if it contains any slashes; this matches
|
|
|
|
// the behavior of sh(1) and friends.
|
|
|
|
if (Name.find('/') != StringRef::npos)
|
|
|
|
return std::string(Name);
|
|
|
|
|
2014-12-02 08:52:01 +08:00
|
|
|
SmallVector<StringRef, 16> EnvironmentPaths;
|
|
|
|
if (Paths.empty())
|
|
|
|
if (const char *PathEnv = std::getenv("PATH")) {
|
|
|
|
SplitString(PathEnv, EnvironmentPaths, ":");
|
|
|
|
Paths = EnvironmentPaths;
|
|
|
|
}
|
2014-11-04 09:29:29 +08:00
|
|
|
|
|
|
|
for (auto Path : Paths) {
|
|
|
|
if (Path.empty())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Check to see if this first directory contains the executable...
|
|
|
|
SmallString<128> FilePath(Path);
|
|
|
|
sys::path::append(FilePath, Name);
|
|
|
|
if (sys::fs::can_execute(FilePath.c_str()))
|
|
|
|
return std::string(FilePath.str()); // Found the executable!
|
|
|
|
}
|
2015-06-14 01:23:04 +08:00
|
|
|
return errc::no_such_file_or_directory;
|
2014-11-04 09:29:29 +08:00
|
|
|
}
|
|
|
|
|
2017-09-14 01:03:37 +08:00
|
|
|
static bool RedirectIO(Optional<StringRef> Path, int FD, std::string* ErrMsg) {
|
2014-04-28 12:05:08 +08:00
|
|
|
if (!Path) // Noop
|
2008-06-12 18:47:18 +08:00
|
|
|
return false;
|
2013-06-15 03:38:45 +08:00
|
|
|
std::string File;
|
|
|
|
if (Path->empty())
|
2008-06-12 18:47:18 +08:00
|
|
|
// Redirect empty paths to /dev/null
|
|
|
|
File = "/dev/null";
|
|
|
|
else
|
2013-06-15 03:38:45 +08:00
|
|
|
File = *Path;
|
2004-12-20 02:00:44 +08:00
|
|
|
|
|
|
|
// Open the file
|
2013-06-15 03:38:45 +08:00
|
|
|
int InFD = open(File.c_str(), FD == 0 ? O_RDONLY : O_WRONLY|O_CREAT, 0666);
|
2004-12-20 02:00:44 +08:00
|
|
|
if (InFD == -1) {
|
2013-06-15 03:38:45 +08:00
|
|
|
MakeErrMsg(ErrMsg, "Cannot open file '" + File + "' for "
|
2009-04-21 04:50:13 +08:00
|
|
|
+ (FD == 0 ? "input" : "output"));
|
2006-08-21 14:02:44 +08:00
|
|
|
return true;
|
2004-12-20 02:00:44 +08:00
|
|
|
}
|
|
|
|
|
2006-08-21 14:02:44 +08:00
|
|
|
// Install it as the requested FD
|
2010-03-15 07:16:45 +08:00
|
|
|
if (dup2(InFD, FD) == -1) {
|
2006-08-21 14:02:44 +08:00
|
|
|
MakeErrMsg(ErrMsg, "Cannot dup2");
|
2010-03-15 07:16:45 +08:00
|
|
|
close(InFD);
|
2006-08-21 14:02:44 +08:00
|
|
|
return true;
|
|
|
|
}
|
2004-12-20 02:00:44 +08:00
|
|
|
close(InFD); // Close the original FD
|
2006-08-21 14:02:44 +08:00
|
|
|
return false;
|
2004-12-20 02:00:44 +08:00
|
|
|
}
|
|
|
|
|
2010-04-18 12:14:37 +08:00
|
|
|
#ifdef HAVE_POSIX_SPAWN
|
2016-04-06 04:45:04 +08:00
|
|
|
static bool RedirectIO_PS(const std::string *Path, int FD, std::string *ErrMsg,
|
|
|
|
posix_spawn_file_actions_t *FileActions) {
|
2014-04-28 12:05:08 +08:00
|
|
|
if (!Path) // Noop
|
2010-04-18 12:14:37 +08:00
|
|
|
return false;
|
2013-07-27 00:21:31 +08:00
|
|
|
const char *File;
|
2013-06-15 03:38:45 +08:00
|
|
|
if (Path->empty())
|
2010-04-18 12:14:37 +08:00
|
|
|
// Redirect empty paths to /dev/null
|
|
|
|
File = "/dev/null";
|
|
|
|
else
|
2013-07-27 00:21:31 +08:00
|
|
|
File = Path->c_str();
|
2010-04-18 17:19:41 +08:00
|
|
|
|
2013-06-15 03:38:45 +08:00
|
|
|
if (int Err = posix_spawn_file_actions_addopen(
|
2013-07-27 00:21:31 +08:00
|
|
|
FileActions, FD, File,
|
2013-06-15 03:38:45 +08:00
|
|
|
FD == 0 ? O_RDONLY : O_WRONLY | O_CREAT, 0666))
|
2010-04-18 12:14:37 +08:00
|
|
|
return MakeErrMsg(ErrMsg, "Cannot dup2", Err);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-04-06 04:45:04 +08:00
|
|
|
static void TimeOutHandler(int Sig) {
|
2009-11-09 04:55:48 +08:00
|
|
|
}
|
|
|
|
|
2017-09-07 00:28:33 +08:00
|
|
|
static void SetMemoryLimits(unsigned size) {
|
2010-02-12 08:37:46 +08:00
|
|
|
#if HAVE_SYS_RESOURCE_H && HAVE_GETRLIMIT && HAVE_SETRLIMIT
|
2007-02-17 03:11:07 +08:00
|
|
|
struct rlimit r;
|
|
|
|
__typeof__ (r.rlim_cur) limit = (__typeof__ (r.rlim_cur)) (size) * 1048576;
|
|
|
|
|
|
|
|
// Heap size
|
|
|
|
getrlimit (RLIMIT_DATA, &r);
|
|
|
|
r.rlim_cur = limit;
|
|
|
|
setrlimit (RLIMIT_DATA, &r);
|
2007-07-06 18:31:27 +08:00
|
|
|
#ifdef RLIMIT_RSS
|
2007-02-17 03:11:07 +08:00
|
|
|
// Resident set size.
|
|
|
|
getrlimit (RLIMIT_RSS, &r);
|
|
|
|
r.rlim_cur = limit;
|
|
|
|
setrlimit (RLIMIT_RSS, &r);
|
2007-04-23 15:22:51 +08:00
|
|
|
#endif
|
2013-02-04 15:03:24 +08:00
|
|
|
#endif
|
2007-02-17 03:11:07 +08:00
|
|
|
}
|
|
|
|
|
2016-04-06 04:45:04 +08:00
|
|
|
}
|
2013-06-13 04:58:35 +08:00
|
|
|
|
2018-06-13 01:43:52 +08:00
|
|
|
static std::vector<const char *>
|
|
|
|
toNullTerminatedCStringArray(ArrayRef<StringRef> Strings, StringSaver &Saver) {
|
|
|
|
std::vector<const char *> Result;
|
|
|
|
for (StringRef S : Strings)
|
|
|
|
Result.push_back(Saver.save(S).data());
|
|
|
|
Result.push_back(nullptr);
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool Execute(ProcessInfo &PI, StringRef Program,
|
|
|
|
ArrayRef<StringRef> Args, Optional<ArrayRef<StringRef>> Env,
|
|
|
|
ArrayRef<Optional<StringRef>> Redirects,
|
2017-09-07 00:28:33 +08:00
|
|
|
unsigned MemoryLimit, std::string *ErrMsg) {
|
2013-10-01 22:28:18 +08:00
|
|
|
if (!llvm::sys::fs::exists(Program)) {
|
|
|
|
if (ErrMsg)
|
|
|
|
*ErrMsg = std::string("Executable \"") + Program.str() +
|
|
|
|
std::string("\" doesn't exist!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-06-13 01:43:52 +08:00
|
|
|
BumpPtrAllocator Allocator;
|
|
|
|
StringSaver Saver(Allocator);
|
|
|
|
std::vector<const char *> ArgVector, EnvVector;
|
|
|
|
const char **Argv = nullptr;
|
|
|
|
const char **Envp = nullptr;
|
|
|
|
ArgVector = toNullTerminatedCStringArray(Args, Saver);
|
|
|
|
Argv = ArgVector.data();
|
|
|
|
if (Env) {
|
|
|
|
EnvVector = toNullTerminatedCStringArray(*Env, Saver);
|
|
|
|
Envp = EnvVector.data();
|
|
|
|
}
|
|
|
|
|
2010-04-18 12:14:37 +08:00
|
|
|
// If this OS has posix_spawn and there is no memory limit being implied, use
|
|
|
|
// posix_spawn. It is more efficient than fork/exec.
|
|
|
|
#ifdef HAVE_POSIX_SPAWN
|
2017-09-07 00:28:33 +08:00
|
|
|
if (MemoryLimit == 0) {
|
2011-03-20 23:52:24 +08:00
|
|
|
posix_spawn_file_actions_t FileActionsStore;
|
2014-04-28 12:05:08 +08:00
|
|
|
posix_spawn_file_actions_t *FileActions = nullptr;
|
2010-04-18 12:14:37 +08:00
|
|
|
|
2013-07-27 00:21:31 +08:00
|
|
|
// If we call posix_spawn_file_actions_addopen we have to make sure the
|
2013-07-27 04:44:45 +08:00
|
|
|
// c strings we pass to it stay alive until the call to posix_spawn,
|
2013-07-27 00:21:31 +08:00
|
|
|
// so we copy any StringRefs into this variable.
|
|
|
|
std::string RedirectsStorage[3];
|
|
|
|
|
2017-09-14 01:03:37 +08:00
|
|
|
if (!Redirects.empty()) {
|
|
|
|
assert(Redirects.size() == 3);
|
2014-04-28 12:05:08 +08:00
|
|
|
std::string *RedirectsStr[3] = {nullptr, nullptr, nullptr};
|
2013-07-27 00:21:31 +08:00
|
|
|
for (int I = 0; I < 3; ++I) {
|
2017-09-07 00:28:33 +08:00
|
|
|
if (Redirects[I]) {
|
|
|
|
RedirectsStorage[I] = *Redirects[I];
|
2013-07-27 00:21:31 +08:00
|
|
|
RedirectsStr[I] = &RedirectsStorage[I];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-20 23:52:24 +08:00
|
|
|
FileActions = &FileActionsStore;
|
|
|
|
posix_spawn_file_actions_init(FileActions);
|
|
|
|
|
2010-04-18 12:14:37 +08:00
|
|
|
// Redirect stdin/stdout.
|
2013-07-27 00:21:31 +08:00
|
|
|
if (RedirectIO_PS(RedirectsStr[0], 0, ErrMsg, FileActions) ||
|
|
|
|
RedirectIO_PS(RedirectsStr[1], 1, ErrMsg, FileActions))
|
2010-04-18 12:14:37 +08:00
|
|
|
return false;
|
2017-09-14 01:03:37 +08:00
|
|
|
if (!Redirects[1] || !Redirects[2] || *Redirects[1] != *Redirects[2]) {
|
2010-04-18 12:14:37 +08:00
|
|
|
// Just redirect stderr
|
2013-07-27 00:21:31 +08:00
|
|
|
if (RedirectIO_PS(RedirectsStr[2], 2, ErrMsg, FileActions))
|
|
|
|
return false;
|
2010-10-29 03:32:53 +08:00
|
|
|
} else {
|
2010-04-18 12:14:37 +08:00
|
|
|
// If stdout and stderr should go to the same place, redirect stderr
|
|
|
|
// to the FD already open for stdout.
|
2011-03-20 23:52:24 +08:00
|
|
|
if (int Err = posix_spawn_file_actions_adddup2(FileActions, 1, 2))
|
2010-04-18 12:14:37 +08:00
|
|
|
return !MakeErrMsg(ErrMsg, "Can't redirect stderr to stdout", Err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-07 00:28:33 +08:00
|
|
|
if (!Envp)
|
2015-02-20 03:50:52 +08:00
|
|
|
#if !USE_NSGETENVIRON
|
2017-09-07 00:28:33 +08:00
|
|
|
Envp = const_cast<const char **>(environ);
|
2010-04-18 17:16:04 +08:00
|
|
|
#else
|
2010-04-19 23:55:10 +08:00
|
|
|
// environ is missing in dylibs.
|
2017-09-07 00:28:33 +08:00
|
|
|
Envp = const_cast<const char **>(*_NSGetEnviron());
|
2010-04-18 15:07:48 +08:00
|
|
|
#endif
|
|
|
|
|
2019-04-25 07:24:53 +08:00
|
|
|
constexpr int maxRetries = 8;
|
|
|
|
int retries = 0;
|
|
|
|
pid_t PID;
|
|
|
|
int Err;
|
|
|
|
do {
|
|
|
|
PID = 0; // Make Valgrind happy.
|
|
|
|
Err = posix_spawn(&PID, Program.str().c_str(), FileActions,
|
|
|
|
/*attrp*/ nullptr, const_cast<char **>(Argv),
|
|
|
|
const_cast<char **>(Envp));
|
|
|
|
} while (Err == EINTR && ++retries < maxRetries);
|
2010-10-29 03:32:53 +08:00
|
|
|
|
2011-03-20 23:52:24 +08:00
|
|
|
if (FileActions)
|
|
|
|
posix_spawn_file_actions_destroy(FileActions);
|
2010-04-18 12:14:37 +08:00
|
|
|
|
|
|
|
if (Err)
|
|
|
|
return !MakeErrMsg(ErrMsg, "posix_spawn failed", Err);
|
2010-10-29 03:32:53 +08:00
|
|
|
|
2013-10-01 22:28:18 +08:00
|
|
|
PI.Pid = PID;
|
2018-06-08 23:16:25 +08:00
|
|
|
PI.Process = PID;
|
2013-10-01 22:28:18 +08:00
|
|
|
|
2010-04-18 12:14:37 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
2010-10-29 03:32:53 +08:00
|
|
|
|
2004-08-30 03:20:41 +08:00
|
|
|
// Create a child process.
|
2004-12-20 02:00:44 +08:00
|
|
|
int child = fork();
|
|
|
|
switch (child) {
|
2011-04-15 13:18:47 +08:00
|
|
|
// An error occurred: Return to the caller.
|
2004-08-30 03:20:41 +08:00
|
|
|
case -1:
|
2006-08-21 14:02:44 +08:00
|
|
|
MakeErrMsg(ErrMsg, "Couldn't fork");
|
2009-07-19 05:43:12 +08:00
|
|
|
return false;
|
2004-08-30 03:20:41 +08:00
|
|
|
|
|
|
|
// Child process: Execute the program.
|
2004-12-14 12:18:51 +08:00
|
|
|
case 0: {
|
2004-12-20 02:00:44 +08:00
|
|
|
// Redirect file descriptors...
|
2017-09-14 01:03:37 +08:00
|
|
|
if (!Redirects.empty()) {
|
2008-06-12 20:53:35 +08:00
|
|
|
// Redirect stdin
|
2017-09-07 00:28:33 +08:00
|
|
|
if (RedirectIO(Redirects[0], 0, ErrMsg)) { return false; }
|
2008-06-12 20:53:35 +08:00
|
|
|
// Redirect stdout
|
2017-09-07 00:28:33 +08:00
|
|
|
if (RedirectIO(Redirects[1], 1, ErrMsg)) { return false; }
|
|
|
|
if (Redirects[1] && Redirects[2] && *Redirects[1] == *Redirects[2]) {
|
2008-06-12 20:53:35 +08:00
|
|
|
// If stdout and stderr should go to the same place, redirect stderr
|
|
|
|
// to the FD already open for stdout.
|
|
|
|
if (-1 == dup2(1,2)) {
|
|
|
|
MakeErrMsg(ErrMsg, "Can't redirect stderr to stdout");
|
2009-07-19 05:43:12 +08:00
|
|
|
return false;
|
2008-06-12 20:53:35 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Just redirect stderr
|
2017-09-07 00:28:33 +08:00
|
|
|
if (RedirectIO(Redirects[2], 2, ErrMsg)) { return false; }
|
2004-12-20 02:00:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-17 03:11:07 +08:00
|
|
|
// Set memory limits
|
2017-09-07 00:28:33 +08:00
|
|
|
if (MemoryLimit!=0) {
|
|
|
|
SetMemoryLimits(MemoryLimit);
|
2007-02-17 03:11:07 +08:00
|
|
|
}
|
2009-07-18 04:38:17 +08:00
|
|
|
|
2004-12-20 02:00:44 +08:00
|
|
|
// Execute!
|
2013-06-15 03:38:45 +08:00
|
|
|
std::string PathStr = Program;
|
2017-09-07 00:28:33 +08:00
|
|
|
if (Envp != nullptr)
|
2018-06-13 01:43:52 +08:00
|
|
|
execve(PathStr.c_str(), const_cast<char **>(Argv),
|
2017-09-07 00:28:33 +08:00
|
|
|
const_cast<char **>(Envp));
|
2006-06-10 04:43:11 +08:00
|
|
|
else
|
2018-06-13 01:43:52 +08:00
|
|
|
execv(PathStr.c_str(), const_cast<char **>(Argv));
|
2009-08-05 08:09:12 +08:00
|
|
|
// If the execve() failed, we should exit. Follow Unix protocol and
|
|
|
|
// return 127 if the executable was not found, and 126 otherwise.
|
|
|
|
// Use _exit rather than exit so that atexit functions and static
|
|
|
|
// object destructors cloned from the parent process aren't
|
|
|
|
// redundantly run, and so that any data buffered in stdio buffers
|
|
|
|
// cloned from the parent aren't redundantly written out.
|
|
|
|
_exit(errno == ENOENT ? 127 : 126);
|
2004-12-14 12:18:51 +08:00
|
|
|
}
|
2004-08-30 03:20:41 +08:00
|
|
|
|
|
|
|
// Parent process: Break out of the switch to do our processing.
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-10-01 22:28:18 +08:00
|
|
|
PI.Pid = child;
|
2018-06-08 23:16:25 +08:00
|
|
|
PI.Process = child;
|
2009-07-19 05:43:12 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-10-01 22:28:18 +08:00
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
ProcessInfo sys::Wait(const ProcessInfo &PI, unsigned SecondsToWait,
|
|
|
|
bool WaitUntilTerminates, std::string *ErrMsg) {
|
2004-12-20 02:00:44 +08:00
|
|
|
struct sigaction Act, Old;
|
2013-10-01 22:28:18 +08:00
|
|
|
assert(PI.Pid && "invalid pid to wait on, process not started?");
|
|
|
|
|
|
|
|
int WaitPidOptions = 0;
|
|
|
|
pid_t ChildPid = PI.Pid;
|
|
|
|
if (WaitUntilTerminates) {
|
|
|
|
SecondsToWait = 0;
|
|
|
|
} else if (SecondsToWait) {
|
|
|
|
// Install a timeout handler. The handler itself does nothing, but the
|
|
|
|
// simple fact of having a handler at all causes the wait below to return
|
|
|
|
// with EINTR, unlike if we used SIG_IGN.
|
2010-07-14 22:32:33 +08:00
|
|
|
memset(&Act, 0, sizeof(Act));
|
2009-11-09 04:55:48 +08:00
|
|
|
Act.sa_handler = TimeOutHandler;
|
2004-12-20 02:00:44 +08:00
|
|
|
sigemptyset(&Act.sa_mask);
|
|
|
|
sigaction(SIGALRM, &Act, &Old);
|
2013-10-01 22:28:18 +08:00
|
|
|
alarm(SecondsToWait);
|
|
|
|
} else if (SecondsToWait == 0)
|
|
|
|
WaitPidOptions = WNOHANG;
|
2004-12-20 02:00:44 +08:00
|
|
|
|
2004-08-30 03:20:41 +08:00
|
|
|
// Parent process: Wait for the child process to terminate.
|
|
|
|
int status;
|
2013-10-01 22:28:18 +08:00
|
|
|
ProcessInfo WaitResult;
|
2014-06-28 02:02:54 +08:00
|
|
|
|
|
|
|
do {
|
|
|
|
WaitResult.Pid = waitpid(ChildPid, &status, WaitPidOptions);
|
|
|
|
} while (WaitUntilTerminates && WaitResult.Pid == -1 && errno == EINTR);
|
|
|
|
|
2013-10-01 22:28:18 +08:00
|
|
|
if (WaitResult.Pid != PI.Pid) {
|
|
|
|
if (WaitResult.Pid == 0) {
|
|
|
|
// Non-blocking wait.
|
|
|
|
return WaitResult;
|
|
|
|
} else {
|
|
|
|
if (SecondsToWait && errno == EINTR) {
|
|
|
|
// Kill the child.
|
|
|
|
kill(PI.Pid, SIGKILL);
|
|
|
|
|
|
|
|
// Turn off the alarm and restore the signal handler
|
|
|
|
alarm(0);
|
2014-04-28 12:05:08 +08:00
|
|
|
sigaction(SIGALRM, &Old, nullptr);
|
2013-10-01 22:28:18 +08:00
|
|
|
|
|
|
|
// Wait for child to die
|
|
|
|
if (wait(&status) != ChildPid)
|
|
|
|
MakeErrMsg(ErrMsg, "Child timed out but wouldn't die");
|
|
|
|
else
|
|
|
|
MakeErrMsg(ErrMsg, "Child timed out", 0);
|
|
|
|
|
|
|
|
WaitResult.ReturnCode = -2; // Timeout detected
|
|
|
|
return WaitResult;
|
|
|
|
} else if (errno != EINTR) {
|
|
|
|
MakeErrMsg(ErrMsg, "Error waiting for child process");
|
|
|
|
WaitResult.ReturnCode = -1;
|
|
|
|
return WaitResult;
|
|
|
|
}
|
2004-12-20 02:00:44 +08:00
|
|
|
}
|
2013-10-01 22:28:18 +08:00
|
|
|
}
|
2004-12-20 02:00:44 +08:00
|
|
|
|
|
|
|
// We exited normally without timeout, so turn off the timer.
|
2013-10-01 22:28:18 +08:00
|
|
|
if (SecondsToWait && !WaitUntilTerminates) {
|
2004-12-20 02:00:44 +08:00
|
|
|
alarm(0);
|
2014-04-28 12:05:08 +08:00
|
|
|
sigaction(SIGALRM, &Old, nullptr);
|
2004-12-20 02:00:44 +08:00
|
|
|
}
|
2004-08-30 03:20:41 +08:00
|
|
|
|
2010-10-30 00:39:01 +08:00
|
|
|
// Return the proper exit status. Detect error conditions
|
|
|
|
// so we can return -1 for them and set ErrMsg informatively.
|
2005-12-23 04:00:16 +08:00
|
|
|
int result = 0;
|
2010-10-30 00:39:01 +08:00
|
|
|
if (WIFEXITED(status)) {
|
2005-12-23 04:00:16 +08:00
|
|
|
result = WEXITSTATUS(status);
|
2013-10-01 22:28:18 +08:00
|
|
|
WaitResult.ReturnCode = result;
|
|
|
|
|
2010-10-30 00:39:01 +08:00
|
|
|
if (result == 127) {
|
2010-10-30 01:20:42 +08:00
|
|
|
if (ErrMsg)
|
|
|
|
*ErrMsg = llvm::sys::StrError(ENOENT);
|
2013-10-01 22:28:18 +08:00
|
|
|
WaitResult.ReturnCode = -1;
|
|
|
|
return WaitResult;
|
2010-10-30 00:39:01 +08:00
|
|
|
}
|
|
|
|
if (result == 126) {
|
2010-10-30 01:20:42 +08:00
|
|
|
if (ErrMsg)
|
|
|
|
*ErrMsg = "Program could not be executed";
|
2013-10-01 22:28:18 +08:00
|
|
|
WaitResult.ReturnCode = -1;
|
|
|
|
return WaitResult;
|
2010-10-30 00:39:01 +08:00
|
|
|
}
|
|
|
|
} else if (WIFSIGNALED(status)) {
|
2010-10-30 01:20:42 +08:00
|
|
|
if (ErrMsg) {
|
2011-05-21 08:56:46 +08:00
|
|
|
*ErrMsg = strsignal(WTERMSIG(status));
|
2005-12-23 04:00:16 +08:00
|
|
|
#ifdef WCOREDUMP
|
2010-10-30 01:20:42 +08:00
|
|
|
if (WCOREDUMP(status))
|
|
|
|
*ErrMsg += " (core dumped)";
|
2005-12-23 04:00:16 +08:00
|
|
|
#endif
|
2010-10-30 01:20:42 +08:00
|
|
|
}
|
2011-05-21 08:56:46 +08:00
|
|
|
// Return a special value to indicate that the process received an unhandled
|
|
|
|
// signal during execution as opposed to failing to execute.
|
2013-10-01 22:28:18 +08:00
|
|
|
WaitResult.ReturnCode = -2;
|
2010-10-30 00:39:01 +08:00
|
|
|
}
|
2013-10-01 22:28:18 +08:00
|
|
|
return WaitResult;
|
2004-08-30 03:20:41 +08:00
|
|
|
}
|
|
|
|
|
2018-05-16 14:43:27 +08:00
|
|
|
std::error_code sys::ChangeStdinToBinary() {
|
2006-06-08 07:18:34 +08:00
|
|
|
// Do nothing, as Unix doesn't differentiate between text and binary.
|
2018-05-16 14:43:27 +08:00
|
|
|
return std::error_code();
|
2006-06-08 07:18:34 +08:00
|
|
|
}
|
|
|
|
|
2018-05-16 14:43:27 +08:00
|
|
|
std::error_code sys::ChangeStdoutToBinary() {
|
2006-06-08 07:18:34 +08:00
|
|
|
// Do nothing, as Unix doesn't differentiate between text and binary.
|
2018-05-16 14:43:27 +08:00
|
|
|
return std::error_code();
|
2006-06-08 07:18:34 +08:00
|
|
|
}
|
|
|
|
|
2014-09-04 04:02:00 +08:00
|
|
|
std::error_code
|
|
|
|
llvm::sys::writeFileWithEncoding(StringRef FileName, StringRef Contents,
|
|
|
|
WindowsEncodingMethod Encoding /*unused*/) {
|
|
|
|
std::error_code EC;
|
|
|
|
llvm::raw_fd_ostream OS(FileName, EC, llvm::sys::fs::OpenFlags::F_Text);
|
|
|
|
|
|
|
|
if (EC)
|
|
|
|
return EC;
|
|
|
|
|
|
|
|
OS << Contents;
|
|
|
|
|
|
|
|
if (OS.has_error())
|
2015-06-14 01:23:04 +08:00
|
|
|
return make_error_code(errc::io_error);
|
2014-09-04 04:02:00 +08:00
|
|
|
|
|
|
|
return EC;
|
|
|
|
}
|
|
|
|
|
2017-09-07 00:28:33 +08:00
|
|
|
bool llvm::sys::commandLineFitsWithinSystemLimits(StringRef Program,
|
2018-06-11 04:57:14 +08:00
|
|
|
ArrayRef<StringRef> Args) {
|
2013-04-11 22:06:34 +08:00
|
|
|
static long ArgMax = sysconf(_SC_ARG_MAX);
|
2018-06-08 23:19:16 +08:00
|
|
|
// POSIX requires that _POSIX_ARG_MAX is 4096, which is the lowest possible
|
|
|
|
// value for ARG_MAX on a POSIX compliant system.
|
|
|
|
static long ArgMin = _POSIX_ARG_MAX;
|
|
|
|
|
|
|
|
// This the same baseline used by xargs.
|
|
|
|
long EffectiveArgMax = 128 * 1024;
|
|
|
|
|
|
|
|
if (EffectiveArgMax > ArgMax)
|
|
|
|
EffectiveArgMax = ArgMax;
|
|
|
|
else if (EffectiveArgMax < ArgMin)
|
|
|
|
EffectiveArgMax = ArgMin;
|
2013-04-11 22:06:34 +08:00
|
|
|
|
|
|
|
// System says no practical limit.
|
|
|
|
if (ArgMax == -1)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Conservatively account for space required by environment variables.
|
2018-06-08 23:19:16 +08:00
|
|
|
long HalfArgMax = EffectiveArgMax / 2;
|
2013-04-11 22:06:34 +08:00
|
|
|
|
2016-01-06 03:56:12 +08:00
|
|
|
size_t ArgLength = Program.size() + 1;
|
2018-06-11 04:57:14 +08:00
|
|
|
for (StringRef Arg : Args) {
|
2017-06-21 04:51:51 +08:00
|
|
|
// Ensure that we do not exceed the MAX_ARG_STRLEN constant on Linux, which
|
|
|
|
// does not have a constant unlike what the man pages would have you
|
|
|
|
// believe. Since this limit is pretty high, perform the check
|
|
|
|
// unconditionally rather than trying to be aggressive and limiting it to
|
|
|
|
// Linux only.
|
2018-06-11 04:57:14 +08:00
|
|
|
if (Arg.size() >= (32 * 4096))
|
2017-06-21 04:51:51 +08:00
|
|
|
return false;
|
|
|
|
|
2018-06-11 04:57:14 +08:00
|
|
|
ArgLength += Arg.size() + 1;
|
2014-08-26 06:53:21 +08:00
|
|
|
if (ArgLength > size_t(HalfArgMax)) {
|
2013-04-11 22:06:34 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2017-06-21 04:51:51 +08:00
|
|
|
|
2013-04-11 22:06:34 +08:00
|
|
|
return true;
|
|
|
|
}
|
2016-04-06 04:45:04 +08:00
|
|
|
}
|