aboutsummaryrefslogtreecommitdiff
path: root/src/net/sys/file_descriptor.cc
blob: b7653088be1d3c1f4810385d7586f90fe1b5fc89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/**
 * ---------------------------------------------------------------------------
 * @notice This file was adapted from the Minnow networking framework,
 * originally developed for Stanford University's CS144 course.
 * It has been incorporated into the Ricochet project as foundational
 * system utility code.
 * ---------------------------------------------------------------------------
 */

#include "ricochet/net/sys/file_descriptor.hh"

#include "ricochet/net/sys/exception.hh"

#include <fcntl.h>
#include <iostream>
#include <stdexcept>
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>

using namespace std;

size_t FileDescriptor::FDWrapper::CheckFDSystemCall( string_view what, ssize_t return_value ) const
{
  if ( return_value >= 0 ) {
    return return_value;
  }

  if ( non_blocking_ and ( errno == EAGAIN or errno == EWOULDBLOCK or errno == EINPROGRESS ) ) {
    return 0;
  }

  throw unix_error { what };
}

size_t FileDescriptor::FDWrapper::CheckRead( string_view what, ssize_t return_value )
{
  if ( return_value == 0 ) {
    eof_ = true;
  }

  return CheckFDSystemCall( what, return_value );
}

size_t FileDescriptor::CheckFDSystemCall( string_view what, ssize_t return_value ) const
{
  if ( not internal_fd_ ) {
    throw runtime_error( "internal error: missing internal_fd_" );
  }
  return internal_fd_->CheckFDSystemCall( what, return_value );
}

size_t FileDescriptor::CheckRead( string_view what, ssize_t return_value )
{
  if ( not internal_fd_ ) {
    throw runtime_error( "internal error: missing internal_fd_" );
  }
  return internal_fd_->CheckRead( what, return_value );
}

// fd is the file descriptor number returned by [open(2)](\ref man2::open) or similar
FileDescriptor::FDWrapper::FDWrapper( int fd ) : fd_( fd )
{
  if ( fd < 0 ) {
    throw runtime_error( "invalid fd number:" + to_string( fd ) );
  }

  const int flags = CheckSystemCall( "fcntl", fcntl( fd, F_GETFL ) ); // NOLINT(*-vararg)
  non_blocking_ = flags & O_NONBLOCK;                                 // NOLINT(*-bitwise)
}

void FileDescriptor::FDWrapper::close()
{
  CheckSystemCall( "close", ::close( fd_ ) );
  eof_ = closed_ = true;
}

FileDescriptor::FDWrapper::~FDWrapper()
{
  try {
    if ( closed_ ) {
      return;
    }
    close();
  } catch ( const exception& e ) {
    // don't throw an exception from the destructor
    cerr << "Exception destructing FDWrapper: " << e.what() << "\n";
  }
}

// fd is the file descriptor number returned by [open(2)](\ref man2::open) or similar
FileDescriptor::FileDescriptor( int fd ) : internal_fd_( make_shared<FDWrapper>( fd ) ) {}

// Private constructor used by duplicate()
FileDescriptor::FileDescriptor( shared_ptr<FDWrapper> other_shared_ptr ) : internal_fd_( move( other_shared_ptr ) )
{}

// returns a copy of this FileDescriptor
FileDescriptor FileDescriptor::duplicate() const
{
  return FileDescriptor { internal_fd_ };
}

// Read into a single buffer of given length (which if empty will be resized to a reasonable value before reading).
// Afer the read, the buffer will be resized to match whatever was read.
void FileDescriptor::read( string& buffer )
{
  if ( buffer.empty() ) {
    buffer.resize( kReadBufferSize );
  }

  const size_t bytes_read = CheckRead( "read", ::read( fd_num(), buffer.data(), buffer.size() ) );
  register_read();

  if ( bytes_read > buffer.size() ) {
    throw runtime_error( "read() read more than requested" );
  }

  buffer.resize( bytes_read );
}

// Read into a vector of buffers (if all empty, the last one will be resized to a reasonable value).
// After the read, the buffers will be resized to match whatever was read.
void FileDescriptor::read( vector<string>& buffers )
{
  if ( buffers.empty() ) {
    throw runtime_error( "FileDescriptor::read called with no buffers" );
  }

  if ( buffers.back().empty() ) {
    buffers.back().resize( kReadBufferSize );
  }

  static thread_local vector<iovec> iovecs;
  const size_t total_size = to_iovecs( buffers, iovecs );

  const size_t bytes_read
    = CheckRead( "readv", readv( fd_num(), iovecs.data(), static_cast<int>( iovecs.size() ) ) );
  register_read();

  if ( bytes_read > total_size ) {
    throw runtime_error( "read() read more than requested" );
  }

  size_t remaining_size = bytes_read;
  for ( auto& buf : buffers ) {
    if ( remaining_size >= buf.size() ) {
      remaining_size -= buf.size();
    } else {
      buf.resize( remaining_size );
      remaining_size = 0;
    }
  }
}

void FileDescriptor::write_all( string_view buffer )
{
  if ( not blocking() ) {
    throw runtime_error( "write_all requires a blocking file descriptor" );
    // otherwise, this could call write on a non-blocking fd that isn't "ready" for writing
  }

  while ( not buffer.empty() ) {
    buffer.remove_prefix( write( buffer ) );
  }
}

size_t FileDescriptor::write( string_view buffer )
{
  const size_t bytes_written = CheckFDSystemCall( "write", ::write( fd_num(), buffer.data(), buffer.size() ) );
  register_write();

  if ( bytes_written == 0 and not buffer.empty() ) {
    throw runtime_error( "write returned 0 given non-empty input buffer" );
  }

  if ( bytes_written > buffer.size() ) {
    throw runtime_error( "write wrote more than length of input buffer" );
  }

  return bytes_written;
}

size_t FileDescriptor::write( const std::vector<iovec>& iovecs, size_t total_size )
{
  const size_t bytes_written
    = CheckFDSystemCall( "writev", ::writev( fd_num(), iovecs.data(), static_cast<int>( iovecs.size() ) ) );
  register_write();

  if ( bytes_written == 0 and total_size != 0 ) {
    throw runtime_error( "writev returned 0 given non-empty input buffer" );
  }

  if ( bytes_written > total_size ) {
    throw runtime_error( "writev wrote more than length of input buffer" );
  }

  return bytes_written;
}

void FileDescriptor::set_blocking( bool blocking )
{
  int flags = CheckSystemCall( "fcntl", fcntl( fd_num(), F_GETFL ) ); // NOLINT(*-vararg)
  if ( blocking ) {
    flags ^= ( flags & O_NONBLOCK ); // NOLINT(*-bitwise)
  } else {
    flags |= O_NONBLOCK; // NOLINT(*-bitwise)
  }

  CheckSystemCall( "fcntl", fcntl( fd_num(), F_SETFL, flags ) ); // NOLINT(*-vararg)

  internal_fd_->non_blocking_ = not blocking;
}