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 5*3864Sraf * Common Development and Distribution License (the "License"). 6*3864Sraf * 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 */ 21*3864Sraf 220Sstevel@tonic-gate /* 23*3864Sraf * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 280Sstevel@tonic-gate 290Sstevel@tonic-gate #include "libuutil_common.h" 300Sstevel@tonic-gate 310Sstevel@tonic-gate #include <assert.h> 320Sstevel@tonic-gate #include <errno.h> 330Sstevel@tonic-gate #include <libintl.h> 340Sstevel@tonic-gate #include <pthread.h> 350Sstevel@tonic-gate #include <stdarg.h> 360Sstevel@tonic-gate #include <stdio.h> 370Sstevel@tonic-gate #include <stdlib.h> 380Sstevel@tonic-gate #include <string.h> 390Sstevel@tonic-gate #include <sys/debug.h> 400Sstevel@tonic-gate #include <thread.h> 410Sstevel@tonic-gate #include <unistd.h> 420Sstevel@tonic-gate 430Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) 440Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" 450Sstevel@tonic-gate #endif 460Sstevel@tonic-gate 47*3864Sraf /* 48*3864Sraf * All of the old code under !defined(PTHREAD_ONCE_KEY_NP) 49*3864Sraf * is here to enable the building of a native version of 50*3864Sraf * libuutil.so when the build machine has not yet been upgraded 51*3864Sraf * to a version of libc that provides pthread_key_create_once_np(). 52*3864Sraf * It should all be deleted when solaris_nevada ships. 53*3864Sraf * The code is not MT-safe in a relaxed memory model. 54*3864Sraf */ 55*3864Sraf 56*3864Sraf #if defined(PTHREAD_ONCE_KEY_NP) 57*3864Sraf static pthread_key_t uu_error_key = PTHREAD_ONCE_KEY_NP; 58*3864Sraf #else /* PTHREAD_ONCE_KEY_NP */ 59*3864Sraf static pthread_key_t uu_error_key = 0; 600Sstevel@tonic-gate static pthread_mutex_t uu_key_lock = PTHREAD_MUTEX_INITIALIZER; 61*3864Sraf #endif /* PTHREAD_ONCE_KEY_NP */ 62*3864Sraf 63*3864Sraf static int uu_error_key_setup = 0; 640Sstevel@tonic-gate 650Sstevel@tonic-gate static pthread_mutex_t uu_panic_lock = PTHREAD_MUTEX_INITIALIZER; 660Sstevel@tonic-gate /* LINTED static unused */ 670Sstevel@tonic-gate static const char *uu_panic_format; 680Sstevel@tonic-gate /* LINTED static unused */ 690Sstevel@tonic-gate static va_list uu_panic_args; 700Sstevel@tonic-gate static pthread_t uu_panic_thread; 710Sstevel@tonic-gate 720Sstevel@tonic-gate static uint32_t _uu_main_error; 730Sstevel@tonic-gate 740Sstevel@tonic-gate void 750Sstevel@tonic-gate uu_set_error(uint_t code) 760Sstevel@tonic-gate { 770Sstevel@tonic-gate if (thr_main() != 0) { 780Sstevel@tonic-gate _uu_main_error = code; 790Sstevel@tonic-gate return; 800Sstevel@tonic-gate } 81*3864Sraf #if defined(PTHREAD_ONCE_KEY_NP) 82*3864Sraf if (pthread_key_create_once_np(&uu_error_key, NULL) != 0) 83*3864Sraf uu_error_key_setup = -1; 84*3864Sraf else 85*3864Sraf uu_error_key_setup = 1; 86*3864Sraf #else /* PTHREAD_ONCE_KEY_NP */ 870Sstevel@tonic-gate if (uu_error_key_setup == 0) { 880Sstevel@tonic-gate (void) pthread_mutex_lock(&uu_key_lock); 890Sstevel@tonic-gate if (uu_error_key_setup == 0) { 90*3864Sraf if (pthread_key_create(&uu_error_key, NULL) != 0) 910Sstevel@tonic-gate uu_error_key_setup = -1; 920Sstevel@tonic-gate else 930Sstevel@tonic-gate uu_error_key_setup = 1; 940Sstevel@tonic-gate } 950Sstevel@tonic-gate (void) pthread_mutex_unlock(&uu_key_lock); 960Sstevel@tonic-gate } 97*3864Sraf #endif /* PTHREAD_ONCE_KEY_NP */ 980Sstevel@tonic-gate if (uu_error_key_setup > 0) 990Sstevel@tonic-gate (void) pthread_setspecific(uu_error_key, 1000Sstevel@tonic-gate (void *)(uintptr_t)code); 1010Sstevel@tonic-gate } 1020Sstevel@tonic-gate 1030Sstevel@tonic-gate uint32_t 1040Sstevel@tonic-gate uu_error(void) 1050Sstevel@tonic-gate { 1060Sstevel@tonic-gate if (thr_main() != 0) 1070Sstevel@tonic-gate return (_uu_main_error); 1080Sstevel@tonic-gate 109*3864Sraf if (uu_error_key_setup < 0) /* can't happen? */ 1100Sstevel@tonic-gate return (UU_ERROR_UNKNOWN); 111*3864Sraf 112*3864Sraf /* 113*3864Sraf * Because UU_ERROR_NONE == 0, if uu_set_error() was 114*3864Sraf * never called, then this will return UU_ERROR_NONE: 115*3864Sraf */ 116*3864Sraf return ((uint32_t)(uintptr_t)pthread_getspecific(uu_error_key)); 1170Sstevel@tonic-gate } 1180Sstevel@tonic-gate 1190Sstevel@tonic-gate const char * 1200Sstevel@tonic-gate uu_strerror(uint32_t code) 1210Sstevel@tonic-gate { 1220Sstevel@tonic-gate const char *str; 1230Sstevel@tonic-gate 1240Sstevel@tonic-gate switch (code) { 1250Sstevel@tonic-gate case UU_ERROR_NONE: 1260Sstevel@tonic-gate str = dgettext(TEXT_DOMAIN, "No error"); 1270Sstevel@tonic-gate break; 1280Sstevel@tonic-gate 1290Sstevel@tonic-gate case UU_ERROR_INVALID_ARGUMENT: 1300Sstevel@tonic-gate str = dgettext(TEXT_DOMAIN, "Invalid argument"); 1310Sstevel@tonic-gate break; 1320Sstevel@tonic-gate 1330Sstevel@tonic-gate case UU_ERROR_UNKNOWN_FLAG: 1340Sstevel@tonic-gate str = dgettext(TEXT_DOMAIN, "Unknown flag passed"); 1350Sstevel@tonic-gate break; 1360Sstevel@tonic-gate 1370Sstevel@tonic-gate case UU_ERROR_NO_MEMORY: 1380Sstevel@tonic-gate str = dgettext(TEXT_DOMAIN, "Out of memory"); 1390Sstevel@tonic-gate break; 1400Sstevel@tonic-gate 1410Sstevel@tonic-gate case UU_ERROR_CALLBACK_FAILED: 1420Sstevel@tonic-gate str = dgettext(TEXT_DOMAIN, "Callback-initiated failure"); 1430Sstevel@tonic-gate break; 1440Sstevel@tonic-gate 1450Sstevel@tonic-gate case UU_ERROR_NOT_SUPPORTED: 1460Sstevel@tonic-gate str = dgettext(TEXT_DOMAIN, "Operation not supported"); 1470Sstevel@tonic-gate break; 1480Sstevel@tonic-gate 1490Sstevel@tonic-gate case UU_ERROR_EMPTY: 1500Sstevel@tonic-gate str = dgettext(TEXT_DOMAIN, "No value provided"); 1510Sstevel@tonic-gate break; 1520Sstevel@tonic-gate 1530Sstevel@tonic-gate case UU_ERROR_UNDERFLOW: 1540Sstevel@tonic-gate str = dgettext(TEXT_DOMAIN, "Value too small"); 1550Sstevel@tonic-gate break; 1560Sstevel@tonic-gate 1570Sstevel@tonic-gate case UU_ERROR_OVERFLOW: 1580Sstevel@tonic-gate str = dgettext(TEXT_DOMAIN, "Value too large"); 1590Sstevel@tonic-gate break; 1600Sstevel@tonic-gate 1610Sstevel@tonic-gate case UU_ERROR_INVALID_CHAR: 1620Sstevel@tonic-gate str = dgettext(TEXT_DOMAIN, 1630Sstevel@tonic-gate "Value contains unexpected character"); 1640Sstevel@tonic-gate break; 1650Sstevel@tonic-gate 1660Sstevel@tonic-gate case UU_ERROR_INVALID_DIGIT: 1670Sstevel@tonic-gate str = dgettext(TEXT_DOMAIN, 1680Sstevel@tonic-gate "Value contains digit not in base"); 1690Sstevel@tonic-gate break; 1700Sstevel@tonic-gate 1710Sstevel@tonic-gate case UU_ERROR_SYSTEM: 1720Sstevel@tonic-gate str = dgettext(TEXT_DOMAIN, "Underlying system error"); 1730Sstevel@tonic-gate break; 1740Sstevel@tonic-gate 1750Sstevel@tonic-gate case UU_ERROR_UNKNOWN: 1760Sstevel@tonic-gate str = dgettext(TEXT_DOMAIN, "Error status not known"); 1770Sstevel@tonic-gate break; 1780Sstevel@tonic-gate 1790Sstevel@tonic-gate default: 1800Sstevel@tonic-gate errno = ESRCH; 1810Sstevel@tonic-gate str = NULL; 1820Sstevel@tonic-gate break; 1830Sstevel@tonic-gate } 1840Sstevel@tonic-gate return (str); 1850Sstevel@tonic-gate } 1860Sstevel@tonic-gate 1870Sstevel@tonic-gate void 1880Sstevel@tonic-gate uu_panic(const char *format, ...) 1890Sstevel@tonic-gate { 1900Sstevel@tonic-gate va_list args; 1910Sstevel@tonic-gate 1920Sstevel@tonic-gate va_start(args, format); 1930Sstevel@tonic-gate 1940Sstevel@tonic-gate (void) pthread_mutex_lock(&uu_panic_lock); 1950Sstevel@tonic-gate if (uu_panic_thread == 0) { 1960Sstevel@tonic-gate uu_panic_thread = pthread_self(); 1970Sstevel@tonic-gate uu_panic_format = format; 1980Sstevel@tonic-gate va_copy(uu_panic_args, args); 1990Sstevel@tonic-gate } 2000Sstevel@tonic-gate (void) pthread_mutex_unlock(&uu_panic_lock); 2010Sstevel@tonic-gate 2020Sstevel@tonic-gate (void) vfprintf(stderr, format, args); 2030Sstevel@tonic-gate 2040Sstevel@tonic-gate if (uu_panic_thread == pthread_self()) 2050Sstevel@tonic-gate abort(); 2060Sstevel@tonic-gate else 2070Sstevel@tonic-gate for (;;) 2080Sstevel@tonic-gate (void) pause(); 2090Sstevel@tonic-gate } 2100Sstevel@tonic-gate 2110Sstevel@tonic-gate int 2120Sstevel@tonic-gate assfail(const char *astring, const char *file, int line) 2130Sstevel@tonic-gate { 2140Sstevel@tonic-gate __assert(astring, file, line); 2150Sstevel@tonic-gate /*NOTREACHED*/ 2160Sstevel@tonic-gate return (0); 2170Sstevel@tonic-gate } 2180Sstevel@tonic-gate 2190Sstevel@tonic-gate static void 2200Sstevel@tonic-gate uu_lockup(void) 2210Sstevel@tonic-gate { 2220Sstevel@tonic-gate (void) pthread_mutex_lock(&uu_panic_lock); 223*3864Sraf #if !defined(PTHREAD_ONCE_KEY_NP) 2240Sstevel@tonic-gate (void) pthread_mutex_lock(&uu_key_lock); 225*3864Sraf #endif 2260Sstevel@tonic-gate uu_avl_lockup(); 2270Sstevel@tonic-gate uu_list_lockup(); 2280Sstevel@tonic-gate } 2290Sstevel@tonic-gate 2300Sstevel@tonic-gate static void 2310Sstevel@tonic-gate uu_release(void) 2320Sstevel@tonic-gate { 2330Sstevel@tonic-gate (void) pthread_mutex_unlock(&uu_panic_lock); 234*3864Sraf #if !defined(PTHREAD_ONCE_KEY_NP) 2350Sstevel@tonic-gate (void) pthread_mutex_unlock(&uu_key_lock); 236*3864Sraf #endif 2370Sstevel@tonic-gate uu_avl_release(); 2380Sstevel@tonic-gate uu_list_release(); 2390Sstevel@tonic-gate } 2400Sstevel@tonic-gate 2410Sstevel@tonic-gate static void 2420Sstevel@tonic-gate uu_release_child(void) 2430Sstevel@tonic-gate { 2440Sstevel@tonic-gate uu_panic_format = NULL; 2450Sstevel@tonic-gate uu_panic_thread = 0; 2460Sstevel@tonic-gate 2470Sstevel@tonic-gate uu_release(); 2480Sstevel@tonic-gate } 2490Sstevel@tonic-gate 2500Sstevel@tonic-gate #pragma init(uu_init) 2510Sstevel@tonic-gate static void 2520Sstevel@tonic-gate uu_init(void) 2530Sstevel@tonic-gate { 2540Sstevel@tonic-gate (void) pthread_atfork(uu_lockup, uu_release, uu_release_child); 2550Sstevel@tonic-gate } 256