diff --git a/libmaxsi/Makefile b/libmaxsi/Makefile index 2cda163e..20e058b1 100644 --- a/libmaxsi/Makefile +++ b/libmaxsi/Makefile @@ -48,7 +48,6 @@ settermmode.o \ isatty.o \ kernelinfo.o \ init.o \ -exit.o \ signal.o \ $(CPU)/signal.o \ $(CPU)/fork.o \ @@ -66,6 +65,7 @@ sort.o \ string.o \ error.o \ format.o \ +abort.o \ access.o \ _assert.o \ chdir.o \ @@ -73,6 +73,9 @@ chmod.o \ close.o \ dup.o \ errorprint.o \ +exit.o \ +_exit.o \ +_Exit.o \ fchmod.o \ fcntl.o \ fstat.o \ @@ -83,6 +86,7 @@ lseek.o \ mbtowc.o \ mkdir.o \ mktemp.o \ +on_exit.o \ open.o \ pipe.o \ print.o \ diff --git a/libmaxsi/_Exit.cpp b/libmaxsi/_Exit.cpp new file mode 100644 index 00000000..66378776 --- /dev/null +++ b/libmaxsi/_Exit.cpp @@ -0,0 +1,31 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2012. + + This file is part of LibMaxsi. + + LibMaxsi is free software: you can redistribute it and/or modify it under + the terms of the GNU Lesser General Public License as published by the Free + Software Foundation, either version 3 of the License, or (at your option) + any later version. + + LibMaxsi is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + details. + + You should have received a copy of the GNU Lesser General Public License + along with LibMaxsi. If not, see . + + _Exit.cpp + Terminates the current process. + +*******************************************************************************/ + +#include +#include + +extern "C" void _Exit(int status) +{ + _exit(status); +} diff --git a/libmaxsi/_exit.cpp b/libmaxsi/_exit.cpp new file mode 100644 index 00000000..070130d1 --- /dev/null +++ b/libmaxsi/_exit.cpp @@ -0,0 +1,38 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2011, 2012. + + This file is part of LibMaxsi. + + LibMaxsi is free software: you can redistribute it and/or modify it under + the terms of the GNU Lesser General Public License as published by the Free + Software Foundation, either version 3 of the License, or (at your option) + any later version. + + LibMaxsi is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + details. + + You should have received a copy of the GNU Lesser General Public License + along with LibMaxsi. If not, see . + + _exit.cpp + Terminates the current process. + +*******************************************************************************/ + +#include +#include +#include + +namespace Maxsi { +DEFN_SYSCALL1_VOID(sys_exit, SYSCALL_EXIT, int); +} // namespace Maxsi +using namespace Maxsi; + +extern "C" void _exit(int status) +{ + sys_exit(status); + while(true); // TODO: noreturn isn't set on sys_exit. +} diff --git a/libmaxsi/abort.cpp b/libmaxsi/abort.cpp new file mode 100644 index 00000000..599850fc --- /dev/null +++ b/libmaxsi/abort.cpp @@ -0,0 +1,31 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2011, 2012. + + This file is part of LibMaxsi. + + LibMaxsi is free software: you can redistribute it and/or modify it under + the terms of the GNU Lesser General Public License as published by the Free + Software Foundation, either version 3 of the License, or (at your option) + any later version. + + LibMaxsi is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + details. + + You should have received a copy of the GNU Lesser General Public License + along with LibMaxsi. If not, see . + + abort.cpp + Abnormal process termination. + +*******************************************************************************/ + +#include + +extern "C" void abort(void) +{ + // TODO: Send SIGABRT instead! + _Exit(128 + 6); +} diff --git a/libmaxsi/exit.cpp b/libmaxsi/exit.cpp index e6ea30d0..3207600f 100644 --- a/libmaxsi/exit.cpp +++ b/libmaxsi/exit.cpp @@ -1,6 +1,6 @@ /******************************************************************************* - COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2012. + Copyright(C) Jonas 'Sortie' Termansen 2011, 2012. This file is part of LibMaxsi. @@ -18,45 +18,20 @@ along with LibMaxsi. If not, see . exit.cpp - Hooks that is called upon process exit. + Terminates the current process. *******************************************************************************/ +#include +#include #include -struct exithandler -{ - void (*hook)(int, void*); - void* param; - struct exithandler* next; -}* exit_handler_stack = NULL; +extern "C" void call_exit_handlers(int status); -extern "C" int on_exit(void (*hook)(int, void*), void* param) +extern "C" void exit(int status) { - struct exithandler* handler = (struct exithandler*) malloc(sizeof(struct exithandler)); - if ( !handler ) { return -1; } - handler->hook = hook; - handler->param = param; - handler->next = exit_handler_stack; - exit_handler_stack = handler; - return 0; -} - -static void atexit_adapter(int /*status*/, void* user) -{ - ((void (*)(void)) user)(); -} - -extern "C" int atexit(void (*hook)(void)) -{ - return on_exit(atexit_adapter, (void*) hook); -} - -extern "C" void call_exit_handlers(int status) -{ - while ( exit_handler_stack ) - { - exit_handler_stack->hook(status, exit_handler_stack->param); - exit_handler_stack = exit_handler_stack->next; - } + call_exit_handlers(status); + dcloseall(); + fcloseall(); + _Exit(status); } diff --git a/libmaxsi/include/libmaxsi/process.h b/libmaxsi/include/libmaxsi/process.h index 9653b65b..25519336 100644 --- a/libmaxsi/include/libmaxsi/process.h +++ b/libmaxsi/include/libmaxsi/process.h @@ -1,6 +1,6 @@ /******************************************************************************* - COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2011, 2012. + Copyright(C) Jonas 'Sortie' Termansen 2011, 2012. This file is part of LibMaxsi. @@ -29,8 +29,6 @@ namespace Maxsi { namespace Process { - void Abort(); - void Exit(int code); pid_t Fork(); pid_t GetPID(); pid_t GetParentPID(); diff --git a/libmaxsi/include/stdlib.h b/libmaxsi/include/stdlib.h index 76b47c48..4545848e 100644 --- a/libmaxsi/include/stdlib.h +++ b/libmaxsi/include/stdlib.h @@ -1,6 +1,6 @@ /******************************************************************************* - COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2011, 2012. + Copyright(C) Jonas 'Sortie' Termansen 2011, 2012. This file is part of LibMaxsi. @@ -47,15 +47,15 @@ typedef int div_t, ldiv_t, lldiv_t; /* TODO: WEXITSTATUS, WIFEXITED, WIFSIGNALED, WIFSTOPPED, WNOHANG, WSTOPSIG, WTERMSIG, WUNTRACED is missing here */ -void abort(void); +void abort(void) __attribute__ ((noreturn)); int abs(int value); int atexit(void (*function)(void)); int atoi(const char*); long atol(const char*); long long atoll(const char*); void* calloc(size_t, size_t); -void exit(int); -void _Exit(int status); +void exit(int) __attribute__ ((noreturn)); +void _Exit(int status) __attribute__ ((noreturn)); void free(void*); long labs(long); long long llabs(long long); diff --git a/libmaxsi/include/unistd.h b/libmaxsi/include/unistd.h index 656ad1f9..3d86de83 100644 --- a/libmaxsi/include/unistd.h +++ b/libmaxsi/include/unistd.h @@ -155,7 +155,7 @@ int access(const char*, int); int chdir(const char*); int close(int); int dup(int); -void _exit(int); +void _exit(int) __attribute__ ((noreturn)); int execl(const char*, ...); int execle(const char*, ...); int execlp(const char*, ...); diff --git a/libmaxsi/on_exit.cpp b/libmaxsi/on_exit.cpp new file mode 100644 index 00000000..d7c06952 --- /dev/null +++ b/libmaxsi/on_exit.cpp @@ -0,0 +1,62 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2012. + + This file is part of LibMaxsi. + + LibMaxsi is free software: you can redistribute it and/or modify it under + the terms of the GNU Lesser General Public License as published by the Free + Software Foundation, either version 3 of the License, or (at your option) + any later version. + + LibMaxsi is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + details. + + You should have received a copy of the GNU Lesser General Public License + along with LibMaxsi. If not, see . + + exit.cpp + Hooks that is called upon process exit. + +*******************************************************************************/ + +#include + +struct exithandler +{ + void (*hook)(int, void*); + void* param; + struct exithandler* next; +}* exit_handler_stack = NULL; + +extern "C" int on_exit(void (*hook)(int, void*), void* param) +{ + struct exithandler* handler = (struct exithandler*) malloc(sizeof(struct exithandler)); + if ( !handler ) { return -1; } + handler->hook = hook; + handler->param = param; + handler->next = exit_handler_stack; + exit_handler_stack = handler; + return 0; +} + +static void atexit_adapter(int /*status*/, void* user) +{ + ((void (*)(void)) user)(); +} + +extern "C" int atexit(void (*hook)(void)) +{ + return on_exit(atexit_adapter, (void*) hook); +} + +extern "C" void call_exit_handlers(int status) +{ + while ( exit_handler_stack ) + { + exit_handler_stack->hook(status, exit_handler_stack->param); + exit_handler_stack = exit_handler_stack->next; + } +} diff --git a/libmaxsi/process.cpp b/libmaxsi/process.cpp index f81cd750..7fc90e32 100644 --- a/libmaxsi/process.cpp +++ b/libmaxsi/process.cpp @@ -37,26 +37,12 @@ namespace Maxsi { namespace Process { - DEFN_SYSCALL1_VOID(SysExit, SYSCALL_EXIT, int); DEFN_SYSCALL3(int, SysExecVE, SYSCALL_EXEC, const char*, char* const*, char* const*); DEFN_SYSCALL2(pid_t, SysTFork, SYSCALL_TFORK, int, tforkregs_t*); DEFN_SYSCALL0(pid_t, SysGetPID, SYSCALL_GETPID); DEFN_SYSCALL0(pid_t, SysGetParentPID, SYSCALL_GETPPID); DEFN_SYSCALL3(pid_t, SysWait, SYSCALL_WAIT, pid_t, int*, int); - void Abort() - { - // TODO: Send SIGABRT instead! - Exit(128 + 6); - } - - extern "C" void abort() { return Abort(); } - - extern "C" void _exit(int status) - { - SysExit(status); - } - extern "C" int execve(const char* pathname, char* const* argv, char* const* envp) { @@ -174,16 +160,6 @@ namespace Maxsi return result; } - extern "C" void call_exit_handlers(int status); - - DUAL_FUNCTION(void, exit, Exit, (int status)) - { - call_exit_handlers(status); - dcloseall(); - fcloseall(); - _exit(status); - } - extern "C" pid_t tfork(int flags, tforkregs_t* regs) { return SysTFork(flags, regs); diff --git a/libmaxsi/signal.cpp b/libmaxsi/signal.cpp index 19bf3ad3..2aa7598e 100644 --- a/libmaxsi/signal.cpp +++ b/libmaxsi/signal.cpp @@ -26,6 +26,7 @@ #include #include #include +#include #include #include @@ -37,27 +38,27 @@ namespace Maxsi void Core(int signum) { - Process::Exit(128 + signum); + exit(128 + signum); } extern "C" void SIG_DFL(int signum) { - if ( signum == SIGHUP ) { Process::Exit(128 + signum); } else - if ( signum == SIGINT ) { Process::Exit(128 + signum); } else + if ( signum == SIGHUP ) { exit(128 + signum); } else + if ( signum == SIGINT ) { exit(128 + signum); } else if ( signum == SIGQUIT ) { Core(signum); } else if ( signum == SIGTRAP ) { Core(signum); } else if ( signum == SIGABRT ) { Core(signum); } else if ( signum == SIGEMT ) { Core(signum); } else if ( signum == SIGFPE ) { Core(signum); } else - if ( signum == SIGKILL ) { Process::Exit(128 + signum); } else + if ( signum == SIGKILL ) { exit(128 + signum); } else if ( signum == SIGBUS ) { Core(signum); } else if ( signum == SIGSEGV ) { Core(signum); } else if ( signum == SIGSYS ) { Core(signum); } else - if ( signum == SIGPIPE ) { Process::Exit(128 + signum); } else - if ( signum == SIGALRM ) { Process::Exit(128 + signum); } else - if ( signum == SIGTERM ) { Process::Exit(128 + signum); } else - if ( signum == SIGUSR1 ) { Process::Exit(128 + signum); } else - if ( signum == SIGUSR2 ) { Process::Exit(128 + signum); } else + if ( signum == SIGPIPE ) { exit(128 + signum); } else + if ( signum == SIGALRM ) { exit(128 + signum); } else + if ( signum == SIGTERM ) { exit(128 + signum); } else + if ( signum == SIGUSR1 ) { exit(128 + signum); } else + if ( signum == SIGUSR2 ) { exit(128 + signum); } else if ( signum == SIGCHLD ) { /* Ignore this signal. */ } else if ( signum == SIGPWR ) { /* Ignore this signal. */ } else if ( signum == SIGWINCH ) { /* Ignore this signal. */ } else @@ -79,7 +80,7 @@ namespace Maxsi extern "C" void SIG_ERR(int /*signum*/) { - Process::Abort(); + abort(); } const int MAX_SIGNALS = 128;