Victor Hsieh | 79f296b | 2021-12-02 15:38:08 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 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 specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | use fuse::mount::MountOption; |
| 18 | use std::fs::OpenOptions; |
| 19 | use std::os::unix::io::AsRawFd; |
| 20 | use std::path::Path; |
| 21 | |
| 22 | use super::AuthFs; |
| 23 | |
Victor Hsieh | 58a5e9b | 2022-03-09 21:57:26 +0000 | [diff] [blame^] | 24 | /// Maximum bytes (excluding the FUSE header) `AuthFs` will receive from the kernel for write |
| 25 | /// operations by another process. |
Victor Hsieh | 79f296b | 2021-12-02 15:38:08 -0800 | [diff] [blame] | 26 | pub const MAX_WRITE_BYTES: u32 = 65536; |
| 27 | |
Victor Hsieh | 58a5e9b | 2022-03-09 21:57:26 +0000 | [diff] [blame^] | 28 | /// Maximum bytes (excluding the FUSE header) `AuthFs` will receive from the kernel for read |
| 29 | /// operations by another process. |
Victor Hsieh | 79f296b | 2021-12-02 15:38:08 -0800 | [diff] [blame] | 30 | /// TODO(victorhsieh): This option is deprecated by FUSE. Figure out if we can remove this. |
| 31 | const MAX_READ_BYTES: u32 = 65536; |
| 32 | |
| 33 | /// Mount and start the FUSE instance to handle messages. This requires CAP_SYS_ADMIN. |
| 34 | pub fn mount_and_enter_message_loop( |
| 35 | authfs: AuthFs, |
| 36 | mountpoint: &Path, |
| 37 | extra_options: &Option<String>, |
| 38 | ) -> Result<(), fuse::Error> { |
| 39 | let dev_fuse = OpenOptions::new() |
| 40 | .read(true) |
| 41 | .write(true) |
| 42 | .open("/dev/fuse") |
| 43 | .expect("Failed to open /dev/fuse"); |
| 44 | |
| 45 | let mut mount_options = vec![ |
| 46 | MountOption::FD(dev_fuse.as_raw_fd()), |
| 47 | MountOption::RootMode(libc::S_IFDIR | libc::S_IXUSR | libc::S_IXGRP | libc::S_IXOTH), |
| 48 | MountOption::AllowOther, |
| 49 | MountOption::UserId(0), |
| 50 | MountOption::GroupId(0), |
| 51 | MountOption::MaxRead(MAX_READ_BYTES), |
| 52 | ]; |
| 53 | if let Some(value) = extra_options { |
| 54 | mount_options.push(MountOption::Extra(value)); |
| 55 | } |
| 56 | |
Victor Hsieh | bbac519 | 2022-02-22 23:54:32 +0000 | [diff] [blame] | 57 | fuse::mount( |
| 58 | mountpoint, |
| 59 | "authfs", |
| 60 | libc::MS_NOSUID | libc::MS_NODEV | libc::MS_NOEXEC, |
| 61 | &mount_options, |
| 62 | ) |
| 63 | .expect("Failed to mount fuse"); |
Victor Hsieh | 79f296b | 2021-12-02 15:38:08 -0800 | [diff] [blame] | 64 | |
Victor Hsieh | 58a5e9b | 2022-03-09 21:57:26 +0000 | [diff] [blame^] | 65 | let mut config = fuse::FuseConfig::new(); |
| 66 | config.dev_fuse(dev_fuse).max_write(MAX_WRITE_BYTES).max_read(MAX_READ_BYTES); |
| 67 | config.enter_message_loop(authfs) |
Victor Hsieh | 79f296b | 2021-12-02 15:38:08 -0800 | [diff] [blame] | 68 | } |