xref: /openbsd/regress/lib/libc/setjmp/jmptest.c (revision 133306f0)
1 /*	$OpenBSD: jmptest.c,v 1.4 2001/01/29 02:05:45 niklas Exp $	*/
2 /*	$NetBSD: jmptest.c,v 1.2 1995/01/01 20:55:35 jtc Exp $	*/
3 
4 /*
5  * Copyright (c) 1994 Christopher G. Demetriou
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by Christopher G. Demetriou
19  *	for the NetBSD Project.
20  * 4. The name of the author may not be used to endorse or promote products
21  *    derived from this software without specific prior written permission
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include <sys/types.h>
36 #include <err.h>
37 #include <setjmp.h>
38 #include <signal.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <unistd.h>
42 
43 #if (TEST_SETJMP + TEST_U_SETJMP + TEST_SIGSETJMP) != 1
44 #error one of TEST_SETJMP, TEST_U_SETJMP, or TEST_SIGSETJMP must be defined
45 #endif
46 
47 #ifdef TEST_SETJMP
48 #define BUF		jmp_buf
49 #define	SET(b, m)	setjmp(b)
50 #define	JMP(b, v)	longjmp(b, v)
51 #endif
52 
53 #ifdef TEST_U_SETJMP
54 #define BUF		jmp_buf
55 #define	SET(b, m)	_setjmp(b)
56 #define	JMP(b, v)	_longjmp(b, v)
57 #endif
58 
59 #ifdef TEST_SIGSETJMP
60 #define BUF		sigjmp_buf
61 #define	SET(b, m)	sigsetjmp(b, m)
62 #define	JMP(b, v)	siglongjmp(b, v)
63 #endif
64 
65 int expectsignal;
66 
67 void
68 aborthandler(signo)
69 	int signo;
70 {
71 
72 	if (expectsignal)
73 		exit(0);
74 	else
75 		errx(1, "kill(SIGABRT) succeeded");
76 }
77 
78 int
79 main(argc, argv)
80 	int argc;
81 	char *argv[];
82 {
83 	struct sigaction sa;
84 	BUF jb;
85 	sigset_t ss;
86 	int i, x;
87 
88 	i = getpid();
89 
90 #ifdef TEST_SETJMP
91 	expectsignal = 0;
92 #endif
93 #ifdef TEST_U_SETJMP
94 	expectsignal = 1;
95 #endif
96 #ifdef TEST_SIGSETJMP
97 	if (argc != 2 ||
98 	    (strcmp(argv[1], "save") && strcmp(argv[1], "nosave"))) {
99 		fprintf(stderr, "usage: %s [save|nosave]\n", argv[0]);
100 		exit(1);
101 	}
102 	expectsignal = (strcmp(argv[1], "save") != 0);
103 #endif
104 
105 	sa.sa_handler = aborthandler;
106 	sigemptyset(&sa.sa_mask);
107 	sa.sa_flags = 0;
108 	if (sigaction(SIGABRT, &sa, NULL) == -1)
109 		err(1, "sigaction failed");
110 
111 	if (sigemptyset(&ss) == -1)
112 		err(1, "sigemptyset failed");
113 	if (sigaddset(&ss, SIGABRT) == -1)
114 		err(1, "sigaddset failed");
115 	if (sigprocmask(SIG_BLOCK, &ss, NULL) == -1)
116 		err(1, "sigprocmask (1) failed");
117 
118 	x = SET(jb, !expectsignal);
119 	if (x != 0) {
120 		if (x != i)
121 			errx(1, "setjmp returned wrong value");
122 
123 		kill(i, SIGABRT);
124 		if (expectsignal)
125 			errx(1, "kill(SIGABRT) failed");
126 		else
127 			exit(0);
128 	}
129 
130 	if (sigprocmask(SIG_UNBLOCK, &ss, NULL) == -1)
131 		err(1, "sigprocmask (2) failed");
132 
133 	JMP(jb, i);
134 
135 	errx(1, "jmp failed");
136 }
137