Skip to content

Commit f817a59

Browse files
committed
Refactor ResolvePossibleSymlinkToHostPath to common directory
Signed-off-by: Dom Del Nano <ddelnano@gmail.com>
1 parent 8540621 commit f817a59

File tree

3 files changed

+124
-45
lines changed

3 files changed

+124
-45
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright 2018- The Pixie Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
*/
18+
19+
#include "src/common/system/linux_headers_utils.h"
20+
21+
#include <fstream>
22+
#include <limits>
23+
#include <memory>
24+
#include <string>
25+
26+
#include "src/common/base/file.h"
27+
#include "src/common/fs/fs_wrapper.h"
28+
#include "src/common/system/config.h"
29+
30+
namespace px {
31+
namespace system {
32+
33+
StatusOr<std::filesystem::path> ResolvePossibleSymlinkToHostPath(const std::filesystem::path p) {
34+
// Check if "p" is a symlink.
35+
std::error_code ec;
36+
const bool is_symlink = std::filesystem::is_symlink(p, ec);
37+
if (ec) {
38+
return error::NotFound(absl::Substitute("Did not find the host headers at path: $0, $1.",
39+
p.string(), ec.message()));
40+
}
41+
42+
if (!is_symlink) {
43+
// Not a symlink, we are good now.
44+
return p;
45+
}
46+
47+
// Resolve the symlink, and re-convert to a host path..
48+
const std::filesystem::path resolved = std::filesystem::read_symlink(p, ec);
49+
if (ec) {
50+
return error::Internal(ec.message());
51+
}
52+
53+
// Relative paths containing "../" can result in an invalid host mount path when using
54+
// ToHostPath. Therefore, we need to treat the absolute and relative cases differently.
55+
std::filesystem::path resolved_host_path;
56+
if (resolved.is_absolute()) {
57+
resolved_host_path = system::Config::GetInstance().ToHostPath(resolved);
58+
VLOG(1) << absl::Substitute(
59+
"Symlink target is an absolute path. Converting that to host path: $0 -> $1.",
60+
resolved.string(), resolved_host_path.string());
61+
} else {
62+
resolved_host_path = p.parent_path();
63+
resolved_host_path /= resolved.string();
64+
VLOG(1) << absl::Substitute(
65+
"Symlink target is a relative path. Concatenating it to parent directory: $0",
66+
resolved_host_path.string());
67+
}
68+
69+
// Downstream won't be ok unless the resolved host path exists; return an error if needed.
70+
if (!fs::Exists(resolved_host_path)) {
71+
return error::NotFound(absl::Substitute("Did not find host headers at resolved path: $0.",
72+
resolved_host_path.string()));
73+
}
74+
return resolved_host_path;
75+
}
76+
77+
} // namespace system
78+
} // namespace px
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2018- The Pixie Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
*/
18+
19+
#pragma once
20+
21+
#include <filesystem>
22+
23+
#include "src/common/base/base.h"
24+
25+
namespace px {
26+
namespace system {
27+
28+
/**
29+
* Resolves a possible symlink path to its corresponding host filesystem path.
30+
*
31+
* This function takes a filesystem path and checks if it is a symbolic link. If it is,
32+
* the symlink is resolved to its target path. Depending on whether the target is an absolute
33+
* or relative path, it is further processed to convert it into a valid host path (as in
34+
* Config::ToHostPath(...) path).
35+
*
36+
* If the input path is not a symlink, it is returned as-is. The function ensures that
37+
* the final resolved path exists in the host filesystem before returning it. Errors are
38+
* returned when the path does not exist, the resolution fails, or when there is an issue
39+
* accessing the filesystem.
40+
*/
41+
StatusOr<std::filesystem::path> ResolvePossibleSymlinkToHostPath(const std::filesystem::path p);
42+
43+
} // namespace system
44+
} // namespace px

src/stirling/utils/linux_headers.cc

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "src/common/fs/temp_file.h"
3232
#include "src/common/minitar/minitar.h"
3333
#include "src/common/system/config.h"
34+
#include "src/common/system/linux_headers_utils.h"
3435
#include "src/common/system/proc_pid_path.h"
3536
#include "src/common/zlib/zlib_wrapper.h"
3637

@@ -209,55 +210,11 @@ Status FindLinuxHeadersDirectory(const std::filesystem::path& lib_modules_dir) {
209210
return error::NotFound("Could not find 'source' or 'build' under $0.", lib_modules_dir.string());
210211
}
211212

212-
StatusOr<std::filesystem::path> ResolvePossibleSymlinkToHostPath(const std::filesystem::path p) {
213-
// Check if "p" is a symlink.
214-
std::error_code ec;
215-
const bool is_symlink = std::filesystem::is_symlink(p, ec);
216-
if (ec) {
217-
return error::NotFound(absl::Substitute("Did not find the host headers at path: $0, $1.",
218-
p.string(), ec.message()));
219-
}
220-
221-
if (!is_symlink) {
222-
// Not a symlink, we are good now.
223-
return p;
224-
}
225-
226-
// Resolve the symlink, and re-convert to a host path..
227-
const std::filesystem::path resolved = std::filesystem::read_symlink(p, ec);
228-
if (ec) {
229-
return error::Internal(ec.message());
230-
}
231-
232-
// Relative paths containing "../" can result in an invalid host mount path when using
233-
// ToHostPath. Therefore, we need to treat the absolute and relative cases differently.
234-
std::filesystem::path resolved_host_path;
235-
if (resolved.is_absolute()) {
236-
resolved_host_path = system::Config::GetInstance().ToHostPath(resolved);
237-
VLOG(1) << absl::Substitute(
238-
"Symlink target is an absolute path. Converting that to host path: $0 -> $1.",
239-
resolved.string(), resolved_host_path.string());
240-
} else {
241-
resolved_host_path = p.parent_path();
242-
resolved_host_path /= resolved.string();
243-
VLOG(1) << absl::Substitute(
244-
"Symlink target is a relative path. Concatenating it to parent directory: $0",
245-
resolved_host_path.string());
246-
}
247-
248-
// Downstream won't be ok unless the resolved host path exists; return an error if needed.
249-
if (!fs::Exists(resolved_host_path)) {
250-
return error::NotFound(absl::Substitute("Did not find host headers at resolved path: $0.",
251-
resolved_host_path.string()));
252-
}
253-
return resolved_host_path;
254-
}
255-
256213
Status LinkHostLinuxHeadersKernel(const std::filesystem::path& lib_modules_dir) {
257214
const auto host_path = system::Config::GetInstance().ToHostPath(lib_modules_dir);
258215
LOG(INFO) << absl::Substitute("Looking for host Linux headers at $0.", host_path.string());
259216

260-
PX_ASSIGN_OR_RETURN(const auto resolved_host_path, ResolvePossibleSymlinkToHostPath(host_path));
217+
PX_ASSIGN_OR_RETURN(const auto resolved_host_path, system::ResolvePossibleSymlinkToHostPath(host_path));
261218
PX_RETURN_IF_ERROR(fs::CreateSymlinkIfNotExists(resolved_host_path, lib_modules_dir));
262219
LOG(INFO) << absl::Substitute("Linked host headers at $0 to symlink in pem namespace at $1.",
263220
resolved_host_path.string(), lib_modules_dir.string());

0 commit comments

Comments
 (0)