From cfe862860329af2bb638b9261b7bc1ed476cb48e Mon Sep 17 00:00:00 2001 From: John Elliott Date: Mon, 4 Mar 2024 12:22:09 -0800 Subject: [PATCH] Move SysctlUtil from eden to edencommon Summary: To support better telemetry and logging in watchman we want to use Eden's components. Lets migrate and detangle the needed pieces. This change moves SysctlUtil from eden to edencommon. Reviewed By: genevievehelsel Differential Revision: D54471684 fbshipit-source-id: df6d0716dfe371d8c7fc1fe80079649b85b50e6b --- eden/common/utils/SysctlUtil.cpp | 30 ++++++++++++++++++++++++++++++ eden/common/utils/SysctlUtil.h | 14 ++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 eden/common/utils/SysctlUtil.cpp create mode 100644 eden/common/utils/SysctlUtil.h diff --git a/eden/common/utils/SysctlUtil.cpp b/eden/common/utils/SysctlUtil.cpp new file mode 100644 index 0000000..22b42e0 --- /dev/null +++ b/eden/common/utils/SysctlUtil.cpp @@ -0,0 +1,30 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#include "eden/common/utils/SysctlUtil.h" + +#ifdef __APPLE__ + +#include // @manual +#include + +#include + +std::string getSysCtlByName(const char* name, size_t size) { + if (size == 0) { + return std::string{}; + } + std::string buffer(size, 0); + size_t returnedSize = size - 1; + auto ret = sysctlbyname(name, &buffer[0], &returnedSize, nullptr, 0); + if (ret != 0) { + folly::throwSystemError("failed to retrieve sysctl ", name); + } + buffer.resize(returnedSize); + return buffer; +} +#endif diff --git a/eden/common/utils/SysctlUtil.h b/eden/common/utils/SysctlUtil.h new file mode 100644 index 0000000..acdda89 --- /dev/null +++ b/eden/common/utils/SysctlUtil.h @@ -0,0 +1,14 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#include + +#ifdef __APPLE__ +// Fetches the value of a sysctl by name. +// The result is assumed to be a string. +std::string getSysCtlByName(const char* name, size_t size); +#endif