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
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 /*ARGSUSED*/
74 void
75 abort_handler_s(const char *_RESTRICT_KYWD msg,
76     void *_RESTRICT_KYWD ptr, errno_t error)
77 {
78 	common_panic("abort_handler_s: ", msg);
79 }
80 
81 /*ARGSUSED*/
82 void
83 ignore_handler_s(const char *_RESTRICT_KYWD msg,
84     void *_RESTRICT_KYWD ptr, errno_t error)
85 {
86 }
87 
88 void
89 __throw_constraint_handler_s(const char *_RESTRICT_KYWD msg, errno_t error)
90 {
91 	constraint_handler_t ch;
92 
93 	mutex_enter(&ch_lock);
94 	ch = (_ch != NULL) ? *_ch : NULL;
95 	mutex_exit(&ch_lock);
96 	if (ch != NULL) {
97 		ch(msg, NULL, error);
98 	} else {
99 		/*
100 		 * If current handler is NULL (there were no calls to
101 		 * set_constraint_handler_s(), or it was called with NULL
102 		 * pointer handler argument), call default constraint handler
103 		 * per K.3.6.1.1 points 4 and 5.
104 		 *
105 		 * This implementation defines abort_handler_s() as default.
106 		 */
107 		abort_handler_s(msg, NULL, error);
108 	}
109 }
110