this fixes a bug reported by Nuno Gonçalves. previously, calling fclose on stdin or stdout resulted in deadlock at exit time, since __stdio_exit attempts to lock these streams to flush/seek them, and has no easy way of knowing that they were closed. conceptually, leaving a FILE stream locked on fclose is valid since, in the abstract machine, it ceases to exist. but to satisfy the implementation-internal assumption in __stdio_exit that it can access these streams unconditionally, we need to unlock them. it's also necessary that fclose leaves permanent streams in a state where __stdio_exit will not attempt any further operations on them. fortunately, the call to fflush already yields this property.
33 lines
529 B
C
33 lines
529 B
C
#include "stdio_impl.h"
|
|
#include "libc.h"
|
|
|
|
static void dummy(FILE *f) { }
|
|
weak_alias(dummy, __unlist_locked_file);
|
|
|
|
int fclose(FILE *f)
|
|
{
|
|
int r;
|
|
int perm;
|
|
|
|
FLOCK(f);
|
|
|
|
__unlist_locked_file(f);
|
|
|
|
if (!(perm = f->flags & F_PERM)) {
|
|
FILE **head = __ofl_lock();
|
|
if (f->prev) f->prev->next = f->next;
|
|
if (f->next) f->next->prev = f->prev;
|
|
if (*head == f) *head = f->next;
|
|
__ofl_unlock();
|
|
}
|
|
|
|
r = fflush(f);
|
|
r |= f->close(f);
|
|
|
|
if (f->getln_buf) free(f->getln_buf);
|
|
if (!perm) free(f);
|
|
else FUNLOCK(f);
|
|
|
|
return r;
|
|
}
|