xref: /DragonOS/kernel/src/filesystem/procfs/syscall.rs (revision bd70d2d1f490aabd570a5301b858bd5eb04149fa)
18d72b68dSJomo use system_error::SystemError;
28d72b68dSJomo 
38d72b68dSJomo use crate::syscall::Syscall;
48d72b68dSJomo 
58d72b68dSJomo use super::kmsg::KMSG;
68d72b68dSJomo 
78d72b68dSJomo /// 操作内核环形缓冲区
88d72b68dSJomo enum SyslogAction {
98d72b68dSJomo     /// Close the log.  Currently a NOP.
10*b5b571e0SLoGin     Close = 0,
118d72b68dSJomo     /// Open the log. Currently a NOP.
12*b5b571e0SLoGin     Open = 1,
138d72b68dSJomo     /// Read from the log.
14*b5b571e0SLoGin     Read = 2,
158d72b68dSJomo     /// Read and clear all messages remaining in the ring buffer.
16*b5b571e0SLoGin     ReadClear = 4,
178d72b68dSJomo     /// Clear ring buffer.
18*b5b571e0SLoGin     Clear = 5,
198d72b68dSJomo     /// Set level of messages printed to console.
20*b5b571e0SLoGin     ConsoleLevel = 8,
218d72b68dSJomo     /// Return size of the log buffer.
22*b5b571e0SLoGin     SizeBuffer = 10,
238d72b68dSJomo     /// Invalid SyslogAction
24*b5b571e0SLoGin     Inval,
258d72b68dSJomo }
268d72b68dSJomo 
278d72b68dSJomo impl From<usize> for SyslogAction {
from(value: usize) -> Self288d72b68dSJomo     fn from(value: usize) -> Self {
298d72b68dSJomo         match value {
30*b5b571e0SLoGin             0 => SyslogAction::Close,
31*b5b571e0SLoGin             1 => SyslogAction::Open,
32*b5b571e0SLoGin             2 => SyslogAction::Read,
33*b5b571e0SLoGin             4 => SyslogAction::ReadClear,
34*b5b571e0SLoGin             5 => SyslogAction::Clear,
35*b5b571e0SLoGin             8 => SyslogAction::ConsoleLevel,
36*b5b571e0SLoGin             10 => SyslogAction::SizeBuffer,
37*b5b571e0SLoGin             _ => SyslogAction::Inval,
388d72b68dSJomo         }
398d72b68dSJomo     }
408d72b68dSJomo }
418d72b68dSJomo 
428d72b68dSJomo impl Syscall {
438d72b68dSJomo     /// # 操作内核环形缓冲区
448d72b68dSJomo     ///
458d72b68dSJomo     /// ## 参数
468d72b68dSJomo     /// - syslog_action_type: 操作码
478d72b68dSJomo     /// - buf:用户缓冲区
488d72b68dSJomo     /// - len: 需要从内核环形缓冲区读取的字节数。如果操作码为8,即SyslogActionConsoleLevel,则len为待设置的日志级别
498d72b68dSJomo     ///
508d72b68dSJomo     /// ## 返回值
518d72b68dSJomo     /// - 成功,Ok(usize)
528d72b68dSJomo     /// - 失败,Err(SystemError) 操作失败,返回posix错误码
538d72b68dSJomo     ///
548d72b68dSJomo 
do_syslog( syslog_action_type: usize, buf: &mut [u8], len: usize, ) -> Result<usize, SystemError>558d72b68dSJomo     pub fn do_syslog(
568d72b68dSJomo         syslog_action_type: usize,
578d72b68dSJomo         buf: &mut [u8],
588d72b68dSJomo         len: usize,
598d72b68dSJomo     ) -> Result<usize, SystemError> {
608d72b68dSJomo         let syslog_action = SyslogAction::from(syslog_action_type);
618d72b68dSJomo 
628d72b68dSJomo         let mut kmsg_guard = unsafe { KMSG.as_ref().unwrap().lock_irqsave() };
638d72b68dSJomo 
648d72b68dSJomo         match syslog_action {
65*b5b571e0SLoGin             SyslogAction::Close => Ok(0),
66*b5b571e0SLoGin             SyslogAction::Open => Ok(0),
67*b5b571e0SLoGin             SyslogAction::Read => kmsg_guard.read(buf),
68*b5b571e0SLoGin             SyslogAction::ReadClear => kmsg_guard.read_clear(buf),
69*b5b571e0SLoGin             SyslogAction::Clear => kmsg_guard.clear(),
70*b5b571e0SLoGin             SyslogAction::SizeBuffer => kmsg_guard.data_size(),
71*b5b571e0SLoGin             SyslogAction::ConsoleLevel => kmsg_guard.set_level(len),
72*b5b571e0SLoGin             SyslogAction::Inval => return Err(SystemError::EINVAL),
738d72b68dSJomo         }
748d72b68dSJomo     }
758d72b68dSJomo }
76