libzypp 17.37.10

linuxhelpers_p.h
Go to the documentation of this file.
1#ifndef ZYPP_BASE_LINUXHELPERS_P_H_DEFINED
2#define ZYPP_BASE_LINUXHELPERS_P_H_DEFINED
3
4#include <string>
5#include <optional>
6#include <zypp-core/zyppng/core/ByteArray>
7#include <zypp-core/AutoDispose.h>
8#include <errno.h>
9
10 namespace zyppng {
11
12 class SockAddr;
13
14 inline std::string strerr_cxx ( const int err = -1 ) {
15 ByteArray strBuf( 1024, '\0' );
16 strerror_r( err == -1 ? errno : err , strBuf.data(), strBuf.size() );
17 return std::string( strBuf.data() );
18 }
19
20
34 template<typename Fun, typename RestartCb, typename... Args >
35 auto eintrSafeCallEx ( const Fun &function, const RestartCb &restartCb, Args&&... args ) {
36 int res = 0;
37 while ( true ) {
38 errno = 0;
39 res = std::invoke(function, args... );
40 if ( res == -1 && errno == EINTR ) {
41 std::invoke( restartCb );
42 continue;
43 }
44 break;
45 };
46 return res;
47 }
48
49 template<typename Fun, typename... Args >
50 auto eintrSafeCall ( Fun &&function, Args&&... args ) {
51 return eintrSafeCallEx( std::forward<Fun>(function), [](){}, std::forward<Args>(args)... );
52 }
53
54 bool blockAllSignalsForCurrentThread ( );
55
56 bool blockSignalsForCurrentThread ( const std::vector<int> &sigs );
57
58 bool trySocketConnection (int &sockFD, const SockAddr &addr, uint64_t timeout );
59
60 // origfd will be accessible as newfd and closed (unless they were equal)
61 void renumberFd (int origfd, int newfd);
62
67 int64_t bytesAvailableOnFD( int fd );
68
72 struct Pipe {
73 zypp::AutoFD readFd;
74 zypp::AutoFD writeFd;
75 static std::optional<Pipe> create ( int flags = 0 );
76
77 void unrefWrite( ) {
78 writeFd = -1;
79 }
80
81 void unrefRead( ) {
82 readFd = -1;
83 }
84 };
85}
86
87#endif // LINUXHELPERS_P_H
zypp::ByteArray
Definition ByteArray.h:19
zyppng::SockAddr
Definition sockaddr.h:26
std::invoke
std::enable_if< std::is_member_pointer< typenamestd::decay< Functor >::type >::value, typenamestd::result_of< Functor &&(Args &&...)>::type >::type invoke(Functor &&f, Args &&... args)
Definition functional.h:32
zyppng
Definition MediaNetworkRequestExecutor.h:16
zyppng::blockAllSignalsForCurrentThread
bool blockAllSignalsForCurrentThread()
Definition linuxhelpers.cc:15
zyppng::trySocketConnection
bool trySocketConnection(int &sockFD, const SockAddr &addr, uint64_t timeout)
Definition linuxhelpers.cc:34
zyppng::eintrSafeCallEx
auto eintrSafeCallEx(const Fun &function, const RestartCb &restartCb, Args &&... args)
Definition linuxhelpers_p.h:35
zyppng::blockSignalsForCurrentThread
bool blockSignalsForCurrentThread(const std::vector< int > &sigs)
Definition linuxhelpers.cc:23
zyppng::eintrSafeCall
auto eintrSafeCall(Fun &&function, Args &&... args)
Definition linuxhelpers_p.h:50
zyppng::bytesAvailableOnFD
int64_t bytesAvailableOnFD(int fd)
Definition linuxhelpers.cc:62
zyppng::renumberFd
void renumberFd(int origfd, int newfd)
Definition linuxhelpers.cc:50
zyppng::strerr_cxx
std::string strerr_cxx(const int err=-1)
Definition linuxhelpers_p.h:14
zypp::AutoFD
AutoDispose<int> calling close
Definition AutoDispose.h:310
zyppng::Pipe
Definition linuxhelpers_p.h:72
zyppng::Pipe::create
static std::optional< Pipe > create(int flags=0)
Definition linuxhelpers.cc:71
zyppng::Pipe::unrefWrite
void unrefWrite()
Definition linuxhelpers_p.h:77
zyppng::Pipe::writeFd
zypp::AutoFD writeFd
Definition linuxhelpers_p.h:74
zyppng::Pipe::readFd
zypp::AutoFD readFd
Definition linuxhelpers_p.h:73
zyppng::Pipe::unrefRead
void unrefRead()
Definition linuxhelpers_p.h:81
AutoDispose.h