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

#include <fd.h>

Inheritance diagram for cuttlefish::Fd:
Inheritance graph
[legend]
Collaboration diagram for cuttlefish::Fd:
Collaboration graph
[legend]

Public Member Functions

 Fd ()
 
 Fd (Fd &)=delete
 
 Fd (Fd &&)
 
 ~Fd ()
 
Fdoperator= (Fd &)=delete
 
Fdoperator= (Fd &&)
 
int Bind (const struct sockaddr *addr, socklen_t addrlen)
 
int Connect (const struct sockaddr *addr, socklen_t addrlen)
 
int ConnectWithTimeout (const struct sockaddr *addr, socklen_t addrlen, struct timeval *timeout)
 
void Close ()
 
bool Chmod (mode_t mode)
 
bool CopyFrom (Fd &in, size_t length, Fd *stop=nullptr)
 
bool CopyAllFrom (Fd &in, Fd *stop=nullptr)
 
bool SendFile (Fd &in, off_t *offset, size_t count)
 
int UNMANAGED_Dup ()
 
int UNMANAGED_Dup2 (int newfd)
 
int Fchdir ()
 
int Fcntl (int command, int value)
 
int Fsync ()
 
Result< void > Flock (int operation)
 
int GetErrno () const
 
int GetSockName (struct sockaddr *addr, socklen_t *addrlen)
 
int Ioctl (int request, void *val=nullptr)
 
bool IsOpen () const
 
bool IsSet (fd_set *in) const
 
bool IsRegular () const
 
int LinkAtCwd (const std::string &path)
 
int Listen (int backlog)
 
off_t LSeek (off_t offset, int whence)
 
ssize_t Recv (void *buf, size_t len, int flags)
 
ssize_t RecvMsg (struct msghdr *msg, int flags)
 
Result< uint64_t > Read (void *buf, uint64_t count) override
 
Result< uint64_t > PRead (void *buf, uint64_t count, uint64_t offset) const override
 
ssize_t Send (const void *buf, size_t len, int flags)
 
ssize_t SendMsg (const struct msghdr *msg, int flags)
 
Result< uint64_t > SeekSet (uint64_t) override
 
Result< uint64_t > SeekCur (int64_t) override
 
Result< uint64_t > SeekEnd (int64_t) override
 
template<typename... Args>
ssize_t SendFileDescriptors (const void *buf, size_t len, Args &&... sent_fds)
 
int Shutdown (int how)
 
void Set (fd_set *dest, int *max_index) const
 
int SetSockOpt (int level, int optname, const void *optval, socklen_t optlen)
 
int GetSockOpt (int level, int optname, void *optval, socklen_t *optlen)
 
int SetTerminalRaw ()
 
std::string StrError () const
 
ScopedMMap MMap (void *addr, size_t length, int prot, int flags, off_t offset)
 
Result< void > Truncate (uint64_t length) override
 
Result< uint64_t > Write (const void *buf, uint64_t count) override
 
Result< uint64_t > PWrite (const void *buf, uint64_t count, uint64_t offset) override
 
bool IsATTY ()
 
int Futimens (const struct timespec times[2])
 
int InotifyAddWatch (const std::string &pathname, uint32_t mask)
 
void InotifyRmWatch (int watch)
 
- Public Member Functions inherited from cuttlefish::ReaderWriterSeeker
Result< void > Visit (IoVisitor &) override
 
Result< uint64_t > Read (void *buf, uint64_t count) override=0
 
Result< uint64_t > Write (const void *buf, uint64_t count) override=0
 
Result< uint64_t > SeekSet (uint64_t offset) override=0
 
Result< uint64_t > SeekCur (int64_t offset) override=0
 
Result< uint64_t > SeekEnd (int64_t offset) override=0
 
Result< uint64_t > PRead (void *buf, uint64_t count, uint64_t offset) const override=0
 
Result< uint64_t > PWrite (const void *buf, uint64_t count, uint64_t offset) override=0
 
Result< void > Truncate (uint64_t size) override
 
Result< void > Visit (IoVisitor &) override
 
virtual Result< uint64_t > PRead (void *buf, uint64_t count, uint64_t offset) const =0
 
- Public Member Functions inherited from cuttlefish::Reader
virtual ~Reader ()=default
 
Result< void > Visit (IoVisitor &) override
 
virtual Result< uint64_t > Read (void *buf, uint64_t count)=0
 
- Public Member Functions inherited from cuttlefish::IoVisitable
virtual ~IoVisitable ()=default
 
virtual Result< void > Visit (IoVisitor &)=0
 
- Public Member Functions inherited from cuttlefish::Seeker
virtual ~Seeker ()=default
 
Result< void > Visit (IoVisitor &) override
 
virtual Result< uint64_t > SeekSet (uint64_t offset)=0
 
virtual Result< uint64_t > SeekCur (int64_t offset)=0
 
virtual Result< uint64_t > SeekEnd (int64_t offset)=0
 
Result< void > Visit (IoVisitor &) override
 
virtual Result< uint64_t > PWrite (const void *buf, uint64_t count, uint64_t offset)=0
 
virtual Result< void > Truncate (uint64_t size)
 
- Public Member Functions inherited from cuttlefish::Writer
virtual ~Writer ()=default
 
Result< void > Visit (IoVisitor &) override
 
virtual Result< uint64_t > Write (const void *buf, uint64_t count)=0
 

Static Public Member Functions

static Result< FdAccept (const Fd &listener)
 
static Result< FdCreat (std::string_view path, mode_t mode)
 
static Result< FdDup (int unmanaged_fd)
 
static Result< FdFifo (std::string_view pathname, mode_t mode)
 
static Result< FdMemfdCreate (std::string_view name, unsigned int flags=0)
 
static Result< std::pair< Fd, std::string > > Mkostemp (std::string_view path, int flags=O_CLOEXEC)
 
static Result< FdOpen (std::string_view pathname, int flags, mode_t mode=0)
 
static Result< std::pair< Fd, Fd > > Pipe ()
 
static Result< std::pair< Fd, Fd > > SocketPair (int domain, int type, int protocol)
 
static Result< FdSocket (int domain, int socket_type, int protocol)
 
static Result< FdSocket6Client (std::string_view host, std::string_view interface, int port, int type, std::chrono::seconds timeout=std::chrono::seconds(0))
 
static Result< FdSocketClient (std::string_view host, int port, int type, std::chrono::seconds timeout=std::chrono::seconds(0))
 
static Result< FdSocketLocalClient (std::string_view name, bool is_abstract, int in_type)
 
static Result< FdSocketLocalClient (std::string_view name, bool is_abstract, int in_type, int timeout_seconds)
 
static Result< FdSocketLocalClient (int port, int type)
 
static Result< FdSocketLocalServer (std::string_view name, bool is_abstract, int in_type, mode_t mode)
 
static Result< FdSocketLocalServer (int port, int type)
 
static void Log (const char *message)
 

Private Member Functions

 Fd (int fd, int in_errno)
 

Static Private Member Functions

static Fd ErrorFD (int error)
 

Private Attributes

int fd_
 
int errno_
 
std::string identity_
 
bool is_regular_file_
 

Friends

class SharedFD
 
class Fd
 
class Epoll
 

Detailed Description

Tracks the lifetime of a file descriptor and provides methods to allow callers to use the file without knowledge of the underlying descriptor number.

Fds have two states: Open and Closed. They may start in either state.

Construction of Fds is limited to select classes to avoid escaping file descriptors.

Constructor & Destructor Documentation

◆ Fd() [1/4]

cuttlefish::Fd::Fd ( )

◆ Fd() [2/4]

cuttlefish::Fd::Fd ( Fd )
delete

◆ Fd() [3/4]

cuttlefish::Fd::Fd ( Fd &&  other)

◆ ~Fd()

cuttlefish::Fd::~Fd ( )

◆ Fd() [4/4]

cuttlefish::Fd::Fd ( int  fd,
int  in_errno 
)
private

Member Function Documentation

◆ Accept()

Result< Fd > cuttlefish::Fd::Accept ( const Fd listener)
static

◆ Bind()

int cuttlefish::Fd::Bind ( const struct sockaddr *  addr,
socklen_t  addrlen 
)

◆ Chmod()

bool cuttlefish::Fd::Chmod ( mode_t  mode)

◆ Close()

void cuttlefish::Fd::Close ( )

◆ Connect()

int cuttlefish::Fd::Connect ( const struct sockaddr *  addr,
socklen_t  addrlen 
)

◆ ConnectWithTimeout()

int cuttlefish::Fd::ConnectWithTimeout ( const struct sockaddr *  addr,
socklen_t  addrlen,
struct timeval *  timeout 
)

◆ CopyAllFrom()

bool cuttlefish::Fd::CopyAllFrom ( Fd in,
Fd stop = nullptr 
)

◆ CopyFrom()

bool cuttlefish::Fd::CopyFrom ( Fd in,
size_t  length,
Fd stop = nullptr 
)

◆ Creat()

Result< Fd > cuttlefish::Fd::Creat ( std::string_view  path,
mode_t  mode 
)
static

◆ Dup()

Result< Fd > cuttlefish::Fd::Dup ( int  unmanaged_fd)
static

◆ ErrorFD()

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

◆ Fchdir()

int cuttlefish::Fd::Fchdir ( )

◆ Fcntl()

int cuttlefish::Fd::Fcntl ( int  command,
int  value 
)

◆ Fifo()

Result< Fd > cuttlefish::Fd::Fifo ( std::string_view  pathname,
mode_t  mode 
)
static

◆ Flock()

Result< void > cuttlefish::Fd::Flock ( int  operation)

◆ Fsync()

int cuttlefish::Fd::Fsync ( )

◆ Futimens()

int cuttlefish::Fd::Futimens ( const struct timespec  times[2])

◆ GetErrno()

int cuttlefish::Fd::GetErrno ( ) const
inline

◆ GetSockName()

int cuttlefish::Fd::GetSockName ( struct sockaddr *  addr,
socklen_t *  addrlen 
)

◆ GetSockOpt()

int cuttlefish::Fd::GetSockOpt ( int  level,
int  optname,
void *  optval,
socklen_t *  optlen 
)

◆ InotifyAddWatch()

int cuttlefish::Fd::InotifyAddWatch ( const std::string &  pathname,
uint32_t  mask 
)

◆ InotifyRmWatch()

void cuttlefish::Fd::InotifyRmWatch ( int  watch)

◆ Ioctl()

int cuttlefish::Fd::Ioctl ( int  request,
void *  val = nullptr 
)

◆ IsATTY()

bool cuttlefish::Fd::IsATTY ( )

◆ IsOpen()

bool cuttlefish::Fd::IsOpen ( ) const
inline

◆ IsRegular()

bool cuttlefish::Fd::IsRegular ( ) const
inline

◆ IsSet()

bool cuttlefish::Fd::IsSet ( fd_set *  in) const

◆ LinkAtCwd()

int cuttlefish::Fd::LinkAtCwd ( const std::string &  path)

Adds a hard link to a file descriptor, based on the current working directory of the process or to some absolute path.

https://www.man7.org/linux/man-pages/man2/linkat.2.html

Using this on a file opened with O_TMPFILE can link it into the filesystem.

◆ Listen()

int cuttlefish::Fd::Listen ( int  backlog)

◆ Log()

void cuttlefish::Fd::Log ( const char *  message)
static

◆ LSeek()

off_t cuttlefish::Fd::LSeek ( off_t  offset,
int  whence 
)

◆ MemfdCreate()

Result< Fd > cuttlefish::Fd::MemfdCreate ( std::string_view  name,
unsigned int  flags = 0 
)
static

◆ Mkostemp()

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

◆ MMap()

ScopedMMap cuttlefish::Fd::MMap ( void *  addr,
size_t  length,
int  prot,
int  flags,
off_t  offset 
)

◆ Open()

Result< Fd > cuttlefish::Fd::Open ( std::string_view  pathname,
int  flags,
mode_t  mode = 0 
)
static

◆ operator=() [1/2]

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

◆ operator=() [2/2]

Fd & cuttlefish::Fd::operator= ( Fd )
delete

◆ Pipe()

Result< std::pair< Fd, Fd > > cuttlefish::Fd::Pipe ( )
static

◆ PRead()

Result< uint64_t > cuttlefish::Fd::PRead ( void *  buf,
uint64_t  count,
uint64_t  offset 
) const
overridevirtual

◆ PWrite()

Result< uint64_t > cuttlefish::Fd::PWrite ( const void *  buf,
uint64_t  count,
uint64_t  offset 
)
overridevirtual

◆ Read()

Result< uint64_t > cuttlefish::Fd::Read ( void *  buf,
uint64_t  count 
)
overridevirtual

◆ Recv()

ssize_t cuttlefish::Fd::Recv ( void *  buf,
size_t  len,
int  flags 
)

◆ RecvMsg()

ssize_t cuttlefish::Fd::RecvMsg ( struct msghdr *  msg,
int  flags 
)

◆ SeekCur()

Result< uint64_t > cuttlefish::Fd::SeekCur ( int64_t  offset)
overridevirtual

◆ SeekEnd()

Result< uint64_t > cuttlefish::Fd::SeekEnd ( int64_t  offset)
overridevirtual

◆ SeekSet()

Result< uint64_t > cuttlefish::Fd::SeekSet ( uint64_t  offset)
overridevirtual

◆ Send()

ssize_t cuttlefish::Fd::Send ( const void *  buf,
size_t  len,
int  flags 
)

◆ SendFile()

bool cuttlefish::Fd::SendFile ( Fd in,
off_t *  offset,
size_t  count 
)

◆ SendFileDescriptors()

template<typename... Args>
ssize_t cuttlefish::Fd::SendFileDescriptors ( const void *  buf,
size_t  len,
Args &&...  sent_fds 
)
inline

◆ SendMsg()

ssize_t cuttlefish::Fd::SendMsg ( const struct msghdr *  msg,
int  flags 
)

◆ Set()

void cuttlefish::Fd::Set ( fd_set *  dest,
int *  max_index 
) const

◆ SetSockOpt()

int cuttlefish::Fd::SetSockOpt ( int  level,
int  optname,
const void *  optval,
socklen_t  optlen 
)

◆ SetTerminalRaw()

int cuttlefish::Fd::SetTerminalRaw ( )

◆ Shutdown()

int cuttlefish::Fd::Shutdown ( int  how)

◆ Socket()

Result< Fd > cuttlefish::Fd::Socket ( int  domain,
int  socket_type,
int  protocol 
)
static

◆ Socket6Client()

Result< Fd > cuttlefish::Fd::Socket6Client ( std::string_view  host,
std::string_view  interface,
int  port,
int  type,
std::chrono::seconds  timeout = std::chrono::seconds(0) 
)
static

◆ SocketClient()

Result< Fd > cuttlefish::Fd::SocketClient ( std::string_view  host,
int  port,
int  type,
std::chrono::seconds  timeout = std::chrono::seconds(0) 
)
static

◆ SocketLocalClient() [1/3]

Result< Fd > cuttlefish::Fd::SocketLocalClient ( int  port,
int  type 
)
static

◆ SocketLocalClient() [2/3]

Result< Fd > cuttlefish::Fd::SocketLocalClient ( std::string_view  name,
bool  is_abstract,
int  in_type 
)
static

◆ SocketLocalClient() [3/3]

Result< Fd > cuttlefish::Fd::SocketLocalClient ( std::string_view  name,
bool  is_abstract,
int  in_type,
int  timeout_seconds 
)
static

◆ SocketLocalServer() [1/2]

Result< Fd > cuttlefish::Fd::SocketLocalServer ( int  port,
int  type 
)
static

◆ SocketLocalServer() [2/2]

Result< Fd > cuttlefish::Fd::SocketLocalServer ( std::string_view  name,
bool  is_abstract,
int  in_type,
mode_t  mode 
)
static

◆ SocketPair()

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

◆ StrError()

std::string cuttlefish::Fd::StrError ( ) const

◆ Truncate()

Result< void > cuttlefish::Fd::Truncate ( uint64_t  length)
overridevirtual

Reimplemented from cuttlefish::WriterSeeker.

◆ UNMANAGED_Dup()

int cuttlefish::Fd::UNMANAGED_Dup ( )

◆ UNMANAGED_Dup2()

int cuttlefish::Fd::UNMANAGED_Dup2 ( int  newfd)

◆ Write()

Result< uint64_t > cuttlefish::Fd::Write ( const void *  buf,
uint64_t  count 
)
overridevirtual

Friends And Related Function Documentation

◆ Epoll

friend class Epoll
friend

◆ Fd

cuttlefish::Fd::Fd
friend

◆ SharedFD

friend class SharedFD
friend

Member Data Documentation

◆ errno_

int cuttlefish::Fd::errno_
private

◆ fd_

int cuttlefish::Fd::fd_
private

◆ identity_

std::string cuttlefish::Fd::identity_
private

◆ is_regular_file_

bool cuttlefish::Fd::is_regular_file_
private

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