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 (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 * Copyright 2013 Joyent, Inc. All rights reserved.
26 * Copyright 2017 OmniOS Community Edition (OmniOSce) Association.
27 */
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 #include <ucontext.h>
43
44 extern int ndebug;
45
46 static struct scf_error_info {
47 scf_error_t ei_code;
48 const char *ei_desc;
49 } scf_errors[] = {
50 {SCF_ERROR_NONE, "no error"},
51 {SCF_ERROR_NOT_BOUND, "handle not bound"},
52 {SCF_ERROR_NOT_SET, "cannot use unset argument"},
53 {SCF_ERROR_NOT_FOUND, "entity not found"},
54 {SCF_ERROR_TYPE_MISMATCH, "type does not match value"},
55 {SCF_ERROR_IN_USE, "cannot modify while in-use"},
56 {SCF_ERROR_CONNECTION_BROKEN, "connection to repository broken"},
57 {SCF_ERROR_INVALID_ARGUMENT, "invalid argument"},
58 {SCF_ERROR_NO_MEMORY, "no memory available"},
59 {SCF_ERROR_CONSTRAINT_VIOLATED, "required constraint not met"},
60 {SCF_ERROR_EXISTS, "object already exists"},
61 {SCF_ERROR_NO_SERVER, "repository server unavailable"},
62 {SCF_ERROR_NO_RESOURCES, "server has insufficient resources"},
63 {SCF_ERROR_PERMISSION_DENIED, "insufficient privileges for action"},
64 {SCF_ERROR_BACKEND_ACCESS, "backend refused access"},
65 {SCF_ERROR_BACKEND_READONLY, "backend is read-only"},
66 {SCF_ERROR_HANDLE_MISMATCH, "mismatched SCF handles"},
67 {SCF_ERROR_HANDLE_DESTROYED, "object bound to destroyed handle"},
68 {SCF_ERROR_VERSION_MISMATCH, "incompatible SCF version"},
69 {SCF_ERROR_DELETED, "object has been deleted"},
70 {SCF_ERROR_TEMPLATE_INVALID, "template data is invalid"},
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 scf_error_t _scf_fallback_error = SCF_ERROR_NONE;
84
85 static pthread_key_t scf_error_key = PTHREAD_ONCE_KEY_NP;
86
87 int
scf_setup_error(void)88 scf_setup_error(void)
89 {
90 return (pthread_key_create_once_np(&scf_error_key, NULL) == 0);
91 }
92
93 int
scf_set_error(scf_error_t code)94 scf_set_error(scf_error_t code)
95 {
96 assert(LOOKS_VALID(code));
97
98 if (scf_setup_error())
99 (void) pthread_setspecific(scf_error_key, (void *)code);
100 else
101 _scf_fallback_error = code;
102 return (-1);
103 }
104
105 scf_error_t
scf_error(void)106 scf_error(void)
107 {
108 scf_error_t ret;
109
110 ret = (scf_error_t)pthread_getspecific(scf_error_key);
111 if (ret == 0)
112 return (_scf_fallback_error);
113 assert(LOOKS_VALID(ret));
114 return (ret);
115 }
116
117 const char *
scf_strerror(scf_error_t code)118 scf_strerror(scf_error_t code)
119 {
120 struct scf_error_info *cur, *end;
121
122 cur = scf_errors;
123 end = cur + SCF_NUM_ERRORS;
124
125 for (; cur < end; cur++)
126 if (code == cur->ei_code)
127 return (dgettext(TEXT_DOMAIN, cur->ei_desc));
128
129 return (dgettext(TEXT_DOMAIN, "unknown error"));
130 }
131
132 const char *
scf_get_msg(scf_msg_t msg)133 scf_get_msg(scf_msg_t msg)
134 {
135 switch (msg) {
136 case SCF_MSG_ARGTOOLONG:
137 return (dgettext(TEXT_DOMAIN,
138 "Argument '%s' is too long, ignoring\n"));
139
140 case SCF_MSG_PATTERN_NOINSTANCE:
141 return (dgettext(TEXT_DOMAIN,
142 "Pattern '%s' doesn't match any instances\n"));
143
144 case SCF_MSG_PATTERN_NOINSTSVC:
145 return (dgettext(TEXT_DOMAIN,
146 "Pattern '%s' doesn't match any instances or services\n"));
147
148 case SCF_MSG_PATTERN_NOSERVICE:
149 return (dgettext(TEXT_DOMAIN,
150 "Pattern '%s' doesn't match any services\n"));
151
152 case SCF_MSG_PATTERN_NOENTITY:
153 return (dgettext(TEXT_DOMAIN,
154 "Pattern '%s' doesn't match any entities\n"));
155
156 case SCF_MSG_PATTERN_MULTIMATCH:
157 return (dgettext(TEXT_DOMAIN,
158 "Pattern '%s' matches multiple instances:\n"));
159
160 case SCF_MSG_PATTERN_POSSIBLE:
161 return (dgettext(TEXT_DOMAIN, " %s\n"));
162
163 case SCF_MSG_PATTERN_LEGACY:
164 return (dgettext(TEXT_DOMAIN,
165 "Operation not supported for legacy service '%s'\n"));
166
167 case SCF_MSG_PATTERN_MULTIPARTIAL:
168 return (dgettext(TEXT_DOMAIN,
169 "Partial FMRI '%s' matches multiple instances:\n"));
170
171 default:
172 abort();
173 /* NOTREACHED */
174 }
175
176 }
177