Go to the documentation of this file.
7 #ifndef _POWERDXX_SYS_SYSCTL_HPP_
8 #define _POWERDXX_SYS_SYSCTL_HPP_
16 #include <sys/types.h>
17 #include <sys/sysctl.h>
59 void *
const oldp,
size_t *
const oldlenp,
60 void const *
const newp,
size_t const newlen) {
61 if (
sysctl(name, namelen, oldp, oldlenp, newp, newlen) == -1) {
78 template <
size_t MibDepth>
79 void sysctl_get(
mib_t const (& mib)[MibDepth],
void *
const oldp,
size_t & oldlen) {
80 sysctl_raw(mib, MibDepth, oldp, &oldlen,
nullptr, 0);
95 template <
size_t MibDepth>
97 size_t const newlen) {
98 sysctl_raw(mib, MibDepth,
nullptr,
nullptr, newp, newlen);
123 template <
size_t MibDepth>
148 template <
typename... Tail>
151 static_assert(MibDepth ==
sizeof...(Tail) + 1,
152 "MIB depth mismatch");
179 void get(
void *
const buf,
size_t const bufsize)
const {
197 template <
typename T>
198 void get(T & value)
const {
199 get(&value,
sizeof(T));
217 template <
typename T>
218 std::unique_ptr<T[]>
get()
const {
219 auto const len =
size();
220 auto result = std::unique_ptr<T[]>(
new T[len /
sizeof(T)]);
221 get(result.get(), len);
233 void set(
void const *
const buf,
size_t const bufsize) {
245 template <
typename T>
246 void set(T
const & value) {
247 set(&value,
sizeof(T));
289 Sysctl(
char const *
const name) : depth{CTL_MAXNAME} {
293 assert(this->depth <= CTL_MAXNAME &&
"MIB depth exceeds limit");
302 sysctl_raw(this->mib, this->depth,
nullptr, &len,
nullptr, 0);
309 void get(
void *
const buf,
size_t const bufsize)
const {
311 sysctl_raw(this->mib, this->depth, buf, &len,
nullptr, 0);
317 template <
typename T>
318 void get(T & value)
const {
319 get(&value,
sizeof(T));
325 template <
typename T>
326 std::unique_ptr<T[]>
get()
const {
327 size_t const len =
size();
328 auto result = std::unique_ptr<T[]>(
new T[len /
sizeof(T)]);
329 get(result.get(), len);
336 void set(
void const *
const buf,
size_t const bufsize) {
337 sysctl_raw(this->mib, this->depth,
nullptr,
nullptr, buf, bufsize);
343 template <
typename T>
344 void set(T
const & value) {
345 set(&value,
sizeof(T));
355 template <
typename ... ArgTs>
361 Sysctl(
char const *
const) -> Sysctl<0>;
387 template <
typename T,
class SysctlT>
423 this->sysctl.set(value);
433 operator T ()
const {
435 this->sysctl.get(value);
456 template <
typename T,
size_t MibDepth = 0>
487 template <
typename T,
class SysctlT>
521 operator T
const &()
const {
539 template <
typename T,
size_t MibDepth = 0>
This is a wrapper around Sysctl that allows semantically transparent use of a sysctl.
Definition: sysctl.hpp:388
T value
The sysctl value read upon construction.
Definition: sysctl.hpp:493
void set(void const *const buf, size_t const bufsize)
Update the the sysctl value with the given buffer.
Definition: sysctl.hpp:336
std::unique_ptr< T[]> get() const
Retrieve an array from the sysctl address.
Definition: sysctl.hpp:326
constexpr Sysctl()
The default constructor.
Definition: sysctl.hpp:278
void sysctl_get(mib_t const (&mib)[MibDepth], void *const oldp, size_t &oldlen)
Returns a sysctl() value to a buffer.
Definition: sysctl.hpp:79
Can be thrown by syscall function wrappers if the function returned with an error.
Definition: error.hpp:26
Once(T const &value, SysctlT const &sysctl) noexcept
The constructor tries to read and store the requested sysctl.
Definition: sysctl.hpp:507
Wrappers around native system interfaces.
void sysctl_raw(mib_t const *name, u_int const namelen, void *const oldp, size_t *const oldlenp, void const *const newp, size_t const newlen)
A wrapper around the sysctl() function.
Definition: sysctl.hpp:58
constexpr Sync()
The default constructor.
Definition: sysctl.hpp:403
The domain error type.
Definition: sysctl.hpp:37
std::unique_ptr< T[]> get() const
Retrieve an array from the sysctl address.
Definition: sysctl.hpp:218
size_t size() const
The size of the sysctl.
Definition: sysctl.hpp:299
int sysctlnametomib(const char *name, int *mibp, size_t *sizep)
Intercept calls to sysctlnametomib().
Definition: libloadplay.cpp:1708
void set(void const *const buf, size_t const bufsize)
Update the the sysctl value with the given buffer.
Definition: sysctl.hpp:233
void get(T &value) const
Update the given value with a value retreived from the sysctl.
Definition: sysctl.hpp:318
void get(void *const buf, size_t const bufsize) const
Update the given buffer with a value retrieved from the sysctl.
Definition: sysctl.hpp:179
void set(T const &value)
Update the the sysctl value with the given value.
Definition: sysctl.hpp:344
Sysctl(char const *const name)
Initialise the MIB address from a character string.
Definition: sysctl.hpp:289
size_t size() const
The size of the sysctl.
Definition: sysctl.hpp:161
Represents a sysctl MIB address.
Definition: sysctl.hpp:124
void get(T &value) const
Update the given value with a value retreived from the sysctl.
Definition: sysctl.hpp:198
size_t depth
The MIB depth.
Definition: sysctl.hpp:270
mib_t mib[MibDepth]
Stores the MIB address.
Definition: sysctl.hpp:129
void set(T const &value)
Update the the sysctl value with the given value.
Definition: sysctl.hpp:246
void get(void *const buf, size_t const bufsize) const
Update the given buffer with a value retrieved from the sysctl.
Definition: sysctl.hpp:309
constexpr Sysctl(mib_t const head, Tail const ... tail) noexcept
Initialise the MIB address directly.
Definition: sysctl.hpp:149
int sysctl(const int *name, u_int namelen, void *oldp, size_t *oldlenp, const void *newp, size_t newlen)
Functions to intercept.
Definition: libloadplay.cpp:1655
constexpr Sync(SysctlT const &sysctl) noexcept
The constructor copies the given Sysctl instance.
Definition: sysctl.hpp:411
Sysctl(mib_t const, ArgTs const ...) -> Sysctl<(1+sizeof...(ArgTs))>
Create a Sysctl from a set of predefined MIBs.
A read once representation of a Sysctl.
Definition: sysctl.hpp:488
int mib_t
Management Information Base identifier type (see sysctl(3)).
Definition: sysctl.hpp:42
SysctlT sysctl
A sysctl to represent.
Definition: sysctl.hpp:393
Sync & operator=(T const &value)
Transparently assiges values of type T to the represented Sysctl instance.
Definition: sysctl.hpp:422
void sysctl_set(mib_t const (&mib)[MibDepth], void const *const newp, size_t const newlen)
Sets a sysctl() value.
Definition: sysctl.hpp:96
Provides system call error handling.