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 "libscf_impl.h" 300Sstevel@tonic-gate 310Sstevel@tonic-gate #include <assert.h> 320Sstevel@tonic-gate #include <dlfcn.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/machelf.h> 400Sstevel@tonic-gate #include <thread.h> 410Sstevel@tonic-gate 420Sstevel@tonic-gate #define walkcontext _walkcontext /* work around 4743525 */ 430Sstevel@tonic-gate #include <ucontext.h> 440Sstevel@tonic-gate 450Sstevel@tonic-gate extern int ndebug; 460Sstevel@tonic-gate 470Sstevel@tonic-gate static struct scf_error_info { 480Sstevel@tonic-gate scf_error_t ei_code; 490Sstevel@tonic-gate const char *ei_desc; 500Sstevel@tonic-gate } scf_errors[] = { 510Sstevel@tonic-gate {SCF_ERROR_NONE, "no error"}, 520Sstevel@tonic-gate {SCF_ERROR_NOT_BOUND, "handle not bound"}, 530Sstevel@tonic-gate {SCF_ERROR_NOT_SET, "cannot use unset argument"}, 540Sstevel@tonic-gate {SCF_ERROR_NOT_FOUND, "entity not found"}, 550Sstevel@tonic-gate {SCF_ERROR_TYPE_MISMATCH, "type does not match value"}, 560Sstevel@tonic-gate {SCF_ERROR_IN_USE, "cannot modify while in-use"}, 570Sstevel@tonic-gate {SCF_ERROR_CONNECTION_BROKEN, "connection to repository broken"}, 580Sstevel@tonic-gate {SCF_ERROR_INVALID_ARGUMENT, "invalid argument"}, 590Sstevel@tonic-gate {SCF_ERROR_NO_MEMORY, "no memory available"}, 600Sstevel@tonic-gate {SCF_ERROR_CONSTRAINT_VIOLATED, "required constraint not met"}, 610Sstevel@tonic-gate {SCF_ERROR_EXISTS, "object already exists"}, 620Sstevel@tonic-gate {SCF_ERROR_NO_SERVER, "repository server unavailable"}, 630Sstevel@tonic-gate {SCF_ERROR_NO_RESOURCES, "server has insufficient resources"}, 640Sstevel@tonic-gate {SCF_ERROR_PERMISSION_DENIED, "insufficient privileges for action"}, 650Sstevel@tonic-gate {SCF_ERROR_BACKEND_ACCESS, "backend refused access"}, 660Sstevel@tonic-gate {SCF_ERROR_BACKEND_READONLY, "backend is read-only"}, 670Sstevel@tonic-gate {SCF_ERROR_HANDLE_MISMATCH, "mismatched SCF handles"}, 680Sstevel@tonic-gate {SCF_ERROR_HANDLE_DESTROYED, "object bound to destroyed handle"}, 690Sstevel@tonic-gate {SCF_ERROR_VERSION_MISMATCH, "incompatible SCF version"}, 700Sstevel@tonic-gate {SCF_ERROR_DELETED, "object has been deleted"}, 710Sstevel@tonic-gate 720Sstevel@tonic-gate {SCF_ERROR_CALLBACK_FAILED, "user callback function failed"}, 730Sstevel@tonic-gate 740Sstevel@tonic-gate {SCF_ERROR_INTERNAL, "internal error"} 750Sstevel@tonic-gate }; 760Sstevel@tonic-gate #define SCF_NUM_ERRORS (sizeof (scf_errors) / sizeof (*scf_errors)) 770Sstevel@tonic-gate 780Sstevel@tonic-gate /* a SWAG just in case things get out of sync, we can notice */ 790Sstevel@tonic-gate #define LOOKS_VALID(e) \ 800Sstevel@tonic-gate ((e) >= scf_errors[0].ei_code && \ 810Sstevel@tonic-gate (e) < scf_errors[SCF_NUM_ERRORS - 1].ei_code + 10) 820Sstevel@tonic-gate 83*3864Sraf static scf_error_t _scf_fallback_error = SCF_ERROR_NONE; 840Sstevel@tonic-gate 85*3864Sraf #if defined(PTHREAD_ONCE_KEY_NP) 86*3864Sraf 87*3864Sraf static pthread_key_t scf_error_key = PTHREAD_ONCE_KEY_NP; 880Sstevel@tonic-gate 890Sstevel@tonic-gate int 900Sstevel@tonic-gate scf_setup_error(void) 910Sstevel@tonic-gate { 92*3864Sraf return (pthread_key_create_once_np(&scf_error_key, NULL) == 0); 93*3864Sraf } 94*3864Sraf 95*3864Sraf #else /* PTHREAD_ONCE_KEY_NP */ 96*3864Sraf 97*3864Sraf /* 98*3864Sraf * This old code is here to enable the building of a native version 99*3864Sraf * of libscf.so when the build machine has not yet been upgraded 100*3864Sraf * to a version of libc that provides pthread_key_create_once_np(). 101*3864Sraf * It should be deleted when solaris_nevada ships. 102*3864Sraf * This code is not MT-safe in a relaxed memory model. 103*3864Sraf */ 104*3864Sraf 105*3864Sraf static pthread_key_t scf_error_key = 0; 106*3864Sraf 107*3864Sraf int 108*3864Sraf scf_setup_error(void) 109*3864Sraf { 110*3864Sraf static pthread_mutex_t scf_key_lock = PTHREAD_MUTEX_INITIALIZER; 111*3864Sraf static volatile int scf_error_key_setup = 0; 112*3864Sraf 1130Sstevel@tonic-gate if (scf_error_key_setup == 0) { 114*3864Sraf (void) pthread_mutex_lock(&scf_key_lock); 115*3864Sraf if (scf_error_key_setup == 0) { 116*3864Sraf if (pthread_key_create(&scf_error_key, NULL) == 0) 117*3864Sraf scf_error_key_setup = 1; 118*3864Sraf } 119*3864Sraf (void) pthread_mutex_unlock(&scf_key_lock); 1200Sstevel@tonic-gate } 1210Sstevel@tonic-gate 1220Sstevel@tonic-gate return (scf_error_key_setup == 1); 1230Sstevel@tonic-gate } 1240Sstevel@tonic-gate 125*3864Sraf #endif /* PTHREAD_ONCE_KEY_NP */ 126*3864Sraf 1270Sstevel@tonic-gate int 1280Sstevel@tonic-gate scf_set_error(scf_error_t code) 1290Sstevel@tonic-gate { 1300Sstevel@tonic-gate assert(LOOKS_VALID(code)); 1310Sstevel@tonic-gate 132*3864Sraf if (scf_setup_error()) 1330Sstevel@tonic-gate (void) pthread_setspecific(scf_error_key, (void *)code); 1340Sstevel@tonic-gate else 1350Sstevel@tonic-gate _scf_fallback_error = code; 1360Sstevel@tonic-gate return (-1); 1370Sstevel@tonic-gate } 1380Sstevel@tonic-gate 1390Sstevel@tonic-gate scf_error_t 1400Sstevel@tonic-gate scf_error(void) 1410Sstevel@tonic-gate { 1420Sstevel@tonic-gate scf_error_t ret; 1430Sstevel@tonic-gate 1440Sstevel@tonic-gate ret = (scf_error_t)pthread_getspecific(scf_error_key); 1450Sstevel@tonic-gate if (ret == 0) 146*3864Sraf return (_scf_fallback_error); 1470Sstevel@tonic-gate assert(LOOKS_VALID(ret)); 1480Sstevel@tonic-gate return (ret); 1490Sstevel@tonic-gate } 1500Sstevel@tonic-gate 1510Sstevel@tonic-gate const char * 1520Sstevel@tonic-gate scf_strerror(scf_error_t code) 1530Sstevel@tonic-gate { 1540Sstevel@tonic-gate struct scf_error_info *cur, *end; 1550Sstevel@tonic-gate 1560Sstevel@tonic-gate cur = scf_errors; 1570Sstevel@tonic-gate end = cur + SCF_NUM_ERRORS; 1580Sstevel@tonic-gate 1590Sstevel@tonic-gate for (; cur < end; cur++) 1600Sstevel@tonic-gate if (code == cur->ei_code) 1610Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, cur->ei_desc)); 1620Sstevel@tonic-gate 1630Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, "unknown error")); 1640Sstevel@tonic-gate } 1650Sstevel@tonic-gate 1660Sstevel@tonic-gate const char * 1670Sstevel@tonic-gate scf_get_msg(scf_msg_t msg) 1680Sstevel@tonic-gate { 1690Sstevel@tonic-gate switch (msg) { 1700Sstevel@tonic-gate case SCF_MSG_ARGTOOLONG: 1710Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, 1720Sstevel@tonic-gate "Argument '%s' is too long, ignoring\n")); 1730Sstevel@tonic-gate 1740Sstevel@tonic-gate case SCF_MSG_PATTERN_NOINSTANCE: 1750Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, 1760Sstevel@tonic-gate "Pattern '%s' doesn't match any instances\n")); 1770Sstevel@tonic-gate 1780Sstevel@tonic-gate case SCF_MSG_PATTERN_NOINSTSVC: 1790Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, 1800Sstevel@tonic-gate "Pattern '%s' doesn't match any instances or services\n")); 1810Sstevel@tonic-gate 1820Sstevel@tonic-gate case SCF_MSG_PATTERN_NOSERVICE: 1830Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, 1840Sstevel@tonic-gate "Pattern '%s' doesn't match any services\n")); 1850Sstevel@tonic-gate 1860Sstevel@tonic-gate case SCF_MSG_PATTERN_NOENTITY: 1870Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, 1880Sstevel@tonic-gate "Pattern '%s' doesn't match any entities\n")); 1890Sstevel@tonic-gate 1900Sstevel@tonic-gate case SCF_MSG_PATTERN_MULTIMATCH: 1910Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, 1920Sstevel@tonic-gate "Pattern '%s' matches multiple instances:\n")); 1930Sstevel@tonic-gate 1940Sstevel@tonic-gate case SCF_MSG_PATTERN_POSSIBLE: 1950Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, " %s\n")); 1960Sstevel@tonic-gate 1970Sstevel@tonic-gate case SCF_MSG_PATTERN_LEGACY: 1980Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, 1990Sstevel@tonic-gate "Operation not supported for legacy service '%s'\n")); 2000Sstevel@tonic-gate 2010Sstevel@tonic-gate default: 2020Sstevel@tonic-gate abort(); 2030Sstevel@tonic-gate /* NOTREACHED */ 2040Sstevel@tonic-gate } 2050Sstevel@tonic-gate 2060Sstevel@tonic-gate } 207