xref: /illumos-gate/usr/src/lib/libscf/common/error.c (revision 1f6eb021)
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 /*
237257d1b4Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include "libscf_impl.h"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <assert.h>
307c478bd9Sstevel@tonic-gate #include <dlfcn.h>
317c478bd9Sstevel@tonic-gate #include <libintl.h>
327c478bd9Sstevel@tonic-gate #include <pthread.h>
337c478bd9Sstevel@tonic-gate #include <stdarg.h>
347c478bd9Sstevel@tonic-gate #include <stdio.h>
357c478bd9Sstevel@tonic-gate #include <stdlib.h>
367c478bd9Sstevel@tonic-gate #include <string.h>
377c478bd9Sstevel@tonic-gate #include <sys/machelf.h>
387c478bd9Sstevel@tonic-gate #include <thread.h>
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #include <ucontext.h>
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate extern int ndebug;
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate static struct scf_error_info {
457c478bd9Sstevel@tonic-gate 	scf_error_t	ei_code;
467c478bd9Sstevel@tonic-gate 	const char	*ei_desc;
477c478bd9Sstevel@tonic-gate } scf_errors[] = {
487c478bd9Sstevel@tonic-gate 	{SCF_ERROR_NONE,		"no error"},
497c478bd9Sstevel@tonic-gate 	{SCF_ERROR_NOT_BOUND,		"handle not bound"},
507c478bd9Sstevel@tonic-gate 	{SCF_ERROR_NOT_SET,		"cannot use unset argument"},
517c478bd9Sstevel@tonic-gate 	{SCF_ERROR_NOT_FOUND,		"entity not found"},
527c478bd9Sstevel@tonic-gate 	{SCF_ERROR_TYPE_MISMATCH,	"type does not match value"},
537c478bd9Sstevel@tonic-gate 	{SCF_ERROR_IN_USE,		"cannot modify while in-use"},
547c478bd9Sstevel@tonic-gate 	{SCF_ERROR_CONNECTION_BROKEN,	"connection to repository broken"},
557c478bd9Sstevel@tonic-gate 	{SCF_ERROR_INVALID_ARGUMENT,	"invalid argument"},
567c478bd9Sstevel@tonic-gate 	{SCF_ERROR_NO_MEMORY,		"no memory available"},
577c478bd9Sstevel@tonic-gate 	{SCF_ERROR_CONSTRAINT_VIOLATED,	"required constraint not met"},
587c478bd9Sstevel@tonic-gate 	{SCF_ERROR_EXISTS,		"object already exists"},
597c478bd9Sstevel@tonic-gate 	{SCF_ERROR_NO_SERVER,		"repository server unavailable"},
607c478bd9Sstevel@tonic-gate 	{SCF_ERROR_NO_RESOURCES,	"server has insufficient resources"},
617c478bd9Sstevel@tonic-gate 	{SCF_ERROR_PERMISSION_DENIED,	"insufficient privileges for action"},
627c478bd9Sstevel@tonic-gate 	{SCF_ERROR_BACKEND_ACCESS,	"backend refused access"},
637c478bd9Sstevel@tonic-gate 	{SCF_ERROR_BACKEND_READONLY,	"backend is read-only"},
647c478bd9Sstevel@tonic-gate 	{SCF_ERROR_HANDLE_MISMATCH,	"mismatched SCF handles"},
657c478bd9Sstevel@tonic-gate 	{SCF_ERROR_HANDLE_DESTROYED,	"object bound to destroyed handle"},
667c478bd9Sstevel@tonic-gate 	{SCF_ERROR_VERSION_MISMATCH,	"incompatible SCF version"},
677c478bd9Sstevel@tonic-gate 	{SCF_ERROR_DELETED,		"object has been deleted"},
68*1f6eb021SLiane Praza 	{SCF_ERROR_TEMPLATE_INVALID,	"template data is invalid"},
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate 	{SCF_ERROR_CALLBACK_FAILED,	"user callback function failed"},
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 	{SCF_ERROR_INTERNAL,		"internal error"}
737c478bd9Sstevel@tonic-gate };
747c478bd9Sstevel@tonic-gate #define	SCF_NUM_ERRORS	(sizeof (scf_errors) / sizeof (*scf_errors))
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate /* a SWAG just in case things get out of sync, we can notice */
777c478bd9Sstevel@tonic-gate #define	LOOKS_VALID(e)	\
787c478bd9Sstevel@tonic-gate 	((e) >= scf_errors[0].ei_code && \
797c478bd9Sstevel@tonic-gate 	    (e) < scf_errors[SCF_NUM_ERRORS - 1].ei_code + 10)
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate static scf_error_t	_scf_fallback_error = SCF_ERROR_NONE;
827c478bd9Sstevel@tonic-gate 
83cb620785Sraf #if defined(PTHREAD_ONCE_KEY_NP)
84cb620785Sraf 
85cb620785Sraf static pthread_key_t	scf_error_key = PTHREAD_ONCE_KEY_NP;
86cb620785Sraf 
877c478bd9Sstevel@tonic-gate int
887c478bd9Sstevel@tonic-gate scf_setup_error(void)
897c478bd9Sstevel@tonic-gate {
90cb620785Sraf 	return (pthread_key_create_once_np(&scf_error_key, NULL) == 0);
91cb620785Sraf }
92cb620785Sraf 
93cb620785Sraf #else	/* PTHREAD_ONCE_KEY_NP */
94cb620785Sraf 
95cb620785Sraf /*
96cb620785Sraf  * This old code is here to enable the building of a native version
97cb620785Sraf  * of libscf.so when the build machine has not yet been upgraded
98cb620785Sraf  * to a version of libc that provides pthread_key_create_once_np().
99cb620785Sraf  * It should be deleted when solaris_nevada ships.
100cb620785Sraf  * This code is not MT-safe in a relaxed memory model.
101cb620785Sraf  */
102cb620785Sraf 
103cb620785Sraf static pthread_key_t	scf_error_key = 0;
104cb620785Sraf 
105cb620785Sraf int
106cb620785Sraf scf_setup_error(void)
107cb620785Sraf {
108cb620785Sraf 	static pthread_mutex_t scf_key_lock = PTHREAD_MUTEX_INITIALIZER;
109cb620785Sraf 	static volatile int scf_error_key_setup = 0;
110cb620785Sraf 
1117c478bd9Sstevel@tonic-gate 	if (scf_error_key_setup == 0) {
112cb620785Sraf 		(void) pthread_mutex_lock(&scf_key_lock);
113cb620785Sraf 		if (scf_error_key_setup == 0) {
114cb620785Sraf 			if (pthread_key_create(&scf_error_key, NULL) == 0)
115cb620785Sraf 				scf_error_key_setup = 1;
116cb620785Sraf 		}
117cb620785Sraf 		(void) pthread_mutex_unlock(&scf_key_lock);
1187c478bd9Sstevel@tonic-gate 	}
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	return (scf_error_key_setup == 1);
1217c478bd9Sstevel@tonic-gate }
1227c478bd9Sstevel@tonic-gate 
123cb620785Sraf #endif	/* PTHREAD_ONCE_KEY_NP */
124cb620785Sraf 
1257c478bd9Sstevel@tonic-gate int
1267c478bd9Sstevel@tonic-gate scf_set_error(scf_error_t code)
1277c478bd9Sstevel@tonic-gate {
1287c478bd9Sstevel@tonic-gate 	assert(LOOKS_VALID(code));
1297c478bd9Sstevel@tonic-gate 
130cb620785Sraf 	if (scf_setup_error())
1317c478bd9Sstevel@tonic-gate 		(void) pthread_setspecific(scf_error_key, (void *)code);
1327c478bd9Sstevel@tonic-gate 	else
1337c478bd9Sstevel@tonic-gate 		_scf_fallback_error = code;
1347c478bd9Sstevel@tonic-gate 	return (-1);
1357c478bd9Sstevel@tonic-gate }
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate scf_error_t
1387c478bd9Sstevel@tonic-gate scf_error(void)
1397c478bd9Sstevel@tonic-gate {
1407c478bd9Sstevel@tonic-gate 	scf_error_t ret;
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	ret = (scf_error_t)pthread_getspecific(scf_error_key);
1437c478bd9Sstevel@tonic-gate 	if (ret == 0)
144cb620785Sraf 		return (_scf_fallback_error);
1457c478bd9Sstevel@tonic-gate 	assert(LOOKS_VALID(ret));
1467c478bd9Sstevel@tonic-gate 	return (ret);
1477c478bd9Sstevel@tonic-gate }
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate const char *
1507c478bd9Sstevel@tonic-gate scf_strerror(scf_error_t code)
1517c478bd9Sstevel@tonic-gate {
1527c478bd9Sstevel@tonic-gate 	struct scf_error_info *cur, *end;
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	cur = scf_errors;
1557c478bd9Sstevel@tonic-gate 	end = cur + SCF_NUM_ERRORS;
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 	for (; cur < end; cur++)
1587c478bd9Sstevel@tonic-gate 		if (code == cur->ei_code)
1597c478bd9Sstevel@tonic-gate 			return (dgettext(TEXT_DOMAIN, cur->ei_desc));
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	return (dgettext(TEXT_DOMAIN, "unknown error"));
1627c478bd9Sstevel@tonic-gate }
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate const char *
1657c478bd9Sstevel@tonic-gate scf_get_msg(scf_msg_t msg)
1667c478bd9Sstevel@tonic-gate {
1677c478bd9Sstevel@tonic-gate 	switch (msg) {
1687c478bd9Sstevel@tonic-gate 	case SCF_MSG_ARGTOOLONG:
1697c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
1707c478bd9Sstevel@tonic-gate 		    "Argument '%s' is too long, ignoring\n"));
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	case SCF_MSG_PATTERN_NOINSTANCE:
1737c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
1747c478bd9Sstevel@tonic-gate 		    "Pattern '%s' doesn't match any instances\n"));
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	case SCF_MSG_PATTERN_NOINSTSVC:
1777c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
1787c478bd9Sstevel@tonic-gate 		    "Pattern '%s' doesn't match any instances or services\n"));
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	case SCF_MSG_PATTERN_NOSERVICE:
1817c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
1827c478bd9Sstevel@tonic-gate 		    "Pattern '%s' doesn't match any services\n"));
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	case SCF_MSG_PATTERN_NOENTITY:
1857c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
1867c478bd9Sstevel@tonic-gate 		    "Pattern '%s' doesn't match any entities\n"));
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	case SCF_MSG_PATTERN_MULTIMATCH:
1897c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
1907c478bd9Sstevel@tonic-gate 		    "Pattern '%s' matches multiple instances:\n"));
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	case SCF_MSG_PATTERN_POSSIBLE:
1937c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "    %s\n"));
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	case SCF_MSG_PATTERN_LEGACY:
1967c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
1977c478bd9Sstevel@tonic-gate 		    "Operation not supported for legacy service '%s'\n"));
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	default:
2007c478bd9Sstevel@tonic-gate 		abort();
2017c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
2027c478bd9Sstevel@tonic-gate 	}
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate }
205