17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5cb620785Sraf * Common Development and Distribution License (the "License").
6cb620785Sraf * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
21cb620785Sraf
227c478bd9Sstevel@tonic-gate /*
23*cb174861Sjoyce mcintosh * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate #include "libuutil_common.h"
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate #include <assert.h>
297c478bd9Sstevel@tonic-gate #include <errno.h>
307c478bd9Sstevel@tonic-gate #include <libintl.h>
317c478bd9Sstevel@tonic-gate #include <pthread.h>
327c478bd9Sstevel@tonic-gate #include <stdarg.h>
337c478bd9Sstevel@tonic-gate #include <stdio.h>
347c478bd9Sstevel@tonic-gate #include <stdlib.h>
357c478bd9Sstevel@tonic-gate #include <string.h>
367c478bd9Sstevel@tonic-gate #include <sys/debug.h>
377c478bd9Sstevel@tonic-gate #include <thread.h>
387c478bd9Sstevel@tonic-gate #include <unistd.h>
39*cb174861Sjoyce mcintosh #include <ctype.h>
407c478bd9Sstevel@tonic-gate
417c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
427c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST"
437c478bd9Sstevel@tonic-gate #endif
447c478bd9Sstevel@tonic-gate
45cb620785Sraf /*
46cb620785Sraf * All of the old code under !defined(PTHREAD_ONCE_KEY_NP)
47cb620785Sraf * is here to enable the building of a native version of
48cb620785Sraf * libuutil.so when the build machine has not yet been upgraded
49cb620785Sraf * to a version of libc that provides pthread_key_create_once_np().
50cb620785Sraf * It should all be deleted when solaris_nevada ships.
51cb620785Sraf * The code is not MT-safe in a relaxed memory model.
52cb620785Sraf */
53cb620785Sraf
54cb620785Sraf #if defined(PTHREAD_ONCE_KEY_NP)
55cb620785Sraf static pthread_key_t uu_error_key = PTHREAD_ONCE_KEY_NP;
56cb620785Sraf #else /* PTHREAD_ONCE_KEY_NP */
57cb620785Sraf static pthread_key_t uu_error_key = 0;
587c478bd9Sstevel@tonic-gate static pthread_mutex_t uu_key_lock = PTHREAD_MUTEX_INITIALIZER;
59cb620785Sraf #endif /* PTHREAD_ONCE_KEY_NP */
60cb620785Sraf
61cb620785Sraf static int uu_error_key_setup = 0;
627c478bd9Sstevel@tonic-gate
637c478bd9Sstevel@tonic-gate static pthread_mutex_t uu_panic_lock = PTHREAD_MUTEX_INITIALIZER;
647c478bd9Sstevel@tonic-gate /* LINTED static unused */
657c478bd9Sstevel@tonic-gate static const char *uu_panic_format;
667c478bd9Sstevel@tonic-gate /* LINTED static unused */
677c478bd9Sstevel@tonic-gate static va_list uu_panic_args;
687c478bd9Sstevel@tonic-gate static pthread_t uu_panic_thread;
697c478bd9Sstevel@tonic-gate
707c478bd9Sstevel@tonic-gate static uint32_t _uu_main_error;
717c478bd9Sstevel@tonic-gate
727c478bd9Sstevel@tonic-gate void
uu_set_error(uint_t code)737c478bd9Sstevel@tonic-gate uu_set_error(uint_t code)
747c478bd9Sstevel@tonic-gate {
757c478bd9Sstevel@tonic-gate if (thr_main() != 0) {
767c478bd9Sstevel@tonic-gate _uu_main_error = code;
777c478bd9Sstevel@tonic-gate return;
787c478bd9Sstevel@tonic-gate }
79cb620785Sraf #if defined(PTHREAD_ONCE_KEY_NP)
80cb620785Sraf if (pthread_key_create_once_np(&uu_error_key, NULL) != 0)
81cb620785Sraf uu_error_key_setup = -1;
82cb620785Sraf else
83cb620785Sraf uu_error_key_setup = 1;
84cb620785Sraf #else /* PTHREAD_ONCE_KEY_NP */
857c478bd9Sstevel@tonic-gate if (uu_error_key_setup == 0) {
867c478bd9Sstevel@tonic-gate (void) pthread_mutex_lock(&uu_key_lock);
877c478bd9Sstevel@tonic-gate if (uu_error_key_setup == 0) {
88cb620785Sraf if (pthread_key_create(&uu_error_key, NULL) != 0)
897c478bd9Sstevel@tonic-gate uu_error_key_setup = -1;
907c478bd9Sstevel@tonic-gate else
917c478bd9Sstevel@tonic-gate uu_error_key_setup = 1;
927c478bd9Sstevel@tonic-gate }
937c478bd9Sstevel@tonic-gate (void) pthread_mutex_unlock(&uu_key_lock);
947c478bd9Sstevel@tonic-gate }
95cb620785Sraf #endif /* PTHREAD_ONCE_KEY_NP */
967c478bd9Sstevel@tonic-gate if (uu_error_key_setup > 0)
977c478bd9Sstevel@tonic-gate (void) pthread_setspecific(uu_error_key,
987c478bd9Sstevel@tonic-gate (void *)(uintptr_t)code);
997c478bd9Sstevel@tonic-gate }
1007c478bd9Sstevel@tonic-gate
1017c478bd9Sstevel@tonic-gate uint32_t
uu_error(void)1027c478bd9Sstevel@tonic-gate uu_error(void)
1037c478bd9Sstevel@tonic-gate {
1047c478bd9Sstevel@tonic-gate if (thr_main() != 0)
1057c478bd9Sstevel@tonic-gate return (_uu_main_error);
1067c478bd9Sstevel@tonic-gate
107cb620785Sraf if (uu_error_key_setup < 0) /* can't happen? */
1087c478bd9Sstevel@tonic-gate return (UU_ERROR_UNKNOWN);
109cb620785Sraf
110cb620785Sraf /*
111cb620785Sraf * Because UU_ERROR_NONE == 0, if uu_set_error() was
112cb620785Sraf * never called, then this will return UU_ERROR_NONE:
113cb620785Sraf */
1147c478bd9Sstevel@tonic-gate return ((uint32_t)(uintptr_t)pthread_getspecific(uu_error_key));
1157c478bd9Sstevel@tonic-gate }
1167c478bd9Sstevel@tonic-gate
1177c478bd9Sstevel@tonic-gate const char *
uu_strerror(uint32_t code)1187c478bd9Sstevel@tonic-gate uu_strerror(uint32_t code)
1197c478bd9Sstevel@tonic-gate {
1207c478bd9Sstevel@tonic-gate const char *str;
1217c478bd9Sstevel@tonic-gate
1227c478bd9Sstevel@tonic-gate switch (code) {
1237c478bd9Sstevel@tonic-gate case UU_ERROR_NONE:
1247c478bd9Sstevel@tonic-gate str = dgettext(TEXT_DOMAIN, "No error");
1257c478bd9Sstevel@tonic-gate break;
1267c478bd9Sstevel@tonic-gate
1277c478bd9Sstevel@tonic-gate case UU_ERROR_INVALID_ARGUMENT:
1287c478bd9Sstevel@tonic-gate str = dgettext(TEXT_DOMAIN, "Invalid argument");
1297c478bd9Sstevel@tonic-gate break;
1307c478bd9Sstevel@tonic-gate
1317c478bd9Sstevel@tonic-gate case UU_ERROR_UNKNOWN_FLAG:
1327c478bd9Sstevel@tonic-gate str = dgettext(TEXT_DOMAIN, "Unknown flag passed");
1337c478bd9Sstevel@tonic-gate break;
1347c478bd9Sstevel@tonic-gate
1357c478bd9Sstevel@tonic-gate case UU_ERROR_NO_MEMORY:
1367c478bd9Sstevel@tonic-gate str = dgettext(TEXT_DOMAIN, "Out of memory");
1377c478bd9Sstevel@tonic-gate break;
1387c478bd9Sstevel@tonic-gate
1397c478bd9Sstevel@tonic-gate case UU_ERROR_CALLBACK_FAILED:
1407c478bd9Sstevel@tonic-gate str = dgettext(TEXT_DOMAIN, "Callback-initiated failure");
1417c478bd9Sstevel@tonic-gate break;
1427c478bd9Sstevel@tonic-gate
1437c478bd9Sstevel@tonic-gate case UU_ERROR_NOT_SUPPORTED:
1447c478bd9Sstevel@tonic-gate str = dgettext(TEXT_DOMAIN, "Operation not supported");
1457c478bd9Sstevel@tonic-gate break;
1467c478bd9Sstevel@tonic-gate
1477c478bd9Sstevel@tonic-gate case UU_ERROR_EMPTY:
1487c478bd9Sstevel@tonic-gate str = dgettext(TEXT_DOMAIN, "No value provided");
1497c478bd9Sstevel@tonic-gate break;
1507c478bd9Sstevel@tonic-gate
1517c478bd9Sstevel@tonic-gate case UU_ERROR_UNDERFLOW:
1527c478bd9Sstevel@tonic-gate str = dgettext(TEXT_DOMAIN, "Value too small");
1537c478bd9Sstevel@tonic-gate break;
1547c478bd9Sstevel@tonic-gate
1557c478bd9Sstevel@tonic-gate case UU_ERROR_OVERFLOW:
1567c478bd9Sstevel@tonic-gate str = dgettext(TEXT_DOMAIN, "Value too large");
1577c478bd9Sstevel@tonic-gate break;
1587c478bd9Sstevel@tonic-gate
1597c478bd9Sstevel@tonic-gate case UU_ERROR_INVALID_CHAR:
1607c478bd9Sstevel@tonic-gate str = dgettext(TEXT_DOMAIN,
1617c478bd9Sstevel@tonic-gate "Value contains unexpected character");
1627c478bd9Sstevel@tonic-gate break;
1637c478bd9Sstevel@tonic-gate
1647c478bd9Sstevel@tonic-gate case UU_ERROR_INVALID_DIGIT:
1657c478bd9Sstevel@tonic-gate str = dgettext(TEXT_DOMAIN,
1667c478bd9Sstevel@tonic-gate "Value contains digit not in base");
1677c478bd9Sstevel@tonic-gate break;
1687c478bd9Sstevel@tonic-gate
1697c478bd9Sstevel@tonic-gate case UU_ERROR_SYSTEM:
1707c478bd9Sstevel@tonic-gate str = dgettext(TEXT_DOMAIN, "Underlying system error");
1717c478bd9Sstevel@tonic-gate break;
1727c478bd9Sstevel@tonic-gate
1737c478bd9Sstevel@tonic-gate case UU_ERROR_UNKNOWN:
1747c478bd9Sstevel@tonic-gate str = dgettext(TEXT_DOMAIN, "Error status not known");
1757c478bd9Sstevel@tonic-gate break;
1767c478bd9Sstevel@tonic-gate
1777c478bd9Sstevel@tonic-gate default:
1787c478bd9Sstevel@tonic-gate errno = ESRCH;
1797c478bd9Sstevel@tonic-gate str = NULL;
1807c478bd9Sstevel@tonic-gate break;
1817c478bd9Sstevel@tonic-gate }
1827c478bd9Sstevel@tonic-gate return (str);
1837c478bd9Sstevel@tonic-gate }
1847c478bd9Sstevel@tonic-gate
1857c478bd9Sstevel@tonic-gate void
uu_panic(const char * format,...)1867c478bd9Sstevel@tonic-gate uu_panic(const char *format, ...)
1877c478bd9Sstevel@tonic-gate {
1887c478bd9Sstevel@tonic-gate va_list args;
1897c478bd9Sstevel@tonic-gate
1907c478bd9Sstevel@tonic-gate va_start(args, format);
1917c478bd9Sstevel@tonic-gate
1927c478bd9Sstevel@tonic-gate (void) pthread_mutex_lock(&uu_panic_lock);
1937c478bd9Sstevel@tonic-gate if (uu_panic_thread == 0) {
1947c478bd9Sstevel@tonic-gate uu_panic_thread = pthread_self();
1957c478bd9Sstevel@tonic-gate uu_panic_format = format;
1967c478bd9Sstevel@tonic-gate va_copy(uu_panic_args, args);
1977c478bd9Sstevel@tonic-gate }
1987c478bd9Sstevel@tonic-gate (void) pthread_mutex_unlock(&uu_panic_lock);
1997c478bd9Sstevel@tonic-gate
2007c478bd9Sstevel@tonic-gate (void) vfprintf(stderr, format, args);
2017c478bd9Sstevel@tonic-gate
2027c478bd9Sstevel@tonic-gate if (uu_panic_thread == pthread_self())
2037c478bd9Sstevel@tonic-gate abort();
2047c478bd9Sstevel@tonic-gate else
2057c478bd9Sstevel@tonic-gate for (;;)
2067c478bd9Sstevel@tonic-gate (void) pause();
2077c478bd9Sstevel@tonic-gate }
2087c478bd9Sstevel@tonic-gate
2097c478bd9Sstevel@tonic-gate int
assfail(const char * astring,const char * file,int line)2107c478bd9Sstevel@tonic-gate assfail(const char *astring, const char *file, int line)
2117c478bd9Sstevel@tonic-gate {
2127c478bd9Sstevel@tonic-gate __assert(astring, file, line);
2137c478bd9Sstevel@tonic-gate /*NOTREACHED*/
2147c478bd9Sstevel@tonic-gate return (0);
2157c478bd9Sstevel@tonic-gate }
2167c478bd9Sstevel@tonic-gate
2177c478bd9Sstevel@tonic-gate static void
uu_lockup(void)2187c478bd9Sstevel@tonic-gate uu_lockup(void)
2197c478bd9Sstevel@tonic-gate {
2207c478bd9Sstevel@tonic-gate (void) pthread_mutex_lock(&uu_panic_lock);
221cb620785Sraf #if !defined(PTHREAD_ONCE_KEY_NP)
2227c478bd9Sstevel@tonic-gate (void) pthread_mutex_lock(&uu_key_lock);
223cb620785Sraf #endif
2247c478bd9Sstevel@tonic-gate uu_avl_lockup();
2257c478bd9Sstevel@tonic-gate uu_list_lockup();
2267c478bd9Sstevel@tonic-gate }
2277c478bd9Sstevel@tonic-gate
2287c478bd9Sstevel@tonic-gate static void
uu_release(void)2297c478bd9Sstevel@tonic-gate uu_release(void)
2307c478bd9Sstevel@tonic-gate {
2317c478bd9Sstevel@tonic-gate (void) pthread_mutex_unlock(&uu_panic_lock);
232cb620785Sraf #if !defined(PTHREAD_ONCE_KEY_NP)
2337c478bd9Sstevel@tonic-gate (void) pthread_mutex_unlock(&uu_key_lock);
234cb620785Sraf #endif
2357c478bd9Sstevel@tonic-gate uu_avl_release();
2367c478bd9Sstevel@tonic-gate uu_list_release();
2377c478bd9Sstevel@tonic-gate }
2387c478bd9Sstevel@tonic-gate
2397c478bd9Sstevel@tonic-gate static void
uu_release_child(void)2407c478bd9Sstevel@tonic-gate uu_release_child(void)
2417c478bd9Sstevel@tonic-gate {
2427c478bd9Sstevel@tonic-gate uu_panic_format = NULL;
2437c478bd9Sstevel@tonic-gate uu_panic_thread = 0;
2447c478bd9Sstevel@tonic-gate
2457c478bd9Sstevel@tonic-gate uu_release();
2467c478bd9Sstevel@tonic-gate }
2477c478bd9Sstevel@tonic-gate
2487c478bd9Sstevel@tonic-gate #pragma init(uu_init)
2497c478bd9Sstevel@tonic-gate static void
uu_init(void)2507c478bd9Sstevel@tonic-gate uu_init(void)
2517c478bd9Sstevel@tonic-gate {
2527c478bd9Sstevel@tonic-gate (void) pthread_atfork(uu_lockup, uu_release, uu_release_child);
2537c478bd9Sstevel@tonic-gate }
254*cb174861Sjoyce mcintosh
255*cb174861Sjoyce mcintosh /*
256*cb174861Sjoyce mcintosh * Dump a block of memory in hex+ascii, for debugging
257*cb174861Sjoyce mcintosh */
258*cb174861Sjoyce mcintosh void
uu_dump(FILE * out,const char * prefix,const void * buf,size_t len)259*cb174861Sjoyce mcintosh uu_dump(FILE *out, const char *prefix, const void *buf, size_t len)
260*cb174861Sjoyce mcintosh {
261*cb174861Sjoyce mcintosh const unsigned char *p = buf;
262*cb174861Sjoyce mcintosh int i;
263*cb174861Sjoyce mcintosh
264*cb174861Sjoyce mcintosh for (i = 0; i < len; i += 16) {
265*cb174861Sjoyce mcintosh int j;
266*cb174861Sjoyce mcintosh
267*cb174861Sjoyce mcintosh (void) fprintf(out, "%s", prefix);
268*cb174861Sjoyce mcintosh for (j = 0; j < 16 && i + j < len; j++) {
269*cb174861Sjoyce mcintosh (void) fprintf(out, "%2.2x ", p[i + j]);
270*cb174861Sjoyce mcintosh }
271*cb174861Sjoyce mcintosh for (; j < 16; j++) {
272*cb174861Sjoyce mcintosh (void) fprintf(out, " ");
273*cb174861Sjoyce mcintosh }
274*cb174861Sjoyce mcintosh for (j = 0; j < 16 && i + j < len; j++) {
275*cb174861Sjoyce mcintosh (void) fprintf(out, "%c",
276*cb174861Sjoyce mcintosh isprint(p[i + j]) ? p[i + j] : '.');
277*cb174861Sjoyce mcintosh }
278*cb174861Sjoyce mcintosh (void) fprintf(out, "\n");
279*cb174861Sjoyce mcintosh }
280*cb174861Sjoyce mcintosh }
281