| /* | |
| * Copyright (C) 2020 The Android Open Source Project | |
| * | |
| * 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 "src/trace_processor/util/symbolizer/subprocess.h" | |
| #if !PERFETTO_BUILDFLAG(PERFETTO_OS_WIN) | |
| #include <signal.h> | |
| #include <sys/types.h> | |
| #include <sys/wait.h> | |
| #include <unistd.h> | |
| #include <algorithm> | |
| #include <cstring> | |
| #include "perfetto/ext/base/utils.h" | |
| extern "C" char** environ; | |
| namespace perfetto { | |
| namespace profiling { | |
| Subprocess::Subprocess(const std::string& file, | |
| std::vector<std::string> args, | |
| const std::vector<std::string>& excluded_env) | |
| : input_pipe_(base::Pipe::Create(base::Pipe::kBothBlock)), | |
| output_pipe_(base::Pipe::Create(base::Pipe::kBothBlock)) { | |
| std::vector<char*> c_str_args; | |
| c_str_args.reserve(args.size()); | |
| for (std::string& arg : args) | |
| c_str_args.push_back(&(arg[0])); | |
| c_str_args.push_back(nullptr); | |
| // Build the environment before fork; the child only redirects pointers. | |
| std::vector<char*> environment; | |
| for (char** entry = environ; *entry; ++entry) { | |
| auto excluded = [entry](const std::string& name) { | |
| return strncmp(*entry, name.c_str(), name.size()) == 0 && | |
| (*entry)[name.size()] == '='; | |
| }; | |
| if (std::none_of(excluded_env.begin(), excluded_env.end(), excluded)) | |
| environment.push_back(*entry); | |
| } | |
| environment.push_back(nullptr); | |
| if ((pid_ = fork()) == 0) { | |
| environ = environment.data(); | |
| // Child | |
| PERFETTO_CHECK(dup2(*input_pipe_.rd, STDIN_FILENO) != -1); | |
| PERFETTO_CHECK(dup2(*output_pipe_.wr, STDOUT_FILENO) != -1); | |
| input_pipe_.wr.reset(); | |
| output_pipe_.rd.reset(); | |
| execvp(file.c_str(), c_str_args.data()); | |
| _exit(127); | |
| } | |
| PERFETTO_CHECK(pid_ != -1); | |
| // Keep input_pipe_.rd open to avoid SIGPIPE if the process exits. | |
| output_pipe_.wr.reset(); | |
| } | |
| Subprocess::~Subprocess() { | |
| if (pid_ != -1) { | |
| kill(pid_, SIGKILL); | |
| int wstatus; | |
| PERFETTO_EINTR(waitpid(pid_, &wstatus, 0)); | |
| } | |
| } | |
| int64_t Subprocess::Write(const char* buffer, size_t size) { | |
| if (!input_pipe_.wr) { | |
| return -1; | |
| } | |
| return PERFETTO_EINTR(write(input_pipe_.wr.get(), buffer, size)); | |
| } | |
| int64_t Subprocess::Read(char* buffer, size_t size) { | |
| if (!output_pipe_.rd) { | |
| return -1; | |
| } | |
| int64_t res = PERFETTO_EINTR(read(output_pipe_.rd.get(), buffer, size)); | |
| if (res == 0) { | |
| // The process exited: stop writes, which could otherwise block forever. | |
| input_pipe_.wr.reset(); | |
| input_pipe_.rd.reset(); | |
| } | |
| return res; | |
| } | |
| } // namespace profiling | |
| } // namespace perfetto | |
| #endif // !PERFETTO_BUILDFLAG(PERFETTO_OS_WIN) |