1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include "libuutil_common.h"
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #include <assert.h>
32*0Sstevel@tonic-gate #include <errno.h>
33*0Sstevel@tonic-gate #include <libintl.h>
34*0Sstevel@tonic-gate #include <pthread.h>
35*0Sstevel@tonic-gate #include <stdarg.h>
36*0Sstevel@tonic-gate #include <stdio.h>
37*0Sstevel@tonic-gate #include <stdlib.h>
38*0Sstevel@tonic-gate #include <string.h>
39*0Sstevel@tonic-gate #include <sys/debug.h>
40*0Sstevel@tonic-gate #include <thread.h>
41*0Sstevel@tonic-gate #include <unistd.h>
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
44*0Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
45*0Sstevel@tonic-gate #endif
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate static pthread_mutex_t	uu_key_lock = PTHREAD_MUTEX_INITIALIZER;
48*0Sstevel@tonic-gate static pthread_key_t	uu_error_key;
49*0Sstevel@tonic-gate static int		uu_error_key_setup;
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate static pthread_mutex_t	uu_panic_lock = PTHREAD_MUTEX_INITIALIZER;
52*0Sstevel@tonic-gate /* LINTED static unused */
53*0Sstevel@tonic-gate static const char	*uu_panic_format;
54*0Sstevel@tonic-gate /* LINTED static unused */
55*0Sstevel@tonic-gate static va_list		uu_panic_args;
56*0Sstevel@tonic-gate static pthread_t	uu_panic_thread;
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate static uint32_t		_uu_main_error;
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate void
61*0Sstevel@tonic-gate uu_set_error(uint_t code)
62*0Sstevel@tonic-gate {
63*0Sstevel@tonic-gate 	int error;
64*0Sstevel@tonic-gate 	if (thr_main() != 0) {
65*0Sstevel@tonic-gate 		_uu_main_error = code;
66*0Sstevel@tonic-gate 		return;
67*0Sstevel@tonic-gate 	}
68*0Sstevel@tonic-gate 	if (uu_error_key_setup == 0) {
69*0Sstevel@tonic-gate 		(void) pthread_mutex_lock(&uu_key_lock);
70*0Sstevel@tonic-gate 		if (uu_error_key_setup == 0) {
71*0Sstevel@tonic-gate 			error = pthread_key_create(&uu_error_key, NULL);
72*0Sstevel@tonic-gate 			if (error != 0)
73*0Sstevel@tonic-gate 				uu_error_key_setup = -1;
74*0Sstevel@tonic-gate 			else
75*0Sstevel@tonic-gate 				uu_error_key_setup = 1;
76*0Sstevel@tonic-gate 		}
77*0Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&uu_key_lock);
78*0Sstevel@tonic-gate 	}
79*0Sstevel@tonic-gate 	if (uu_error_key_setup > 0)
80*0Sstevel@tonic-gate 		(void) pthread_setspecific(uu_error_key,
81*0Sstevel@tonic-gate 		    (void *)(uintptr_t)code);
82*0Sstevel@tonic-gate }
83*0Sstevel@tonic-gate 
84*0Sstevel@tonic-gate uint32_t
85*0Sstevel@tonic-gate uu_error(void)
86*0Sstevel@tonic-gate {
87*0Sstevel@tonic-gate 	if (thr_main() != 0)
88*0Sstevel@tonic-gate 		return (_uu_main_error);
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate 	if (uu_error_key_setup < 0)
91*0Sstevel@tonic-gate 		return (UU_ERROR_UNKNOWN);
92*0Sstevel@tonic-gate 	else if (uu_error_key_setup == 0)
93*0Sstevel@tonic-gate 		return (UU_ERROR_NONE);
94*0Sstevel@tonic-gate 	else
95*0Sstevel@tonic-gate 		return ((uint32_t)(uintptr_t)pthread_getspecific(uu_error_key));
96*0Sstevel@tonic-gate }
97*0Sstevel@tonic-gate 
98*0Sstevel@tonic-gate const char *
99*0Sstevel@tonic-gate uu_strerror(uint32_t code)
100*0Sstevel@tonic-gate {
101*0Sstevel@tonic-gate 	const char *str;
102*0Sstevel@tonic-gate 
103*0Sstevel@tonic-gate 	switch (code) {
104*0Sstevel@tonic-gate 	case UU_ERROR_NONE:
105*0Sstevel@tonic-gate 		str = dgettext(TEXT_DOMAIN, "No error");
106*0Sstevel@tonic-gate 		break;
107*0Sstevel@tonic-gate 
108*0Sstevel@tonic-gate 	case UU_ERROR_INVALID_ARGUMENT:
109*0Sstevel@tonic-gate 		str = dgettext(TEXT_DOMAIN, "Invalid argument");
110*0Sstevel@tonic-gate 		break;
111*0Sstevel@tonic-gate 
112*0Sstevel@tonic-gate 	case UU_ERROR_UNKNOWN_FLAG:
113*0Sstevel@tonic-gate 		str = dgettext(TEXT_DOMAIN, "Unknown flag passed");
114*0Sstevel@tonic-gate 		break;
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate 	case UU_ERROR_NO_MEMORY:
117*0Sstevel@tonic-gate 		str = dgettext(TEXT_DOMAIN, "Out of memory");
118*0Sstevel@tonic-gate 		break;
119*0Sstevel@tonic-gate 
120*0Sstevel@tonic-gate 	case UU_ERROR_CALLBACK_FAILED:
121*0Sstevel@tonic-gate 		str = dgettext(TEXT_DOMAIN, "Callback-initiated failure");
122*0Sstevel@tonic-gate 		break;
123*0Sstevel@tonic-gate 
124*0Sstevel@tonic-gate 	case UU_ERROR_NOT_SUPPORTED:
125*0Sstevel@tonic-gate 		str = dgettext(TEXT_DOMAIN, "Operation not supported");
126*0Sstevel@tonic-gate 		break;
127*0Sstevel@tonic-gate 
128*0Sstevel@tonic-gate 	case UU_ERROR_EMPTY:
129*0Sstevel@tonic-gate 		str = dgettext(TEXT_DOMAIN, "No value provided");
130*0Sstevel@tonic-gate 		break;
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate 	case UU_ERROR_UNDERFLOW:
133*0Sstevel@tonic-gate 		str = dgettext(TEXT_DOMAIN, "Value too small");
134*0Sstevel@tonic-gate 		break;
135*0Sstevel@tonic-gate 
136*0Sstevel@tonic-gate 	case UU_ERROR_OVERFLOW:
137*0Sstevel@tonic-gate 		str = dgettext(TEXT_DOMAIN, "Value too large");
138*0Sstevel@tonic-gate 		break;
139*0Sstevel@tonic-gate 
140*0Sstevel@tonic-gate 	case UU_ERROR_INVALID_CHAR:
141*0Sstevel@tonic-gate 		str = dgettext(TEXT_DOMAIN,
142*0Sstevel@tonic-gate 		    "Value contains unexpected character");
143*0Sstevel@tonic-gate 		break;
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate 	case UU_ERROR_INVALID_DIGIT:
146*0Sstevel@tonic-gate 		str = dgettext(TEXT_DOMAIN,
147*0Sstevel@tonic-gate 		    "Value contains digit not in base");
148*0Sstevel@tonic-gate 		break;
149*0Sstevel@tonic-gate 
150*0Sstevel@tonic-gate 	case UU_ERROR_SYSTEM:
151*0Sstevel@tonic-gate 		str = dgettext(TEXT_DOMAIN, "Underlying system error");
152*0Sstevel@tonic-gate 		break;
153*0Sstevel@tonic-gate 
154*0Sstevel@tonic-gate 	case UU_ERROR_UNKNOWN:
155*0Sstevel@tonic-gate 		str = dgettext(TEXT_DOMAIN, "Error status not known");
156*0Sstevel@tonic-gate 		break;
157*0Sstevel@tonic-gate 
158*0Sstevel@tonic-gate 	default:
159*0Sstevel@tonic-gate 		errno = ESRCH;
160*0Sstevel@tonic-gate 		str = NULL;
161*0Sstevel@tonic-gate 		break;
162*0Sstevel@tonic-gate 	}
163*0Sstevel@tonic-gate 	return (str);
164*0Sstevel@tonic-gate }
165*0Sstevel@tonic-gate 
166*0Sstevel@tonic-gate void
167*0Sstevel@tonic-gate uu_panic(const char *format, ...)
168*0Sstevel@tonic-gate {
169*0Sstevel@tonic-gate 	va_list args;
170*0Sstevel@tonic-gate 
171*0Sstevel@tonic-gate 	va_start(args, format);
172*0Sstevel@tonic-gate 
173*0Sstevel@tonic-gate 	(void) pthread_mutex_lock(&uu_panic_lock);
174*0Sstevel@tonic-gate 	if (uu_panic_thread == 0) {
175*0Sstevel@tonic-gate 		uu_panic_thread = pthread_self();
176*0Sstevel@tonic-gate 		uu_panic_format = format;
177*0Sstevel@tonic-gate 		va_copy(uu_panic_args, args);
178*0Sstevel@tonic-gate 	}
179*0Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&uu_panic_lock);
180*0Sstevel@tonic-gate 
181*0Sstevel@tonic-gate 	(void) vfprintf(stderr, format, args);
182*0Sstevel@tonic-gate 
183*0Sstevel@tonic-gate 	if (uu_panic_thread == pthread_self())
184*0Sstevel@tonic-gate 		abort();
185*0Sstevel@tonic-gate 	else
186*0Sstevel@tonic-gate 		for (;;)
187*0Sstevel@tonic-gate 			(void) pause();
188*0Sstevel@tonic-gate }
189*0Sstevel@tonic-gate 
190*0Sstevel@tonic-gate int
191*0Sstevel@tonic-gate assfail(const char *astring, const char *file, int line)
192*0Sstevel@tonic-gate {
193*0Sstevel@tonic-gate 	__assert(astring, file, line);
194*0Sstevel@tonic-gate 	/*NOTREACHED*/
195*0Sstevel@tonic-gate 	return (0);
196*0Sstevel@tonic-gate }
197*0Sstevel@tonic-gate 
198*0Sstevel@tonic-gate static void
199*0Sstevel@tonic-gate uu_lockup(void)
200*0Sstevel@tonic-gate {
201*0Sstevel@tonic-gate 	(void) pthread_mutex_lock(&uu_panic_lock);
202*0Sstevel@tonic-gate 	(void) pthread_mutex_lock(&uu_key_lock);
203*0Sstevel@tonic-gate 	uu_avl_lockup();
204*0Sstevel@tonic-gate 	uu_list_lockup();
205*0Sstevel@tonic-gate }
206*0Sstevel@tonic-gate 
207*0Sstevel@tonic-gate static void
208*0Sstevel@tonic-gate uu_release(void)
209*0Sstevel@tonic-gate {
210*0Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&uu_panic_lock);
211*0Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&uu_key_lock);
212*0Sstevel@tonic-gate 	uu_avl_release();
213*0Sstevel@tonic-gate 	uu_list_release();
214*0Sstevel@tonic-gate }
215*0Sstevel@tonic-gate 
216*0Sstevel@tonic-gate static void
217*0Sstevel@tonic-gate uu_release_child(void)
218*0Sstevel@tonic-gate {
219*0Sstevel@tonic-gate 	uu_panic_format = NULL;
220*0Sstevel@tonic-gate 	uu_panic_thread = 0;
221*0Sstevel@tonic-gate 
222*0Sstevel@tonic-gate 	uu_release();
223*0Sstevel@tonic-gate }
224*0Sstevel@tonic-gate 
225*0Sstevel@tonic-gate #pragma init(uu_init)
226*0Sstevel@tonic-gate static void
227*0Sstevel@tonic-gate uu_init(void)
228*0Sstevel@tonic-gate {
229*0Sstevel@tonic-gate 	(void) pthread_atfork(uu_lockup, uu_release, uu_release_child);
230*0Sstevel@tonic-gate }
231