1 /*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "src/profiling/symbolizer/subprocess.h"
18
19 #if !PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
20
21 #include <signal.h>
22 #include <sys/types.h>
23 #include <sys/wait.h>
24 #include <unistd.h>
25
26 #include "perfetto/ext/base/utils.h"
27
28 namespace perfetto {
29 namespace profiling {
30
Subprocess(const std::string & file,std::vector<std::string> args)31 Subprocess::Subprocess(const std::string& file, std::vector<std::string> args)
32 : input_pipe_(base::Pipe::Create(base::Pipe::kBothBlock)),
33 output_pipe_(base::Pipe::Create(base::Pipe::kBothBlock)) {
34 std::vector<char*> c_str_args;
35 c_str_args.reserve(args.size());
36 for (std::string& arg : args)
37 c_str_args.push_back(&(arg[0]));
38 c_str_args.push_back(nullptr);
39
40 if ((pid_ = fork()) == 0) {
41 // Child
42 PERFETTO_CHECK(dup2(*input_pipe_.rd, STDIN_FILENO) != -1);
43 PERFETTO_CHECK(dup2(*output_pipe_.wr, STDOUT_FILENO) != -1);
44 input_pipe_.wr.reset();
45 output_pipe_.rd.reset();
46 if (execvp(file.c_str(), c_str_args.data()) == -1)
47 PERFETTO_FATAL("Failed to exec %s", file.c_str());
48 }
49 PERFETTO_CHECK(pid_ != -1);
50 input_pipe_.rd.reset();
51 output_pipe_.wr.reset();
52 }
53
~Subprocess()54 Subprocess::~Subprocess() {
55 if (pid_ != -1) {
56 kill(pid_, SIGKILL);
57 int wstatus;
58 PERFETTO_EINTR(waitpid(pid_, &wstatus, 0));
59 }
60 }
61
Write(const char * buffer,size_t size)62 int64_t Subprocess::Write(const char* buffer, size_t size) {
63 if (!input_pipe_.wr) {
64 return -1;
65 }
66 return PERFETTO_EINTR(write(input_pipe_.wr.get(), buffer, size));
67 }
68
Read(char * buffer,size_t size)69 int64_t Subprocess::Read(char* buffer, size_t size) {
70 if (!output_pipe_.rd) {
71 return -1;
72 }
73 return PERFETTO_EINTR(read(output_pipe_.rd.get(), buffer, size));
74 }
75
76 } // namespace profiling
77 } // namespace perfetto
78
79 #endif // !PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
80