From fa522fac1b77539f1402ab9368c9e2283b8e433f Mon Sep 17 00:00:00 2001 From: Rafal Mielniczuk Date: Fri, 20 Oct 2023 00:56:53 +0100 Subject: [PATCH] init: Better setup session for shell --- disk_scripts/install_os.sh | 17 +------ userspace/src/bin/init/main.rs | 16 ++++--- userspace/src/bin/shell/main.rs | 78 ++++++++++----------------------- 3 files changed, 35 insertions(+), 76 deletions(-) diff --git a/disk_scripts/install_os.sh b/disk_scripts/install_os.sh index b44f0498..523e7edb 100755 --- a/disk_scripts/install_os.sh +++ b/disk_scripts/install_os.sh @@ -24,21 +24,8 @@ for prog in $PROGS; do cp -f sysroot/build/$prog mnt/bin/$prog done -cp -r sysroot/cykusz/usr mnt/ -cp -r sysroot/cykusz/etc mnt/ - -#cp -r sysroot/cross/x86_64-cykusz/lib/* mnt/usr/lib/ -#cp sysroot/test.c mnt/ -#cp sysroot/stack.c mnt/ -cp sysroot/sum.cpp mnt/ -cp sysroot/money.cpp mnt/ -cp sysroot/lest.hpp mnt/ -cp sysroot/ncurses.c mnt/ -cp sysroot/kbd.c mnt/ -cp sysroot/float.c mnt/ -cp sysroot/ncurses mnt/ -#cp sysroot/hello.cpp mnt/ -##cp sysroot/cykusz/usr/bin/{readelf,objdump,nm,strings,size} mnt/bin/ +rsync -a sysroot/cykusz/usr mnt/ +rsync -a sysroot/cykusz/etc mnt/ mkdir -p mnt/etc mkdir -p mnt/home diff --git a/userspace/src/bin/init/main.rs b/userspace/src/bin/init/main.rs index 8fded0bb..b49fa592 100644 --- a/userspace/src/bin/init/main.rs +++ b/userspace/src/bin/init/main.rs @@ -9,14 +9,19 @@ extern crate syscall_user as syscall; use syscall_defs::waitpid::WaitPidFlags; fn spawn_shell() -> usize { + syscall::ioctl(0, syscall_defs::ioctl::tty::TIOCSCTTY, 0).expect("Failed to connect to tty"); if let Ok(pid) = syscall::fork() { if pid == 0 { - if let Err(e) = syscall::setsid() { - println!("[ init ] setsid failed {:?}", e); - } - syscall::ioctl(0, syscall_defs::ioctl::tty::TIOCSCTTY, 0) - .expect("Failed to attach tty"); syscall::setpgid(0, 0).expect("Failed to make process a group leader"); + + let pid = syscall::getpid().unwrap(); + syscall::ioctl( + 0, + syscall_defs::ioctl::tty::TIOCSPGRP, + core::ptr::addr_of!(pid) as usize, + ) + .expect("Failed to attach tty"); + if let Err(e) = syscall::exec( "/usr/bin/bash", None, @@ -27,6 +32,7 @@ fn spawn_shell() -> usize { unreachable!(); } else { + syscall::setpgid(pid, pid).expect("Failed to make process a group leader"); return pid; } } else { diff --git a/userspace/src/bin/shell/main.rs b/userspace/src/bin/shell/main.rs index 4e35e904..523ff7b8 100644 --- a/userspace/src/bin/shell/main.rs +++ b/userspace/src/bin/shell/main.rs @@ -768,68 +768,34 @@ fn sigchld_handler(_sig: usize) { } } -#[no_mangle] -pub fn main() { - { - let tty = Tty::new(); - tty.attach(); +fn setup_signal_handler(sig: usize, handler: SignalHandler, flags: SignalFlags) { + if let Err(e) = syscall::sigaction(sig, Some(&SigAction::new(handler, 0, flags)), None) { + println!("Failed to install signal handler: {:?}", e); } +} - if let Err(e) = syscall::sigaction( +#[no_mangle] +pub fn main() { + setup_signal_handler( syscall_defs::signal::SIGINT, - Some(&SigAction::new( - SignalHandler::Handle(sigint_handler), - 0, - SignalFlags::RESTART, - )), - None, - ) { - println!("Failed to install signal handler: {:?}", e); - } - if let Err(e) = syscall::sigaction( + SignalHandler::Handle(sigint_handler), + SignalFlags::RESTART, + ); + setup_signal_handler( syscall_defs::signal::SIGTSTP, - Some(&SigAction::new( - SignalHandler::Handle(sigint_handler), - 0, - SignalFlags::RESTART, - )), - None, - ) { - println!("Failed to install signal handler: {:?}", e); - } - if let Err(e) = syscall::sigaction( + SignalHandler::Handle(sigint_handler), + SignalFlags::RESTART, + ); + setup_signal_handler( syscall_defs::signal::SIGQUIT, - Some(&SigAction::new( - SignalHandler::Ignore, - 0, - SignalFlags::empty(), - )), - None, - ) { - println!("Failed to install signal handler: {:?}", e); - } - if let Err(e) = syscall::sigaction( - syscall_defs::signal::SIGCHLD, - Some(&SigAction::new( - SignalHandler::Ignore, - 0, - SignalFlags::RESTART, - )), - None, - ) { - println!("Failed to install signal handler: {:?}", e); - } - if let Err(e) = syscall::sigaction( + SignalHandler::Ignore, + SignalFlags::empty(), + ); + setup_signal_handler( syscall_defs::signal::SIGHUP, - Some(&SigAction::new( - SignalHandler::Ignore, - 0, - SignalFlags::RESTART, - )), - None, - ) { - println!("Failed to install signal handler: {:?}", e); - } + SignalHandler::Ignore, + SignalFlags::RESTART, + ); loop { let mut buf = [0u8; 256];