mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Richard Pennington <rich@pennware.com>
To: musl@lists.openwall.com
Subject: Timing of destructors?
Date: Thu, 14 Jun 2012 06:01:45 -0500	[thread overview]
Message-ID: <5479702.eSrS0Tqozu@main.pennware.com> (raw)

Hi,

I have a small test program:
#include <stdio.h>
#include <stdlib.h>

void before(void) __attribute__((constructor));
void before(void)
{
    printf("before\n");
}

void after(void) __attribute__((destructor));
void after(void)
{
    printf("after\n");
}

int main()
{
    printf("hello world\n");
}

which doesn't print "after". This is because stdio is cleaned up before 
destructors are called in exit.c. Using stdio in destructors can be handy for 
debugging (if nothing else). Would it be evil to modify exit.c to look like 
this?:

void exit(int code)
{
        static int lock;

        /* If more than one thread calls exit, hang until _Exit ends it all */
        while (a_swap(&lock, 1)) __syscall(SYS_pause);

        /* Destructor s**t is kept separate from atexit to avoid bloat */
        if (libc.fini) libc.fini();
        if (libc.ldso_fini) libc.ldso_fini();

        /* Only do atexit & stdio flush if they were actually used */
        __funcs_on_exit();
        __fflush_on_exit();

        _Exit(code);
        for(;;);
}

The change is to move the *_on_exit() calls to after the destructors have been 
called.

-Rich


             reply	other threads:[~2012-06-14 11:01 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-06-14 11:01 Richard Pennington [this message]
2012-06-14 12:32 ` Szabolcs Nagy
2012-06-14 12:35   ` Rich Felker

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5479702.eSrS0Tqozu@main.pennware.com \
    --to=rich@pennware.com \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).