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