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