libzypp  17.35.14
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>
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 
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 {
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::AutoFD writeFd
bool trySocketConnection(int &sockFD, const SockAddr &addr, uint64_t timeout)
Definition: linuxhelpers.cc:34
void unrefWrite()
AutoDispose<int> calling ::close
Definition: AutoDispose.h:309
std::enable_if< std::is_member_pointer< typename std::decay< Functor >::type >::value, typename std::result_of< Functor &&(Args &&...)>::type >::type invoke(Functor &&f, Args &&... args)
Definition: functional.h:32
bool blockSignalsForCurrentThread(const std::vector< int > &sigs)
Definition: linuxhelpers.cc:23
void renumberFd(int origfd, int newfd)
Definition: linuxhelpers.cc:50
zypp::AutoFD readFd
std::string strerr_cxx(const int err=-1)
static std::optional< Pipe > create(int flags=0)
Definition: linuxhelpers.cc:71
auto eintrSafeCall(Fun &&function, Args &&... args)
void unrefRead()
int64_t bytesAvailableOnFD(int fd)
Definition: linuxhelpers.cc:62
bool blockAllSignalsForCurrentThread()
Definition: linuxhelpers.cc:15
auto eintrSafeCallEx(const Fun &function, const RestartCb &restartCb, Args &&... args)