forked from Pissandshittium/pissandshittium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add sysctl utility functions to return strings
This creates two sysctl utility functions to return C++ strings, and modifies callers to use it. Fixed: 1477725 Change-Id: I0bf200b12694c3604bc8c838ee20c11c218f1048 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4833071 Commit-Queue: Avi Drissman <avi@chromium.org> Auto-Submit: Avi Drissman <avi@chromium.org> Reviewed-by: Mark Mentovai <mark@chromium.org> Cr-Commit-Position: refs/heads/main@{#1195389}
- Loading branch information
Avi Drissman
authored and
Chromium LUCI CQ
committed
Sep 12, 2023
1 parent
d1aaf8a
commit 837d106
Showing
11 changed files
with
173 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright 2023 The Chromium Authors | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "base/posix/sysctl.h" | ||
|
||
#include <sys/sysctl.h> | ||
|
||
#include <initializer_list> | ||
#include <string> | ||
|
||
#include "base/check_op.h" | ||
#include "base/functional/function_ref.h" | ||
#include "base/numerics/safe_conversions.h" | ||
#include "build/build_config.h" | ||
#include "third_party/abseil-cpp/absl/types/optional.h" | ||
|
||
namespace { | ||
|
||
absl::optional<std::string> StringSysctlImpl( | ||
base::FunctionRef<int(char* /*out*/, size_t* /*out_len*/)> sysctl_func) { | ||
size_t buf_len; | ||
int result = sysctl_func(nullptr, &buf_len); | ||
if (result < 0 || buf_len < 1) { | ||
return absl::nullopt; | ||
} | ||
|
||
std::string value(buf_len - 1, '\0'); | ||
result = sysctl_func(&value[0], &buf_len); | ||
if (result < 0) { | ||
return absl::nullopt; | ||
} | ||
CHECK_LE(buf_len - 1, value.size()); | ||
CHECK_EQ(value[buf_len - 1], '\0'); | ||
value.resize(buf_len - 1); | ||
|
||
return value; | ||
} | ||
} // namespace | ||
|
||
namespace base { | ||
|
||
absl::optional<std::string> StringSysctl( | ||
const std::initializer_list<int>& mib) { | ||
return StringSysctlImpl([mib](char* out, size_t* out_len) { | ||
return sysctl(const_cast<int*>(std::data(mib)), | ||
checked_cast<unsigned int>(std::size(mib)), out, out_len, | ||
nullptr, 0); | ||
}); | ||
} | ||
|
||
#if !BUILDFLAG(IS_OPENBSD) | ||
absl::optional<std::string> StringSysctlByName(const char* name) { | ||
return StringSysctlImpl([name](char* out, size_t* out_len) { | ||
return sysctlbyname(name, out, out_len, nullptr, 0); | ||
}); | ||
} | ||
#endif | ||
|
||
} // namespace base |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright 2023 The Chromium Authors | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef BASE_POSIX_SYSCTL_H_ | ||
#define BASE_POSIX_SYSCTL_H_ | ||
|
||
#include <initializer_list> | ||
#include <string> | ||
|
||
#include "base/base_export.h" | ||
#include "build/build_config.h" | ||
#include "third_party/abseil-cpp/absl/types/optional.h" | ||
|
||
// NB: While a BSD utility file, this lives in /base/posix/ for simplicity as | ||
// there is no /base/bsd/. | ||
|
||
namespace base { | ||
|
||
// Returns the value returned by `sysctl` as a std::string, or nullopt on error. | ||
BASE_EXPORT absl::optional<std::string> StringSysctl( | ||
const std::initializer_list<int>& mib); | ||
|
||
#if !BUILDFLAG(IS_OPENBSD) | ||
// Returns the value returned by `sysctlbyname` as a std::string, or nullopt | ||
// on error. | ||
BASE_EXPORT absl::optional<std::string> StringSysctlByName(const char* name); | ||
#endif | ||
|
||
} // namespace base | ||
|
||
#endif // BASE_POSIX_SYSCTL_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright 2023 The Chromium Authors | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "base/posix/sysctl.h" | ||
|
||
#include <sys/sysctl.h> | ||
|
||
#include "build/build_config.h" | ||
#include "testing/gtest/include/gtest/gtest.h" | ||
|
||
namespace base { | ||
|
||
namespace { | ||
|
||
using SysctlTest = testing::Test; | ||
|
||
TEST(SysctlTest, MibSuccess) { | ||
absl::optional<std::string> result1 = StringSysctl({CTL_HW, HW_MACHINE}); | ||
EXPECT_TRUE(result1); | ||
|
||
#if !BUILDFLAG(IS_OPENBSD) | ||
absl::optional<std::string> result2 = StringSysctlByName("hw.machine"); | ||
EXPECT_TRUE(result2); | ||
|
||
EXPECT_EQ(result1, result2); | ||
#endif | ||
} | ||
|
||
TEST(SysctlTest, MibFailure) { | ||
absl::optional<std::string> result = StringSysctl({-1}); | ||
EXPECT_FALSE(result); | ||
|
||
#if !BUILDFLAG(IS_OPENBSD) | ||
result = StringSysctlByName("banananananananana"); | ||
EXPECT_FALSE(result); | ||
#endif | ||
} | ||
|
||
} // namespace | ||
|
||
} // namespace base |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.