Skip to content

Commit

Permalink
fix some context
Browse files Browse the repository at this point in the history
  • Loading branch information
yfblock committed Mar 15, 2024
1 parent bd3cd31 commit 07bbb82
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 20 deletions.
2 changes: 2 additions & 0 deletions kernel/src/syscall/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ cfg_if::cfg_if! {
pub const SYS_SETPGID: usize = 154;
pub const SYS_GETPGID: usize = 155;
pub const SYS_SETSID: usize = 157;
pub const SYS_SETGROUPS: usize = 159;
pub const SYS_UNAME: usize = 160;
pub const SYS_GETRUSAGE: usize = 165;
pub const SYS_GETTIMEOFDAY: usize = 169;
Expand Down Expand Up @@ -330,6 +331,7 @@ cfg_if::cfg_if! {
pub const SYS_PWRITE: usize = 18;
pub const SYS_SENDFILE: usize = 40;
pub const SYS_SELECT: usize = 23;
pub const SYS_SETGROUPS: usize = 116;
pub const SYS_PSELECT: usize = 270;
pub const SYS_PPOLL: usize = 271;
pub const SYS_READLINK: usize = 89;
Expand Down
19 changes: 5 additions & 14 deletions kernel/src/syscall/fd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,19 +239,10 @@ impl UserTaskContainer {
.ok_or(LinuxError::EBADF)?;
file.stat(stat_ref)
.map_err(from_vfs)?;
// if let Ok(metadata) = file.metadata() {
// // stat_ref.ino = metadata.filename.as_ptr() as _;

// }
let _ = file.metadata().inspect(|x| {
if x.filename == "libz.so.1" {
stat_ref.ino = 1000;
}
if x.filename == "libcrypto.so.3" {
stat_ref.ino = 1001;
}
});
stat_ref.mode |= StatMode::OWNER_MASK | StatMode::GROUP_MASK | StatMode::OTHER_MASK;
stat_ref.mode |= StatMode::OWNER_MASK;
if let Ok(metadata) = file.metadata() {
stat_ref.ino = metadata.filename.as_ptr() as _;
}
Ok(0)
}

Expand All @@ -278,7 +269,7 @@ impl UserTaskContainer {
.node
.stat(stat)
.map_err(from_vfs)?;
stat.mode |= StatMode::OWNER_MASK | StatMode::GROUP_MASK | StatMode::OTHER_MASK;
stat.mode |= StatMode::OWNER_MASK;
Ok(0)
}

Expand Down
3 changes: 2 additions & 1 deletion kernel/src/syscall/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ impl UserTaskContainer {
.await
}
SYS_SOCKETPAIR => {
self.sys_socket_pair(args[0] as _, args[1] as _, args[2] as _, args[3] as _)
self.sys_socket_pair(args[0] as _, args[1] as _, args[2] as _, args[3].into())
.await
}
SYS_BIND => {
Expand Down Expand Up @@ -359,6 +359,7 @@ impl UserTaskContainer {
Ok(0)
}
SYS_SCHED_GETAFFINITY => self.sys_sched_getaffinity(args[0], args[1], args[2].into()).await,
SYS_SETGROUPS => Ok(0),
#[cfg(not(target_arch = "x86_64"))]
SYS_CLONE => {
self.sys_clone(
Expand Down
17 changes: 15 additions & 2 deletions kernel/src/syscall/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use sync::Lazy;
use vfscore::OpenFlags;

use crate::socket::{self, NetType};
use crate::user::socket_pair::create_socket_pair;
use crate::user::UserTaskContainer;

use super::consts::{LinuxError, UserRef};
Expand Down Expand Up @@ -81,13 +82,23 @@ impl UserTaskContainer {
domain: usize,
net_type: usize,
protocol: usize,
socket_vector: *mut u32,
socket_vector: UserRef<u32>,
) -> SysResult {
debug!(
"sys_socket_pair @ domain: {} net_type: {:#x} protocol: {} socket_vector: {:?}",
domain, net_type, protocol, socket_vector
);
self.sys_pipe2((socket_vector as usize).into(), 0).await?;
let fds = socket_vector.slice_mut_with_len(2);

let socket = create_socket_pair();
let rx_fd = self.task.alloc_fd().ok_or(LinuxError::ENFILE)?;
self.task.set_fd(rx_fd, FileItem::new_dev(socket.clone()));
fds[0] = rx_fd as u32;

let tx_fd = self.task.alloc_fd().ok_or(LinuxError::ENFILE)?;
self.task.set_fd(tx_fd, FileItem::new_dev(socket.clone()));
fds[1] = tx_fd as u32;

Ok(0)
}

Expand Down Expand Up @@ -378,6 +389,8 @@ impl UserTaskContainer {
// recv buffer
0x8 => *optval = 32000,
0x2 => *optval = 2000,
// getsockopt
0x4 => return Err(LinuxError::EPERM),
_ => {
// *optval = 2000;
}
Expand Down
1 change: 1 addition & 0 deletions kernel/src/user/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::{

pub mod entry;
pub mod signal;
pub mod socket_pair;

pub struct UserTaskContainer {
pub task: Arc<UserTask>,
Expand Down
63 changes: 63 additions & 0 deletions kernel/src/user/socket_pair.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use core::cmp;

use alloc::{
collections::VecDeque,
sync::Arc,
};
use sync::Mutex;
use vfscore::{INodeInterface, PollEvent, VfsResult};

pub struct SocketPair {
inner: Arc<Mutex<VecDeque<u8>>>
}

impl INodeInterface for SocketPair {
fn writeat(&self, _offset: usize, buffer: &[u8]) -> VfsResult<usize> {
let mut queue = self.inner.lock();
if queue.len() > 0x50000 {
Err(vfscore::VfsError::Blocking)
} else {
let wlen = buffer.len();
queue.extend(buffer.iter());
Ok(wlen)
}
}

fn readat(&self, _offset: usize, buffer: &mut [u8]) -> VfsResult<usize> {
let mut queue = self.inner.lock();
let rlen = cmp::min(queue.len(), buffer.len());
queue
.drain(..rlen)
.enumerate()
.into_iter()
.for_each(|(i, x)| {
buffer[i] = x;
});
if rlen == 0 {
Err(vfscore::VfsError::Blocking)
} else {
Ok(rlen)
}
}

fn poll(&self, events: PollEvent) -> VfsResult<PollEvent> {
let mut res = PollEvent::NONE;
if events.contains(PollEvent::POLLOUT) {
if self.inner.lock().len() <= 0x50000 {
res |= PollEvent::POLLOUT;
}
}
if events.contains(PollEvent::POLLIN) {
if self.inner.lock().len() > 0 {
res |= PollEvent::POLLIN;
}
}
Ok(res)
}
}

pub fn create_socket_pair() -> Arc<SocketPair> {
Arc::new(SocketPair {
inner: Arc::new(Mutex::new(VecDeque::new()))
})
}
9 changes: 6 additions & 3 deletions modules/executor/src/memset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,9 @@ impl MemArea {

/// Check the memory is overlapping.
pub fn overlapping(&self, start: usize, end: usize) -> bool {
let range = self.start..self.start + self.len;
let jrange = start..end;
range.contains(&start) || jrange.contains(&self.start)
let self_end = self.start + self.len;
let res = !((start <= self.start && end <= self.start) || (start >= self_end && end >= self_end));
res
}

/// write page to file
Expand All @@ -163,6 +163,9 @@ impl MemArea {
/// Sub the memory from this memory area.
/// the return value indicates whether the memory is splited.
pub fn sub(&mut self, start: usize, end: usize, pt: &PageTable) -> Option<MemArea> {
if !self.overlapping(start, end) {
return None;
}
let range = self.start..self.start + self.len;
let jrange = start..end;

Expand Down

0 comments on commit 07bbb82

Please sign in to comment.