Fix typos and add utils

This commit is contained in:
Jianfeng Jiang
2023-05-31 10:46:51 +08:00
committed by Tate, Hongliang Tian
parent b2f2c55c9b
commit d96fe31e36
26 changed files with 388 additions and 71 deletions

View File

@ -2,6 +2,7 @@
use crate::events::Observer;
use crate::fs::utils::{AccessMode, IoEvents, IoctlCmd, Metadata, Poller, SeekFrom, StatusFlags};
use crate::net::socket::Socket;
use crate::prelude::*;
use core::any::Any;
@ -67,6 +68,10 @@ pub trait FileLike: Send + Sync + Any {
) -> Result<Weak<dyn Observer<IoEvents>>> {
return_errno_with_message!(Errno::EINVAL, "unregister_observer is not supported")
}
fn as_socket(&self) -> Option<&dyn Socket> {
None
}
}
impl dyn FileLike {

View File

@ -1,4 +1,5 @@
use crate::events::{Events, Observer, Subject};
use crate::net::socket::Socket;
use crate::prelude::*;
use core::cell::Cell;
@ -101,6 +102,12 @@ impl FileTable {
.ok_or(Error::with_message(Errno::EBADF, "fd not exits"))
}
pub fn get_socket(&self, sockfd: FileDescripter) -> Result<&dyn Socket> {
self.get_file(sockfd)?
.as_socket()
.ok_or_else(|| Error::with_message(Errno::ENOTSOCK, "the fd is not a socket"))
}
pub fn get_entry(&self, fd: FileDescripter) -> Result<&FileTableEntry> {
self.table
.get(fd as usize)

View File

@ -8,6 +8,7 @@ use keyable_arc::KeyableWeak;
/// A pollee maintains a set of active events, which can be polled with
/// pollers or be monitored with observers.
#[derive(Clone)]
pub struct Pollee {
inner: Arc<PolleeInner>,
}

View File

@ -16,14 +16,17 @@
#![feature(specialization)]
#![feature(fn_traits)]
#![feature(linked_list_remove)]
#![feature(trait_alias)]
#![feature(register_tool)]
#![feature(trait_upcasting)]
#![register_tool(component_access_control)]
use crate::{
prelude::*,
process::status::ProcessStatus,
thread::{kernel_thread::KernelThreadExt, Thread},
};
use jinux_frame::exit_qemu;
use process::Process;
extern crate alloc;
@ -36,6 +39,7 @@ pub mod driver;
pub mod error;
pub mod events;
pub mod fs;
pub mod net;
pub mod prelude;
mod process;
pub mod syscall;
@ -46,6 +50,7 @@ pub mod vm;
pub fn init(ramdisk: &[u8]) {
driver::init();
net::init();
process::fifo_scheduler::init();
fs::initramfs::init(ramdisk).unwrap();
device::init().unwrap();
@ -56,6 +61,7 @@ fn init_thread() {
"[kernel] Spawn init thread, tid = {}",
current_thread!().tid()
);
net::lazy_init();
// driver::pci::virtio::block::block_device_test();
let thread = Thread::spawn_kernel_thread(|| {
println!("[kernel] Hello world from kernel!");
@ -70,9 +76,14 @@ fn init_thread() {
);
print_banner();
run_busybox().expect("run busybox fails");
let busybox = run_busybox().expect("run busybox fails");
loop {
// If busybox becomes zombie, then exit qemu.
if *busybox.status().lock() == ProcessStatus::Zombie {
println!("Exit jinux.");
exit_qemu(jinux_frame::QemuExitCode::Success);
}
// We don't have preemptive scheduler now.
// The long running init thread should yield its own execution to allow other tasks to go on.
Thread::yield_now();

View File

@ -17,12 +17,13 @@ pub(crate) use core::any::Any;
pub(crate) use core::ffi::CStr;
pub(crate) use int_to_c_enum::TryFromInt;
pub(crate) use jinux_frame::config::PAGE_SIZE;
pub(crate) use jinux_frame::sync::{Mutex, MutexGuard};
// pub(crate) use jinux_frame::sync::{Mutex, MutexGuard};
pub(crate) use jinux_frame::vm::Vaddr;
pub(crate) use jinux_frame::{print, println};
pub(crate) use log::{debug, error, info, trace, warn};
pub(crate) use pod::Pod;
pub(crate) use spin::RwLock;
pub(crate) use spin::{Mutex, MutexGuard};
/// return current process
#[macro_export]

View File

@ -69,16 +69,30 @@ use crate::syscall::write::sys_write;
use crate::syscall::writev::sys_writev;
use jinux_frame::cpu::UserContext;
use self::accept::sys_accept;
use self::bind::sys_bind;
use self::connect::sys_connect;
use self::getpeername::sys_getpeername;
use self::getsockname::sys_getsockname;
use self::listen::sys_listen;
use self::pread64::sys_pread64;
use self::recvfrom::sys_recvfrom;
use self::sendto::sys_sendto;
use self::setsockopt::sys_setsockopt;
use self::shutdown::sys_shutdown;
use self::socket::sys_socket;
mod accept;
mod access;
mod arch_prctl;
mod bind;
mod brk;
mod chdir;
mod clock_gettime;
mod clock_nanosleep;
mod clone;
mod close;
mod connect;
mod constants;
mod dup;
mod epoll;
@ -93,15 +107,18 @@ mod getdents64;
mod getegid;
mod geteuid;
mod getgid;
mod getpeername;
mod getpgrp;
mod getpid;
mod getppid;
mod getsockname;
mod gettid;
mod gettimeofday;
mod getuid;
mod ioctl;
mod kill;
mod link;
mod listen;
mod lseek;
mod madvise;
mod mkdir;
@ -117,6 +134,7 @@ mod pread64;
mod prlimit64;
mod read;
mod readlink;
mod recvfrom;
mod rename;
mod rmdir;
mod rt_sigaction;
@ -124,9 +142,13 @@ mod rt_sigprocmask;
mod rt_sigreturn;
mod sched_yield;
mod select;
mod sendto;
mod set_robust_list;
mod set_tid_address;
mod setpgid;
mod setsockopt;
mod shutdown;
mod socket;
mod stat;
mod symlink;
mod tgkill;
@ -199,6 +221,17 @@ define_syscall_nums!(
SYS_DUP2 = 33,
SYS_PAUSE = 34,
SYS_GETPID = 39,
SYS_SOCKET = 41,
SYS_CONNECT = 42,
SYS_ACCEPT = 43,
SYS_SENDTO = 44,
SYS_RECVFROM = 45,
SYS_SHUTDOWN = 48,
SYS_BIND = 49,
SYS_LISTEN = 50,
SYS_GETSOCKNAME = 51,
SYS_GETPEERNAME = 52,
SYS_SETSOCKOPT = 54,
SYS_CLONE = 56,
SYS_FORK = 57,
SYS_EXECVE = 59,
@ -340,6 +373,17 @@ pub fn syscall_dispatch(
SYS_DUP2 => syscall_handler!(2, sys_dup2, args),
SYS_PAUSE => syscall_handler!(0, sys_pause),
SYS_GETPID => syscall_handler!(0, sys_getpid),
SYS_SOCKET => syscall_handler!(3, sys_socket, args),
SYS_CONNECT => syscall_handler!(3, sys_connect, args),
SYS_ACCEPT => syscall_handler!(3, sys_accept, args),
SYS_SENDTO => syscall_handler!(6, sys_sendto, args),
SYS_RECVFROM => syscall_handler!(6, sys_recvfrom, args),
SYS_SHUTDOWN => syscall_handler!(2, sys_shutdown, args),
SYS_BIND => syscall_handler!(3, sys_bind, args),
SYS_LISTEN => syscall_handler!(2, sys_listen, args),
SYS_GETSOCKNAME => syscall_handler!(3, sys_getsockname, args),
SYS_GETPEERNAME => syscall_handler!(3, sys_getpeername, args),
SYS_SETSOCKOPT => syscall_handler!(5, sys_setsockopt, args),
SYS_CLONE => syscall_handler!(5, sys_clone, args, context.clone()),
SYS_FORK => syscall_handler!(0, sys_fork, context.clone()),
SYS_EXECVE => syscall_handler!(3, sys_execve, args, context),

View File

@ -1,5 +1,10 @@
use crate::prelude::*;
use crate::{
net::socket::SocketAddr,
prelude::*,
util::net::{InAddr, SaFamily, SockAddr, SockAddrIn, SockAddrIn6, SockAddrUn},
};
use jinux_frame::vm::VmIo;
pub mod net;
/// copy bytes from user space of current process. The bytes len is the len of dest.
pub fn read_bytes_from_user(src: Vaddr, dest: &mut [u8]) -> Result<()> {
@ -35,3 +40,51 @@ pub fn read_cstring_from_user(addr: Vaddr, max_len: usize) -> Result<CString> {
read_bytes_from_user(addr, &mut buffer)?;
Ok(CString::from(CStr::from_bytes_until_nul(&buffer)?))
}
pub fn read_socket_addr_from_user(addr: Vaddr, addr_len: usize) -> Result<SocketAddr> {
debug_assert!(addr_len >= core::mem::size_of::<SockAddr>());
let sockaddr: SockAddr = read_val_from_user(addr)?;
let socket_addr = match sockaddr.sa_family()? {
SaFamily::AF_UNSPEC => {
return_errno_with_message!(Errno::EINVAL, "the socket addr family is unspecified")
}
SaFamily::AF_UNIX => {
debug_assert!(addr_len >= core::mem::size_of::<SockAddrUn>());
let sock_addr_un: SockAddrUn = read_val_from_user(addr)?;
todo!()
}
SaFamily::AF_INET => {
debug_assert!(addr_len >= core::mem::size_of::<SockAddrIn>());
let sock_addr_in: SockAddrIn = read_val_from_user(addr)?;
SocketAddr::from(sock_addr_in)
}
SaFamily::AF_INET6 => {
debug_assert!(addr_len >= core::mem::size_of::<SockAddrIn6>());
let sock_addr_in6: SockAddrIn6 = read_val_from_user(addr)?;
todo!()
}
_ => {
return_errno_with_message!(Errno::EAFNOSUPPORT, "cannot support address for the family")
}
};
Ok(socket_addr)
}
pub fn write_socket_addr_to_user(
socket_addr: &SocketAddr,
dest: Vaddr,
max_len: usize,
) -> Result<usize> {
match socket_addr {
SocketAddr::Unix => todo!(),
SocketAddr::IPv4(addr, port) => {
let in_addr = InAddr::from(*addr);
let sock_addr_in = SockAddrIn::new(*port, in_addr);
let write_size = core::mem::size_of::<SockAddrIn>();
debug_assert!(max_len >= write_size);
write_val_to_user(dest, &sock_addr_in)?;
Ok(write_size)
}
SocketAddr::IPv6 => todo!(),
}
}