blob: fd99e8699645e1c9772f8d122b780cf07fd23291 [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
5#include "storage_tool.h"
6
7#include "process_with_output.h"
8#include "process_with_id.h"
9
Ben Chan55903dd2014-04-24 00:29:04 -070010using base::StringPrintf;
11
Gediminas Ramanauskas2f0b8852013-03-14 13:52:32 -070012namespace debugd {
13
14const char* kSmartctl = "/usr/sbin/smartctl";
15const char* kBadblocks = "/sbin/badblocks";
16const char* kDevice = "/dev/sda";
17
18StorageTool::StorageTool() { }
19StorageTool::~StorageTool() { }
20
21std::string StorageTool::Smartctl(const std::string& option,
Ben Chana0011d82014-05-13 00:19:29 -070022 DBus::Error* error) {
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
57std::string StorageTool::Start(const DBus::FileDescriptor& outfd,
Ben Chana0011d82014-05-13 00:19:29 -070058 DBus::Error* error) {
Gediminas Ramanauskas2f0b8852013-03-14 13:52:32 -070059 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 Chana0011d82014-05-13 00:19:29 -070073} // namespace debugd