Mike Frysinger | c52bd4c | 2020-03-18 05:40:25 -0400 | [diff] [blame] | 1 | // Copyright 2020 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "debugd/src/ipaddrs_tool.h" |
| 6 | |
| 7 | #include <base/strings/string_number_conversions.h> |
| 8 | #include <base/strings/string_util.h> |
| 9 | #include <base/strings/stringprintf.h> |
Simon Glass | 2b1da09 | 2020-05-21 12:24:16 -0600 | [diff] [blame] | 10 | #include <brillo/process/process.h> |
Mike Frysinger | c52bd4c | 2020-03-18 05:40:25 -0400 | [diff] [blame] | 11 | |
| 12 | #include "debugd/src/process_with_output.h" |
| 13 | |
| 14 | namespace debugd { |
| 15 | |
| 16 | namespace { |
| 17 | |
| 18 | const char kIpTool[] = "/bin/ip"; |
| 19 | |
| 20 | bool RunOneIPCommand(std::vector<std::string>* result, |
| 21 | const std::vector<std::string>& argv) { |
| 22 | ProcessWithOutput p; |
| 23 | if (!p.Init()) { |
| 24 | result->push_back("[ ProcessWithOutput::Init() failed ]"); |
| 25 | return false; |
| 26 | } |
| 27 | |
| 28 | p.AddArg(kIpTool); |
| 29 | for (const auto& arg : argv) { |
| 30 | p.AddArg(arg); |
| 31 | } |
| 32 | |
| 33 | if (p.Run()) { |
| 34 | result->push_back("[ ProcessWithOutput::Run() failed ]"); |
| 35 | return false; |
| 36 | } |
| 37 | |
| 38 | p.GetOutputLines(result); |
| 39 | |
| 40 | // GetOutputLines() overwrites |result|, so the heading needs to be |
| 41 | // inserted afterward. |
| 42 | result->insert( |
| 43 | result->begin(), |
| 44 | base::StringPrintf("[ ip %s ]", base::JoinString(argv, " ").c_str())); |
| 45 | return true; |
| 46 | } |
| 47 | |
| 48 | } // namespace |
| 49 | |
| 50 | std::vector<std::string> IpAddrsTool::GetIpAddresses( |
| 51 | const brillo::VariantDictionary& options) { |
| 52 | std::vector<std::string> full_result, cmd_result; |
| 53 | |
| 54 | std::string ip_version = "-4"; |
| 55 | if (brillo::GetVariantValueOrDefault<bool>(options, "v6")) |
| 56 | ip_version = "-6"; |
| 57 | |
| 58 | RunOneIPCommand(&full_result, {ip_version, "addr", "show"}); |
| 59 | |
| 60 | return full_result; |
| 61 | } |
| 62 | |
| 63 | } // namespace debugd |