Android-cuttlefish cvd tool
Public Member Functions | Static Public Member Functions | Static Private Member Functions | Private Attributes | Friends | List of all members
cuttlefish::SharedFD Class Reference

#include <shared_fd.h>

Public Member Functions

 SharedFD ()
 
 SharedFD (const std::shared_ptr< Fd > &in)
 
 SharedFD (SharedFD const &)=default
 
 SharedFD (SharedFD &&other)
 
 SharedFD (Fd other)
 
SharedFDoperator= (SharedFD const &)=default
 
SharedFDoperator= (SharedFD &&other)
 
SharedFDoperator= (Fd other)
 
auto operator<=> (const SharedFD &) const =default
 
std::shared_ptr< Fdoperator-> () const
 
const Fdoperator* () const
 
Fdoperator* ()
 

Static Public Member Functions

static SharedFD Dup (int unmanaged_fd)
 
static SharedFD Open (const char *pathname, int flags, mode_t mode=0)
 
static SharedFD Open (const std::string &pathname, int flags, mode_t mode=0)
 
static bool Pipe (SharedFD *fd0, SharedFD *fd1)
 
static SharedFD MemfdCreateWithData (const std::string &name, const std::string &data, unsigned int flags=0)
 
static Result< std::pair< SharedFD, std::string > > Mkostemp (std::string_view path, int flags=O_CLOEXEC)
 
static int Poll (PollSharedFd *fds, size_t num_fds, int timeout)
 
static int Poll (std::vector< PollSharedFd > &fds, int timeout)
 
static bool SocketPair (int domain, int type, int protocol, SharedFD *fd0, SharedFD *fd1)
 
static Result< std::pair< SharedFD, SharedFD > > SocketPair (int domain, int type, int protocol)
 
static SharedFD Socket (int domain, int socket_type, int protocol)
 
static SharedFD SocketLocalClient (const std::string &name, bool is_abstract, int in_type)
 
static SharedFD SocketLocalClient (const std::string &name, bool is_abstract, int in_type, int timeout_seconds)
 
static SharedFD SocketLocalClient (int port, int type)
 
static SharedFD SocketClient (const std::string &host, int port, int type, std::chrono::seconds timeout=std::chrono::seconds(0))
 
static SharedFD Socket6Client (const std::string &host, const std::string &interface, int port, int type, std::chrono::seconds timeout=std::chrono::seconds(0))
 
static SharedFD SocketLocalServer (const std::string &name, bool is_abstract, int in_type, mode_t mode)
 
static SharedFD SocketLocalServer (int port, int type)
 

Static Private Member Functions

static SharedFD ErrorFD (int error)
 

Private Attributes

std::shared_ptr< Fdvalue_
 

Friends

class WeakFD
 

Detailed Description

Counted reference to a Fd.

This is also the place where most new Fds are created. The creation methods correspond to the underlying POSIX calls.

SharedFDs can be compared and stored in STL containers. The semantics are slightly different from POSIX file descriptors:

o The value of the SharedFD is the identity of its underlying Fd.

o Each newly created SharedFD has a unique, closed Fd: SharedFD a, b; assert (a != b); a = b; assert(a == b);

o The identity of the Fd is not affected by closing the file: SharedFD a, b; set<SharedFD> s; s.insert(a); assert(s.count(a) == 1); assert(s.count(b) == 0); a->Close(); assert(s.count(a) == 1); assert(s.count(b) == 0);

o Fds are never visibly recycled.

o If all of the SharedFDs referring to a Fd go out of scope the file is closed and the Fd is recycled.

Creation methods must ensure that no references to the new file descriptor escape. The underlying Fd should have the only reference to the file descriptor. Any method that needs to know the fd must be in either SharedFD or Fd.

SharedFDs always have an underlying Fd, so all of the method calls are safe in accordance with the null object pattern.

Errors on system calls that create new Fds, such as Open, are reported with a new, closed Fd with the errno set.

Constructor & Destructor Documentation

◆ SharedFD() [1/5]

cuttlefish::SharedFD::SharedFD ( )

◆ SharedFD() [2/5]

cuttlefish::SharedFD::SharedFD ( const std::shared_ptr< Fd > &  in)
inline

◆ SharedFD() [3/5]

cuttlefish::SharedFD::SharedFD ( SharedFD const &  )
default

◆ SharedFD() [4/5]

cuttlefish::SharedFD::SharedFD ( SharedFD &&  other)

◆ SharedFD() [5/5]

cuttlefish::SharedFD::SharedFD ( Fd  other)

Member Function Documentation

◆ Dup()

SharedFD cuttlefish::SharedFD::Dup ( int  unmanaged_fd)
static

◆ ErrorFD()

SharedFD cuttlefish::SharedFD::ErrorFD ( int  error)
staticprivate

◆ MemfdCreateWithData()

SharedFD cuttlefish::SharedFD::MemfdCreateWithData ( const std::string &  name,
const std::string &  data,
unsigned int  flags = 0 
)
static

◆ Mkostemp()

Result< std::pair< SharedFD, std::string > > cuttlefish::SharedFD::Mkostemp ( std::string_view  path,
int  flags = O_CLOEXEC 
)
static

◆ Open() [1/2]

SharedFD cuttlefish::SharedFD::Open ( const char *  pathname,
int  flags,
mode_t  mode = 0 
)
static

◆ Open() [2/2]

SharedFD cuttlefish::SharedFD::Open ( const std::string &  pathname,
int  flags,
mode_t  mode = 0 
)
static

◆ operator*() [1/2]

Fd & cuttlefish::SharedFD::operator* ( )
inline

◆ operator*() [2/2]

const Fd & cuttlefish::SharedFD::operator* ( ) const
inline

◆ operator->()

std::shared_ptr< Fd > cuttlefish::SharedFD::operator-> ( ) const
inline

◆ operator<=>()

auto cuttlefish::SharedFD::operator<=> ( const SharedFD ) const
default

◆ operator=() [1/3]

SharedFD & cuttlefish::SharedFD::operator= ( Fd  other)

◆ operator=() [2/3]

SharedFD & cuttlefish::SharedFD::operator= ( SharedFD &&  other)

◆ operator=() [3/3]

SharedFD & cuttlefish::SharedFD::operator= ( SharedFD const &  )
default

◆ Pipe()

bool cuttlefish::SharedFD::Pipe ( SharedFD fd0,
SharedFD fd1 
)
static

◆ Poll() [1/2]

int cuttlefish::SharedFD::Poll ( PollSharedFd fds,
size_t  num_fds,
int  timeout 
)
static

◆ Poll() [2/2]

int cuttlefish::SharedFD::Poll ( std::vector< PollSharedFd > &  fds,
int  timeout 
)
static

◆ Socket()

SharedFD cuttlefish::SharedFD::Socket ( int  domain,
int  socket_type,
int  protocol 
)
static

◆ Socket6Client()

SharedFD cuttlefish::SharedFD::Socket6Client ( const std::string &  host,
const std::string &  interface,
int  port,
int  type,
std::chrono::seconds  timeout = std::chrono::seconds(0) 
)
static

◆ SocketClient()

SharedFD cuttlefish::SharedFD::SocketClient ( const std::string &  host,
int  port,
int  type,
std::chrono::seconds  timeout = std::chrono::seconds(0) 
)
static

◆ SocketLocalClient() [1/3]

SharedFD cuttlefish::SharedFD::SocketLocalClient ( const std::string &  name,
bool  is_abstract,
int  in_type 
)
static

◆ SocketLocalClient() [2/3]

SharedFD cuttlefish::SharedFD::SocketLocalClient ( const std::string &  name,
bool  is_abstract,
int  in_type,
int  timeout_seconds 
)
static

◆ SocketLocalClient() [3/3]

SharedFD cuttlefish::SharedFD::SocketLocalClient ( int  port,
int  type 
)
static

◆ SocketLocalServer() [1/2]

SharedFD cuttlefish::SharedFD::SocketLocalServer ( const std::string &  name,
bool  is_abstract,
int  in_type,
mode_t  mode 
)
static

◆ SocketLocalServer() [2/2]

SharedFD cuttlefish::SharedFD::SocketLocalServer ( int  port,
int  type 
)
static

◆ SocketPair() [1/2]

Result< std::pair< SharedFD, SharedFD > > cuttlefish::SharedFD::SocketPair ( int  domain,
int  type,
int  protocol 
)
static

◆ SocketPair() [2/2]

bool cuttlefish::SharedFD::SocketPair ( int  domain,
int  type,
int  protocol,
SharedFD fd0,
SharedFD fd1 
)
static

Friends And Related Function Documentation

◆ WeakFD

friend class WeakFD
friend

Member Data Documentation

◆ value_

std::shared_ptr<Fd> cuttlefish::SharedFD::value_
private

The documentation for this class was generated from the following files: