blob: ddeac36c63fb09602bddb4a168983946611f3282 [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
10namespace debugd {
11
12const char* kSmartctl = "/usr/sbin/smartctl";
13const char* kBadblocks = "/sbin/badblocks";
14const char* kDevice = "/dev/sda";
15
16StorageTool::StorageTool() { }
17StorageTool::~StorageTool() { }
18
19std::string StorageTool::Smartctl(const std::string& option,
20 DBus::Error& error) {
21 char *envvar = getenv("DEBUGD_HELPERS");
22 std::string path = StringPrintf("%s/storage", envvar ? envvar
23 : "/usr/libexec/debugd/helpers");
24 if (path.length() > PATH_MAX)
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
57std::string StorageTool::Start(const DBus::FileDescriptor& outfd,
58 DBus::Error& error) {
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
73}; // namespace debugd