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
594501b61Sskamm * Common Development and Distribution License (the "License").
694501b61Sskamm * 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 */
217c478bd9Sstevel@tonic-gate /*
227c478bd9Sstevel@tonic-gate *
23*eed64e98Sgm * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate * This file contains routines to manipulate lists of repository values that
297c478bd9Sstevel@tonic-gate * are used to store process ids and the internal state. There are routines
307c478bd9Sstevel@tonic-gate * to read/write the lists from/to the repository and routines to modify or
317c478bd9Sstevel@tonic-gate * inspect the lists. It also contains routines that deal with the
327c478bd9Sstevel@tonic-gate * repository side of contract ids.
337c478bd9Sstevel@tonic-gate */
347c478bd9Sstevel@tonic-gate
357c478bd9Sstevel@tonic-gate #include <errno.h>
367c478bd9Sstevel@tonic-gate #include <stdlib.h>
377c478bd9Sstevel@tonic-gate #include <libintl.h>
387c478bd9Sstevel@tonic-gate #include <unistd.h>
397c478bd9Sstevel@tonic-gate #include <string.h>
407c478bd9Sstevel@tonic-gate #include <signal.h>
4194501b61Sskamm #include <sys/param.h>
4294501b61Sskamm #include <sys/types.h>
4394501b61Sskamm #include <sys/stat.h>
4494501b61Sskamm #include <libscf_priv.h>
457c478bd9Sstevel@tonic-gate #include "inetd_impl.h"
467c478bd9Sstevel@tonic-gate
477c478bd9Sstevel@tonic-gate /*
487c478bd9Sstevel@tonic-gate * Number of consecutive repository bind retries performed by bind_to_rep()
497c478bd9Sstevel@tonic-gate * before failing.
507c478bd9Sstevel@tonic-gate */
517c478bd9Sstevel@tonic-gate #define BIND_TO_REP_RETRIES 10
527c478bd9Sstevel@tonic-gate
537c478bd9Sstevel@tonic-gate /* Name of property group where inetd's state for a service is stored. */
547c478bd9Sstevel@tonic-gate #define PG_NAME_INSTANCE_STATE (const char *) "inetd_state"
557c478bd9Sstevel@tonic-gate
567c478bd9Sstevel@tonic-gate /* uu_list repval list pool */
577c478bd9Sstevel@tonic-gate static uu_list_pool_t *rep_val_pool = NULL;
587c478bd9Sstevel@tonic-gate
597c478bd9Sstevel@tonic-gate /*
607c478bd9Sstevel@tonic-gate * Repository object pointers that get set-up in repval_init() and closed down
617c478bd9Sstevel@tonic-gate * in repval_fini(). They're used in _retrieve_rep_vals(), _store_rep_vals(),
627c478bd9Sstevel@tonic-gate * add_remove_contract_norebind(), and adopt_repository_contracts(). They're
637c478bd9Sstevel@tonic-gate * global so they can be initialized once on inetd startup, and re-used
647c478bd9Sstevel@tonic-gate * there-after in the referenced functions.
657c478bd9Sstevel@tonic-gate */
667c478bd9Sstevel@tonic-gate static scf_handle_t *rep_handle = NULL;
677c478bd9Sstevel@tonic-gate static scf_propertygroup_t *pg = NULL;
687c478bd9Sstevel@tonic-gate static scf_instance_t *inst = NULL;
697c478bd9Sstevel@tonic-gate static scf_transaction_t *trans = NULL;
707c478bd9Sstevel@tonic-gate static scf_transaction_entry_t *entry = NULL;
717c478bd9Sstevel@tonic-gate static scf_property_t *prop = NULL;
727c478bd9Sstevel@tonic-gate
7394501b61Sskamm /*
7494501b61Sskamm * Pathname storage for paths generated from the fmri.
7594501b61Sskamm * Used when updating the ctid and (start) pid files for an inetd service.
7694501b61Sskamm */
7794501b61Sskamm static char genfmri_filename[MAXPATHLEN] = "";
7894501b61Sskamm static char genfmri_temp_filename[MAXPATHLEN] = "";
7994501b61Sskamm
807c478bd9Sstevel@tonic-gate /*
817c478bd9Sstevel@tonic-gate * Try and make the given handle bind be bound to the repository. If
827c478bd9Sstevel@tonic-gate * it's already bound, or we succeed a new bind return 0; else return
837c478bd9Sstevel@tonic-gate * -1 on failure, with the SCF error set to one of the following:
847c478bd9Sstevel@tonic-gate * SCF_ERROR_NO_SERVER
857c478bd9Sstevel@tonic-gate * SCF_ERROR_NO_RESOURCES
867c478bd9Sstevel@tonic-gate */
877c478bd9Sstevel@tonic-gate int
make_handle_bound(scf_handle_t * hdl)887c478bd9Sstevel@tonic-gate make_handle_bound(scf_handle_t *hdl)
897c478bd9Sstevel@tonic-gate {
907c478bd9Sstevel@tonic-gate uint_t retries;
917c478bd9Sstevel@tonic-gate
927c478bd9Sstevel@tonic-gate for (retries = 0; retries <= BIND_TO_REP_RETRIES; retries++) {
937c478bd9Sstevel@tonic-gate if ((scf_handle_bind(hdl) == 0) ||
947c478bd9Sstevel@tonic-gate (scf_error() == SCF_ERROR_IN_USE))
957c478bd9Sstevel@tonic-gate return (0);
967c478bd9Sstevel@tonic-gate
977c478bd9Sstevel@tonic-gate assert(scf_error() != SCF_ERROR_INVALID_ARGUMENT);
987c478bd9Sstevel@tonic-gate }
997c478bd9Sstevel@tonic-gate
1007c478bd9Sstevel@tonic-gate return (-1);
1017c478bd9Sstevel@tonic-gate }
1027c478bd9Sstevel@tonic-gate
1037c478bd9Sstevel@tonic-gate int
repval_init(void)1047c478bd9Sstevel@tonic-gate repval_init(void)
1057c478bd9Sstevel@tonic-gate {
1067c478bd9Sstevel@tonic-gate /*
1077c478bd9Sstevel@tonic-gate * Create the repval list pool.
1087c478bd9Sstevel@tonic-gate */
1097c478bd9Sstevel@tonic-gate rep_val_pool = uu_list_pool_create("rep_val_pool", sizeof (rep_val_t),
1107c478bd9Sstevel@tonic-gate offsetof(rep_val_t, link), NULL, UU_LIST_POOL_DEBUG);
1117c478bd9Sstevel@tonic-gate if (rep_val_pool == NULL) {
1127c478bd9Sstevel@tonic-gate error_msg("%s: %s", gettext("Failed to create rep_val pool"),
1137c478bd9Sstevel@tonic-gate uu_strerror(uu_error()));
1147c478bd9Sstevel@tonic-gate return (-1);
1157c478bd9Sstevel@tonic-gate }
1167c478bd9Sstevel@tonic-gate
1177c478bd9Sstevel@tonic-gate /*
1187c478bd9Sstevel@tonic-gate * Create and bind a repository handle, and create all repository
1197c478bd9Sstevel@tonic-gate * objects that we'll use later that are associated with it. On any
1207c478bd9Sstevel@tonic-gate * errors we simply return -1 and let repval_fini() clean-up after
1217c478bd9Sstevel@tonic-gate * us.
1227c478bd9Sstevel@tonic-gate */
1237c478bd9Sstevel@tonic-gate if ((rep_handle = scf_handle_create(SCF_VERSION)) == NULL) {
1247c478bd9Sstevel@tonic-gate error_msg("%s: %s",
1257c478bd9Sstevel@tonic-gate gettext("Failed to create repository handle"),
1267c478bd9Sstevel@tonic-gate scf_strerror(scf_error()));
1277c478bd9Sstevel@tonic-gate goto cleanup;
1287c478bd9Sstevel@tonic-gate } else if (make_handle_bound(rep_handle) == -1) {
1297c478bd9Sstevel@tonic-gate goto cleanup;
1307c478bd9Sstevel@tonic-gate } else if (((pg = scf_pg_create(rep_handle)) == NULL) ||
1317c478bd9Sstevel@tonic-gate ((inst = scf_instance_create(rep_handle)) == NULL) ||
1327c478bd9Sstevel@tonic-gate ((trans = scf_transaction_create(rep_handle)) == NULL) ||
1337c478bd9Sstevel@tonic-gate ((entry = scf_entry_create(rep_handle)) == NULL) ||
1347c478bd9Sstevel@tonic-gate ((prop = scf_property_create(rep_handle)) == NULL)) {
1357c478bd9Sstevel@tonic-gate error_msg("%s: %s",
1367c478bd9Sstevel@tonic-gate gettext("Failed to create repository object"),
1377c478bd9Sstevel@tonic-gate scf_strerror(scf_error()));
1387c478bd9Sstevel@tonic-gate goto cleanup;
1397c478bd9Sstevel@tonic-gate }
1407c478bd9Sstevel@tonic-gate
1417c478bd9Sstevel@tonic-gate return (0);
1427c478bd9Sstevel@tonic-gate cleanup:
1437c478bd9Sstevel@tonic-gate repval_fini();
1447c478bd9Sstevel@tonic-gate return (-1);
1457c478bd9Sstevel@tonic-gate }
1467c478bd9Sstevel@tonic-gate
1477c478bd9Sstevel@tonic-gate void
repval_fini(void)1487c478bd9Sstevel@tonic-gate repval_fini(void)
1497c478bd9Sstevel@tonic-gate {
1507c478bd9Sstevel@tonic-gate if (rep_handle != NULL) {
1517c478bd9Sstevel@tonic-gate /*
1527c478bd9Sstevel@tonic-gate * We unbind from the repository before we free the repository
1537c478bd9Sstevel@tonic-gate * objects for efficiency reasons.
1547c478bd9Sstevel@tonic-gate */
1557c478bd9Sstevel@tonic-gate (void) scf_handle_unbind(rep_handle);
1567c478bd9Sstevel@tonic-gate
1577c478bd9Sstevel@tonic-gate scf_pg_destroy(pg);
1587c478bd9Sstevel@tonic-gate pg = NULL;
1597c478bd9Sstevel@tonic-gate scf_instance_destroy(inst);
1607c478bd9Sstevel@tonic-gate inst = NULL;
1617c478bd9Sstevel@tonic-gate scf_transaction_destroy(trans);
1627c478bd9Sstevel@tonic-gate trans = NULL;
1637c478bd9Sstevel@tonic-gate scf_entry_destroy(entry);
1647c478bd9Sstevel@tonic-gate entry = NULL;
1657c478bd9Sstevel@tonic-gate scf_property_destroy(prop);
1667c478bd9Sstevel@tonic-gate prop = NULL;
1677c478bd9Sstevel@tonic-gate
1687c478bd9Sstevel@tonic-gate scf_handle_destroy(rep_handle);
1697c478bd9Sstevel@tonic-gate rep_handle = NULL;
1707c478bd9Sstevel@tonic-gate }
1717c478bd9Sstevel@tonic-gate
1727c478bd9Sstevel@tonic-gate if (rep_val_pool != NULL) {
1737c478bd9Sstevel@tonic-gate uu_list_pool_destroy(rep_val_pool);
1747c478bd9Sstevel@tonic-gate rep_val_pool = NULL;
1757c478bd9Sstevel@tonic-gate }
1767c478bd9Sstevel@tonic-gate }
1777c478bd9Sstevel@tonic-gate
1787c478bd9Sstevel@tonic-gate uu_list_t *
create_rep_val_list(void)1797c478bd9Sstevel@tonic-gate create_rep_val_list(void)
1807c478bd9Sstevel@tonic-gate {
1817c478bd9Sstevel@tonic-gate uu_list_t *ret;
1827c478bd9Sstevel@tonic-gate
1837c478bd9Sstevel@tonic-gate if ((ret = uu_list_create(rep_val_pool, NULL, 0)) == NULL)
1847c478bd9Sstevel@tonic-gate assert(uu_error() == UU_ERROR_NO_MEMORY);
1857c478bd9Sstevel@tonic-gate
1867c478bd9Sstevel@tonic-gate return (ret);
1877c478bd9Sstevel@tonic-gate }
1887c478bd9Sstevel@tonic-gate
1897c478bd9Sstevel@tonic-gate void
destroy_rep_val_list(uu_list_t * list)1907c478bd9Sstevel@tonic-gate destroy_rep_val_list(uu_list_t *list)
1917c478bd9Sstevel@tonic-gate {
1927c478bd9Sstevel@tonic-gate if (list != NULL) {
1937c478bd9Sstevel@tonic-gate empty_rep_val_list(list);
1947c478bd9Sstevel@tonic-gate uu_list_destroy(list);
1957c478bd9Sstevel@tonic-gate }
1967c478bd9Sstevel@tonic-gate }
1977c478bd9Sstevel@tonic-gate
1987c478bd9Sstevel@tonic-gate rep_val_t *
find_rep_val(uu_list_t * list,int64_t val)1997c478bd9Sstevel@tonic-gate find_rep_val(uu_list_t *list, int64_t val)
2007c478bd9Sstevel@tonic-gate {
2017c478bd9Sstevel@tonic-gate rep_val_t *rv;
2027c478bd9Sstevel@tonic-gate
2037c478bd9Sstevel@tonic-gate for (rv = uu_list_first(list); rv != NULL;
2047c478bd9Sstevel@tonic-gate rv = uu_list_next(list, rv)) {
2057c478bd9Sstevel@tonic-gate if (rv->val == val)
2067c478bd9Sstevel@tonic-gate break;
2077c478bd9Sstevel@tonic-gate }
2087c478bd9Sstevel@tonic-gate return (rv);
2097c478bd9Sstevel@tonic-gate }
2107c478bd9Sstevel@tonic-gate
2117c478bd9Sstevel@tonic-gate int
add_rep_val(uu_list_t * list,int64_t val)2127c478bd9Sstevel@tonic-gate add_rep_val(uu_list_t *list, int64_t val)
2137c478bd9Sstevel@tonic-gate {
2147c478bd9Sstevel@tonic-gate rep_val_t *rv;
2157c478bd9Sstevel@tonic-gate
2167c478bd9Sstevel@tonic-gate if ((rv = malloc(sizeof (rep_val_t))) == NULL)
2177c478bd9Sstevel@tonic-gate return (-1);
2187c478bd9Sstevel@tonic-gate
2197c478bd9Sstevel@tonic-gate uu_list_node_init(rv, &rv->link, rep_val_pool);
2207c478bd9Sstevel@tonic-gate rv->val = val;
2217c478bd9Sstevel@tonic-gate rv->scf_val = NULL;
2227c478bd9Sstevel@tonic-gate (void) uu_list_insert_after(list, NULL, rv);
2237c478bd9Sstevel@tonic-gate
2247c478bd9Sstevel@tonic-gate return (0);
2257c478bd9Sstevel@tonic-gate }
2267c478bd9Sstevel@tonic-gate
2277c478bd9Sstevel@tonic-gate void
remove_rep_val(uu_list_t * list,int64_t val)2287c478bd9Sstevel@tonic-gate remove_rep_val(uu_list_t *list, int64_t val)
2297c478bd9Sstevel@tonic-gate {
2307c478bd9Sstevel@tonic-gate rep_val_t *rv;
2317c478bd9Sstevel@tonic-gate
2327c478bd9Sstevel@tonic-gate if ((rv = find_rep_val(list, val)) != NULL) {
2337c478bd9Sstevel@tonic-gate uu_list_remove(list, rv);
2347c478bd9Sstevel@tonic-gate assert(rv->scf_val == NULL);
2357c478bd9Sstevel@tonic-gate free(rv);
2367c478bd9Sstevel@tonic-gate }
2377c478bd9Sstevel@tonic-gate }
2387c478bd9Sstevel@tonic-gate
2397c478bd9Sstevel@tonic-gate void
empty_rep_val_list(uu_list_t * list)2407c478bd9Sstevel@tonic-gate empty_rep_val_list(uu_list_t *list)
2417c478bd9Sstevel@tonic-gate {
2427c478bd9Sstevel@tonic-gate void *cookie = NULL;
2437c478bd9Sstevel@tonic-gate rep_val_t *rv;
2447c478bd9Sstevel@tonic-gate
2457c478bd9Sstevel@tonic-gate while ((rv = uu_list_teardown(list, &cookie)) != NULL) {
2467c478bd9Sstevel@tonic-gate if (rv->scf_val != NULL)
2477c478bd9Sstevel@tonic-gate scf_value_destroy(rv->scf_val);
2487c478bd9Sstevel@tonic-gate free(rv);
2497c478bd9Sstevel@tonic-gate }
2507c478bd9Sstevel@tonic-gate }
2517c478bd9Sstevel@tonic-gate
2527c478bd9Sstevel@tonic-gate int64_t
get_single_rep_val(uu_list_t * list)2537c478bd9Sstevel@tonic-gate get_single_rep_val(uu_list_t *list)
2547c478bd9Sstevel@tonic-gate {
2557c478bd9Sstevel@tonic-gate rep_val_t *rv = uu_list_first(list);
2567c478bd9Sstevel@tonic-gate
2577c478bd9Sstevel@tonic-gate assert(rv != NULL);
2587c478bd9Sstevel@tonic-gate return (rv->val);
2597c478bd9Sstevel@tonic-gate }
2607c478bd9Sstevel@tonic-gate
2617c478bd9Sstevel@tonic-gate int
set_single_rep_val(uu_list_t * list,int64_t val)2627c478bd9Sstevel@tonic-gate set_single_rep_val(uu_list_t *list, int64_t val)
2637c478bd9Sstevel@tonic-gate {
2647c478bd9Sstevel@tonic-gate rep_val_t *rv = uu_list_first(list);
2657c478bd9Sstevel@tonic-gate
2667c478bd9Sstevel@tonic-gate if (rv == NULL) {
2677c478bd9Sstevel@tonic-gate if (add_rep_val(list, val) == -1)
2687c478bd9Sstevel@tonic-gate return (-1);
2697c478bd9Sstevel@tonic-gate } else {
2707c478bd9Sstevel@tonic-gate rv->val = val;
2717c478bd9Sstevel@tonic-gate }
2727c478bd9Sstevel@tonic-gate
2737c478bd9Sstevel@tonic-gate return (0);
2747c478bd9Sstevel@tonic-gate }
2757c478bd9Sstevel@tonic-gate
2767c478bd9Sstevel@tonic-gate /*
2777c478bd9Sstevel@tonic-gate * Partner to add_tr_entry_values. This function frees the scf_values created
2787c478bd9Sstevel@tonic-gate * in add_tr_entry_values() in the list 'vals'.
2797c478bd9Sstevel@tonic-gate */
2807c478bd9Sstevel@tonic-gate static void
remove_tr_entry_values(uu_list_t * vals)2817c478bd9Sstevel@tonic-gate remove_tr_entry_values(uu_list_t *vals)
2827c478bd9Sstevel@tonic-gate {
2837c478bd9Sstevel@tonic-gate rep_val_t *rval;
2847c478bd9Sstevel@tonic-gate
2857c478bd9Sstevel@tonic-gate for (rval = uu_list_first(vals); rval != NULL;
2867c478bd9Sstevel@tonic-gate rval = uu_list_next(vals, rval)) {
2877c478bd9Sstevel@tonic-gate if (rval->scf_val != NULL) {
2887c478bd9Sstevel@tonic-gate scf_value_destroy(rval->scf_val);
2897c478bd9Sstevel@tonic-gate rval->scf_val = NULL;
2907c478bd9Sstevel@tonic-gate }
2917c478bd9Sstevel@tonic-gate }
2927c478bd9Sstevel@tonic-gate }
2937c478bd9Sstevel@tonic-gate
2947c478bd9Sstevel@tonic-gate /*
2957c478bd9Sstevel@tonic-gate * This function creates and associates with transaction entry 'entry' an
2967c478bd9Sstevel@tonic-gate * scf value for each value in 'vals'. The pointers to the scf values
2977c478bd9Sstevel@tonic-gate * are stored in the list for later cleanup by remove_tr_entry_values.
2987c478bd9Sstevel@tonic-gate * Returns 0 on success, else -1 on error with scf_error() set to:
2997c478bd9Sstevel@tonic-gate * SCF_ERROR_NO_MEMORY if memory allocation failed.
3007c478bd9Sstevel@tonic-gate * SCF_ERROR_CONNECTION_BROKEN if the connection to the repository was broken.
3017c478bd9Sstevel@tonic-gate */
3027c478bd9Sstevel@tonic-gate static int
add_tr_entry_values(scf_handle_t * hdl,scf_transaction_entry_t * entry,uu_list_t * vals)3037c478bd9Sstevel@tonic-gate add_tr_entry_values(scf_handle_t *hdl, scf_transaction_entry_t *entry,
3047c478bd9Sstevel@tonic-gate uu_list_t *vals)
3057c478bd9Sstevel@tonic-gate {
3067c478bd9Sstevel@tonic-gate rep_val_t *rval;
3077c478bd9Sstevel@tonic-gate
3087c478bd9Sstevel@tonic-gate for (rval = uu_list_first(vals); rval != NULL;
3097c478bd9Sstevel@tonic-gate rval = uu_list_next(vals, rval)) {
3107c478bd9Sstevel@tonic-gate
3117c478bd9Sstevel@tonic-gate assert(rval->scf_val == NULL);
3127c478bd9Sstevel@tonic-gate if ((rval->scf_val = scf_value_create(hdl)) == NULL) {
3137c478bd9Sstevel@tonic-gate remove_tr_entry_values(vals);
3147c478bd9Sstevel@tonic-gate return (-1);
3157c478bd9Sstevel@tonic-gate }
3167c478bd9Sstevel@tonic-gate
3177c478bd9Sstevel@tonic-gate scf_value_set_integer(rval->scf_val, rval->val);
3187c478bd9Sstevel@tonic-gate
3197c478bd9Sstevel@tonic-gate if (scf_entry_add_value(entry, rval->scf_val) < 0) {
3207c478bd9Sstevel@tonic-gate remove_tr_entry_values(vals);
3217c478bd9Sstevel@tonic-gate return (-1);
3227c478bd9Sstevel@tonic-gate }
3237c478bd9Sstevel@tonic-gate }
3247c478bd9Sstevel@tonic-gate
3257c478bd9Sstevel@tonic-gate return (0);
3267c478bd9Sstevel@tonic-gate }
3277c478bd9Sstevel@tonic-gate
3287c478bd9Sstevel@tonic-gate /*
3297c478bd9Sstevel@tonic-gate * Stores the values contained in the list 'vals' into the property 'prop_name'
3307c478bd9Sstevel@tonic-gate * of the instance with fmri 'inst_fmri', within the instance's instance
3317c478bd9Sstevel@tonic-gate * state property group.
3327c478bd9Sstevel@tonic-gate *
3337c478bd9Sstevel@tonic-gate * Returns 0 on success, else one of the following on failure:
3347c478bd9Sstevel@tonic-gate * SCF_ERROR_NO_MEMORY if memory allocation failed.
3357c478bd9Sstevel@tonic-gate * SCF_ERROR_NO_RESOURCES if the server doesn't have required resources.
3367c478bd9Sstevel@tonic-gate * SCF_ERROR_VERSION_MISMATCH if program compiled against a newer libscf
3377c478bd9Sstevel@tonic-gate * than on system.
3387c478bd9Sstevel@tonic-gate * SCF_ERROR_PERMISSION_DENIED if insufficient privileges to modify pg.
3397c478bd9Sstevel@tonic-gate * SCF_ERROR_BACKEND_ACCESS if the repository back-end refused the pg modify.
3407c478bd9Sstevel@tonic-gate * SCF_ERROR_CONNECTION_BROKEN if the connection to the repository was broken.
3417c478bd9Sstevel@tonic-gate */
3427c478bd9Sstevel@tonic-gate static scf_error_t
_store_rep_vals(uu_list_t * vals,const char * inst_fmri,const char * prop_name)3437c478bd9Sstevel@tonic-gate _store_rep_vals(uu_list_t *vals, const char *inst_fmri, const char *prop_name)
3447c478bd9Sstevel@tonic-gate {
3457c478bd9Sstevel@tonic-gate int cret;
3467c478bd9Sstevel@tonic-gate int ret;
3477c478bd9Sstevel@tonic-gate
3487c478bd9Sstevel@tonic-gate if (scf_handle_decode_fmri(rep_handle, inst_fmri, NULL, NULL, inst,
3497c478bd9Sstevel@tonic-gate NULL, NULL, SCF_DECODE_FMRI_EXACT) == -1)
3507c478bd9Sstevel@tonic-gate return (scf_error());
3517c478bd9Sstevel@tonic-gate
3527c478bd9Sstevel@tonic-gate /*
3537c478bd9Sstevel@tonic-gate * Fetch the instance state pg, and if it doesn't exist try and
3547c478bd9Sstevel@tonic-gate * create it.
3557c478bd9Sstevel@tonic-gate */
3567c478bd9Sstevel@tonic-gate if (scf_instance_get_pg(inst, PG_NAME_INSTANCE_STATE, pg) < 0) {
3577c478bd9Sstevel@tonic-gate if (scf_error() != SCF_ERROR_NOT_FOUND)
3587c478bd9Sstevel@tonic-gate return (scf_error());
3597c478bd9Sstevel@tonic-gate if (scf_instance_add_pg(inst, PG_NAME_INSTANCE_STATE,
3607c478bd9Sstevel@tonic-gate SCF_GROUP_FRAMEWORK, SCF_PG_FLAG_NONPERSISTENT, pg) < 0)
3617c478bd9Sstevel@tonic-gate return (scf_error());
3627c478bd9Sstevel@tonic-gate }
3637c478bd9Sstevel@tonic-gate
3647c478bd9Sstevel@tonic-gate /*
3657c478bd9Sstevel@tonic-gate * Perform a transaction to write the values to the requested property.
3667c478bd9Sstevel@tonic-gate * If someone got there before us, loop and retry.
3677c478bd9Sstevel@tonic-gate */
3687c478bd9Sstevel@tonic-gate do {
3697c478bd9Sstevel@tonic-gate if (scf_transaction_start(trans, pg) < 0)
3707c478bd9Sstevel@tonic-gate return (scf_error());
3717c478bd9Sstevel@tonic-gate
3727c478bd9Sstevel@tonic-gate if ((scf_transaction_property_new(trans, entry,
3737c478bd9Sstevel@tonic-gate prop_name, SCF_TYPE_INTEGER) < 0) &&
3747c478bd9Sstevel@tonic-gate (scf_transaction_property_change_type(trans, entry,
3757c478bd9Sstevel@tonic-gate prop_name, SCF_TYPE_INTEGER) < 0)) {
3767c478bd9Sstevel@tonic-gate ret = scf_error();
3777c478bd9Sstevel@tonic-gate goto cleanup;
3787c478bd9Sstevel@tonic-gate }
3797c478bd9Sstevel@tonic-gate
3807c478bd9Sstevel@tonic-gate if (add_tr_entry_values(rep_handle, entry, vals) < 0) {
3817c478bd9Sstevel@tonic-gate ret = scf_error();
3827c478bd9Sstevel@tonic-gate goto cleanup;
3837c478bd9Sstevel@tonic-gate }
3847c478bd9Sstevel@tonic-gate
3857c478bd9Sstevel@tonic-gate if ((cret = scf_transaction_commit(trans)) < 0) {
3867c478bd9Sstevel@tonic-gate ret = scf_error();
3877c478bd9Sstevel@tonic-gate goto cleanup;
3887c478bd9Sstevel@tonic-gate } else if (cret == 0) {
3897c478bd9Sstevel@tonic-gate scf_transaction_reset(trans);
3907c478bd9Sstevel@tonic-gate scf_entry_reset(entry);
3917c478bd9Sstevel@tonic-gate remove_tr_entry_values(vals);
3927c478bd9Sstevel@tonic-gate if (scf_pg_update(pg) < 0) {
3937c478bd9Sstevel@tonic-gate ret = scf_error();
3947c478bd9Sstevel@tonic-gate goto cleanup;
3957c478bd9Sstevel@tonic-gate }
3967c478bd9Sstevel@tonic-gate }
3977c478bd9Sstevel@tonic-gate } while (cret == 0);
3987c478bd9Sstevel@tonic-gate
3997c478bd9Sstevel@tonic-gate ret = 0;
4007c478bd9Sstevel@tonic-gate cleanup:
4017c478bd9Sstevel@tonic-gate scf_transaction_reset(trans);
4027c478bd9Sstevel@tonic-gate scf_entry_reset(entry);
4037c478bd9Sstevel@tonic-gate remove_tr_entry_values(vals);
4047c478bd9Sstevel@tonic-gate return (ret);
4057c478bd9Sstevel@tonic-gate }
4067c478bd9Sstevel@tonic-gate
4077c478bd9Sstevel@tonic-gate /*
4087c478bd9Sstevel@tonic-gate * Retrieves the repository values of property 'prop_name', of the instance
4097c478bd9Sstevel@tonic-gate * with fmri 'fmri', from within the instance's instance state property
4107c478bd9Sstevel@tonic-gate * group and adds them to the value list 'list'.
4117c478bd9Sstevel@tonic-gate *
4127c478bd9Sstevel@tonic-gate * Returns 0 on success, else one of the following values on error:
4137c478bd9Sstevel@tonic-gate * SCF_ERROR_NOT_FOUND if the property doesn't exist.
4147c478bd9Sstevel@tonic-gate * SCF_ERROR_NO_MEMORY if memory allocation failed.
4157c478bd9Sstevel@tonic-gate * SCF_ERROR_CONNECTION_BROKEN if the connection to the repository was broken.
4167c478bd9Sstevel@tonic-gate * SCF_ERROR_TYPE_MISMATCH if the property was of an unexpected type.
4177c478bd9Sstevel@tonic-gate *
4187c478bd9Sstevel@tonic-gate */
4197c478bd9Sstevel@tonic-gate static scf_error_t
_retrieve_rep_vals(uu_list_t * list,const char * fmri,const char * prop_name)4207c478bd9Sstevel@tonic-gate _retrieve_rep_vals(uu_list_t *list, const char *fmri, const char *prop_name)
4217c478bd9Sstevel@tonic-gate {
4227c478bd9Sstevel@tonic-gate scf_simple_prop_t *sp;
4237c478bd9Sstevel@tonic-gate int64_t *ip;
4247c478bd9Sstevel@tonic-gate
4257c478bd9Sstevel@tonic-gate if ((sp = scf_simple_prop_get(rep_handle, fmri, PG_NAME_INSTANCE_STATE,
4267c478bd9Sstevel@tonic-gate prop_name)) == NULL)
4277c478bd9Sstevel@tonic-gate return (scf_error());
4287c478bd9Sstevel@tonic-gate
4297c478bd9Sstevel@tonic-gate while ((ip = scf_simple_prop_next_integer(sp)) != NULL) {
4307c478bd9Sstevel@tonic-gate if (add_rep_val(list, *ip) == -1) {
4317c478bd9Sstevel@tonic-gate empty_rep_val_list(list);
4327c478bd9Sstevel@tonic-gate scf_simple_prop_free(sp);
4337c478bd9Sstevel@tonic-gate return (SCF_ERROR_NO_MEMORY);
4347c478bd9Sstevel@tonic-gate }
4357c478bd9Sstevel@tonic-gate }
4367c478bd9Sstevel@tonic-gate if (scf_error() != SCF_ERROR_NONE) {
4377c478bd9Sstevel@tonic-gate assert(scf_error() == SCF_ERROR_TYPE_MISMATCH);
4387c478bd9Sstevel@tonic-gate empty_rep_val_list(list);
4397c478bd9Sstevel@tonic-gate scf_simple_prop_free(sp);
4407c478bd9Sstevel@tonic-gate return (scf_error());
4417c478bd9Sstevel@tonic-gate }
4427c478bd9Sstevel@tonic-gate
4437c478bd9Sstevel@tonic-gate scf_simple_prop_free(sp);
4447c478bd9Sstevel@tonic-gate return (0);
4457c478bd9Sstevel@tonic-gate }
4467c478bd9Sstevel@tonic-gate
4477c478bd9Sstevel@tonic-gate /*
44894501b61Sskamm * Writes the repository values in the vals list to
44994501b61Sskamm * a file that is generated based on the passed in fmri and name.
45094501b61Sskamm * Returns 0 on success,
45194501b61Sskamm * ENAMETOOLONG if unable to generate filename from fmri (including
45294501b61Sskamm * the inability to create the directory for the generated filename) and
45394501b61Sskamm * ENOENT on all other failures.
45494501b61Sskamm */
45594501b61Sskamm static int
repvals_to_file(const char * fmri,const char * name,uu_list_t * vals)45694501b61Sskamm repvals_to_file(const char *fmri, const char *name, uu_list_t *vals)
45794501b61Sskamm {
45894501b61Sskamm int tfd;
45994501b61Sskamm FILE *tfp; /* temp fp */
46094501b61Sskamm rep_val_t *spval; /* Contains a start_pid or ctid */
46194501b61Sskamm int ret = 0;
46294501b61Sskamm
46394501b61Sskamm if (gen_filenms_from_fmri(fmri, name, genfmri_filename,
46494501b61Sskamm genfmri_temp_filename) != 0) {
46594501b61Sskamm /* Failure either from fmri too long or mkdir failure */
46694501b61Sskamm return (ENAMETOOLONG);
46794501b61Sskamm }
46894501b61Sskamm
46994501b61Sskamm if ((tfd = mkstemp(genfmri_temp_filename)) == -1) {
47094501b61Sskamm return (ENOENT);
47194501b61Sskamm }
47294501b61Sskamm
47394501b61Sskamm if (fchmod(tfd, (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1) {
47494501b61Sskamm (void) close(tfd);
47594501b61Sskamm ret = ENOENT;
47694501b61Sskamm goto unlink_out;
47794501b61Sskamm }
47894501b61Sskamm
47994501b61Sskamm if ((tfp = fdopen(tfd, "w")) == NULL) {
48094501b61Sskamm (void) close(tfd);
48194501b61Sskamm ret = ENOENT;
48294501b61Sskamm goto unlink_out;
48394501b61Sskamm }
48494501b61Sskamm
48594501b61Sskamm for (spval = uu_list_first(vals); spval != NULL;
48694501b61Sskamm spval = uu_list_next(vals, spval)) {
48794501b61Sskamm if (fprintf(tfp, "%lld\n", spval->val) <= 0) {
48894501b61Sskamm (void) fclose(tfp);
48994501b61Sskamm ret = ENOENT;
49094501b61Sskamm goto unlink_out;
49194501b61Sskamm }
49294501b61Sskamm }
49394501b61Sskamm if (fclose(tfp) != 0) {
49494501b61Sskamm ret = ENOENT;
49594501b61Sskamm goto unlink_out;
49694501b61Sskamm }
49794501b61Sskamm if (rename(genfmri_temp_filename, genfmri_filename) != 0) {
49894501b61Sskamm ret = ENOENT;
49994501b61Sskamm goto unlink_out;
50094501b61Sskamm }
50194501b61Sskamm return (0);
50294501b61Sskamm
50394501b61Sskamm unlink_out:
50494501b61Sskamm if (unlink(genfmri_temp_filename) != 0) {
50594501b61Sskamm warn_msg(gettext("Removal of temp file "
50694501b61Sskamm "%s failed. Please remove manually."),
50794501b61Sskamm genfmri_temp_filename);
50894501b61Sskamm }
50994501b61Sskamm return (ret);
51094501b61Sskamm }
51194501b61Sskamm
51294501b61Sskamm /*
51394501b61Sskamm * A routine that loops trying to read/write values until either success,
51494501b61Sskamm * an error other than a broken repository connection or
5157c478bd9Sstevel@tonic-gate * the number of retries reaches REP_OP_RETRIES.
51694501b61Sskamm * This action is used to read/write the values:
51794501b61Sskamm * reads/writes to a file for the START_PIDS property due to scalability
51894501b61Sskamm * problems with libscf
51994501b61Sskamm * reads/writes to the repository for all other properties.
5207c478bd9Sstevel@tonic-gate * Returns 0 on success, else the error value from either _store_rep_vals or
52194501b61Sskamm * _retrieve_rep_vals (based on whether 'store' was set or not), or one of the
52294501b61Sskamm * following:
52394501b61Sskamm * SCF_ERROR_NO_RESOURCES if the server doesn't have adequate resources
52494501b61Sskamm * SCF_ERROR_NO_MEMORY if a memory allocation failure
5257c478bd9Sstevel@tonic-gate * SCF_ERROR_NO_SERVER if the server isn't running.
52694501b61Sskamm * SCF_ERROR_CONSTRAINT_VIOLATED if an error in dealing with the speedy files
5277c478bd9Sstevel@tonic-gate */
5287c478bd9Sstevel@tonic-gate static scf_error_t
store_retrieve_rep_vals(uu_list_t * vals,const char * fmri,const char * prop,boolean_t store)5297c478bd9Sstevel@tonic-gate store_retrieve_rep_vals(uu_list_t *vals, const char *fmri,
5307c478bd9Sstevel@tonic-gate const char *prop, boolean_t store)
5317c478bd9Sstevel@tonic-gate {
53294501b61Sskamm scf_error_t ret = 0;
5337c478bd9Sstevel@tonic-gate uint_t retries;
53494501b61Sskamm FILE *tfp; /* temp fp */
53594501b61Sskamm int64_t tval; /* temp val holder */
53694501b61Sskamm int fscanf_ret;
53794501b61Sskamm int fopen_retry_cnt = 2;
5387c478bd9Sstevel@tonic-gate
53994501b61Sskamm /* inetd specific action for START_PIDS property */
54094501b61Sskamm if (strcmp(prop, PR_NAME_START_PIDS) == 0) {
54194501b61Sskamm /*
54294501b61Sskamm * Storage performance of START_PIDS is important,
54394501b61Sskamm * so each instance has its own file and all start_pids
54494501b61Sskamm * in the list are written to a temp file and then
54594501b61Sskamm * moved (renamed).
54694501b61Sskamm */
54794501b61Sskamm if (store) {
54894501b61Sskamm /* Write all values in list to file */
54994501b61Sskamm if (repvals_to_file(fmri, "pid", vals)) {
55094501b61Sskamm return (SCF_ERROR_CONSTRAINT_VIOLATED);
55194501b61Sskamm }
55294501b61Sskamm } else {
55394501b61Sskamm /* no temp name needed */
55494501b61Sskamm if (gen_filenms_from_fmri(fmri, "pid", genfmri_filename,
55594501b61Sskamm NULL) != 0)
55694501b61Sskamm return (SCF_ERROR_CONSTRAINT_VIOLATED);
55794501b61Sskamm
55894501b61Sskamm retry_fopen:
55994501b61Sskamm /* It's ok if no file, there are just no pids */
56094501b61Sskamm if ((tfp = fopen(genfmri_filename, "r")) == NULL) {
56194501b61Sskamm if ((errno == EINTR) && (fopen_retry_cnt > 0)) {
56294501b61Sskamm fopen_retry_cnt--;
56394501b61Sskamm goto retry_fopen;
56494501b61Sskamm }
56594501b61Sskamm return (0);
56694501b61Sskamm }
56794501b61Sskamm /* fscanf may not set errno, so clear it first */
56894501b61Sskamm errno = 0;
56994501b61Sskamm while ((fscanf_ret = fscanf(tfp, "%lld", &tval)) == 1) {
57094501b61Sskamm /* If tval isn't a valid pid, then fail. */
57194501b61Sskamm if ((tval > MAXPID) || (tval <= 0)) {
57294501b61Sskamm empty_rep_val_list(vals);
57394501b61Sskamm return (SCF_ERROR_CONSTRAINT_VIOLATED);
57494501b61Sskamm }
57594501b61Sskamm if (add_rep_val(vals, tval) == -1) {
57694501b61Sskamm empty_rep_val_list(vals);
57794501b61Sskamm return (SCF_ERROR_NO_MEMORY);
57894501b61Sskamm }
57994501b61Sskamm errno = 0;
58094501b61Sskamm }
58194501b61Sskamm /* EOF is ok when no errno */
58294501b61Sskamm if ((fscanf_ret != EOF) || (errno != 0)) {
58394501b61Sskamm empty_rep_val_list(vals);
58494501b61Sskamm return (SCF_ERROR_CONSTRAINT_VIOLATED);
58594501b61Sskamm }
58694501b61Sskamm if (fclose(tfp) != 0) {
58794501b61Sskamm /* for close failure just log a message */
58894501b61Sskamm warn_msg(gettext("Close of file %s failed."),
58994501b61Sskamm genfmri_filename);
59094501b61Sskamm }
5917c478bd9Sstevel@tonic-gate }
59294501b61Sskamm } else {
59394501b61Sskamm for (retries = 0; retries <= REP_OP_RETRIES; retries++) {
59494501b61Sskamm if (make_handle_bound(rep_handle) == -1) {
59594501b61Sskamm ret = scf_error();
59694501b61Sskamm break;
59794501b61Sskamm }
5987c478bd9Sstevel@tonic-gate
59994501b61Sskamm if ((ret = (store ? _store_rep_vals(vals, fmri, prop) :
60094501b61Sskamm _retrieve_rep_vals(vals, fmri, prop))) !=
60194501b61Sskamm SCF_ERROR_CONNECTION_BROKEN)
60294501b61Sskamm break;
6037c478bd9Sstevel@tonic-gate
60494501b61Sskamm (void) scf_handle_unbind(rep_handle);
60594501b61Sskamm }
6067c478bd9Sstevel@tonic-gate }
6077c478bd9Sstevel@tonic-gate
60894501b61Sskamm out:
6097c478bd9Sstevel@tonic-gate return (ret);
6107c478bd9Sstevel@tonic-gate }
6117c478bd9Sstevel@tonic-gate
6127c478bd9Sstevel@tonic-gate scf_error_t
store_rep_vals(uu_list_t * vals,const char * fmri,const char * prop)6137c478bd9Sstevel@tonic-gate store_rep_vals(uu_list_t *vals, const char *fmri, const char *prop)
6147c478bd9Sstevel@tonic-gate {
6157c478bd9Sstevel@tonic-gate return (store_retrieve_rep_vals(vals, fmri, prop, B_TRUE));
6167c478bd9Sstevel@tonic-gate }
6177c478bd9Sstevel@tonic-gate
6187c478bd9Sstevel@tonic-gate scf_error_t
retrieve_rep_vals(uu_list_t * vals,const char * fmri,const char * prop)6197c478bd9Sstevel@tonic-gate retrieve_rep_vals(uu_list_t *vals, const char *fmri, const char *prop)
6207c478bd9Sstevel@tonic-gate {
6217c478bd9Sstevel@tonic-gate return (store_retrieve_rep_vals(vals, fmri, prop, B_FALSE));
6227c478bd9Sstevel@tonic-gate }
6237c478bd9Sstevel@tonic-gate
6247c478bd9Sstevel@tonic-gate /*
62594501b61Sskamm * Adds/removes a contract id to/from the cached list kept in the instance.
62694501b61Sskamm * Then the cached list is written to a file named "ctid" in a directory
62794501b61Sskamm * based on the fmri. Cached list is written to a file due to scalability
62894501b61Sskamm * problems in libscf. The file "ctid" is used when inetd is restarted
62994501b61Sskamm * so that inetd can adopt the contracts that it had previously.
63094501b61Sskamm * Returns:
63194501b61Sskamm * 0 on success
63294501b61Sskamm * ENAMETOOLONG if unable to generate filename from fmri (including
63394501b61Sskamm * the inability to create the directory for the generated filename)
63494501b61Sskamm * ENOENT - failure accessing file
63594501b61Sskamm * ENOMEM - memory allocation failure
6367c478bd9Sstevel@tonic-gate */
63794501b61Sskamm int
add_remove_contract(instance_t * inst,boolean_t add,ctid_t ctid)63894501b61Sskamm add_remove_contract(instance_t *inst, boolean_t add, ctid_t ctid)
6397c478bd9Sstevel@tonic-gate {
64094501b61Sskamm FILE *tfp; /* temp fp */
64194501b61Sskamm int ret = 0;
64294501b61Sskamm int repval_ret = 0;
64394501b61Sskamm int fopen_retry_cnt = 2;
6447c478bd9Sstevel@tonic-gate
64594501b61Sskamm /*
64694501b61Sskamm * Storage performance of contract ids is important,
64794501b61Sskamm * so each instance has its own file. An add of a
64894501b61Sskamm * ctid will be appended to the ctid file.
64994501b61Sskamm * The removal of a ctid will result in the remaining
65094501b61Sskamm * ctids in the list being written to a temp file and then
65194501b61Sskamm * moved (renamed).
65294501b61Sskamm */
65394501b61Sskamm if (add) {
65494501b61Sskamm if (gen_filenms_from_fmri(inst->fmri, "ctid", genfmri_filename,
65594501b61Sskamm NULL) != 0) {
65694501b61Sskamm /* Failure either from fmri too long or mkdir failure */
65794501b61Sskamm return (ENAMETOOLONG);
65894501b61Sskamm }
6597c478bd9Sstevel@tonic-gate
66094501b61Sskamm retry_fopen:
66194501b61Sskamm if ((tfp = fopen(genfmri_filename, "a")) == NULL) {
66294501b61Sskamm if ((errno == EINTR) && (fopen_retry_cnt > 0)) {
66394501b61Sskamm fopen_retry_cnt--;
66494501b61Sskamm goto retry_fopen;
66594501b61Sskamm }
66694501b61Sskamm ret = ENOENT;
66794501b61Sskamm goto out;
66894501b61Sskamm }
6697c478bd9Sstevel@tonic-gate
67094501b61Sskamm /* Always store ctids as long long */
67194501b61Sskamm if (fprintf(tfp, "%llu\n", (uint64_t)ctid) <= 0) {
67294501b61Sskamm (void) fclose(tfp);
67394501b61Sskamm ret = ENOENT;
67494501b61Sskamm goto out;
6757c478bd9Sstevel@tonic-gate }
6767c478bd9Sstevel@tonic-gate
67794501b61Sskamm if (fclose(tfp) != 0) {
67894501b61Sskamm ret = ENOENT;
67994501b61Sskamm goto out;
68094501b61Sskamm }
6817c478bd9Sstevel@tonic-gate
68294501b61Sskamm if (add_rep_val(inst->start_ctids, ctid) != 0) {
68394501b61Sskamm ret = ENOMEM;
68494501b61Sskamm goto out;
68594501b61Sskamm }
68694501b61Sskamm } else {
68794501b61Sskamm remove_rep_val(inst->start_ctids, ctid);
68894501b61Sskamm
68994501b61Sskamm /* Write all values in list to file */
69094501b61Sskamm if ((repval_ret = repvals_to_file(inst->fmri, "ctid",
69194501b61Sskamm inst->start_ctids)) != 0) {
69294501b61Sskamm ret = repval_ret;
69394501b61Sskamm goto out;
69494501b61Sskamm }
6957c478bd9Sstevel@tonic-gate }
6967c478bd9Sstevel@tonic-gate
69794501b61Sskamm out:
69894501b61Sskamm return (ret);
6997c478bd9Sstevel@tonic-gate }
7007c478bd9Sstevel@tonic-gate
7017c478bd9Sstevel@tonic-gate /*
70294501b61Sskamm * If sig !=0, iterate over all contracts in the cached list of contract
70394501b61Sskamm * ids kept in the instance. Send each contract the specified signal.
70494501b61Sskamm * If sig == 0, read in the contract ids that were last associated
70594501b61Sskamm * with this instance (reload the cache) and call adopt_contract()
70694501b61Sskamm * to take ownership.
7077c478bd9Sstevel@tonic-gate *
70894501b61Sskamm * Returns 0 on success;
70994501b61Sskamm * ENAMETOOLONG if unable to generate filename from fmri (including
71094501b61Sskamm * the inability to create the directory for the generated filename) and
71194501b61Sskamm * ENXIO if a failure accessing the file
71294501b61Sskamm * ENOMEM if there was a memory allocation failure
71394501b61Sskamm * ENOENT if the instance, its restarter property group, or its
71494501b61Sskamm * contract property don't exist
71594501b61Sskamm * EIO if invalid data read from the file
7167c478bd9Sstevel@tonic-gate */
7177c478bd9Sstevel@tonic-gate int
iterate_repository_contracts(instance_t * inst,int sig)71894501b61Sskamm iterate_repository_contracts(instance_t *inst, int sig)
7197c478bd9Sstevel@tonic-gate {
7207c478bd9Sstevel@tonic-gate int ret = 0;
72194501b61Sskamm FILE *fp;
72294501b61Sskamm rep_val_t *spval = NULL; /* Contains a start_pid */
72394501b61Sskamm uint64_t tval; /* temp val holder */
72494501b61Sskamm uu_list_t *uup = NULL;
72594501b61Sskamm int fscanf_ret;
72694501b61Sskamm int fopen_retry_cnt = 2;
7277c478bd9Sstevel@tonic-gate
72894501b61Sskamm if (sig != 0) {
72994501b61Sskamm /*
73094501b61Sskamm * Send a signal to all in the contract; ESRCH just
73194501b61Sskamm * means they all exited before we could kill them
73294501b61Sskamm */
73394501b61Sskamm for (spval = uu_list_first(inst->start_ctids); spval != NULL;
73494501b61Sskamm spval = uu_list_next(inst->start_ctids, spval)) {
73594501b61Sskamm if (sigsend(P_CTID, (ctid_t)spval->val, sig) == -1 &&
73694501b61Sskamm errno != ESRCH) {
73794501b61Sskamm warn_msg(gettext("Unable to signal all "
73894501b61Sskamm "contract members of instance %s: %s"),
73994501b61Sskamm inst->fmri, strerror(errno));
7407c478bd9Sstevel@tonic-gate }
7417c478bd9Sstevel@tonic-gate }
74294501b61Sskamm return (0);
7437c478bd9Sstevel@tonic-gate }
7447c478bd9Sstevel@tonic-gate
74594501b61Sskamm /*
74694501b61Sskamm * sig == 0 case.
74794501b61Sskamm * Attempt to adopt the contract for each ctid.
74894501b61Sskamm */
74994501b61Sskamm if (gen_filenms_from_fmri(inst->fmri, "ctid", genfmri_filename,
75094501b61Sskamm NULL) != 0) {
75194501b61Sskamm /* Failure either from fmri too long or mkdir failure */
75294501b61Sskamm return (ENAMETOOLONG);
7537c478bd9Sstevel@tonic-gate }
7547c478bd9Sstevel@tonic-gate
75594501b61Sskamm retry_fopen:
75694501b61Sskamm /* It's ok if no file, there are no ctids to adopt */
75794501b61Sskamm if ((fp = fopen(genfmri_filename, "r")) == NULL) {
75894501b61Sskamm if ((errno == EINTR) && (fopen_retry_cnt > 0)) {
75994501b61Sskamm fopen_retry_cnt--;
76094501b61Sskamm goto retry_fopen;
7617c478bd9Sstevel@tonic-gate }
76294501b61Sskamm return (0);
7637c478bd9Sstevel@tonic-gate }
7647c478bd9Sstevel@tonic-gate
76594501b61Sskamm /*
76694501b61Sskamm * Read ctids from file into 2 lists:
76794501b61Sskamm * - temporary list to be traversed (uup)
76894501b61Sskamm * - cached list that can be modified if adoption of
76994501b61Sskamm * contract fails (inst->start_ctids).
77094501b61Sskamm * Always treat ctids as long longs.
77194501b61Sskamm */
77294501b61Sskamm uup = create_rep_val_list();
77394501b61Sskamm /* fscanf may not set errno, so clear it first */
77494501b61Sskamm errno = 0;
77594501b61Sskamm while ((fscanf_ret = fscanf(fp, "%llu", &tval)) == 1) {
77694501b61Sskamm /* If tval isn't a valid ctid, then fail. */
77794501b61Sskamm if (tval == 0) {
77894501b61Sskamm (void) fclose(fp);
77994501b61Sskamm ret = EIO;
7807c478bd9Sstevel@tonic-gate goto out;
7817c478bd9Sstevel@tonic-gate }
78294501b61Sskamm if ((add_rep_val(uup, tval) == -1) ||
78394501b61Sskamm (add_rep_val(inst->start_ctids, tval) == -1)) {
78494501b61Sskamm (void) fclose(fp);
78594501b61Sskamm ret = ENOMEM;
7867c478bd9Sstevel@tonic-gate goto out;
7877c478bd9Sstevel@tonic-gate }
78894501b61Sskamm errno = 0;
78994501b61Sskamm }
79094501b61Sskamm /* EOF is not a failure when no errno */
79194501b61Sskamm if ((fscanf_ret != EOF) || (errno != 0)) {
79294501b61Sskamm ret = EIO;
79394501b61Sskamm goto out;
7947c478bd9Sstevel@tonic-gate }
7957c478bd9Sstevel@tonic-gate
79694501b61Sskamm if (fclose(fp) != 0) {
79794501b61Sskamm ret = ENXIO;
79894501b61Sskamm goto out;
79994501b61Sskamm }
8007c478bd9Sstevel@tonic-gate
80194501b61Sskamm for (spval = uu_list_first(uup); spval != NULL;
80294501b61Sskamm spval = uu_list_next(uup, spval)) {
80394501b61Sskamm /* Try to adopt the contract */
80494501b61Sskamm if (adopt_contract((ctid_t)spval->val,
80594501b61Sskamm inst->fmri) != 0) {
8067c478bd9Sstevel@tonic-gate /*
80794501b61Sskamm * Adoption failed. No reason to think it'll
80894501b61Sskamm * work later, so remove the id from our list
80994501b61Sskamm * in the instance.
8107c478bd9Sstevel@tonic-gate */
81194501b61Sskamm remove_rep_val(inst->start_ctids, spval->val);
8127c478bd9Sstevel@tonic-gate }
8137c478bd9Sstevel@tonic-gate }
8147c478bd9Sstevel@tonic-gate out:
81594501b61Sskamm if (uup) {
81694501b61Sskamm empty_rep_val_list(uup);
81794501b61Sskamm destroy_rep_val_list(uup);
81894501b61Sskamm }
81994501b61Sskamm
82094501b61Sskamm if (ret != 0)
82194501b61Sskamm empty_rep_val_list(inst->start_ctids);
82294501b61Sskamm
8237c478bd9Sstevel@tonic-gate return (ret);
8247c478bd9Sstevel@tonic-gate }
825