4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
29 #include "libuutil_common.h"
39 #include <sys/debug.h>
43 #if !defined(TEXT_DOMAIN)
44 #define TEXT_DOMAIN "SYS_TEST"
48 * All of the old code under !defined(PTHREAD_ONCE_KEY_NP)
49 * is here to enable the building of a native version of
50 * libuutil.so when the build machine has not yet been upgraded
51 * to a version of libc that provides pthread_key_create_once_np().
52 * It should all be deleted when solaris_nevada ships.
53 * The code is not MT-safe in a relaxed memory model.
56 #if defined(PTHREAD_ONCE_KEY_NP)
57 static pthread_key_t uu_error_key = PTHREAD_ONCE_KEY_NP;
58 #else /* PTHREAD_ONCE_KEY_NP */
59 static pthread_key_t uu_error_key = 0;
60 static pthread_mutex_t uu_key_lock = PTHREAD_MUTEX_INITIALIZER;
61 #endif /* PTHREAD_ONCE_KEY_NP */
63 static int uu_error_key_setup = 0;
65 static pthread_mutex_t uu_panic_lock = PTHREAD_MUTEX_INITIALIZER;
66 /* LINTED static unused */
67 static const char *uu_panic_format;
68 /* LINTED static unused */
69 static va_list uu_panic_args;
70 static pthread_t uu_panic_thread;
72 static uint32_t _uu_main_error;
75 uu_set_error(uint_t code)
78 #if defined(PTHREAD_ONCE_KEY_NP)
79 if (pthread_key_create_once_np(&uu_error_key, NULL) != 0)
80 uu_error_key_setup = -1;
82 uu_error_key_setup = 1;
83 #else /* PTHREAD_ONCE_KEY_NP */
84 if (uu_error_key_setup == 0) {
85 (void) pthread_mutex_lock(&uu_key_lock);
86 if (uu_error_key_setup == 0) {
87 if (pthread_key_create(&uu_error_key, NULL) != 0)
88 uu_error_key_setup = -1;
90 uu_error_key_setup = 1;
92 (void) pthread_mutex_unlock(&uu_key_lock);
94 #endif /* PTHREAD_ONCE_KEY_NP */
95 if (uu_error_key_setup > 0)
96 (void) pthread_setspecific(uu_error_key,
97 (void *)(uintptr_t)code);
104 if (uu_error_key_setup < 0) /* can't happen? */
105 return (UU_ERROR_UNKNOWN);
108 * Because UU_ERROR_NONE == 0, if uu_set_error() was
109 * never called, then this will return UU_ERROR_NONE:
111 return ((uint32_t)(uintptr_t)pthread_getspecific(uu_error_key));
115 uu_strerror(uint32_t code)
121 str = dgettext(TEXT_DOMAIN, "No error");
124 case UU_ERROR_INVALID_ARGUMENT:
125 str = dgettext(TEXT_DOMAIN, "Invalid argument");
128 case UU_ERROR_UNKNOWN_FLAG:
129 str = dgettext(TEXT_DOMAIN, "Unknown flag passed");
132 case UU_ERROR_NO_MEMORY:
133 str = dgettext(TEXT_DOMAIN, "Out of memory");
136 case UU_ERROR_CALLBACK_FAILED:
137 str = dgettext(TEXT_DOMAIN, "Callback-initiated failure");
140 case UU_ERROR_NOT_SUPPORTED:
141 str = dgettext(TEXT_DOMAIN, "Operation not supported");
145 str = dgettext(TEXT_DOMAIN, "No value provided");
148 case UU_ERROR_UNDERFLOW:
149 str = dgettext(TEXT_DOMAIN, "Value too small");
152 case UU_ERROR_OVERFLOW:
153 str = dgettext(TEXT_DOMAIN, "Value too large");
156 case UU_ERROR_INVALID_CHAR:
157 str = dgettext(TEXT_DOMAIN,
158 "Value contains unexpected character");
161 case UU_ERROR_INVALID_DIGIT:
162 str = dgettext(TEXT_DOMAIN,
163 "Value contains digit not in base");
166 case UU_ERROR_SYSTEM:
167 str = dgettext(TEXT_DOMAIN, "Underlying system error");
170 case UU_ERROR_UNKNOWN:
171 str = dgettext(TEXT_DOMAIN, "Error status not known");
183 uu_panic(const char *format, ...)
187 va_start(args, format);
189 (void) pthread_mutex_lock(&uu_panic_lock);
190 if (uu_panic_thread == 0) {
191 uu_panic_thread = pthread_self();
192 uu_panic_format = format;
193 va_copy(uu_panic_args, args);
195 (void) pthread_mutex_unlock(&uu_panic_lock);
197 (void) vfprintf(stderr, format, args);
199 if (uu_panic_thread == pthread_self())
207 assfail(const char *astring, const char *file, int line)
209 __assert(astring, file, line);
217 (void) pthread_mutex_lock(&uu_panic_lock);
218 #if !defined(PTHREAD_ONCE_KEY_NP)
219 (void) pthread_mutex_lock(&uu_key_lock);
228 (void) pthread_mutex_unlock(&uu_panic_lock);
229 #if !defined(PTHREAD_ONCE_KEY_NP)
230 (void) pthread_mutex_unlock(&uu_key_lock);
237 uu_release_child(void)
239 uu_panic_format = NULL;
245 #pragma init(uu_init)
249 (void) pthread_atfork(uu_lockup, uu_release, uu_release_child);