blob: c2f9362bc638375774abd85caf86891d4906558b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/chromeos/system/udev_info_provider.h"
#include "base/bind.h"
#include "base/command_line.h"
#include "base/lazy_instance.h"
#include "base/process_util.h"
#include "base/string_util.h"
#include "chrome/browser/chromeos/system/name_value_pairs_parser.h"
#include "content/public/browser/browser_thread.h"
using content::BrowserThread;
namespace chromeos {
namespace system {
namespace {
// Absolute path of the udevadm tool.
const char kUdevadmToolPath[] = "/sbin/udevadm";
// Args to pass to udevadm (followed by the device path).
const char* kUdevadmToolArgs[] = { "info", "--query=property", "--path" };
// Key-value separator and string delimiter in output of "udevadm info".
const char kUdevadmEq[] = "=";
const char kUdevadmDelim[] = "\n";
} // namespace
class UdevInfoProviderImpl : public UdevInfoProvider {
public:
UdevInfoProviderImpl() {}
// UdevInfoProvider implementation.
virtual bool QueryDeviceProperty(const std::string& sys_path,
const std::string& property_name,
std::string* result) const OVERRIDE;
static UdevInfoProvider* GetInstance();
private:
DISALLOW_COPY_AND_ASSIGN(UdevInfoProviderImpl);
};
base::LazyInstance<UdevInfoProviderImpl> g_udev_info_provider(
base::LINKER_INITIALIZED);
bool UdevInfoProviderImpl::QueryDeviceProperty(const std::string& sys_path,
const std::string& property_name,
std::string* result) const {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
FilePath udevadm_path(kUdevadmToolPath);
CommandLine cmd_line(udevadm_path);
// TODO(ivankr): manually appending switches with AppendArg is a workaround
// to pass "info" argument before switches like "--path" (CommandLine forces
// arguments after switches).
for (size_t i = 0; i < arraysize(kUdevadmToolArgs); ++i)
cmd_line.AppendArg(kUdevadmToolArgs[i]);
cmd_line.AppendArg(sys_path);
std::string output_string;
if (!base::GetAppOutput(cmd_line, &output_string)) {
LOG(ERROR) << "Error executing: " << cmd_line.GetCommandLineString();
return false;
}
TrimWhitespaceASCII(output_string, TRIM_ALL, &output_string);
NameValuePairsParser::NameValueMap device_info;
NameValuePairsParser parser(&device_info);
if (!parser.ParseNameValuePairs(output_string, kUdevadmEq, kUdevadmDelim))
return false;
NameValuePairsParser::NameValueMap::const_iterator it =
device_info.find(property_name);
if (it == device_info.end())
return false;
*result = it->second;
return true;
}
UdevInfoProvider* UdevInfoProviderImpl::GetInstance() {
return &g_udev_info_provider.Get();
}
UdevInfoProvider* UdevInfoProvider::GetInstance() {
return UdevInfoProviderImpl::GetInstance();
}
} // namespace system
} // namespace chromeos
|