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