Primiano Tucci | 6aa3b75 | 2021-01-05 14:22:43 +0100 | [diff] [blame] | 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 "perfetto/ext/base/subprocess.h" |
| 18 | |
| 19 | #include <tuple> |
| 20 | |
| 21 | // This file contains only the common bits (ctors / dtors / move operators). |
| 22 | // The rest lives in subprocess_posix.cc and subprocess_windows.cc. |
| 23 | |
| 24 | namespace perfetto { |
| 25 | namespace base { |
| 26 | |
| 27 | Subprocess::Args::Args(Args&&) noexcept = default; |
| 28 | Subprocess::Args& Subprocess::Args::operator=(Args&&) = default; |
| 29 | |
| 30 | Subprocess::Subprocess(std::initializer_list<std::string> a) |
| 31 | : args(a), s_(new MovableState()) {} |
| 32 | |
| 33 | Subprocess::Subprocess(Subprocess&& other) noexcept { |
| 34 | static_assert(sizeof(Subprocess) == |
| 35 | sizeof(std::tuple<std::unique_ptr<MovableState>, Args>), |
| 36 | "base::Subprocess' move ctor needs updating"); |
| 37 | s_ = std::move(other.s_); |
| 38 | args = std::move(other.args); |
| 39 | |
| 40 | // Reset the state of the moved-from object. |
| 41 | other.s_.reset(new MovableState()); |
| 42 | other.~Subprocess(); |
| 43 | new (&other) Subprocess(); |
| 44 | } |
| 45 | |
| 46 | Subprocess& Subprocess::operator=(Subprocess&& other) { |
| 47 | this->~Subprocess(); |
| 48 | new (this) Subprocess(std::move(other)); |
| 49 | return *this; |
| 50 | } |
| 51 | |
| 52 | Subprocess::~Subprocess() { |
| 53 | if (s_->status == kRunning) |
| 54 | KillAndWaitForTermination(); |
| 55 | } |
| 56 | |
| 57 | bool Subprocess::Call(int timeout_ms) { |
| 58 | PERFETTO_CHECK(s_->status == kNotStarted); |
| 59 | Start(); |
| 60 | |
| 61 | if (!Wait(timeout_ms)) { |
| 62 | s_->timed_out = true; |
| 63 | KillAndWaitForTermination(kTimeoutSignal); |
| 64 | } |
| 65 | PERFETTO_DCHECK(s_->status != kRunning); |
| 66 | return s_->status == kTerminated && s_->returncode == 0; |
| 67 | } |
| 68 | |
| 69 | std::string Subprocess::Args::GetCmdString() const { |
| 70 | std::string str; |
| 71 | for (size_t i = 0; i < exec_cmd.size(); i++) { |
| 72 | str += i > 0 ? " \"" : ""; |
| 73 | str += exec_cmd[i]; |
| 74 | str += i > 0 ? "\"" : ""; |
| 75 | } |
| 76 | return str; |
| 77 | } |
| 78 | |
| 79 | } // namespace base |
| 80 | } // namespace perfetto |