starnix_core/fs/fuchsia/
syslog.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;
6use crate::vfs::buffers::{InputBuffer, OutputBuffer};
7use crate::vfs::{
8    Anon, FileHandle, FileObject, FileOps, default_ioctl, fileops_impl_nonseekable,
9    fileops_impl_noop_sync,
10};
11use starnix_logging::log_info;
12use starnix_sync::{FileOpsCore, LockEqualOrBefore, Locked, Unlocked};
13use starnix_syscalls::{SyscallArg, SyscallResult};
14use starnix_uapi::errors::Errno;
15use starnix_uapi::open_flags::OpenFlags;
16
17pub struct SyslogFile;
18
19impl SyslogFile {
20    pub fn new_file<L>(locked: &mut Locked<L>, current_task: &CurrentTask) -> FileHandle
21    where
22        L: LockEqualOrBefore<FileOpsCore>,
23    {
24        // TODO: https://fxbug.dev/404739824 - Use a non-private node once labeling of external resources is addressed.
25        Anon::new_private_file(
26            locked,
27            current_task,
28            Box::new(SyslogFile),
29            OpenFlags::RDWR,
30            "[fuchsia:syslog]",
31        )
32    }
33}
34
35impl FileOps for SyslogFile {
36    fileops_impl_nonseekable!();
37    fileops_impl_noop_sync!();
38
39    fn write(
40        &self,
41        _locked: &mut Locked<FileOpsCore>,
42        _file: &FileObject,
43        _current_task: &CurrentTask,
44        offset: usize,
45        data: &mut dyn InputBuffer,
46    ) -> Result<usize, Errno> {
47        debug_assert!(offset == 0);
48        data.read_each(&mut |bytes| {
49            log_info!(tag = "stdio"; "{}", String::from_utf8_lossy(bytes));
50            Ok(bytes.len())
51        })
52    }
53
54    fn read(
55        &self,
56        _locked: &mut Locked<FileOpsCore>,
57        _file: &FileObject,
58        _current_task: &CurrentTask,
59        offset: usize,
60        _data: &mut dyn OutputBuffer,
61    ) -> Result<usize, Errno> {
62        debug_assert!(offset == 0);
63        Ok(0)
64    }
65
66    fn ioctl(
67        &self,
68        locked: &mut Locked<Unlocked>,
69        file: &FileObject,
70        current_task: &CurrentTask,
71        request: u32,
72        arg: SyscallArg,
73    ) -> Result<SyscallResult, Errno> {
74        default_ioctl(file, locked, current_task, request, arg)
75    }
76}