jehanne: call atexit handlers on return from main

This commit is contained in:
Giacomo Tesio 2017-10-01 22:39:53 +02:00
parent 5c85f0f91d
commit 05f05759ad
5 changed files with 108 additions and 8 deletions

View File

@ -14,7 +14,8 @@ extra_objs =
endif
lib_a_SOURCES = getenv_r.c getenv.c malloc.c mallocr.c free.c freer.c \
calloc.c callocr.c realloc.c reallocr.c environ.c signal.c
calloc.c callocr.c realloc.c reallocr.c environ.c signal.c \
sysfcntl.c exit.c
lib_a_LIBADD = $(extra_objs)
EXTRA_lib_a_SOURCES = libposix_conf.c syscalls.c chown.c getrusage.c \

View File

@ -62,7 +62,8 @@ am_lib_a_OBJECTS = lib_a-getenv_r.$(OBJEXT) lib_a-getenv.$(OBJEXT) \
lib_a-free.$(OBJEXT) lib_a-freer.$(OBJEXT) \
lib_a-calloc.$(OBJEXT) lib_a-callocr.$(OBJEXT) \
lib_a-realloc.$(OBJEXT) lib_a-reallocr.$(OBJEXT) \
lib_a-environ.$(OBJEXT) lib_a-signal.$(OBJEXT)
lib_a-environ.$(OBJEXT) lib_a-signal.$(OBJEXT) \
lib_a-sysfcntl.$(OBJEXT) lib_a-exit.$(OBJEXT)
lib_a_OBJECTS = $(am_lib_a_OBJECTS)
DEFAULT_INCLUDES = -I.@am__isrc@
depcomp =
@ -188,7 +189,8 @@ noinst_LIBRARIES = lib.a
@MAY_SUPPLY_SYSCALLS_TRUE@ termios.o
lib_a_SOURCES = getenv_r.c getenv.c malloc.c mallocr.c free.c freer.c \
calloc.c callocr.c realloc.c reallocr.c environ.c signal.c
calloc.c callocr.c realloc.c reallocr.c environ.c signal.c \
sysfcntl.c exit.c
lib_a_LIBADD = $(extra_objs)
EXTRA_lib_a_SOURCES = libposix_conf.c syscalls.c chown.c getrusage.c \
@ -335,6 +337,18 @@ lib_a-signal.o: signal.c
lib_a-signal.obj: signal.c
$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-signal.obj `if test -f 'signal.c'; then $(CYGPATH_W) 'signal.c'; else $(CYGPATH_W) '$(srcdir)/signal.c'; fi`
lib_a-sysfcntl.o: sysfcntl.c
$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-sysfcntl.o `test -f 'sysfcntl.c' || echo '$(srcdir)/'`sysfcntl.c
lib_a-sysfcntl.obj: sysfcntl.c
$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-sysfcntl.obj `if test -f 'sysfcntl.c'; then $(CYGPATH_W) 'sysfcntl.c'; else $(CYGPATH_W) '$(srcdir)/sysfcntl.c'; fi`
lib_a-exit.o: exit.c
$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-exit.o `test -f 'exit.c' || echo '$(srcdir)/'`exit.c
lib_a-exit.obj: exit.c
$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-exit.obj `if test -f 'exit.c'; then $(CYGPATH_W) 'exit.c'; else $(CYGPATH_W) '$(srcdir)/exit.c'; fi`
lib_a-libposix_conf.o: libposix_conf.c
$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-libposix_conf.o `test -f 'libposix_conf.c' || echo '$(srcdir)/'`libposix_conf.c

View File

@ -0,0 +1,27 @@
/*
* This file is part of Jehanne.
*
* Copyright (C) 2017 Giacomo Tesio <giacomo@tesio.it>
*
* Jehanne is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 2 of the License.
*
* Jehanne 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Jehanne. If not, see <http://www.gnu.org/licenses/>.
*/
#include <unistd.h>
extern void on_process_disposition(int status);
void
exit(int status)
{
on_process_disposition(status);
_exit(status);
}

View File

@ -113,7 +113,10 @@ __stat_reader(struct stat *s, const Dir *d)
/* we have to map types to UNIX */
if(d->mode & DMDIR)
s->st_mode |= _IFDIR;
else
else if(strcmp("cons", d->name) == 0 || strstr(d->name, "tty") != nil){
/* newlib expect consoles to be character devices */
s->st_mode |= _IFCHR;
} else
s->st_mode |= _IFREG; /* UNIX lack fantasy :-) */
s->st_nlink = 1;
s->st_uid = 1;
@ -254,6 +257,18 @@ default_timezone_reader(void *tz, const Tm *time)
return 0;
}
void
on_process_disposition(int status)
{
extern void __call_exitprocs (int, void*);
fflush(NULL);
__call_exitprocs(status, NULL);
if (_GLOBAL_REENT->__cleanup)
(*_GLOBAL_REENT->__cleanup) (_GLOBAL_REENT);
}
void
initialize_newlib(void)
{
@ -267,6 +282,7 @@ initialize_newlib(void)
libposix_translate_open(open_translator);
libposix_translate_error(default_error_translator, 0);
libposix_set_wait_options(0, WNOHANG, 0);
libposix_on_process_disposition(on_process_disposition);
/* error numbers */
libposix_define_errno(PosixE2BIG, E2BIG);
@ -360,8 +376,13 @@ initialize_newlib(void)
int
_fcntl_r(struct _reent *r, int fd, int cmd, int arg)
{
extern int jehanne_print(const char*, ...);
extern uintptr_t jehanne_getcallerpc(void);
jehanne_fprint(2, "_fcntl_r(%d, %d, %d) from %#p\n", fd, cmd, arg, jehanne_getcallerpc());
int *errnop = &r->_errno;
PosixFDCmds pcmd;
int tmp;
switch(cmd){
case F_DUPFD:
pcmd = PosixFDCDupFD;
@ -370,14 +391,23 @@ _fcntl_r(struct _reent *r, int fd, int cmd, int arg)
pcmd = PosixFDCDupFDCloseOnExec;
break;
case F_GETFD:
pcmd = PosixFDCGetFD;
break;
if(POSIX_fcntl(errnop, fd, PosixFDCGetFD, arg))
return O_CLOEXEC;
return 0;
case F_SETFD:
pcmd = PosixFDCSetFD;
break;
case F_GETFL:
pcmd = PosixFDCGetFL;
break;
tmp = POSIX_fcntl(errnop, fd, PosixFDCGetFL, arg);
if(tmp < 0)
return -1;
if((tmp & ORDWR) == ORDWR)
return O_RDWR;
if(tmp & OREAD)
return O_RDONLY;
if(tmp & OWRITE)
return O_WRONLY;
return 0;
case F_SETFL:
pcmd = PosixFDCSetFL;
break;

View File

@ -0,0 +1,28 @@
/*
* This file is part of Jehanne.
*
* Copyright (C) 2017 Giacomo Tesio <giacomo@tesio.it>
*
* Jehanne is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 2 of the License.
*
* Jehanne 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Jehanne. If not, see <http://www.gnu.org/licenses/>.
*/
#include <u.h>
#include <libc.h>
#include <posix.h>
#include <reent.h>
extern _fcntl_r(struct _reent *r, int fd, int cmd, int arg);
int
fcntl(int fd, int cmd, int arg)
{
return _fcntl_r (_REENT, fd, cmd, arg);
}