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