blob: 38503df5262c5ad2a904d6199a7121c1dc13b83d [file] [log] [blame]
Victor Hsieh79f296b2021-12-02 15:38:08 -08001/*
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
17use fuse::mount::MountOption;
18use std::fs::OpenOptions;
19use std::os::unix::io::AsRawFd;
20use std::path::Path;
21
22use super::AuthFs;
23
Victor Hsieh58a5e9b2022-03-09 21:57:26 +000024/// Maximum bytes (excluding the FUSE header) `AuthFs` will receive from the kernel for write
25/// operations by another process.
Victor Hsieh79f296b2021-12-02 15:38:08 -080026pub const MAX_WRITE_BYTES: u32 = 65536;
27
Victor Hsieh58a5e9b2022-03-09 21:57:26 +000028/// Maximum bytes (excluding the FUSE header) `AuthFs` will receive from the kernel for read
29/// operations by another process.
Victor Hsieh79f296b2021-12-02 15:38:08 -080030/// TODO(victorhsieh): This option is deprecated by FUSE. Figure out if we can remove this.
31const MAX_READ_BYTES: u32 = 65536;
32
33/// Mount and start the FUSE instance to handle messages. This requires CAP_SYS_ADMIN.
34pub 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 Hsiehbbac5192022-02-22 23:54:32 +000057 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 Hsieh79f296b2021-12-02 15:38:08 -080064
Victor Hsieh58a5e9b2022-03-09 21:57:26 +000065 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 Hsieh79f296b2021-12-02 15:38:08 -080068}