summaryrefslogtreecommitdiff
path: root/regress/lib/csu/dtors/dtors.c
blob: 2bed24f08bb253832a346263161d397d53ecd57f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*	$OpenBSD: dtors.c,v 1.2 2002/01/31 16:47:02 art Exp $	*/
/*
 * Written by Artur Grabowski <art@openbsd.org> Public Domain.
 */
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/wait.h>

#include <stdio.h>
#include <string.h>
#include <err.h>

void zap(void) __attribute__((destructor));

void *destarea;

#define MAGIC "destructed"

void
zap(void)
{
	memcpy(destarea, MAGIC, sizeof(MAGIC));
}

/*
 * XXX - horrible abuse of exit(), minherit and fork().
 */
int
main()
{
	int status;

	destarea = mmap(NULL, getpagesize(), PROT_READ|PROT_WRITE, MAP_ANON,
	    -1, 0);
	if (destarea == MAP_FAILED)
		err(1, "mmap");

	if (minherit(destarea, getpagesize(), MAP_INHERIT_SHARE) != 0)
		err(1, "minherit");

	memset(destarea, 0, sizeof(MAGIC));

	switch(fork()) {
	case -1:
		err(1, "fork");
	case 0:
		/*
		 * Yes, it's exit(), not _exit(). We _want_ to run the
		 * destructors in the child.
		 */
		exit(0);
	}

	if (wait(&status) < 0)
		err(1, "wait");		/* XXX uses exit() */

	if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
		err(1, "child error");	/* XXX uses exit() */

	_exit(memcmp(destarea, MAGIC, sizeof(MAGIC)) != 0);
}