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 #define __STDC_WANT_LIB_EXT1__ 1
31
32 #include "lint.h"
33
34 #include <sys/types.h>
35 #include <errno.h>
36 #include <stddef.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <synch.h>
40 #include <thread.h>
41 #include <unistd.h>
42
43 #include "libc.h"
44
45 /*
46 * Rationale recommends allocating new memory each time.
47 */
48 static constraint_handler_t *_ch = NULL;
49 static mutex_t ch_lock = ERRORCHECKMUTEX;
50
51 constraint_handler_t
set_constraint_handler_s(constraint_handler_t handler)52 set_constraint_handler_s(constraint_handler_t handler)
53 {
54 constraint_handler_t *new, *old, ret;
55
56 new = malloc(sizeof (constraint_handler_t));
57 if (new == NULL)
58 return (NULL);
59 *new = handler;
60 mutex_enter(&ch_lock);
61 old = _ch;
62 _ch = new;
63 mutex_exit(&ch_lock);
64 if (old == NULL) {
65 ret = NULL;
66 } else {
67 ret = *old;
68 free(old);
69 }
70 return (ret);
71 }
72
73 void
abort_handler_s(const char * _RESTRICT_KYWD msg,void * _RESTRICT_KYWD ptr __unused,errno_t error __unused)74 abort_handler_s(const char *_RESTRICT_KYWD msg,
75 void *_RESTRICT_KYWD ptr __unused, errno_t error __unused)
76 {
77 common_panic("abort_handler_s: ", msg);
78 }
79
80 void
ignore_handler_s(const char * _RESTRICT_KYWD msg __unused,void * _RESTRICT_KYWD ptr __unused,errno_t error __unused)81 ignore_handler_s(const char *_RESTRICT_KYWD msg __unused,
82 void *_RESTRICT_KYWD ptr __unused, errno_t error __unused)
83 {
84 }
85
86 void
__throw_constraint_handler_s(const char * _RESTRICT_KYWD msg,errno_t error)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 }
108