diff --git a/eden/common/utils/SysctlUtil.cpp b/eden/common/utils/SysctlUtil.cpp new file mode 100644 index 00000000..22b42e0a --- /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 00000000..acdda89d --- /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