1eda14cbcSMatt Macy /*
2eda14cbcSMatt Macy * CDDL HEADER START
3eda14cbcSMatt Macy *
4eda14cbcSMatt Macy * The contents of this file are subject to the terms of the
5eda14cbcSMatt Macy * Common Development and Distribution License (the "License").
6eda14cbcSMatt Macy * You may not use this file except in compliance with the License.
7eda14cbcSMatt Macy *
8eda14cbcSMatt Macy * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*271171e0SMartin Matuska * or https://opensource.org/licenses/CDDL-1.0.
10eda14cbcSMatt Macy * See the License for the specific language governing permissions
11eda14cbcSMatt Macy * and limitations under the License.
12eda14cbcSMatt Macy *
13eda14cbcSMatt Macy * When distributing Covered Code, include this CDDL HEADER in each
14eda14cbcSMatt Macy * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15eda14cbcSMatt Macy * If applicable, add the following below this CDDL HEADER, with the
16eda14cbcSMatt Macy * fields enclosed by brackets "[]" replaced with your own identifying
17eda14cbcSMatt Macy * information: Portions Copyright [yyyy] [name of copyright owner]
18eda14cbcSMatt Macy *
19eda14cbcSMatt Macy * CDDL HEADER END
20eda14cbcSMatt Macy */
21eda14cbcSMatt Macy
22eda14cbcSMatt Macy /*
23eda14cbcSMatt Macy * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
24eda14cbcSMatt Macy */
25eda14cbcSMatt Macy
26eda14cbcSMatt Macy #include "libuutil_common.h"
27eda14cbcSMatt Macy
28eda14cbcSMatt Macy #include <assert.h>
29eda14cbcSMatt Macy #include <errno.h>
30eda14cbcSMatt Macy #include <libintl.h>
31eda14cbcSMatt Macy #include <pthread.h>
32eda14cbcSMatt Macy #include <stdarg.h>
33eda14cbcSMatt Macy #include <stdio.h>
34eda14cbcSMatt Macy #include <stdlib.h>
35eda14cbcSMatt Macy #include <string.h>
36eda14cbcSMatt Macy #include <sys/debug.h>
37eda14cbcSMatt Macy #include <unistd.h>
38eda14cbcSMatt Macy #include <ctype.h>
39eda14cbcSMatt Macy
40eda14cbcSMatt Macy #if !defined(TEXT_DOMAIN)
41eda14cbcSMatt Macy #define TEXT_DOMAIN "SYS_TEST"
42eda14cbcSMatt Macy #endif
43eda14cbcSMatt Macy
44eda14cbcSMatt Macy /*
45eda14cbcSMatt Macy * All of the old code under !defined(PTHREAD_ONCE_KEY_NP)
46eda14cbcSMatt Macy * is here to enable the building of a native version of
47eda14cbcSMatt Macy * libuutil.so when the build machine has not yet been upgraded
48eda14cbcSMatt Macy * to a version of libc that provides pthread_key_create_once_np().
49eda14cbcSMatt Macy * It should all be deleted when solaris_nevada ships.
50eda14cbcSMatt Macy * The code is not MT-safe in a relaxed memory model.
51eda14cbcSMatt Macy */
52eda14cbcSMatt Macy
53eda14cbcSMatt Macy #if defined(PTHREAD_ONCE_KEY_NP)
54eda14cbcSMatt Macy static pthread_key_t uu_error_key = PTHREAD_ONCE_KEY_NP;
55eda14cbcSMatt Macy #else /* PTHREAD_ONCE_KEY_NP */
56eda14cbcSMatt Macy static pthread_key_t uu_error_key = 0;
57eda14cbcSMatt Macy static pthread_mutex_t uu_key_lock = PTHREAD_MUTEX_INITIALIZER;
58eda14cbcSMatt Macy #endif /* PTHREAD_ONCE_KEY_NP */
59eda14cbcSMatt Macy
60eda14cbcSMatt Macy static int uu_error_key_setup = 0;
61eda14cbcSMatt Macy
62eda14cbcSMatt Macy static pthread_mutex_t uu_panic_lock = PTHREAD_MUTEX_INITIALIZER;
63eda14cbcSMatt Macy /* LINTED static unused */
64eda14cbcSMatt Macy static const char *uu_panic_format;
65eda14cbcSMatt Macy /* LINTED static unused */
66eda14cbcSMatt Macy static va_list uu_panic_args;
67eda14cbcSMatt Macy static pthread_t uu_panic_thread;
68eda14cbcSMatt Macy
69eda14cbcSMatt Macy static uint32_t _uu_main_error;
70eda14cbcSMatt Macy static __thread int _uu_main_thread = 0;
71eda14cbcSMatt Macy
72eda14cbcSMatt Macy void
uu_set_error(uint_t code)73eda14cbcSMatt Macy uu_set_error(uint_t code)
74eda14cbcSMatt Macy {
75eda14cbcSMatt Macy if (_uu_main_thread) {
76eda14cbcSMatt Macy _uu_main_error = code;
77eda14cbcSMatt Macy return;
78eda14cbcSMatt Macy }
79eda14cbcSMatt Macy #if defined(PTHREAD_ONCE_KEY_NP)
80eda14cbcSMatt Macy if (pthread_key_create_once_np(&uu_error_key, NULL) != 0)
81eda14cbcSMatt Macy uu_error_key_setup = -1;
82eda14cbcSMatt Macy else
83eda14cbcSMatt Macy uu_error_key_setup = 1;
84eda14cbcSMatt Macy #else /* PTHREAD_ONCE_KEY_NP */
85eda14cbcSMatt Macy if (uu_error_key_setup == 0) {
86eda14cbcSMatt Macy (void) pthread_mutex_lock(&uu_key_lock);
87eda14cbcSMatt Macy if (uu_error_key_setup == 0) {
88eda14cbcSMatt Macy if (pthread_key_create(&uu_error_key, NULL) != 0)
89eda14cbcSMatt Macy uu_error_key_setup = -1;
90eda14cbcSMatt Macy else
91eda14cbcSMatt Macy uu_error_key_setup = 1;
92eda14cbcSMatt Macy }
93eda14cbcSMatt Macy (void) pthread_mutex_unlock(&uu_key_lock);
94eda14cbcSMatt Macy }
95eda14cbcSMatt Macy #endif /* PTHREAD_ONCE_KEY_NP */
96eda14cbcSMatt Macy if (uu_error_key_setup > 0)
97eda14cbcSMatt Macy (void) pthread_setspecific(uu_error_key,
98eda14cbcSMatt Macy (void *)(uintptr_t)code);
99eda14cbcSMatt Macy }
100eda14cbcSMatt Macy
101eda14cbcSMatt Macy uint32_t
uu_error(void)102eda14cbcSMatt Macy uu_error(void)
103eda14cbcSMatt Macy {
104eda14cbcSMatt Macy if (_uu_main_thread)
105eda14cbcSMatt Macy return (_uu_main_error);
106eda14cbcSMatt Macy
107eda14cbcSMatt Macy if (uu_error_key_setup < 0) /* can't happen? */
108eda14cbcSMatt Macy return (UU_ERROR_UNKNOWN);
109eda14cbcSMatt Macy
110eda14cbcSMatt Macy /*
111eda14cbcSMatt Macy * Because UU_ERROR_NONE == 0, if uu_set_error() was
112eda14cbcSMatt Macy * never called, then this will return UU_ERROR_NONE:
113eda14cbcSMatt Macy */
114eda14cbcSMatt Macy return ((uint32_t)(uintptr_t)pthread_getspecific(uu_error_key));
115eda14cbcSMatt Macy }
116eda14cbcSMatt Macy
117eda14cbcSMatt Macy const char *
uu_strerror(uint32_t code)118eda14cbcSMatt Macy uu_strerror(uint32_t code)
119eda14cbcSMatt Macy {
120eda14cbcSMatt Macy const char *str;
121eda14cbcSMatt Macy
122eda14cbcSMatt Macy switch (code) {
123eda14cbcSMatt Macy case UU_ERROR_NONE:
124eda14cbcSMatt Macy str = dgettext(TEXT_DOMAIN, "No error");
125eda14cbcSMatt Macy break;
126eda14cbcSMatt Macy
127eda14cbcSMatt Macy case UU_ERROR_INVALID_ARGUMENT:
128eda14cbcSMatt Macy str = dgettext(TEXT_DOMAIN, "Invalid argument");
129eda14cbcSMatt Macy break;
130eda14cbcSMatt Macy
131eda14cbcSMatt Macy case UU_ERROR_UNKNOWN_FLAG:
132eda14cbcSMatt Macy str = dgettext(TEXT_DOMAIN, "Unknown flag passed");
133eda14cbcSMatt Macy break;
134eda14cbcSMatt Macy
135eda14cbcSMatt Macy case UU_ERROR_NO_MEMORY:
136eda14cbcSMatt Macy str = dgettext(TEXT_DOMAIN, "Out of memory");
137eda14cbcSMatt Macy break;
138eda14cbcSMatt Macy
139eda14cbcSMatt Macy case UU_ERROR_CALLBACK_FAILED:
140eda14cbcSMatt Macy str = dgettext(TEXT_DOMAIN, "Callback-initiated failure");
141eda14cbcSMatt Macy break;
142eda14cbcSMatt Macy
143eda14cbcSMatt Macy case UU_ERROR_NOT_SUPPORTED:
144eda14cbcSMatt Macy str = dgettext(TEXT_DOMAIN, "Operation not supported");
145eda14cbcSMatt Macy break;
146eda14cbcSMatt Macy
147eda14cbcSMatt Macy case UU_ERROR_EMPTY:
148eda14cbcSMatt Macy str = dgettext(TEXT_DOMAIN, "No value provided");
149eda14cbcSMatt Macy break;
150eda14cbcSMatt Macy
151eda14cbcSMatt Macy case UU_ERROR_UNDERFLOW:
152eda14cbcSMatt Macy str = dgettext(TEXT_DOMAIN, "Value too small");
153eda14cbcSMatt Macy break;
154eda14cbcSMatt Macy
155eda14cbcSMatt Macy case UU_ERROR_OVERFLOW:
156eda14cbcSMatt Macy str = dgettext(TEXT_DOMAIN, "Value too large");
157eda14cbcSMatt Macy break;
158eda14cbcSMatt Macy
159eda14cbcSMatt Macy case UU_ERROR_INVALID_CHAR:
160eda14cbcSMatt Macy str = dgettext(TEXT_DOMAIN,
161eda14cbcSMatt Macy "Value contains unexpected character");
162eda14cbcSMatt Macy break;
163eda14cbcSMatt Macy
164eda14cbcSMatt Macy case UU_ERROR_INVALID_DIGIT:
165eda14cbcSMatt Macy str = dgettext(TEXT_DOMAIN,
166eda14cbcSMatt Macy "Value contains digit not in base");
167eda14cbcSMatt Macy break;
168eda14cbcSMatt Macy
169eda14cbcSMatt Macy case UU_ERROR_SYSTEM:
170eda14cbcSMatt Macy str = dgettext(TEXT_DOMAIN, "Underlying system error");
171eda14cbcSMatt Macy break;
172eda14cbcSMatt Macy
173eda14cbcSMatt Macy case UU_ERROR_UNKNOWN:
174eda14cbcSMatt Macy str = dgettext(TEXT_DOMAIN, "Error status not known");
175eda14cbcSMatt Macy break;
176eda14cbcSMatt Macy
177eda14cbcSMatt Macy default:
178eda14cbcSMatt Macy errno = ESRCH;
179eda14cbcSMatt Macy str = NULL;
180eda14cbcSMatt Macy break;
181eda14cbcSMatt Macy }
182eda14cbcSMatt Macy return (str);
183eda14cbcSMatt Macy }
184eda14cbcSMatt Macy
185eda14cbcSMatt Macy void
uu_panic(const char * format,...)186eda14cbcSMatt Macy uu_panic(const char *format, ...)
187eda14cbcSMatt Macy {
188eda14cbcSMatt Macy va_list args;
189eda14cbcSMatt Macy
190eda14cbcSMatt Macy va_start(args, format);
191eda14cbcSMatt Macy
192eda14cbcSMatt Macy (void) pthread_mutex_lock(&uu_panic_lock);
193eda14cbcSMatt Macy if (uu_panic_thread == 0) {
194eda14cbcSMatt Macy uu_panic_thread = pthread_self();
195eda14cbcSMatt Macy uu_panic_format = format;
196eda14cbcSMatt Macy va_copy(uu_panic_args, args);
197eda14cbcSMatt Macy }
198eda14cbcSMatt Macy (void) pthread_mutex_unlock(&uu_panic_lock);
199eda14cbcSMatt Macy
200eda14cbcSMatt Macy (void) vfprintf(stderr, format, args);
201eda14cbcSMatt Macy
202eda14cbcSMatt Macy va_end(args);
203eda14cbcSMatt Macy
204eda14cbcSMatt Macy if (uu_panic_thread == pthread_self())
205eda14cbcSMatt Macy abort();
206eda14cbcSMatt Macy else
207eda14cbcSMatt Macy for (;;)
208eda14cbcSMatt Macy (void) pause();
209eda14cbcSMatt Macy }
210eda14cbcSMatt Macy
211eda14cbcSMatt Macy static void
uu_lockup(void)212eda14cbcSMatt Macy uu_lockup(void)
213eda14cbcSMatt Macy {
214eda14cbcSMatt Macy (void) pthread_mutex_lock(&uu_panic_lock);
215eda14cbcSMatt Macy #if !defined(PTHREAD_ONCE_KEY_NP)
216eda14cbcSMatt Macy (void) pthread_mutex_lock(&uu_key_lock);
217eda14cbcSMatt Macy #endif
218eda14cbcSMatt Macy uu_avl_lockup();
219eda14cbcSMatt Macy uu_list_lockup();
220eda14cbcSMatt Macy }
221eda14cbcSMatt Macy
222eda14cbcSMatt Macy static void
uu_release(void)223eda14cbcSMatt Macy uu_release(void)
224eda14cbcSMatt Macy {
225eda14cbcSMatt Macy (void) pthread_mutex_unlock(&uu_panic_lock);
226eda14cbcSMatt Macy #if !defined(PTHREAD_ONCE_KEY_NP)
227eda14cbcSMatt Macy (void) pthread_mutex_unlock(&uu_key_lock);
228eda14cbcSMatt Macy #endif
229eda14cbcSMatt Macy uu_avl_release();
230eda14cbcSMatt Macy uu_list_release();
231eda14cbcSMatt Macy }
232eda14cbcSMatt Macy
233eda14cbcSMatt Macy static void
uu_release_child(void)234eda14cbcSMatt Macy uu_release_child(void)
235eda14cbcSMatt Macy {
236eda14cbcSMatt Macy uu_panic_format = NULL;
237eda14cbcSMatt Macy uu_panic_thread = 0;
238eda14cbcSMatt Macy
239eda14cbcSMatt Macy uu_release();
240eda14cbcSMatt Macy }
241eda14cbcSMatt Macy
242eda14cbcSMatt Macy #ifdef __GNUC__
243eda14cbcSMatt Macy static void
244eda14cbcSMatt Macy uu_init(void) __attribute__((constructor));
245eda14cbcSMatt Macy #else
246eda14cbcSMatt Macy #pragma init(uu_init)
247eda14cbcSMatt Macy #endif
248eda14cbcSMatt Macy
249eda14cbcSMatt Macy static void
uu_init(void)250eda14cbcSMatt Macy uu_init(void)
251eda14cbcSMatt Macy {
252eda14cbcSMatt Macy _uu_main_thread = 1;
253eda14cbcSMatt Macy (void) pthread_atfork(uu_lockup, uu_release, uu_release_child);
254eda14cbcSMatt Macy }
255