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