2018-02-04 03:22:01 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
2018-01-10 22:16:34 +01:00
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
2018-02-04 03:22:01 +01:00
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
2018-01-10 22:16:34 +01:00
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
2018-02-04 03:22:01 +01:00
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
2018-01-10 22:16:34 +01:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#define HAVE_IRCD_UTIL_SYSCALL_H
|
|
|
|
|
2019-06-23 10:01:26 +02:00
|
|
|
namespace ircd {
|
|
|
|
inline namespace util
|
2018-01-10 22:16:34 +01:00
|
|
|
{
|
|
|
|
template<class function, class... args> long syscall(function&& f, args&&... a);
|
|
|
|
template<long number, class... args> long syscall(args&&... a);
|
|
|
|
|
|
|
|
template<class function, class... args> long syscall_nointr(function&& f, args&&... a);
|
|
|
|
template<long number, class... args> long syscall_nointr(args&&... a);
|
2019-06-23 10:01:26 +02:00
|
|
|
}}
|
2018-01-10 22:16:34 +01:00
|
|
|
|
2018-01-11 12:21:19 +01:00
|
|
|
/// Posix system call template to check for returned error value and throw the
|
|
|
|
/// approps errno in the proper std::system_error exception. Note the usage
|
|
|
|
/// here, the libc wrapper function is the first argument i.e:
|
|
|
|
/// syscall(::foo, bar, baz) not syscall(::foo(bar, baz));
|
|
|
|
///
|
2018-01-10 22:16:34 +01:00
|
|
|
template<class function,
|
|
|
|
class... args>
|
|
|
|
long
|
|
|
|
ircd::util::syscall(function&& f,
|
|
|
|
args&&... a)
|
|
|
|
{
|
|
|
|
const auto ret
|
|
|
|
{
|
|
|
|
f(std::forward<args>(a)...)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(unlikely(ret == -1))
|
|
|
|
throw std::system_error
|
|
|
|
{
|
|
|
|
errno, std::system_category()
|
|
|
|
};
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-01-11 12:21:19 +01:00
|
|
|
/// Posix system call template to check for returned error value and throw the
|
|
|
|
/// approps errno in the proper std::system_error exception. This template
|
|
|
|
/// requires a system call number in the parameters. The arguments are only
|
|
|
|
/// the actual arguments passed to the syscall because the number is given
|
|
|
|
/// in the template.
|
|
|
|
///
|
2018-01-10 22:16:34 +01:00
|
|
|
template<long number,
|
|
|
|
class... args>
|
|
|
|
long
|
|
|
|
ircd::util::syscall(args&&... a)
|
|
|
|
{
|
|
|
|
const auto ret
|
|
|
|
{
|
|
|
|
::syscall(number, std::forward<args>(a)...)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(unlikely(ret == -1))
|
|
|
|
throw std::system_error
|
|
|
|
{
|
|
|
|
errno, std::system_category()
|
|
|
|
};
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-01-11 12:21:19 +01:00
|
|
|
/// Uninterruptible posix system call template to check for returned error
|
|
|
|
/// value and throw the approps errno in the proper std::system_error
|
|
|
|
/// exception. Note the usage here, the libc wrapper function is the first
|
|
|
|
/// argument i.e: syscall(::foo, bar, baz) not syscall(::foo(bar, baz));
|
|
|
|
///
|
|
|
|
/// The syscall is restarted until it no longer returns with EINTR.
|
|
|
|
///
|
2018-01-10 22:16:34 +01:00
|
|
|
template<class function,
|
|
|
|
class... args>
|
|
|
|
long
|
|
|
|
ircd::util::syscall_nointr(function&& f,
|
|
|
|
args&&... a)
|
|
|
|
{
|
|
|
|
long ret; do
|
|
|
|
{
|
|
|
|
ret = f(std::forward<args>(a)...);
|
|
|
|
}
|
|
|
|
while(unlikely(ret == -1 && errno == EINTR));
|
|
|
|
|
|
|
|
if(unlikely(ret == -1))
|
2018-01-11 12:21:19 +01:00
|
|
|
throw std::system_error
|
|
|
|
{
|
|
|
|
errno, std::system_category()
|
|
|
|
};
|
2018-01-10 22:16:34 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-01-11 12:21:19 +01:00
|
|
|
/// Uninterruptible posix system call template to check for returned error
|
|
|
|
/// value and throw the approps errno in the proper std::system_error
|
|
|
|
/// exception. This template requires a system call number in the parameters.
|
|
|
|
/// The arguments are only the actual arguments passed to the syscall because
|
|
|
|
/// the number is given in the template.
|
|
|
|
///
|
|
|
|
/// The syscall is restarted until it no longer returns with EINTR.
|
|
|
|
///
|
2018-01-10 22:16:34 +01:00
|
|
|
template<long number,
|
|
|
|
class... args>
|
|
|
|
long
|
|
|
|
ircd::util::syscall_nointr(args&&... a)
|
|
|
|
{
|
|
|
|
long ret; do
|
|
|
|
{
|
|
|
|
ret = ::syscall(number, std::forward<args>(a)...);
|
|
|
|
}
|
|
|
|
while(unlikely(ret == -1 && errno == EINTR));
|
|
|
|
|
|
|
|
if(unlikely(ret == -1))
|
2018-01-11 12:21:19 +01:00
|
|
|
throw std::system_error
|
|
|
|
{
|
|
|
|
errno, std::system_category()
|
|
|
|
};
|
2018-01-10 22:16:34 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|