Skip to main content

starnix_core/vfs/socket/
socket_fs.rs

1// Copyright 2021 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use crate::task::{CurrentTask, Kernel};
6use crate::vfs::{
7    CacheMode, FileSystem, FileSystemHandle, FileSystemOps, FileSystemOptions, FsStr,
8};
9use starnix_types::vfs::default_statfs;
10use starnix_uapi::errors::Errno;
11use starnix_uapi::{SOCKFS_MAGIC, statfs};
12
13/// `SocketFs` is the file system where anonymous socket nodes are created, for example in
14/// `sys_socket`.
15pub struct SocketFs;
16impl FileSystemOps for SocketFs {
17    fn statfs(&self, _fs: &FileSystem, _current_task: &CurrentTask) -> Result<statfs, Errno> {
18        Ok(default_statfs(SOCKFS_MAGIC))
19    }
20    fn name(&self) -> &'static FsStr {
21        "sockfs".into()
22    }
23}
24
25/// Returns a handle to the `SocketFs` instance in `kernel`, initializing it if needed.
26pub fn socket_fs(kernel: &Kernel) -> FileSystemHandle {
27    struct SocketFsHandle(FileSystemHandle);
28
29    kernel
30        .expando
31        .get_or_init(|| {
32            SocketFsHandle(
33                FileSystem::new(
34                    kernel,
35                    CacheMode::Uncached,
36                    SocketFs,
37                    FileSystemOptions::default(),
38                )
39                .expect("socketfs constructed with valid options"),
40            )
41        })
42        .0
43        .clone()
44}