Gediminas Ramanauskas | 2f0b885 | 2013-03-14 13:52:32 -0700 | [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 | |
| 5 | #include "storage_tool.h" |
| 6 | |
| 7 | #include "process_with_output.h" |
| 8 | #include "process_with_id.h" |
| 9 | |
Ben Chan | 55903dd | 2014-04-24 00:29:04 -0700 | [diff] [blame] | 10 | using base::StringPrintf; |
| 11 | |
Gediminas Ramanauskas | 2f0b885 | 2013-03-14 13:52:32 -0700 | [diff] [blame] | 12 | namespace debugd { |
| 13 | |
| 14 | const char* kSmartctl = "/usr/sbin/smartctl"; |
| 15 | const char* kBadblocks = "/sbin/badblocks"; |
| 16 | const char* kDevice = "/dev/sda"; |
| 17 | |
| 18 | StorageTool::StorageTool() { } |
| 19 | StorageTool::~StorageTool() { } |
| 20 | |
| 21 | std::string StorageTool::Smartctl(const std::string& option, |
Ben Chan | a0011d8 | 2014-05-13 00:19:29 -0700 | [diff] [blame^] | 22 | DBus::Error* error) { |
Ben Chan | 297c3c2 | 2013-07-17 17:34:12 -0700 | [diff] [blame] | 23 | std::string path; |
| 24 | if (!SandboxedProcess::GetHelperPath("storage", &path)) |
Gediminas Ramanauskas | 2f0b885 | 2013-03-14 13:52:32 -0700 | [diff] [blame] | 25 | return "<path too long>"; |
| 26 | |
| 27 | ProcessWithOutput process; |
| 28 | // Disabling sandboxing since smartctl requires higher privileges. |
| 29 | process.DisableSandbox(); |
| 30 | if (!process.Init()) |
| 31 | return "<process init failed>"; |
| 32 | |
| 33 | process.AddArg(kSmartctl); |
| 34 | |
| 35 | if (option == "abort_test") |
| 36 | process.AddArg("-X"); |
| 37 | if (option == "attributes") |
| 38 | process.AddArg("-A"); |
| 39 | if (option == "capabilities") |
| 40 | process.AddArg("-c"); |
| 41 | if (option == "error") |
| 42 | process.AddStringOption("-l", "error"); |
| 43 | if (option == "health") |
| 44 | process.AddArg("-H"); |
| 45 | if (option == "selftest") |
| 46 | process.AddStringOption("-l", "selftest"); |
| 47 | if (option == "short_test") |
| 48 | process.AddStringOption("-t", "short"); |
| 49 | |
| 50 | process.AddArg(kDevice); |
| 51 | process.Run(); |
| 52 | std::string output; |
| 53 | process.GetOutput(&output); |
| 54 | return output; |
| 55 | } |
| 56 | |
| 57 | std::string StorageTool::Start(const DBus::FileDescriptor& outfd, |
Ben Chan | a0011d8 | 2014-05-13 00:19:29 -0700 | [diff] [blame^] | 58 | DBus::Error* error) { |
Gediminas Ramanauskas | 2f0b885 | 2013-03-14 13:52:32 -0700 | [diff] [blame] | 59 | ProcessWithId* p = CreateProcess(false); |
| 60 | if (!p) |
| 61 | return ""; |
| 62 | |
| 63 | p->AddArg(kBadblocks); |
| 64 | p->AddArg("-sv"); |
| 65 | p->AddArg(kDevice); |
| 66 | p->BindFd(outfd.get(), STDOUT_FILENO); |
| 67 | p->BindFd(outfd.get(), STDERR_FILENO); |
| 68 | LOG(INFO) << "badblocks: running process id: " << p->id(); |
| 69 | p->Start(); |
| 70 | return p->id(); |
| 71 | } |
| 72 | |
Ben Chan | a0011d8 | 2014-05-13 00:19:29 -0700 | [diff] [blame^] | 73 | } // namespace debugd |