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