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 5*cb620785Sraf * Common Development and Distribution License (the "License"). 6*cb620785Sraf * 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 */ 21*cb620785Sraf 227c478bd9Sstevel@tonic-gate /* 23*cb620785Sraf * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 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 #define walkcontext _walkcontext /* work around 4743525 */ 437c478bd9Sstevel@tonic-gate #include <ucontext.h> 447c478bd9Sstevel@tonic-gate 457c478bd9Sstevel@tonic-gate extern int ndebug; 467c478bd9Sstevel@tonic-gate 477c478bd9Sstevel@tonic-gate static struct scf_error_info { 487c478bd9Sstevel@tonic-gate scf_error_t ei_code; 497c478bd9Sstevel@tonic-gate const char *ei_desc; 507c478bd9Sstevel@tonic-gate } scf_errors[] = { 517c478bd9Sstevel@tonic-gate {SCF_ERROR_NONE, "no error"}, 527c478bd9Sstevel@tonic-gate {SCF_ERROR_NOT_BOUND, "handle not bound"}, 537c478bd9Sstevel@tonic-gate {SCF_ERROR_NOT_SET, "cannot use unset argument"}, 547c478bd9Sstevel@tonic-gate {SCF_ERROR_NOT_FOUND, "entity not found"}, 557c478bd9Sstevel@tonic-gate {SCF_ERROR_TYPE_MISMATCH, "type does not match value"}, 567c478bd9Sstevel@tonic-gate {SCF_ERROR_IN_USE, "cannot modify while in-use"}, 577c478bd9Sstevel@tonic-gate {SCF_ERROR_CONNECTION_BROKEN, "connection to repository broken"}, 587c478bd9Sstevel@tonic-gate {SCF_ERROR_INVALID_ARGUMENT, "invalid argument"}, 597c478bd9Sstevel@tonic-gate {SCF_ERROR_NO_MEMORY, "no memory available"}, 607c478bd9Sstevel@tonic-gate {SCF_ERROR_CONSTRAINT_VIOLATED, "required constraint not met"}, 617c478bd9Sstevel@tonic-gate {SCF_ERROR_EXISTS, "object already exists"}, 627c478bd9Sstevel@tonic-gate {SCF_ERROR_NO_SERVER, "repository server unavailable"}, 637c478bd9Sstevel@tonic-gate {SCF_ERROR_NO_RESOURCES, "server has insufficient resources"}, 647c478bd9Sstevel@tonic-gate {SCF_ERROR_PERMISSION_DENIED, "insufficient privileges for action"}, 657c478bd9Sstevel@tonic-gate {SCF_ERROR_BACKEND_ACCESS, "backend refused access"}, 667c478bd9Sstevel@tonic-gate {SCF_ERROR_BACKEND_READONLY, "backend is read-only"}, 677c478bd9Sstevel@tonic-gate {SCF_ERROR_HANDLE_MISMATCH, "mismatched SCF handles"}, 687c478bd9Sstevel@tonic-gate {SCF_ERROR_HANDLE_DESTROYED, "object bound to destroyed handle"}, 697c478bd9Sstevel@tonic-gate {SCF_ERROR_VERSION_MISMATCH, "incompatible SCF version"}, 707c478bd9Sstevel@tonic-gate {SCF_ERROR_DELETED, "object has been deleted"}, 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 85*cb620785Sraf #if defined(PTHREAD_ONCE_KEY_NP) 86*cb620785Sraf 87*cb620785Sraf static pthread_key_t scf_error_key = PTHREAD_ONCE_KEY_NP; 88*cb620785Sraf 897c478bd9Sstevel@tonic-gate int 907c478bd9Sstevel@tonic-gate scf_setup_error(void) 917c478bd9Sstevel@tonic-gate { 92*cb620785Sraf return (pthread_key_create_once_np(&scf_error_key, NULL) == 0); 93*cb620785Sraf } 94*cb620785Sraf 95*cb620785Sraf #else /* PTHREAD_ONCE_KEY_NP */ 96*cb620785Sraf 97*cb620785Sraf /* 98*cb620785Sraf * This old code is here to enable the building of a native version 99*cb620785Sraf * of libscf.so when the build machine has not yet been upgraded 100*cb620785Sraf * to a version of libc that provides pthread_key_create_once_np(). 101*cb620785Sraf * It should be deleted when solaris_nevada ships. 102*cb620785Sraf * This code is not MT-safe in a relaxed memory model. 103*cb620785Sraf */ 104*cb620785Sraf 105*cb620785Sraf static pthread_key_t scf_error_key = 0; 106*cb620785Sraf 107*cb620785Sraf int 108*cb620785Sraf scf_setup_error(void) 109*cb620785Sraf { 110*cb620785Sraf static pthread_mutex_t scf_key_lock = PTHREAD_MUTEX_INITIALIZER; 111*cb620785Sraf static volatile int scf_error_key_setup = 0; 112*cb620785Sraf 1137c478bd9Sstevel@tonic-gate if (scf_error_key_setup == 0) { 114*cb620785Sraf (void) pthread_mutex_lock(&scf_key_lock); 115*cb620785Sraf if (scf_error_key_setup == 0) { 116*cb620785Sraf if (pthread_key_create(&scf_error_key, NULL) == 0) 117*cb620785Sraf scf_error_key_setup = 1; 118*cb620785Sraf } 119*cb620785Sraf (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 125*cb620785Sraf #endif /* PTHREAD_ONCE_KEY_NP */ 126*cb620785Sraf 1277c478bd9Sstevel@tonic-gate int 1287c478bd9Sstevel@tonic-gate scf_set_error(scf_error_t code) 1297c478bd9Sstevel@tonic-gate { 1307c478bd9Sstevel@tonic-gate assert(LOOKS_VALID(code)); 1317c478bd9Sstevel@tonic-gate 132*cb620785Sraf 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 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) 146*cb620785Sraf 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 * 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 * 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 2017c478bd9Sstevel@tonic-gate default: 2027c478bd9Sstevel@tonic-gate abort(); 2037c478bd9Sstevel@tonic-gate /* NOTREACHED */ 2047c478bd9Sstevel@tonic-gate } 2057c478bd9Sstevel@tonic-gate 2067c478bd9Sstevel@tonic-gate } 207