Refactor libc process exit and abortion.

Removed Maxsi::Process:: functions as they suck and are barely used. Gave
the functions standard names and put them in their own source files.

The declarations now have nice noreturn attributes attached.
This commit is contained in:
Jonas 'Sortie' Termansen 2012-09-06 22:30:09 +02:00
parent 34970e63f3
commit b4192c10e9
11 changed files with 194 additions and 78 deletions

View File

@ -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 \

31
libmaxsi/_Exit.cpp Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
_Exit.cpp
Terminates the current process.
*******************************************************************************/
#include <stdlib.h>
#include <unistd.h>
extern "C" void _Exit(int status)
{
_exit(status);
}

38
libmaxsi/_exit.cpp Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
_exit.cpp
Terminates the current process.
*******************************************************************************/
#include <libmaxsi/platform.h>
#include <libmaxsi/syscall.h>
#include <unistd.h>
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.
}

31
libmaxsi/abort.cpp Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
abort.cpp
Abnormal process termination.
*******************************************************************************/
#include <stdlib.h>
extern "C" void abort(void)
{
// TODO: Send SIGABRT instead!
_Exit(128 + 6);
}

View File

@ -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 <http://www.gnu.org/licenses/>.
exit.cpp
Hooks that is called upon process exit.
Terminates the current process.
*******************************************************************************/
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
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);
}

View File

@ -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();

View File

@ -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);

View File

@ -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*, ...);

62
libmaxsi/on_exit.cpp Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
exit.cpp
Hooks that is called upon process exit.
*******************************************************************************/
#include <stdlib.h>
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;
}
}

View File

@ -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);

View File

@ -26,6 +26,7 @@
#include <libmaxsi/memory.h>
#include <libmaxsi/syscall.h>
#include <libmaxsi/process.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
@ -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;