10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
53864Sraf * Common Development and Distribution License (the "License").
63864Sraf * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
213864Sraf
220Sstevel@tonic-gate /*
23*12890SJoyce.McIntosh@Sun.COM * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #include "libuutil_common.h"
270Sstevel@tonic-gate
280Sstevel@tonic-gate #include <assert.h>
290Sstevel@tonic-gate #include <errno.h>
300Sstevel@tonic-gate #include <libintl.h>
310Sstevel@tonic-gate #include <pthread.h>
320Sstevel@tonic-gate #include <stdarg.h>
330Sstevel@tonic-gate #include <stdio.h>
340Sstevel@tonic-gate #include <stdlib.h>
350Sstevel@tonic-gate #include <string.h>
360Sstevel@tonic-gate #include <sys/debug.h>
370Sstevel@tonic-gate #include <thread.h>
380Sstevel@tonic-gate #include <unistd.h>
39*12890SJoyce.McIntosh@Sun.COM #include <ctype.h>
400Sstevel@tonic-gate
410Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
420Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST"
430Sstevel@tonic-gate #endif
440Sstevel@tonic-gate
453864Sraf /*
463864Sraf * All of the old code under !defined(PTHREAD_ONCE_KEY_NP)
473864Sraf * is here to enable the building of a native version of
483864Sraf * libuutil.so when the build machine has not yet been upgraded
493864Sraf * to a version of libc that provides pthread_key_create_once_np().
503864Sraf * It should all be deleted when solaris_nevada ships.
513864Sraf * The code is not MT-safe in a relaxed memory model.
523864Sraf */
533864Sraf
543864Sraf #if defined(PTHREAD_ONCE_KEY_NP)
553864Sraf static pthread_key_t uu_error_key = PTHREAD_ONCE_KEY_NP;
563864Sraf #else /* PTHREAD_ONCE_KEY_NP */
573864Sraf static pthread_key_t uu_error_key = 0;
580Sstevel@tonic-gate static pthread_mutex_t uu_key_lock = PTHREAD_MUTEX_INITIALIZER;
593864Sraf #endif /* PTHREAD_ONCE_KEY_NP */
603864Sraf
613864Sraf static int uu_error_key_setup = 0;
620Sstevel@tonic-gate
630Sstevel@tonic-gate static pthread_mutex_t uu_panic_lock = PTHREAD_MUTEX_INITIALIZER;
640Sstevel@tonic-gate /* LINTED static unused */
650Sstevel@tonic-gate static const char *uu_panic_format;
660Sstevel@tonic-gate /* LINTED static unused */
670Sstevel@tonic-gate static va_list uu_panic_args;
680Sstevel@tonic-gate static pthread_t uu_panic_thread;
690Sstevel@tonic-gate
700Sstevel@tonic-gate static uint32_t _uu_main_error;
710Sstevel@tonic-gate
720Sstevel@tonic-gate void
uu_set_error(uint_t code)730Sstevel@tonic-gate uu_set_error(uint_t code)
740Sstevel@tonic-gate {
750Sstevel@tonic-gate if (thr_main() != 0) {
760Sstevel@tonic-gate _uu_main_error = code;
770Sstevel@tonic-gate return;
780Sstevel@tonic-gate }
793864Sraf #if defined(PTHREAD_ONCE_KEY_NP)
803864Sraf if (pthread_key_create_once_np(&uu_error_key, NULL) != 0)
813864Sraf uu_error_key_setup = -1;
823864Sraf else
833864Sraf uu_error_key_setup = 1;
843864Sraf #else /* PTHREAD_ONCE_KEY_NP */
850Sstevel@tonic-gate if (uu_error_key_setup == 0) {
860Sstevel@tonic-gate (void) pthread_mutex_lock(&uu_key_lock);
870Sstevel@tonic-gate if (uu_error_key_setup == 0) {
883864Sraf if (pthread_key_create(&uu_error_key, NULL) != 0)
890Sstevel@tonic-gate uu_error_key_setup = -1;
900Sstevel@tonic-gate else
910Sstevel@tonic-gate uu_error_key_setup = 1;
920Sstevel@tonic-gate }
930Sstevel@tonic-gate (void) pthread_mutex_unlock(&uu_key_lock);
940Sstevel@tonic-gate }
953864Sraf #endif /* PTHREAD_ONCE_KEY_NP */
960Sstevel@tonic-gate if (uu_error_key_setup > 0)
970Sstevel@tonic-gate (void) pthread_setspecific(uu_error_key,
980Sstevel@tonic-gate (void *)(uintptr_t)code);
990Sstevel@tonic-gate }
1000Sstevel@tonic-gate
1010Sstevel@tonic-gate uint32_t
uu_error(void)1020Sstevel@tonic-gate uu_error(void)
1030Sstevel@tonic-gate {
1040Sstevel@tonic-gate if (thr_main() != 0)
1050Sstevel@tonic-gate return (_uu_main_error);
1060Sstevel@tonic-gate
1073864Sraf if (uu_error_key_setup < 0) /* can't happen? */
1080Sstevel@tonic-gate return (UU_ERROR_UNKNOWN);
1093864Sraf
1103864Sraf /*
1113864Sraf * Because UU_ERROR_NONE == 0, if uu_set_error() was
1123864Sraf * never called, then this will return UU_ERROR_NONE:
1133864Sraf */
1143864Sraf return ((uint32_t)(uintptr_t)pthread_getspecific(uu_error_key));
1150Sstevel@tonic-gate }
1160Sstevel@tonic-gate
1170Sstevel@tonic-gate const char *
uu_strerror(uint32_t code)1180Sstevel@tonic-gate uu_strerror(uint32_t code)
1190Sstevel@tonic-gate {
1200Sstevel@tonic-gate const char *str;
1210Sstevel@tonic-gate
1220Sstevel@tonic-gate switch (code) {
1230Sstevel@tonic-gate case UU_ERROR_NONE:
1240Sstevel@tonic-gate str = dgettext(TEXT_DOMAIN, "No error");
1250Sstevel@tonic-gate break;
1260Sstevel@tonic-gate
1270Sstevel@tonic-gate case UU_ERROR_INVALID_ARGUMENT:
1280Sstevel@tonic-gate str = dgettext(TEXT_DOMAIN, "Invalid argument");
1290Sstevel@tonic-gate break;
1300Sstevel@tonic-gate
1310Sstevel@tonic-gate case UU_ERROR_UNKNOWN_FLAG:
1320Sstevel@tonic-gate str = dgettext(TEXT_DOMAIN, "Unknown flag passed");
1330Sstevel@tonic-gate break;
1340Sstevel@tonic-gate
1350Sstevel@tonic-gate case UU_ERROR_NO_MEMORY:
1360Sstevel@tonic-gate str = dgettext(TEXT_DOMAIN, "Out of memory");
1370Sstevel@tonic-gate break;
1380Sstevel@tonic-gate
1390Sstevel@tonic-gate case UU_ERROR_CALLBACK_FAILED:
1400Sstevel@tonic-gate str = dgettext(TEXT_DOMAIN, "Callback-initiated failure");
1410Sstevel@tonic-gate break;
1420Sstevel@tonic-gate
1430Sstevel@tonic-gate case UU_ERROR_NOT_SUPPORTED:
1440Sstevel@tonic-gate str = dgettext(TEXT_DOMAIN, "Operation not supported");
1450Sstevel@tonic-gate break;
1460Sstevel@tonic-gate
1470Sstevel@tonic-gate case UU_ERROR_EMPTY:
1480Sstevel@tonic-gate str = dgettext(TEXT_DOMAIN, "No value provided");
1490Sstevel@tonic-gate break;
1500Sstevel@tonic-gate
1510Sstevel@tonic-gate case UU_ERROR_UNDERFLOW:
1520Sstevel@tonic-gate str = dgettext(TEXT_DOMAIN, "Value too small");
1530Sstevel@tonic-gate break;
1540Sstevel@tonic-gate
1550Sstevel@tonic-gate case UU_ERROR_OVERFLOW:
1560Sstevel@tonic-gate str = dgettext(TEXT_DOMAIN, "Value too large");
1570Sstevel@tonic-gate break;
1580Sstevel@tonic-gate
1590Sstevel@tonic-gate case UU_ERROR_INVALID_CHAR:
1600Sstevel@tonic-gate str = dgettext(TEXT_DOMAIN,
1610Sstevel@tonic-gate "Value contains unexpected character");
1620Sstevel@tonic-gate break;
1630Sstevel@tonic-gate
1640Sstevel@tonic-gate case UU_ERROR_INVALID_DIGIT:
1650Sstevel@tonic-gate str = dgettext(TEXT_DOMAIN,
1660Sstevel@tonic-gate "Value contains digit not in base");
1670Sstevel@tonic-gate break;
1680Sstevel@tonic-gate
1690Sstevel@tonic-gate case UU_ERROR_SYSTEM:
1700Sstevel@tonic-gate str = dgettext(TEXT_DOMAIN, "Underlying system error");
1710Sstevel@tonic-gate break;
1720Sstevel@tonic-gate
1730Sstevel@tonic-gate case UU_ERROR_UNKNOWN:
1740Sstevel@tonic-gate str = dgettext(TEXT_DOMAIN, "Error status not known");
1750Sstevel@tonic-gate break;
1760Sstevel@tonic-gate
1770Sstevel@tonic-gate default:
1780Sstevel@tonic-gate errno = ESRCH;
1790Sstevel@tonic-gate str = NULL;
1800Sstevel@tonic-gate break;
1810Sstevel@tonic-gate }
1820Sstevel@tonic-gate return (str);
1830Sstevel@tonic-gate }
1840Sstevel@tonic-gate
1850Sstevel@tonic-gate void
uu_panic(const char * format,...)1860Sstevel@tonic-gate uu_panic(const char *format, ...)
1870Sstevel@tonic-gate {
1880Sstevel@tonic-gate va_list args;
1890Sstevel@tonic-gate
1900Sstevel@tonic-gate va_start(args, format);
1910Sstevel@tonic-gate
1920Sstevel@tonic-gate (void) pthread_mutex_lock(&uu_panic_lock);
1930Sstevel@tonic-gate if (uu_panic_thread == 0) {
1940Sstevel@tonic-gate uu_panic_thread = pthread_self();
1950Sstevel@tonic-gate uu_panic_format = format;
1960Sstevel@tonic-gate va_copy(uu_panic_args, args);
1970Sstevel@tonic-gate }
1980Sstevel@tonic-gate (void) pthread_mutex_unlock(&uu_panic_lock);
1990Sstevel@tonic-gate
2000Sstevel@tonic-gate (void) vfprintf(stderr, format, args);
2010Sstevel@tonic-gate
2020Sstevel@tonic-gate if (uu_panic_thread == pthread_self())
2030Sstevel@tonic-gate abort();
2040Sstevel@tonic-gate else
2050Sstevel@tonic-gate for (;;)
2060Sstevel@tonic-gate (void) pause();
2070Sstevel@tonic-gate }
2080Sstevel@tonic-gate
2090Sstevel@tonic-gate int
assfail(const char * astring,const char * file,int line)2100Sstevel@tonic-gate assfail(const char *astring, const char *file, int line)
2110Sstevel@tonic-gate {
2120Sstevel@tonic-gate __assert(astring, file, line);
2130Sstevel@tonic-gate /*NOTREACHED*/
2140Sstevel@tonic-gate return (0);
2150Sstevel@tonic-gate }
2160Sstevel@tonic-gate
2170Sstevel@tonic-gate static void
uu_lockup(void)2180Sstevel@tonic-gate uu_lockup(void)
2190Sstevel@tonic-gate {
2200Sstevel@tonic-gate (void) pthread_mutex_lock(&uu_panic_lock);
2213864Sraf #if !defined(PTHREAD_ONCE_KEY_NP)
2220Sstevel@tonic-gate (void) pthread_mutex_lock(&uu_key_lock);
2233864Sraf #endif
2240Sstevel@tonic-gate uu_avl_lockup();
2250Sstevel@tonic-gate uu_list_lockup();
2260Sstevel@tonic-gate }
2270Sstevel@tonic-gate
2280Sstevel@tonic-gate static void
uu_release(void)2290Sstevel@tonic-gate uu_release(void)
2300Sstevel@tonic-gate {
2310Sstevel@tonic-gate (void) pthread_mutex_unlock(&uu_panic_lock);
2323864Sraf #if !defined(PTHREAD_ONCE_KEY_NP)
2330Sstevel@tonic-gate (void) pthread_mutex_unlock(&uu_key_lock);
2343864Sraf #endif
2350Sstevel@tonic-gate uu_avl_release();
2360Sstevel@tonic-gate uu_list_release();
2370Sstevel@tonic-gate }
2380Sstevel@tonic-gate
2390Sstevel@tonic-gate static void
uu_release_child(void)2400Sstevel@tonic-gate uu_release_child(void)
2410Sstevel@tonic-gate {
2420Sstevel@tonic-gate uu_panic_format = NULL;
2430Sstevel@tonic-gate uu_panic_thread = 0;
2440Sstevel@tonic-gate
2450Sstevel@tonic-gate uu_release();
2460Sstevel@tonic-gate }
2470Sstevel@tonic-gate
2480Sstevel@tonic-gate #pragma init(uu_init)
2490Sstevel@tonic-gate static void
uu_init(void)2500Sstevel@tonic-gate uu_init(void)
2510Sstevel@tonic-gate {
2520Sstevel@tonic-gate (void) pthread_atfork(uu_lockup, uu_release, uu_release_child);
2530Sstevel@tonic-gate }
254*12890SJoyce.McIntosh@Sun.COM
255*12890SJoyce.McIntosh@Sun.COM /*
256*12890SJoyce.McIntosh@Sun.COM * Dump a block of memory in hex+ascii, for debugging
257*12890SJoyce.McIntosh@Sun.COM */
258*12890SJoyce.McIntosh@Sun.COM void
uu_dump(FILE * out,const char * prefix,const void * buf,size_t len)259*12890SJoyce.McIntosh@Sun.COM uu_dump(FILE *out, const char *prefix, const void *buf, size_t len)
260*12890SJoyce.McIntosh@Sun.COM {
261*12890SJoyce.McIntosh@Sun.COM const unsigned char *p = buf;
262*12890SJoyce.McIntosh@Sun.COM int i;
263*12890SJoyce.McIntosh@Sun.COM
264*12890SJoyce.McIntosh@Sun.COM for (i = 0; i < len; i += 16) {
265*12890SJoyce.McIntosh@Sun.COM int j;
266*12890SJoyce.McIntosh@Sun.COM
267*12890SJoyce.McIntosh@Sun.COM (void) fprintf(out, "%s", prefix);
268*12890SJoyce.McIntosh@Sun.COM for (j = 0; j < 16 && i + j < len; j++) {
269*12890SJoyce.McIntosh@Sun.COM (void) fprintf(out, "%2.2x ", p[i + j]);
270*12890SJoyce.McIntosh@Sun.COM }
271*12890SJoyce.McIntosh@Sun.COM for (; j < 16; j++) {
272*12890SJoyce.McIntosh@Sun.COM (void) fprintf(out, " ");
273*12890SJoyce.McIntosh@Sun.COM }
274*12890SJoyce.McIntosh@Sun.COM for (j = 0; j < 16 && i + j < len; j++) {
275*12890SJoyce.McIntosh@Sun.COM (void) fprintf(out, "%c",
276*12890SJoyce.McIntosh@Sun.COM isprint(p[i + j]) ? p[i + j] : '.');
277*12890SJoyce.McIntosh@Sun.COM }
278*12890SJoyce.McIntosh@Sun.COM (void) fprintf(out, "\n");
279*12890SJoyce.McIntosh@Sun.COM }
280*12890SJoyce.McIntosh@Sun.COM }
281