summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDaichi Hirono2017-03-22 02:41:46 -0500
committerDaichi Hirono2017-03-22 20:04:15 -0500
commit0bb22bd50ea7264684fa8c35b34fcc380e9d2cdc (patch)
treedc8ee89f3beba20ceda8e075554427c8709b3f37 /libappfuse/FuseBuffer.cc
parentce009490fbf3fffc7aa2d1cd91b1485d6e98eaf8 (diff)
downloadplatform-system-core-0bb22bd50ea7264684fa8c35b34fcc380e9d2cdc.tar.gz
platform-system-core-0bb22bd50ea7264684fa8c35b34fcc380e9d2cdc.tar.xz
platform-system-core-0bb22bd50ea7264684fa8c35b34fcc380e9d2cdc.zip
Add FuseMessage::WriteWithBody function
The funciton is going to be used to write FUSE header with external body. Bug: 35229514 Test: libappfuse_tests Change-Id: I303022b555deca960b8e08f26140a5ef10133efe
Diffstat (limited to 'libappfuse/FuseBuffer.cc')
-rw-r--r--libappfuse/FuseBuffer.cc64
1 files changed, 43 insertions, 21 deletions
diff --git a/libappfuse/FuseBuffer.cc b/libappfuse/FuseBuffer.cc
index 13cfc88ec..5bc549743 100644
--- a/libappfuse/FuseBuffer.cc
+++ b/libappfuse/FuseBuffer.cc
@@ -24,6 +24,7 @@
24#include <type_traits> 24#include <type_traits>
25 25
26#include <sys/socket.h> 26#include <sys/socket.h>
27#include <sys/uio.h>
27 28
28#include <android-base/file.h> 29#include <android-base/file.h>
29#include <android-base/logging.h> 30#include <android-base/logging.h>
@@ -34,9 +35,9 @@ namespace fuse {
34namespace { 35namespace {
35 36
36template <typename T> 37template <typename T>
37bool CheckHeaderLength(const FuseMessage<T>* self, const char* name) { 38bool CheckHeaderLength(const FuseMessage<T>* self, const char* name, size_t max_size) {
38 const auto& header = static_cast<const T*>(self)->header; 39 const auto& header = static_cast<const T*>(self)->header;
39 if (header.len >= sizeof(header) && header.len <= sizeof(T)) { 40 if (header.len >= sizeof(header) && header.len <= max_size) {
40 return true; 41 return true;
41 } else { 42 } else {
42 LOG(ERROR) << "Invalid header length is found in " << name << ": " << header.len; 43 LOG(ERROR) << "Invalid header length is found in " << name << ": " << header.len;
@@ -68,7 +69,7 @@ ResultOrAgain ReadInternal(FuseMessage<T>* self, int fd, int sockflag) {
68 return ResultOrAgain::kFailure; 69 return ResultOrAgain::kFailure;
69 } 70 }
70 71
71 if (!CheckHeaderLength<T>(self, "Read")) { 72 if (!CheckHeaderLength<T>(self, "Read", sizeof(T))) {
72 return ResultOrAgain::kFailure; 73 return ResultOrAgain::kFailure;
73 } 74 }
74 75
@@ -81,15 +82,26 @@ ResultOrAgain ReadInternal(FuseMessage<T>* self, int fd, int sockflag) {
81} 82}
82 83
83template <typename T> 84template <typename T>
84ResultOrAgain WriteInternal(const FuseMessage<T>* self, int fd, int sockflag) { 85ResultOrAgain WriteInternal(const FuseMessage<T>* self, int fd, int sockflag, const void* data,
85 if (!CheckHeaderLength<T>(self, "Write")) { 86 size_t max_size) {
87 if (!CheckHeaderLength<T>(self, "Write", max_size)) {
86 return ResultOrAgain::kFailure; 88 return ResultOrAgain::kFailure;
87 } 89 }
88 90
89 const char* const buf = reinterpret_cast<const char*>(self); 91 const char* const buf = reinterpret_cast<const char*>(self);
90 const auto& header = static_cast<const T*>(self)->header; 92 const auto& header = static_cast<const T*>(self)->header;
91 const int result = sockflag ? TEMP_FAILURE_RETRY(send(fd, buf, header.len, sockflag)) 93
92 : TEMP_FAILURE_RETRY(write(fd, buf, header.len)); 94 int result;
95 if (sockflag) {
96 CHECK(data == nullptr);
97 result = TEMP_FAILURE_RETRY(send(fd, buf, header.len, sockflag));
98 } else if (data) {
99 const struct iovec vec[] = {{const_cast<char*>(buf), sizeof(header)},
100 {const_cast<void*>(data), header.len - sizeof(header)}};
101 result = TEMP_FAILURE_RETRY(writev(fd, vec, arraysize(vec)));
102 } else {
103 result = TEMP_FAILURE_RETRY(write(fd, buf, header.len));
104 }
93 105
94 if (result == -1) { 106 if (result == -1) {
95 if (errno == EAGAIN) { 107 if (errno == EAGAIN) {
@@ -143,16 +155,19 @@ ResultOrAgain FuseMessage<T>::ReadOrAgain(int fd) {
143 155
144template <typename T> 156template <typename T>
145bool FuseMessage<T>::Write(int fd) const { 157bool FuseMessage<T>::Write(int fd) const {
146 return WriteInternal(this, fd, 0) == ResultOrAgain::kSuccess; 158 return WriteInternal(this, fd, 0, nullptr, sizeof(T)) == ResultOrAgain::kSuccess;
147} 159}
148 160
149template <typename T> 161template <typename T>
150ResultOrAgain FuseMessage<T>::WriteOrAgain(int fd) const { 162bool FuseMessage<T>::WriteWithBody(int fd, size_t max_size, const void* data) const {
151 return WriteInternal(this, fd, MSG_DONTWAIT); 163 CHECK(data != nullptr);
164 return WriteInternal<T>(this, fd, 0, data, max_size) == ResultOrAgain::kSuccess;
152} 165}
153 166
154template class FuseMessage<FuseRequest>; 167template <typename T>
155template class FuseMessage<FuseResponse>; 168ResultOrAgain FuseMessage<T>::WriteOrAgain(int fd) const {
169 return WriteInternal(this, fd, MSG_DONTWAIT, nullptr, sizeof(T));
170}
156 171
157void FuseRequest::Reset( 172void FuseRequest::Reset(
158 uint32_t data_length, uint32_t opcode, uint64_t unique) { 173 uint32_t data_length, uint32_t opcode, uint64_t unique) {
@@ -162,17 +177,18 @@ void FuseRequest::Reset(
162 header.unique = unique; 177 header.unique = unique;
163} 178}
164 179
165void FuseResponse::ResetHeader( 180template <size_t N>
166 uint32_t data_length, int32_t error, uint64_t unique) { 181void FuseResponseBase<N>::ResetHeader(uint32_t data_length, int32_t error, uint64_t unique) {
167 CHECK_LE(error, 0) << "error should be zero or negative."; 182 CHECK_LE(error, 0) << "error should be zero or negative.";
168 header.len = sizeof(fuse_out_header) + data_length; 183 header.len = sizeof(fuse_out_header) + data_length;
169 header.error = error; 184 header.error = error;
170 header.unique = unique; 185 header.unique = unique;
171} 186}
172 187
173void FuseResponse::Reset(uint32_t data_length, int32_t error, uint64_t unique) { 188template <size_t N>
174 memset(this, 0, sizeof(fuse_out_header) + data_length); 189void FuseResponseBase<N>::Reset(uint32_t data_length, int32_t error, uint64_t unique) {
175 ResetHeader(data_length, error, unique); 190 memset(this, 0, sizeof(fuse_out_header) + data_length);
191 ResetHeader(data_length, error, unique);
176} 192}
177 193
178void FuseBuffer::HandleInit() { 194void FuseBuffer::HandleInit() {
@@ -222,5 +238,11 @@ void FuseBuffer::HandleNotImpl() {
222 response.Reset(0, -ENOSYS, unique); 238 response.Reset(0, -ENOSYS, unique);
223} 239}
224 240
241template class FuseMessage<FuseRequest>;
242template class FuseMessage<FuseResponse>;
243template class FuseMessage<FuseSimpleResponse>;
244template struct FuseResponseBase<0u>;
245template struct FuseResponseBase<kFuseMaxRead>;
246
225} // namespace fuse 247} // namespace fuse
226} // namespace android 248} // namespace android