0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-05 05:18:54 +01:00
construct/ircd/sys.cc
Jason Volk eb00134100 ircd::util: Move syscall() templates out of util; minor reorg includes.
ircd::sys: Add abstract sysfs tool and template from fs::dev.
2020-06-11 09:49:33 -07:00

60 lines
1.2 KiB
C++

// The Construct
//
// Copyright (C) The Construct Developers, Authors & Contributors
// Copyright (C) 2016-2020 Jason Volk <jason@zemos.net>
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice is present in all copies. The
// full license for this software is available in the LICENSE file.
decltype(ircd::sys::log)
ircd::sys::log
{
"sys"
};
ircd::string_view
ircd::sys::get(const mutable_buffer &out,
const string_view &relpath)
try
{
thread_local char buf[1024];
string_view path(relpath);
path = lstrip(relpath, "/sys/"_sv); // tolerate any errant /sys/ prefix
path = strlcpy(buf, "/sys/"_sv);
path = strlcat(buf, relpath);
fs::fd::opts fdopts;
fdopts.errlog = false;
const fs::fd fd
{
path, fdopts
};
string_view ret
{
data(out), sys::call(::read, fd, data(out), size(out))
};
ret = rstrip(ret, '\n');
ret = rstrip(ret, ' ');
return ret;
}
catch(const ctx::interrupted &)
{
throw;
}
catch(const std::exception &e)
{
#if 0
log::derror
{
log, "sysfs query `%s' :%s",
relpath,
e.what(),
};
#endif
return {};
}