|
Packit |
6c4009 |
/* Helper file for tst-{atexit,at_quick_exit,cxa_atexit,on_exit}.
|
|
Packit |
6c4009 |
Copyright (C) 2017-2018 Free Software Foundation, Inc.
|
|
Packit |
6c4009 |
This file is part of the GNU C Library.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is free software; you can redistribute it and/or
|
|
Packit |
6c4009 |
modify it under the terms of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License as published by the Free Software Foundation; either
|
|
Packit |
6c4009 |
version 2.1 of the License, or (at your option) any later version.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is distributed in the hope that it will be useful,
|
|
Packit |
6c4009 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
6c4009 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
6c4009 |
Lesser General Public License for more details.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
You should have received a copy of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License along with the GNU C Library; if not, see
|
|
Packit |
6c4009 |
<http://www.gnu.org/licenses/>. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include <assert.h>
|
|
Packit |
6c4009 |
#include <stdio.h>
|
|
Packit |
6c4009 |
#include <stdlib.h>
|
|
Packit |
6c4009 |
#include <string.h>
|
|
Packit |
6c4009 |
#include <unistd.h>
|
|
Packit |
6c4009 |
#include <sys/wait.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* http://pubs.opengroup.org/onlinepubs/000095399/functions/atexit.html
|
|
Packit |
6c4009 |
requires that we support at least 32 atexit handlers.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The number we actually support is limited by memory. Here we simply
|
|
Packit |
6c4009 |
check that we support at least the minimum required. */
|
|
Packit |
6c4009 |
#define MAX_ATEXIT 32
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Arbitrary sequence matching current registrations. */
|
|
Packit |
6c4009 |
const char expected[] = "00000000000000000000000003021121130211";
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static char crumbs[sizeof (expected)];
|
|
Packit |
6c4009 |
static int next_slot = 0;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Helper: flush stdout and _exit. */
|
|
Packit |
6c4009 |
static void
|
|
Packit |
6c4009 |
_exit_with_flush (int code)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
fflush (stdout);
|
|
Packit |
6c4009 |
_exit (code);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static void
|
|
Packit |
6c4009 |
fn0 (void)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
crumbs[next_slot++] = '0';
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static void
|
|
Packit |
6c4009 |
fn1 (void)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
crumbs[next_slot++] = '1';
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static void
|
|
Packit |
6c4009 |
fn2 (void)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
crumbs[next_slot++] = '2';
|
|
Packit |
6c4009 |
ATEXIT (fn1);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static void
|
|
Packit |
6c4009 |
fn3 (void)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
crumbs[next_slot++] = '3';
|
|
Packit |
6c4009 |
ATEXIT (fn2);
|
|
Packit |
6c4009 |
ATEXIT (fn0);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static void
|
|
Packit |
6c4009 |
fn_final (void)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
if (strcmp (crumbs, expected) == 0)
|
|
Packit |
6c4009 |
_exit_with_flush (0);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
printf ("crumbs: %s\n", crumbs);
|
|
Packit |
6c4009 |
printf ("expected: %s\n", expected);
|
|
Packit |
6c4009 |
_exit_with_flush (1);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static int
|
|
Packit |
6c4009 |
do_test (void)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
int slots_remaining = MAX_ATEXIT;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Register this first so it can verify expected order of the rest. */
|
|
Packit |
6c4009 |
ATEXIT (fn_final); --slots_remaining;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
ATEXIT (fn1); --slots_remaining;
|
|
Packit |
6c4009 |
ATEXIT (fn3); --slots_remaining;
|
|
Packit |
6c4009 |
ATEXIT (fn1); --slots_remaining;
|
|
Packit |
6c4009 |
ATEXIT (fn2); --slots_remaining;
|
|
Packit |
6c4009 |
ATEXIT (fn1); --slots_remaining;
|
|
Packit |
6c4009 |
ATEXIT (fn3); --slots_remaining;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Fill the rest of available slots with fn0. */
|
|
Packit |
6c4009 |
while (slots_remaining > 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
ATEXIT (fn0); --slots_remaining;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Verify that handlers registered above are inherited across fork. */
|
|
Packit |
6c4009 |
const pid_t child = fork ();
|
|
Packit |
6c4009 |
switch (child)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
case -1:
|
|
Packit |
6c4009 |
printf ("fork: %m\n");
|
|
Packit |
6c4009 |
_exit_with_flush (3);
|
|
Packit |
6c4009 |
case 0: /* Child. */
|
|
Packit |
6c4009 |
break;
|
|
Packit |
6c4009 |
default:
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
int status;
|
|
Packit |
6c4009 |
const pid_t exited = waitpid (child, &status, 0);
|
|
Packit |
6c4009 |
if (child != exited)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("unexpected child: %d, expected %d\n", exited, child);
|
|
Packit |
6c4009 |
_exit_with_flush (4);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
if (status != 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
if (WIFEXITED (status))
|
|
Packit |
6c4009 |
printf ("unexpected exit status %d from child %d\n",
|
|
Packit |
6c4009 |
WEXITSTATUS (status), child);
|
|
Packit |
6c4009 |
else if (WIFSIGNALED (status))
|
|
Packit |
6c4009 |
printf ("unexpected signal %d from child %d\n",
|
|
Packit |
6c4009 |
WTERMSIG (status), child);
|
|
Packit |
6c4009 |
else
|
|
Packit |
6c4009 |
printf ("unexpected status %d from child %d\n", status, child);
|
|
Packit |
6c4009 |
_exit_with_flush (5);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
break;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
EXIT (2); /* If we see this exit code, fn_final must have not worked. */
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#define TEST_FUNCTION do_test
|
|
Packit |
6c4009 |
#include <support/test-driver.c>
|