blob: acb963cfcce33a87089c1ef0122526da1f24cdf3 [file] [log] [blame]
Daichi Hironoc6134762016-10-27 14:57:55 +09001/*
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/FuseBridgeLoop.h"
18
19#include <android-base/logging.h>
20#include <android-base/unique_fd.h>
21
22namespace android {
23
24bool FuseBridgeLoop::Start(
25 int raw_dev_fd, int raw_proxy_fd, FuseBridgeLoop::Callback* callback) {
26 base::unique_fd dev_fd(raw_dev_fd);
27 base::unique_fd proxy_fd(raw_proxy_fd);
Daichi Hironoa0aecda2016-11-08 10:17:51 +090028 fuse::FuseBuffer buffer;
Daichi Hironoc6134762016-10-27 14:57:55 +090029
30 LOG(DEBUG) << "Start fuse loop.";
31 while (true) {
Daichi Hironoa0aecda2016-11-08 10:17:51 +090032 if (!buffer.request.Read(dev_fd)) {
Daichi Hironoc6134762016-10-27 14:57:55 +090033 return false;
34 }
35
Daichi Hironoa0aecda2016-11-08 10:17:51 +090036 const uint32_t opcode = buffer.request.header.opcode;
Daichi Hironoc6134762016-10-27 14:57:55 +090037 LOG(VERBOSE) << "Read a fuse packet, opcode=" << opcode;
38 switch (opcode) {
39 case FUSE_FORGET:
40 // Do not reply to FUSE_FORGET.
41 continue;
42
43 case FUSE_LOOKUP:
44 case FUSE_GETATTR:
45 case FUSE_OPEN:
46 case FUSE_READ:
47 case FUSE_WRITE:
48 case FUSE_RELEASE:
Daichi Hironoa0aecda2016-11-08 10:17:51 +090049 case FUSE_FSYNC:
50 if (!buffer.request.Write(proxy_fd)) {
Daichi Hironoc6134762016-10-27 14:57:55 +090051 LOG(ERROR) << "Failed to write a request to the proxy.";
52 return false;
53 }
Daichi Hironoa0aecda2016-11-08 10:17:51 +090054 if (!buffer.response.Read(proxy_fd)) {
Daichi Hironoc6134762016-10-27 14:57:55 +090055 LOG(ERROR) << "Failed to read a response from the proxy.";
56 return false;
57 }
58 break;
59
60 case FUSE_INIT:
Daichi Hironoa0aecda2016-11-08 10:17:51 +090061 buffer.HandleInit();
Daichi Hironoc6134762016-10-27 14:57:55 +090062 break;
63
64 default:
Daichi Hironoa0aecda2016-11-08 10:17:51 +090065 buffer.HandleNotImpl();
Daichi Hironoc6134762016-10-27 14:57:55 +090066 break;
67 }
68
Daichi Hironoa0aecda2016-11-08 10:17:51 +090069 if (!buffer.response.Write(dev_fd)) {
Daichi Hironoc6134762016-10-27 14:57:55 +090070 LOG(ERROR) << "Failed to write a response to the device.";
71 return false;
72 }
73
74 if (opcode == FUSE_INIT) {
75 callback->OnMount();
76 }
77 }
78}
79
Daichi Hironoa0aecda2016-11-08 10:17:51 +090080namespace fuse {
81
82bool StartFuseBridgeLoop(
83 int raw_dev_fd, int raw_proxy_fd, FuseBridgeLoopCallback* callback) {
84 return FuseBridgeLoop().Start(raw_dev_fd, raw_proxy_fd, callback);
85}
86
87} // namespace fuse
Daichi Hironoc6134762016-10-27 14:57:55 +090088} // namespace android