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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
|
//
// Automated Testing Framework (atf)
//
// Copyright (c) 2007 The NetBSD Foundation, Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
// CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
// IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
extern "C" {
#include <sys/param.h>
#include <sys/sysctl.h>
}
#include <cassert>
#include <cerrno>
#include <cstdlib>
#include <cstring>
#include <stdexcept>
#include "config.hpp"
#include "defs.hpp"
#include "env.hpp"
#include "fs.hpp"
#include "requirements.hpp"
#include "text.hpp"
#include "user.hpp"
namespace impl = tools;
namespace {
typedef std::map< std::string, std::string > vars_map;
static
bool
has_program(const tools::fs::path& program)
{
bool found = false;
if (program.is_absolute()) {
found = tools::fs::is_executable(program);
} else {
if (program.str().find('/') != std::string::npos)
throw std::runtime_error("Relative paths are not allowed "
"when searching for a program (" +
program.str() + ")");
const std::vector< std::string > dirs = tools::text::split(
tools::env::get("PATH"), ":");
for (std::vector< std::string >::const_iterator iter = dirs.begin();
!found && iter != dirs.end(); iter++) {
const tools::fs::path& p = tools::fs::path(*iter) / program;
if (tools::fs::is_executable(p))
found = true;
}
}
return found;
}
static
std::string
check_arch(const std::string& arches)
{
const std::vector< std::string > v = tools::text::split(arches, " ");
for (std::vector< std::string >::const_iterator iter = v.begin();
iter != v.end(); iter++) {
if ((*iter) == tools::config::get("atf_arch"))
return "";
}
if (v.size() == 1)
return "Requires the '" + arches + "' architecture";
else
return "Requires one of the '" + arches + "' architectures";
}
static
std::string
check_config(const std::string& variables, const vars_map& config)
{
const std::vector< std::string > v = tools::text::split(variables, " ");
for (std::vector< std::string >::const_iterator iter = v.begin();
iter != v.end(); iter++) {
if (config.find((*iter)) == config.end())
return "Required configuration variable '" + (*iter) + "' not "
"defined";
}
return "";
}
static
std::string
check_files(const std::string& progs)
{
const std::vector< std::string > v = tools::text::split(progs, " ");
for (std::vector< std::string >::const_iterator iter = v.begin();
iter != v.end(); iter++) {
const tools::fs::path file(*iter);
if (!file.is_absolute())
throw std::runtime_error("Relative paths are not allowed when "
"checking for a required file (" + file.str() + ")");
if (!tools::fs::exists(file))
return "Required file '" + file.str() + "' not found";
}
return "";
}
static
std::string
check_machine(const std::string& machines)
{
const std::vector< std::string > v = tools::text::split(machines, " ");
for (std::vector< std::string >::const_iterator iter = v.begin();
iter != v.end(); iter++) {
if ((*iter) == tools::config::get("atf_machine"))
return "";
}
if (v.size() == 1)
return "Requires the '" + machines + "' machine type";
else
return "Requires one of the '" + machines + "' machine types";
}
#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
static
std::string
check_memory_sysctl(const int64_t needed, const char* sysctl_variable)
{
int64_t available;
std::size_t available_length = sizeof(available);
if (::sysctlbyname(sysctl_variable, &available, &available_length,
NULL, 0) == -1) {
const char* e = std::strerror(errno);
return "Failed to get sysctl(hw.usermem64) value: " + std::string(e);
}
if (available < needed) {
return "Not enough memory; needed " + tools::text::to_string(needed) +
", available " + tools::text::to_string(available);
} else
return "";
}
# if defined(__APPLE__)
static
std::string
check_memory_darwin(const int64_t needed)
{
return check_memory_sysctl(needed, "hw.usermem");
}
# elif defined(__FreeBSD__)
static
std::string
check_memory_freebsd(const int64_t needed)
{
return check_memory_sysctl(needed, "hw.usermem");
}
# elif defined(__NetBSD__)
static
std::string
check_memory_netbsd(const int64_t needed)
{
return check_memory_sysctl(needed, "hw.usermem64");
}
# else
# error "Conditional error"
# endif
#else
static
std::string
check_memory_unknown(const int64_t needed ATF_DEFS_ATTRIBUTE_UNUSED)
{
return "";
}
#endif
static
std::string
check_memory(const std::string& raw_memory)
{
const int64_t needed = tools::text::to_bytes(raw_memory);
#if defined(__APPLE__)
return check_memory_darwin(needed);
#elif defined(__FreeBSD__)
return check_memory_freebsd(needed);
#elif defined(__NetBSD__)
return check_memory_netbsd(needed);
#else
return check_memory_unknown(needed);
#endif
}
static
std::string
check_progs(const std::string& progs)
{
const std::vector< std::string > v = tools::text::split(progs, " ");
for (std::vector< std::string >::const_iterator iter = v.begin();
iter != v.end(); iter++) {
if (!has_program(tools::fs::path(*iter)))
return "Required program '" + (*iter) + "' not found in the PATH";
}
return "";
}
static
std::string
check_user(const std::string& user, const vars_map& config)
{
if (user == "root") {
if (!tools::user::is_root())
return "Requires root privileges";
else
return "";
} else if (user == "unprivileged") {
if (tools::user::is_root()) {
const vars_map::const_iterator iter = config.find(
"unprivileged-user");
if (iter == config.end())
return "Requires an unprivileged user and the "
"'unprivileged-user' configuration variable is not set";
else {
const std::string& unprivileged_user = (*iter).second;
try {
(void)tools::user::get_user_ids(unprivileged_user);
return "";
} catch (const std::runtime_error& e) {
return "Failed to get information for user " +
unprivileged_user;
}
}
} else
return "";
} else
throw std::runtime_error("Invalid value '" + user + "' for property "
"require.user");
}
} // anonymous namespace
std::string
impl::check_requirements(const vars_map& metadata,
const vars_map& config)
{
std::string failure_reason = "";
for (vars_map::const_iterator iter = metadata.begin();
failure_reason.empty() && iter != metadata.end(); iter++) {
const std::string& name = (*iter).first;
const std::string& value = (*iter).second;
assert(!value.empty()); // Enforced by application/X-atf-tp parser.
if (name == "require.arch")
failure_reason = check_arch(value);
else if (name == "require.config")
failure_reason = check_config(value, config);
else if (name == "require.files")
failure_reason = check_files(value);
else if (name == "require.machine")
failure_reason = check_machine(value);
else if (name == "require.memory")
failure_reason = check_memory(value);
else if (name == "require.progs")
failure_reason = check_progs(value);
else if (name == "require.user")
failure_reason = check_user(value, config);
else {
// Unknown require.* properties are forbidden by the
// application/X-atf-tp parser.
assert(failure_reason.find("require.") != 0);
}
}
return failure_reason;
}
std::pair< int, int >
impl::get_required_user(const vars_map& metadata,
const vars_map& config)
{
const vars_map::const_iterator user = metadata.find(
"require.user");
if (user == metadata.end())
return std::make_pair(-1, -1);
if ((*user).second == "unprivileged") {
if (tools::user::is_root()) {
const vars_map::const_iterator iter = config.find(
"unprivileged-user");
try {
return tools::user::get_user_ids((*iter).second);
} catch (const std::exception& e) {
std::abort(); // This has been validated by check_user.
}
} else {
return std::make_pair(-1, -1);
}
} else
return std::make_pair(-1, -1);
}
|