xref: /illumos-gate/usr/src/lib/libscf/common/error.c (revision 7c478bd9)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include "libscf_impl.h"
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #include <assert.h>
32*7c478bd9Sstevel@tonic-gate #include <dlfcn.h>
33*7c478bd9Sstevel@tonic-gate #include <libintl.h>
34*7c478bd9Sstevel@tonic-gate #include <pthread.h>
35*7c478bd9Sstevel@tonic-gate #include <stdarg.h>
36*7c478bd9Sstevel@tonic-gate #include <stdio.h>
37*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
38*7c478bd9Sstevel@tonic-gate #include <string.h>
39*7c478bd9Sstevel@tonic-gate #include <sys/machelf.h>
40*7c478bd9Sstevel@tonic-gate #include <thread.h>
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate #define	walkcontext	_walkcontext		/* work around 4743525 */
43*7c478bd9Sstevel@tonic-gate #include <ucontext.h>
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate extern int ndebug;
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate static struct scf_error_info {
48*7c478bd9Sstevel@tonic-gate 	scf_error_t	ei_code;
49*7c478bd9Sstevel@tonic-gate 	const char	*ei_desc;
50*7c478bd9Sstevel@tonic-gate } scf_errors[] = {
51*7c478bd9Sstevel@tonic-gate 	{SCF_ERROR_NONE,		"no error"},
52*7c478bd9Sstevel@tonic-gate 	{SCF_ERROR_NOT_BOUND,		"handle not bound"},
53*7c478bd9Sstevel@tonic-gate 	{SCF_ERROR_NOT_SET,		"cannot use unset argument"},
54*7c478bd9Sstevel@tonic-gate 	{SCF_ERROR_NOT_FOUND,		"entity not found"},
55*7c478bd9Sstevel@tonic-gate 	{SCF_ERROR_TYPE_MISMATCH,	"type does not match value"},
56*7c478bd9Sstevel@tonic-gate 	{SCF_ERROR_IN_USE,		"cannot modify while in-use"},
57*7c478bd9Sstevel@tonic-gate 	{SCF_ERROR_CONNECTION_BROKEN,	"connection to repository broken"},
58*7c478bd9Sstevel@tonic-gate 	{SCF_ERROR_INVALID_ARGUMENT,	"invalid argument"},
59*7c478bd9Sstevel@tonic-gate 	{SCF_ERROR_NO_MEMORY,		"no memory available"},
60*7c478bd9Sstevel@tonic-gate 	{SCF_ERROR_CONSTRAINT_VIOLATED,	"required constraint not met"},
61*7c478bd9Sstevel@tonic-gate 	{SCF_ERROR_EXISTS,		"object already exists"},
62*7c478bd9Sstevel@tonic-gate 	{SCF_ERROR_NO_SERVER,		"repository server unavailable"},
63*7c478bd9Sstevel@tonic-gate 	{SCF_ERROR_NO_RESOURCES,	"server has insufficient resources"},
64*7c478bd9Sstevel@tonic-gate 	{SCF_ERROR_PERMISSION_DENIED,	"insufficient privileges for action"},
65*7c478bd9Sstevel@tonic-gate 	{SCF_ERROR_BACKEND_ACCESS,	"backend refused access"},
66*7c478bd9Sstevel@tonic-gate 	{SCF_ERROR_BACKEND_READONLY,	"backend is read-only"},
67*7c478bd9Sstevel@tonic-gate 	{SCF_ERROR_HANDLE_MISMATCH,	"mismatched SCF handles"},
68*7c478bd9Sstevel@tonic-gate 	{SCF_ERROR_HANDLE_DESTROYED,	"object bound to destroyed handle"},
69*7c478bd9Sstevel@tonic-gate 	{SCF_ERROR_VERSION_MISMATCH,	"incompatible SCF version"},
70*7c478bd9Sstevel@tonic-gate 	{SCF_ERROR_DELETED,		"object has been deleted"},
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate 	{SCF_ERROR_CALLBACK_FAILED,	"user callback function failed"},
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate 	{SCF_ERROR_INTERNAL,		"internal error"}
75*7c478bd9Sstevel@tonic-gate };
76*7c478bd9Sstevel@tonic-gate #define	SCF_NUM_ERRORS	(sizeof (scf_errors) / sizeof (*scf_errors))
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate /* a SWAG just in case things get out of sync, we can notice */
79*7c478bd9Sstevel@tonic-gate #define	LOOKS_VALID(e)	\
80*7c478bd9Sstevel@tonic-gate 	((e) >= scf_errors[0].ei_code && \
81*7c478bd9Sstevel@tonic-gate 	    (e) < scf_errors[SCF_NUM_ERRORS - 1].ei_code + 10)
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate static pthread_mutex_t	scf_key_lock = PTHREAD_MUTEX_INITIALIZER;
84*7c478bd9Sstevel@tonic-gate static pthread_key_t	scf_error_key;
85*7c478bd9Sstevel@tonic-gate static volatile int	scf_error_key_setup;
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate static scf_error_t	_scf_fallback_error = SCF_ERROR_NONE;
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate int
90*7c478bd9Sstevel@tonic-gate scf_setup_error(void)
91*7c478bd9Sstevel@tonic-gate {
92*7c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&scf_key_lock);
93*7c478bd9Sstevel@tonic-gate 	if (scf_error_key_setup == 0) {
94*7c478bd9Sstevel@tonic-gate 		if (pthread_key_create(&scf_error_key, NULL) != 0)
95*7c478bd9Sstevel@tonic-gate 			scf_error_key_setup = -1;
96*7c478bd9Sstevel@tonic-gate 		else
97*7c478bd9Sstevel@tonic-gate 			scf_error_key_setup = 1;
98*7c478bd9Sstevel@tonic-gate 	}
99*7c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&scf_key_lock);
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate 	return (scf_error_key_setup == 1);
102*7c478bd9Sstevel@tonic-gate }
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate int
105*7c478bd9Sstevel@tonic-gate scf_set_error(scf_error_t code)
106*7c478bd9Sstevel@tonic-gate {
107*7c478bd9Sstevel@tonic-gate 	assert(LOOKS_VALID(code));
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate 	if (scf_error_key_setup == 0)
110*7c478bd9Sstevel@tonic-gate 		(void) scf_setup_error();
111*7c478bd9Sstevel@tonic-gate 
112*7c478bd9Sstevel@tonic-gate 	if (scf_error_key_setup > 0)
113*7c478bd9Sstevel@tonic-gate 		(void) pthread_setspecific(scf_error_key, (void *)code);
114*7c478bd9Sstevel@tonic-gate 	else
115*7c478bd9Sstevel@tonic-gate 		_scf_fallback_error = code;
116*7c478bd9Sstevel@tonic-gate 	return (-1);
117*7c478bd9Sstevel@tonic-gate }
118*7c478bd9Sstevel@tonic-gate 
119*7c478bd9Sstevel@tonic-gate scf_error_t
120*7c478bd9Sstevel@tonic-gate scf_error(void)
121*7c478bd9Sstevel@tonic-gate {
122*7c478bd9Sstevel@tonic-gate 	scf_error_t ret;
123*7c478bd9Sstevel@tonic-gate 
124*7c478bd9Sstevel@tonic-gate 	if (scf_error_key_setup < 0)
125*7c478bd9Sstevel@tonic-gate 		return (_scf_fallback_error);
126*7c478bd9Sstevel@tonic-gate 
127*7c478bd9Sstevel@tonic-gate 	if (scf_error_key_setup == 0)
128*7c478bd9Sstevel@tonic-gate 		return (SCF_ERROR_NONE);
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate 	ret = (scf_error_t)pthread_getspecific(scf_error_key);
131*7c478bd9Sstevel@tonic-gate 	if (ret == 0)
132*7c478bd9Sstevel@tonic-gate 		return (SCF_ERROR_NONE);
133*7c478bd9Sstevel@tonic-gate 	assert(LOOKS_VALID(ret));
134*7c478bd9Sstevel@tonic-gate 	return (ret);
135*7c478bd9Sstevel@tonic-gate }
136*7c478bd9Sstevel@tonic-gate 
137*7c478bd9Sstevel@tonic-gate const char *
138*7c478bd9Sstevel@tonic-gate scf_strerror(scf_error_t code)
139*7c478bd9Sstevel@tonic-gate {
140*7c478bd9Sstevel@tonic-gate 	struct scf_error_info *cur, *end;
141*7c478bd9Sstevel@tonic-gate 
142*7c478bd9Sstevel@tonic-gate 	cur = scf_errors;
143*7c478bd9Sstevel@tonic-gate 	end = cur + SCF_NUM_ERRORS;
144*7c478bd9Sstevel@tonic-gate 
145*7c478bd9Sstevel@tonic-gate 	for (; cur < end; cur++)
146*7c478bd9Sstevel@tonic-gate 		if (code == cur->ei_code)
147*7c478bd9Sstevel@tonic-gate 			return (dgettext(TEXT_DOMAIN, cur->ei_desc));
148*7c478bd9Sstevel@tonic-gate 
149*7c478bd9Sstevel@tonic-gate 	return (dgettext(TEXT_DOMAIN, "unknown error"));
150*7c478bd9Sstevel@tonic-gate }
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate const char *
153*7c478bd9Sstevel@tonic-gate scf_get_msg(scf_msg_t msg)
154*7c478bd9Sstevel@tonic-gate {
155*7c478bd9Sstevel@tonic-gate 	switch (msg) {
156*7c478bd9Sstevel@tonic-gate 	case SCF_MSG_ARGTOOLONG:
157*7c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
158*7c478bd9Sstevel@tonic-gate 		    "Argument '%s' is too long, ignoring\n"));
159*7c478bd9Sstevel@tonic-gate 
160*7c478bd9Sstevel@tonic-gate 	case SCF_MSG_PATTERN_NOINSTANCE:
161*7c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
162*7c478bd9Sstevel@tonic-gate 		    "Pattern '%s' doesn't match any instances\n"));
163*7c478bd9Sstevel@tonic-gate 
164*7c478bd9Sstevel@tonic-gate 	case SCF_MSG_PATTERN_NOINSTSVC:
165*7c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
166*7c478bd9Sstevel@tonic-gate 		    "Pattern '%s' doesn't match any instances or services\n"));
167*7c478bd9Sstevel@tonic-gate 
168*7c478bd9Sstevel@tonic-gate 	case SCF_MSG_PATTERN_NOSERVICE:
169*7c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
170*7c478bd9Sstevel@tonic-gate 		    "Pattern '%s' doesn't match any services\n"));
171*7c478bd9Sstevel@tonic-gate 
172*7c478bd9Sstevel@tonic-gate 	case SCF_MSG_PATTERN_NOENTITY:
173*7c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
174*7c478bd9Sstevel@tonic-gate 		    "Pattern '%s' doesn't match any entities\n"));
175*7c478bd9Sstevel@tonic-gate 
176*7c478bd9Sstevel@tonic-gate 	case SCF_MSG_PATTERN_MULTIMATCH:
177*7c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
178*7c478bd9Sstevel@tonic-gate 		    "Pattern '%s' matches multiple instances:\n"));
179*7c478bd9Sstevel@tonic-gate 
180*7c478bd9Sstevel@tonic-gate 	case SCF_MSG_PATTERN_POSSIBLE:
181*7c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "    %s\n"));
182*7c478bd9Sstevel@tonic-gate 
183*7c478bd9Sstevel@tonic-gate 	case SCF_MSG_PATTERN_LEGACY:
184*7c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
185*7c478bd9Sstevel@tonic-gate 		    "Operation not supported for legacy service '%s'\n"));
186*7c478bd9Sstevel@tonic-gate 
187*7c478bd9Sstevel@tonic-gate 	default:
188*7c478bd9Sstevel@tonic-gate 		abort();
189*7c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
190*7c478bd9Sstevel@tonic-gate 	}
191*7c478bd9Sstevel@tonic-gate 
192*7c478bd9Sstevel@tonic-gate }
193