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*6812Sraf * Copyright 2008 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 #include <ucontext.h> 430Sstevel@tonic-gate 440Sstevel@tonic-gate extern int ndebug; 450Sstevel@tonic-gate 460Sstevel@tonic-gate static struct scf_error_info { 470Sstevel@tonic-gate scf_error_t ei_code; 480Sstevel@tonic-gate const char *ei_desc; 490Sstevel@tonic-gate } scf_errors[] = { 500Sstevel@tonic-gate {SCF_ERROR_NONE, "no error"}, 510Sstevel@tonic-gate {SCF_ERROR_NOT_BOUND, "handle not bound"}, 520Sstevel@tonic-gate {SCF_ERROR_NOT_SET, "cannot use unset argument"}, 530Sstevel@tonic-gate {SCF_ERROR_NOT_FOUND, "entity not found"}, 540Sstevel@tonic-gate {SCF_ERROR_TYPE_MISMATCH, "type does not match value"}, 550Sstevel@tonic-gate {SCF_ERROR_IN_USE, "cannot modify while in-use"}, 560Sstevel@tonic-gate {SCF_ERROR_CONNECTION_BROKEN, "connection to repository broken"}, 570Sstevel@tonic-gate {SCF_ERROR_INVALID_ARGUMENT, "invalid argument"}, 580Sstevel@tonic-gate {SCF_ERROR_NO_MEMORY, "no memory available"}, 590Sstevel@tonic-gate {SCF_ERROR_CONSTRAINT_VIOLATED, "required constraint not met"}, 600Sstevel@tonic-gate {SCF_ERROR_EXISTS, "object already exists"}, 610Sstevel@tonic-gate {SCF_ERROR_NO_SERVER, "repository server unavailable"}, 620Sstevel@tonic-gate {SCF_ERROR_NO_RESOURCES, "server has insufficient resources"}, 630Sstevel@tonic-gate {SCF_ERROR_PERMISSION_DENIED, "insufficient privileges for action"}, 640Sstevel@tonic-gate {SCF_ERROR_BACKEND_ACCESS, "backend refused access"}, 650Sstevel@tonic-gate {SCF_ERROR_BACKEND_READONLY, "backend is read-only"}, 660Sstevel@tonic-gate {SCF_ERROR_HANDLE_MISMATCH, "mismatched SCF handles"}, 670Sstevel@tonic-gate {SCF_ERROR_HANDLE_DESTROYED, "object bound to destroyed handle"}, 680Sstevel@tonic-gate {SCF_ERROR_VERSION_MISMATCH, "incompatible SCF version"}, 690Sstevel@tonic-gate {SCF_ERROR_DELETED, "object has been deleted"}, 700Sstevel@tonic-gate 710Sstevel@tonic-gate {SCF_ERROR_CALLBACK_FAILED, "user callback function failed"}, 720Sstevel@tonic-gate 730Sstevel@tonic-gate {SCF_ERROR_INTERNAL, "internal error"} 740Sstevel@tonic-gate }; 750Sstevel@tonic-gate #define SCF_NUM_ERRORS (sizeof (scf_errors) / sizeof (*scf_errors)) 760Sstevel@tonic-gate 770Sstevel@tonic-gate /* a SWAG just in case things get out of sync, we can notice */ 780Sstevel@tonic-gate #define LOOKS_VALID(e) \ 790Sstevel@tonic-gate ((e) >= scf_errors[0].ei_code && \ 800Sstevel@tonic-gate (e) < scf_errors[SCF_NUM_ERRORS - 1].ei_code + 10) 810Sstevel@tonic-gate 823864Sraf static scf_error_t _scf_fallback_error = SCF_ERROR_NONE; 830Sstevel@tonic-gate 843864Sraf #if defined(PTHREAD_ONCE_KEY_NP) 853864Sraf 863864Sraf static pthread_key_t scf_error_key = PTHREAD_ONCE_KEY_NP; 870Sstevel@tonic-gate 880Sstevel@tonic-gate int 890Sstevel@tonic-gate scf_setup_error(void) 900Sstevel@tonic-gate { 913864Sraf return (pthread_key_create_once_np(&scf_error_key, NULL) == 0); 923864Sraf } 933864Sraf 943864Sraf #else /* PTHREAD_ONCE_KEY_NP */ 953864Sraf 963864Sraf /* 973864Sraf * This old code is here to enable the building of a native version 983864Sraf * of libscf.so when the build machine has not yet been upgraded 993864Sraf * to a version of libc that provides pthread_key_create_once_np(). 1003864Sraf * It should be deleted when solaris_nevada ships. 1013864Sraf * This code is not MT-safe in a relaxed memory model. 1023864Sraf */ 1033864Sraf 1043864Sraf static pthread_key_t scf_error_key = 0; 1053864Sraf 1063864Sraf int 1073864Sraf scf_setup_error(void) 1083864Sraf { 1093864Sraf static pthread_mutex_t scf_key_lock = PTHREAD_MUTEX_INITIALIZER; 1103864Sraf static volatile int scf_error_key_setup = 0; 1113864Sraf 1120Sstevel@tonic-gate if (scf_error_key_setup == 0) { 1133864Sraf (void) pthread_mutex_lock(&scf_key_lock); 1143864Sraf if (scf_error_key_setup == 0) { 1153864Sraf if (pthread_key_create(&scf_error_key, NULL) == 0) 1163864Sraf scf_error_key_setup = 1; 1173864Sraf } 1183864Sraf (void) pthread_mutex_unlock(&scf_key_lock); 1190Sstevel@tonic-gate } 1200Sstevel@tonic-gate 1210Sstevel@tonic-gate return (scf_error_key_setup == 1); 1220Sstevel@tonic-gate } 1230Sstevel@tonic-gate 1243864Sraf #endif /* PTHREAD_ONCE_KEY_NP */ 1253864Sraf 1260Sstevel@tonic-gate int 1270Sstevel@tonic-gate scf_set_error(scf_error_t code) 1280Sstevel@tonic-gate { 1290Sstevel@tonic-gate assert(LOOKS_VALID(code)); 1300Sstevel@tonic-gate 1313864Sraf if (scf_setup_error()) 1320Sstevel@tonic-gate (void) pthread_setspecific(scf_error_key, (void *)code); 1330Sstevel@tonic-gate else 1340Sstevel@tonic-gate _scf_fallback_error = code; 1350Sstevel@tonic-gate return (-1); 1360Sstevel@tonic-gate } 1370Sstevel@tonic-gate 1380Sstevel@tonic-gate scf_error_t 1390Sstevel@tonic-gate scf_error(void) 1400Sstevel@tonic-gate { 1410Sstevel@tonic-gate scf_error_t ret; 1420Sstevel@tonic-gate 1430Sstevel@tonic-gate ret = (scf_error_t)pthread_getspecific(scf_error_key); 1440Sstevel@tonic-gate if (ret == 0) 1453864Sraf return (_scf_fallback_error); 1460Sstevel@tonic-gate assert(LOOKS_VALID(ret)); 1470Sstevel@tonic-gate return (ret); 1480Sstevel@tonic-gate } 1490Sstevel@tonic-gate 1500Sstevel@tonic-gate const char * 1510Sstevel@tonic-gate scf_strerror(scf_error_t code) 1520Sstevel@tonic-gate { 1530Sstevel@tonic-gate struct scf_error_info *cur, *end; 1540Sstevel@tonic-gate 1550Sstevel@tonic-gate cur = scf_errors; 1560Sstevel@tonic-gate end = cur + SCF_NUM_ERRORS; 1570Sstevel@tonic-gate 1580Sstevel@tonic-gate for (; cur < end; cur++) 1590Sstevel@tonic-gate if (code == cur->ei_code) 1600Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, cur->ei_desc)); 1610Sstevel@tonic-gate 1620Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, "unknown error")); 1630Sstevel@tonic-gate } 1640Sstevel@tonic-gate 1650Sstevel@tonic-gate const char * 1660Sstevel@tonic-gate scf_get_msg(scf_msg_t msg) 1670Sstevel@tonic-gate { 1680Sstevel@tonic-gate switch (msg) { 1690Sstevel@tonic-gate case SCF_MSG_ARGTOOLONG: 1700Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, 1710Sstevel@tonic-gate "Argument '%s' is too long, ignoring\n")); 1720Sstevel@tonic-gate 1730Sstevel@tonic-gate case SCF_MSG_PATTERN_NOINSTANCE: 1740Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, 1750Sstevel@tonic-gate "Pattern '%s' doesn't match any instances\n")); 1760Sstevel@tonic-gate 1770Sstevel@tonic-gate case SCF_MSG_PATTERN_NOINSTSVC: 1780Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, 1790Sstevel@tonic-gate "Pattern '%s' doesn't match any instances or services\n")); 1800Sstevel@tonic-gate 1810Sstevel@tonic-gate case SCF_MSG_PATTERN_NOSERVICE: 1820Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, 1830Sstevel@tonic-gate "Pattern '%s' doesn't match any services\n")); 1840Sstevel@tonic-gate 1850Sstevel@tonic-gate case SCF_MSG_PATTERN_NOENTITY: 1860Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, 1870Sstevel@tonic-gate "Pattern '%s' doesn't match any entities\n")); 1880Sstevel@tonic-gate 1890Sstevel@tonic-gate case SCF_MSG_PATTERN_MULTIMATCH: 1900Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, 1910Sstevel@tonic-gate "Pattern '%s' matches multiple instances:\n")); 1920Sstevel@tonic-gate 1930Sstevel@tonic-gate case SCF_MSG_PATTERN_POSSIBLE: 1940Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, " %s\n")); 1950Sstevel@tonic-gate 1960Sstevel@tonic-gate case SCF_MSG_PATTERN_LEGACY: 1970Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, 1980Sstevel@tonic-gate "Operation not supported for legacy service '%s'\n")); 1990Sstevel@tonic-gate 2000Sstevel@tonic-gate default: 2010Sstevel@tonic-gate abort(); 2020Sstevel@tonic-gate /* NOTREACHED */ 2030Sstevel@tonic-gate } 2040Sstevel@tonic-gate 2050Sstevel@tonic-gate } 206