1 /* $OpenBSD: atexit_test.c,v 1.5 2003/09/02 23:52:16 david Exp $ */ 2 3 /* 4 * Copyright (c) 2002 Daniel Hartmeier 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 * 11 * - Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * - Redistributions in binary form must reproduce the above 14 * copyright notice, this list of conditions and the following 15 * disclaimer in the documentation and/or other materials provided 16 * with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 22 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 26 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 28 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 * 31 * Effort sponsored in part by the Defense Advanced Research Projects 32 * Agency (DARPA) and Air Force Research Laboratory, Air Force 33 * Materiel Command, USAF, under agreement number F30602-01-2-0537. 34 * 35 */ 36 37 #include <stdio.h> 38 #include <stdlib.h> 39 #include <string.h> 40 #include <signal.h> 41 #include "stdlib/atexit.h" 42 43 void handle_first(void); 44 void handle_middle(void); 45 void handle_last(void); 46 void handle_invalid(void); 47 void handle_cleanup(void); 48 void handle_signal(int); 49 50 static int counter; 51 52 int 53 main(int argc, char *argv[]) 54 { 55 int i; 56 57 if (argc != 2 || (strcmp(argv[1], "-valid") && 58 strcmp(argv[1], "-invalid-atexit") && 59 strcmp(argv[1], "-invalid-cleanup"))) { 60 fprintf(stderr, "%s -valid/-invalid-atexit/-invalid-cleanup\n", 61 argv[0]); 62 return (1); 63 } 64 fprintf(stderr, "main()\n"); 65 if (atexit(handle_last)) { 66 perror("atexit(handle_last) failed"); 67 return (1); 68 } 69 for (i = 0; i < 65535; ++i) { 70 if (atexit(handle_middle)) { 71 perror("atexit(handle_middle) failed"); 72 return (1); 73 } 74 } 75 if (atexit(handle_first)) { 76 perror("atexit(handle_first) failed"); 77 return (1); 78 } 79 /* this is supposed to segfault */ 80 if (!strcmp(argv[1], "-invalid-atexit")) { 81 signal(SIGSEGV, handle_signal); 82 __atexit->fns[0] = handle_invalid; 83 } else if (!strcmp(argv[1], "-invalid-cleanup")) { 84 struct atexit *p = __atexit; 85 86 signal(SIGSEGV, handle_signal); 87 while (p != NULL && p->next != NULL) 88 p = p->next; 89 if (p == NULL) 90 fprintf(stderr, "p == NULL, no page found\n"); 91 p->fns[0] = handle_invalid; 92 } 93 __atexit_register_cleanup(handle_cleanup); 94 counter = 0; 95 fprintf(stderr, "main() returns\n"); 96 return (0); 97 } 98 99 void 100 handle_first(void) 101 { 102 fprintf(stderr, "handle_first() counter == %i\n", counter); 103 } 104 105 void 106 handle_middle(void) 107 { 108 counter++; 109 } 110 111 void 112 handle_last(void) 113 { 114 fprintf(stderr, "handle_last() counter == %i\n", counter); 115 } 116 117 void 118 handle_cleanup(void) 119 { 120 fprintf(stderr, "handle_cleanup()\n"); 121 } 122 123 void 124 handle_invalid(void) 125 { 126 fprintf(stderr, "handle_invalid() THIS SHOULD HAVE SEGFAULTED INSTEAD!\n"); 127 } 128 129 void 130 handle_signal(int sigraised) 131 { 132 switch (sigraised) { 133 case SIGSEGV: 134 fprintf(stderr, "SIGSEGV\n"); 135 exit(0); 136 default: 137 fprintf(stderr, "unexpected signal caught\n"); 138 exit(1); 139 } 140 } 141