blob: 693d8cf3ba53b7be328c73396f4eb1002b3fd22d [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,
22 DBus::Error& error) {
23 char *envvar = getenv("DEBUGD_HELPERS");
24 std::string path = StringPrintf("%s/storage", envvar ? envvar
25 : "/usr/libexec/debugd/helpers");
26 if (path.length() > PATH_MAX)
27 return "<path too long>";
28
29 ProcessWithOutput process;
30 // Disabling sandboxing since smartctl requires higher privileges.
31 process.DisableSandbox();
32 if (!process.Init())
33 return "<process init failed>";
34
35 process.AddArg(kSmartctl);
36
37 if (option == "abort_test")
38 process.AddArg("-X");
39 if (option == "attributes")
40 process.AddArg("-A");
41 if (option == "capabilities")
42 process.AddArg("-c");
43 if (option == "error")
44 process.AddStringOption("-l", "error");
45 if (option == "health")
46 process.AddArg("-H");
47 if (option == "selftest")
48 process.AddStringOption("-l", "selftest");
49 if (option == "short_test")
50 process.AddStringOption("-t", "short");
51
52 process.AddArg(kDevice);
53 process.Run();
54 std::string output;
55 process.GetOutput(&output);
56 return output;
57}
58
59std::string StorageTool::Start(const DBus::FileDescriptor& outfd,
60 DBus::Error& error) {
61 ProcessWithId* p = CreateProcess(false);
62 if (!p)
63 return "";
64
65 p->AddArg(kBadblocks);
66 p->AddArg("-sv");
67 p->AddArg(kDevice);
68 p->BindFd(outfd.get(), STDOUT_FILENO);
69 p->BindFd(outfd.get(), STDERR_FILENO);
70 LOG(INFO) << "badblocks: running process id: " << p->id();
71 p->Start();
72 return p->id();
73}
74
75}; // namespace debugd