13 #ifndef __STOUT_OS_SYSCTL_HPP__ 14 #define __STOUT_OS_SYSCTL_HPP__ 17 #if !defined(__APPLE__) && !defined(__FreeBSD__) 18 #error "stout/os/sysctl.hpp is only available on OS X and FreeBSD." 21 #include <sys/sysctl.h> 23 #include <sys/types.h> 66 explicit sysctl(
int level1);
67 sysctl(
int level1,
int level2);
68 sysctl(
int level1,
int level2,
int level3);
69 sysctl(
int level1,
int level2,
int level3,
int level4);
70 sysctl(
int level1,
int level2,
int level3,
int level4,
int level5);
74 private:
struct Integer;
93 private:
struct Table;
100 Integer(
int _levels,
int* _name);
102 template <
typename T>
113 template <
typename T>
127 : levels(1), name(new int[levels])
134 : levels(2), name(new int[levels])
142 : levels(3), name(new int[levels])
151 : levels(4), name(new int[levels])
166 : levels(5), name(new int[levels])
184 return Integer(levels, name);
192 if (::
sysctl(name, levels,
nullptr, &size,
nullptr, 0) == -1) {
197 size_t length = size /
sizeof(char);
198 char*
temp =
new char[length];
199 if (::
sysctl(name, levels, temp, &size,
nullptr, 0) == -1) {
212 std::string result(temp, size - 1);
221 size_t size =
sizeof(result);
222 if (::
sysctl(name, levels, &result, &size,
nullptr, 0) == -1) {
231 return Table(levels, name, length);
235 inline sysctl::Integer::Integer(
243 template <
typename T>
244 sysctl::Integer::operator
Try<T>()
247 size_t size =
sizeof(i);
248 if (::
sysctl(name, levels, &i, &size,
nullptr, 0) == -1) {
255 inline sysctl::Table::Table(
265 template <
typename T>
269 if (length.isNone()) {
270 if (::
sysctl(name, levels,
nullptr, &size,
nullptr, 0) == -1) {
273 if (size %
sizeof(T) != 0) {
274 return Error(
"Failed to determine the length of result, " 275 "amount of available data is not a multiple " 276 "of the table type");
281 T* ts =
new T[length.get()];
282 size = length.get() *
sizeof(T);
283 if (::
sysctl(name, levels, ts, &size,
nullptr, 0) == -1) {
294 length = size /
sizeof(T);
296 std::vector<T> results;
297 for (
size_t i = 0; i < length.get(); i++) {
298 results.push_back(ts[i]);
306 #endif // __STOUT_OS_SYSCTL_HPP__ Definition: errorbase.hpp:36
Try< Bytes > size(const std::string &path, const FollowSymlink follow=FollowSymlink::FOLLOW_SYMLINK)
Definition: stat.hpp:130
Definition: errorbase.hpp:50
Definition: posix_signalhandler.hpp:23
Try< std::string > string() const
Definition: sysctl.hpp:188
Table table(const Option< size_t > &length=None()) const
Definition: sysctl.hpp:229
sysctl(int level1)
Definition: sysctl.hpp:126
~sysctl()
Definition: sysctl.hpp:176
Integer integer() const
Definition: sysctl.hpp:182
std::string error(const std::string &msg, uint32_t code)
Definition: sysctl.hpp:59
std::string temp()
Definition: temp.hpp:27
Try< timeval > time() const
Definition: sysctl.hpp:218