summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDaichi Hirono2016-10-26 20:37:05 -0500
committerDaichi Hirono2016-10-26 22:40:24 -0500
commit7f8e819ded361e963df7aa648ca2c6d49c7b46b6 (patch)
tree33e131ab74de2a90c895c66066d561fc923fae84 /libappfuse
parent459ec807a7c2da0e45cd30e43fa5bf12e5f017dd (diff)
downloadplatform-system-core-7f8e819ded361e963df7aa648ca2c6d49c7b46b6.tar.gz
platform-system-core-7f8e819ded361e963df7aa648ca2c6d49c7b46b6.tar.xz
platform-system-core-7f8e819ded361e963df7aa648ca2c6d49c7b46b6.zip
Add utility functions for FUSE.
The CL adds utility functions to framework to parse FUSE messages from the kernel. The library will be used from framework JNI and service JNI. Bug: 32260320 Test: libappfuse_test Change-Id: Ib89b26d34789e6c26a3288beceb3ea145c1ae780
Diffstat (limited to 'libappfuse')
-rw-r--r--libappfuse/Android.bp26
-rw-r--r--libappfuse/AppFuse.cc136
-rw-r--r--libappfuse/include/libappfuse/AppFuse.h76
-rw-r--r--libappfuse/tests/AppFuseTest.cc187
4 files changed, 425 insertions, 0 deletions
diff --git a/libappfuse/Android.bp b/libappfuse/Android.bp
new file mode 100644
index 000000000..8d24af10d
--- /dev/null
+++ b/libappfuse/Android.bp
@@ -0,0 +1,26 @@
1// Copyright 2016 The Android Open Source Project
2
3cc_defaults {
4 name: "libappfuse_defaults",
5 local_include_dirs: ["include"],
6 shared_libs: ["libbase"],
7 cflags: [
8 "-Wall",
9 "-Werror",
10 ],
11 clang: true
12}
13
14cc_library_shared {
15 name: "libappfuse",
16 defaults: ["libappfuse_defaults"],
17 export_include_dirs: ["include"],
18 srcs: ["AppFuse.cc"]
19}
20
21cc_test {
22 name: "libappfuse_test",
23 defaults: ["libappfuse_defaults"],
24 shared_libs: ["libappfuse"],
25 srcs: ["tests/AppFuseTest.cc"]
26}
diff --git a/libappfuse/AppFuse.cc b/libappfuse/AppFuse.cc
new file mode 100644
index 000000000..8701c4800
--- /dev/null
+++ b/libappfuse/AppFuse.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/AppFuse.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
diff --git a/libappfuse/include/libappfuse/AppFuse.h b/libappfuse/include/libappfuse/AppFuse.h
new file mode 100644
index 000000000..b6af48d78
--- /dev/null
+++ b/libappfuse/include/libappfuse/AppFuse.h
@@ -0,0 +1,76 @@
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#ifndef ANDROID_LIBAPPFUSE_APPFUSE_H_
18#define ANDROID_LIBAPPFUSE_APPFUSE_H_
19
20#include <linux/fuse.h>
21
22namespace android {
23
24// The numbers came from sdcard.c.
25// Maximum number of bytes to write/read in one request/one reply.
26constexpr size_t kFuseMaxWrite = 256 * 1024;
27constexpr size_t kFuseMaxRead = 128 * 1024;
28constexpr int32_t kFuseSuccess = 0;
29
30template<typename T, typename Header>
31struct FuseMessage {
32 Header header;
33 bool Read(int fd);
34 bool Write(int fd) const;
35 private:
36 bool CheckHeaderLength() const;
37 bool CheckResult(int result, const char* operation_name) const;
38};
39
40struct FuseRequest : public FuseMessage<FuseRequest, fuse_in_header> {
41 union {
42 struct {
43 fuse_write_in write_in;
44 char write_data[kFuseMaxWrite];
45 };
46 fuse_open_in open_in;
47 fuse_init_in init_in;
48 fuse_read_in read_in;
49 char lookup_name[];
50 };
51};
52
53struct FuseResponse : public FuseMessage<FuseResponse, fuse_out_header> {
54 union {
55 fuse_init_out init_out;
56 fuse_entry_out entry_out;
57 fuse_attr_out attr_out;
58 fuse_open_out open_out;
59 char read_data[kFuseMaxRead];
60 fuse_write_out write_out;
61 };
62 void Reset(uint32_t data_length, int32_t error, uint64_t unique);
63 void ResetHeader(uint32_t data_length, int32_t error, uint64_t unique);
64};
65
66union FuseBuffer {
67 FuseRequest request;
68 FuseResponse response;
69
70 void HandleInit();
71 void HandleNotImpl();
72};
73
74} // namespace android
75
76#endif // ANDROID_LIBAPPFUSE_APPFUSE_H_
diff --git a/libappfuse/tests/AppFuseTest.cc b/libappfuse/tests/AppFuseTest.cc
new file mode 100644
index 000000000..8c2cc4712
--- /dev/null
+++ b/libappfuse/tests/AppFuseTest.cc
@@ -0,0 +1,187 @@
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/AppFuse.h"
18
19#include <fcntl.h>
20#include <string.h>
21#include <sys/socket.h>
22
23#include <android-base/unique_fd.h>
24#include <gtest/gtest.h>
25
26namespace android {
27
28constexpr char kTempFile[] = "/data/local/tmp/appfuse_test_dump";
29
30void OpenTempFile(android::base::unique_fd* fd) {
31 fd->reset(open(kTempFile, O_CREAT | O_RDWR));
32 ASSERT_NE(-1, *fd) << strerror(errno);
33 unlink(kTempFile);
34 ASSERT_NE(-1, *fd) << strerror(errno);
35}
36
37void TestReadInvalidLength(size_t headerSize, size_t write_size) {
38 android::base::unique_fd fd;
39 OpenTempFile(&fd);
40
41 char buffer[std::max(headerSize, sizeof(FuseRequest))];
42 FuseRequest* const packet = reinterpret_cast<FuseRequest*>(buffer);
43 packet->header.len = headerSize;
44 ASSERT_NE(-1, write(fd, packet, write_size)) << strerror(errno);
45
46 lseek(fd, 0, SEEK_SET);
47 EXPECT_FALSE(packet->Read(fd));
48}
49
50void TestWriteInvalidLength(size_t size) {
51 android::base::unique_fd fd;
52 OpenTempFile(&fd);
53
54 char buffer[std::max(size, sizeof(FuseRequest))];
55 FuseRequest* const packet = reinterpret_cast<FuseRequest*>(buffer);
56 packet->header.len = size;
57 EXPECT_FALSE(packet->Write(fd));
58}
59
60// Use FuseRequest as a template instance of FuseMessage.
61
62TEST(FuseMessageTest, ReadAndWrite) {
63 android::base::unique_fd fd;
64 OpenTempFile(&fd);
65
66 FuseRequest request;
67 request.header.len = sizeof(FuseRequest);
68 request.header.opcode = 1;
69 request.header.unique = 2;
70 request.header.nodeid = 3;
71 request.header.uid = 4;
72 request.header.gid = 5;
73 request.header.pid = 6;
74 strcpy(request.lookup_name, "test");
75
76 ASSERT_TRUE(request.Write(fd));
77
78 memset(&request, 0, sizeof(FuseRequest));
79 lseek(fd, 0, SEEK_SET);
80
81 ASSERT_TRUE(request.Read(fd));
82 EXPECT_EQ(sizeof(FuseRequest), request.header.len);
83 EXPECT_EQ(1u, request.header.opcode);
84 EXPECT_EQ(2u, request.header.unique);
85 EXPECT_EQ(3u, request.header.nodeid);
86 EXPECT_EQ(4u, request.header.uid);
87 EXPECT_EQ(5u, request.header.gid);
88 EXPECT_EQ(6u, request.header.pid);
89 EXPECT_STREQ("test", request.lookup_name);
90}
91
92TEST(FuseMessageTest, Read_InconsistentLength) {
93 TestReadInvalidLength(sizeof(fuse_in_header), sizeof(fuse_in_header) + 1);
94}
95
96TEST(FuseMessageTest, Read_TooLong) {
97 TestReadInvalidLength(sizeof(FuseRequest) + 1, sizeof(FuseRequest) + 1);
98}
99
100TEST(FuseMessageTest, Read_TooShort) {
101 TestReadInvalidLength(sizeof(fuse_in_header) - 1, sizeof(fuse_in_header) - 1);
102}
103
104TEST(FuseMessageTest, Write_TooLong) {
105 TestWriteInvalidLength(sizeof(FuseRequest) + 1);
106}
107
108TEST(FuseMessageTest, Write_TooShort) {
109 TestWriteInvalidLength(sizeof(fuse_in_header) - 1);
110}
111
112TEST(FuseResponseTest, Reset) {
113 FuseResponse response;
114 // Write 1 to the first ten bytes.
115 memset(response.read_data, 'a', 10);
116
117 response.Reset(0, -1, 2);
118 EXPECT_EQ(sizeof(fuse_out_header), response.header.len);
119 EXPECT_EQ(-1, response.header.error);
120 EXPECT_EQ(2u, response.header.unique);
121 EXPECT_EQ('a', response.read_data[0]);
122 EXPECT_EQ('a', response.read_data[9]);
123
124 response.Reset(5, -4, 3);
125 EXPECT_EQ(sizeof(fuse_out_header) + 5, response.header.len);
126 EXPECT_EQ(-4, response.header.error);
127 EXPECT_EQ(3u, response.header.unique);
128 EXPECT_EQ(0, response.read_data[0]);
129 EXPECT_EQ(0, response.read_data[1]);
130 EXPECT_EQ(0, response.read_data[2]);
131 EXPECT_EQ(0, response.read_data[3]);
132 EXPECT_EQ(0, response.read_data[4]);
133 EXPECT_EQ('a', response.read_data[5]);
134}
135
136TEST(FuseResponseTest, ResetHeader) {
137 FuseResponse response;
138 // Write 1 to the first ten bytes.
139 memset(response.read_data, 'a', 10);
140
141 response.ResetHeader(0, -1, 2);
142 EXPECT_EQ(sizeof(fuse_out_header), response.header.len);
143 EXPECT_EQ(-1, response.header.error);
144 EXPECT_EQ(2u, response.header.unique);
145 EXPECT_EQ('a', response.read_data[0]);
146 EXPECT_EQ('a', response.read_data[9]);
147
148 response.ResetHeader(5, -4, 3);
149 EXPECT_EQ(sizeof(fuse_out_header) + 5, response.header.len);
150 EXPECT_EQ(-4, response.header.error);
151 EXPECT_EQ(3u, response.header.unique);
152 EXPECT_EQ('a', response.read_data[0]);
153 EXPECT_EQ('a', response.read_data[9]);
154}
155
156TEST(FuseBufferTest, HandleInit) {
157 FuseBuffer buffer;
158 memset(&buffer, 0, sizeof(FuseBuffer));
159
160 buffer.request.header.opcode = FUSE_INIT;
161 buffer.request.init_in.major = FUSE_KERNEL_VERSION;
162 buffer.request.init_in.minor = FUSE_KERNEL_MINOR_VERSION;
163
164 buffer.HandleInit();
165
166 ASSERT_EQ(sizeof(fuse_out_header) + sizeof(fuse_init_out),
167 buffer.response.header.len);
168 EXPECT_EQ(kFuseSuccess, buffer.response.header.error);
169 EXPECT_EQ(static_cast<unsigned int>(FUSE_KERNEL_VERSION),
170 buffer.response.init_out.major);
171 EXPECT_EQ(15u, buffer.response.init_out.minor);
172 EXPECT_EQ(static_cast<unsigned int>(FUSE_ATOMIC_O_TRUNC | FUSE_BIG_WRITES),
173 buffer.response.init_out.flags);
174 EXPECT_EQ(kFuseMaxWrite, buffer.response.init_out.max_write);
175}
176
177TEST(FuseBufferTest, HandleNotImpl) {
178 FuseBuffer buffer;
179 memset(&buffer, 0, sizeof(FuseBuffer));
180
181 buffer.HandleNotImpl();
182
183 ASSERT_EQ(sizeof(fuse_out_header), buffer.response.header.len);
184 EXPECT_EQ(-ENOSYS, buffer.response.header.error);
185}
186}
187 // namespace android