chromium/third_party/fuzztest/src/fuzztest/internal/subprocess.cc

// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "./fuzztest/internal/subprocess.h"

#include <cerrno>
#include <csignal>
#include <cstdlib>
#include <cstring>
#include <variant>

#if !defined(_MSC_VER)
#include <fcntl.h>
#include <poll.h>
#include <spawn.h>
#include <sys/wait.h>
#include <unistd.h>
#endif  // !defined(_MSC_VER)

#include <future>
#include <string>
#include <vector>

#include "absl/container/flat_hash_map.h"
#include "absl/strings/str_cat.h"
#include "absl/time/clock.h"
#include "absl/time/time.h"
#include "./fuzztest/internal/logging.h"

namespace fuzztest::internal {

#if !defined(_MSC_VER) && !(defined(__ANDROID_MIN_SDK_VERSION__) && \
                            __ANDROID_MIN_SDK_VERSION__ < 28)

TerminationStatus::TerminationStatus(int status) :{}

bool TerminationStatus::Exited() const {}

bool TerminationStatus::Signaled() const {}

std::variant<ExitCodeT, SignalT> TerminationStatus::Status() const {}

// Helper class for running commands in a subprocess.
class SubProcess {};

// Creates parent/child pipes for piping stdout/stderr from child to parent.
void SubProcess::CreatePipes() {}

void SubProcess::CloseChildPipes() {}

void SubProcess::CloseParentPipes() {}

// Create file actions, which specify file-related actions to be performed in
// the child between the fork() and exec() steps.
posix_spawn_file_actions_t SubProcess::CreateChildFileActions() {}

// Do fork() and exec() in one step, using posix_spawnp().
pid_t SubProcess::StartChild(
    const std::vector<std::string>& command_line,
    const absl::flat_hash_map<std::string, std::string>& environment) {}

static bool ShouldRetry(int e) {}

void SubProcess::ReadChildOutput(std::string* stdout_output,
                                 std::string* stderr_output) {}

namespace {

int Wait(pid_t pid) {}

// TODO(lszekeres): Consider optimizing this further by eliminating polling.
// Could potentially be done using pselect() to wait for SIGCHLD with a timeout.
// I.e., by setting all args to null, except timeout with a sigmask for SIGCHLD.
int WaitWithTimeout(pid_t pid, absl::Duration timeout) {}

}  // anonymous namespace

RunResults SubProcess::Run(
    const std::vector<std::string>& command_line,
    const absl::flat_hash_map<std::string, std::string>& environment,
    absl::Duration timeout) {}

#endif  // !defined(_MSC_VER) && !(defined(__ANDROID_MIN_SDK_VERSION__) &&
        // __ANDROID_MIN_SDK_VERSION__ < 28)

RunResults RunCommand(
    const std::vector<std::string>& command_line,
    const absl::flat_hash_map<std::string, std::string>& environment,
    absl::Duration timeout) {}

}  // namespace fuzztest::internal