From 452aa40770592582830c90e1dab7f58325923e31 Mon Sep 17 00:00:00 2001 From: "Enrico Fraccaroli (Galfurian)" Date: Tue, 22 Aug 2023 16:32:43 -0400 Subject: [PATCH] Properly set exit code for killed processed. --- mentos/src/process/scheduler.c | 17 ++++++++------- mentos/src/system/signal.c | 38 ++++++++++++++++++++++------------ programs/shell.c | 5 +++++ programs/tests/t_shm_read.c | 3 +-- 4 files changed, 40 insertions(+), 23 deletions(-) diff --git a/mentos/src/process/scheduler.c b/mentos/src/process/scheduler.c index 52df7a2..24de4a2 100644 --- a/mentos/src/process/scheduler.c +++ b/mentos/src/process/scheduler.c @@ -4,10 +4,10 @@ /// See LICENSE.md for details. // Setup the logging for this file (do this before any other include). -#include "sys/kernel_levels.h" // Include kernel log levels. -#define __DEBUG_HEADER__ "[SCHED ]" ///< Change header. -#define __DEBUG_LEVEL__ LOGLEVEL_NOTICE ///< Set log level. -#include "io/debug.h" // Include debugging functions. +#include "sys/kernel_levels.h" // Include kernel log levels. +#define __DEBUG_HEADER__ "[SCHED ]" ///< Change header. +#define __DEBUG_LEVEL__ LOGLEVEL_DEBUG ///< Set log level. +#include "io/debug.h" // Include debugging functions. #include "process/scheduler_feedback.h" #include "process/scheduler.h" @@ -119,7 +119,7 @@ void scheduler_dequeue_task(task_struct *process) --runqueue.num_active; if (process->se.is_periodic) runqueue.num_periodic--; - + #ifdef ENABLE_SCHEDULER_FEEDBACK scheduler_feedback_task_remove(process->pid); #endif @@ -485,8 +485,9 @@ pid_t sys_waitpid(pid_t pid, int *status, int options) // Save the pid to return. pid_t ppid = entry->pid; // Save the state (TODO: Improve status set). - if (status) - (*status) = entry->state; + if (status) { + (*status) = entry->exit_code; + } // Finalize the VFS structures. vfs_destroy_task(entry); // Remove entry from children of parent. @@ -515,7 +516,7 @@ void sys_exit(int exit_code) } // Set the termination code of the process. - runqueue.curr->exit_code = (exit_code << 8) & 0xFF00; + runqueue.curr->exit_code = exit_code; // Set the state of the process to zombie. runqueue.curr->state = EXIT_ZOMBIE; // Send a SIGCHLD to the parent process. diff --git a/mentos/src/system/signal.c b/mentos/src/system/signal.c index 26de7b8..abd1aed 100644 --- a/mentos/src/system/signal.c +++ b/mentos/src/system/signal.c @@ -19,6 +19,8 @@ #include "klib/irqflags.h" #include "klib/stack_helper.h" +#define GET_EXIT_STATUS(status) (((status)&0x00FF) << 8) + /// SLAB caches for signal bits. static kmem_cache_t *sigqueue_cachep; @@ -477,28 +479,38 @@ int do_signal(struct pt_regs *f) continue; case SIGQUIT: + sys_exit(GET_EXIT_STATUS(1)); + continue; case SIGILL: + sys_exit(GET_EXIT_STATUS(132)); + continue; case SIGTRAP: - + sys_exit(GET_EXIT_STATUS(133)); + continue; case SIGABRT: - sys_exit(3); - + sys_exit(GET_EXIT_STATUS(134)); continue; case SIGFPE: - case SIGSEGV: + sys_exit(GET_EXIT_STATUS(136) | signr); + __unlock_task_sighand(current_process); + return 1; case SIGBUS: - case SIGSYS: + sys_exit(GET_EXIT_STATUS(138) | signr); + __unlock_task_sighand(current_process); + return 1; + case SIGSEGV: + sys_exit(GET_EXIT_STATUS(139) | signr); + __unlock_task_sighand(current_process); + return 1; case SIGXCPU: + sys_exit(GET_EXIT_STATUS(158) | signr); + __unlock_task_sighand(current_process); case SIGXFSZ: -#if 0 - if (do_coredump(signr, f)) - exit_code |= 0x80; -#endif + sys_exit(GET_EXIT_STATUS(159) | signr); + __unlock_task_sighand(current_process); + case SIGSYS: default: -#if 0 - current_process->flags |= PF_SIGNALED; -#endif - sys_exit(exit_code); + sys_exit(GET_EXIT_STATUS(exit_code) | signr); __unlock_task_sighand(current_process); return 1; } diff --git a/programs/shell.c b/programs/shell.c index 5d0d7f9..fda32ae 100644 --- a/programs/shell.c +++ b/programs/shell.c @@ -766,6 +766,11 @@ int main(int argc, char *argv[]) } if (blocking) { waitpid(cpid, &status, 0); + if (WIFSIGNALED(status)) { + printf(FG_RED "\nExit status %d, killed by signal %d\n" FG_RESET, WEXITSTATUS(status), WTERMSIG(status)); + } else if (WIFSTOPPED(status)) { + printf(FG_YELLOW "\nExit status %d, stopped by signal %d\n" FG_RESET, WEXITSTATUS(status), WSTOPSIG(status)); + } } } // Free up the memory reserved for the arguments. diff --git a/programs/tests/t_shm_read.c b/programs/tests/t_shm_read.c index c258c2a..31f89d1 100644 --- a/programs/tests/t_shm_read.c +++ b/programs/tests/t_shm_read.c @@ -40,8 +40,7 @@ int main(int argc, char *argv[]) } // Print the message. printf("Data read from memory: %s (%p)\n", str, str); - strcpy(str, "Hello!"); - // printf("Data read from memory: %s (%p)\n", str, str); + str[0] = 'H'; // Detatch the shared memory. if (shmdt(str) < 0) { perror("shmdt");