Elly Jones | 0b6bd18 | 2012-02-14 18:24:22 -0500 | [diff] [blame] | 1 | // Copyright (c) 2012 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 | |
Brian Norris | ca4fc04 | 2018-04-03 00:24:26 -0700 | [diff] [blame] | 5 | #include <string> |
| 6 | |
Alex Vakulenko | 262be3f | 2014-07-30 15:25:50 -0700 | [diff] [blame] | 7 | #include "debugd/src/debug_logs_tool.h" |
Brian Norris | ca4fc04 | 2018-04-03 00:24:26 -0700 | [diff] [blame] | 8 | #include "debugd/src/log_tool.h" |
Elly Jones | 0b6bd18 | 2012-02-14 18:24:22 -0500 | [diff] [blame] | 9 | |
Ben Chan | cd8fda4 | 2014-09-05 08:21:06 -0700 | [diff] [blame] | 10 | #include <base/files/file_util.h> |
Yusuke Sato | 39acf51 | 2016-10-17 21:57:00 -0700 | [diff] [blame] | 11 | #include <base/files/scoped_temp_dir.h> |
| 12 | #include <base/logging.h> |
Simon Glass | 2b1da09 | 2020-05-21 12:24:16 -0600 | [diff] [blame] | 13 | #include <brillo/process/process.h> |
Elly Jones | 0b6bd18 | 2012-02-14 18:24:22 -0500 | [diff] [blame] | 14 | |
| 15 | namespace debugd { |
| 16 | |
Yusuke Sato | 39acf51 | 2016-10-17 21:57:00 -0700 | [diff] [blame] | 17 | namespace { |
| 18 | |
Brian Norris | ca4fc04 | 2018-04-03 00:24:26 -0700 | [diff] [blame] | 19 | constexpr char kFeedbackLogsDir[] = "feedback"; |
Yusuke Sato | 39acf51 | 2016-10-17 21:57:00 -0700 | [diff] [blame] | 20 | |
| 21 | constexpr char kTar[] = "/bin/tar"; |
| 22 | constexpr char kSystemLogs[] = "/var/log"; |
Pin-Yen Lin | 187e167 | 2020-02-17 18:22:42 +0800 | [diff] [blame] | 23 | constexpr char kCrashLogs[] = "/var/spool/crash"; |
Yusuke Sato | 39acf51 | 2016-10-17 21:57:00 -0700 | [diff] [blame] | 24 | |
Yusuke Sato | 39acf51 | 2016-10-17 21:57:00 -0700 | [diff] [blame] | 25 | } // namespace |
Elly Jones | 0b6bd18 | 2012-02-14 18:24:22 -0500 | [diff] [blame] | 26 | |
Tom Hughes | d6c2d39 | 2020-08-24 18:12:11 -0700 | [diff] [blame] | 27 | void DebugLogsTool::GetDebugLogs(bool is_compressed, const base::ScopedFD& fd) { |
Brian Norris | ca4fc04 | 2018-04-03 00:24:26 -0700 | [diff] [blame] | 28 | base::ScopedTempDir temp_dir; |
| 29 | if (!temp_dir.CreateUniqueTempDir()) { |
| 30 | PLOG(WARNING) << "Failed to create a temporary directory"; |
| 31 | return; |
| 32 | } |
Yusuke Sato | 39acf51 | 2016-10-17 21:57:00 -0700 | [diff] [blame] | 33 | |
Brian Norris | ca4fc04 | 2018-04-03 00:24:26 -0700 | [diff] [blame] | 34 | base::FilePath logs_path = temp_dir.GetPath().Append(kFeedbackLogsDir); |
| 35 | if (!base::CreateDirectory(logs_path)) { |
| 36 | PLOG(WARNING) << "Failed to create dir: " << logs_path.value(); |
| 37 | return; |
| 38 | } |
| 39 | |
| 40 | LogTool log_tool(bus_); |
| 41 | LogTool::LogMap logs = log_tool.GetAllDebugLogs(); |
| 42 | for (const auto& l : logs) { |
| 43 | const std::string& name = l.first; |
| 44 | const std::string& contents = l.second; |
| 45 | if (base::WriteFile(logs_path.Append(name), contents.data(), |
Tom Hughes | d6c2d39 | 2020-08-24 18:12:11 -0700 | [diff] [blame] | 46 | contents.size()) < 0) { |
Brian Norris | ca4fc04 | 2018-04-03 00:24:26 -0700 | [diff] [blame] | 47 | PLOG(WARNING) << "Failed to write file: " << name; |
| 48 | } |
Yusuke Sato | 39acf51 | 2016-10-17 21:57:00 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Alex Vakulenko | e769653 | 2015-10-16 16:27:29 -0700 | [diff] [blame] | 51 | brillo::ProcessImpl p; |
Jonathan Backer | 7b806fd | 2013-04-18 15:00:58 -0400 | [diff] [blame] | 52 | p.AddArg(kTar); |
| 53 | p.AddArg("-c"); |
Zelidrag Hornung | d407be0 | 2014-07-09 09:30:25 -0700 | [diff] [blame] | 54 | if (is_compressed) |
| 55 | p.AddArg("-z"); |
Brian Norris | ca4fc04 | 2018-04-03 00:24:26 -0700 | [diff] [blame] | 56 | p.AddArg("-C"); |
| 57 | p.AddArg(temp_dir.GetPath().value()); |
| 58 | p.AddArg(kFeedbackLogsDir); |
Jonathan Backer | 7b806fd | 2013-04-18 15:00:58 -0400 | [diff] [blame] | 59 | p.AddArg(kSystemLogs); |
Pin-Yen Lin | 187e167 | 2020-02-17 18:22:42 +0800 | [diff] [blame] | 60 | p.AddArg(kCrashLogs); |
Eric Caruso | 0b24188 | 2018-04-04 13:43:46 -0700 | [diff] [blame] | 61 | p.BindFd(fd.get(), STDOUT_FILENO); |
Jonathan Backer | 7b806fd | 2013-04-18 15:00:58 -0400 | [diff] [blame] | 62 | p.Run(); |
Elly Jones | 0b6bd18 | 2012-02-14 18:24:22 -0500 | [diff] [blame] | 63 | } |
| 64 | |
Ben Chan | a0011d8 | 2014-05-13 00:19:29 -0700 | [diff] [blame] | 65 | } // namespace debugd |