blob: 980f5f605a9b30124433ab369d3eabb7291fc441 [file] [log] [blame]
Gediminas Ramanauskas2f0b8852013-03-14 13:52:32 -07001// 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 Vakulenko262be3f2014-07-30 15:25:50 -07005#include "debugd/src/storage_tool.h"
Gediminas Ramanauskas2f0b8852013-03-14 13:52:32 -07006
Alex Vakulenko262be3f2014-07-30 15:25:50 -07007#include "debugd/src/process_with_id.h"
8#include "debugd/src/process_with_output.h"
Gediminas Ramanauskas2f0b8852013-03-14 13:52:32 -07009
Ben Chan55903dd2014-04-24 00:29:04 -070010using base::StringPrintf;
11
Gediminas Ramanauskas2f0b8852013-03-14 13:52:32 -070012namespace debugd {
13
Ben Chanaf125862017-02-08 23:11:18 -080014namespace {
15
16const char kSmartctl[] = "/usr/sbin/smartctl";
17const char kBadblocks[] = "/sbin/badblocks";
18const char kDevice[] = "/dev/sda";
19
20} // namespace
Gediminas Ramanauskas2f0b8852013-03-14 13:52:32 -070021
Eric Carusoc93a15c2017-04-24 16:15:12 -070022std::string StorageTool::Smartctl(const std::string& option) {
Ben Chan297c3c22013-07-17 17:34:12 -070023 std::string path;
24 if (!SandboxedProcess::GetHelperPath("storage", &path))
Gediminas Ramanauskas2f0b8852013-03-14 13:52:32 -070025 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
Eric Carusoc93a15c2017-04-24 16:15:12 -070057std::string StorageTool::Start(const DBus::FileDescriptor& outfd) {
Gediminas Ramanauskas2f0b8852013-03-14 13:52:32 -070058 ProcessWithId* p = CreateProcess(false);
59 if (!p)
60 return "";
61
62 p->AddArg(kBadblocks);
63 p->AddArg("-sv");
64 p->AddArg(kDevice);
65 p->BindFd(outfd.get(), STDOUT_FILENO);
66 p->BindFd(outfd.get(), STDERR_FILENO);
67 LOG(INFO) << "badblocks: running process id: " << p->id();
68 p->Start();
69 return p->id();
70}
71
Ben Chana0011d82014-05-13 00:19:29 -070072} // namespace debugd