xref: /openbsd/regress/lib/csu/callbacks/libaa/aa.c (revision a4527715)
1 /*      $OpenBSD: aa.c,v 1.1 2014/11/23 08:46:49 guenther Exp $       */
2 
3 /*
4  * Copyright (c) 2014 Philip Guenther <guenther@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <pthread.h>
22 
23 #define	CALLBACK(file, name)					\
24 	static void name(void)					\
25 	{							\
26 		fprintf(file, "libaa "#name"\n");		\
27 		fflush(file);					\
28 	}
29 
CALLBACK(stdout,cleanup)30 CALLBACK(stdout, cleanup)
31 
32 void
33 aa(void)
34 {
35 	atexit(cleanup);
36 }
37 
38 static FILE *otherf;
39 
CALLBACK(stdout,atfork_prepare)40 CALLBACK(stdout, atfork_prepare)
41 CALLBACK(stdout, atfork_parent)
42 CALLBACK(otherf, atfork_child)
43 
44 void
45 aa_atfork(FILE *f)
46 {
47 	otherf = f;
48 	pthread_atfork(atfork_prepare, atfork_parent, atfork_child);
49 }
50