blob: 6ea060740ab9a3710dce6f30a448d4bad48b9f8e [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
Gediminas Ramanauskas2f0b8852013-03-14 13:52:32 -070022std::string StorageTool::Smartctl(const std::string& option,
Ben Chana0011d82014-05-13 00:19:29 -070023 DBus::Error* error) {
Ben Chan297c3c22013-07-17 17:34:12 -070024 std::string path;
25 if (!SandboxedProcess::GetHelperPath("storage", &path))
Gediminas Ramanauskas2f0b8852013-03-14 13:52:32 -070026 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
58std::string StorageTool::Start(const DBus::FileDescriptor& outfd,
Ben Chana0011d82014-05-13 00:19:29 -070059 DBus::Error* error) {
Gediminas Ramanauskas2f0b8852013-03-14 13:52:32 -070060 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 Chana0011d82014-05-13 00:19:29 -070074} // namespace debugd