Change stdio to use atexit() rather than the internal __clean variable; this

breaks the dependency between exit/atexit and stdio. Buffers are no longer
flushed on abort() (because it's pretty risky). Move the relevant functions
into sys/core.
This commit is contained in:
David Given 2018-06-23 18:35:45 +02:00
parent f744a21699
commit af22b7ea85
13 changed files with 48 additions and 43 deletions

View file

@ -44,7 +44,7 @@ for _, plat in ipairs(vars.plats) do
"./core/ctype/*.c",
"./core/misc/*.c",
"./sys/malloc/*.c",
"./signal/*.c",
"./sys/exit/*.c",
"./assert/*.c",
"./stdio/*.c",
"./stdlib/*.c",

View file

@ -4,17 +4,11 @@
*/
/* $Id$ */
#if defined(_POSIX_SOURCE)
#include <sys/types.h>
#endif
#include <signal.h>
#include <stdlib.h>
extern void (*_clean)(void);
#include <sys/types.h>
#include <signal.h>
void abort(void)
{
if (_clean)
_clean(); /* flush all output files */
raise(SIGABRT);
}

View file

@ -4,14 +4,17 @@
*/
/* $Id$ */
#if defined(_POSIX_SOURCE)
#include <stdlib.h>
#include <sys/types.h>
#endif
#include <signal.h>
#if ACKCONF_WANT_EMULATED_RAISE
int raise(int sig)
{
if (sig < 0 || sig > _NSIG)
return -1;
return kill(getpid(), sig);
}
#endif

View file

@ -24,4 +24,14 @@
#define ACKCONF_WANT_TERMIOS 0
#endif
#ifndef ACKCONF_WANT_EMULATED_RAISE
/* Implement raise() in terms of kill() and getpid(). */
#define ACKCONF_WANT_EMULATED_RAISE 1
#endif
#ifndef ACKCONF_WANT_MALLOC
/* Uses sbrk() to get memory from the system. */
#define ACKCONF_WANT_MALLOC 1
#endif
#endif

View file

@ -1 +0,0 @@
raise.c

View file

@ -1,2 +0,0 @@
clean:
rm -f raise.o OLIST

View file

@ -68,7 +68,7 @@ int fflush(FILE* stream)
return EOF;
}
void __cleanup(void)
static void cleanup(void)
{
register int i;
@ -76,3 +76,13 @@ void __cleanup(void)
if (__iotab[i] && io_testflag(__iotab[i], _IOWRITING))
(void)fflush(__iotab[i]);
}
void __register_stdio_cleanup(void)
{
static char registered = 0;
if (!registered)
{
registered = 1;
atexit(cleanup);
}
}

View file

@ -8,8 +8,6 @@
#include <unistd.h>
#include "loc_incl.h"
extern void (*_clean)(void);
static int
do_write(int d, char* buf, int nbytes)
{
@ -28,7 +26,7 @@ do_write(int d, char* buf, int nbytes)
int __flushbuf(int c, FILE* stream)
{
_clean = __cleanup;
__register_stdio_cleanup();
if (fileno(stream) < 0)
return EOF;
if (!io_testflag(stream, _IOWRITE))

View file

@ -14,7 +14,8 @@ int _doprnt(const char *format, va_list ap, FILE *stream);
int _doscan(FILE * stream, const char *format, va_list ap);
char *_i_compute(unsigned long val, int base, char *s, int nrdigits);
char *_f_print(va_list *ap, int flags, char *s, char c, int precision);
void __cleanup(void);
extern void __register_stdio_cleanup(void);
FILE *popen(const char *command, const char *type);
FILE *fdopen(int fd, const char *mode);

View file

@ -13,7 +13,7 @@ int setvbuf(register FILE* stream, char* buf, int mode, size_t size)
{
int retval = 0;
_clean = __cleanup;
__register_stdio_cleanup();
if (mode != _IOFBF && mode != _IOLBF && mode != _IONBF)
return EOF;

View file

@ -1,11 +1,7 @@
/* $Id$ */
#include <stdlib.h>
#define NEXITS 32
extern void (*__functab[NEXITS])(void);
extern int __funccnt;
#include "atexits.h"
int atexit(void (*func)(void))
{

View file

@ -0,0 +1,9 @@
#ifndef ATEXITS_H
#define ATEXITS_H
#define NEXITS 32
extern void (*__functab[NEXITS])(void);
extern int __funccnt;
#endif

View file

@ -7,29 +7,16 @@
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#define NEXITS 32
#include "atexits.h"
void (*__functab[NEXITS])(void);
int __funccnt = 0;
/* only flush output buffers when necessary */
int (*_clean)(void) = NULL;
static void
_calls(void)
{
register int i = __funccnt;
/* "Called in reversed order of their registration" */
while (--i >= 0)
(*__functab[i])();
}
void exit(int status)
{
_calls();
if (_clean)
_clean();
/* "Called in reversed order of their registration" */
while (__funccnt >= 0)
(*__functab[__funccnt])();
_exit(status);
}