1 /* 2 * Copyright (c) 2017 Juniper Networks. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23 * SUCH DAMAGE. 24 */ 25 26 /* 27 * Copyright 2018 Nexenta Systems, Inc. 28 */ 29 30 #include "lint.h" 31 32 #include <sys/types.h> 33 #include <errno.h> 34 #include <stddef.h> 35 #include <stdlib.h> 36 #include <string.h> 37 #include <synch.h> 38 #include <thread.h> 39 #include <unistd.h> 40 41 #include "libc.h" 42 43 /* 44 * Rationale recommends allocating new memory each time. 45 */ 46 static constraint_handler_t *_ch = NULL; 47 static mutex_t ch_lock = ERRORCHECKMUTEX; 48 49 constraint_handler_t 50 set_constraint_handler_s(constraint_handler_t handler) 51 { 52 constraint_handler_t *new, *old, ret; 53 54 new = malloc(sizeof (constraint_handler_t)); 55 if (new == NULL) 56 return (NULL); 57 *new = handler; 58 mutex_enter(&ch_lock); 59 old = _ch; 60 _ch = new; 61 mutex_exit(&ch_lock); 62 if (old == NULL) { 63 ret = NULL; 64 } else { 65 ret = *old; 66 free(old); 67 } 68 return (ret); 69 } 70 71 /*ARGSUSED*/ 72 void 73 abort_handler_s(const char *_RESTRICT_KYWD msg, 74 void *_RESTRICT_KYWD ptr, errno_t error) 75 { 76 common_panic("abort_handler_s: ", msg); 77 } 78 79 /*ARGSUSED*/ 80 void 81 ignore_handler_s(const char *_RESTRICT_KYWD msg, 82 void *_RESTRICT_KYWD ptr, errno_t error) 83 { 84 } 85 86 void 87 __throw_constraint_handler_s(const char *_RESTRICT_KYWD msg, errno_t error) 88 { 89 constraint_handler_t ch; 90 91 mutex_enter(&ch_lock); 92 ch = (_ch != NULL) ? *_ch : NULL; 93 mutex_exit(&ch_lock); 94 if (ch != NULL) { 95 ch(msg, NULL, error); 96 } else { 97 /* 98 * If current handler is NULL (there were no calls to 99 * set_constraint_handler_s(), or it was called with NULL 100 * pointer handler argument), call default constraint handler 101 * per K.3.6.1.1 points 4 and 5. 102 * 103 * This implementation defines abort_handler_s() as default. 104 */ 105 abort_handler_s(msg, NULL, error); 106 } 107 }