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