Skip to content

Commit

Permalink
init: Better setup session for shell
Browse files Browse the repository at this point in the history
  • Loading branch information
rafalmiel committed Oct 19, 2023
1 parent 8c4da29 commit fa522fa
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 76 deletions.
17 changes: 2 additions & 15 deletions disk_scripts/install_os.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 11 additions & 5 deletions userspace/src/bin/init/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand Down
78 changes: 22 additions & 56 deletions userspace/src/bin/shell/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down

0 comments on commit fa522fa

Please sign in to comment.