17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
5cb62078raf * Common Development and Distribution License (the "License").
6cb62078raf * You may not use this file except in compliance with the License.
77c478bdstevel@tonic-gate *
87c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bdstevel@tonic-gate * See the License for the specific language governing permissions
117c478bdstevel@tonic-gate * and limitations under the License.
127c478bdstevel@tonic-gate *
137c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bdstevel@tonic-gate *
197c478bdstevel@tonic-gate * CDDL HEADER END
207c478bdstevel@tonic-gate */
21cb62078raf
227c478bdstevel@tonic-gate/*
237257d1braf * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bdstevel@tonic-gate * Use is subject to license terms.
256c7c876Jerry Jelinek * Copyright 2013 Joyent, Inc.  All rights reserved.
26ad3ad82Andy Fiddaman * Copyright 2017 OmniOS Community Edition (OmniOSce) Association.
277c478bdstevel@tonic-gate */
287c478bdstevel@tonic-gate
297c478bdstevel@tonic-gate#include "libscf_impl.h"
307c478bdstevel@tonic-gate
317c478bdstevel@tonic-gate#include <assert.h>
327c478bdstevel@tonic-gate#include <dlfcn.h>
337c478bdstevel@tonic-gate#include <libintl.h>
347c478bdstevel@tonic-gate#include <pthread.h>
357c478bdstevel@tonic-gate#include <stdarg.h>
367c478bdstevel@tonic-gate#include <stdio.h>
377c478bdstevel@tonic-gate#include <stdlib.h>
387c478bdstevel@tonic-gate#include <string.h>
397c478bdstevel@tonic-gate#include <sys/machelf.h>
407c478bdstevel@tonic-gate#include <thread.h>
417c478bdstevel@tonic-gate
427c478bdstevel@tonic-gate#include <ucontext.h>
437c478bdstevel@tonic-gate
447c478bdstevel@tonic-gateextern int ndebug;
457c478bdstevel@tonic-gate
467c478bdstevel@tonic-gatestatic struct scf_error_info {
477c478bdstevel@tonic-gate	scf_error_t	ei_code;
487c478bdstevel@tonic-gate	const char	*ei_desc;
497c478bdstevel@tonic-gate} scf_errors[] = {
507c478bdstevel@tonic-gate	{SCF_ERROR_NONE,		"no error"},
517c478bdstevel@tonic-gate	{SCF_ERROR_NOT_BOUND,		"handle not bound"},
527c478bdstevel@tonic-gate	{SCF_ERROR_NOT_SET,		"cannot use unset argument"},
537c478bdstevel@tonic-gate	{SCF_ERROR_NOT_FOUND,		"entity not found"},
547c478bdstevel@tonic-gate	{SCF_ERROR_TYPE_MISMATCH,	"type does not match value"},
557c478bdstevel@tonic-gate	{SCF_ERROR_IN_USE,		"cannot modify while in-use"},
567c478bdstevel@tonic-gate	{SCF_ERROR_CONNECTION_BROKEN,	"connection to repository broken"},
577c478bdstevel@tonic-gate	{SCF_ERROR_INVALID_ARGUMENT,	"invalid argument"},
587c478bdstevel@tonic-gate	{SCF_ERROR_NO_MEMORY,		"no memory available"},
597c478bdstevel@tonic-gate	{SCF_ERROR_CONSTRAINT_VIOLATED,	"required constraint not met"},
607c478bdstevel@tonic-gate	{SCF_ERROR_EXISTS,		"object already exists"},
617c478bdstevel@tonic-gate	{SCF_ERROR_NO_SERVER,		"repository server unavailable"},
627c478bdstevel@tonic-gate	{SCF_ERROR_NO_RESOURCES,	"server has insufficient resources"},
637c478bdstevel@tonic-gate	{SCF_ERROR_PERMISSION_DENIED,	"insufficient privileges for action"},
647c478bdstevel@tonic-gate	{SCF_ERROR_BACKEND_ACCESS,	"backend refused access"},
657c478bdstevel@tonic-gate	{SCF_ERROR_BACKEND_READONLY,	"backend is read-only"},
667c478bdstevel@tonic-gate	{SCF_ERROR_HANDLE_MISMATCH,	"mismatched SCF handles"},
677c478bdstevel@tonic-gate	{SCF_ERROR_HANDLE_DESTROYED,	"object bound to destroyed handle"},
687c478bdstevel@tonic-gate	{SCF_ERROR_VERSION_MISMATCH,	"incompatible SCF version"},
697c478bdstevel@tonic-gate	{SCF_ERROR_DELETED,		"object has been deleted"},
701f6eb02Liane Praza	{SCF_ERROR_TEMPLATE_INVALID,	"template data is invalid"},
717c478bdstevel@tonic-gate
727c478bdstevel@tonic-gate	{SCF_ERROR_CALLBACK_FAILED,	"user callback function failed"},
737c478bdstevel@tonic-gate
747c478bdstevel@tonic-gate	{SCF_ERROR_INTERNAL,		"internal error"}
757c478bdstevel@tonic-gate};
767c478bdstevel@tonic-gate#define	SCF_NUM_ERRORS	(sizeof (scf_errors) / sizeof (*scf_errors))
777c478bdstevel@tonic-gate
787c478bdstevel@tonic-gate/* a SWAG just in case things get out of sync, we can notice */
797c478bdstevel@tonic-gate#define	LOOKS_VALID(e)	\
807c478bdstevel@tonic-gate	((e) >= scf_errors[0].ei_code && \
817c478bdstevel@tonic-gate	    (e) < scf_errors[SCF_NUM_ERRORS - 1].ei_code + 10)
827c478bdstevel@tonic-gate
837c478bdstevel@tonic-gatestatic scf_error_t	_scf_fallback_error = SCF_ERROR_NONE;
847c478bdstevel@tonic-gate
85cb62078raf#if defined(PTHREAD_ONCE_KEY_NP)
86cb62078raf
87cb62078rafstatic pthread_key_t	scf_error_key = PTHREAD_ONCE_KEY_NP;
88cb62078raf
897c478bdstevel@tonic-gateint
907c478bdstevel@tonic-gatescf_setup_error(void)
917c478bdstevel@tonic-gate{
92cb62078raf	return (pthread_key_create_once_np(&scf_error_key, NULL) == 0);
93cb62078raf}
94cb62078raf
95cb62078raf#else	/* PTHREAD_ONCE_KEY_NP */
96cb62078raf
97cb62078raf/*
98cb62078raf * This old code is here to enable the building of a native version
99cb62078raf * of libscf.so when the build machine has not yet been upgraded
100cb62078raf * to a version of libc that provides pthread_key_create_once_np().
101cb62078raf * It should be deleted when solaris_nevada ships.
102cb62078raf * This code is not MT-safe in a relaxed memory model.
103cb62078raf */
104cb62078raf
105cb62078rafstatic pthread_key_t	scf_error_key = 0;
106cb62078raf
107cb62078rafint
108cb62078rafscf_setup_error(void)
109cb62078raf{
110cb62078raf	static pthread_mutex_t scf_key_lock = PTHREAD_MUTEX_INITIALIZER;
111cb62078raf	static volatile int scf_error_key_setup = 0;
112cb62078raf
1137c478bdstevel@tonic-gate	if (scf_error_key_setup == 0) {
114cb62078raf		(void) pthread_mutex_lock(&scf_key_lock);
115cb62078raf		if (scf_error_key_setup == 0) {
116cb62078raf			if (pthread_key_create(&scf_error_key, NULL) == 0)
117cb62078raf				scf_error_key_setup = 1;
118cb62078raf		}
119cb62078raf		(void) pthread_mutex_unlock(&scf_key_lock);
1207c478bdstevel@tonic-gate	}
1217c478bdstevel@tonic-gate
1227c478bdstevel@tonic-gate	return (scf_error_key_setup == 1);
1237c478bdstevel@tonic-gate}
1247c478bdstevel@tonic-gate
125cb62078raf#endif	/* PTHREAD_ONCE_KEY_NP */
126cb62078raf
1277c478bdstevel@tonic-gateint
1287c478bdstevel@tonic-gatescf_set_error(scf_error_t code)
1297c478bdstevel@tonic-gate{
1307c478bdstevel@tonic-gate	assert(LOOKS_VALID(code));
1317c478bdstevel@tonic-gate
132cb62078raf	if (scf_setup_error())
1337c478bdstevel@tonic-gate		(void) pthread_setspecific(scf_error_key, (void *)code);
1347c478bdstevel@tonic-gate	else
1357c478bdstevel@tonic-gate		_scf_fallback_error = code;
1367c478bdstevel@tonic-gate	return (-1);
1377c478bdstevel@tonic-gate}
1387c478bdstevel@tonic-gate
1397c478bdstevel@tonic-gatescf_error_t
1407c478bdstevel@tonic-gatescf_error(void)
1417c478bdstevel@tonic-gate{
1427c478bdstevel@tonic-gate	scf_error_t ret;
1437c478bdstevel@tonic-gate
1447c478bdstevel@tonic-gate	ret = (scf_error_t)pthread_getspecific(scf_error_key);
1457c478bdstevel@tonic-gate	if (ret == 0)
146cb62078raf		return (_scf_fallback_error);
1477c478bdstevel@tonic-gate	assert(LOOKS_VALID(ret));
1487c478bdstevel@tonic-gate	return (ret);
1497c478bdstevel@tonic-gate}
1507c478bdstevel@tonic-gate
1517c478bdstevel@tonic-gateconst char *
1527c478bdstevel@tonic-gatescf_strerror(scf_error_t code)
1537c478bdstevel@tonic-gate{
1547c478bdstevel@tonic-gate	struct scf_error_info *cur, *end;
1557c478bdstevel@tonic-gate
1567c478bdstevel@tonic-gate	cur = scf_errors;
1577c478bdstevel@tonic-gate	end = cur + SCF_NUM_ERRORS;
1587c478bdstevel@tonic-gate
1597c478bdstevel@tonic-gate	for (; cur < end; cur++)
1607c478bdstevel@tonic-gate		if (code == cur->ei_code)
1617c478bdstevel@tonic-gate			return (dgettext(TEXT_DOMAIN, cur->ei_desc));
1627c478bdstevel@tonic-gate
1637c478bdstevel@tonic-gate	return (dgettext(TEXT_DOMAIN, "unknown error"));
1647c478bdstevel@tonic-gate}
1657c478bdstevel@tonic-gate
1667c478bdstevel@tonic-gateconst char *
1677c478bdstevel@tonic-gatescf_get_msg(scf_msg_t msg)
1687c478bdstevel@tonic-gate{
1697c478bdstevel@tonic-gate	switch (msg) {
1707c478bdstevel@tonic-gate	case SCF_MSG_ARGTOOLONG:
1717c478bdstevel@tonic-gate		return (dgettext(TEXT_DOMAIN,
1727c478bdstevel@tonic-gate		    "Argument '%s' is too long, ignoring\n"));
1737c478bdstevel@tonic-gate
1747c478bdstevel@tonic-gate	case SCF_MSG_PATTERN_NOINSTANCE:
1757c478bdstevel@tonic-gate		return (dgettext(TEXT_DOMAIN,
1767c478bdstevel@tonic-gate		    "Pattern '%s' doesn't match any instances\n"));
1777c478bdstevel@tonic-gate
1787c478bdstevel@tonic-gate	case SCF_MSG_PATTERN_NOINSTSVC:
1797c478bdstevel@tonic-gate		return (dgettext(TEXT_DOMAIN,
1807c478bdstevel@tonic-gate		    "Pattern '%s' doesn't match any instances or services\n"));
1817c478bdstevel@tonic-gate
1827c478bdstevel@tonic-gate	case SCF_MSG_PATTERN_NOSERVICE:
1837c478bdstevel@tonic-gate		return (dgettext(TEXT_DOMAIN,
1847c478bdstevel@tonic-gate		    "Pattern '%s' doesn't match any services\n"));
1857c478bdstevel@tonic-gate
1867c478bdstevel@tonic-gate	case SCF_MSG_PATTERN_NOENTITY:
1877c478bdstevel@tonic-gate		return (dgettext(TEXT_DOMAIN,
1887c478bdstevel@tonic-gate		    "Pattern '%s' doesn't match any entities\n"));
1897c478bdstevel@tonic-gate
1907c478bdstevel@tonic-gate	case SCF_MSG_PATTERN_MULTIMATCH:
1917c478bdstevel@tonic-gate		return (dgettext(TEXT_DOMAIN,
1927c478bdstevel@tonic-gate		    "Pattern '%s' matches multiple instances:\n"));
1937c478bdstevel@tonic-gate
1947c478bdstevel@tonic-gate	case SCF_MSG_PATTERN_POSSIBLE:
1957c478bdstevel@tonic-gate		return (dgettext(TEXT_DOMAIN, "    %s\n"));
1967c478bdstevel@tonic-gate
1977c478bdstevel@tonic-gate	case SCF_MSG_PATTERN_LEGACY:
1987c478bdstevel@tonic-gate		return (dgettext(TEXT_DOMAIN,
1997c478bdstevel@tonic-gate		    "Operation not supported for legacy service '%s'\n"));
2007c478bdstevel@tonic-gate
2016c7c876Jerry Jelinek	case SCF_MSG_PATTERN_MULTIPARTIAL:
2026c7c876Jerry Jelinek		return (dgettext(TEXT_DOMAIN,
203ad3ad82Andy Fiddaman		    "Partial FMRI '%s' matches multiple instances:\n"));
2046c7c876Jerry Jelinek
2057c478bdstevel@tonic-gate	default:
2067c478bdstevel@tonic-gate		abort();
2077c478bdstevel@tonic-gate		/* NOTREACHED */
2087c478bdstevel@tonic-gate	}
2097c478bdstevel@tonic-gate
2107c478bdstevel@tonic-gate}
211