-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdelegate_impl.cc
157 lines (124 loc) · 4.05 KB
/
delegate_impl.cc
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Copyright 2019 The Chromium OS 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 <grp.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <memory>
#include <vector>
#include <base/files/file.h>
#include <base/files/file_path.h>
#include <base/files/file_util.h>
#include <base/logging.h>
#include <base/process/launch.h>
#include <base/strings/stringprintf.h>
#include "mems_setup/delegate_impl.h"
namespace mems_setup {
namespace {
const char kVpdDataPath[] =
"/mnt/stateful_partition/unencrypted/cache/vpd/full-v2.txt";
const char kSysModulePath[] = "/sys/module";
} // namespace
bool LoadVpdFromString(const std::string& vpd_data,
std::map<std::string, std::string>* cache) {
CHECK(cache);
size_t nl_pos = 0;
const size_t vpd_len = vpd_data.size();
while (nl_pos < vpd_len) {
const auto i_eq = vpd_data.find('=', nl_pos);
if (i_eq == std::string::npos)
break;
auto i_nl = vpd_data.find('\n', i_eq + 1);
if (i_nl == std::string::npos)
i_nl = vpd_len;
// VPD entries come in "key"="value" form, including the quotes;
// the purpose of the substring operations here is to remove those
// quotes, as they are inconvenient to deal with from C++ code.
const auto key = vpd_data.substr(nl_pos + 1, i_eq - nl_pos - 2);
const auto value = vpd_data.substr(i_eq + 2, i_nl - i_eq - 3);
cache->emplace(key, value);
nl_pos = i_nl + 1;
}
return true;
}
void DelegateImpl::LoadVpdIfNeeded() {
if (vpd_loaded_)
return;
std::string vpd_data;
base::FilePath vpd_path(kVpdDataPath);
if (!base::ReadFileToString(vpd_path, &vpd_data)) {
LOG(ERROR) << "failed to read VPD data";
return;
}
vpd_loaded_ = LoadVpdFromString(vpd_data, &vpd_cache_);
}
base::Optional<std::string> DelegateImpl::ReadVpdValue(const std::string& key) {
LoadVpdIfNeeded();
auto k = vpd_cache_.find(key);
if (k != vpd_cache_.end())
return k->second;
else
return base::nullopt;
}
bool DelegateImpl::ProbeKernelModule(const std::string& module) {
base::FilePath init_path(kSysModulePath);
init_path = init_path.Append(module).Append("initstate");
std::string init_data;
// If we can tell that a module has been loaded, then just return along
// the happy path instead of forking a new process.
if (base::ReadFileToString(init_path, &init_data)) {
if (init_data == "live\n")
return true;
}
std::vector<std::string> argv;
argv.emplace_back("/sbin/modprobe");
argv.emplace_back("-q");
argv.emplace_back(module);
base::Process process(base::LaunchProcess(argv, base::LaunchOptions()));
if (!process.IsValid()) {
LOG(ERROR) << "failed to launch modprobe";
return false;
}
int exit_code = -1;
if (!process.WaitForExit(&exit_code)) {
LOG(ERROR) << "modprobe exit could not be detected";
return false;
}
return exit_code == 0;
}
bool DelegateImpl::CreateDirectory(const base::FilePath& fp) {
return base::CreateDirectory(fp);
}
bool DelegateImpl::Exists(const base::FilePath& fp) {
return base::PathExists(fp);
}
base::Optional<gid_t> DelegateImpl::FindGroupId(const char* group) {
size_t len = 1024;
const auto max_len = sysconf(_SC_GETGR_R_SIZE_MAX);
if (max_len != -1)
len = max_len;
std::vector<char> buf(len);
struct group result;
struct group* resultp = nullptr;
getgrnam_r(group, &result, buf.data(), len, &resultp);
if (!resultp)
return base::nullopt;
return resultp->gr_gid;
}
int DelegateImpl::GetPermissions(const base::FilePath& path) {
int mode = 0;
bool ok = base::GetPosixFilePermissions(path, &mode);
if (ok)
return mode;
return 0;
}
bool DelegateImpl::SetPermissions(const base::FilePath& path, int mode) {
return base::SetPosixFilePermissions(path, mode);
}
bool DelegateImpl::SetOwnership(const base::FilePath& path,
uid_t user,
gid_t group) {
return lchown(path.value().c_str(), user, group) == 0;
}
} // namespace mems_setup