xref: /titanic_54/usr/src/cmd/svc/svcs/svcs.c (revision b71850598a2db561ea000776fd51cc0bdc94c188)
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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  *
227c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * svcs - display attributes of service instances
307c478bd9Sstevel@tonic-gate  *
317c478bd9Sstevel@tonic-gate  * We have two output formats and six instance selection mechanisms.  The
327c478bd9Sstevel@tonic-gate  * primary output format is a line of attributes (selected by -o), possibly
337c478bd9Sstevel@tonic-gate  * followed by process description lines (if -p is specified), for each
347c478bd9Sstevel@tonic-gate  * instance selected.  The columns available to display are described by the
357c478bd9Sstevel@tonic-gate  * struct column columns array.  The columns to actually display are kept in
367c478bd9Sstevel@tonic-gate  * the opt_columns array as indicies into the columns array.  The selection
377c478bd9Sstevel@tonic-gate  * mechanisms available for this format are service FMRIs (selects all child
387c478bd9Sstevel@tonic-gate  * instances), instance FMRIs, instance FMRI glob patterns, instances with
397c478bd9Sstevel@tonic-gate  * a certain restarter (-R), dependencies of instances (-d), and dependents of
407c478bd9Sstevel@tonic-gate  * instances (-D).  Since the lines must be sorted (per -sS), we'll just stick
417c478bd9Sstevel@tonic-gate  * each into a data structure and print them in order when we're done.  To
427c478bd9Sstevel@tonic-gate  * avoid listing the same instance twice (when -d and -D aren't given), we'll
437c478bd9Sstevel@tonic-gate  * use a hash table of FMRIs to record that we've listed (added to the tree)
447c478bd9Sstevel@tonic-gate  * an instance.
457c478bd9Sstevel@tonic-gate  *
467c478bd9Sstevel@tonic-gate  * The secondary output format (-l "long") is a paragraph of text for the
477c478bd9Sstevel@tonic-gate  * services or instances selected.  Not needing to be sorted, it's implemented
487c478bd9Sstevel@tonic-gate  * by just calling print_detailed() for each FMRI given.
497c478bd9Sstevel@tonic-gate  */
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate #include "svcs.h"
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate /* Get the byteorder macros to ease sorting. */
547c478bd9Sstevel@tonic-gate #include <sys/types.h>
557c478bd9Sstevel@tonic-gate #include <netinet/in.h>
567c478bd9Sstevel@tonic-gate #include <inttypes.h>
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate #include <sys/contract.h>
597c478bd9Sstevel@tonic-gate #include <sys/ctfs.h>
607c478bd9Sstevel@tonic-gate #include <sys/stat.h>
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate #include <assert.h>
637c478bd9Sstevel@tonic-gate #include <ctype.h>
647c478bd9Sstevel@tonic-gate #include <errno.h>
657c478bd9Sstevel@tonic-gate #include <fcntl.h>
667c478bd9Sstevel@tonic-gate #include <fnmatch.h>
677c478bd9Sstevel@tonic-gate #include <libcontract.h>
687c478bd9Sstevel@tonic-gate #include <libcontract_priv.h>
697c478bd9Sstevel@tonic-gate #include <libintl.h>
707c478bd9Sstevel@tonic-gate #include <libscf.h>
717c478bd9Sstevel@tonic-gate #include <libscf_priv.h>
727c478bd9Sstevel@tonic-gate #include <libuutil.h>
737c478bd9Sstevel@tonic-gate #include <locale.h>
747c478bd9Sstevel@tonic-gate #include <procfs.h>
757c478bd9Sstevel@tonic-gate #include <stdarg.h>
767c478bd9Sstevel@tonic-gate #include <stdio.h>
777c478bd9Sstevel@tonic-gate #include <stdlib.h>
787c478bd9Sstevel@tonic-gate #include <strings.h>
797c478bd9Sstevel@tonic-gate #include <time.h>
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate #ifndef TEXT_DOMAIN
837c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SUNW_OST_OSCMD"
847c478bd9Sstevel@tonic-gate #endif /* TEXT_DOMAIN */
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate #define	LEGACY_SCHEME	"lrc:"
877c478bd9Sstevel@tonic-gate #define	LEGACY_UNKNOWN	"unknown"
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate /* Flags for pg_get_single_val() */
907c478bd9Sstevel@tonic-gate #define	EMPTY_OK	0x01
917c478bd9Sstevel@tonic-gate #define	MULTI_OK	0x02
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate /*
957c478bd9Sstevel@tonic-gate  * An AVL-storable node for output lines and the keys to sort them by.
967c478bd9Sstevel@tonic-gate  */
977c478bd9Sstevel@tonic-gate struct avl_string {
987c478bd9Sstevel@tonic-gate 	uu_avl_node_t	node;
997c478bd9Sstevel@tonic-gate 	char		*key;
1007c478bd9Sstevel@tonic-gate 	char		*str;
1017c478bd9Sstevel@tonic-gate };
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate /*
1047c478bd9Sstevel@tonic-gate  * For lists of parsed restarter FMRIs.
1057c478bd9Sstevel@tonic-gate  */
1067c478bd9Sstevel@tonic-gate struct pfmri_list {
1077c478bd9Sstevel@tonic-gate 	const char		*scope;
1087c478bd9Sstevel@tonic-gate 	const char		*service;
1097c478bd9Sstevel@tonic-gate 	const char		*instance;
1107c478bd9Sstevel@tonic-gate 	struct pfmri_list	*next;
1117c478bd9Sstevel@tonic-gate };
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate /*
1157c478bd9Sstevel@tonic-gate  * Globals
1167c478bd9Sstevel@tonic-gate  */
1177c478bd9Sstevel@tonic-gate scf_handle_t *h;
1187c478bd9Sstevel@tonic-gate static scf_propertygroup_t *g_pg;
1197c478bd9Sstevel@tonic-gate static scf_property_t *g_prop;
1207c478bd9Sstevel@tonic-gate static scf_value_t *g_val;
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate static size_t line_sz;			/* Bytes in the header line. */
1237c478bd9Sstevel@tonic-gate static size_t sortkey_sz;		/* Bytes in sort keys. */
1247c478bd9Sstevel@tonic-gate static uu_avl_pool_t *lines_pool;
1257c478bd9Sstevel@tonic-gate static uu_avl_t *lines;			/* Output lines. */
1267c478bd9Sstevel@tonic-gate int exit_status;
1277c478bd9Sstevel@tonic-gate ssize_t max_scf_name_length;
1287c478bd9Sstevel@tonic-gate ssize_t max_scf_value_length;
1297c478bd9Sstevel@tonic-gate ssize_t max_scf_fmri_length;
1307c478bd9Sstevel@tonic-gate static time_t now;
1317c478bd9Sstevel@tonic-gate static struct pfmri_list *restarters = NULL;
1327c478bd9Sstevel@tonic-gate static int first_paragraph = 1;		/* For -l mode. */
1337c478bd9Sstevel@tonic-gate static char *common_name_buf;		/* Sized for maximal length value. */
1347c478bd9Sstevel@tonic-gate char *locale;				/* Current locale. */
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate /* Options */
1377c478bd9Sstevel@tonic-gate static int *opt_columns = NULL;		/* Indices into columns to display. */
1387c478bd9Sstevel@tonic-gate static int opt_cnum = 0;
1397c478bd9Sstevel@tonic-gate static int opt_processes = 0;		/* Print processes? */
1407c478bd9Sstevel@tonic-gate static int *opt_sort = NULL;		/* Indices into columns to sort. */
1417c478bd9Sstevel@tonic-gate static int opt_snum = 0;
1427c478bd9Sstevel@tonic-gate static int opt_nstate_shown = 0;	/* Will nstate be shown? */
1437c478bd9Sstevel@tonic-gate static int opt_verbose = 0;
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate /* Minimize string constants. */
1467c478bd9Sstevel@tonic-gate static const char * const scf_property_state = SCF_PROPERTY_STATE;
1477c478bd9Sstevel@tonic-gate static const char * const scf_property_next_state = SCF_PROPERTY_NEXT_STATE;
1487c478bd9Sstevel@tonic-gate static const char * const scf_property_contract = SCF_PROPERTY_CONTRACT;
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate /*
1527c478bd9Sstevel@tonic-gate  * Utility functions
1537c478bd9Sstevel@tonic-gate  */
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate /*
1567c478bd9Sstevel@tonic-gate  * For unexpected libscf errors.  The ending newline is necessary to keep
1577c478bd9Sstevel@tonic-gate  * uu_die() from appending the errno error.
1587c478bd9Sstevel@tonic-gate  */
1597c478bd9Sstevel@tonic-gate #ifndef NDEBUG
1607c478bd9Sstevel@tonic-gate void
1617c478bd9Sstevel@tonic-gate do_scfdie(const char *file, int line)
1627c478bd9Sstevel@tonic-gate {
1637c478bd9Sstevel@tonic-gate 	uu_die(gettext("%s:%d: Unexpected libscf error: %s.  Exiting.\n"),
1647c478bd9Sstevel@tonic-gate 	    file, line, scf_strerror(scf_error()));
1657c478bd9Sstevel@tonic-gate }
1667c478bd9Sstevel@tonic-gate #else
1677c478bd9Sstevel@tonic-gate void
1687c478bd9Sstevel@tonic-gate scfdie(void)
1697c478bd9Sstevel@tonic-gate {
1707c478bd9Sstevel@tonic-gate 	uu_die(gettext("Unexpected libscf error: %s.  Exiting.\n"),
1717c478bd9Sstevel@tonic-gate 	    scf_strerror(scf_error()));
1727c478bd9Sstevel@tonic-gate }
1737c478bd9Sstevel@tonic-gate #endif
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate void *
1767c478bd9Sstevel@tonic-gate safe_malloc(size_t sz)
1777c478bd9Sstevel@tonic-gate {
1787c478bd9Sstevel@tonic-gate 	void *ptr;
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	ptr = malloc(sz);
1817c478bd9Sstevel@tonic-gate 	if (ptr == NULL)
1827c478bd9Sstevel@tonic-gate 		uu_die(gettext("Out of memory"));
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	return (ptr);
1857c478bd9Sstevel@tonic-gate }
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate char *
1887c478bd9Sstevel@tonic-gate safe_strdup(const char *str)
1897c478bd9Sstevel@tonic-gate {
1907c478bd9Sstevel@tonic-gate 	char *cp;
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	cp = strdup(str);
1937c478bd9Sstevel@tonic-gate 	if (cp == NULL)
1947c478bd9Sstevel@tonic-gate 		uu_die(gettext("Out of memory.\n"));
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	return (cp);
1977c478bd9Sstevel@tonic-gate }
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate static void
2007c478bd9Sstevel@tonic-gate sanitize_locale(char *locale)
2017c478bd9Sstevel@tonic-gate {
2027c478bd9Sstevel@tonic-gate 	for (; *locale != '\0'; locale++)
2037c478bd9Sstevel@tonic-gate 		if (!isalnum(*locale))
2047c478bd9Sstevel@tonic-gate 			*locale = '_';
2057c478bd9Sstevel@tonic-gate }
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate /*
2087c478bd9Sstevel@tonic-gate  * FMRI hashtable.  For uniquifing listings.
2097c478bd9Sstevel@tonic-gate  */
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate struct ht_elem {
2127c478bd9Sstevel@tonic-gate 	const char	*fmri;
2137c478bd9Sstevel@tonic-gate 	struct ht_elem	*next;
2147c478bd9Sstevel@tonic-gate };
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate static struct ht_elem	**ht_buckets;
2177c478bd9Sstevel@tonic-gate static uint_t		ht_buckets_num;
2187c478bd9Sstevel@tonic-gate static uint_t		ht_num;
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate static void
2217c478bd9Sstevel@tonic-gate ht_init()
2227c478bd9Sstevel@tonic-gate {
2237c478bd9Sstevel@tonic-gate 	ht_buckets_num = 8;
2247c478bd9Sstevel@tonic-gate 	ht_buckets = safe_malloc(sizeof (*ht_buckets) * ht_buckets_num);
2257c478bd9Sstevel@tonic-gate 	bzero(ht_buckets, sizeof (*ht_buckets) * ht_buckets_num);
2267c478bd9Sstevel@tonic-gate 	ht_num = 0;
2277c478bd9Sstevel@tonic-gate }
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate static uint_t
2307c478bd9Sstevel@tonic-gate ht_hash_fmri(const char *fmri)
2317c478bd9Sstevel@tonic-gate {
2327c478bd9Sstevel@tonic-gate 	uint_t h = 0, g;
2337c478bd9Sstevel@tonic-gate 	const char *p, *k;
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	/* All FMRIs begin with svc:/, so skip that part. */
2367c478bd9Sstevel@tonic-gate 	assert(strncmp(fmri, "svc:/", sizeof ("svc:/") - 1) == 0);
2377c478bd9Sstevel@tonic-gate 	k = fmri + sizeof ("svc:/") - 1;
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	/*
2407c478bd9Sstevel@tonic-gate 	 * Generic hash function from uts/common/os/modhash.c.
2417c478bd9Sstevel@tonic-gate 	 */
2427c478bd9Sstevel@tonic-gate 	for (p = k; *p != '\0'; ++p) {
2437c478bd9Sstevel@tonic-gate 		h = (h << 4) + *p;
2447c478bd9Sstevel@tonic-gate 		if ((g = (h & 0xf0000000)) != 0) {
2457c478bd9Sstevel@tonic-gate 			h ^= (g >> 24);
2467c478bd9Sstevel@tonic-gate 			h ^= g;
2477c478bd9Sstevel@tonic-gate 		}
2487c478bd9Sstevel@tonic-gate 	}
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	return (h);
2517c478bd9Sstevel@tonic-gate }
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate static void
2547c478bd9Sstevel@tonic-gate ht_grow()
2557c478bd9Sstevel@tonic-gate {
2567c478bd9Sstevel@tonic-gate 	uint_t new_ht_buckets_num;
2577c478bd9Sstevel@tonic-gate 	struct ht_elem **new_ht_buckets;
2587c478bd9Sstevel@tonic-gate 	int i;
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 	new_ht_buckets_num = ht_buckets_num * 2;
2617c478bd9Sstevel@tonic-gate 	assert(new_ht_buckets_num > ht_buckets_num);
2627c478bd9Sstevel@tonic-gate 	new_ht_buckets =
2637c478bd9Sstevel@tonic-gate 	    safe_malloc(sizeof (*new_ht_buckets) * new_ht_buckets_num);
2647c478bd9Sstevel@tonic-gate 	bzero(new_ht_buckets, sizeof (*new_ht_buckets) * new_ht_buckets_num);
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	for (i = 0; i < ht_buckets_num; ++i) {
2677c478bd9Sstevel@tonic-gate 		struct ht_elem *elem, *next;
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 		for (elem = ht_buckets[i]; elem != NULL; elem = next) {
2707c478bd9Sstevel@tonic-gate 			uint_t h;
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 			next = elem->next;
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 			h = ht_hash_fmri(elem->fmri);
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 			elem->next =
2777c478bd9Sstevel@tonic-gate 			    new_ht_buckets[h & (new_ht_buckets_num - 1)];
2787c478bd9Sstevel@tonic-gate 			new_ht_buckets[h & (new_ht_buckets_num - 1)] = elem;
2797c478bd9Sstevel@tonic-gate 		}
2807c478bd9Sstevel@tonic-gate 	}
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 	free(ht_buckets);
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 	ht_buckets = new_ht_buckets;
2857c478bd9Sstevel@tonic-gate 	ht_buckets_num = new_ht_buckets_num;
2867c478bd9Sstevel@tonic-gate }
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate /*
2897c478bd9Sstevel@tonic-gate  * Add an FMRI to the hash table.  Returns 1 if it was already there,
2907c478bd9Sstevel@tonic-gate  * 0 otherwise.
2917c478bd9Sstevel@tonic-gate  */
2927c478bd9Sstevel@tonic-gate static int
2937c478bd9Sstevel@tonic-gate ht_add(const char *fmri)
2947c478bd9Sstevel@tonic-gate {
2957c478bd9Sstevel@tonic-gate 	uint_t h;
2967c478bd9Sstevel@tonic-gate 	struct ht_elem *elem;
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	h = ht_hash_fmri(fmri);
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 	elem = ht_buckets[h & (ht_buckets_num - 1)];
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 	for (; elem != NULL; elem = elem->next) {
3037c478bd9Sstevel@tonic-gate 		if (strcmp(elem->fmri, fmri) == 0)
3047c478bd9Sstevel@tonic-gate 			return (1);
3057c478bd9Sstevel@tonic-gate 	}
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 	/* Grow when average chain length is over 3. */
3087c478bd9Sstevel@tonic-gate 	if (ht_num > 3 * ht_buckets_num)
3097c478bd9Sstevel@tonic-gate 		ht_grow();
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	++ht_num;
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	elem = safe_malloc(sizeof (*elem));
3147c478bd9Sstevel@tonic-gate 	elem->fmri = strdup(fmri);
3157c478bd9Sstevel@tonic-gate 	elem->next = ht_buckets[h & (ht_buckets_num - 1)];
3167c478bd9Sstevel@tonic-gate 	ht_buckets[h & (ht_buckets_num - 1)] = elem;
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 	return (0);
3197c478bd9Sstevel@tonic-gate }
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate /*
3247c478bd9Sstevel@tonic-gate  * Convenience libscf wrapper functions.
3257c478bd9Sstevel@tonic-gate  */
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate /*
3287c478bd9Sstevel@tonic-gate  * Get the single value of the named property in the given property group,
3297c478bd9Sstevel@tonic-gate  * which must have type ty, and put it in *vp.  If ty is SCF_TYPE_ASTRING, vp
3307c478bd9Sstevel@tonic-gate  * is taken to be a char **, and sz is the size of the buffer.  sz is unused
3317c478bd9Sstevel@tonic-gate  * otherwise.  Return 0 on success, -1 if the property doesn't exist, has the
3327c478bd9Sstevel@tonic-gate  * wrong type, or doesn't have a single value.  If flags has EMPTY_OK, don't
3337c478bd9Sstevel@tonic-gate  * complain if the property has no values (but return nonzero).  If flags has
3347c478bd9Sstevel@tonic-gate  * MULTI_OK and the property has multiple values, succeed with E2BIG.
3357c478bd9Sstevel@tonic-gate  */
3367c478bd9Sstevel@tonic-gate int
3377c478bd9Sstevel@tonic-gate pg_get_single_val(scf_propertygroup_t *pg, const char *propname, scf_type_t ty,
3387c478bd9Sstevel@tonic-gate     void *vp, size_t sz, uint_t flags)
3397c478bd9Sstevel@tonic-gate {
3407c478bd9Sstevel@tonic-gate 	char *buf;
3417c478bd9Sstevel@tonic-gate 	size_t buf_sz;
3427c478bd9Sstevel@tonic-gate 	int ret = -1, r;
3437c478bd9Sstevel@tonic-gate 	boolean_t multi = B_FALSE;
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 	assert((flags & ~(EMPTY_OK | MULTI_OK)) == 0);
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 	if (scf_pg_get_property(pg, propname, g_prop) == -1) {
3487c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_NOT_FOUND)
3497c478bd9Sstevel@tonic-gate 			scfdie();
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 		goto out;
3527c478bd9Sstevel@tonic-gate 	}
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 	if (scf_property_is_type(g_prop, ty) != SCF_SUCCESS) {
3557c478bd9Sstevel@tonic-gate 		if (scf_error() == SCF_ERROR_TYPE_MISMATCH)
3567c478bd9Sstevel@tonic-gate 			goto misconfigured;
3577c478bd9Sstevel@tonic-gate 		scfdie();
3587c478bd9Sstevel@tonic-gate 	}
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	if (scf_property_get_value(g_prop, g_val) != SCF_SUCCESS) {
3617c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
3627c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
3637c478bd9Sstevel@tonic-gate 			if (flags & EMPTY_OK)
3647c478bd9Sstevel@tonic-gate 				goto out;
3657c478bd9Sstevel@tonic-gate 			goto misconfigured;
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
3687c478bd9Sstevel@tonic-gate 			if (flags & MULTI_OK) {
3697c478bd9Sstevel@tonic-gate 				multi = B_TRUE;
3707c478bd9Sstevel@tonic-gate 				break;
3717c478bd9Sstevel@tonic-gate 			}
3727c478bd9Sstevel@tonic-gate 			goto misconfigured;
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 		default:
3757c478bd9Sstevel@tonic-gate 			scfdie();
3767c478bd9Sstevel@tonic-gate 		}
3777c478bd9Sstevel@tonic-gate 	}
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	switch (ty) {
3807c478bd9Sstevel@tonic-gate 	case SCF_TYPE_ASTRING:
3817c478bd9Sstevel@tonic-gate 		r = scf_value_get_astring(g_val, vp, sz) > 0 ? SCF_SUCCESS : -1;
3827c478bd9Sstevel@tonic-gate 		break;
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 	case SCF_TYPE_BOOLEAN:
3857c478bd9Sstevel@tonic-gate 		r = scf_value_get_boolean(g_val, (uint8_t *)vp);
3867c478bd9Sstevel@tonic-gate 		break;
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 	case SCF_TYPE_COUNT:
3897c478bd9Sstevel@tonic-gate 		r = scf_value_get_count(g_val, (uint64_t *)vp);
3907c478bd9Sstevel@tonic-gate 		break;
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 	case SCF_TYPE_INTEGER:
3937c478bd9Sstevel@tonic-gate 		r = scf_value_get_integer(g_val, (int64_t *)vp);
3947c478bd9Sstevel@tonic-gate 		break;
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 	case SCF_TYPE_TIME: {
3977c478bd9Sstevel@tonic-gate 		int64_t sec;
3987c478bd9Sstevel@tonic-gate 		int32_t ns;
3997c478bd9Sstevel@tonic-gate 		r = scf_value_get_time(g_val, &sec, &ns);
4007c478bd9Sstevel@tonic-gate 		((struct timeval *)vp)->tv_sec = sec;
4017c478bd9Sstevel@tonic-gate 		((struct timeval *)vp)->tv_usec = ns / 1000;
4027c478bd9Sstevel@tonic-gate 		break;
4037c478bd9Sstevel@tonic-gate 	}
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 	case SCF_TYPE_USTRING:
4067c478bd9Sstevel@tonic-gate 		r = scf_value_get_ustring(g_val, vp, sz) > 0 ? SCF_SUCCESS : -1;
4077c478bd9Sstevel@tonic-gate 		break;
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 	default:
4107c478bd9Sstevel@tonic-gate #ifndef NDEBUG
4117c478bd9Sstevel@tonic-gate 		uu_warn("%s:%d: Unknown type %d.\n", __FILE__, __LINE__, ty);
4127c478bd9Sstevel@tonic-gate #endif
4137c478bd9Sstevel@tonic-gate 		abort();
4147c478bd9Sstevel@tonic-gate 	}
4157c478bd9Sstevel@tonic-gate 	if (r != SCF_SUCCESS)
4167c478bd9Sstevel@tonic-gate 		scfdie();
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	ret = multi ? E2BIG : 0;
4197c478bd9Sstevel@tonic-gate 	goto out;
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate misconfigured:
4227c478bd9Sstevel@tonic-gate 	buf_sz = max_scf_fmri_length + 1;
4237c478bd9Sstevel@tonic-gate 	buf = safe_malloc(buf_sz);
4247c478bd9Sstevel@tonic-gate 	if (scf_property_to_fmri(g_prop, buf, buf_sz) == -1)
4257c478bd9Sstevel@tonic-gate 		scfdie();
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 	uu_warn(gettext("Property \"%s\" is misconfigured.\n"), buf);
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 	free(buf);
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate out:
4327c478bd9Sstevel@tonic-gate 	return (ret);
4337c478bd9Sstevel@tonic-gate }
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate static scf_snapshot_t *
4367c478bd9Sstevel@tonic-gate get_running_snapshot(scf_instance_t *inst)
4377c478bd9Sstevel@tonic-gate {
4387c478bd9Sstevel@tonic-gate 	scf_snapshot_t *snap;
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 	snap = scf_snapshot_create(h);
4417c478bd9Sstevel@tonic-gate 	if (snap == NULL)
4427c478bd9Sstevel@tonic-gate 		scfdie();
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 	if (scf_instance_get_snapshot(inst, "running", snap) == 0)
4457c478bd9Sstevel@tonic-gate 		return (snap);
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate 	if (scf_error() != SCF_ERROR_NOT_FOUND)
4487c478bd9Sstevel@tonic-gate 		scfdie();
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 	scf_snapshot_destroy(snap);
4517c478bd9Sstevel@tonic-gate 	return (NULL);
4527c478bd9Sstevel@tonic-gate }
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate /*
4557c478bd9Sstevel@tonic-gate  * As pg_get_single_val(), except look the property group up in an
4567c478bd9Sstevel@tonic-gate  * instance.  If "use_running" is set, and the running snapshot exists,
4577c478bd9Sstevel@tonic-gate  * do a composed lookup there.  Otherwise, do an (optionally composed)
4587c478bd9Sstevel@tonic-gate  * lookup on the current values.  Note that lookups using snapshots are
4597c478bd9Sstevel@tonic-gate  * always composed.
4607c478bd9Sstevel@tonic-gate  */
4617c478bd9Sstevel@tonic-gate int
4627c478bd9Sstevel@tonic-gate inst_get_single_val(scf_instance_t *inst, const char *pgname,
4637c478bd9Sstevel@tonic-gate     const char *propname, scf_type_t ty, void *vp, size_t sz, uint_t flags,
4647c478bd9Sstevel@tonic-gate     int use_running, int composed)
4657c478bd9Sstevel@tonic-gate {
4667c478bd9Sstevel@tonic-gate 	scf_snapshot_t *snap = NULL;
4677c478bd9Sstevel@tonic-gate 	int r;
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 	if (use_running)
4707c478bd9Sstevel@tonic-gate 		snap = get_running_snapshot(inst);
4717c478bd9Sstevel@tonic-gate 	if (composed || use_running)
4727c478bd9Sstevel@tonic-gate 		r = scf_instance_get_pg_composed(inst, snap, pgname, g_pg);
4737c478bd9Sstevel@tonic-gate 	else
4747c478bd9Sstevel@tonic-gate 		r = scf_instance_get_pg(inst, pgname, g_pg);
4757c478bd9Sstevel@tonic-gate 	if (snap)
4767c478bd9Sstevel@tonic-gate 		scf_snapshot_destroy(snap);
4777c478bd9Sstevel@tonic-gate 	if (r == -1)
4787c478bd9Sstevel@tonic-gate 		return (-1);
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 	r = pg_get_single_val(g_pg, propname, ty, vp, sz, flags);
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate 	return (r);
4837c478bd9Sstevel@tonic-gate }
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate static int
4867c478bd9Sstevel@tonic-gate instance_enabled(scf_instance_t *inst, boolean_t temp)
4877c478bd9Sstevel@tonic-gate {
4887c478bd9Sstevel@tonic-gate 	uint8_t b;
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate 	if (inst_get_single_val(inst,
4917c478bd9Sstevel@tonic-gate 	    temp ? SCF_PG_GENERAL_OVR : SCF_PG_GENERAL, SCF_PROPERTY_ENABLED,
4927c478bd9Sstevel@tonic-gate 	    SCF_TYPE_BOOLEAN, &b, 0, 0, 0, 0) != 0)
4937c478bd9Sstevel@tonic-gate 		return (-1);
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate 	return (b ? 1 : 0);
4967c478bd9Sstevel@tonic-gate }
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate /*
4997c478bd9Sstevel@tonic-gate  * Get a string property from the restarter property group of the given
5007c478bd9Sstevel@tonic-gate  * instance.  Return an empty string on normal problems.
5017c478bd9Sstevel@tonic-gate  */
5027c478bd9Sstevel@tonic-gate static void
5037c478bd9Sstevel@tonic-gate get_restarter_string_prop(scf_instance_t *inst, const char *pname,
5047c478bd9Sstevel@tonic-gate     char *buf, size_t buf_sz)
5057c478bd9Sstevel@tonic-gate {
5067c478bd9Sstevel@tonic-gate 	if (inst_get_single_val(inst, SCF_PG_RESTARTER, pname,
5077c478bd9Sstevel@tonic-gate 	    SCF_TYPE_ASTRING, buf, buf_sz, 0, 0, 1) != 0)
5087c478bd9Sstevel@tonic-gate 		*buf = '\0';
5097c478bd9Sstevel@tonic-gate }
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate static int
5127c478bd9Sstevel@tonic-gate get_restarter_time_prop(scf_instance_t *inst, const char *pname,
5137c478bd9Sstevel@tonic-gate     struct timeval *tvp, int ok_if_empty)
5147c478bd9Sstevel@tonic-gate {
5157c478bd9Sstevel@tonic-gate 	int r;
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 	r = inst_get_single_val(inst, SCF_PG_RESTARTER, pname, SCF_TYPE_TIME,
5187c478bd9Sstevel@tonic-gate 	    tvp, NULL, ok_if_empty ? EMPTY_OK : 0, 0, 1);
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 	return (r == 0 ? 0 : -1);
5217c478bd9Sstevel@tonic-gate }
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate static int
5247c478bd9Sstevel@tonic-gate get_restarter_count_prop(scf_instance_t *inst, const char *pname, uint64_t *cp,
5257c478bd9Sstevel@tonic-gate     uint_t flags)
5267c478bd9Sstevel@tonic-gate {
5277c478bd9Sstevel@tonic-gate 	return (inst_get_single_val(inst, SCF_PG_RESTARTER, pname,
5287c478bd9Sstevel@tonic-gate 	    SCF_TYPE_COUNT, cp, 0, flags, 0, 1));
5297c478bd9Sstevel@tonic-gate }
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate /*
5337c478bd9Sstevel@tonic-gate  * Generic functions
5347c478bd9Sstevel@tonic-gate  */
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate static int
5377c478bd9Sstevel@tonic-gate propvals_to_pids(scf_propertygroup_t *pg, const char *pname, pid_t **pidsp,
5387c478bd9Sstevel@tonic-gate     uint_t *np, scf_property_t *prop, scf_value_t *val, scf_iter_t *iter)
5397c478bd9Sstevel@tonic-gate {
5407c478bd9Sstevel@tonic-gate 	scf_type_t ty;
5417c478bd9Sstevel@tonic-gate 	int r, fd, err;
5427c478bd9Sstevel@tonic-gate 	uint64_t c;
5437c478bd9Sstevel@tonic-gate 	ct_stathdl_t ctst;
5447c478bd9Sstevel@tonic-gate 	pid_t *pids;
5457c478bd9Sstevel@tonic-gate 	uint_t m;
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 	if (scf_pg_get_property(pg, pname, prop) != 0) {
5487c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_NOT_FOUND)
5497c478bd9Sstevel@tonic-gate 			scfdie();
5507c478bd9Sstevel@tonic-gate 
5517c478bd9Sstevel@tonic-gate 		return (ENOENT);
5527c478bd9Sstevel@tonic-gate 	}
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	if (scf_property_type(prop, &ty) != 0)
5557c478bd9Sstevel@tonic-gate 		scfdie();
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 	if (ty != SCF_TYPE_COUNT)
5587c478bd9Sstevel@tonic-gate 		return (EINVAL);
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 	if (scf_iter_property_values(iter, prop) != 0)
5617c478bd9Sstevel@tonic-gate 		scfdie();
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 	for (;;) {
5647c478bd9Sstevel@tonic-gate 		r = scf_iter_next_value(iter, val);
5657c478bd9Sstevel@tonic-gate 		if (r == -1)
5667c478bd9Sstevel@tonic-gate 			scfdie();
5677c478bd9Sstevel@tonic-gate 		if (r == 0)
5687c478bd9Sstevel@tonic-gate 			break;
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate 		if (scf_value_get_count(val, &c) != 0)
5717c478bd9Sstevel@tonic-gate 			scfdie();
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 		fd = contract_open(c, NULL, "status", O_RDONLY);
5747c478bd9Sstevel@tonic-gate 		if (fd < 0)
5757c478bd9Sstevel@tonic-gate 			continue;
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 		err = ct_status_read(fd, CTD_ALL, &ctst);
5787c478bd9Sstevel@tonic-gate 		if (err != 0) {
5797c478bd9Sstevel@tonic-gate 			uu_warn(gettext("Could not read status of contract "
5807c478bd9Sstevel@tonic-gate 			    "%ld: %s.\n"), c, strerror(err));
5817c478bd9Sstevel@tonic-gate 			(void) close(fd);
5827c478bd9Sstevel@tonic-gate 			continue;
5837c478bd9Sstevel@tonic-gate 		}
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate 		(void) close(fd);
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate 		r = ct_pr_status_get_members(ctst, &pids, &m);
5887c478bd9Sstevel@tonic-gate 		assert(r == 0);
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate 		if (m == 0) {
5917c478bd9Sstevel@tonic-gate 			ct_status_free(ctst);
5927c478bd9Sstevel@tonic-gate 			continue;
5937c478bd9Sstevel@tonic-gate 		}
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 		*pidsp = realloc(*pidsp, (*np + m) * sizeof (*pidsp));
5967c478bd9Sstevel@tonic-gate 		if (*pidsp == NULL)
5977c478bd9Sstevel@tonic-gate 			uu_die(gettext("Out of memory"));
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate 		bcopy(pids, *pidsp + *np, m * sizeof (*pids));
6007c478bd9Sstevel@tonic-gate 		*np += m;
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate 		ct_status_free(ctst);
6037c478bd9Sstevel@tonic-gate 	}
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate 	return (0);
6067c478bd9Sstevel@tonic-gate }
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate static int
6097c478bd9Sstevel@tonic-gate instance_processes(scf_instance_t *inst, pid_t **pids, uint_t *np)
6107c478bd9Sstevel@tonic-gate {
6117c478bd9Sstevel@tonic-gate 	scf_iter_t *iter;
6127c478bd9Sstevel@tonic-gate 	int ret;
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 	if ((iter = scf_iter_create(h)) == NULL)
6157c478bd9Sstevel@tonic-gate 		scfdie();
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate 	if (scf_instance_get_pg(inst, SCF_PG_RESTARTER, g_pg) == 0) {
6187c478bd9Sstevel@tonic-gate 		*pids = NULL;
6197c478bd9Sstevel@tonic-gate 		*np = 0;
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 		(void) propvals_to_pids(g_pg, scf_property_contract, pids, np,
6227c478bd9Sstevel@tonic-gate 		    g_prop, g_val, iter);
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate 		(void) propvals_to_pids(g_pg, SCF_PROPERTY_TRANSIENT_CONTRACT,
6257c478bd9Sstevel@tonic-gate 		    pids, np, g_prop, g_val, iter);
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate 		ret = 0;
6287c478bd9Sstevel@tonic-gate 	} else {
6297c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_NOT_FOUND)
6307c478bd9Sstevel@tonic-gate 			scfdie();
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate 		ret = -1;
6337c478bd9Sstevel@tonic-gate 	}
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate 	scf_iter_destroy(iter);
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 	return (ret);
6387c478bd9Sstevel@tonic-gate }
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate static int
6417c478bd9Sstevel@tonic-gate get_psinfo(pid_t pid, psinfo_t *psip)
6427c478bd9Sstevel@tonic-gate {
6437c478bd9Sstevel@tonic-gate 	char path[100];
6447c478bd9Sstevel@tonic-gate 	int fd;
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "/proc/%lu/psinfo", pid);
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 	fd = open64(path, O_RDONLY);
6497c478bd9Sstevel@tonic-gate 	if (fd < 0)
6507c478bd9Sstevel@tonic-gate 		return (-1);
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate 	if (read(fd, psip, sizeof (*psip)) < 0)
6537c478bd9Sstevel@tonic-gate 		uu_die(gettext("Could not read info for process %lu"), pid);
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate 	(void) close(fd);
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 	return (0);
6587c478bd9Sstevel@tonic-gate }
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate /*
6637c478bd9Sstevel@tonic-gate  * Column sprint and sortkey functions
6647c478bd9Sstevel@tonic-gate  */
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate struct column {
6677c478bd9Sstevel@tonic-gate 	const char *name;
6687c478bd9Sstevel@tonic-gate 	int width;
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 	/*
6717c478bd9Sstevel@tonic-gate 	 * This function should write the value for the column into buf, and
6727c478bd9Sstevel@tonic-gate 	 * grow or allocate buf accordingly.  It should always write at least
6737c478bd9Sstevel@tonic-gate 	 * width bytes, blanking unused bytes with spaces.  If the field is
6747c478bd9Sstevel@tonic-gate 	 * greater than the column width we allow it to overlap other columns.
6757c478bd9Sstevel@tonic-gate 	 * In particular, it shouldn't write any null bytes.  (Though an extra
6767c478bd9Sstevel@tonic-gate 	 * null byte past the end is currently tolerated.)  If the property
6777c478bd9Sstevel@tonic-gate 	 * group is non-NULL, then we are dealing with a legacy service.
6787c478bd9Sstevel@tonic-gate 	 */
6797c478bd9Sstevel@tonic-gate 	void (*sprint)(char **, scf_walkinfo_t *);
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate 	int sortkey_width;
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate 	/*
6847c478bd9Sstevel@tonic-gate 	 * This function should write sortkey_width bytes into buf which will
6857c478bd9Sstevel@tonic-gate 	 * cause memcmp() to sort it properly.  (Unlike sprint() above,
6867c478bd9Sstevel@tonic-gate 	 * however, an extra null byte may overrun the buffer.)  The second
6877c478bd9Sstevel@tonic-gate 	 * argument controls whether the results are sorted in forward or
6887c478bd9Sstevel@tonic-gate 	 * reverse order.
6897c478bd9Sstevel@tonic-gate 	 */
6907c478bd9Sstevel@tonic-gate 	void (*get_sortkey)(char *, int, scf_walkinfo_t *);
6917c478bd9Sstevel@tonic-gate };
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate static void
6947c478bd9Sstevel@tonic-gate reverse_bytes(char *buf, size_t len)
6957c478bd9Sstevel@tonic-gate {
6967c478bd9Sstevel@tonic-gate 	int i;
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 	for (i = 0; i < len; ++i)
6997c478bd9Sstevel@tonic-gate 		buf[i] = ~buf[i];
7007c478bd9Sstevel@tonic-gate }
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate /* CTID */
7037c478bd9Sstevel@tonic-gate #define	CTID_COLUMN_WIDTH		6
7047c478bd9Sstevel@tonic-gate 
7057c478bd9Sstevel@tonic-gate static void
7067c478bd9Sstevel@tonic-gate sprint_ctid(char **buf, scf_walkinfo_t *wip)
7077c478bd9Sstevel@tonic-gate {
7087c478bd9Sstevel@tonic-gate 	int r;
7097c478bd9Sstevel@tonic-gate 	uint64_t c;
7107c478bd9Sstevel@tonic-gate 	size_t newsize = (*buf ? strlen(*buf) : 0) + CTID_COLUMN_WIDTH + 2;
7117c478bd9Sstevel@tonic-gate 	char *newbuf = safe_malloc(newsize);
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate 	if (wip->pg != NULL)
7147c478bd9Sstevel@tonic-gate 		r = pg_get_single_val(wip->pg, scf_property_contract,
7157c478bd9Sstevel@tonic-gate 		    SCF_TYPE_COUNT, &c, 0, EMPTY_OK | MULTI_OK);
7167c478bd9Sstevel@tonic-gate 	else
7177c478bd9Sstevel@tonic-gate 		r = get_restarter_count_prop(wip->inst, scf_property_contract,
7187c478bd9Sstevel@tonic-gate 		    &c, EMPTY_OK | MULTI_OK);
7197c478bd9Sstevel@tonic-gate 
7207c478bd9Sstevel@tonic-gate 	if (r == 0)
7217c478bd9Sstevel@tonic-gate 		(void) snprintf(newbuf, newsize, "%s%*lu ",
7227c478bd9Sstevel@tonic-gate 		    *buf ? *buf : "", CTID_COLUMN_WIDTH, (ctid_t)c);
7237c478bd9Sstevel@tonic-gate 	else if (r == E2BIG)
7247c478bd9Sstevel@tonic-gate 		(void) snprintf(newbuf, newsize, "%s%*lu* ",
7257c478bd9Sstevel@tonic-gate 		    *buf ? *buf : "", CTID_COLUMN_WIDTH - 1, (ctid_t)c);
7267c478bd9Sstevel@tonic-gate 	else
7277c478bd9Sstevel@tonic-gate 		(void) snprintf(newbuf, newsize, "%s%*s ",
7287c478bd9Sstevel@tonic-gate 		    *buf ? *buf : "", CTID_COLUMN_WIDTH, "-");
7297c478bd9Sstevel@tonic-gate 	if (*buf)
7307c478bd9Sstevel@tonic-gate 		free(*buf);
7317c478bd9Sstevel@tonic-gate 	*buf = newbuf;
7327c478bd9Sstevel@tonic-gate }
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate #define	CTID_SORTKEY_WIDTH		(sizeof (uint64_t))
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate static void
7377c478bd9Sstevel@tonic-gate sortkey_ctid(char *buf, int reverse, scf_walkinfo_t *wip)
7387c478bd9Sstevel@tonic-gate {
7397c478bd9Sstevel@tonic-gate 	int r;
7407c478bd9Sstevel@tonic-gate 	uint64_t c;
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate 	if (wip->pg != NULL)
7437c478bd9Sstevel@tonic-gate 		r = pg_get_single_val(wip->pg, scf_property_contract,
7447c478bd9Sstevel@tonic-gate 		    SCF_TYPE_COUNT, &c, 0, EMPTY_OK);
7457c478bd9Sstevel@tonic-gate 	else
7467c478bd9Sstevel@tonic-gate 		r = get_restarter_count_prop(wip->inst, scf_property_contract,
7477c478bd9Sstevel@tonic-gate 		    &c, EMPTY_OK);
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate 	if (r == 0) {
7507c478bd9Sstevel@tonic-gate 		/*
7517c478bd9Sstevel@tonic-gate 		 * Use the id itself, but it must be big-endian for this to
7527c478bd9Sstevel@tonic-gate 		 * work.
7537c478bd9Sstevel@tonic-gate 		 */
7547c478bd9Sstevel@tonic-gate 		c = BE_64(c);
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate 		bcopy(&c, buf, CTID_SORTKEY_WIDTH);
7577c478bd9Sstevel@tonic-gate 	} else {
7587c478bd9Sstevel@tonic-gate 		bzero(buf, CTID_SORTKEY_WIDTH);
7597c478bd9Sstevel@tonic-gate 	}
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate 	if (reverse)
7627c478bd9Sstevel@tonic-gate 		reverse_bytes(buf, CTID_SORTKEY_WIDTH);
7637c478bd9Sstevel@tonic-gate }
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate /* DESC */
7667c478bd9Sstevel@tonic-gate #define	DESC_COLUMN_WIDTH	100
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate static void
7697c478bd9Sstevel@tonic-gate sprint_desc(char **buf, scf_walkinfo_t *wip)
7707c478bd9Sstevel@tonic-gate {
7717c478bd9Sstevel@tonic-gate 	char *x;
7727c478bd9Sstevel@tonic-gate 	size_t newsize;
7737c478bd9Sstevel@tonic-gate 	char *newbuf;
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate 	if (common_name_buf == NULL)
7767c478bd9Sstevel@tonic-gate 		common_name_buf = safe_malloc(max_scf_value_length + 1);
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate 	bzero(common_name_buf, max_scf_value_length + 1);
7797c478bd9Sstevel@tonic-gate 
7807c478bd9Sstevel@tonic-gate 	if (wip->pg != NULL) {
7817c478bd9Sstevel@tonic-gate 		common_name_buf[0] = '-';
7827c478bd9Sstevel@tonic-gate 	} else if (inst_get_single_val(wip->inst, SCF_PG_TM_COMMON_NAME, locale,
7837c478bd9Sstevel@tonic-gate 	    SCF_TYPE_USTRING, common_name_buf, max_scf_value_length, 0,
7847c478bd9Sstevel@tonic-gate 	    1, 1) == -1 &&
7857c478bd9Sstevel@tonic-gate 	    inst_get_single_val(wip->inst, SCF_PG_TM_COMMON_NAME, "C",
7867c478bd9Sstevel@tonic-gate 	    SCF_TYPE_USTRING, common_name_buf, max_scf_value_length, 0,
7877c478bd9Sstevel@tonic-gate 	    1, 1) == -1) {
7887c478bd9Sstevel@tonic-gate 		common_name_buf[0] = '-';
7897c478bd9Sstevel@tonic-gate 	}
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate 	/*
7927c478bd9Sstevel@tonic-gate 	 * Collapse multi-line tm_common_name values into a single line.
7937c478bd9Sstevel@tonic-gate 	 */
7947c478bd9Sstevel@tonic-gate 	for (x = common_name_buf; *x != '\0'; x++)
7957c478bd9Sstevel@tonic-gate 		if (*x == '\n')
7967c478bd9Sstevel@tonic-gate 			*x = ' ';
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate 	if (strlen(common_name_buf) > DESC_COLUMN_WIDTH)
7997c478bd9Sstevel@tonic-gate 		newsize = (*buf ? strlen(*buf) : 0) +
8007c478bd9Sstevel@tonic-gate 		    strlen(common_name_buf) + 1;
8017c478bd9Sstevel@tonic-gate 	else
8027c478bd9Sstevel@tonic-gate 		newsize = (*buf ? strlen(*buf) : 0) + DESC_COLUMN_WIDTH + 1;
8037c478bd9Sstevel@tonic-gate 	newbuf = safe_malloc(newsize);
8047c478bd9Sstevel@tonic-gate 	(void) snprintf(newbuf, newsize, "%s%-*s ", *buf ? *buf : "",
8057c478bd9Sstevel@tonic-gate 			DESC_COLUMN_WIDTH, common_name_buf);
8067c478bd9Sstevel@tonic-gate 	if (*buf)
8077c478bd9Sstevel@tonic-gate 		free(*buf);
8087c478bd9Sstevel@tonic-gate 	*buf = newbuf;
8097c478bd9Sstevel@tonic-gate }
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate /* ARGSUSED */
8127c478bd9Sstevel@tonic-gate static void
8137c478bd9Sstevel@tonic-gate sortkey_desc(char *buf, int reverse, scf_walkinfo_t *wip)
8147c478bd9Sstevel@tonic-gate {
8157c478bd9Sstevel@tonic-gate 	bzero(buf, DESC_COLUMN_WIDTH);
8167c478bd9Sstevel@tonic-gate }
8177c478bd9Sstevel@tonic-gate 
8187c478bd9Sstevel@tonic-gate /* State columns (STATE, NSTATE, S, N, SN, STA, NSTA) */
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate static char
8217c478bd9Sstevel@tonic-gate state_to_char(const char *state)
8227c478bd9Sstevel@tonic-gate {
8237c478bd9Sstevel@tonic-gate 	if (strcmp(state, SCF_STATE_STRING_UNINIT) == 0)
8247c478bd9Sstevel@tonic-gate 		return ('u');
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate 	if (strcmp(state, SCF_STATE_STRING_OFFLINE) == 0)
8277c478bd9Sstevel@tonic-gate 		return ('0');
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate 	if (strcmp(state, SCF_STATE_STRING_ONLINE) == 0)
8307c478bd9Sstevel@tonic-gate 		return ('1');
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 	if (strcmp(state, SCF_STATE_STRING_MAINT) == 0)
8337c478bd9Sstevel@tonic-gate 		return ('m');
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate 	if (strcmp(state, SCF_STATE_STRING_DISABLED) == 0)
8367c478bd9Sstevel@tonic-gate 		return ('d');
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate 	if (strcmp(state, SCF_STATE_STRING_DEGRADED) == 0)
8397c478bd9Sstevel@tonic-gate 		return ('D');
8407c478bd9Sstevel@tonic-gate 
8417c478bd9Sstevel@tonic-gate 	if (strcmp(state, SCF_STATE_STRING_LEGACY) == 0)
8427c478bd9Sstevel@tonic-gate 		return ('L');
8437c478bd9Sstevel@tonic-gate 
8447c478bd9Sstevel@tonic-gate 	return ('?');
8457c478bd9Sstevel@tonic-gate }
8467c478bd9Sstevel@tonic-gate 
8477c478bd9Sstevel@tonic-gate /* Return true if inst is transitioning. */
8487c478bd9Sstevel@tonic-gate static int
8497c478bd9Sstevel@tonic-gate transitioning(scf_instance_t *inst)
8507c478bd9Sstevel@tonic-gate {
8517c478bd9Sstevel@tonic-gate 	char nstate_name[MAX_SCF_STATE_STRING_SZ];
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate 	get_restarter_string_prop(inst, scf_property_next_state, nstate_name,
8547c478bd9Sstevel@tonic-gate 	    sizeof (nstate_name));
8557c478bd9Sstevel@tonic-gate 
8567c478bd9Sstevel@tonic-gate 	return (state_to_char(nstate_name) != '?');
8577c478bd9Sstevel@tonic-gate }
8587c478bd9Sstevel@tonic-gate 
8597c478bd9Sstevel@tonic-gate /* ARGSUSED */
8607c478bd9Sstevel@tonic-gate static void
8617c478bd9Sstevel@tonic-gate sortkey_states(const char *pname, char *buf, int reverse, scf_walkinfo_t *wip)
8627c478bd9Sstevel@tonic-gate {
8637c478bd9Sstevel@tonic-gate 	char state_name[MAX_SCF_STATE_STRING_SZ];
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate 	/*
8667c478bd9Sstevel@tonic-gate 	 * Lower numbers are printed first, so these are arranged from least
8677c478bd9Sstevel@tonic-gate 	 * interesting ("legacy run") to most interesting (unknown).
8687c478bd9Sstevel@tonic-gate 	 */
8697c478bd9Sstevel@tonic-gate 	if (wip->pg == NULL) {
8707c478bd9Sstevel@tonic-gate 		get_restarter_string_prop(wip->inst, pname, state_name,
8717c478bd9Sstevel@tonic-gate 		    sizeof (state_name));
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate 		if (strcmp(state_name, SCF_STATE_STRING_ONLINE) == 0)
8747c478bd9Sstevel@tonic-gate 			*buf = 2;
8757c478bd9Sstevel@tonic-gate 		else if (strcmp(state_name, SCF_STATE_STRING_DEGRADED) == 0)
8767c478bd9Sstevel@tonic-gate 			*buf = 3;
8777c478bd9Sstevel@tonic-gate 		else if (strcmp(state_name, SCF_STATE_STRING_OFFLINE) == 0)
8787c478bd9Sstevel@tonic-gate 			*buf = 4;
8797c478bd9Sstevel@tonic-gate 		else if (strcmp(state_name, SCF_STATE_STRING_MAINT) == 0)
8807c478bd9Sstevel@tonic-gate 			*buf = 5;
8817c478bd9Sstevel@tonic-gate 		else if (strcmp(state_name, SCF_STATE_STRING_DISABLED) == 0)
8827c478bd9Sstevel@tonic-gate 			*buf = 1;
8837c478bd9Sstevel@tonic-gate 		else if (strcmp(state_name, SCF_STATE_STRING_UNINIT) == 0)
8847c478bd9Sstevel@tonic-gate 			*buf = 6;
8857c478bd9Sstevel@tonic-gate 		else
8867c478bd9Sstevel@tonic-gate 			*buf = 7;
8877c478bd9Sstevel@tonic-gate 	} else
8887c478bd9Sstevel@tonic-gate 		*buf = 0;
8897c478bd9Sstevel@tonic-gate 
8907c478bd9Sstevel@tonic-gate 	if (reverse)
8917c478bd9Sstevel@tonic-gate 		*buf = 255 - *buf;
8927c478bd9Sstevel@tonic-gate }
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate static void
8957c478bd9Sstevel@tonic-gate sprint_state(char **buf, scf_walkinfo_t *wip)
8967c478bd9Sstevel@tonic-gate {
8977c478bd9Sstevel@tonic-gate 	char state_name[MAX_SCF_STATE_STRING_SZ + 1];
8987c478bd9Sstevel@tonic-gate 	size_t newsize;
8997c478bd9Sstevel@tonic-gate 	char *newbuf;
9007c478bd9Sstevel@tonic-gate 
9017c478bd9Sstevel@tonic-gate 	if (wip->pg == NULL) {
9027c478bd9Sstevel@tonic-gate 		get_restarter_string_prop(wip->inst, scf_property_state,
9037c478bd9Sstevel@tonic-gate 		    state_name, sizeof (state_name));
9047c478bd9Sstevel@tonic-gate 
9057c478bd9Sstevel@tonic-gate 		/* Don't print blank fields, to ease parsing. */
9067c478bd9Sstevel@tonic-gate 		if (state_name[0] == '\0') {
9077c478bd9Sstevel@tonic-gate 			state_name[0] = '-';
9087c478bd9Sstevel@tonic-gate 			state_name[1] = '\0';
9097c478bd9Sstevel@tonic-gate 		}
9107c478bd9Sstevel@tonic-gate 
9117c478bd9Sstevel@tonic-gate 		if (!opt_nstate_shown && transitioning(wip->inst)) {
9127c478bd9Sstevel@tonic-gate 			/* Append an asterisk if nstate is valid. */
9137c478bd9Sstevel@tonic-gate 			(void) strcat(state_name, "*");
9147c478bd9Sstevel@tonic-gate 		}
9157c478bd9Sstevel@tonic-gate 	} else
9167c478bd9Sstevel@tonic-gate 		(void) strcpy(state_name, SCF_STATE_STRING_LEGACY);
9177c478bd9Sstevel@tonic-gate 
9187c478bd9Sstevel@tonic-gate 	newsize = (*buf ? strlen(*buf) : 0) + MAX_SCF_STATE_STRING_SZ + 2;
9197c478bd9Sstevel@tonic-gate 	newbuf = safe_malloc(newsize);
9207c478bd9Sstevel@tonic-gate 	(void) snprintf(newbuf, newsize, "%s%-*s ", *buf ? *buf : "",
9217c478bd9Sstevel@tonic-gate 	    MAX_SCF_STATE_STRING_SZ + 1, state_name);
9227c478bd9Sstevel@tonic-gate 
9237c478bd9Sstevel@tonic-gate 	if (*buf)
9247c478bd9Sstevel@tonic-gate 		free(*buf);
9257c478bd9Sstevel@tonic-gate 	*buf = newbuf;
9267c478bd9Sstevel@tonic-gate }
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate static void
9297c478bd9Sstevel@tonic-gate sortkey_state(char *buf, int reverse, scf_walkinfo_t *wip)
9307c478bd9Sstevel@tonic-gate {
9317c478bd9Sstevel@tonic-gate 	sortkey_states(scf_property_state, buf, reverse, wip);
9327c478bd9Sstevel@tonic-gate }
9337c478bd9Sstevel@tonic-gate 
9347c478bd9Sstevel@tonic-gate static void
9357c478bd9Sstevel@tonic-gate sprint_nstate(char **buf, scf_walkinfo_t *wip)
9367c478bd9Sstevel@tonic-gate {
9377c478bd9Sstevel@tonic-gate 	char next_state_name[MAX_SCF_STATE_STRING_SZ];
9387c478bd9Sstevel@tonic-gate 	boolean_t blank = 0;
9397c478bd9Sstevel@tonic-gate 	size_t newsize;
9407c478bd9Sstevel@tonic-gate 	char *newbuf;
9417c478bd9Sstevel@tonic-gate 
9427c478bd9Sstevel@tonic-gate 	if (wip->pg == NULL) {
9437c478bd9Sstevel@tonic-gate 		get_restarter_string_prop(wip->inst, scf_property_next_state,
9447c478bd9Sstevel@tonic-gate 		    next_state_name, sizeof (next_state_name));
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate 		/* Don't print blank fields, to ease parsing. */
9477c478bd9Sstevel@tonic-gate 		if (next_state_name[0] == '\0' ||
9487c478bd9Sstevel@tonic-gate 		    strcmp(next_state_name, SCF_STATE_STRING_NONE) == 0)
9497c478bd9Sstevel@tonic-gate 			blank = 1;
9507c478bd9Sstevel@tonic-gate 	} else
9517c478bd9Sstevel@tonic-gate 		blank = 1;
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate 	if (blank) {
9547c478bd9Sstevel@tonic-gate 		next_state_name[0] = '-';
9557c478bd9Sstevel@tonic-gate 		next_state_name[1] = '\0';
9567c478bd9Sstevel@tonic-gate 	}
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate 	newsize = (*buf ? strlen(*buf) : 0) + MAX_SCF_STATE_STRING_SZ + 1;
9597c478bd9Sstevel@tonic-gate 	newbuf = safe_malloc(newsize);
9607c478bd9Sstevel@tonic-gate 	(void) snprintf(newbuf, newsize, "%s%-*s ", *buf ? *buf : "",
9617c478bd9Sstevel@tonic-gate 	    MAX_SCF_STATE_STRING_SZ - 1, next_state_name);
9627c478bd9Sstevel@tonic-gate 	if (*buf)
9637c478bd9Sstevel@tonic-gate 		free(*buf);
9647c478bd9Sstevel@tonic-gate 	*buf = newbuf;
9657c478bd9Sstevel@tonic-gate }
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate static void
9687c478bd9Sstevel@tonic-gate sortkey_nstate(char *buf, int reverse, scf_walkinfo_t *wip)
9697c478bd9Sstevel@tonic-gate {
9707c478bd9Sstevel@tonic-gate 	sortkey_states(scf_property_next_state, buf, reverse, wip);
9717c478bd9Sstevel@tonic-gate }
9727c478bd9Sstevel@tonic-gate 
9737c478bd9Sstevel@tonic-gate static void
9747c478bd9Sstevel@tonic-gate sprint_s(char **buf, scf_walkinfo_t *wip)
9757c478bd9Sstevel@tonic-gate {
9767c478bd9Sstevel@tonic-gate 	char tmp[3];
9777c478bd9Sstevel@tonic-gate 	char state_name[MAX_SCF_STATE_STRING_SZ];
9787c478bd9Sstevel@tonic-gate 	size_t newsize = (*buf ? strlen(*buf) : 0) + 4;
9797c478bd9Sstevel@tonic-gate 	char *newbuf = safe_malloc(newsize);
9807c478bd9Sstevel@tonic-gate 
9817c478bd9Sstevel@tonic-gate 	if (wip->pg == NULL) {
9827c478bd9Sstevel@tonic-gate 		get_restarter_string_prop(wip->inst, scf_property_state,
9837c478bd9Sstevel@tonic-gate 		    state_name, sizeof (state_name));
9847c478bd9Sstevel@tonic-gate 		tmp[0] = state_to_char(state_name);
9857c478bd9Sstevel@tonic-gate 
9867c478bd9Sstevel@tonic-gate 		if (!opt_nstate_shown && transitioning(wip->inst))
9877c478bd9Sstevel@tonic-gate 			tmp[1] = '*';
9887c478bd9Sstevel@tonic-gate 		else
9897c478bd9Sstevel@tonic-gate 			tmp[1] = ' ';
9907c478bd9Sstevel@tonic-gate 	} else {
9917c478bd9Sstevel@tonic-gate 		tmp[0] = 'L';
9927c478bd9Sstevel@tonic-gate 		tmp[1] = ' ';
9937c478bd9Sstevel@tonic-gate 	}
9947c478bd9Sstevel@tonic-gate 	tmp[2] = ' ';
9957c478bd9Sstevel@tonic-gate 	(void) snprintf(newbuf, newsize, "%s%-*s", *buf ? *buf : "",
9967c478bd9Sstevel@tonic-gate 	    3, tmp);
9977c478bd9Sstevel@tonic-gate 	if (*buf)
9987c478bd9Sstevel@tonic-gate 		free(*buf);
9997c478bd9Sstevel@tonic-gate 	*buf = newbuf;
10007c478bd9Sstevel@tonic-gate }
10017c478bd9Sstevel@tonic-gate 
10027c478bd9Sstevel@tonic-gate static void
10037c478bd9Sstevel@tonic-gate sprint_n(char **buf, scf_walkinfo_t *wip)
10047c478bd9Sstevel@tonic-gate {
10057c478bd9Sstevel@tonic-gate 	char tmp[2];
10067c478bd9Sstevel@tonic-gate 	size_t newsize = (*buf ? strlen(*buf) : 0) + 3;
10077c478bd9Sstevel@tonic-gate 	char *newbuf = safe_malloc(newsize);
10087c478bd9Sstevel@tonic-gate 	char nstate_name[MAX_SCF_STATE_STRING_SZ];
10097c478bd9Sstevel@tonic-gate 
10107c478bd9Sstevel@tonic-gate 	if (wip->pg == NULL) {
10117c478bd9Sstevel@tonic-gate 		get_restarter_string_prop(wip->inst, scf_property_next_state,
10127c478bd9Sstevel@tonic-gate 		    nstate_name, sizeof (nstate_name));
10137c478bd9Sstevel@tonic-gate 
10147c478bd9Sstevel@tonic-gate 		if (strcmp(nstate_name, SCF_STATE_STRING_NONE) == 0)
10157c478bd9Sstevel@tonic-gate 			tmp[0] = '-';
10167c478bd9Sstevel@tonic-gate 		else
10177c478bd9Sstevel@tonic-gate 			tmp[0] = state_to_char(nstate_name);
10187c478bd9Sstevel@tonic-gate 	} else
10197c478bd9Sstevel@tonic-gate 		tmp[0] = '-';
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate 	(void) snprintf(newbuf, newsize, "%s%-*s ", *buf ? *buf : "",
10227c478bd9Sstevel@tonic-gate 	    2, tmp);
10237c478bd9Sstevel@tonic-gate 	if (*buf)
10247c478bd9Sstevel@tonic-gate 		free(*buf);
10257c478bd9Sstevel@tonic-gate 	*buf = newbuf;
10267c478bd9Sstevel@tonic-gate }
10277c478bd9Sstevel@tonic-gate 
10287c478bd9Sstevel@tonic-gate static void
10297c478bd9Sstevel@tonic-gate sprint_sn(char **buf, scf_walkinfo_t *wip)
10307c478bd9Sstevel@tonic-gate {
10317c478bd9Sstevel@tonic-gate 	char tmp[3];
10327c478bd9Sstevel@tonic-gate 	size_t newsize = (*buf ? strlen(*buf) : 0) + 4;
10337c478bd9Sstevel@tonic-gate 	char *newbuf = safe_malloc(newsize);
10347c478bd9Sstevel@tonic-gate 	char nstate_name[MAX_SCF_STATE_STRING_SZ];
10357c478bd9Sstevel@tonic-gate 	char state_name[MAX_SCF_STATE_STRING_SZ];
10367c478bd9Sstevel@tonic-gate 
10377c478bd9Sstevel@tonic-gate 	if (wip->pg == NULL) {
10387c478bd9Sstevel@tonic-gate 		get_restarter_string_prop(wip->inst, scf_property_state,
10397c478bd9Sstevel@tonic-gate 		    state_name, sizeof (state_name));
10407c478bd9Sstevel@tonic-gate 		get_restarter_string_prop(wip->inst, scf_property_next_state,
10417c478bd9Sstevel@tonic-gate 		    nstate_name, sizeof (nstate_name));
10427c478bd9Sstevel@tonic-gate 		tmp[0] = state_to_char(state_name);
10437c478bd9Sstevel@tonic-gate 
10447c478bd9Sstevel@tonic-gate 		if (strcmp(nstate_name, SCF_STATE_STRING_NONE) == 0)
10457c478bd9Sstevel@tonic-gate 			tmp[1] = '-';
10467c478bd9Sstevel@tonic-gate 		else
10477c478bd9Sstevel@tonic-gate 			tmp[1] = state_to_char(nstate_name);
10487c478bd9Sstevel@tonic-gate 	} else {
10497c478bd9Sstevel@tonic-gate 		tmp[0] = 'L';
10507c478bd9Sstevel@tonic-gate 		tmp[1] = '-';
10517c478bd9Sstevel@tonic-gate 	}
10527c478bd9Sstevel@tonic-gate 
10537c478bd9Sstevel@tonic-gate 	tmp[2] = ' ';
10547c478bd9Sstevel@tonic-gate 	(void) snprintf(newbuf, newsize, "%s%-*s ", *buf ? *buf : "",
10557c478bd9Sstevel@tonic-gate 	    3, tmp);
10567c478bd9Sstevel@tonic-gate 	if (*buf)
10577c478bd9Sstevel@tonic-gate 		free(*buf);
10587c478bd9Sstevel@tonic-gate 	*buf = newbuf;
10597c478bd9Sstevel@tonic-gate }
10607c478bd9Sstevel@tonic-gate 
10617c478bd9Sstevel@tonic-gate /* ARGSUSED */
10627c478bd9Sstevel@tonic-gate static void
10637c478bd9Sstevel@tonic-gate sortkey_sn(char *buf, int reverse, scf_walkinfo_t *wip)
10647c478bd9Sstevel@tonic-gate {
10657c478bd9Sstevel@tonic-gate 	sortkey_state(buf, reverse, wip);
10667c478bd9Sstevel@tonic-gate 	sortkey_nstate(buf + 1, reverse, wip);
10677c478bd9Sstevel@tonic-gate }
10687c478bd9Sstevel@tonic-gate 
10697c478bd9Sstevel@tonic-gate static const char *
10707c478bd9Sstevel@tonic-gate state_abbrev(const char *state)
10717c478bd9Sstevel@tonic-gate {
10727c478bd9Sstevel@tonic-gate 	if (strcmp(state, SCF_STATE_STRING_UNINIT) == 0)
10737c478bd9Sstevel@tonic-gate 		return ("UN");
10747c478bd9Sstevel@tonic-gate 	if (strcmp(state, SCF_STATE_STRING_OFFLINE) == 0)
10757c478bd9Sstevel@tonic-gate 		return ("OFF");
10767c478bd9Sstevel@tonic-gate 	if (strcmp(state, SCF_STATE_STRING_ONLINE) == 0)
10777c478bd9Sstevel@tonic-gate 		return ("ON");
10787c478bd9Sstevel@tonic-gate 	if (strcmp(state, SCF_STATE_STRING_MAINT) == 0)
10797c478bd9Sstevel@tonic-gate 		return ("MNT");
10807c478bd9Sstevel@tonic-gate 	if (strcmp(state, SCF_STATE_STRING_DISABLED) == 0)
10817c478bd9Sstevel@tonic-gate 		return ("DIS");
10827c478bd9Sstevel@tonic-gate 	if (strcmp(state, SCF_STATE_STRING_DEGRADED) == 0)
10837c478bd9Sstevel@tonic-gate 		return ("DGD");
10847c478bd9Sstevel@tonic-gate 	if (strcmp(state, SCF_STATE_STRING_LEGACY) == 0)
10857c478bd9Sstevel@tonic-gate 		return ("LRC");
10867c478bd9Sstevel@tonic-gate 
10877c478bd9Sstevel@tonic-gate 	return ("?");
10887c478bd9Sstevel@tonic-gate }
10897c478bd9Sstevel@tonic-gate 
10907c478bd9Sstevel@tonic-gate static void
10917c478bd9Sstevel@tonic-gate sprint_sta(char **buf, scf_walkinfo_t *wip)
10927c478bd9Sstevel@tonic-gate {
10937c478bd9Sstevel@tonic-gate 	char state_name[MAX_SCF_STATE_STRING_SZ];
10947c478bd9Sstevel@tonic-gate 	char sta[5];
10957c478bd9Sstevel@tonic-gate 	size_t newsize = (*buf ? strlen(*buf) : 0) + 6;
10967c478bd9Sstevel@tonic-gate 	char *newbuf = safe_malloc(newsize);
10977c478bd9Sstevel@tonic-gate 
10987c478bd9Sstevel@tonic-gate 	if (wip->pg == NULL)
10997c478bd9Sstevel@tonic-gate 		get_restarter_string_prop(wip->inst, scf_property_state,
11007c478bd9Sstevel@tonic-gate 		    state_name, sizeof (state_name));
11017c478bd9Sstevel@tonic-gate 	else
11027c478bd9Sstevel@tonic-gate 		(void) strcpy(state_name, SCF_STATE_STRING_LEGACY);
11037c478bd9Sstevel@tonic-gate 
11047c478bd9Sstevel@tonic-gate 	(void) strcpy(sta, state_abbrev(state_name));
11057c478bd9Sstevel@tonic-gate 
11067c478bd9Sstevel@tonic-gate 	if (wip->pg == NULL && !opt_nstate_shown && transitioning(wip->inst))
11077c478bd9Sstevel@tonic-gate 		(void) strcat(sta, "*");
11087c478bd9Sstevel@tonic-gate 
11097c478bd9Sstevel@tonic-gate 	(void) snprintf(newbuf, newsize, "%s%-4s ", *buf ? *buf : "", sta);
11107c478bd9Sstevel@tonic-gate 	if (*buf)
11117c478bd9Sstevel@tonic-gate 		free(*buf);
11127c478bd9Sstevel@tonic-gate 	*buf = newbuf;
11137c478bd9Sstevel@tonic-gate }
11147c478bd9Sstevel@tonic-gate 
11157c478bd9Sstevel@tonic-gate static void
11167c478bd9Sstevel@tonic-gate sprint_nsta(char **buf, scf_walkinfo_t *wip)
11177c478bd9Sstevel@tonic-gate {
11187c478bd9Sstevel@tonic-gate 	char state_name[MAX_SCF_STATE_STRING_SZ];
11197c478bd9Sstevel@tonic-gate 	size_t newsize = (*buf ? strlen(*buf) : 0) + 6;
11207c478bd9Sstevel@tonic-gate 	char *newbuf = safe_malloc(newsize);
11217c478bd9Sstevel@tonic-gate 
11227c478bd9Sstevel@tonic-gate 	if (wip->pg == NULL)
11237c478bd9Sstevel@tonic-gate 		get_restarter_string_prop(wip->inst, scf_property_next_state,
11247c478bd9Sstevel@tonic-gate 		    state_name, sizeof (state_name));
11257c478bd9Sstevel@tonic-gate 	else
11267c478bd9Sstevel@tonic-gate 		(void) strcpy(state_name, SCF_STATE_STRING_NONE);
11277c478bd9Sstevel@tonic-gate 
11287c478bd9Sstevel@tonic-gate 	if (strcmp(state_name, SCF_STATE_STRING_NONE) == 0)
11297c478bd9Sstevel@tonic-gate 		(void) snprintf(newbuf, newsize, "%s%-4s ", *buf ? *buf : "",
11307c478bd9Sstevel@tonic-gate 		    "-");
11317c478bd9Sstevel@tonic-gate 	else
11327c478bd9Sstevel@tonic-gate 		(void) snprintf(newbuf, newsize, "%s%-4s ", *buf ? *buf : "",
11337c478bd9Sstevel@tonic-gate 		    state_abbrev(state_name));
11347c478bd9Sstevel@tonic-gate 	if (*buf)
11357c478bd9Sstevel@tonic-gate 		free(*buf);
11367c478bd9Sstevel@tonic-gate 	*buf = newbuf;
11377c478bd9Sstevel@tonic-gate }
11387c478bd9Sstevel@tonic-gate 
11397c478bd9Sstevel@tonic-gate /* FMRI */
11407c478bd9Sstevel@tonic-gate #define	FMRI_COLUMN_WIDTH	50
11417c478bd9Sstevel@tonic-gate static void
11427c478bd9Sstevel@tonic-gate sprint_fmri(char **buf, scf_walkinfo_t *wip)
11437c478bd9Sstevel@tonic-gate {
11447c478bd9Sstevel@tonic-gate 	char *fmri_buf = safe_malloc(max_scf_fmri_length + 1);
11457c478bd9Sstevel@tonic-gate 	size_t newsize;
11467c478bd9Sstevel@tonic-gate 	char *newbuf;
11477c478bd9Sstevel@tonic-gate 
11487c478bd9Sstevel@tonic-gate 	if (wip->pg == NULL) {
11497c478bd9Sstevel@tonic-gate 		if (scf_instance_to_fmri(wip->inst, fmri_buf,
11507c478bd9Sstevel@tonic-gate 		    max_scf_fmri_length + 1) == -1)
11517c478bd9Sstevel@tonic-gate 			scfdie();
11527c478bd9Sstevel@tonic-gate 	} else {
11537c478bd9Sstevel@tonic-gate 		(void) strcpy(fmri_buf, LEGACY_SCHEME);
11547c478bd9Sstevel@tonic-gate 		if (pg_get_single_val(wip->pg, SCF_LEGACY_PROPERTY_NAME,
11557c478bd9Sstevel@tonic-gate 		    SCF_TYPE_ASTRING, fmri_buf + sizeof (LEGACY_SCHEME) - 1,
11567c478bd9Sstevel@tonic-gate 		    max_scf_fmri_length + 1 - (sizeof (LEGACY_SCHEME) - 1),
11577c478bd9Sstevel@tonic-gate 		    0) != 0)
11587c478bd9Sstevel@tonic-gate 			(void) strcat(fmri_buf, LEGACY_UNKNOWN);
11597c478bd9Sstevel@tonic-gate 	}
11607c478bd9Sstevel@tonic-gate 
11617c478bd9Sstevel@tonic-gate 	if (strlen(fmri_buf) > FMRI_COLUMN_WIDTH)
11627c478bd9Sstevel@tonic-gate 		newsize = (*buf ? strlen(*buf) : 0) + strlen(fmri_buf) + 2;
11637c478bd9Sstevel@tonic-gate 	else
11647c478bd9Sstevel@tonic-gate 		newsize = (*buf ? strlen(*buf) : 0) + FMRI_COLUMN_WIDTH + 2;
11657c478bd9Sstevel@tonic-gate 	newbuf = safe_malloc(newsize);
11667c478bd9Sstevel@tonic-gate 	(void) snprintf(newbuf, newsize, "%s%-*s ", *buf ? *buf : "",
11677c478bd9Sstevel@tonic-gate 	    FMRI_COLUMN_WIDTH, fmri_buf);
11687c478bd9Sstevel@tonic-gate 	free(fmri_buf);
11697c478bd9Sstevel@tonic-gate 	if (*buf)
11707c478bd9Sstevel@tonic-gate 		free(*buf);
11717c478bd9Sstevel@tonic-gate 	*buf = newbuf;
11727c478bd9Sstevel@tonic-gate }
11737c478bd9Sstevel@tonic-gate 
11747c478bd9Sstevel@tonic-gate static void
11757c478bd9Sstevel@tonic-gate sortkey_fmri(char *buf, int reverse, scf_walkinfo_t *wip)
11767c478bd9Sstevel@tonic-gate {
11777c478bd9Sstevel@tonic-gate 	char *tmp = NULL;
11787c478bd9Sstevel@tonic-gate 
11797c478bd9Sstevel@tonic-gate 	sprint_fmri(&tmp, wip);
11807c478bd9Sstevel@tonic-gate 	bcopy(tmp, buf, FMRI_COLUMN_WIDTH);
11817c478bd9Sstevel@tonic-gate 	free(tmp);
11827c478bd9Sstevel@tonic-gate 	if (reverse)
11837c478bd9Sstevel@tonic-gate 		reverse_bytes(buf, FMRI_COLUMN_WIDTH);
11847c478bd9Sstevel@tonic-gate }
11857c478bd9Sstevel@tonic-gate 
11867c478bd9Sstevel@tonic-gate /* Component columns */
11877c478bd9Sstevel@tonic-gate #define	COMPONENT_COLUMN_WIDTH	20
11887c478bd9Sstevel@tonic-gate static void
11897c478bd9Sstevel@tonic-gate sprint_scope(char **buf, scf_walkinfo_t *wip)
11907c478bd9Sstevel@tonic-gate {
11917c478bd9Sstevel@tonic-gate 	char *scope_buf = safe_malloc(max_scf_name_length + 1);
11927c478bd9Sstevel@tonic-gate 	size_t newsize = (*buf ? strlen(*buf) : 0) + COMPONENT_COLUMN_WIDTH + 2;
11937c478bd9Sstevel@tonic-gate 	char *newbuf = safe_malloc(newsize);
11947c478bd9Sstevel@tonic-gate 
11957c478bd9Sstevel@tonic-gate 	assert(wip->scope != NULL);
11967c478bd9Sstevel@tonic-gate 
11977c478bd9Sstevel@tonic-gate 	if (scf_scope_get_name(wip->scope, scope_buf, max_scf_name_length) < 0)
11987c478bd9Sstevel@tonic-gate 		scfdie();
11997c478bd9Sstevel@tonic-gate 
12007c478bd9Sstevel@tonic-gate 	(void) snprintf(newbuf, newsize, "%s%-*s ", *buf ? *buf : "",
12017c478bd9Sstevel@tonic-gate 	    COMPONENT_COLUMN_WIDTH, scope_buf);
12027c478bd9Sstevel@tonic-gate 	if (*buf)
12037c478bd9Sstevel@tonic-gate 		free(*buf);
12047c478bd9Sstevel@tonic-gate 	*buf = newbuf;
12057c478bd9Sstevel@tonic-gate 	free(scope_buf);
12067c478bd9Sstevel@tonic-gate }
12077c478bd9Sstevel@tonic-gate 
12087c478bd9Sstevel@tonic-gate static void
12097c478bd9Sstevel@tonic-gate sortkey_scope(char *buf, int reverse, scf_walkinfo_t *wip)
12107c478bd9Sstevel@tonic-gate {
12117c478bd9Sstevel@tonic-gate 	char *tmp = NULL;
12127c478bd9Sstevel@tonic-gate 
12137c478bd9Sstevel@tonic-gate 	sprint_scope(&tmp, wip);
12147c478bd9Sstevel@tonic-gate 	bcopy(tmp, buf, COMPONENT_COLUMN_WIDTH);
12157c478bd9Sstevel@tonic-gate 	free(tmp);
12167c478bd9Sstevel@tonic-gate 	if (reverse)
12177c478bd9Sstevel@tonic-gate 		reverse_bytes(buf, COMPONENT_COLUMN_WIDTH);
12187c478bd9Sstevel@tonic-gate }
12197c478bd9Sstevel@tonic-gate 
12207c478bd9Sstevel@tonic-gate static void
12217c478bd9Sstevel@tonic-gate sprint_service(char **buf, scf_walkinfo_t *wip)
12227c478bd9Sstevel@tonic-gate {
12237c478bd9Sstevel@tonic-gate 	char *svc_buf = safe_malloc(max_scf_name_length + 1);
12247c478bd9Sstevel@tonic-gate 	char *newbuf;
12257c478bd9Sstevel@tonic-gate 	size_t newsize;
12267c478bd9Sstevel@tonic-gate 
12277c478bd9Sstevel@tonic-gate 	if (wip->pg == NULL) {
12287c478bd9Sstevel@tonic-gate 		if (scf_service_get_name(wip->svc, svc_buf,
12297c478bd9Sstevel@tonic-gate 		    max_scf_name_length + 1) < 0)
12307c478bd9Sstevel@tonic-gate 			scfdie();
12317c478bd9Sstevel@tonic-gate 	} else {
12327c478bd9Sstevel@tonic-gate 		if (pg_get_single_val(wip->pg, "name", SCF_TYPE_ASTRING,
12337c478bd9Sstevel@tonic-gate 		    svc_buf, max_scf_name_length + 1, EMPTY_OK) != 0)
12347c478bd9Sstevel@tonic-gate 			(void) strcpy(svc_buf, LEGACY_UNKNOWN);
12357c478bd9Sstevel@tonic-gate 	}
12367c478bd9Sstevel@tonic-gate 
12377c478bd9Sstevel@tonic-gate 
12387c478bd9Sstevel@tonic-gate 	if (strlen(svc_buf) > COMPONENT_COLUMN_WIDTH)
12397c478bd9Sstevel@tonic-gate 		newsize = (*buf ? strlen(*buf) : 0) + strlen(svc_buf) + 2;
12407c478bd9Sstevel@tonic-gate 	else
12417c478bd9Sstevel@tonic-gate 		newsize = (*buf ? strlen(*buf) : 0) +
12427c478bd9Sstevel@tonic-gate 		    COMPONENT_COLUMN_WIDTH + 2;
12437c478bd9Sstevel@tonic-gate 	newbuf = safe_malloc(newsize);
12447c478bd9Sstevel@tonic-gate 	(void) snprintf(newbuf, newsize, "%s%-*s ", *buf ? *buf : "",
12457c478bd9Sstevel@tonic-gate 	    COMPONENT_COLUMN_WIDTH, svc_buf);
12467c478bd9Sstevel@tonic-gate 	free(svc_buf);
12477c478bd9Sstevel@tonic-gate 	if (*buf)
12487c478bd9Sstevel@tonic-gate 		free(*buf);
12497c478bd9Sstevel@tonic-gate 	*buf = newbuf;
12507c478bd9Sstevel@tonic-gate }
12517c478bd9Sstevel@tonic-gate 
12527c478bd9Sstevel@tonic-gate static void
12537c478bd9Sstevel@tonic-gate sortkey_service(char *buf, int reverse, scf_walkinfo_t *wip)
12547c478bd9Sstevel@tonic-gate {
12557c478bd9Sstevel@tonic-gate 	char *tmp = NULL;
12567c478bd9Sstevel@tonic-gate 
12577c478bd9Sstevel@tonic-gate 	sprint_service(&tmp, wip);
12587c478bd9Sstevel@tonic-gate 	bcopy(tmp, buf, COMPONENT_COLUMN_WIDTH);
12597c478bd9Sstevel@tonic-gate 	free(tmp);
12607c478bd9Sstevel@tonic-gate 	if (reverse)
12617c478bd9Sstevel@tonic-gate 		reverse_bytes(buf, COMPONENT_COLUMN_WIDTH);
12627c478bd9Sstevel@tonic-gate }
12637c478bd9Sstevel@tonic-gate 
12647c478bd9Sstevel@tonic-gate /* INST */
12657c478bd9Sstevel@tonic-gate static void
12667c478bd9Sstevel@tonic-gate sprint_instance(char **buf, scf_walkinfo_t *wip)
12677c478bd9Sstevel@tonic-gate {
12687c478bd9Sstevel@tonic-gate 	char *tmp = safe_malloc(max_scf_name_length + 1);
12697c478bd9Sstevel@tonic-gate 	size_t newsize = (*buf ? strlen(*buf) : 0) + COMPONENT_COLUMN_WIDTH + 2;
12707c478bd9Sstevel@tonic-gate 	char *newbuf = safe_malloc(newsize);
12717c478bd9Sstevel@tonic-gate 
12727c478bd9Sstevel@tonic-gate 	if (wip->pg == NULL) {
12737c478bd9Sstevel@tonic-gate 		if (scf_instance_get_name(wip->inst, tmp,
12747c478bd9Sstevel@tonic-gate 		    max_scf_name_length + 1) < 0)
12757c478bd9Sstevel@tonic-gate 			scfdie();
12767c478bd9Sstevel@tonic-gate 	} else {
12777c478bd9Sstevel@tonic-gate 		tmp[0] = '-';
12787c478bd9Sstevel@tonic-gate 		tmp[1] = '\0';
12797c478bd9Sstevel@tonic-gate 	}
12807c478bd9Sstevel@tonic-gate 
12817c478bd9Sstevel@tonic-gate 	(void) snprintf(newbuf, newsize, "%s%-*s ", *buf ? *buf : "",
12827c478bd9Sstevel@tonic-gate 	    COMPONENT_COLUMN_WIDTH, tmp);
12837c478bd9Sstevel@tonic-gate 	if (*buf)
12847c478bd9Sstevel@tonic-gate 		free(*buf);
12857c478bd9Sstevel@tonic-gate 	*buf = newbuf;
12867c478bd9Sstevel@tonic-gate 	free(tmp);
12877c478bd9Sstevel@tonic-gate }
12887c478bd9Sstevel@tonic-gate 
12897c478bd9Sstevel@tonic-gate static void
12907c478bd9Sstevel@tonic-gate sortkey_instance(char *buf, int reverse, scf_walkinfo_t *wip)
12917c478bd9Sstevel@tonic-gate {
12927c478bd9Sstevel@tonic-gate 	char *tmp = NULL;
12937c478bd9Sstevel@tonic-gate 
12947c478bd9Sstevel@tonic-gate 	sprint_instance(&tmp, wip);
12957c478bd9Sstevel@tonic-gate 	bcopy(tmp, buf, COMPONENT_COLUMN_WIDTH);
12967c478bd9Sstevel@tonic-gate 	free(tmp);
12977c478bd9Sstevel@tonic-gate 	if (reverse)
12987c478bd9Sstevel@tonic-gate 		reverse_bytes(buf, COMPONENT_COLUMN_WIDTH);
12997c478bd9Sstevel@tonic-gate }
13007c478bd9Sstevel@tonic-gate 
13017c478bd9Sstevel@tonic-gate /* STIME */
13027c478bd9Sstevel@tonic-gate #define	STIME_COLUMN_WIDTH		8
13037c478bd9Sstevel@tonic-gate #define	FORMAT_TIME			"%k:%M:%S"
13047c478bd9Sstevel@tonic-gate #define	FORMAT_DATE			"%b_%d  "
13057c478bd9Sstevel@tonic-gate #define	FORMAT_YEAR			"%Y    "
13067c478bd9Sstevel@tonic-gate 
13077c478bd9Sstevel@tonic-gate static void
13087c478bd9Sstevel@tonic-gate sprint_stime(char **buf, scf_walkinfo_t *wip)
13097c478bd9Sstevel@tonic-gate {
13107c478bd9Sstevel@tonic-gate 	int r;
13117c478bd9Sstevel@tonic-gate 	struct timeval tv;
13127c478bd9Sstevel@tonic-gate 	time_t then;
13137c478bd9Sstevel@tonic-gate 	struct tm *tm;
13147c478bd9Sstevel@tonic-gate 	char st_buf[STIME_COLUMN_WIDTH + 1];
13157c478bd9Sstevel@tonic-gate 	size_t newsize = (*buf ? strlen(*buf) : 0) + STIME_COLUMN_WIDTH + 2;
13167c478bd9Sstevel@tonic-gate 	char *newbuf = safe_malloc(newsize);
13177c478bd9Sstevel@tonic-gate 
13187c478bd9Sstevel@tonic-gate 	if (wip->pg == NULL) {
13197c478bd9Sstevel@tonic-gate 		r = get_restarter_time_prop(wip->inst,
13207c478bd9Sstevel@tonic-gate 		    SCF_PROPERTY_STATE_TIMESTAMP, &tv, 0);
13217c478bd9Sstevel@tonic-gate 	} else {
13227c478bd9Sstevel@tonic-gate 		r = pg_get_single_val(wip->pg, SCF_PROPERTY_STATE_TIMESTAMP,
13237c478bd9Sstevel@tonic-gate 		    SCF_TYPE_TIME, &tv, NULL, 0);
13247c478bd9Sstevel@tonic-gate 	}
13257c478bd9Sstevel@tonic-gate 
13267c478bd9Sstevel@tonic-gate 	if (r != 0) {
13277c478bd9Sstevel@tonic-gate 		(void) snprintf(newbuf, newsize, "%s%-*s", *buf ? *buf : "",
13287c478bd9Sstevel@tonic-gate 		    STIME_COLUMN_WIDTH + 1, "?");
13297c478bd9Sstevel@tonic-gate 		return;
13307c478bd9Sstevel@tonic-gate 	}
13317c478bd9Sstevel@tonic-gate 
13327c478bd9Sstevel@tonic-gate 	then = (time_t)tv.tv_sec;
13337c478bd9Sstevel@tonic-gate 
13347c478bd9Sstevel@tonic-gate 	tm = localtime(&then);
13357c478bd9Sstevel@tonic-gate 	/*
13367c478bd9Sstevel@tonic-gate 	 * Print time if started within the past 24 hours, print date
13377c478bd9Sstevel@tonic-gate 	 * if within the past 12 months, print year if started greater than
13387c478bd9Sstevel@tonic-gate 	 * 12 months ago.
13397c478bd9Sstevel@tonic-gate 	 */
13407c478bd9Sstevel@tonic-gate 	if (now - then < 24 * 60 * 60)
13417c478bd9Sstevel@tonic-gate 		(void) strftime(st_buf, sizeof (st_buf), gettext(FORMAT_TIME),
13427c478bd9Sstevel@tonic-gate 				tm);
13437c478bd9Sstevel@tonic-gate 	else if (now - then < 12 * 30 * 24 * 60 * 60)
13447c478bd9Sstevel@tonic-gate 		(void) strftime(st_buf, sizeof (st_buf), gettext(FORMAT_DATE),
13457c478bd9Sstevel@tonic-gate 				tm);
13467c478bd9Sstevel@tonic-gate 	else
13477c478bd9Sstevel@tonic-gate 		(void) strftime(st_buf, sizeof (st_buf), gettext(FORMAT_YEAR),
13487c478bd9Sstevel@tonic-gate 				tm);
13497c478bd9Sstevel@tonic-gate 
13507c478bd9Sstevel@tonic-gate 	(void) snprintf(newbuf, newsize, "%s%-*s ", *buf ? *buf : "",
13517c478bd9Sstevel@tonic-gate 	    STIME_COLUMN_WIDTH + 1, st_buf);
13527c478bd9Sstevel@tonic-gate 	if (*buf)
13537c478bd9Sstevel@tonic-gate 		free(*buf);
13547c478bd9Sstevel@tonic-gate 	*buf = newbuf;
13557c478bd9Sstevel@tonic-gate }
13567c478bd9Sstevel@tonic-gate 
13577c478bd9Sstevel@tonic-gate #define	STIME_SORTKEY_WIDTH		(sizeof (uint64_t) + sizeof (uint32_t))
13587c478bd9Sstevel@tonic-gate 
13597c478bd9Sstevel@tonic-gate /* ARGSUSED */
13607c478bd9Sstevel@tonic-gate static void
13617c478bd9Sstevel@tonic-gate sortkey_stime(char *buf, int reverse, scf_walkinfo_t *wip)
13627c478bd9Sstevel@tonic-gate {
13637c478bd9Sstevel@tonic-gate 	struct timeval tv;
13647c478bd9Sstevel@tonic-gate 	int r;
13657c478bd9Sstevel@tonic-gate 
13667c478bd9Sstevel@tonic-gate 	if (wip->pg == NULL)
13677c478bd9Sstevel@tonic-gate 		r = get_restarter_time_prop(wip->inst,
13687c478bd9Sstevel@tonic-gate 		    SCF_PROPERTY_STATE_TIMESTAMP, &tv, 0);
13697c478bd9Sstevel@tonic-gate 	else
13707c478bd9Sstevel@tonic-gate 		r = pg_get_single_val(wip->pg, SCF_PROPERTY_STATE_TIMESTAMP,
13717c478bd9Sstevel@tonic-gate 		    SCF_TYPE_TIME, &tv, NULL, 0);
13727c478bd9Sstevel@tonic-gate 
13737c478bd9Sstevel@tonic-gate 	if (r == 0) {
13747c478bd9Sstevel@tonic-gate 		int64_t sec;
13757c478bd9Sstevel@tonic-gate 		int32_t us;
13767c478bd9Sstevel@tonic-gate 
13777c478bd9Sstevel@tonic-gate 		/* Stick it straight into the buffer. */
13787c478bd9Sstevel@tonic-gate 		sec = tv.tv_sec;
13797c478bd9Sstevel@tonic-gate 		us = tv.tv_usec;
13807c478bd9Sstevel@tonic-gate 
13817c478bd9Sstevel@tonic-gate 		sec = BE_64(sec);
13827c478bd9Sstevel@tonic-gate 		us = BE_32(us);
13837c478bd9Sstevel@tonic-gate 		bcopy(&sec, buf, sizeof (sec));
13847c478bd9Sstevel@tonic-gate 		bcopy(&us, buf + sizeof (sec), sizeof (us));
13857c478bd9Sstevel@tonic-gate 	} else {
13867c478bd9Sstevel@tonic-gate 		bzero(buf, STIME_SORTKEY_WIDTH);
13877c478bd9Sstevel@tonic-gate 	}
13887c478bd9Sstevel@tonic-gate 
13897c478bd9Sstevel@tonic-gate 	if (reverse)
13907c478bd9Sstevel@tonic-gate 		reverse_bytes(buf, STIME_SORTKEY_WIDTH);
13917c478bd9Sstevel@tonic-gate }
13927c478bd9Sstevel@tonic-gate 
13937c478bd9Sstevel@tonic-gate 
13947c478bd9Sstevel@tonic-gate /*
13957c478bd9Sstevel@tonic-gate  * Information about columns which can be displayed.  If you add something,
13967c478bd9Sstevel@tonic-gate  * check MAX_COLUMN_NAME_LENGTH_STR & update description_of_column() below.
13977c478bd9Sstevel@tonic-gate  */
13987c478bd9Sstevel@tonic-gate static const struct column columns[] = {
13997c478bd9Sstevel@tonic-gate 	{ "CTID", CTID_COLUMN_WIDTH, sprint_ctid,
14007c478bd9Sstevel@tonic-gate 		CTID_SORTKEY_WIDTH, sortkey_ctid },
14017c478bd9Sstevel@tonic-gate 	{ "DESC", DESC_COLUMN_WIDTH, sprint_desc,
14027c478bd9Sstevel@tonic-gate 		DESC_COLUMN_WIDTH, sortkey_desc },
14037c478bd9Sstevel@tonic-gate 	{ "FMRI", FMRI_COLUMN_WIDTH, sprint_fmri,
14047c478bd9Sstevel@tonic-gate 		FMRI_COLUMN_WIDTH, sortkey_fmri },
14057c478bd9Sstevel@tonic-gate 	{ "INST", COMPONENT_COLUMN_WIDTH, sprint_instance,
14067c478bd9Sstevel@tonic-gate 		COMPONENT_COLUMN_WIDTH, sortkey_instance },
14077c478bd9Sstevel@tonic-gate 	{ "N", 1,  sprint_n, 1, sortkey_nstate },
14087c478bd9Sstevel@tonic-gate 	{ "NSTA", 4, sprint_nsta, 1, sortkey_nstate },
14097c478bd9Sstevel@tonic-gate 	{ "NSTATE", MAX_SCF_STATE_STRING_SZ - 1, sprint_nstate,
14107c478bd9Sstevel@tonic-gate 		1, sortkey_nstate },
14117c478bd9Sstevel@tonic-gate 	{ "S", 2, sprint_s, 1, sortkey_state },
14127c478bd9Sstevel@tonic-gate 	{ "SCOPE", COMPONENT_COLUMN_WIDTH, sprint_scope,
14137c478bd9Sstevel@tonic-gate 		COMPONENT_COLUMN_WIDTH, sortkey_scope },
14147c478bd9Sstevel@tonic-gate 	{ "SN", 2, sprint_sn, 2, sortkey_sn },
14157c478bd9Sstevel@tonic-gate 	{ "SVC", COMPONENT_COLUMN_WIDTH, sprint_service,
14167c478bd9Sstevel@tonic-gate 		COMPONENT_COLUMN_WIDTH, sortkey_service },
14177c478bd9Sstevel@tonic-gate 	{ "STA", 4, sprint_sta, 1, sortkey_state },
14187c478bd9Sstevel@tonic-gate 	{ "STATE", MAX_SCF_STATE_STRING_SZ - 1 + 1, sprint_state,
14197c478bd9Sstevel@tonic-gate 		1, sortkey_state },
14207c478bd9Sstevel@tonic-gate 	{ "STIME", STIME_COLUMN_WIDTH, sprint_stime,
14217c478bd9Sstevel@tonic-gate 		STIME_SORTKEY_WIDTH, sortkey_stime },
14227c478bd9Sstevel@tonic-gate };
14237c478bd9Sstevel@tonic-gate 
14247c478bd9Sstevel@tonic-gate #define	MAX_COLUMN_NAME_LENGTH_STR	"6"
14257c478bd9Sstevel@tonic-gate 
14267c478bd9Sstevel@tonic-gate static const int ncolumns = sizeof (columns) / sizeof (columns[0]);
14277c478bd9Sstevel@tonic-gate 
14287c478bd9Sstevel@tonic-gate /*
14297c478bd9Sstevel@tonic-gate  * Necessary thanks to gettext() & xgettext.
14307c478bd9Sstevel@tonic-gate  */
14317c478bd9Sstevel@tonic-gate static const char *
14327c478bd9Sstevel@tonic-gate description_of_column(int c)
14337c478bd9Sstevel@tonic-gate {
14347c478bd9Sstevel@tonic-gate 	const char *s = NULL;
14357c478bd9Sstevel@tonic-gate 
14367c478bd9Sstevel@tonic-gate 	switch (c) {
14377c478bd9Sstevel@tonic-gate 	case 0:
14387c478bd9Sstevel@tonic-gate 		s = gettext("contract ID for service (see contract(4))");
14397c478bd9Sstevel@tonic-gate 		break;
14407c478bd9Sstevel@tonic-gate 	case 1:
14417c478bd9Sstevel@tonic-gate 		s = gettext("human-readable description of the service");
14427c478bd9Sstevel@tonic-gate 		break;
14437c478bd9Sstevel@tonic-gate 	case 2:
14447c478bd9Sstevel@tonic-gate 		s = gettext("Fault Managed Resource Identifier for service");
14457c478bd9Sstevel@tonic-gate 		break;
14467c478bd9Sstevel@tonic-gate 	case 3:
14477c478bd9Sstevel@tonic-gate 		s = gettext("portion of the FMRI indicating service instance");
14487c478bd9Sstevel@tonic-gate 		break;
14497c478bd9Sstevel@tonic-gate 	case 4:
14507c478bd9Sstevel@tonic-gate 		s = gettext("abbreviation for next state (if in transition)");
14517c478bd9Sstevel@tonic-gate 		break;
14527c478bd9Sstevel@tonic-gate 	case 5:
14537c478bd9Sstevel@tonic-gate 		s = gettext("abbreviation for next state (if in transition)");
14547c478bd9Sstevel@tonic-gate 		break;
14557c478bd9Sstevel@tonic-gate 	case 6:
14567c478bd9Sstevel@tonic-gate 		s = gettext("name for next state (if in transition)");
14577c478bd9Sstevel@tonic-gate 		break;
14587c478bd9Sstevel@tonic-gate 	case 7:
14597c478bd9Sstevel@tonic-gate 		s = gettext("abbreviation for current state");
14607c478bd9Sstevel@tonic-gate 		break;
14617c478bd9Sstevel@tonic-gate 	case 8:
14627c478bd9Sstevel@tonic-gate 		s = gettext("name for scope associated with service");
14637c478bd9Sstevel@tonic-gate 		break;
14647c478bd9Sstevel@tonic-gate 	case 9:
14657c478bd9Sstevel@tonic-gate 		s = gettext("abbreviation for current state and next state");
14667c478bd9Sstevel@tonic-gate 		break;
14677c478bd9Sstevel@tonic-gate 	case 10:
14687c478bd9Sstevel@tonic-gate 		s = gettext("portion of the FMRI representing service name");
14697c478bd9Sstevel@tonic-gate 		break;
14707c478bd9Sstevel@tonic-gate 	case 11:
14717c478bd9Sstevel@tonic-gate 		s = gettext("abbreviation for current state");
14727c478bd9Sstevel@tonic-gate 		break;
14737c478bd9Sstevel@tonic-gate 	case 12:
14747c478bd9Sstevel@tonic-gate 		s = gettext("name for current state");
14757c478bd9Sstevel@tonic-gate 		break;
14767c478bd9Sstevel@tonic-gate 	case 13:
14777c478bd9Sstevel@tonic-gate 		s = gettext("time of last state change");
14787c478bd9Sstevel@tonic-gate 		break;
14797c478bd9Sstevel@tonic-gate 	}
14807c478bd9Sstevel@tonic-gate 
14817c478bd9Sstevel@tonic-gate 	assert(s != NULL);
14827c478bd9Sstevel@tonic-gate 	return (s);
14837c478bd9Sstevel@tonic-gate }
14847c478bd9Sstevel@tonic-gate 
14857c478bd9Sstevel@tonic-gate 
14867c478bd9Sstevel@tonic-gate static void
14877c478bd9Sstevel@tonic-gate print_usage(const char *progname, FILE *f, boolean_t do_exit)
14887c478bd9Sstevel@tonic-gate {
14897c478bd9Sstevel@tonic-gate 	(void) fprintf(f, gettext(
14907c478bd9Sstevel@tonic-gate 	    "Usage: %1$s [-aHpv] [-o col[,col ... ]] [-R restarter] "
14917c478bd9Sstevel@tonic-gate 	    "[-sS col] [<service> ...]\n"
14927c478bd9Sstevel@tonic-gate 	    "       %1$s -d | -D [-Hpv] [-o col[,col ... ]] [-sS col] "
14937c478bd9Sstevel@tonic-gate 	    "[<service> ...]\n"
14947c478bd9Sstevel@tonic-gate 	    "       %1$s -l <service> ...\n"
14957c478bd9Sstevel@tonic-gate 	    "       %1$s -x [-v] [<service> ...]\n"
14967c478bd9Sstevel@tonic-gate 	    "       %1$s -?\n"), progname);
14977c478bd9Sstevel@tonic-gate 
14987c478bd9Sstevel@tonic-gate 	if (do_exit)
14997c478bd9Sstevel@tonic-gate 		exit(UU_EXIT_USAGE);
15007c478bd9Sstevel@tonic-gate }
15017c478bd9Sstevel@tonic-gate 
15027c478bd9Sstevel@tonic-gate #define	argserr(progname)	print_usage(progname, stderr, B_TRUE)
15037c478bd9Sstevel@tonic-gate 
15047c478bd9Sstevel@tonic-gate static void
15057c478bd9Sstevel@tonic-gate print_help(const char *progname)
15067c478bd9Sstevel@tonic-gate {
15077c478bd9Sstevel@tonic-gate 	int i;
15087c478bd9Sstevel@tonic-gate 
15097c478bd9Sstevel@tonic-gate 	print_usage(progname, stdout, B_FALSE);
15107c478bd9Sstevel@tonic-gate 
15117c478bd9Sstevel@tonic-gate 	(void) printf(gettext("\n"
15127c478bd9Sstevel@tonic-gate 	"\t-a  list all service instances rather than "
15137c478bd9Sstevel@tonic-gate 	"only those that are enabled\n"
15147c478bd9Sstevel@tonic-gate 	"\t-d  list dependencies of the specified service(s)\n"
15157c478bd9Sstevel@tonic-gate 	"\t-D  list dependents of the specified service(s)\n"
15167c478bd9Sstevel@tonic-gate 	"\t-H  omit header line from output\n"
15177c478bd9Sstevel@tonic-gate 	"\t-l  list detailed information about the specified service(s)\n"
15187c478bd9Sstevel@tonic-gate 	"\t-o  list only the specified columns in the output\n"
15197c478bd9Sstevel@tonic-gate 	"\t-p  list process IDs and names associated with each service\n"
15207c478bd9Sstevel@tonic-gate 	"\t-R  list only those services with the specified restarter\n"
15217c478bd9Sstevel@tonic-gate 	"\t-s  sort output in ascending order by the specified column(s)\n"
15227c478bd9Sstevel@tonic-gate 	"\t-S  sort output in descending order by the specified column(s)\n"
15237c478bd9Sstevel@tonic-gate 	"\t-v  list verbose information appropriate to the type of output\n"
15247c478bd9Sstevel@tonic-gate 	"\t-x  explain the status of services that might require maintenance,\n"
15257c478bd9Sstevel@tonic-gate 	"\t    or explain the status of the specified service(s)\n"
15267c478bd9Sstevel@tonic-gate 	"\n\t"
15277c478bd9Sstevel@tonic-gate 	"Services can be specified using an FMRI, abbreviation, or fnmatch(5)\n"
15287c478bd9Sstevel@tonic-gate 	"\tpattern, as shown in these examples for svc:/network/smtp:sendmail\n"
15297c478bd9Sstevel@tonic-gate 	"\n"
15307c478bd9Sstevel@tonic-gate 	"\t%1$s [opts] svc:/network/smtp:sendmail\n"
15317c478bd9Sstevel@tonic-gate 	"\t%1$s [opts] network/smtp:sendmail\n"
15327c478bd9Sstevel@tonic-gate 	"\t%1$s [opts] network/*mail\n"
15337c478bd9Sstevel@tonic-gate 	"\t%1$s [opts] network/smtp\n"
15347c478bd9Sstevel@tonic-gate 	"\t%1$s [opts] smtp:sendmail\n"
15357c478bd9Sstevel@tonic-gate 	"\t%1$s [opts] smtp\n"
15367c478bd9Sstevel@tonic-gate 	"\t%1$s [opts] sendmail\n"
15377c478bd9Sstevel@tonic-gate 	"\n\t"
15387c478bd9Sstevel@tonic-gate 	"Columns for output or sorting can be specified using these names:\n"
15397c478bd9Sstevel@tonic-gate 	"\n"), progname);
15407c478bd9Sstevel@tonic-gate 
15417c478bd9Sstevel@tonic-gate 	for (i = 0; i < ncolumns; i++) {
15427c478bd9Sstevel@tonic-gate 		(void) printf("\t%-" MAX_COLUMN_NAME_LENGTH_STR "s  %s\n",
15437c478bd9Sstevel@tonic-gate 		    columns[i].name, description_of_column(i));
15447c478bd9Sstevel@tonic-gate 	}
15457c478bd9Sstevel@tonic-gate }
15467c478bd9Sstevel@tonic-gate 
15477c478bd9Sstevel@tonic-gate 
15487c478bd9Sstevel@tonic-gate /*
15497c478bd9Sstevel@tonic-gate  * A getsubopt()-like function which returns an index into the columns table.
15507c478bd9Sstevel@tonic-gate  * On success, *optionp is set to point to the next sub-option, or the
15517c478bd9Sstevel@tonic-gate  * terminating null if there are none.
15527c478bd9Sstevel@tonic-gate  */
15537c478bd9Sstevel@tonic-gate static int
15547c478bd9Sstevel@tonic-gate getcolumnopt(char **optionp)
15557c478bd9Sstevel@tonic-gate {
15567c478bd9Sstevel@tonic-gate 	char *str = *optionp, *cp;
15577c478bd9Sstevel@tonic-gate 	int i;
15587c478bd9Sstevel@tonic-gate 
15597c478bd9Sstevel@tonic-gate 	assert(optionp != NULL);
15607c478bd9Sstevel@tonic-gate 	assert(*optionp != NULL);
15617c478bd9Sstevel@tonic-gate 
15627c478bd9Sstevel@tonic-gate 	cp = strchr(*optionp, ',');
15637c478bd9Sstevel@tonic-gate 	if (cp != NULL)
15647c478bd9Sstevel@tonic-gate 		*cp = '\0';
15657c478bd9Sstevel@tonic-gate 
15667c478bd9Sstevel@tonic-gate 	for (i = 0; i < ncolumns; ++i) {
15677c478bd9Sstevel@tonic-gate 		if (strcasecmp(str, columns[i].name) == 0) {
15687c478bd9Sstevel@tonic-gate 			if (cp != NULL)
15697c478bd9Sstevel@tonic-gate 				*optionp = cp + 1;
15707c478bd9Sstevel@tonic-gate 			else
15717c478bd9Sstevel@tonic-gate 				*optionp = strchr(*optionp, '\0');
15727c478bd9Sstevel@tonic-gate 
15737c478bd9Sstevel@tonic-gate 			return (i);
15747c478bd9Sstevel@tonic-gate 		}
15757c478bd9Sstevel@tonic-gate 	}
15767c478bd9Sstevel@tonic-gate 
15777c478bd9Sstevel@tonic-gate 	return (-1);
15787c478bd9Sstevel@tonic-gate }
15797c478bd9Sstevel@tonic-gate 
15807c478bd9Sstevel@tonic-gate static void
15817c478bd9Sstevel@tonic-gate print_header()
15827c478bd9Sstevel@tonic-gate {
15837c478bd9Sstevel@tonic-gate 	int i;
15847c478bd9Sstevel@tonic-gate 	char *line_buf, *cp;
15857c478bd9Sstevel@tonic-gate 
15867c478bd9Sstevel@tonic-gate 	line_buf = safe_malloc(line_sz);
15877c478bd9Sstevel@tonic-gate 	cp = line_buf;
15887c478bd9Sstevel@tonic-gate 	for (i = 0; i < opt_cnum; ++i) {
15897c478bd9Sstevel@tonic-gate 		const struct column * const colp = &columns[opt_columns[i]];
15907c478bd9Sstevel@tonic-gate 
15917c478bd9Sstevel@tonic-gate 		(void) snprintf(cp, colp->width + 1, "%-*s", colp->width,
15927c478bd9Sstevel@tonic-gate 		    colp->name);
15937c478bd9Sstevel@tonic-gate 		cp += colp->width;
15947c478bd9Sstevel@tonic-gate 		*cp++ = ' ';
15957c478bd9Sstevel@tonic-gate 	}
15967c478bd9Sstevel@tonic-gate 
15977c478bd9Sstevel@tonic-gate 	/* Trim the trailing whitespace */
15987c478bd9Sstevel@tonic-gate 	--cp;
15997c478bd9Sstevel@tonic-gate 	while (*cp == ' ')
16007c478bd9Sstevel@tonic-gate 		--cp;
16017c478bd9Sstevel@tonic-gate 	*(cp+1) = '\0';
16027c478bd9Sstevel@tonic-gate 	(void) puts(line_buf);
16037c478bd9Sstevel@tonic-gate 
16047c478bd9Sstevel@tonic-gate 	free(line_buf);
16057c478bd9Sstevel@tonic-gate }
16067c478bd9Sstevel@tonic-gate 
16077c478bd9Sstevel@tonic-gate 
16087c478bd9Sstevel@tonic-gate 
16097c478bd9Sstevel@tonic-gate /*
16107c478bd9Sstevel@tonic-gate  * Long listing (-l) functions.
16117c478bd9Sstevel@tonic-gate  */
16127c478bd9Sstevel@tonic-gate 
16137c478bd9Sstevel@tonic-gate static int
16147c478bd9Sstevel@tonic-gate pidcmp(const void *l, const void *r)
16157c478bd9Sstevel@tonic-gate {
16167c478bd9Sstevel@tonic-gate 	pid_t lp = *(pid_t *)l, rp = *(pid_t *)r;
16177c478bd9Sstevel@tonic-gate 
16187c478bd9Sstevel@tonic-gate 	if (lp < rp)
16197c478bd9Sstevel@tonic-gate 		return (-1);
16207c478bd9Sstevel@tonic-gate 	if (lp > rp)
16217c478bd9Sstevel@tonic-gate 		return (1);
16227c478bd9Sstevel@tonic-gate 	return (0);
16237c478bd9Sstevel@tonic-gate }
16247c478bd9Sstevel@tonic-gate 
16257c478bd9Sstevel@tonic-gate /*
16267c478bd9Sstevel@tonic-gate  * This is the strlen() of the longest label ("description"), plus intercolumn
16277c478bd9Sstevel@tonic-gate  * space.
16287c478bd9Sstevel@tonic-gate  */
16297c478bd9Sstevel@tonic-gate #define	DETAILED_WIDTH	(11 + 2)
16307c478bd9Sstevel@tonic-gate 
16317c478bd9Sstevel@tonic-gate static void
16327c478bd9Sstevel@tonic-gate detailed_list_processes(scf_instance_t *inst)
16337c478bd9Sstevel@tonic-gate {
16347c478bd9Sstevel@tonic-gate 	uint64_t c;
16357c478bd9Sstevel@tonic-gate 	pid_t *pids;
16367c478bd9Sstevel@tonic-gate 	uint_t i, n;
16377c478bd9Sstevel@tonic-gate 	psinfo_t psi;
16387c478bd9Sstevel@tonic-gate 
16397c478bd9Sstevel@tonic-gate 	if (get_restarter_count_prop(inst, scf_property_contract, &c,
16407c478bd9Sstevel@tonic-gate 	    EMPTY_OK) != 0)
16417c478bd9Sstevel@tonic-gate 		return;
16427c478bd9Sstevel@tonic-gate 
16437c478bd9Sstevel@tonic-gate 	if (instance_processes(inst, &pids, &n) != 0)
16447c478bd9Sstevel@tonic-gate 		return;
16457c478bd9Sstevel@tonic-gate 
16467c478bd9Sstevel@tonic-gate 	qsort(pids, n, sizeof (*pids), pidcmp);
16477c478bd9Sstevel@tonic-gate 
16487c478bd9Sstevel@tonic-gate 	for (i = 0; i < n; ++i) {
16497c478bd9Sstevel@tonic-gate 		(void) printf("%-*s%lu", DETAILED_WIDTH, gettext("process"),
16507c478bd9Sstevel@tonic-gate 		    pids[i]);
16517c478bd9Sstevel@tonic-gate 
16527c478bd9Sstevel@tonic-gate 		if (get_psinfo(pids[i], &psi) == 0)
16537c478bd9Sstevel@tonic-gate 			(void) printf(" %.*s", PRARGSZ, psi.pr_psargs);
16547c478bd9Sstevel@tonic-gate 
16557c478bd9Sstevel@tonic-gate 		(void) putchar('\n');
16567c478bd9Sstevel@tonic-gate 	}
16577c478bd9Sstevel@tonic-gate 
16587c478bd9Sstevel@tonic-gate 	free(pids);
16597c478bd9Sstevel@tonic-gate }
16607c478bd9Sstevel@tonic-gate 
16617c478bd9Sstevel@tonic-gate /*
16627c478bd9Sstevel@tonic-gate  * Determines the state of a dependency.  If the FMRI specifies a file, then we
16637c478bd9Sstevel@tonic-gate  * fake up a state based on whether we can access the file.
16647c478bd9Sstevel@tonic-gate  */
16657c478bd9Sstevel@tonic-gate static void
16667c478bd9Sstevel@tonic-gate get_fmri_state(char *fmri, char *state, size_t state_sz)
16677c478bd9Sstevel@tonic-gate {
16687c478bd9Sstevel@tonic-gate 	char *lfmri;
16697c478bd9Sstevel@tonic-gate 	const char *svc_name, *inst_name, *pg_name, *path;
16707c478bd9Sstevel@tonic-gate 	scf_service_t *svc;
16717c478bd9Sstevel@tonic-gate 	scf_instance_t *inst;
16727c478bd9Sstevel@tonic-gate 	scf_iter_t *iter;
16737c478bd9Sstevel@tonic-gate 
16747c478bd9Sstevel@tonic-gate 	lfmri = safe_strdup(fmri);
16757c478bd9Sstevel@tonic-gate 
16767c478bd9Sstevel@tonic-gate 	/*
16777c478bd9Sstevel@tonic-gate 	 * Check for file:// dependencies
16787c478bd9Sstevel@tonic-gate 	 */
16797c478bd9Sstevel@tonic-gate 	if (scf_parse_file_fmri(lfmri, NULL, &path) == SCF_SUCCESS) {
16807c478bd9Sstevel@tonic-gate 		struct stat64 statbuf;
16817c478bd9Sstevel@tonic-gate 		const char *msg;
16827c478bd9Sstevel@tonic-gate 
16837c478bd9Sstevel@tonic-gate 		if (stat64(path, &statbuf) == 0)
16847c478bd9Sstevel@tonic-gate 			msg = "online";
16857c478bd9Sstevel@tonic-gate 		else if (errno == ENOENT)
16867c478bd9Sstevel@tonic-gate 			msg = "absent";
16877c478bd9Sstevel@tonic-gate 		else
16887c478bd9Sstevel@tonic-gate 			msg = "unknown";
16897c478bd9Sstevel@tonic-gate 
16907c478bd9Sstevel@tonic-gate 		(void) strlcpy(state, msg, state_sz);
16917c478bd9Sstevel@tonic-gate 		return;
16927c478bd9Sstevel@tonic-gate 	}
16937c478bd9Sstevel@tonic-gate 
16947c478bd9Sstevel@tonic-gate 	/*
16957c478bd9Sstevel@tonic-gate 	 * scf_parse_file_fmri() may have overwritten part of the string, so
16967c478bd9Sstevel@tonic-gate 	 * copy it back.
16977c478bd9Sstevel@tonic-gate 	 */
16987c478bd9Sstevel@tonic-gate 	(void) strcpy(lfmri, fmri);
16997c478bd9Sstevel@tonic-gate 
17007c478bd9Sstevel@tonic-gate 	if (scf_parse_svc_fmri(lfmri, NULL, &svc_name, &inst_name,
17017c478bd9Sstevel@tonic-gate 	    &pg_name, NULL) != SCF_SUCCESS) {
17027c478bd9Sstevel@tonic-gate 		free(lfmri);
17037c478bd9Sstevel@tonic-gate 		(void) strlcpy(state, "invalid", state_sz);
17047c478bd9Sstevel@tonic-gate 		return;
17057c478bd9Sstevel@tonic-gate 	}
17067c478bd9Sstevel@tonic-gate 
17077c478bd9Sstevel@tonic-gate 	free(lfmri);
17087c478bd9Sstevel@tonic-gate 
17097c478bd9Sstevel@tonic-gate 	if (svc_name == NULL || pg_name != NULL) {
17107c478bd9Sstevel@tonic-gate 		(void) strlcpy(state, "invalid", state_sz);
17117c478bd9Sstevel@tonic-gate 		return;
17127c478bd9Sstevel@tonic-gate 	}
17137c478bd9Sstevel@tonic-gate 
17147c478bd9Sstevel@tonic-gate 	if (inst_name != NULL) {
17157c478bd9Sstevel@tonic-gate 		/* instance: get state */
17167c478bd9Sstevel@tonic-gate 		inst = scf_instance_create(h);
17177c478bd9Sstevel@tonic-gate 		if (inst == NULL)
17187c478bd9Sstevel@tonic-gate 			scfdie();
17197c478bd9Sstevel@tonic-gate 
17207c478bd9Sstevel@tonic-gate 		if (scf_handle_decode_fmri(h, fmri, NULL, NULL, inst, NULL,
17217c478bd9Sstevel@tonic-gate 		    NULL, SCF_DECODE_FMRI_EXACT) == SCF_SUCCESS)
17227c478bd9Sstevel@tonic-gate 			get_restarter_string_prop(inst, scf_property_state,
17237c478bd9Sstevel@tonic-gate 			    state, state_sz);
17247c478bd9Sstevel@tonic-gate 		else {
17257c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
17267c478bd9Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
17277c478bd9Sstevel@tonic-gate 				(void) strlcpy(state, "invalid", state_sz);
17287c478bd9Sstevel@tonic-gate 				break;
17297c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_FOUND:
17307c478bd9Sstevel@tonic-gate 				(void) strlcpy(state, "absent", state_sz);
17317c478bd9Sstevel@tonic-gate 				break;
17327c478bd9Sstevel@tonic-gate 
17337c478bd9Sstevel@tonic-gate 			default:
17347c478bd9Sstevel@tonic-gate 				scfdie();
17357c478bd9Sstevel@tonic-gate 			}
17367c478bd9Sstevel@tonic-gate 		}
17377c478bd9Sstevel@tonic-gate 
17387c478bd9Sstevel@tonic-gate 		scf_instance_destroy(inst);
17397c478bd9Sstevel@tonic-gate 		return;
17407c478bd9Sstevel@tonic-gate 	}
17417c478bd9Sstevel@tonic-gate 
17427c478bd9Sstevel@tonic-gate 	/*
17437c478bd9Sstevel@tonic-gate 	 * service: If only one instance, use that state.  Otherwise, say
17447c478bd9Sstevel@tonic-gate 	 * "multiple".
17457c478bd9Sstevel@tonic-gate 	 */
17467c478bd9Sstevel@tonic-gate 	if ((svc = scf_service_create(h)) == NULL ||
17477c478bd9Sstevel@tonic-gate 	    (inst = scf_instance_create(h)) == NULL ||
17487c478bd9Sstevel@tonic-gate 	    (iter = scf_iter_create(h)) == NULL)
17497c478bd9Sstevel@tonic-gate 		scfdie();
17507c478bd9Sstevel@tonic-gate 
17517c478bd9Sstevel@tonic-gate 	if (scf_handle_decode_fmri(h, fmri, NULL, svc, NULL, NULL, NULL,
17527c478bd9Sstevel@tonic-gate 	    SCF_DECODE_FMRI_EXACT) != SCF_SUCCESS) {
17537c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
17547c478bd9Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
17557c478bd9Sstevel@tonic-gate 			(void) strlcpy(state, "invalid", state_sz);
17567c478bd9Sstevel@tonic-gate 			goto out;
17577c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
17587c478bd9Sstevel@tonic-gate 			(void) strlcpy(state, "absent", state_sz);
17597c478bd9Sstevel@tonic-gate 			goto out;
17607c478bd9Sstevel@tonic-gate 
17617c478bd9Sstevel@tonic-gate 		default:
17627c478bd9Sstevel@tonic-gate 			scfdie();
17637c478bd9Sstevel@tonic-gate 		}
17647c478bd9Sstevel@tonic-gate 	}
17657c478bd9Sstevel@tonic-gate 
17667c478bd9Sstevel@tonic-gate 	if (scf_iter_service_instances(iter, svc) != SCF_SUCCESS)
17677c478bd9Sstevel@tonic-gate 		scfdie();
17687c478bd9Sstevel@tonic-gate 
17697c478bd9Sstevel@tonic-gate 	switch (scf_iter_next_instance(iter, inst)) {
17707c478bd9Sstevel@tonic-gate 	case 0:
17717c478bd9Sstevel@tonic-gate 		(void) strlcpy(state, "absent", state_sz);
17727c478bd9Sstevel@tonic-gate 		goto out;
17737c478bd9Sstevel@tonic-gate 
17747c478bd9Sstevel@tonic-gate 	case 1:
17757c478bd9Sstevel@tonic-gate 		break;
17767c478bd9Sstevel@tonic-gate 
17777c478bd9Sstevel@tonic-gate 	default:
17787c478bd9Sstevel@tonic-gate 		scfdie();
17797c478bd9Sstevel@tonic-gate 	}
17807c478bd9Sstevel@tonic-gate 
17817c478bd9Sstevel@tonic-gate 	/* Get the state in case this is the only instance. */
17827c478bd9Sstevel@tonic-gate 	get_restarter_string_prop(inst, scf_property_state, state, state_sz);
17837c478bd9Sstevel@tonic-gate 
17847c478bd9Sstevel@tonic-gate 	switch (scf_iter_next_instance(iter, inst)) {
17857c478bd9Sstevel@tonic-gate 	case 0:
17867c478bd9Sstevel@tonic-gate 		break;
17877c478bd9Sstevel@tonic-gate 
17887c478bd9Sstevel@tonic-gate 	case 1:
17897c478bd9Sstevel@tonic-gate 		/* Nope, multiple instances. */
17907c478bd9Sstevel@tonic-gate 		(void) strlcpy(state, "multiple", state_sz);
17917c478bd9Sstevel@tonic-gate 		goto out;
17927c478bd9Sstevel@tonic-gate 
17937c478bd9Sstevel@tonic-gate 	default:
17947c478bd9Sstevel@tonic-gate 		scfdie();
17957c478bd9Sstevel@tonic-gate 	}
17967c478bd9Sstevel@tonic-gate 
17977c478bd9Sstevel@tonic-gate out:
17987c478bd9Sstevel@tonic-gate 	scf_iter_destroy(iter);
17997c478bd9Sstevel@tonic-gate 	scf_instance_destroy(inst);
18007c478bd9Sstevel@tonic-gate 	scf_service_destroy(svc);
18017c478bd9Sstevel@tonic-gate }
18027c478bd9Sstevel@tonic-gate 
18037c478bd9Sstevel@tonic-gate static void
18047c478bd9Sstevel@tonic-gate print_detailed_dependency(scf_propertygroup_t *pg)
18057c478bd9Sstevel@tonic-gate {
18067c478bd9Sstevel@tonic-gate 	scf_property_t *eprop;
18077c478bd9Sstevel@tonic-gate 	scf_iter_t *iter;
18087c478bd9Sstevel@tonic-gate 	scf_type_t ty;
18097c478bd9Sstevel@tonic-gate 	char *val_buf;
18107c478bd9Sstevel@tonic-gate 	int i;
18117c478bd9Sstevel@tonic-gate 
18127c478bd9Sstevel@tonic-gate 	if ((eprop = scf_property_create(h)) == NULL ||
18137c478bd9Sstevel@tonic-gate 	    (iter = scf_iter_create(h)) == NULL)
18147c478bd9Sstevel@tonic-gate 		scfdie();
18157c478bd9Sstevel@tonic-gate 
18167c478bd9Sstevel@tonic-gate 	val_buf = safe_malloc(max_scf_value_length + 1);
18177c478bd9Sstevel@tonic-gate 
18187c478bd9Sstevel@tonic-gate 	if (scf_pg_get_property(pg, SCF_PROPERTY_ENTITIES, eprop) !=
18197c478bd9Sstevel@tonic-gate 	    SCF_SUCCESS ||
18207c478bd9Sstevel@tonic-gate 	    scf_property_type(eprop, &ty) != SCF_SUCCESS ||
18217c478bd9Sstevel@tonic-gate 	    ty != SCF_TYPE_FMRI)
18227c478bd9Sstevel@tonic-gate 		return;
18237c478bd9Sstevel@tonic-gate 
18247c478bd9Sstevel@tonic-gate 	(void) printf("%-*s", DETAILED_WIDTH, gettext("dependency"));
18257c478bd9Sstevel@tonic-gate 
18267c478bd9Sstevel@tonic-gate 	/* Print the grouping */
18277c478bd9Sstevel@tonic-gate 	if (pg_get_single_val(pg, SCF_PROPERTY_GROUPING, SCF_TYPE_ASTRING,
18287c478bd9Sstevel@tonic-gate 	    val_buf, max_scf_value_length + 1, 0) == 0)
18297c478bd9Sstevel@tonic-gate 		(void) fputs(val_buf, stdout);
18307c478bd9Sstevel@tonic-gate 	else
18317c478bd9Sstevel@tonic-gate 		(void) putchar('?');
18327c478bd9Sstevel@tonic-gate 
18337c478bd9Sstevel@tonic-gate 	(void) putchar('/');
18347c478bd9Sstevel@tonic-gate 
18357c478bd9Sstevel@tonic-gate 	if (pg_get_single_val(pg, SCF_PROPERTY_RESTART_ON, SCF_TYPE_ASTRING,
18367c478bd9Sstevel@tonic-gate 	    val_buf, max_scf_value_length + 1, 0) == 0)
18377c478bd9Sstevel@tonic-gate 		(void) fputs(val_buf, stdout);
18387c478bd9Sstevel@tonic-gate 	else
18397c478bd9Sstevel@tonic-gate 		(void) putchar('?');
18407c478bd9Sstevel@tonic-gate 
18417c478bd9Sstevel@tonic-gate 	/* Print the dependency entities. */
18427c478bd9Sstevel@tonic-gate 	if (scf_iter_property_values(iter, eprop) == -1)
18437c478bd9Sstevel@tonic-gate 		scfdie();
18447c478bd9Sstevel@tonic-gate 
18457c478bd9Sstevel@tonic-gate 	while ((i = scf_iter_next_value(iter, g_val)) == 1) {
18467c478bd9Sstevel@tonic-gate 		char state[MAX_SCF_STATE_STRING_SZ];
18477c478bd9Sstevel@tonic-gate 
18487c478bd9Sstevel@tonic-gate 		if (scf_value_get_astring(g_val, val_buf,
18497c478bd9Sstevel@tonic-gate 		    max_scf_value_length + 1) < 0)
18507c478bd9Sstevel@tonic-gate 			scfdie();
18517c478bd9Sstevel@tonic-gate 
18527c478bd9Sstevel@tonic-gate 		(void) putchar(' ');
18537c478bd9Sstevel@tonic-gate 		(void) fputs(val_buf, stdout);
18547c478bd9Sstevel@tonic-gate 
18557c478bd9Sstevel@tonic-gate 		/* Print the state. */
18567c478bd9Sstevel@tonic-gate 		state[0] = '-';
18577c478bd9Sstevel@tonic-gate 		state[1] = '\0';
18587c478bd9Sstevel@tonic-gate 
18597c478bd9Sstevel@tonic-gate 		get_fmri_state(val_buf, state, sizeof (state));
18607c478bd9Sstevel@tonic-gate 
18617c478bd9Sstevel@tonic-gate 		(void) printf(" (%s)", state);
18627c478bd9Sstevel@tonic-gate 	}
18637c478bd9Sstevel@tonic-gate 	if (i == -1)
18647c478bd9Sstevel@tonic-gate 		scfdie();
18657c478bd9Sstevel@tonic-gate 
18667c478bd9Sstevel@tonic-gate 	(void) putchar('\n');
18677c478bd9Sstevel@tonic-gate 
18687c478bd9Sstevel@tonic-gate 	free(val_buf);
18697c478bd9Sstevel@tonic-gate 	scf_iter_destroy(iter);
18707c478bd9Sstevel@tonic-gate 	scf_property_destroy(eprop);
18717c478bd9Sstevel@tonic-gate }
18727c478bd9Sstevel@tonic-gate 
18737c478bd9Sstevel@tonic-gate /* ARGSUSED */
18747c478bd9Sstevel@tonic-gate static int
18757c478bd9Sstevel@tonic-gate print_detailed(void *unused, scf_walkinfo_t *wip)
18767c478bd9Sstevel@tonic-gate {
18777c478bd9Sstevel@tonic-gate 	scf_snapshot_t *snap;
18787c478bd9Sstevel@tonic-gate 	scf_propertygroup_t *rpg;
18797c478bd9Sstevel@tonic-gate 	scf_iter_t *pg_iter;
18807c478bd9Sstevel@tonic-gate 
18817c478bd9Sstevel@tonic-gate 	char *buf;
18827c478bd9Sstevel@tonic-gate 	char *timebuf;
18837c478bd9Sstevel@tonic-gate 	size_t tbsz;
18847c478bd9Sstevel@tonic-gate 	int ret;
18857c478bd9Sstevel@tonic-gate 	uint64_t c;
18867c478bd9Sstevel@tonic-gate 	int temp, perm;
18877c478bd9Sstevel@tonic-gate 	struct timeval tv;
18887c478bd9Sstevel@tonic-gate 	time_t stime;
18897c478bd9Sstevel@tonic-gate 	struct tm *tmp;
18907c478bd9Sstevel@tonic-gate 
18917c478bd9Sstevel@tonic-gate 	const char * const fmt = "%-*s%s\n";
18927c478bd9Sstevel@tonic-gate 
18937c478bd9Sstevel@tonic-gate 	assert(wip->pg == NULL);
18947c478bd9Sstevel@tonic-gate 
18957c478bd9Sstevel@tonic-gate 	rpg = scf_pg_create(h);
18967c478bd9Sstevel@tonic-gate 	if (rpg == NULL)
18977c478bd9Sstevel@tonic-gate 		scfdie();
18987c478bd9Sstevel@tonic-gate 
18997c478bd9Sstevel@tonic-gate 	if (first_paragraph)
19007c478bd9Sstevel@tonic-gate 		first_paragraph = 0;
19017c478bd9Sstevel@tonic-gate 	else
19027c478bd9Sstevel@tonic-gate 		(void) putchar('\n');
19037c478bd9Sstevel@tonic-gate 
19047c478bd9Sstevel@tonic-gate 	buf = safe_malloc(max_scf_fmri_length + 1);
19057c478bd9Sstevel@tonic-gate 
19067c478bd9Sstevel@tonic-gate 	if (scf_instance_to_fmri(wip->inst, buf, max_scf_fmri_length + 1) != -1)
19077c478bd9Sstevel@tonic-gate 		(void) printf(fmt, DETAILED_WIDTH, "fmri", buf);
19087c478bd9Sstevel@tonic-gate 
19097c478bd9Sstevel@tonic-gate 	if (common_name_buf == NULL)
19107c478bd9Sstevel@tonic-gate 		common_name_buf = safe_malloc(max_scf_value_length + 1);
19117c478bd9Sstevel@tonic-gate 
19127c478bd9Sstevel@tonic-gate 	if (inst_get_single_val(wip->inst, SCF_PG_TM_COMMON_NAME, locale,
19137c478bd9Sstevel@tonic-gate 	    SCF_TYPE_USTRING, common_name_buf, max_scf_value_length, 0, 1, 1)
19147c478bd9Sstevel@tonic-gate 	    == 0)
19157c478bd9Sstevel@tonic-gate 		(void) printf(fmt, DETAILED_WIDTH, gettext("name"),
19167c478bd9Sstevel@tonic-gate 		    common_name_buf);
19177c478bd9Sstevel@tonic-gate 	else if (inst_get_single_val(wip->inst, SCF_PG_TM_COMMON_NAME, "C",
19187c478bd9Sstevel@tonic-gate 	    SCF_TYPE_USTRING, common_name_buf, max_scf_value_length, 0, 1, 1)
19197c478bd9Sstevel@tonic-gate 	    == 0)
19207c478bd9Sstevel@tonic-gate 		(void) printf(fmt, DETAILED_WIDTH, gettext("name"),
19217c478bd9Sstevel@tonic-gate 		    common_name_buf);
19227c478bd9Sstevel@tonic-gate 
19237c478bd9Sstevel@tonic-gate 	/*
19247c478bd9Sstevel@tonic-gate 	 * Synthesize an 'enabled' property that hides the enabled_ovr
19257c478bd9Sstevel@tonic-gate 	 * implementation from the user.  If the service has been temporarily
19267c478bd9Sstevel@tonic-gate 	 * set to a state other than its permanent value, alert the user with
19277c478bd9Sstevel@tonic-gate 	 * a '(temporary)' message.
19287c478bd9Sstevel@tonic-gate 	 */
19297c478bd9Sstevel@tonic-gate 	perm = instance_enabled(wip->inst, B_FALSE);
19307c478bd9Sstevel@tonic-gate 	temp = instance_enabled(wip->inst, B_TRUE);
19317c478bd9Sstevel@tonic-gate 	if (temp != -1) {
19327c478bd9Sstevel@tonic-gate 		if (temp != perm)
19337c478bd9Sstevel@tonic-gate 			(void) printf(gettext("%-*s%s (temporary)\n"),
19347c478bd9Sstevel@tonic-gate 			    DETAILED_WIDTH, gettext("enabled"),
19357c478bd9Sstevel@tonic-gate 			    temp ? gettext("true") : gettext("false"));
19367c478bd9Sstevel@tonic-gate 		else
19377c478bd9Sstevel@tonic-gate 			(void) printf(fmt, DETAILED_WIDTH,
19387c478bd9Sstevel@tonic-gate 			    gettext("enabled"), temp ? gettext("true") :
19397c478bd9Sstevel@tonic-gate 			    gettext("false"));
19407c478bd9Sstevel@tonic-gate 	} else if (perm != -1) {
19417c478bd9Sstevel@tonic-gate 		(void) printf(fmt, DETAILED_WIDTH, gettext("enabled"),
19427c478bd9Sstevel@tonic-gate 		    perm ? gettext("true") : gettext("false"));
19437c478bd9Sstevel@tonic-gate 	}
19447c478bd9Sstevel@tonic-gate 
19457c478bd9Sstevel@tonic-gate 	/*
19467c478bd9Sstevel@tonic-gate 	 * Property values may be longer than max_scf_fmri_length, but these
19477c478bd9Sstevel@tonic-gate 	 * shouldn't be, so we'll just reuse buf.  The user can use svcprop if
19487c478bd9Sstevel@tonic-gate 	 * he suspects something fishy.
19497c478bd9Sstevel@tonic-gate 	 */
19507c478bd9Sstevel@tonic-gate 	if (scf_instance_get_pg(wip->inst, SCF_PG_RESTARTER, rpg) != 0) {
19517c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_NOT_FOUND)
19527c478bd9Sstevel@tonic-gate 			scfdie();
19537c478bd9Sstevel@tonic-gate 
19547c478bd9Sstevel@tonic-gate 		scf_pg_destroy(rpg);
19557c478bd9Sstevel@tonic-gate 		rpg = NULL;
19567c478bd9Sstevel@tonic-gate 	}
19577c478bd9Sstevel@tonic-gate 
19587c478bd9Sstevel@tonic-gate 	if (rpg) {
19597c478bd9Sstevel@tonic-gate 		if (pg_get_single_val(rpg, scf_property_state, SCF_TYPE_ASTRING,
19607c478bd9Sstevel@tonic-gate 		    buf, max_scf_fmri_length + 1, 0) == 0)
19617c478bd9Sstevel@tonic-gate 			(void) printf(fmt, DETAILED_WIDTH, gettext("state"),
19627c478bd9Sstevel@tonic-gate 			    buf);
19637c478bd9Sstevel@tonic-gate 
19647c478bd9Sstevel@tonic-gate 		if (pg_get_single_val(rpg, scf_property_next_state,
19657c478bd9Sstevel@tonic-gate 		    SCF_TYPE_ASTRING, buf, max_scf_fmri_length + 1, 0) == 0)
19667c478bd9Sstevel@tonic-gate 			(void) printf(fmt, DETAILED_WIDTH,
19677c478bd9Sstevel@tonic-gate 			    gettext("next_state"), buf);
19687c478bd9Sstevel@tonic-gate 
19697c478bd9Sstevel@tonic-gate 		if (pg_get_single_val(rpg, SCF_PROPERTY_STATE_TIMESTAMP,
19707c478bd9Sstevel@tonic-gate 		    SCF_TYPE_TIME, &tv, NULL, 0) == 0) {
19717c478bd9Sstevel@tonic-gate 			stime = tv.tv_sec;
19727c478bd9Sstevel@tonic-gate 			tmp = localtime(&stime);
19737c478bd9Sstevel@tonic-gate 			for (tbsz = 50; ; tbsz *= 2) {
19747c478bd9Sstevel@tonic-gate 				timebuf = safe_malloc(tbsz);
19757c478bd9Sstevel@tonic-gate 				if (strftime(timebuf, tbsz, NULL, tmp) != 0)
19767c478bd9Sstevel@tonic-gate 					break;
19777c478bd9Sstevel@tonic-gate 				free(timebuf);
19787c478bd9Sstevel@tonic-gate 			}
19797c478bd9Sstevel@tonic-gate 			(void) printf(fmt, DETAILED_WIDTH,
19807c478bd9Sstevel@tonic-gate 			    gettext("state_time"),
19817c478bd9Sstevel@tonic-gate 			    timebuf);
19827c478bd9Sstevel@tonic-gate 			free(timebuf);
19837c478bd9Sstevel@tonic-gate 		}
19847c478bd9Sstevel@tonic-gate 
19857c478bd9Sstevel@tonic-gate 		if (pg_get_single_val(rpg, SCF_PROPERTY_ALT_LOGFILE,
19867c478bd9Sstevel@tonic-gate 		    SCF_TYPE_ASTRING, buf, max_scf_fmri_length + 1, 0) == 0)
1987*b7185059Srm88369 			(void) printf(fmt, DETAILED_WIDTH,
1988*b7185059Srm88369 			    gettext("alt_logfile"), buf);
19897c478bd9Sstevel@tonic-gate 
19907c478bd9Sstevel@tonic-gate 		if (pg_get_single_val(rpg, SCF_PROPERTY_LOGFILE,
19917c478bd9Sstevel@tonic-gate 		    SCF_TYPE_ASTRING, buf, max_scf_fmri_length + 1, 0) == 0)
1992*b7185059Srm88369 			(void) printf(fmt, DETAILED_WIDTH, gettext("logfile"),
1993*b7185059Srm88369 			    buf);
1994*b7185059Srm88369 	}
19957c478bd9Sstevel@tonic-gate 
19967c478bd9Sstevel@tonic-gate 	if (inst_get_single_val(wip->inst, SCF_PG_GENERAL,
19977c478bd9Sstevel@tonic-gate 	    SCF_PROPERTY_RESTARTER, SCF_TYPE_ASTRING, buf,
19987c478bd9Sstevel@tonic-gate 	    max_scf_fmri_length + 1, 0, 0, 1) == 0)
19997c478bd9Sstevel@tonic-gate 		(void) printf(fmt, DETAILED_WIDTH, gettext("restarter"), buf);
20007c478bd9Sstevel@tonic-gate 	else
20017c478bd9Sstevel@tonic-gate 		(void) printf(fmt, DETAILED_WIDTH, gettext("restarter"),
20027c478bd9Sstevel@tonic-gate 		    SCF_SERVICE_STARTD);
20037c478bd9Sstevel@tonic-gate 
20047c478bd9Sstevel@tonic-gate 	free(buf);
20057c478bd9Sstevel@tonic-gate 
20067c478bd9Sstevel@tonic-gate 	if (rpg) {
20077c478bd9Sstevel@tonic-gate 		scf_iter_t *iter;
20087c478bd9Sstevel@tonic-gate 
20097c478bd9Sstevel@tonic-gate 		if ((iter = scf_iter_create(h)) == NULL)
20107c478bd9Sstevel@tonic-gate 			scfdie();
20117c478bd9Sstevel@tonic-gate 
20127c478bd9Sstevel@tonic-gate 		if (scf_pg_get_property(rpg, scf_property_contract, g_prop) ==
20137c478bd9Sstevel@tonic-gate 		    0) {
20147c478bd9Sstevel@tonic-gate 			if (scf_property_is_type(g_prop, SCF_TYPE_COUNT) == 0) {
20157c478bd9Sstevel@tonic-gate 				(void) printf("%-*s", DETAILED_WIDTH,
20167c478bd9Sstevel@tonic-gate 				    "contract_id");
20177c478bd9Sstevel@tonic-gate 
20187c478bd9Sstevel@tonic-gate 				if (scf_iter_property_values(iter, g_prop) != 0)
20197c478bd9Sstevel@tonic-gate 					scfdie();
20207c478bd9Sstevel@tonic-gate 
20217c478bd9Sstevel@tonic-gate 				for (;;) {
20227c478bd9Sstevel@tonic-gate 					ret = scf_iter_next_value(iter, g_val);
20237c478bd9Sstevel@tonic-gate 					if (ret == -1)
20247c478bd9Sstevel@tonic-gate 						scfdie();
20257c478bd9Sstevel@tonic-gate 					if (ret == 0)
20267c478bd9Sstevel@tonic-gate 						break;
20277c478bd9Sstevel@tonic-gate 
20287c478bd9Sstevel@tonic-gate 					if (scf_value_get_count(g_val, &c) != 0)
20297c478bd9Sstevel@tonic-gate 						scfdie();
20307c478bd9Sstevel@tonic-gate 					(void) printf("%lu ", (ctid_t)c);
20317c478bd9Sstevel@tonic-gate 				}
20327c478bd9Sstevel@tonic-gate 
20337c478bd9Sstevel@tonic-gate 				(void) putchar('\n');
20347c478bd9Sstevel@tonic-gate 			} else {
20357c478bd9Sstevel@tonic-gate 				if (scf_error() != SCF_ERROR_TYPE_MISMATCH)
20367c478bd9Sstevel@tonic-gate 					scfdie();
20377c478bd9Sstevel@tonic-gate 			}
20387c478bd9Sstevel@tonic-gate 		} else {
20397c478bd9Sstevel@tonic-gate 			if (scf_error() != SCF_ERROR_NOT_FOUND)
20407c478bd9Sstevel@tonic-gate 				scfdie();
20417c478bd9Sstevel@tonic-gate 		}
20427c478bd9Sstevel@tonic-gate 
20437c478bd9Sstevel@tonic-gate 		scf_iter_destroy(iter);
20447c478bd9Sstevel@tonic-gate 	} else {
20457c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_NOT_FOUND)
20467c478bd9Sstevel@tonic-gate 			scfdie();
20477c478bd9Sstevel@tonic-gate 	}
20487c478bd9Sstevel@tonic-gate 
20497c478bd9Sstevel@tonic-gate 	scf_pg_destroy(rpg);
20507c478bd9Sstevel@tonic-gate 
20517c478bd9Sstevel@tonic-gate 	/* Dependencies. */
20527c478bd9Sstevel@tonic-gate 	if ((pg_iter = scf_iter_create(h)) == NULL)
20537c478bd9Sstevel@tonic-gate 		scfdie();
20547c478bd9Sstevel@tonic-gate 
20557c478bd9Sstevel@tonic-gate 	snap = get_running_snapshot(wip->inst);
20567c478bd9Sstevel@tonic-gate 
20577c478bd9Sstevel@tonic-gate 	if (scf_iter_instance_pgs_typed_composed(pg_iter, wip->inst, snap,
20587c478bd9Sstevel@tonic-gate 	    SCF_GROUP_DEPENDENCY) != SCF_SUCCESS)
20597c478bd9Sstevel@tonic-gate 		scfdie();
20607c478bd9Sstevel@tonic-gate 
20617c478bd9Sstevel@tonic-gate 	while ((ret = scf_iter_next_pg(pg_iter, g_pg)) == 1)
20627c478bd9Sstevel@tonic-gate 		print_detailed_dependency(g_pg);
20637c478bd9Sstevel@tonic-gate 	if (ret == -1)
20647c478bd9Sstevel@tonic-gate 		scfdie();
20657c478bd9Sstevel@tonic-gate 
20667c478bd9Sstevel@tonic-gate 	scf_snapshot_destroy(snap);
20677c478bd9Sstevel@tonic-gate 	scf_iter_destroy(pg_iter);
20687c478bd9Sstevel@tonic-gate 
20697c478bd9Sstevel@tonic-gate 	if (opt_processes)
20707c478bd9Sstevel@tonic-gate 		detailed_list_processes(wip->inst);
20717c478bd9Sstevel@tonic-gate 
20727c478bd9Sstevel@tonic-gate 	return (0);
20737c478bd9Sstevel@tonic-gate }
20747c478bd9Sstevel@tonic-gate 
20757c478bd9Sstevel@tonic-gate /*
20767c478bd9Sstevel@tonic-gate  * Append a one-lined description of each process in inst's contract(s) and
20777c478bd9Sstevel@tonic-gate  * return the augmented string.
20787c478bd9Sstevel@tonic-gate  */
20797c478bd9Sstevel@tonic-gate static char *
20807c478bd9Sstevel@tonic-gate add_processes(char *line, scf_instance_t *inst, scf_propertygroup_t *lpg)
20817c478bd9Sstevel@tonic-gate {
20827c478bd9Sstevel@tonic-gate 	pid_t *pids = NULL;
20837c478bd9Sstevel@tonic-gate 	uint_t i, n = 0;
20847c478bd9Sstevel@tonic-gate 
20857c478bd9Sstevel@tonic-gate 	if (lpg == NULL) {
20867c478bd9Sstevel@tonic-gate 		if (instance_processes(inst, &pids, &n) != 0)
20877c478bd9Sstevel@tonic-gate 			return (line);
20887c478bd9Sstevel@tonic-gate 	} else {
20897c478bd9Sstevel@tonic-gate 		scf_iter_t *iter;
20907c478bd9Sstevel@tonic-gate 
20917c478bd9Sstevel@tonic-gate 		if ((iter = scf_iter_create(h)) == NULL)
20927c478bd9Sstevel@tonic-gate 			scfdie();
20937c478bd9Sstevel@tonic-gate 
20947c478bd9Sstevel@tonic-gate 		(void) propvals_to_pids(lpg, scf_property_contract, &pids, &n,
20957c478bd9Sstevel@tonic-gate 		    g_prop, g_val, iter);
20967c478bd9Sstevel@tonic-gate 
20977c478bd9Sstevel@tonic-gate 		scf_iter_destroy(iter);
20987c478bd9Sstevel@tonic-gate 	}
20997c478bd9Sstevel@tonic-gate 
21007c478bd9Sstevel@tonic-gate 	if (n == 0)
21017c478bd9Sstevel@tonic-gate 		return (line);
21027c478bd9Sstevel@tonic-gate 
21037c478bd9Sstevel@tonic-gate 	qsort(pids, n, sizeof (*pids), pidcmp);
21047c478bd9Sstevel@tonic-gate 
21057c478bd9Sstevel@tonic-gate 	for (i = 0; i < n; ++i) {
21067c478bd9Sstevel@tonic-gate 		char *cp, stime[9];
21077c478bd9Sstevel@tonic-gate 		psinfo_t psi;
21087c478bd9Sstevel@tonic-gate 		struct tm *tm;
21097c478bd9Sstevel@tonic-gate 		int len = 1 + 15 + 8 + 3 + 6 + 1 + PRFNSZ;
21107c478bd9Sstevel@tonic-gate 
21117c478bd9Sstevel@tonic-gate 		if (get_psinfo(pids[i], &psi) != 0)
21127c478bd9Sstevel@tonic-gate 			continue;
21137c478bd9Sstevel@tonic-gate 
21147c478bd9Sstevel@tonic-gate 		line = realloc(line, strlen(line) + len);
21157c478bd9Sstevel@tonic-gate 		if (line == NULL)
21167c478bd9Sstevel@tonic-gate 			uu_die(gettext("Out of memory.\n"));
21177c478bd9Sstevel@tonic-gate 
21187c478bd9Sstevel@tonic-gate 		cp = strchr(line, '\0');
21197c478bd9Sstevel@tonic-gate 
21207c478bd9Sstevel@tonic-gate 		tm = localtime(&psi.pr_start.tv_sec);
21217c478bd9Sstevel@tonic-gate 
21227c478bd9Sstevel@tonic-gate 		/*
21237c478bd9Sstevel@tonic-gate 		 * Print time if started within the past 24 hours, print date
21247c478bd9Sstevel@tonic-gate 		 * if within the past 12 months, print year if started greater
21257c478bd9Sstevel@tonic-gate 		 * than 12 months ago.
21267c478bd9Sstevel@tonic-gate 		 */
21277c478bd9Sstevel@tonic-gate 		if (now - psi.pr_start.tv_sec < 24 * 60 * 60)
21287c478bd9Sstevel@tonic-gate 		    (void) strftime(stime, sizeof (stime), gettext(FORMAT_TIME),
21297c478bd9Sstevel@tonic-gate 			tm);
21307c478bd9Sstevel@tonic-gate 		else if (now - psi.pr_start.tv_sec < 12 * 30 * 24 * 60 * 60)
21317c478bd9Sstevel@tonic-gate 		    (void) strftime(stime, sizeof (stime), gettext(FORMAT_DATE),
21327c478bd9Sstevel@tonic-gate 			tm);
21337c478bd9Sstevel@tonic-gate 		else
21347c478bd9Sstevel@tonic-gate 		    (void) strftime(stime, sizeof (stime), gettext(FORMAT_YEAR),
21357c478bd9Sstevel@tonic-gate 			tm);
21367c478bd9Sstevel@tonic-gate 
21377c478bd9Sstevel@tonic-gate 		(void) snprintf(cp, len, "\n               %-8s   %6ld %.*s",
21387c478bd9Sstevel@tonic-gate 		    stime, pids[i], PRFNSZ, psi.pr_fname);
21397c478bd9Sstevel@tonic-gate 	}
21407c478bd9Sstevel@tonic-gate 
21417c478bd9Sstevel@tonic-gate 	free(pids);
21427c478bd9Sstevel@tonic-gate 
21437c478bd9Sstevel@tonic-gate 	return (line);
21447c478bd9Sstevel@tonic-gate }
21457c478bd9Sstevel@tonic-gate 
21467c478bd9Sstevel@tonic-gate /*ARGSUSED*/
21477c478bd9Sstevel@tonic-gate static int
21487c478bd9Sstevel@tonic-gate list_instance(void *unused, scf_walkinfo_t *wip)
21497c478bd9Sstevel@tonic-gate {
21507c478bd9Sstevel@tonic-gate 	struct avl_string *lp;
21517c478bd9Sstevel@tonic-gate 	char *cp;
21527c478bd9Sstevel@tonic-gate 	int i;
21537c478bd9Sstevel@tonic-gate 	uu_avl_index_t idx;
21547c478bd9Sstevel@tonic-gate 
21557c478bd9Sstevel@tonic-gate 	/*
21567c478bd9Sstevel@tonic-gate 	 * If the user has specified a restarter, check for a match first
21577c478bd9Sstevel@tonic-gate 	 */
21587c478bd9Sstevel@tonic-gate 	if (restarters != NULL) {
21597c478bd9Sstevel@tonic-gate 		struct pfmri_list *rest;
21607c478bd9Sstevel@tonic-gate 		int match;
21617c478bd9Sstevel@tonic-gate 		char *restarter_fmri;
21627c478bd9Sstevel@tonic-gate 		const char *scope_name, *svc_name, *inst_name, *pg_name;
21637c478bd9Sstevel@tonic-gate 
21647c478bd9Sstevel@tonic-gate 		/* legacy services don't have restarters */
21657c478bd9Sstevel@tonic-gate 		if (wip->pg != NULL)
21667c478bd9Sstevel@tonic-gate 			return (0);
21677c478bd9Sstevel@tonic-gate 
21687c478bd9Sstevel@tonic-gate 		restarter_fmri = safe_malloc(max_scf_fmri_length + 1);
21697c478bd9Sstevel@tonic-gate 
21707c478bd9Sstevel@tonic-gate 		if (inst_get_single_val(wip->inst, SCF_PG_GENERAL,
21717c478bd9Sstevel@tonic-gate 		    SCF_PROPERTY_RESTARTER, SCF_TYPE_ASTRING, restarter_fmri,
21727c478bd9Sstevel@tonic-gate 		    max_scf_fmri_length + 1, 0, 0, 1) != 0)
21737c478bd9Sstevel@tonic-gate 			(void) strcpy(restarter_fmri, SCF_SERVICE_STARTD);
21747c478bd9Sstevel@tonic-gate 
21757c478bd9Sstevel@tonic-gate 		if (scf_parse_svc_fmri(restarter_fmri, &scope_name, &svc_name,
21767c478bd9Sstevel@tonic-gate 		    &inst_name, &pg_name, NULL) != SCF_SUCCESS) {
21777c478bd9Sstevel@tonic-gate 			free(restarter_fmri);
21787c478bd9Sstevel@tonic-gate 			return (0);
21797c478bd9Sstevel@tonic-gate 		}
21807c478bd9Sstevel@tonic-gate 
21817c478bd9Sstevel@tonic-gate 		match = 0;
21827c478bd9Sstevel@tonic-gate 		for (rest = restarters; rest != NULL; rest = rest->next) {
21837c478bd9Sstevel@tonic-gate 			if (strcmp(rest->scope, scope_name) == 0 &&
21847c478bd9Sstevel@tonic-gate 			    strcmp(rest->service, svc_name) == 0 &&
21857c478bd9Sstevel@tonic-gate 			    strcmp(rest->instance, inst_name) == 0)
21867c478bd9Sstevel@tonic-gate 				match = 1;
21877c478bd9Sstevel@tonic-gate 		}
21887c478bd9Sstevel@tonic-gate 
21897c478bd9Sstevel@tonic-gate 		free(restarter_fmri);
21907c478bd9Sstevel@tonic-gate 
21917c478bd9Sstevel@tonic-gate 		if (!match)
21927c478bd9Sstevel@tonic-gate 			return (0);
21937c478bd9Sstevel@tonic-gate 	}
21947c478bd9Sstevel@tonic-gate 
21957c478bd9Sstevel@tonic-gate 	if (wip->pg == NULL && ht_buckets != NULL && ht_add(wip->fmri)) {
21967c478bd9Sstevel@tonic-gate 		/* It was already there. */
21977c478bd9Sstevel@tonic-gate 		return (0);
21987c478bd9Sstevel@tonic-gate 	}
21997c478bd9Sstevel@tonic-gate 
22007c478bd9Sstevel@tonic-gate 	lp = safe_malloc(sizeof (*lp));
22017c478bd9Sstevel@tonic-gate 
22027c478bd9Sstevel@tonic-gate 	lp->str = NULL;
22037c478bd9Sstevel@tonic-gate 	for (i = 0; i < opt_cnum; ++i) {
22047c478bd9Sstevel@tonic-gate 		columns[opt_columns[i]].sprint(&lp->str, wip);
22057c478bd9Sstevel@tonic-gate 	}
22067c478bd9Sstevel@tonic-gate 	cp = lp->str + strlen(lp->str);
22077c478bd9Sstevel@tonic-gate 	cp--;
22087c478bd9Sstevel@tonic-gate 	while (*cp == ' ')
22097c478bd9Sstevel@tonic-gate 		cp--;
22107c478bd9Sstevel@tonic-gate 	*(cp+1) = '\0';
22117c478bd9Sstevel@tonic-gate 
22127c478bd9Sstevel@tonic-gate 	/* If we're supposed to list the processes, too, do that now. */
22137c478bd9Sstevel@tonic-gate 	if (opt_processes)
22147c478bd9Sstevel@tonic-gate 		lp->str = add_processes(lp->str, wip->inst, wip->pg);
22157c478bd9Sstevel@tonic-gate 
22167c478bd9Sstevel@tonic-gate 	/* Create the sort key. */
22177c478bd9Sstevel@tonic-gate 	cp = lp->key = safe_malloc(sortkey_sz);
22187c478bd9Sstevel@tonic-gate 	for (i = 0; i < opt_snum; ++i) {
22197c478bd9Sstevel@tonic-gate 		int j = opt_sort[i] & 0xff;
22207c478bd9Sstevel@tonic-gate 
22217c478bd9Sstevel@tonic-gate 		assert(columns[j].get_sortkey != NULL);
22227c478bd9Sstevel@tonic-gate 		columns[j].get_sortkey(cp, opt_sort[i] & ~0xff, wip);
22237c478bd9Sstevel@tonic-gate 		cp += columns[j].sortkey_width;
22247c478bd9Sstevel@tonic-gate 	}
22257c478bd9Sstevel@tonic-gate 
22267c478bd9Sstevel@tonic-gate 	/* Insert into AVL tree. */
22277c478bd9Sstevel@tonic-gate 	uu_avl_node_init(lp, &lp->node, lines_pool);
22287c478bd9Sstevel@tonic-gate 	(void) uu_avl_find(lines, lp, NULL, &idx);
22297c478bd9Sstevel@tonic-gate 	uu_avl_insert(lines, lp, idx);
22307c478bd9Sstevel@tonic-gate 
22317c478bd9Sstevel@tonic-gate 	return (0);
22327c478bd9Sstevel@tonic-gate }
22337c478bd9Sstevel@tonic-gate 
22347c478bd9Sstevel@tonic-gate static int
22357c478bd9Sstevel@tonic-gate list_if_enabled(void *unused, scf_walkinfo_t *wip)
22367c478bd9Sstevel@tonic-gate {
22377c478bd9Sstevel@tonic-gate 	if (wip->pg != NULL ||
22387c478bd9Sstevel@tonic-gate 	    instance_enabled(wip->inst, B_FALSE) == 1 ||
22397c478bd9Sstevel@tonic-gate 	    instance_enabled(wip->inst, B_TRUE) == 1)
22407c478bd9Sstevel@tonic-gate 		return (list_instance(unused, wip));
22417c478bd9Sstevel@tonic-gate 
22427c478bd9Sstevel@tonic-gate 	return (0);
22437c478bd9Sstevel@tonic-gate }
22447c478bd9Sstevel@tonic-gate 
22457c478bd9Sstevel@tonic-gate /*
22467c478bd9Sstevel@tonic-gate  * Service FMRI selection: Lookup and call list_instance() for the instances.
22477c478bd9Sstevel@tonic-gate  * Instance FMRI selection: Lookup and call list_instance().
22487c478bd9Sstevel@tonic-gate  *
22497c478bd9Sstevel@tonic-gate  * Note: This is shoehorned into a walk_dependencies() callback prototype so
22507c478bd9Sstevel@tonic-gate  * it can be used in list_dependencies.
22517c478bd9Sstevel@tonic-gate  */
22527c478bd9Sstevel@tonic-gate static int
22537c478bd9Sstevel@tonic-gate list_svc_or_inst_fmri(void *complain, scf_walkinfo_t *wip)
22547c478bd9Sstevel@tonic-gate {
22557c478bd9Sstevel@tonic-gate 	char *fmri;
22567c478bd9Sstevel@tonic-gate 	const char *svc_name, *inst_name, *pg_name, *save;
22577c478bd9Sstevel@tonic-gate 	scf_iter_t *iter;
22587c478bd9Sstevel@tonic-gate 	int ret;
22597c478bd9Sstevel@tonic-gate 
22607c478bd9Sstevel@tonic-gate 	fmri = safe_strdup(wip->fmri);
22617c478bd9Sstevel@tonic-gate 
22627c478bd9Sstevel@tonic-gate 	if (scf_parse_svc_fmri(fmri, NULL, &svc_name, &inst_name, &pg_name,
22637c478bd9Sstevel@tonic-gate 	    NULL) != SCF_SUCCESS) {
22647c478bd9Sstevel@tonic-gate 		if (complain)
22657c478bd9Sstevel@tonic-gate 			uu_warn(gettext("FMRI \"%s\" is invalid.\n"),
22667c478bd9Sstevel@tonic-gate 			    wip->fmri);
22677c478bd9Sstevel@tonic-gate 		exit_status = UU_EXIT_FATAL;
22687c478bd9Sstevel@tonic-gate 		free(fmri);
22697c478bd9Sstevel@tonic-gate 		return (0);
22707c478bd9Sstevel@tonic-gate 	}
22717c478bd9Sstevel@tonic-gate 
22727c478bd9Sstevel@tonic-gate 	/*
22737c478bd9Sstevel@tonic-gate 	 * Yes, this invalidates *_name, but we only care whether they're NULL
22747c478bd9Sstevel@tonic-gate 	 * or not.
22757c478bd9Sstevel@tonic-gate 	 */
22767c478bd9Sstevel@tonic-gate 	free(fmri);
22777c478bd9Sstevel@tonic-gate 
22787c478bd9Sstevel@tonic-gate 	if (svc_name == NULL || pg_name != NULL) {
22797c478bd9Sstevel@tonic-gate 		if (complain)
22807c478bd9Sstevel@tonic-gate 			uu_warn(gettext("FMRI \"%s\" does not designate a "
22817c478bd9Sstevel@tonic-gate 			    "service or instance.\n"), wip->fmri);
22827c478bd9Sstevel@tonic-gate 		return (0);
22837c478bd9Sstevel@tonic-gate 	}
22847c478bd9Sstevel@tonic-gate 
22857c478bd9Sstevel@tonic-gate 	if (inst_name != NULL) {
22867c478bd9Sstevel@tonic-gate 		/* instance */
22877c478bd9Sstevel@tonic-gate 		if (scf_handle_decode_fmri(h, wip->fmri, wip->scope, wip->svc,
22887c478bd9Sstevel@tonic-gate 		    wip->inst, NULL, NULL, 0) != SCF_SUCCESS) {
22897c478bd9Sstevel@tonic-gate 			if (scf_error() != SCF_ERROR_NOT_FOUND)
22907c478bd9Sstevel@tonic-gate 				scfdie();
22917c478bd9Sstevel@tonic-gate 
22927c478bd9Sstevel@tonic-gate 			if (complain)
22937c478bd9Sstevel@tonic-gate 				uu_warn(gettext(
22947c478bd9Sstevel@tonic-gate 				    "Instance \"%s\" does not exist.\n"),
22957c478bd9Sstevel@tonic-gate 				    wip->fmri);
22967c478bd9Sstevel@tonic-gate 			return (0);
22977c478bd9Sstevel@tonic-gate 		}
22987c478bd9Sstevel@tonic-gate 
22997c478bd9Sstevel@tonic-gate 		return (list_instance(NULL, wip));
23007c478bd9Sstevel@tonic-gate 	}
23017c478bd9Sstevel@tonic-gate 
23027c478bd9Sstevel@tonic-gate 	/* service: Walk the instances. */
23037c478bd9Sstevel@tonic-gate 	if (scf_handle_decode_fmri(h, wip->fmri, wip->scope, wip->svc, NULL,
23047c478bd9Sstevel@tonic-gate 	    NULL, NULL, 0) != SCF_SUCCESS) {
23057c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_NOT_FOUND)
23067c478bd9Sstevel@tonic-gate 			scfdie();
23077c478bd9Sstevel@tonic-gate 
23087c478bd9Sstevel@tonic-gate 		if (complain)
23097c478bd9Sstevel@tonic-gate 			uu_warn(gettext("Service \"%s\" does not exist.\n"),
23107c478bd9Sstevel@tonic-gate 			    wip->fmri);
23117c478bd9Sstevel@tonic-gate 
23127c478bd9Sstevel@tonic-gate 		exit_status = UU_EXIT_FATAL;
23137c478bd9Sstevel@tonic-gate 
23147c478bd9Sstevel@tonic-gate 		return (0);
23157c478bd9Sstevel@tonic-gate 	}
23167c478bd9Sstevel@tonic-gate 
23177c478bd9Sstevel@tonic-gate 	iter = scf_iter_create(h);
23187c478bd9Sstevel@tonic-gate 	if (iter == NULL)
23197c478bd9Sstevel@tonic-gate 		scfdie();
23207c478bd9Sstevel@tonic-gate 
23217c478bd9Sstevel@tonic-gate 	if (scf_iter_service_instances(iter, wip->svc) != SCF_SUCCESS)
23227c478bd9Sstevel@tonic-gate 		scfdie();
23237c478bd9Sstevel@tonic-gate 
23247c478bd9Sstevel@tonic-gate 	if ((fmri = malloc(max_scf_fmri_length + 1)) == NULL) {
23257c478bd9Sstevel@tonic-gate 		scf_iter_destroy(iter);
23267c478bd9Sstevel@tonic-gate 		exit_status = UU_EXIT_FATAL;
23277c478bd9Sstevel@tonic-gate 		return (0);
23287c478bd9Sstevel@tonic-gate 	}
23297c478bd9Sstevel@tonic-gate 
23307c478bd9Sstevel@tonic-gate 	save = wip->fmri;
23317c478bd9Sstevel@tonic-gate 	wip->fmri = fmri;
23327c478bd9Sstevel@tonic-gate 	while ((ret = scf_iter_next_instance(iter, wip->inst)) == 1) {
23337c478bd9Sstevel@tonic-gate 		if (scf_instance_to_fmri(wip->inst, fmri,
23347c478bd9Sstevel@tonic-gate 		    max_scf_fmri_length + 1) <= 0)
23357c478bd9Sstevel@tonic-gate 			scfdie();
23367c478bd9Sstevel@tonic-gate 		(void) list_instance(NULL, wip);
23377c478bd9Sstevel@tonic-gate 	}
23387c478bd9Sstevel@tonic-gate 	free(fmri);
23397c478bd9Sstevel@tonic-gate 	wip->fmri = save;
23407c478bd9Sstevel@tonic-gate 	if (ret == -1)
23417c478bd9Sstevel@tonic-gate 		scfdie();
23427c478bd9Sstevel@tonic-gate 
23437c478bd9Sstevel@tonic-gate 	exit_status = UU_EXIT_OK;
23447c478bd9Sstevel@tonic-gate 
23457c478bd9Sstevel@tonic-gate 	scf_iter_destroy(iter);
23467c478bd9Sstevel@tonic-gate 
23477c478bd9Sstevel@tonic-gate 	return (0);
23487c478bd9Sstevel@tonic-gate }
23497c478bd9Sstevel@tonic-gate 
23507c478bd9Sstevel@tonic-gate /*
23517c478bd9Sstevel@tonic-gate  * Dependency selection: Straightforward since each instance lists the
23527c478bd9Sstevel@tonic-gate  * services it depends on.
23537c478bd9Sstevel@tonic-gate  */
23547c478bd9Sstevel@tonic-gate 
23557c478bd9Sstevel@tonic-gate static void
23567c478bd9Sstevel@tonic-gate walk_dependencies(scf_walkinfo_t *wip, scf_walk_callback callback, void *data)
23577c478bd9Sstevel@tonic-gate {
23587c478bd9Sstevel@tonic-gate 	scf_snapshot_t *snap;
23597c478bd9Sstevel@tonic-gate 	scf_iter_t *iter, *viter;
23607c478bd9Sstevel@tonic-gate 	int ret, vret;
23617c478bd9Sstevel@tonic-gate 	char *dep;
23627c478bd9Sstevel@tonic-gate 
23637c478bd9Sstevel@tonic-gate 	assert(wip->inst != NULL);
23647c478bd9Sstevel@tonic-gate 
23657c478bd9Sstevel@tonic-gate 	if ((iter = scf_iter_create(h)) == NULL ||
23667c478bd9Sstevel@tonic-gate 	    (viter = scf_iter_create(h)) == NULL)
23677c478bd9Sstevel@tonic-gate 		scfdie();
23687c478bd9Sstevel@tonic-gate 
23697c478bd9Sstevel@tonic-gate 	snap = get_running_snapshot(wip->inst);
23707c478bd9Sstevel@tonic-gate 
23717c478bd9Sstevel@tonic-gate 	if (scf_iter_instance_pgs_typed_composed(iter, wip->inst, snap,
23727c478bd9Sstevel@tonic-gate 	    SCF_GROUP_DEPENDENCY) != SCF_SUCCESS)
23737c478bd9Sstevel@tonic-gate 		scfdie();
23747c478bd9Sstevel@tonic-gate 
23757c478bd9Sstevel@tonic-gate 	dep = safe_malloc(max_scf_value_length + 1);
23767c478bd9Sstevel@tonic-gate 
23777c478bd9Sstevel@tonic-gate 	while ((ret = scf_iter_next_pg(iter, g_pg)) == 1) {
23787c478bd9Sstevel@tonic-gate 		scf_type_t ty;
23797c478bd9Sstevel@tonic-gate 
23807c478bd9Sstevel@tonic-gate 		/* Ignore exclude_any dependencies. */
23817c478bd9Sstevel@tonic-gate 		if (scf_pg_get_property(g_pg, SCF_PROPERTY_GROUPING, g_prop) !=
23827c478bd9Sstevel@tonic-gate 		    SCF_SUCCESS) {
23837c478bd9Sstevel@tonic-gate 			if (scf_error() != SCF_ERROR_NOT_FOUND)
23847c478bd9Sstevel@tonic-gate 				scfdie();
23857c478bd9Sstevel@tonic-gate 
23867c478bd9Sstevel@tonic-gate 			continue;
23877c478bd9Sstevel@tonic-gate 		}
23887c478bd9Sstevel@tonic-gate 
23897c478bd9Sstevel@tonic-gate 		if (scf_property_type(g_prop, &ty) != SCF_SUCCESS)
23907c478bd9Sstevel@tonic-gate 			scfdie();
23917c478bd9Sstevel@tonic-gate 
23927c478bd9Sstevel@tonic-gate 		if (ty != SCF_TYPE_ASTRING)
23937c478bd9Sstevel@tonic-gate 			continue;
23947c478bd9Sstevel@tonic-gate 
23957c478bd9Sstevel@tonic-gate 		if (scf_property_get_value(g_prop, g_val) != SCF_SUCCESS) {
23967c478bd9Sstevel@tonic-gate 			if (scf_error() != SCF_ERROR_CONSTRAINT_VIOLATED)
23977c478bd9Sstevel@tonic-gate 				scfdie();
23987c478bd9Sstevel@tonic-gate 
23997c478bd9Sstevel@tonic-gate 			continue;
24007c478bd9Sstevel@tonic-gate 		}
24017c478bd9Sstevel@tonic-gate 
24027c478bd9Sstevel@tonic-gate 		if (scf_value_get_astring(g_val, dep,
24037c478bd9Sstevel@tonic-gate 		    max_scf_value_length + 1) < 0)
24047c478bd9Sstevel@tonic-gate 			scfdie();
24057c478bd9Sstevel@tonic-gate 
24067c478bd9Sstevel@tonic-gate 		if (strcmp(dep, SCF_DEP_EXCLUDE_ALL) == 0)
24077c478bd9Sstevel@tonic-gate 			continue;
24087c478bd9Sstevel@tonic-gate 
24097c478bd9Sstevel@tonic-gate 		if (scf_pg_get_property(g_pg, SCF_PROPERTY_ENTITIES, g_prop) !=
24107c478bd9Sstevel@tonic-gate 		    SCF_SUCCESS) {
24117c478bd9Sstevel@tonic-gate 			if (scf_error() != SCF_ERROR_NOT_FOUND)
24127c478bd9Sstevel@tonic-gate 				scfdie();
24137c478bd9Sstevel@tonic-gate 
24147c478bd9Sstevel@tonic-gate 			continue;
24157c478bd9Sstevel@tonic-gate 		}
24167c478bd9Sstevel@tonic-gate 
24177c478bd9Sstevel@tonic-gate 		if (scf_iter_property_values(viter, g_prop) != SCF_SUCCESS)
24187c478bd9Sstevel@tonic-gate 			scfdie();
24197c478bd9Sstevel@tonic-gate 
24207c478bd9Sstevel@tonic-gate 		while ((vret = scf_iter_next_value(viter, g_val)) == 1) {
24217c478bd9Sstevel@tonic-gate 			if (scf_value_get_astring(g_val, dep,
24227c478bd9Sstevel@tonic-gate 			    max_scf_value_length + 1) < 0)
24237c478bd9Sstevel@tonic-gate 				scfdie();
24247c478bd9Sstevel@tonic-gate 
24257c478bd9Sstevel@tonic-gate 			wip->fmri = dep;
24267c478bd9Sstevel@tonic-gate 			if (callback(data, wip) != 0)
24277c478bd9Sstevel@tonic-gate 				goto out;
24287c478bd9Sstevel@tonic-gate 		}
24297c478bd9Sstevel@tonic-gate 		if (vret == -1)
24307c478bd9Sstevel@tonic-gate 			scfdie();
24317c478bd9Sstevel@tonic-gate 	}
24327c478bd9Sstevel@tonic-gate 	if (ret == -1)
24337c478bd9Sstevel@tonic-gate 		scfdie();
24347c478bd9Sstevel@tonic-gate 
24357c478bd9Sstevel@tonic-gate out:
24367c478bd9Sstevel@tonic-gate 	scf_iter_destroy(viter);
24377c478bd9Sstevel@tonic-gate 	scf_iter_destroy(iter);
24387c478bd9Sstevel@tonic-gate 	scf_snapshot_destroy(snap);
24397c478bd9Sstevel@tonic-gate }
24407c478bd9Sstevel@tonic-gate 
24417c478bd9Sstevel@tonic-gate static int
24427c478bd9Sstevel@tonic-gate list_dependencies(void *data, scf_walkinfo_t *wip)
24437c478bd9Sstevel@tonic-gate {
24447c478bd9Sstevel@tonic-gate 	walk_dependencies(wip, list_svc_or_inst_fmri, data);
24457c478bd9Sstevel@tonic-gate 	return (0);
24467c478bd9Sstevel@tonic-gate }
24477c478bd9Sstevel@tonic-gate 
24487c478bd9Sstevel@tonic-gate 
24497c478bd9Sstevel@tonic-gate /*
24507c478bd9Sstevel@tonic-gate  * Dependent selection: The "providing" service's or instance's FMRI is parsed
24517c478bd9Sstevel@tonic-gate  * into the provider_* variables, the instances are walked, and any instance
24527c478bd9Sstevel@tonic-gate  * which lists an FMRI which parses to these components is selected.  This is
24537c478bd9Sstevel@tonic-gate  * inefficient in the face of multiple operands, but that should be uncommon.
24547c478bd9Sstevel@tonic-gate  */
24557c478bd9Sstevel@tonic-gate 
24567c478bd9Sstevel@tonic-gate static char *provider_scope;
24577c478bd9Sstevel@tonic-gate static char *provider_svc;
24587c478bd9Sstevel@tonic-gate static char *provider_inst;	/* NULL for services */
24597c478bd9Sstevel@tonic-gate 
24607c478bd9Sstevel@tonic-gate /*ARGSUSED*/
24617c478bd9Sstevel@tonic-gate static int
24627c478bd9Sstevel@tonic-gate check_against_provider(void *arg, scf_walkinfo_t *wip)
24637c478bd9Sstevel@tonic-gate {
24647c478bd9Sstevel@tonic-gate 	char *cfmri;
24657c478bd9Sstevel@tonic-gate 	const char *scope_name, *svc_name, *inst_name, *pg_name;
24667c478bd9Sstevel@tonic-gate 	int *matchp = arg;
24677c478bd9Sstevel@tonic-gate 
24687c478bd9Sstevel@tonic-gate 	cfmri = safe_strdup(wip->fmri);
24697c478bd9Sstevel@tonic-gate 
24707c478bd9Sstevel@tonic-gate 	if (scf_parse_svc_fmri(cfmri, &scope_name, &svc_name, &inst_name,
24717c478bd9Sstevel@tonic-gate 	    &pg_name, NULL) != SCF_SUCCESS) {
24727c478bd9Sstevel@tonic-gate 		free(cfmri);
24737c478bd9Sstevel@tonic-gate 		return (0);
24747c478bd9Sstevel@tonic-gate 	}
24757c478bd9Sstevel@tonic-gate 
24767c478bd9Sstevel@tonic-gate 	if (svc_name == NULL || pg_name != NULL) {
24777c478bd9Sstevel@tonic-gate 		free(cfmri);
24787c478bd9Sstevel@tonic-gate 		return (0);
24797c478bd9Sstevel@tonic-gate 	}
24807c478bd9Sstevel@tonic-gate 
24817c478bd9Sstevel@tonic-gate 	/*
24827c478bd9Sstevel@tonic-gate 	 * If the user has specified an instance, then also match dependencies
24837c478bd9Sstevel@tonic-gate 	 * on the service itself.
24847c478bd9Sstevel@tonic-gate 	 */
24857c478bd9Sstevel@tonic-gate 	*matchp = (strcmp(provider_scope, scope_name) == 0 &&
24867c478bd9Sstevel@tonic-gate 	    strcmp(provider_svc, svc_name) == 0 &&
24877c478bd9Sstevel@tonic-gate 	    (provider_inst == NULL ? (inst_name == NULL) :
24887c478bd9Sstevel@tonic-gate 	    (inst_name == NULL || strcmp(provider_inst, inst_name) == 0)));
24897c478bd9Sstevel@tonic-gate 
24907c478bd9Sstevel@tonic-gate 	free(cfmri);
24917c478bd9Sstevel@tonic-gate 
24927c478bd9Sstevel@tonic-gate 	/* Stop on matches. */
24937c478bd9Sstevel@tonic-gate 	return (*matchp);
24947c478bd9Sstevel@tonic-gate }
24957c478bd9Sstevel@tonic-gate 
24967c478bd9Sstevel@tonic-gate static int
24977c478bd9Sstevel@tonic-gate list_if_dependent(void *unused, scf_walkinfo_t *wip)
24987c478bd9Sstevel@tonic-gate {
24997c478bd9Sstevel@tonic-gate 	/* Only proceed if this instance depends on provider_*. */
25007c478bd9Sstevel@tonic-gate 	int match = 0;
25017c478bd9Sstevel@tonic-gate 
25027c478bd9Sstevel@tonic-gate 	(void) walk_dependencies(wip, check_against_provider, &match);
25037c478bd9Sstevel@tonic-gate 
25047c478bd9Sstevel@tonic-gate 	if (match)
25057c478bd9Sstevel@tonic-gate 		return (list_instance(unused, wip));
25067c478bd9Sstevel@tonic-gate 
25077c478bd9Sstevel@tonic-gate 	return (0);
25087c478bd9Sstevel@tonic-gate }
25097c478bd9Sstevel@tonic-gate 
25107c478bd9Sstevel@tonic-gate /*ARGSUSED*/
25117c478bd9Sstevel@tonic-gate static int
25127c478bd9Sstevel@tonic-gate list_dependents(void *unused, scf_walkinfo_t *wip)
25137c478bd9Sstevel@tonic-gate {
25147c478bd9Sstevel@tonic-gate 	char *save;
25157c478bd9Sstevel@tonic-gate 	int ret;
25167c478bd9Sstevel@tonic-gate 
25177c478bd9Sstevel@tonic-gate 	if (scf_scope_get_name(wip->scope, provider_scope,
25187c478bd9Sstevel@tonic-gate 	    max_scf_fmri_length) <= 0 ||
25197c478bd9Sstevel@tonic-gate 	    scf_service_get_name(wip->svc, provider_svc,
25207c478bd9Sstevel@tonic-gate 	    max_scf_fmri_length) <= 0)
25217c478bd9Sstevel@tonic-gate 		scfdie();
25227c478bd9Sstevel@tonic-gate 
25237c478bd9Sstevel@tonic-gate 	save = provider_inst;
25247c478bd9Sstevel@tonic-gate 	if (wip->inst == NULL)
25257c478bd9Sstevel@tonic-gate 		provider_inst = NULL;
25267c478bd9Sstevel@tonic-gate 	else if (scf_instance_get_name(wip->inst, provider_inst,
25277c478bd9Sstevel@tonic-gate 	    max_scf_fmri_length) <= 0)
25287c478bd9Sstevel@tonic-gate 		scfdie();
25297c478bd9Sstevel@tonic-gate 
25307c478bd9Sstevel@tonic-gate 	ret = scf_walk_fmri(h, 0, NULL, 0, list_if_dependent, NULL, NULL,
25317c478bd9Sstevel@tonic-gate 	    uu_warn);
25327c478bd9Sstevel@tonic-gate 
25337c478bd9Sstevel@tonic-gate 	provider_inst = save;
25347c478bd9Sstevel@tonic-gate 
25357c478bd9Sstevel@tonic-gate 	return (ret);
25367c478bd9Sstevel@tonic-gate }
25377c478bd9Sstevel@tonic-gate 
25387c478bd9Sstevel@tonic-gate /*
25397c478bd9Sstevel@tonic-gate  * main() & helpers
25407c478bd9Sstevel@tonic-gate  */
25417c478bd9Sstevel@tonic-gate 
25427c478bd9Sstevel@tonic-gate static void
25437c478bd9Sstevel@tonic-gate add_sort_column(const char *col, int reverse)
25447c478bd9Sstevel@tonic-gate {
25457c478bd9Sstevel@tonic-gate 	int i;
25467c478bd9Sstevel@tonic-gate 
25477c478bd9Sstevel@tonic-gate 	++opt_snum;
25487c478bd9Sstevel@tonic-gate 
25497c478bd9Sstevel@tonic-gate 	opt_sort = realloc(opt_sort, opt_snum * sizeof (*opt_sort));
25507c478bd9Sstevel@tonic-gate 	if (opt_sort == NULL)
25517c478bd9Sstevel@tonic-gate 		uu_die(gettext("Too many sort criteria: out of memory.\n"));
25527c478bd9Sstevel@tonic-gate 
25537c478bd9Sstevel@tonic-gate 	for (i = 0; i < ncolumns; ++i) {
25547c478bd9Sstevel@tonic-gate 		if (strcasecmp(col, columns[i].name) == 0)
25557c478bd9Sstevel@tonic-gate 			break;
25567c478bd9Sstevel@tonic-gate 	}
25577c478bd9Sstevel@tonic-gate 
25587c478bd9Sstevel@tonic-gate 	if (i < ncolumns)
25597c478bd9Sstevel@tonic-gate 		opt_sort[opt_snum - 1] = (reverse ? i | 0x100 : i);
25607c478bd9Sstevel@tonic-gate 	else
25617c478bd9Sstevel@tonic-gate 		uu_die(gettext("Unrecognized sort column \"%s\".\n"), col);
25627c478bd9Sstevel@tonic-gate 
25637c478bd9Sstevel@tonic-gate 	sortkey_sz += columns[i].sortkey_width;
25647c478bd9Sstevel@tonic-gate }
25657c478bd9Sstevel@tonic-gate 
25667c478bd9Sstevel@tonic-gate static void
25677c478bd9Sstevel@tonic-gate add_restarter(const char *fmri)
25687c478bd9Sstevel@tonic-gate {
25697c478bd9Sstevel@tonic-gate 	char *cfmri;
25707c478bd9Sstevel@tonic-gate 	const char *pg_name;
25717c478bd9Sstevel@tonic-gate 	struct pfmri_list *rest;
25727c478bd9Sstevel@tonic-gate 
25737c478bd9Sstevel@tonic-gate 	cfmri = safe_strdup(fmri);
25747c478bd9Sstevel@tonic-gate 	rest = safe_malloc(sizeof (*rest));
25757c478bd9Sstevel@tonic-gate 
25767c478bd9Sstevel@tonic-gate 	if (scf_parse_svc_fmri(cfmri, &rest->scope, &rest->service,
25777c478bd9Sstevel@tonic-gate 	    &rest->instance, &pg_name, NULL) != SCF_SUCCESS)
25787c478bd9Sstevel@tonic-gate 		uu_die(gettext("Restarter FMRI \"%s\" is invalid.\n"), fmri);
25797c478bd9Sstevel@tonic-gate 
25807c478bd9Sstevel@tonic-gate 	if (rest->instance == NULL || pg_name != NULL)
25817c478bd9Sstevel@tonic-gate 		uu_die(gettext("Restarter FMRI \"%s\" does not designate an "
25827c478bd9Sstevel@tonic-gate 		    "instance.\n"), fmri);
25837c478bd9Sstevel@tonic-gate 
25847c478bd9Sstevel@tonic-gate 	rest->next = restarters;
25857c478bd9Sstevel@tonic-gate 	restarters = rest;
25867c478bd9Sstevel@tonic-gate 	return;
25877c478bd9Sstevel@tonic-gate 
25887c478bd9Sstevel@tonic-gate err:
25897c478bd9Sstevel@tonic-gate 	free(cfmri);
25907c478bd9Sstevel@tonic-gate 	free(rest);
25917c478bd9Sstevel@tonic-gate }
25927c478bd9Sstevel@tonic-gate 
25937c478bd9Sstevel@tonic-gate /* ARGSUSED */
25947c478bd9Sstevel@tonic-gate static int
25957c478bd9Sstevel@tonic-gate line_cmp(const void *l_arg, const void *r_arg, void *private)
25967c478bd9Sstevel@tonic-gate {
25977c478bd9Sstevel@tonic-gate 	const struct avl_string *l = l_arg;
25987c478bd9Sstevel@tonic-gate 	const struct avl_string *r = r_arg;
25997c478bd9Sstevel@tonic-gate 
26007c478bd9Sstevel@tonic-gate 	return (memcmp(l->key, r->key, sortkey_sz));
26017c478bd9Sstevel@tonic-gate }
26027c478bd9Sstevel@tonic-gate 
26037c478bd9Sstevel@tonic-gate /* ARGSUSED */
26047c478bd9Sstevel@tonic-gate static int
26057c478bd9Sstevel@tonic-gate print_line(void *e, void *private)
26067c478bd9Sstevel@tonic-gate {
26077c478bd9Sstevel@tonic-gate 	struct avl_string *lp = e;
26087c478bd9Sstevel@tonic-gate 
26097c478bd9Sstevel@tonic-gate 	(void) puts(lp->str);
26107c478bd9Sstevel@tonic-gate 
26117c478bd9Sstevel@tonic-gate 	return (UU_WALK_NEXT);
26127c478bd9Sstevel@tonic-gate }
26137c478bd9Sstevel@tonic-gate 
26147c478bd9Sstevel@tonic-gate int
26157c478bd9Sstevel@tonic-gate main(int argc, char **argv)
26167c478bd9Sstevel@tonic-gate {
26177c478bd9Sstevel@tonic-gate 	char opt, opt_mode;
26187c478bd9Sstevel@tonic-gate 	int i, n;
26197c478bd9Sstevel@tonic-gate 	char *columns_str = NULL;
26207c478bd9Sstevel@tonic-gate 	char *cp;
26217c478bd9Sstevel@tonic-gate 	const char *progname;
26227c478bd9Sstevel@tonic-gate 	int err;
26237c478bd9Sstevel@tonic-gate 
26247c478bd9Sstevel@tonic-gate 	int show_all = 0;
26257c478bd9Sstevel@tonic-gate 	int show_header = 1;
26267c478bd9Sstevel@tonic-gate 
26277c478bd9Sstevel@tonic-gate 	const char * const options = "aHpvo:R:s:S:dDl?x";
26287c478bd9Sstevel@tonic-gate 
26297c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
26307c478bd9Sstevel@tonic-gate 
26317c478bd9Sstevel@tonic-gate 	locale = setlocale(LC_MESSAGES, "");
26327c478bd9Sstevel@tonic-gate 	if (locale) {
26337c478bd9Sstevel@tonic-gate 		locale = safe_strdup(locale);
26347c478bd9Sstevel@tonic-gate 		sanitize_locale(locale);
26357c478bd9Sstevel@tonic-gate 	}
26367c478bd9Sstevel@tonic-gate 
26377c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
26387c478bd9Sstevel@tonic-gate 	progname = uu_setpname(argv[0]);
26397c478bd9Sstevel@tonic-gate 
26407c478bd9Sstevel@tonic-gate 	exit_status = UU_EXIT_OK;
26417c478bd9Sstevel@tonic-gate 
26427c478bd9Sstevel@tonic-gate 	max_scf_name_length = scf_limit(SCF_LIMIT_MAX_NAME_LENGTH);
26437c478bd9Sstevel@tonic-gate 	max_scf_value_length = scf_limit(SCF_LIMIT_MAX_VALUE_LENGTH);
26447c478bd9Sstevel@tonic-gate 	max_scf_fmri_length = scf_limit(SCF_LIMIT_MAX_FMRI_LENGTH);
26457c478bd9Sstevel@tonic-gate 
26467c478bd9Sstevel@tonic-gate 	if (max_scf_name_length == -1 || max_scf_value_length == -1 ||
26477c478bd9Sstevel@tonic-gate 	    max_scf_fmri_length == -1)
26487c478bd9Sstevel@tonic-gate 		scfdie();
26497c478bd9Sstevel@tonic-gate 
26507c478bd9Sstevel@tonic-gate 	now = time(NULL);
26517c478bd9Sstevel@tonic-gate 	assert(now != -1);
26527c478bd9Sstevel@tonic-gate 
26537c478bd9Sstevel@tonic-gate 	/*
26547c478bd9Sstevel@tonic-gate 	 * opt_mode is the mode of operation.  0 for plain, 'd' for
26557c478bd9Sstevel@tonic-gate 	 * dependencies, 'D' for dependents, and 'l' for detailed (long).  We
26567c478bd9Sstevel@tonic-gate 	 * need to know now so we know which options are valid.
26577c478bd9Sstevel@tonic-gate 	 */
26587c478bd9Sstevel@tonic-gate 	opt_mode = 0;
26597c478bd9Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, options)) != -1) {
26607c478bd9Sstevel@tonic-gate 		switch (opt) {
26617c478bd9Sstevel@tonic-gate 		case '?':
26627c478bd9Sstevel@tonic-gate 			if (optopt == '?') {
26637c478bd9Sstevel@tonic-gate 				print_help(progname);
26647c478bd9Sstevel@tonic-gate 				return (UU_EXIT_OK);
26657c478bd9Sstevel@tonic-gate 			} else {
26667c478bd9Sstevel@tonic-gate 				argserr(progname);
26677c478bd9Sstevel@tonic-gate 				/* NOTREACHED */
26687c478bd9Sstevel@tonic-gate 			}
26697c478bd9Sstevel@tonic-gate 
26707c478bd9Sstevel@tonic-gate 		case 'd':
26717c478bd9Sstevel@tonic-gate 		case 'D':
26727c478bd9Sstevel@tonic-gate 		case 'l':
26737c478bd9Sstevel@tonic-gate 			if (opt_mode != 0)
26747c478bd9Sstevel@tonic-gate 				argserr(progname);
26757c478bd9Sstevel@tonic-gate 
26767c478bd9Sstevel@tonic-gate 			opt_mode = opt;
26777c478bd9Sstevel@tonic-gate 			break;
26787c478bd9Sstevel@tonic-gate 
26797c478bd9Sstevel@tonic-gate 		case 'x':
26807c478bd9Sstevel@tonic-gate 			if (opt_mode != 0)
26817c478bd9Sstevel@tonic-gate 				argserr(progname);
26827c478bd9Sstevel@tonic-gate 
26837c478bd9Sstevel@tonic-gate 			opt_mode = opt;
26847c478bd9Sstevel@tonic-gate 			break;
26857c478bd9Sstevel@tonic-gate 
26867c478bd9Sstevel@tonic-gate 		default:
26877c478bd9Sstevel@tonic-gate 			break;
26887c478bd9Sstevel@tonic-gate 		}
26897c478bd9Sstevel@tonic-gate 	}
26907c478bd9Sstevel@tonic-gate 
26917c478bd9Sstevel@tonic-gate 	sortkey_sz = 0;
26927c478bd9Sstevel@tonic-gate 
26937c478bd9Sstevel@tonic-gate 	optind = 1;	/* Reset getopt() */
26947c478bd9Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, options)) != -1) {
26957c478bd9Sstevel@tonic-gate 		switch (opt) {
26967c478bd9Sstevel@tonic-gate 		case 'a':
26977c478bd9Sstevel@tonic-gate 			if (opt_mode != 0)
26987c478bd9Sstevel@tonic-gate 				argserr(progname);
26997c478bd9Sstevel@tonic-gate 			show_all = 1;
27007c478bd9Sstevel@tonic-gate 			break;
27017c478bd9Sstevel@tonic-gate 
27027c478bd9Sstevel@tonic-gate 		case 'H':
27037c478bd9Sstevel@tonic-gate 			if (opt_mode == 'l' || opt_mode == 'x')
27047c478bd9Sstevel@tonic-gate 				argserr(progname);
27057c478bd9Sstevel@tonic-gate 			show_header = 0;
27067c478bd9Sstevel@tonic-gate 			break;
27077c478bd9Sstevel@tonic-gate 
27087c478bd9Sstevel@tonic-gate 		case 'p':
27097c478bd9Sstevel@tonic-gate 			if (opt_mode == 'x')
27107c478bd9Sstevel@tonic-gate 				argserr(progname);
27117c478bd9Sstevel@tonic-gate 			opt_processes = 1;
27127c478bd9Sstevel@tonic-gate 			break;
27137c478bd9Sstevel@tonic-gate 
27147c478bd9Sstevel@tonic-gate 		case 'v':
27157c478bd9Sstevel@tonic-gate 			if (opt_mode == 'l')
27167c478bd9Sstevel@tonic-gate 				argserr(progname);
27177c478bd9Sstevel@tonic-gate 			opt_verbose = 1;
27187c478bd9Sstevel@tonic-gate 			break;
27197c478bd9Sstevel@tonic-gate 
27207c478bd9Sstevel@tonic-gate 		case 'o':
27217c478bd9Sstevel@tonic-gate 			if (opt_mode == 'l' || opt_mode == 'x')
27227c478bd9Sstevel@tonic-gate 				argserr(progname);
27237c478bd9Sstevel@tonic-gate 			columns_str = optarg;
27247c478bd9Sstevel@tonic-gate 			break;
27257c478bd9Sstevel@tonic-gate 
27267c478bd9Sstevel@tonic-gate 		case 'R':
27277c478bd9Sstevel@tonic-gate 			if (opt_mode != 0 || opt_mode == 'x')
27287c478bd9Sstevel@tonic-gate 				argserr(progname);
27297c478bd9Sstevel@tonic-gate 
27307c478bd9Sstevel@tonic-gate 			add_restarter(optarg);
27317c478bd9Sstevel@tonic-gate 			break;
27327c478bd9Sstevel@tonic-gate 
27337c478bd9Sstevel@tonic-gate 		case 's':
27347c478bd9Sstevel@tonic-gate 		case 'S':
27357c478bd9Sstevel@tonic-gate 			if (opt_mode != 0)
27367c478bd9Sstevel@tonic-gate 				argserr(progname);
27377c478bd9Sstevel@tonic-gate 
27387c478bd9Sstevel@tonic-gate 			add_sort_column(optarg, optopt == 'S');
27397c478bd9Sstevel@tonic-gate 			break;
27407c478bd9Sstevel@tonic-gate 
27417c478bd9Sstevel@tonic-gate 		case 'd':
27427c478bd9Sstevel@tonic-gate 		case 'D':
27437c478bd9Sstevel@tonic-gate 		case 'l':
27447c478bd9Sstevel@tonic-gate 		case 'x':
27457c478bd9Sstevel@tonic-gate 			assert(opt_mode == optopt);
27467c478bd9Sstevel@tonic-gate 			break;
27477c478bd9Sstevel@tonic-gate 
27487c478bd9Sstevel@tonic-gate 		case '?':
27497c478bd9Sstevel@tonic-gate 			argserr(progname);
27507c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
27517c478bd9Sstevel@tonic-gate 
27527c478bd9Sstevel@tonic-gate 		default:
27537c478bd9Sstevel@tonic-gate 			assert(0);
27547c478bd9Sstevel@tonic-gate 			abort();
27557c478bd9Sstevel@tonic-gate 		}
27567c478bd9Sstevel@tonic-gate 	}
27577c478bd9Sstevel@tonic-gate 
27587c478bd9Sstevel@tonic-gate 	/*
27597c478bd9Sstevel@tonic-gate 	 * -a is only meaningful when given no arguments
27607c478bd9Sstevel@tonic-gate 	 */
27617c478bd9Sstevel@tonic-gate 	if (show_all && optind != argc)
27627c478bd9Sstevel@tonic-gate 		uu_warn(gettext("-a ignored when used with arguments.\n"));
27637c478bd9Sstevel@tonic-gate 
27647c478bd9Sstevel@tonic-gate 	h = scf_handle_create(SCF_VERSION);
27657c478bd9Sstevel@tonic-gate 	if (h == NULL)
27667c478bd9Sstevel@tonic-gate 		scfdie();
27677c478bd9Sstevel@tonic-gate 
27687c478bd9Sstevel@tonic-gate 	if (scf_handle_bind(h) == -1)
27697c478bd9Sstevel@tonic-gate 		uu_die(gettext("Could not bind to repository server: %s.  "
27707c478bd9Sstevel@tonic-gate 		    "Exiting.\n"), scf_strerror(scf_error()));
27717c478bd9Sstevel@tonic-gate 
27727c478bd9Sstevel@tonic-gate 	if ((g_pg = scf_pg_create(h)) == NULL ||
27737c478bd9Sstevel@tonic-gate 	    (g_prop = scf_property_create(h)) == NULL ||
27747c478bd9Sstevel@tonic-gate 	    (g_val = scf_value_create(h)) == NULL)
27757c478bd9Sstevel@tonic-gate 		scfdie();
27767c478bd9Sstevel@tonic-gate 
27777c478bd9Sstevel@tonic-gate 	argc -= optind;
27787c478bd9Sstevel@tonic-gate 	argv += optind;
27797c478bd9Sstevel@tonic-gate 
27807c478bd9Sstevel@tonic-gate 	/*
27817c478bd9Sstevel@tonic-gate 	 * If we're in long mode, take care of it now before we deal with the
27827c478bd9Sstevel@tonic-gate 	 * sorting and the columns, since we won't use them anyway.
27837c478bd9Sstevel@tonic-gate 	 */
27847c478bd9Sstevel@tonic-gate 	if (opt_mode == 'l') {
27857c478bd9Sstevel@tonic-gate 		if (argc == 0)
27867c478bd9Sstevel@tonic-gate 			argserr(progname);
27877c478bd9Sstevel@tonic-gate 
27887c478bd9Sstevel@tonic-gate 		if ((err = scf_walk_fmri(h, argc, argv, SCF_WALK_MULTIPLE,
27897c478bd9Sstevel@tonic-gate 		    print_detailed, NULL, &exit_status, uu_warn)) != 0) {
27907c478bd9Sstevel@tonic-gate 			uu_warn(gettext("failed to iterate over "
27917c478bd9Sstevel@tonic-gate 			    "instances: %s\n"), scf_strerror(err));
27927c478bd9Sstevel@tonic-gate 			exit_status = UU_EXIT_FATAL;
27937c478bd9Sstevel@tonic-gate 		}
27947c478bd9Sstevel@tonic-gate 
27957c478bd9Sstevel@tonic-gate 		return (exit_status);
27967c478bd9Sstevel@tonic-gate 	}
27977c478bd9Sstevel@tonic-gate 
27987c478bd9Sstevel@tonic-gate 	if (opt_mode == 'x') {
27997c478bd9Sstevel@tonic-gate 		explain(opt_verbose, argc, argv);
28007c478bd9Sstevel@tonic-gate 
28017c478bd9Sstevel@tonic-gate 		return (exit_status);
28027c478bd9Sstevel@tonic-gate 	}
28037c478bd9Sstevel@tonic-gate 
28047c478bd9Sstevel@tonic-gate 
28057c478bd9Sstevel@tonic-gate 	if (opt_snum == 0) {
28067c478bd9Sstevel@tonic-gate 		/* Default sort. */
28077c478bd9Sstevel@tonic-gate 		add_sort_column("state", 0);
28087c478bd9Sstevel@tonic-gate 		add_sort_column("stime", 0);
28097c478bd9Sstevel@tonic-gate 		add_sort_column("fmri", 0);
28107c478bd9Sstevel@tonic-gate 	}
28117c478bd9Sstevel@tonic-gate 
28127c478bd9Sstevel@tonic-gate 	if (columns_str == NULL) {
28137c478bd9Sstevel@tonic-gate 		if (!opt_verbose)
28147c478bd9Sstevel@tonic-gate 			columns_str = safe_strdup("state,stime,fmri");
28157c478bd9Sstevel@tonic-gate 		else
28167c478bd9Sstevel@tonic-gate 			columns_str =
28177c478bd9Sstevel@tonic-gate 			    safe_strdup("state,nstate,stime,ctid,fmri");
28187c478bd9Sstevel@tonic-gate 	}
28197c478bd9Sstevel@tonic-gate 
28207c478bd9Sstevel@tonic-gate 	/* Decode columns_str into opt_columns. */
28217c478bd9Sstevel@tonic-gate 	line_sz = 0;
28227c478bd9Sstevel@tonic-gate 
28237c478bd9Sstevel@tonic-gate 	opt_cnum = 1;
28247c478bd9Sstevel@tonic-gate 	for (cp = columns_str; *cp != '\0'; ++cp)
28257c478bd9Sstevel@tonic-gate 		if (*cp == ',')
28267c478bd9Sstevel@tonic-gate 			++opt_cnum;
28277c478bd9Sstevel@tonic-gate 
28287c478bd9Sstevel@tonic-gate 	opt_columns = malloc(opt_cnum * sizeof (*opt_columns));
28297c478bd9Sstevel@tonic-gate 	if (opt_columns == NULL)
28307c478bd9Sstevel@tonic-gate 		uu_die(gettext("Too many columns.\n"));
28317c478bd9Sstevel@tonic-gate 
28327c478bd9Sstevel@tonic-gate 	for (n = 0; *columns_str != '\0'; ++n) {
28337c478bd9Sstevel@tonic-gate 		i = getcolumnopt(&columns_str);
28347c478bd9Sstevel@tonic-gate 		if (i == -1)
28357c478bd9Sstevel@tonic-gate 			uu_die(gettext("Unknown column \"%s\".\n"),
28367c478bd9Sstevel@tonic-gate 			    columns_str);
28377c478bd9Sstevel@tonic-gate 
28387c478bd9Sstevel@tonic-gate 		if (strcmp(columns[i].name, "N") == 0 ||
28397c478bd9Sstevel@tonic-gate 		    strcmp(columns[i].name, "SN") == 0 ||
28407c478bd9Sstevel@tonic-gate 		    strcmp(columns[i].name, "NSTA") == 0 ||
28417c478bd9Sstevel@tonic-gate 		    strcmp(columns[i].name, "NSTATE") == 0)
28427c478bd9Sstevel@tonic-gate 			opt_nstate_shown = 1;
28437c478bd9Sstevel@tonic-gate 
28447c478bd9Sstevel@tonic-gate 		opt_columns[n] = i;
28457c478bd9Sstevel@tonic-gate 		line_sz += columns[i].width + 1;
28467c478bd9Sstevel@tonic-gate 	}
28477c478bd9Sstevel@tonic-gate 
28487c478bd9Sstevel@tonic-gate 
28497c478bd9Sstevel@tonic-gate 	if ((lines_pool = uu_avl_pool_create("lines_pool",
28507c478bd9Sstevel@tonic-gate 	    sizeof (struct avl_string), offsetof(struct avl_string, node),
28517c478bd9Sstevel@tonic-gate 	    line_cmp, UU_AVL_DEBUG)) == NULL ||
28527c478bd9Sstevel@tonic-gate 	    (lines = uu_avl_create(lines_pool, NULL, 0)) == NULL)
28537c478bd9Sstevel@tonic-gate 		uu_die(gettext("Unexpected libuutil error: %s.  Exiting.\n"),
28547c478bd9Sstevel@tonic-gate 		    uu_strerror(uu_error()));
28557c478bd9Sstevel@tonic-gate 
28567c478bd9Sstevel@tonic-gate 	switch (opt_mode) {
28577c478bd9Sstevel@tonic-gate 	case 0:
28587c478bd9Sstevel@tonic-gate 		ht_init();
28597c478bd9Sstevel@tonic-gate 
28607c478bd9Sstevel@tonic-gate 		/* Always show all FMRIs when given arguments or restarters */
28617c478bd9Sstevel@tonic-gate 		if (argc != 0 || restarters != NULL)
28627c478bd9Sstevel@tonic-gate 			show_all =  1;
28637c478bd9Sstevel@tonic-gate 
28647c478bd9Sstevel@tonic-gate 		if ((err = scf_walk_fmri(h, argc, argv,
28657c478bd9Sstevel@tonic-gate 		    SCF_WALK_MULTIPLE | SCF_WALK_LEGACY,
28667c478bd9Sstevel@tonic-gate 		    show_all ? list_instance : list_if_enabled, NULL,
28677c478bd9Sstevel@tonic-gate 		    &exit_status, uu_warn)) != 0) {
28687c478bd9Sstevel@tonic-gate 			uu_warn(gettext("failed to iterate over "
28697c478bd9Sstevel@tonic-gate 			    "instances: %s\n"), scf_strerror(err));
28707c478bd9Sstevel@tonic-gate 			exit_status = UU_EXIT_FATAL;
28717c478bd9Sstevel@tonic-gate 		}
28727c478bd9Sstevel@tonic-gate 		break;
28737c478bd9Sstevel@tonic-gate 
28747c478bd9Sstevel@tonic-gate 	case 'd':
28757c478bd9Sstevel@tonic-gate 		if (argc == 0)
28767c478bd9Sstevel@tonic-gate 			argserr(progname);
28777c478bd9Sstevel@tonic-gate 
28787c478bd9Sstevel@tonic-gate 		if ((err = scf_walk_fmri(h, argc, argv,
28797c478bd9Sstevel@tonic-gate 		    SCF_WALK_MULTIPLE, list_dependencies, NULL,
28807c478bd9Sstevel@tonic-gate 		    &exit_status, uu_warn)) != 0) {
28817c478bd9Sstevel@tonic-gate 			uu_warn(gettext("failed to iterate over "
28827c478bd9Sstevel@tonic-gate 			    "instances: %s\n"), scf_strerror(err));
28837c478bd9Sstevel@tonic-gate 			exit_status = UU_EXIT_FATAL;
28847c478bd9Sstevel@tonic-gate 		}
28857c478bd9Sstevel@tonic-gate 		break;
28867c478bd9Sstevel@tonic-gate 
28877c478bd9Sstevel@tonic-gate 	case 'D':
28887c478bd9Sstevel@tonic-gate 		if (argc == 0)
28897c478bd9Sstevel@tonic-gate 			argserr(progname);
28907c478bd9Sstevel@tonic-gate 
28917c478bd9Sstevel@tonic-gate 		provider_scope = safe_malloc(max_scf_fmri_length);
28927c478bd9Sstevel@tonic-gate 		provider_svc = safe_malloc(max_scf_fmri_length);
28937c478bd9Sstevel@tonic-gate 		provider_inst = safe_malloc(max_scf_fmri_length);
28947c478bd9Sstevel@tonic-gate 
28957c478bd9Sstevel@tonic-gate 		if ((err = scf_walk_fmri(h, argc, argv,
28967c478bd9Sstevel@tonic-gate 		    SCF_WALK_MULTIPLE | SCF_WALK_SERVICE,
28977c478bd9Sstevel@tonic-gate 		    list_dependents, NULL, &exit_status, uu_warn)) != 0) {
28987c478bd9Sstevel@tonic-gate 			uu_warn(gettext("failed to iterate over "
28997c478bd9Sstevel@tonic-gate 			    "instances: %s\n"), scf_strerror(err));
29007c478bd9Sstevel@tonic-gate 			exit_status = UU_EXIT_FATAL;
29017c478bd9Sstevel@tonic-gate 		}
29027c478bd9Sstevel@tonic-gate 
29037c478bd9Sstevel@tonic-gate 		free(provider_scope);
29047c478bd9Sstevel@tonic-gate 		free(provider_svc);
29057c478bd9Sstevel@tonic-gate 		free(provider_inst);
29067c478bd9Sstevel@tonic-gate 		break;
29077c478bd9Sstevel@tonic-gate 
29087c478bd9Sstevel@tonic-gate 	default:
29097c478bd9Sstevel@tonic-gate 		assert(0);
29107c478bd9Sstevel@tonic-gate 		abort();
29117c478bd9Sstevel@tonic-gate 	}
29127c478bd9Sstevel@tonic-gate 
29137c478bd9Sstevel@tonic-gate 	if (show_header)
29147c478bd9Sstevel@tonic-gate 		print_header();
29157c478bd9Sstevel@tonic-gate 
29167c478bd9Sstevel@tonic-gate 	(void) uu_avl_walk(lines, print_line, NULL, 0);
29177c478bd9Sstevel@tonic-gate 
29187c478bd9Sstevel@tonic-gate 	return (exit_status);
29197c478bd9Sstevel@tonic-gate }
2920