summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDaichi Hirono2016-10-27 00:57:55 -0500
committerDaichi Hirono2016-10-27 01:04:15 -0500
commitc6134762975204ceebcf7949e364484833714345 (patch)
treeed01ec85a5175e2985e5e62986500da138d09d1d /libappfuse/FuseBuffer.cc
parent7f8e819ded361e963df7aa648ca2c6d49c7b46b6 (diff)
downloadplatform-system-core-c6134762975204ceebcf7949e364484833714345.tar.gz
platform-system-core-c6134762975204ceebcf7949e364484833714345.tar.xz
platform-system-core-c6134762975204ceebcf7949e364484833714345.zip
Add FuseBridgeLoop to libappfuse.
The CL adds FuseBridgeLoop class to libappfuse, which is used in the system service to proxy fuse commands to applications. Bug: 29970149 Test: libappfuse_test Change-Id: I0708f608b3868721ab16ba4028fd2c17a6735af7
Diffstat (limited to 'libappfuse/FuseBuffer.cc')
-rw-r--r--libappfuse/FuseBuffer.cc136
1 files changed, 136 insertions, 0 deletions
diff --git a/libappfuse/FuseBuffer.cc b/libappfuse/FuseBuffer.cc
new file mode 100644
index 000000000..45280a5cf
--- /dev/null
+++ b/libappfuse/FuseBuffer.cc
@@ -0,0 +1,136 @@
1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specic language governing permissions and
14 * limitations under the License.
15 */
16
17#include "libappfuse/FuseBuffer.h"
18
19#include <inttypes.h>
20#include <string.h>
21#include <unistd.h>
22
23#include <algorithm>
24
25#include <android-base/logging.h>
26#include <android-base/macros.h>
27
28namespace android {
29
30template <typename T, typename Header>
31bool FuseMessage<T, Header>::CheckHeaderLength() const {
32 if (sizeof(Header) <= header.len && header.len <= sizeof(T)) {
33 return true;
34 } else {
35 LOG(ERROR) << "Packet size is invalid=" << header.len;
36 return false;
37 }
38}
39
40template <typename T, typename Header>
41bool FuseMessage<T, Header>::CheckResult(
42 int result, const char* operation_name) const {
43 if (result >= 0 && static_cast<uint32_t>(result) == header.len) {
44 return true;
45 } else {
46 PLOG(ERROR) << "Failed to " << operation_name
47 << " a packet from FD. result=" << result << " header.len="
48 << header.len;
49 return false;
50 }
51}
52
53template <typename T, typename Header>
54bool FuseMessage<T, Header>::Read(int fd) {
55 const ssize_t result = TEMP_FAILURE_RETRY(::read(fd, this, sizeof(T)));
56 return CheckHeaderLength() && CheckResult(result, "read");
57}
58
59template <typename T, typename Header>
60bool FuseMessage<T, Header>::Write(int fd) const {
61 if (!CheckHeaderLength()) {
62 return false;
63 }
64 const ssize_t result = TEMP_FAILURE_RETRY(::write(fd, this, header.len));
65 return CheckResult(result, "write");
66}
67
68template struct FuseMessage<FuseRequest, fuse_in_header>;
69template struct FuseMessage<FuseResponse, fuse_out_header>;
70
71void FuseResponse::ResetHeader(
72 uint32_t data_length, int32_t error, uint64_t unique) {
73 CHECK_LE(error, 0) << "error should be zero or negative.";
74 header.len = sizeof(fuse_out_header) + data_length;
75 header.error = error;
76 header.unique = unique;
77}
78
79void FuseResponse::Reset(uint32_t data_length, int32_t error, uint64_t unique) {
80 memset(this, 0, sizeof(fuse_out_header) + data_length);
81 ResetHeader(data_length, error, unique);
82}
83
84void FuseBuffer::HandleInit() {
85 const fuse_init_in* const in = &request.init_in;
86
87 // Before writing |out|, we need to copy data from |in|.
88 const uint64_t unique = request.header.unique;
89 const uint32_t minor = in->minor;
90 const uint32_t max_readahead = in->max_readahead;
91
92 // Kernel 2.6.16 is the first stable kernel with struct fuse_init_out
93 // defined (fuse version 7.6). The structure is the same from 7.6 through
94 // 7.22. Beginning with 7.23, the structure increased in size and added
95 // new parameters.
96 if (in->major != FUSE_KERNEL_VERSION || in->minor < 6) {
97 LOG(ERROR) << "Fuse kernel version mismatch: Kernel version " << in->major
98 << "." << in->minor << " Expected at least " << FUSE_KERNEL_VERSION
99 << ".6";
100 response.Reset(0, -EPERM, unique);
101 return;
102 }
103
104 // We limit ourselves to 15 because we don't handle BATCH_FORGET yet
105 size_t response_size = sizeof(fuse_init_out);
106#if defined(FUSE_COMPAT_22_INIT_OUT_SIZE)
107 // FUSE_KERNEL_VERSION >= 23.
108
109 // If the kernel only works on minor revs older than or equal to 22,
110 // then use the older structure size since this code only uses the 7.22
111 // version of the structure.
112 if (minor <= 22) {
113 response_size = FUSE_COMPAT_22_INIT_OUT_SIZE;
114 }
115#endif
116
117 response.Reset(response_size, kFuseSuccess, unique);
118 fuse_init_out* const out = &response.init_out;
119 out->major = FUSE_KERNEL_VERSION;
120 // We limit ourselves to 15 because we don't handle BATCH_FORGET yet.
121 out->minor = std::min(minor, 15u);
122 out->max_readahead = max_readahead;
123 out->flags = FUSE_ATOMIC_O_TRUNC | FUSE_BIG_WRITES;
124 out->max_background = 32;
125 out->congestion_threshold = 32;
126 out->max_write = kFuseMaxWrite;
127}
128
129void FuseBuffer::HandleNotImpl() {
130 LOG(VERBOSE) << "NOTIMPL op=" << request.header.opcode << " uniq="
131 << request.header.unique << " nid=" << request.header.nodeid;
132 const uint64_t unique = request.header.unique;
133 response.Reset(0, -ENOSYS, unique);
134}
135
136} // namespace android