xref: /titanic_53/usr/src/cmd/svc/svcs/svcs.c (revision f6e214c7418f43af38bd8c3a557e3d0a1d311cfa)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
594501b61Sskamm  * Common Development and Distribution License (the "License").
694501b61Sskamm  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
203eae19d9Swesolows  */
213eae19d9Swesolows 
223eae19d9Swesolows /*
23*f6e214c7SGavin Maltby  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * svcs - display attributes of service instances
287c478bd9Sstevel@tonic-gate  *
297c478bd9Sstevel@tonic-gate  * We have two output formats and six instance selection mechanisms.  The
307c478bd9Sstevel@tonic-gate  * primary output format is a line of attributes (selected by -o), possibly
317c478bd9Sstevel@tonic-gate  * followed by process description lines (if -p is specified), for each
327c478bd9Sstevel@tonic-gate  * instance selected.  The columns available to display are described by the
337c478bd9Sstevel@tonic-gate  * struct column columns array.  The columns to actually display are kept in
347c478bd9Sstevel@tonic-gate  * the opt_columns array as indicies into the columns array.  The selection
357c478bd9Sstevel@tonic-gate  * mechanisms available for this format are service FMRIs (selects all child
367c478bd9Sstevel@tonic-gate  * instances), instance FMRIs, instance FMRI glob patterns, instances with
377c478bd9Sstevel@tonic-gate  * a certain restarter (-R), dependencies of instances (-d), and dependents of
387c478bd9Sstevel@tonic-gate  * instances (-D).  Since the lines must be sorted (per -sS), we'll just stick
397c478bd9Sstevel@tonic-gate  * each into a data structure and print them in order when we're done.  To
407c478bd9Sstevel@tonic-gate  * avoid listing the same instance twice (when -d and -D aren't given), we'll
417c478bd9Sstevel@tonic-gate  * use a hash table of FMRIs to record that we've listed (added to the tree)
427c478bd9Sstevel@tonic-gate  * an instance.
437c478bd9Sstevel@tonic-gate  *
447c478bd9Sstevel@tonic-gate  * The secondary output format (-l "long") is a paragraph of text for the
457c478bd9Sstevel@tonic-gate  * services or instances selected.  Not needing to be sorted, it's implemented
467c478bd9Sstevel@tonic-gate  * by just calling print_detailed() for each FMRI given.
477c478bd9Sstevel@tonic-gate  */
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate #include "svcs.h"
50*f6e214c7SGavin Maltby #include "notify_params.h"
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate /* Get the byteorder macros to ease sorting. */
537c478bd9Sstevel@tonic-gate #include <sys/types.h>
547c478bd9Sstevel@tonic-gate #include <netinet/in.h>
557c478bd9Sstevel@tonic-gate #include <inttypes.h>
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate #include <sys/contract.h>
587c478bd9Sstevel@tonic-gate #include <sys/ctfs.h>
597c478bd9Sstevel@tonic-gate #include <sys/stat.h>
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate #include <assert.h>
627c478bd9Sstevel@tonic-gate #include <errno.h>
637c478bd9Sstevel@tonic-gate #include <fcntl.h>
647c478bd9Sstevel@tonic-gate #include <fnmatch.h>
657c478bd9Sstevel@tonic-gate #include <libcontract.h>
667c478bd9Sstevel@tonic-gate #include <libcontract_priv.h>
677c478bd9Sstevel@tonic-gate #include <libintl.h>
687c478bd9Sstevel@tonic-gate #include <libscf.h>
697c478bd9Sstevel@tonic-gate #include <libscf_priv.h>
707c478bd9Sstevel@tonic-gate #include <libuutil.h>
71*f6e214c7SGavin Maltby #include <libnvpair.h>
727c478bd9Sstevel@tonic-gate #include <locale.h>
737c478bd9Sstevel@tonic-gate #include <procfs.h>
747c478bd9Sstevel@tonic-gate #include <stdarg.h>
757c478bd9Sstevel@tonic-gate #include <stdio.h>
767c478bd9Sstevel@tonic-gate #include <stdlib.h>
777c478bd9Sstevel@tonic-gate #include <strings.h>
787c478bd9Sstevel@tonic-gate #include <time.h>
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate #ifndef TEXT_DOMAIN
827c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SUNW_OST_OSCMD"
837c478bd9Sstevel@tonic-gate #endif /* TEXT_DOMAIN */
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate #define	LEGACY_UNKNOWN	"unknown"
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate /* Flags for pg_get_single_val() */
887c478bd9Sstevel@tonic-gate #define	EMPTY_OK	0x01
897c478bd9Sstevel@tonic-gate #define	MULTI_OK	0x02
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate /*
937c478bd9Sstevel@tonic-gate  * An AVL-storable node for output lines and the keys to sort them by.
947c478bd9Sstevel@tonic-gate  */
957c478bd9Sstevel@tonic-gate struct avl_string {
967c478bd9Sstevel@tonic-gate 	uu_avl_node_t	node;
977c478bd9Sstevel@tonic-gate 	char		*key;
987c478bd9Sstevel@tonic-gate 	char		*str;
997c478bd9Sstevel@tonic-gate };
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate /*
1027c478bd9Sstevel@tonic-gate  * For lists of parsed restarter FMRIs.
1037c478bd9Sstevel@tonic-gate  */
1047c478bd9Sstevel@tonic-gate struct pfmri_list {
1057c478bd9Sstevel@tonic-gate 	const char		*scope;
1067c478bd9Sstevel@tonic-gate 	const char		*service;
1077c478bd9Sstevel@tonic-gate 	const char		*instance;
1087c478bd9Sstevel@tonic-gate 	struct pfmri_list	*next;
1097c478bd9Sstevel@tonic-gate };
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate /*
1137c478bd9Sstevel@tonic-gate  * Globals
1147c478bd9Sstevel@tonic-gate  */
1157c478bd9Sstevel@tonic-gate scf_handle_t *h;
1167c478bd9Sstevel@tonic-gate static scf_propertygroup_t *g_pg;
1177c478bd9Sstevel@tonic-gate static scf_property_t *g_prop;
1187c478bd9Sstevel@tonic-gate static scf_value_t *g_val;
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate static size_t line_sz;			/* Bytes in the header line. */
1217c478bd9Sstevel@tonic-gate static size_t sortkey_sz;		/* Bytes in sort keys. */
1227c478bd9Sstevel@tonic-gate static uu_avl_pool_t *lines_pool;
1237c478bd9Sstevel@tonic-gate static uu_avl_t *lines;			/* Output lines. */
1247c478bd9Sstevel@tonic-gate int exit_status;
1257c478bd9Sstevel@tonic-gate ssize_t max_scf_name_length;
1267c478bd9Sstevel@tonic-gate ssize_t max_scf_value_length;
1277c478bd9Sstevel@tonic-gate ssize_t max_scf_fmri_length;
1281f6eb021SLiane Praza static ssize_t max_scf_type_length;
1297c478bd9Sstevel@tonic-gate static time_t now;
1307c478bd9Sstevel@tonic-gate static struct pfmri_list *restarters = NULL;
1317c478bd9Sstevel@tonic-gate static int first_paragraph = 1;		/* For -l mode. */
1327c478bd9Sstevel@tonic-gate static char *common_name_buf;		/* Sized for maximal length value. */
1337c478bd9Sstevel@tonic-gate char *locale;				/* Current locale. */
1347c478bd9Sstevel@tonic-gate 
13594501b61Sskamm /*
13694501b61Sskamm  * Pathname storage for path generated from the fmri.
13794501b61Sskamm  * Used for reading the ctid and (start) pid files for an inetd service.
13894501b61Sskamm  */
13994501b61Sskamm static char genfmri_filename[MAXPATHLEN] = "";
14094501b61Sskamm 
1417c478bd9Sstevel@tonic-gate /* Options */
1427c478bd9Sstevel@tonic-gate static int *opt_columns = NULL;		/* Indices into columns to display. */
1437c478bd9Sstevel@tonic-gate static int opt_cnum = 0;
1447c478bd9Sstevel@tonic-gate static int opt_processes = 0;		/* Print processes? */
1457c478bd9Sstevel@tonic-gate static int *opt_sort = NULL;		/* Indices into columns to sort. */
1467c478bd9Sstevel@tonic-gate static int opt_snum = 0;
1477c478bd9Sstevel@tonic-gate static int opt_nstate_shown = 0;	/* Will nstate be shown? */
1487c478bd9Sstevel@tonic-gate static int opt_verbose = 0;
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate /* Minimize string constants. */
1517c478bd9Sstevel@tonic-gate static const char * const scf_property_state = SCF_PROPERTY_STATE;
1527c478bd9Sstevel@tonic-gate static const char * const scf_property_next_state = SCF_PROPERTY_NEXT_STATE;
1537c478bd9Sstevel@tonic-gate static const char * const scf_property_contract = SCF_PROPERTY_CONTRACT;
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate /*
1577c478bd9Sstevel@tonic-gate  * Utility functions
1587c478bd9Sstevel@tonic-gate  */
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate /*
1617c478bd9Sstevel@tonic-gate  * For unexpected libscf errors.  The ending newline is necessary to keep
1627c478bd9Sstevel@tonic-gate  * uu_die() from appending the errno error.
1637c478bd9Sstevel@tonic-gate  */
1647c478bd9Sstevel@tonic-gate #ifndef NDEBUG
1657c478bd9Sstevel@tonic-gate void
1667c478bd9Sstevel@tonic-gate do_scfdie(const char *file, int line)
1677c478bd9Sstevel@tonic-gate {
1687c478bd9Sstevel@tonic-gate 	uu_die(gettext("%s:%d: Unexpected libscf error: %s.  Exiting.\n"),
1697c478bd9Sstevel@tonic-gate 	    file, line, scf_strerror(scf_error()));
1707c478bd9Sstevel@tonic-gate }
1717c478bd9Sstevel@tonic-gate #else
1727c478bd9Sstevel@tonic-gate void
1737c478bd9Sstevel@tonic-gate scfdie(void)
1747c478bd9Sstevel@tonic-gate {
1757c478bd9Sstevel@tonic-gate 	uu_die(gettext("Unexpected libscf error: %s.  Exiting.\n"),
1767c478bd9Sstevel@tonic-gate 	    scf_strerror(scf_error()));
1777c478bd9Sstevel@tonic-gate }
1787c478bd9Sstevel@tonic-gate #endif
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate void *
1817c478bd9Sstevel@tonic-gate safe_malloc(size_t sz)
1827c478bd9Sstevel@tonic-gate {
1837c478bd9Sstevel@tonic-gate 	void *ptr;
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	ptr = malloc(sz);
1867c478bd9Sstevel@tonic-gate 	if (ptr == NULL)
1877c478bd9Sstevel@tonic-gate 		uu_die(gettext("Out of memory"));
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	return (ptr);
1907c478bd9Sstevel@tonic-gate }
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate char *
1937c478bd9Sstevel@tonic-gate safe_strdup(const char *str)
1947c478bd9Sstevel@tonic-gate {
1957c478bd9Sstevel@tonic-gate 	char *cp;
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	cp = strdup(str);
1987c478bd9Sstevel@tonic-gate 	if (cp == NULL)
1997c478bd9Sstevel@tonic-gate 		uu_die(gettext("Out of memory.\n"));
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	return (cp);
2027c478bd9Sstevel@tonic-gate }
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate /*
2057c478bd9Sstevel@tonic-gate  * FMRI hashtable.  For uniquifing listings.
2067c478bd9Sstevel@tonic-gate  */
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate struct ht_elem {
2097c478bd9Sstevel@tonic-gate 	const char	*fmri;
2107c478bd9Sstevel@tonic-gate 	struct ht_elem	*next;
2117c478bd9Sstevel@tonic-gate };
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate static struct ht_elem	**ht_buckets;
2147c478bd9Sstevel@tonic-gate static uint_t		ht_buckets_num;
2157c478bd9Sstevel@tonic-gate static uint_t		ht_num;
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate static void
2187c478bd9Sstevel@tonic-gate ht_init()
2197c478bd9Sstevel@tonic-gate {
2207c478bd9Sstevel@tonic-gate 	ht_buckets_num = 8;
2217c478bd9Sstevel@tonic-gate 	ht_buckets = safe_malloc(sizeof (*ht_buckets) * ht_buckets_num);
2227c478bd9Sstevel@tonic-gate 	bzero(ht_buckets, sizeof (*ht_buckets) * ht_buckets_num);
2237c478bd9Sstevel@tonic-gate 	ht_num = 0;
2247c478bd9Sstevel@tonic-gate }
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate static uint_t
2277c478bd9Sstevel@tonic-gate ht_hash_fmri(const char *fmri)
2287c478bd9Sstevel@tonic-gate {
2297c478bd9Sstevel@tonic-gate 	uint_t h = 0, g;
2307c478bd9Sstevel@tonic-gate 	const char *p, *k;
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	/* All FMRIs begin with svc:/, so skip that part. */
2337c478bd9Sstevel@tonic-gate 	assert(strncmp(fmri, "svc:/", sizeof ("svc:/") - 1) == 0);
2347c478bd9Sstevel@tonic-gate 	k = fmri + sizeof ("svc:/") - 1;
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	/*
2377c478bd9Sstevel@tonic-gate 	 * Generic hash function from uts/common/os/modhash.c.
2387c478bd9Sstevel@tonic-gate 	 */
2397c478bd9Sstevel@tonic-gate 	for (p = k; *p != '\0'; ++p) {
2407c478bd9Sstevel@tonic-gate 		h = (h << 4) + *p;
2417c478bd9Sstevel@tonic-gate 		if ((g = (h & 0xf0000000)) != 0) {
2427c478bd9Sstevel@tonic-gate 			h ^= (g >> 24);
2437c478bd9Sstevel@tonic-gate 			h ^= g;
2447c478bd9Sstevel@tonic-gate 		}
2457c478bd9Sstevel@tonic-gate 	}
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	return (h);
2487c478bd9Sstevel@tonic-gate }
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate static void
2517c478bd9Sstevel@tonic-gate ht_grow()
2527c478bd9Sstevel@tonic-gate {
2537c478bd9Sstevel@tonic-gate 	uint_t new_ht_buckets_num;
2547c478bd9Sstevel@tonic-gate 	struct ht_elem **new_ht_buckets;
2557c478bd9Sstevel@tonic-gate 	int i;
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	new_ht_buckets_num = ht_buckets_num * 2;
2587c478bd9Sstevel@tonic-gate 	assert(new_ht_buckets_num > ht_buckets_num);
2597c478bd9Sstevel@tonic-gate 	new_ht_buckets =
2607c478bd9Sstevel@tonic-gate 	    safe_malloc(sizeof (*new_ht_buckets) * new_ht_buckets_num);
2617c478bd9Sstevel@tonic-gate 	bzero(new_ht_buckets, sizeof (*new_ht_buckets) * new_ht_buckets_num);
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	for (i = 0; i < ht_buckets_num; ++i) {
2647c478bd9Sstevel@tonic-gate 		struct ht_elem *elem, *next;
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 		for (elem = ht_buckets[i]; elem != NULL; elem = next) {
2677c478bd9Sstevel@tonic-gate 			uint_t h;
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 			next = elem->next;
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 			h = ht_hash_fmri(elem->fmri);
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 			elem->next =
2747c478bd9Sstevel@tonic-gate 			    new_ht_buckets[h & (new_ht_buckets_num - 1)];
2757c478bd9Sstevel@tonic-gate 			new_ht_buckets[h & (new_ht_buckets_num - 1)] = elem;
2767c478bd9Sstevel@tonic-gate 		}
2777c478bd9Sstevel@tonic-gate 	}
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	free(ht_buckets);
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	ht_buckets = new_ht_buckets;
2827c478bd9Sstevel@tonic-gate 	ht_buckets_num = new_ht_buckets_num;
2837c478bd9Sstevel@tonic-gate }
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate /*
2867c478bd9Sstevel@tonic-gate  * Add an FMRI to the hash table.  Returns 1 if it was already there,
2877c478bd9Sstevel@tonic-gate  * 0 otherwise.
2887c478bd9Sstevel@tonic-gate  */
2897c478bd9Sstevel@tonic-gate static int
2907c478bd9Sstevel@tonic-gate ht_add(const char *fmri)
2917c478bd9Sstevel@tonic-gate {
2927c478bd9Sstevel@tonic-gate 	uint_t h;
2937c478bd9Sstevel@tonic-gate 	struct ht_elem *elem;
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 	h = ht_hash_fmri(fmri);
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	elem = ht_buckets[h & (ht_buckets_num - 1)];
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	for (; elem != NULL; elem = elem->next) {
3007c478bd9Sstevel@tonic-gate 		if (strcmp(elem->fmri, fmri) == 0)
3017c478bd9Sstevel@tonic-gate 			return (1);
3027c478bd9Sstevel@tonic-gate 	}
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	/* Grow when average chain length is over 3. */
3057c478bd9Sstevel@tonic-gate 	if (ht_num > 3 * ht_buckets_num)
3067c478bd9Sstevel@tonic-gate 		ht_grow();
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	++ht_num;
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 	elem = safe_malloc(sizeof (*elem));
3117c478bd9Sstevel@tonic-gate 	elem->fmri = strdup(fmri);
3127c478bd9Sstevel@tonic-gate 	elem->next = ht_buckets[h & (ht_buckets_num - 1)];
3137c478bd9Sstevel@tonic-gate 	ht_buckets[h & (ht_buckets_num - 1)] = elem;
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	return (0);
3167c478bd9Sstevel@tonic-gate }
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate /*
3217c478bd9Sstevel@tonic-gate  * Convenience libscf wrapper functions.
3227c478bd9Sstevel@tonic-gate  */
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate /*
3257c478bd9Sstevel@tonic-gate  * Get the single value of the named property in the given property group,
3267c478bd9Sstevel@tonic-gate  * which must have type ty, and put it in *vp.  If ty is SCF_TYPE_ASTRING, vp
3277c478bd9Sstevel@tonic-gate  * is taken to be a char **, and sz is the size of the buffer.  sz is unused
3287c478bd9Sstevel@tonic-gate  * otherwise.  Return 0 on success, -1 if the property doesn't exist, has the
3297c478bd9Sstevel@tonic-gate  * wrong type, or doesn't have a single value.  If flags has EMPTY_OK, don't
3307c478bd9Sstevel@tonic-gate  * complain if the property has no values (but return nonzero).  If flags has
3317c478bd9Sstevel@tonic-gate  * MULTI_OK and the property has multiple values, succeed with E2BIG.
3327c478bd9Sstevel@tonic-gate  */
3337c478bd9Sstevel@tonic-gate int
3347c478bd9Sstevel@tonic-gate pg_get_single_val(scf_propertygroup_t *pg, const char *propname, scf_type_t ty,
3357c478bd9Sstevel@tonic-gate     void *vp, size_t sz, uint_t flags)
3367c478bd9Sstevel@tonic-gate {
3377c478bd9Sstevel@tonic-gate 	char *buf;
3387c478bd9Sstevel@tonic-gate 	size_t buf_sz;
3397c478bd9Sstevel@tonic-gate 	int ret = -1, r;
3407c478bd9Sstevel@tonic-gate 	boolean_t multi = B_FALSE;
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	assert((flags & ~(EMPTY_OK | MULTI_OK)) == 0);
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	if (scf_pg_get_property(pg, propname, g_prop) == -1) {
3457c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_NOT_FOUND)
3467c478bd9Sstevel@tonic-gate 			scfdie();
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 		goto out;
3497c478bd9Sstevel@tonic-gate 	}
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 	if (scf_property_is_type(g_prop, ty) != SCF_SUCCESS) {
3527c478bd9Sstevel@tonic-gate 		if (scf_error() == SCF_ERROR_TYPE_MISMATCH)
3537c478bd9Sstevel@tonic-gate 			goto misconfigured;
3547c478bd9Sstevel@tonic-gate 		scfdie();
3557c478bd9Sstevel@tonic-gate 	}
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 	if (scf_property_get_value(g_prop, g_val) != SCF_SUCCESS) {
3587c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
3597c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
3607c478bd9Sstevel@tonic-gate 			if (flags & EMPTY_OK)
3617c478bd9Sstevel@tonic-gate 				goto out;
3627c478bd9Sstevel@tonic-gate 			goto misconfigured;
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
3657c478bd9Sstevel@tonic-gate 			if (flags & MULTI_OK) {
3667c478bd9Sstevel@tonic-gate 				multi = B_TRUE;
3677c478bd9Sstevel@tonic-gate 				break;
3687c478bd9Sstevel@tonic-gate 			}
3697c478bd9Sstevel@tonic-gate 			goto misconfigured;
3707c478bd9Sstevel@tonic-gate 
3713eae19d9Swesolows 		case SCF_ERROR_PERMISSION_DENIED:
3727c478bd9Sstevel@tonic-gate 		default:
3737c478bd9Sstevel@tonic-gate 			scfdie();
3747c478bd9Sstevel@tonic-gate 		}
3757c478bd9Sstevel@tonic-gate 	}
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 	switch (ty) {
3787c478bd9Sstevel@tonic-gate 	case SCF_TYPE_ASTRING:
3797c478bd9Sstevel@tonic-gate 		r = scf_value_get_astring(g_val, vp, sz) > 0 ? SCF_SUCCESS : -1;
3807c478bd9Sstevel@tonic-gate 		break;
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	case SCF_TYPE_BOOLEAN:
3837c478bd9Sstevel@tonic-gate 		r = scf_value_get_boolean(g_val, (uint8_t *)vp);
3847c478bd9Sstevel@tonic-gate 		break;
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 	case SCF_TYPE_COUNT:
3877c478bd9Sstevel@tonic-gate 		r = scf_value_get_count(g_val, (uint64_t *)vp);
3887c478bd9Sstevel@tonic-gate 		break;
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 	case SCF_TYPE_INTEGER:
3917c478bd9Sstevel@tonic-gate 		r = scf_value_get_integer(g_val, (int64_t *)vp);
3927c478bd9Sstevel@tonic-gate 		break;
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 	case SCF_TYPE_TIME: {
3957c478bd9Sstevel@tonic-gate 		int64_t sec;
3967c478bd9Sstevel@tonic-gate 		int32_t ns;
3977c478bd9Sstevel@tonic-gate 		r = scf_value_get_time(g_val, &sec, &ns);
3987c478bd9Sstevel@tonic-gate 		((struct timeval *)vp)->tv_sec = sec;
3997c478bd9Sstevel@tonic-gate 		((struct timeval *)vp)->tv_usec = ns / 1000;
4007c478bd9Sstevel@tonic-gate 		break;
4017c478bd9Sstevel@tonic-gate 	}
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 	case SCF_TYPE_USTRING:
4047c478bd9Sstevel@tonic-gate 		r = scf_value_get_ustring(g_val, vp, sz) > 0 ? SCF_SUCCESS : -1;
4057c478bd9Sstevel@tonic-gate 		break;
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 	default:
4087c478bd9Sstevel@tonic-gate #ifndef NDEBUG
4097c478bd9Sstevel@tonic-gate 		uu_warn("%s:%d: Unknown type %d.\n", __FILE__, __LINE__, ty);
4107c478bd9Sstevel@tonic-gate #endif
4117c478bd9Sstevel@tonic-gate 		abort();
4127c478bd9Sstevel@tonic-gate 	}
4137c478bd9Sstevel@tonic-gate 	if (r != SCF_SUCCESS)
4147c478bd9Sstevel@tonic-gate 		scfdie();
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 	ret = multi ? E2BIG : 0;
4177c478bd9Sstevel@tonic-gate 	goto out;
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate misconfigured:
4207c478bd9Sstevel@tonic-gate 	buf_sz = max_scf_fmri_length + 1;
4217c478bd9Sstevel@tonic-gate 	buf = safe_malloc(buf_sz);
4227c478bd9Sstevel@tonic-gate 	if (scf_property_to_fmri(g_prop, buf, buf_sz) == -1)
4237c478bd9Sstevel@tonic-gate 		scfdie();
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 	uu_warn(gettext("Property \"%s\" is misconfigured.\n"), buf);
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 	free(buf);
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate out:
4307c478bd9Sstevel@tonic-gate 	return (ret);
4317c478bd9Sstevel@tonic-gate }
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate static scf_snapshot_t *
4347c478bd9Sstevel@tonic-gate get_running_snapshot(scf_instance_t *inst)
4357c478bd9Sstevel@tonic-gate {
4367c478bd9Sstevel@tonic-gate 	scf_snapshot_t *snap;
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 	snap = scf_snapshot_create(h);
4397c478bd9Sstevel@tonic-gate 	if (snap == NULL)
4407c478bd9Sstevel@tonic-gate 		scfdie();
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 	if (scf_instance_get_snapshot(inst, "running", snap) == 0)
4437c478bd9Sstevel@tonic-gate 		return (snap);
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 	if (scf_error() != SCF_ERROR_NOT_FOUND)
4467c478bd9Sstevel@tonic-gate 		scfdie();
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	scf_snapshot_destroy(snap);
4497c478bd9Sstevel@tonic-gate 	return (NULL);
4507c478bd9Sstevel@tonic-gate }
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate /*
4537c478bd9Sstevel@tonic-gate  * As pg_get_single_val(), except look the property group up in an
4547c478bd9Sstevel@tonic-gate  * instance.  If "use_running" is set, and the running snapshot exists,
4557c478bd9Sstevel@tonic-gate  * do a composed lookup there.  Otherwise, do an (optionally composed)
4567c478bd9Sstevel@tonic-gate  * lookup on the current values.  Note that lookups using snapshots are
4577c478bd9Sstevel@tonic-gate  * always composed.
4587c478bd9Sstevel@tonic-gate  */
4597c478bd9Sstevel@tonic-gate int
4607c478bd9Sstevel@tonic-gate inst_get_single_val(scf_instance_t *inst, const char *pgname,
4617c478bd9Sstevel@tonic-gate     const char *propname, scf_type_t ty, void *vp, size_t sz, uint_t flags,
4627c478bd9Sstevel@tonic-gate     int use_running, int composed)
4637c478bd9Sstevel@tonic-gate {
4647c478bd9Sstevel@tonic-gate 	scf_snapshot_t *snap = NULL;
4657c478bd9Sstevel@tonic-gate 	int r;
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 	if (use_running)
4687c478bd9Sstevel@tonic-gate 		snap = get_running_snapshot(inst);
4697c478bd9Sstevel@tonic-gate 	if (composed || use_running)
4707c478bd9Sstevel@tonic-gate 		r = scf_instance_get_pg_composed(inst, snap, pgname, g_pg);
4717c478bd9Sstevel@tonic-gate 	else
4727c478bd9Sstevel@tonic-gate 		r = scf_instance_get_pg(inst, pgname, g_pg);
4737c478bd9Sstevel@tonic-gate 	if (snap)
4747c478bd9Sstevel@tonic-gate 		scf_snapshot_destroy(snap);
4757c478bd9Sstevel@tonic-gate 	if (r == -1)
4767c478bd9Sstevel@tonic-gate 		return (-1);
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate 	r = pg_get_single_val(g_pg, propname, ty, vp, sz, flags);
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 	return (r);
4817c478bd9Sstevel@tonic-gate }
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate static int
4847c478bd9Sstevel@tonic-gate instance_enabled(scf_instance_t *inst, boolean_t temp)
4857c478bd9Sstevel@tonic-gate {
4867c478bd9Sstevel@tonic-gate 	uint8_t b;
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 	if (inst_get_single_val(inst,
4897c478bd9Sstevel@tonic-gate 	    temp ? SCF_PG_GENERAL_OVR : SCF_PG_GENERAL, SCF_PROPERTY_ENABLED,
4907c478bd9Sstevel@tonic-gate 	    SCF_TYPE_BOOLEAN, &b, 0, 0, 0, 0) != 0)
4917c478bd9Sstevel@tonic-gate 		return (-1);
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 	return (b ? 1 : 0);
4947c478bd9Sstevel@tonic-gate }
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate /*
4977c478bd9Sstevel@tonic-gate  * Get a string property from the restarter property group of the given
4987c478bd9Sstevel@tonic-gate  * instance.  Return an empty string on normal problems.
4997c478bd9Sstevel@tonic-gate  */
5007c478bd9Sstevel@tonic-gate static void
5017c478bd9Sstevel@tonic-gate get_restarter_string_prop(scf_instance_t *inst, const char *pname,
5027c478bd9Sstevel@tonic-gate     char *buf, size_t buf_sz)
5037c478bd9Sstevel@tonic-gate {
5047c478bd9Sstevel@tonic-gate 	if (inst_get_single_val(inst, SCF_PG_RESTARTER, pname,
5057c478bd9Sstevel@tonic-gate 	    SCF_TYPE_ASTRING, buf, buf_sz, 0, 0, 1) != 0)
5067c478bd9Sstevel@tonic-gate 		*buf = '\0';
5077c478bd9Sstevel@tonic-gate }
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate static int
5107c478bd9Sstevel@tonic-gate get_restarter_time_prop(scf_instance_t *inst, const char *pname,
5117c478bd9Sstevel@tonic-gate     struct timeval *tvp, int ok_if_empty)
5127c478bd9Sstevel@tonic-gate {
5137c478bd9Sstevel@tonic-gate 	int r;
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 	r = inst_get_single_val(inst, SCF_PG_RESTARTER, pname, SCF_TYPE_TIME,
5167c478bd9Sstevel@tonic-gate 	    tvp, NULL, ok_if_empty ? EMPTY_OK : 0, 0, 1);
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 	return (r == 0 ? 0 : -1);
5197c478bd9Sstevel@tonic-gate }
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate static int
5227c478bd9Sstevel@tonic-gate get_restarter_count_prop(scf_instance_t *inst, const char *pname, uint64_t *cp,
5237c478bd9Sstevel@tonic-gate     uint_t flags)
5247c478bd9Sstevel@tonic-gate {
5257c478bd9Sstevel@tonic-gate 	return (inst_get_single_val(inst, SCF_PG_RESTARTER, pname,
5267c478bd9Sstevel@tonic-gate 	    SCF_TYPE_COUNT, cp, 0, flags, 0, 1));
5277c478bd9Sstevel@tonic-gate }
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate /*
5317c478bd9Sstevel@tonic-gate  * Generic functions
5327c478bd9Sstevel@tonic-gate  */
5337c478bd9Sstevel@tonic-gate 
53494501b61Sskamm /*
53594501b61Sskamm  * Return an array of pids associated with the given contract id.
53694501b61Sskamm  * Returned pids are added to the end of the pidsp array.
53794501b61Sskamm  */
53894501b61Sskamm static void
53994501b61Sskamm ctid_to_pids(uint64_t c, pid_t **pidsp, uint_t *np)
54094501b61Sskamm {
54194501b61Sskamm 	ct_stathdl_t ctst;
54294501b61Sskamm 	uint_t m;
54394501b61Sskamm 	int fd;
54494501b61Sskamm 	int r, err;
54594501b61Sskamm 	pid_t *pids;
54694501b61Sskamm 
54794501b61Sskamm 	fd = contract_open(c, NULL, "status", O_RDONLY);
54894501b61Sskamm 	if (fd < 0)
54994501b61Sskamm 		return;
55094501b61Sskamm 
55194501b61Sskamm 	err = ct_status_read(fd, CTD_ALL, &ctst);
55294501b61Sskamm 	if (err != 0) {
55394501b61Sskamm 		uu_warn(gettext("Could not read status of contract "
55494501b61Sskamm 		    "%ld: %s.\n"), c, strerror(err));
55594501b61Sskamm 		(void) close(fd);
55694501b61Sskamm 		return;
55794501b61Sskamm 	}
55894501b61Sskamm 
55994501b61Sskamm 	(void) close(fd);
56094501b61Sskamm 
56194501b61Sskamm 	r = ct_pr_status_get_members(ctst, &pids, &m);
56294501b61Sskamm 	assert(r == 0);
56394501b61Sskamm 
56494501b61Sskamm 	if (m == 0) {
56594501b61Sskamm 		ct_status_free(ctst);
56694501b61Sskamm 		return;
56794501b61Sskamm 	}
56894501b61Sskamm 
56994501b61Sskamm 	*pidsp = realloc(*pidsp, (*np + m) * sizeof (*pidsp));
57094501b61Sskamm 	if (*pidsp == NULL)
57194501b61Sskamm 		uu_die(gettext("Out of memory"));
57294501b61Sskamm 
57394501b61Sskamm 	bcopy(pids, *pidsp + *np, m * sizeof (*pids));
57494501b61Sskamm 	*np += m;
57594501b61Sskamm 
57694501b61Sskamm 	ct_status_free(ctst);
57794501b61Sskamm }
57894501b61Sskamm 
5797c478bd9Sstevel@tonic-gate static int
5807c478bd9Sstevel@tonic-gate propvals_to_pids(scf_propertygroup_t *pg, const char *pname, pid_t **pidsp,
5817c478bd9Sstevel@tonic-gate     uint_t *np, scf_property_t *prop, scf_value_t *val, scf_iter_t *iter)
5827c478bd9Sstevel@tonic-gate {
5837c478bd9Sstevel@tonic-gate 	scf_type_t ty;
5847c478bd9Sstevel@tonic-gate 	uint64_t c;
58594501b61Sskamm 	int r;
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate 	if (scf_pg_get_property(pg, pname, prop) != 0) {
5887c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_NOT_FOUND)
5897c478bd9Sstevel@tonic-gate 			scfdie();
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate 		return (ENOENT);
5927c478bd9Sstevel@tonic-gate 	}
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 	if (scf_property_type(prop, &ty) != 0)
5957c478bd9Sstevel@tonic-gate 		scfdie();
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate 	if (ty != SCF_TYPE_COUNT)
5987c478bd9Sstevel@tonic-gate 		return (EINVAL);
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 	if (scf_iter_property_values(iter, prop) != 0)
6017c478bd9Sstevel@tonic-gate 		scfdie();
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 	for (;;) {
6047c478bd9Sstevel@tonic-gate 		r = scf_iter_next_value(iter, val);
6057c478bd9Sstevel@tonic-gate 		if (r == -1)
6067c478bd9Sstevel@tonic-gate 			scfdie();
6077c478bd9Sstevel@tonic-gate 		if (r == 0)
6087c478bd9Sstevel@tonic-gate 			break;
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 		if (scf_value_get_count(val, &c) != 0)
6117c478bd9Sstevel@tonic-gate 			scfdie();
6127c478bd9Sstevel@tonic-gate 
61394501b61Sskamm 		ctid_to_pids(c, pidsp, np);
6147c478bd9Sstevel@tonic-gate 	}
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate 	return (0);
6177c478bd9Sstevel@tonic-gate }
6187c478bd9Sstevel@tonic-gate 
61994501b61Sskamm /*
62094501b61Sskamm  * Check if instance has general/restarter property that matches
62194501b61Sskamm  * given string.  Restarter string must be in canonified form.
62294501b61Sskamm  * Returns 0 for success; -1 otherwise.
62394501b61Sskamm  */
6247c478bd9Sstevel@tonic-gate static int
62594501b61Sskamm check_for_restarter(scf_instance_t *inst, const char *restarter)
62694501b61Sskamm {
62794501b61Sskamm 	char	*fmri_buf;
62894501b61Sskamm 	char	*fmri_buf_canonified = NULL;
62994501b61Sskamm 	int	ret = -1;
63094501b61Sskamm 
63194501b61Sskamm 	if (inst == NULL)
63294501b61Sskamm 		return (-1);
63394501b61Sskamm 
63494501b61Sskamm 	/* Get restarter */
63594501b61Sskamm 	fmri_buf = safe_malloc(max_scf_fmri_length + 1);
63694501b61Sskamm 	if (inst_get_single_val(inst, SCF_PG_GENERAL,
63794501b61Sskamm 	    SCF_PROPERTY_RESTARTER, SCF_TYPE_ASTRING, fmri_buf,
63894501b61Sskamm 	    max_scf_fmri_length + 1, 0, 0, 1) != 0)
63994501b61Sskamm 		goto out;
64094501b61Sskamm 
64194501b61Sskamm 	fmri_buf_canonified = safe_malloc(max_scf_fmri_length + 1);
64294501b61Sskamm 	if (scf_canonify_fmri(fmri_buf, fmri_buf_canonified,
64394501b61Sskamm 	    (max_scf_fmri_length + 1)) < 0)
64494501b61Sskamm 		goto out;
64594501b61Sskamm 
64694501b61Sskamm 	if (strcmp(fmri_buf, restarter) == 0)
64794501b61Sskamm 		ret = 0;
64894501b61Sskamm 
64994501b61Sskamm out:
65094501b61Sskamm 	free(fmri_buf);
65194501b61Sskamm 	if (fmri_buf_canonified)
65294501b61Sskamm 		free(fmri_buf_canonified);
65394501b61Sskamm 	return (ret);
65494501b61Sskamm }
65594501b61Sskamm 
65694501b61Sskamm /*
65794501b61Sskamm  * Common code that is used by ctids_by_restarter and pids_by_restarter.
65894501b61Sskamm  * Checks for a common restarter and if one is available, it generates
65994501b61Sskamm  * the appropriate filename using wip->fmri and stores that in the
66094501b61Sskamm  * global genfmri_filename.
66194501b61Sskamm  *
66294501b61Sskamm  * Restarters currently supported are: svc:/network/inetd:default
66394501b61Sskamm  * If a restarter specific action is available, then restarter_spec
66494501b61Sskamm  * is set to 1.  If a restarter specific action is not available, then
66594501b61Sskamm  * restarter_spec is set to 0 and a -1 is returned.
66694501b61Sskamm  *
66794501b61Sskamm  * Returns:
66894501b61Sskamm  * 0 if success: restarter specific action found and filename generated
66994501b61Sskamm  * -1 if restarter specific action not found,
67094501b61Sskamm  *    if restarter specific action found but an error was encountered
67194501b61Sskamm  *    during the generation of the wip->fmri based filename
67294501b61Sskamm  */
67394501b61Sskamm static int
67494501b61Sskamm common_by_restarter(scf_instance_t *inst, const char *fmri,
67594501b61Sskamm     int *restarter_specp)
67694501b61Sskamm {
67794501b61Sskamm 	int		ret = -1;
67894501b61Sskamm 	int		r;
67994501b61Sskamm 
68094501b61Sskamm 	/* Check for inetd specific restarter */
68194501b61Sskamm 	if (check_for_restarter(inst, "svc:/network/inetd:default") != 0) {
68294501b61Sskamm 		*restarter_specp = 0;
68394501b61Sskamm 		return (ret);
68494501b61Sskamm 	}
68594501b61Sskamm 
68694501b61Sskamm 	*restarter_specp = 1;
68794501b61Sskamm 
68894501b61Sskamm 	/* Get the ctid filename associated with this instance */
68994501b61Sskamm 	r = gen_filenms_from_fmri(fmri, "ctid", genfmri_filename, NULL);
69094501b61Sskamm 
69194501b61Sskamm 	switch (r) {
69294501b61Sskamm 	case 0:
69394501b61Sskamm 		break;
69494501b61Sskamm 
69594501b61Sskamm 	case -1:
69694501b61Sskamm 		/*
69794501b61Sskamm 		 * Unable to get filename from fmri.  Print warning
69894501b61Sskamm 		 * and return failure with no ctids.
69994501b61Sskamm 		 */
70094501b61Sskamm 		uu_warn(gettext("Unable to read contract ids for %s -- "
70194501b61Sskamm 		    "FMRI is too long\n"), fmri);
70294501b61Sskamm 		return (ret);
70394501b61Sskamm 
70494501b61Sskamm 	case -2:
70594501b61Sskamm 		/*
70694501b61Sskamm 		 * The directory didn't exist, so no contracts.
70794501b61Sskamm 		 * Return failure with no ctids.
70894501b61Sskamm 		 */
70994501b61Sskamm 		return (ret);
71094501b61Sskamm 
71194501b61Sskamm 	default:
71294501b61Sskamm 		uu_warn(gettext("%s:%d: gen_filenms_from_fmri() failed with "
71394501b61Sskamm 		    "unknown error %d\n"), __FILE__, __LINE__, r);
71494501b61Sskamm 		abort();
71594501b61Sskamm 	}
71694501b61Sskamm 
71794501b61Sskamm 	return (0);
71894501b61Sskamm 
71994501b61Sskamm }
72094501b61Sskamm 
72194501b61Sskamm /*
72294501b61Sskamm  * Get or print a contract id using a restarter specific action.
72394501b61Sskamm  *
72494501b61Sskamm  * If the print_flag is not set, this routine gets the single contract
72594501b61Sskamm  * id associated with this instance.
72694501b61Sskamm  * If the print flag is set, then print each contract id found.
72794501b61Sskamm  *
72894501b61Sskamm  * Returns:
72994501b61Sskamm  * 0 if success: restarter specific action found and used with no error
73094501b61Sskamm  * -1 if restarter specific action not found
73194501b61Sskamm  * -1 if restarter specific action found, but there was a failure
73294501b61Sskamm  * -1 if print flag is not set and no contract id is found or multiple
73394501b61Sskamm  *    contract ids were found
73494501b61Sskamm  * E2BIG if print flag is not set, MULTI_OK bit in flag is set and multiple
73594501b61Sskamm  *    contract ids were found
73694501b61Sskamm  */
73794501b61Sskamm static int
73894501b61Sskamm ctids_by_restarter(scf_walkinfo_t *wip, uint64_t *cp, int print_flag,
73994501b61Sskamm     uint_t flags, int *restarter_specp, void (*callback_header)(),
74094501b61Sskamm     void (*callback_ctid)(uint64_t))
74194501b61Sskamm {
74294501b61Sskamm 	FILE		*fp;
74394501b61Sskamm 	int		ret = -1;
74494501b61Sskamm 	int		fscanf_ret;
74594501b61Sskamm 	uint64_t	cp2;
74694501b61Sskamm 	int		rest_ret;
74794501b61Sskamm 
74894501b61Sskamm 	/* Check if callbacks are needed and were passed in */
74994501b61Sskamm 	if (print_flag) {
75094501b61Sskamm 		if ((callback_header == NULL) || (callback_ctid == NULL))
75194501b61Sskamm 			return (ret);
75294501b61Sskamm 	}
75394501b61Sskamm 
75494501b61Sskamm 	/* Check for restarter specific action and generation of filename */
75594501b61Sskamm 	rest_ret = common_by_restarter(wip->inst, wip->fmri, restarter_specp);
75694501b61Sskamm 	if (rest_ret != 0)
75794501b61Sskamm 		return (rest_ret);
75894501b61Sskamm 
75994501b61Sskamm 	/*
76094501b61Sskamm 	 * If fopen fails, then ctid file hasn't been created yet.
76194501b61Sskamm 	 * If print_flag is set, this is ok; otherwise fail.
76294501b61Sskamm 	 */
76394501b61Sskamm 	if ((fp = fopen(genfmri_filename, "r")) == NULL) {
76494501b61Sskamm 		if (print_flag)
76594501b61Sskamm 			return (0);
76694501b61Sskamm 		goto out;
76794501b61Sskamm 	}
76894501b61Sskamm 
76994501b61Sskamm 	if (print_flag) {
77094501b61Sskamm 		/*
77194501b61Sskamm 		 * Print all contract ids that are found.
77294501b61Sskamm 		 * First callback to print ctid header.
77394501b61Sskamm 		 */
77494501b61Sskamm 		callback_header();
77594501b61Sskamm 
77694501b61Sskamm 		/* fscanf may not set errno, so be sure to clear it first */
77794501b61Sskamm 		errno = 0;
77894501b61Sskamm 		while ((fscanf_ret = fscanf(fp, "%llu", cp)) == 1) {
77994501b61Sskamm 			/* Callback to print contract id */
78094501b61Sskamm 			callback_ctid(*cp);
78194501b61Sskamm 			errno = 0;
78294501b61Sskamm 		}
78394501b61Sskamm 		/* EOF is not a failure when no errno. */
78494501b61Sskamm 		if ((fscanf_ret != EOF) || (errno != 0)) {
78594501b61Sskamm 			uu_die(gettext("Unable to read ctid file for %s"),
78694501b61Sskamm 			    wip->fmri);
78794501b61Sskamm 		}
78894501b61Sskamm 		(void) putchar('\n');
78994501b61Sskamm 		ret = 0;
79094501b61Sskamm 	} else {
79194501b61Sskamm 		/* Must find 1 ctid or fail */
79294501b61Sskamm 		if (fscanf(fp, "%llu", cp) == 1) {
79394501b61Sskamm 			/* If 2nd ctid found - fail */
79494501b61Sskamm 			if (fscanf(fp, "%llu", &cp2) == 1) {
79594501b61Sskamm 				if (flags & MULTI_OK)
79694501b61Sskamm 					ret = E2BIG;
79794501b61Sskamm 			} else {
79894501b61Sskamm 				/* Success - found only 1 ctid */
79994501b61Sskamm 				ret = 0;
80094501b61Sskamm 			}
80194501b61Sskamm 		}
80294501b61Sskamm 	}
80394501b61Sskamm 	(void) fclose(fp);
80494501b61Sskamm 
80594501b61Sskamm out:
80694501b61Sskamm 	return (ret);
80794501b61Sskamm }
80894501b61Sskamm 
80994501b61Sskamm /*
81094501b61Sskamm  * Get the process ids associated with an instance using a restarter
81194501b61Sskamm  * specific action.
81294501b61Sskamm  *
81394501b61Sskamm  * Returns:
81494501b61Sskamm  *	0 if success: restarter specific action found and used with no error
81594501b61Sskamm  *	-1 restarter specific action not found or if failure
81694501b61Sskamm  */
81794501b61Sskamm static int
81894501b61Sskamm pids_by_restarter(scf_instance_t *inst, const char *fmri,
81994501b61Sskamm     pid_t **pids, uint_t *np, int *restarter_specp)
82094501b61Sskamm {
82194501b61Sskamm 	uint64_t	c;
82294501b61Sskamm 	FILE		*fp;
82394501b61Sskamm 	int		fscanf_ret;
82494501b61Sskamm 	int		rest_ret;
82594501b61Sskamm 
82694501b61Sskamm 	/* Check for restarter specific action and generation of filename */
82794501b61Sskamm 	rest_ret = common_by_restarter(inst, fmri, restarter_specp);
82894501b61Sskamm 	if (rest_ret != 0)
82994501b61Sskamm 		return (rest_ret);
83094501b61Sskamm 
83194501b61Sskamm 	/*
83294501b61Sskamm 	 * If fopen fails with ENOENT then the ctid file hasn't been
83394501b61Sskamm 	 * created yet so return success.
83494501b61Sskamm 	 * For all other errors - fail with uu_die.
83594501b61Sskamm 	 */
83694501b61Sskamm 	if ((fp = fopen(genfmri_filename, "r")) == NULL) {
83794501b61Sskamm 		if (errno == ENOENT)
83894501b61Sskamm 			return (0);
83994501b61Sskamm 		uu_die(gettext("Unable to open ctid file for %s"), fmri);
84094501b61Sskamm 	}
84194501b61Sskamm 
84294501b61Sskamm 	/* fscanf may not set errno, so be sure to clear it first */
84394501b61Sskamm 	errno = 0;
84494501b61Sskamm 	while ((fscanf_ret = fscanf(fp, "%llu", &c)) == 1) {
84594501b61Sskamm 		if (c == 0) {
84694501b61Sskamm 			(void) fclose(fp);
84794501b61Sskamm 			uu_die(gettext("ctid file for %s has corrupt data"),
84894501b61Sskamm 			    fmri);
84994501b61Sskamm 		}
85094501b61Sskamm 		ctid_to_pids(c, pids, np);
85194501b61Sskamm 		errno = 0;
85294501b61Sskamm 	}
85394501b61Sskamm 	/* EOF is not a failure when no errno. */
85494501b61Sskamm 	if ((fscanf_ret != EOF) || (errno != 0)) {
85594501b61Sskamm 		uu_die(gettext("Unable to read ctid file for %s"), fmri);
85694501b61Sskamm 	}
85794501b61Sskamm 
85894501b61Sskamm 	(void) fclose(fp);
85994501b61Sskamm 	return (0);
86094501b61Sskamm }
86194501b61Sskamm 
86294501b61Sskamm static int
86394501b61Sskamm instance_processes(scf_instance_t *inst, const char *fmri,
86494501b61Sskamm     pid_t **pids, uint_t *np)
8657c478bd9Sstevel@tonic-gate {
8667c478bd9Sstevel@tonic-gate 	scf_iter_t *iter;
8677c478bd9Sstevel@tonic-gate 	int ret;
86894501b61Sskamm 	int restarter_spec;
86994501b61Sskamm 
87094501b61Sskamm 	/* Use the restarter specific get pids routine, if available. */
87194501b61Sskamm 	ret = pids_by_restarter(inst, fmri, pids, np, &restarter_spec);
87294501b61Sskamm 	if (restarter_spec == 1)
87394501b61Sskamm 		return (ret);
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate 	if ((iter = scf_iter_create(h)) == NULL)
8767c478bd9Sstevel@tonic-gate 		scfdie();
8777c478bd9Sstevel@tonic-gate 
8787c478bd9Sstevel@tonic-gate 	if (scf_instance_get_pg(inst, SCF_PG_RESTARTER, g_pg) == 0) {
8797c478bd9Sstevel@tonic-gate 		*pids = NULL;
8807c478bd9Sstevel@tonic-gate 		*np = 0;
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate 		(void) propvals_to_pids(g_pg, scf_property_contract, pids, np,
8837c478bd9Sstevel@tonic-gate 		    g_prop, g_val, iter);
8847c478bd9Sstevel@tonic-gate 
8857c478bd9Sstevel@tonic-gate 		(void) propvals_to_pids(g_pg, SCF_PROPERTY_TRANSIENT_CONTRACT,
8867c478bd9Sstevel@tonic-gate 		    pids, np, g_prop, g_val, iter);
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate 		ret = 0;
8897c478bd9Sstevel@tonic-gate 	} else {
8907c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_NOT_FOUND)
8917c478bd9Sstevel@tonic-gate 			scfdie();
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate 		ret = -1;
8947c478bd9Sstevel@tonic-gate 	}
8957c478bd9Sstevel@tonic-gate 
8967c478bd9Sstevel@tonic-gate 	scf_iter_destroy(iter);
8977c478bd9Sstevel@tonic-gate 
8987c478bd9Sstevel@tonic-gate 	return (ret);
8997c478bd9Sstevel@tonic-gate }
9007c478bd9Sstevel@tonic-gate 
9017c478bd9Sstevel@tonic-gate static int
9027c478bd9Sstevel@tonic-gate get_psinfo(pid_t pid, psinfo_t *psip)
9037c478bd9Sstevel@tonic-gate {
9047c478bd9Sstevel@tonic-gate 	char path[100];
9057c478bd9Sstevel@tonic-gate 	int fd;
9067c478bd9Sstevel@tonic-gate 
9077c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "/proc/%lu/psinfo", pid);
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate 	fd = open64(path, O_RDONLY);
9107c478bd9Sstevel@tonic-gate 	if (fd < 0)
9117c478bd9Sstevel@tonic-gate 		return (-1);
9127c478bd9Sstevel@tonic-gate 
9137c478bd9Sstevel@tonic-gate 	if (read(fd, psip, sizeof (*psip)) < 0)
9147c478bd9Sstevel@tonic-gate 		uu_die(gettext("Could not read info for process %lu"), pid);
9157c478bd9Sstevel@tonic-gate 
9167c478bd9Sstevel@tonic-gate 	(void) close(fd);
9177c478bd9Sstevel@tonic-gate 
9187c478bd9Sstevel@tonic-gate 	return (0);
9197c478bd9Sstevel@tonic-gate }
9207c478bd9Sstevel@tonic-gate 
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate 
9237c478bd9Sstevel@tonic-gate /*
9247c478bd9Sstevel@tonic-gate  * Column sprint and sortkey functions
9257c478bd9Sstevel@tonic-gate  */
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate struct column {
9287c478bd9Sstevel@tonic-gate 	const char *name;
9297c478bd9Sstevel@tonic-gate 	int width;
9307c478bd9Sstevel@tonic-gate 
9317c478bd9Sstevel@tonic-gate 	/*
9327c478bd9Sstevel@tonic-gate 	 * This function should write the value for the column into buf, and
9337c478bd9Sstevel@tonic-gate 	 * grow or allocate buf accordingly.  It should always write at least
9347c478bd9Sstevel@tonic-gate 	 * width bytes, blanking unused bytes with spaces.  If the field is
9357c478bd9Sstevel@tonic-gate 	 * greater than the column width we allow it to overlap other columns.
9367c478bd9Sstevel@tonic-gate 	 * In particular, it shouldn't write any null bytes.  (Though an extra
9377c478bd9Sstevel@tonic-gate 	 * null byte past the end is currently tolerated.)  If the property
9387c478bd9Sstevel@tonic-gate 	 * group is non-NULL, then we are dealing with a legacy service.
9397c478bd9Sstevel@tonic-gate 	 */
9407c478bd9Sstevel@tonic-gate 	void (*sprint)(char **, scf_walkinfo_t *);
9417c478bd9Sstevel@tonic-gate 
9427c478bd9Sstevel@tonic-gate 	int sortkey_width;
9437c478bd9Sstevel@tonic-gate 
9447c478bd9Sstevel@tonic-gate 	/*
9457c478bd9Sstevel@tonic-gate 	 * This function should write sortkey_width bytes into buf which will
9467c478bd9Sstevel@tonic-gate 	 * cause memcmp() to sort it properly.  (Unlike sprint() above,
9477c478bd9Sstevel@tonic-gate 	 * however, an extra null byte may overrun the buffer.)  The second
9487c478bd9Sstevel@tonic-gate 	 * argument controls whether the results are sorted in forward or
9497c478bd9Sstevel@tonic-gate 	 * reverse order.
9507c478bd9Sstevel@tonic-gate 	 */
9517c478bd9Sstevel@tonic-gate 	void (*get_sortkey)(char *, int, scf_walkinfo_t *);
9527c478bd9Sstevel@tonic-gate };
9537c478bd9Sstevel@tonic-gate 
9547c478bd9Sstevel@tonic-gate static void
9557c478bd9Sstevel@tonic-gate reverse_bytes(char *buf, size_t len)
9567c478bd9Sstevel@tonic-gate {
9577c478bd9Sstevel@tonic-gate 	int i;
9587c478bd9Sstevel@tonic-gate 
9597c478bd9Sstevel@tonic-gate 	for (i = 0; i < len; ++i)
9607c478bd9Sstevel@tonic-gate 		buf[i] = ~buf[i];
9617c478bd9Sstevel@tonic-gate }
9627c478bd9Sstevel@tonic-gate 
9637c478bd9Sstevel@tonic-gate /* CTID */
9647c478bd9Sstevel@tonic-gate #define	CTID_COLUMN_WIDTH		6
9657c478bd9Sstevel@tonic-gate 
9667c478bd9Sstevel@tonic-gate static void
9677c478bd9Sstevel@tonic-gate sprint_ctid(char **buf, scf_walkinfo_t *wip)
9687c478bd9Sstevel@tonic-gate {
9697c478bd9Sstevel@tonic-gate 	int r;
9707c478bd9Sstevel@tonic-gate 	uint64_t c;
9717c478bd9Sstevel@tonic-gate 	size_t newsize = (*buf ? strlen(*buf) : 0) + CTID_COLUMN_WIDTH + 2;
9727c478bd9Sstevel@tonic-gate 	char *newbuf = safe_malloc(newsize);
97394501b61Sskamm 	int restarter_spec;
9747c478bd9Sstevel@tonic-gate 
97594501b61Sskamm 	/*
97694501b61Sskamm 	 * Use the restarter specific get pids routine, if available.
97794501b61Sskamm 	 * Only check for non-legacy services (wip->pg == 0).
97894501b61Sskamm 	 */
97994501b61Sskamm 	if (wip->pg != NULL) {
9807c478bd9Sstevel@tonic-gate 		r = pg_get_single_val(wip->pg, scf_property_contract,
9817c478bd9Sstevel@tonic-gate 		    SCF_TYPE_COUNT, &c, 0, EMPTY_OK | MULTI_OK);
98294501b61Sskamm 	} else {
98394501b61Sskamm 		r = ctids_by_restarter(wip, &c, 0, MULTI_OK, &restarter_spec,
98494501b61Sskamm 		    NULL, NULL);
98594501b61Sskamm 		if (restarter_spec == 0) {
98694501b61Sskamm 			/* No restarter specific routine */
98794501b61Sskamm 			r = get_restarter_count_prop(wip->inst,
98894501b61Sskamm 			    scf_property_contract, &c, EMPTY_OK | MULTI_OK);
98994501b61Sskamm 		}
99094501b61Sskamm 	}
9917c478bd9Sstevel@tonic-gate 
9927c478bd9Sstevel@tonic-gate 	if (r == 0)
9937c478bd9Sstevel@tonic-gate 		(void) snprintf(newbuf, newsize, "%s%*lu ",
9947c478bd9Sstevel@tonic-gate 		    *buf ? *buf : "", CTID_COLUMN_WIDTH, (ctid_t)c);
9957c478bd9Sstevel@tonic-gate 	else if (r == E2BIG)
9967c478bd9Sstevel@tonic-gate 		(void) snprintf(newbuf, newsize, "%s%*lu* ",
9977c478bd9Sstevel@tonic-gate 		    *buf ? *buf : "", CTID_COLUMN_WIDTH - 1, (ctid_t)c);
9987c478bd9Sstevel@tonic-gate 	else
9997c478bd9Sstevel@tonic-gate 		(void) snprintf(newbuf, newsize, "%s%*s ",
10007c478bd9Sstevel@tonic-gate 		    *buf ? *buf : "", CTID_COLUMN_WIDTH, "-");
10017c478bd9Sstevel@tonic-gate 	if (*buf)
10027c478bd9Sstevel@tonic-gate 		free(*buf);
10037c478bd9Sstevel@tonic-gate 	*buf = newbuf;
10047c478bd9Sstevel@tonic-gate }
10057c478bd9Sstevel@tonic-gate 
10067c478bd9Sstevel@tonic-gate #define	CTID_SORTKEY_WIDTH		(sizeof (uint64_t))
10077c478bd9Sstevel@tonic-gate 
10087c478bd9Sstevel@tonic-gate static void
10097c478bd9Sstevel@tonic-gate sortkey_ctid(char *buf, int reverse, scf_walkinfo_t *wip)
10107c478bd9Sstevel@tonic-gate {
10117c478bd9Sstevel@tonic-gate 	int r;
10127c478bd9Sstevel@tonic-gate 	uint64_t c;
101394501b61Sskamm 	int restarter_spec;
10147c478bd9Sstevel@tonic-gate 
101594501b61Sskamm 	/*
101694501b61Sskamm 	 * Use the restarter specific get pids routine, if available.
101794501b61Sskamm 	 * Only check for non-legacy services (wip->pg == 0).
101894501b61Sskamm 	 */
101994501b61Sskamm 	if (wip->pg != NULL) {
10207c478bd9Sstevel@tonic-gate 		r = pg_get_single_val(wip->pg, scf_property_contract,
10217c478bd9Sstevel@tonic-gate 		    SCF_TYPE_COUNT, &c, 0, EMPTY_OK);
102294501b61Sskamm 	} else {
102394501b61Sskamm 		r = ctids_by_restarter(wip, &c, 0, MULTI_OK, &restarter_spec,
102494501b61Sskamm 		    NULL, NULL);
102594501b61Sskamm 		if (restarter_spec == 0) {
102694501b61Sskamm 			/* No restarter specific routine */
102794501b61Sskamm 			r = get_restarter_count_prop(wip->inst,
102894501b61Sskamm 			    scf_property_contract, &c, EMPTY_OK);
102994501b61Sskamm 		}
103094501b61Sskamm 	}
10317c478bd9Sstevel@tonic-gate 
10327c478bd9Sstevel@tonic-gate 	if (r == 0) {
10337c478bd9Sstevel@tonic-gate 		/*
10347c478bd9Sstevel@tonic-gate 		 * Use the id itself, but it must be big-endian for this to
10357c478bd9Sstevel@tonic-gate 		 * work.
10367c478bd9Sstevel@tonic-gate 		 */
10377c478bd9Sstevel@tonic-gate 		c = BE_64(c);
10387c478bd9Sstevel@tonic-gate 
10397c478bd9Sstevel@tonic-gate 		bcopy(&c, buf, CTID_SORTKEY_WIDTH);
10407c478bd9Sstevel@tonic-gate 	} else {
10417c478bd9Sstevel@tonic-gate 		bzero(buf, CTID_SORTKEY_WIDTH);
10427c478bd9Sstevel@tonic-gate 	}
10437c478bd9Sstevel@tonic-gate 
10447c478bd9Sstevel@tonic-gate 	if (reverse)
10457c478bd9Sstevel@tonic-gate 		reverse_bytes(buf, CTID_SORTKEY_WIDTH);
10467c478bd9Sstevel@tonic-gate }
10477c478bd9Sstevel@tonic-gate 
10487c478bd9Sstevel@tonic-gate /* DESC */
10497c478bd9Sstevel@tonic-gate #define	DESC_COLUMN_WIDTH	100
10507c478bd9Sstevel@tonic-gate 
10517c478bd9Sstevel@tonic-gate static void
10527c478bd9Sstevel@tonic-gate sprint_desc(char **buf, scf_walkinfo_t *wip)
10537c478bd9Sstevel@tonic-gate {
10547c478bd9Sstevel@tonic-gate 	char *x;
10557c478bd9Sstevel@tonic-gate 	size_t newsize;
10567c478bd9Sstevel@tonic-gate 	char *newbuf;
10577c478bd9Sstevel@tonic-gate 
10587c478bd9Sstevel@tonic-gate 	if (common_name_buf == NULL)
10597c478bd9Sstevel@tonic-gate 		common_name_buf = safe_malloc(max_scf_value_length + 1);
10607c478bd9Sstevel@tonic-gate 
10617c478bd9Sstevel@tonic-gate 	bzero(common_name_buf, max_scf_value_length + 1);
10627c478bd9Sstevel@tonic-gate 
10637c478bd9Sstevel@tonic-gate 	if (wip->pg != NULL) {
10647c478bd9Sstevel@tonic-gate 		common_name_buf[0] = '-';
10657c478bd9Sstevel@tonic-gate 	} else if (inst_get_single_val(wip->inst, SCF_PG_TM_COMMON_NAME, locale,
10667c478bd9Sstevel@tonic-gate 	    SCF_TYPE_USTRING, common_name_buf, max_scf_value_length, 0,
10677c478bd9Sstevel@tonic-gate 	    1, 1) == -1 &&
10687c478bd9Sstevel@tonic-gate 	    inst_get_single_val(wip->inst, SCF_PG_TM_COMMON_NAME, "C",
10697c478bd9Sstevel@tonic-gate 	    SCF_TYPE_USTRING, common_name_buf, max_scf_value_length, 0,
10707c478bd9Sstevel@tonic-gate 	    1, 1) == -1) {
10717c478bd9Sstevel@tonic-gate 		common_name_buf[0] = '-';
10727c478bd9Sstevel@tonic-gate 	}
10737c478bd9Sstevel@tonic-gate 
10747c478bd9Sstevel@tonic-gate 	/*
10757c478bd9Sstevel@tonic-gate 	 * Collapse multi-line tm_common_name values into a single line.
10767c478bd9Sstevel@tonic-gate 	 */
10777c478bd9Sstevel@tonic-gate 	for (x = common_name_buf; *x != '\0'; x++)
10787c478bd9Sstevel@tonic-gate 		if (*x == '\n')
10797c478bd9Sstevel@tonic-gate 			*x = ' ';
10807c478bd9Sstevel@tonic-gate 
10817c478bd9Sstevel@tonic-gate 	if (strlen(common_name_buf) > DESC_COLUMN_WIDTH)
10827c478bd9Sstevel@tonic-gate 		newsize = (*buf ? strlen(*buf) : 0) +
10837c478bd9Sstevel@tonic-gate 		    strlen(common_name_buf) + 1;
10847c478bd9Sstevel@tonic-gate 	else
10857c478bd9Sstevel@tonic-gate 		newsize = (*buf ? strlen(*buf) : 0) + DESC_COLUMN_WIDTH + 1;
10867c478bd9Sstevel@tonic-gate 	newbuf = safe_malloc(newsize);
10877c478bd9Sstevel@tonic-gate 	(void) snprintf(newbuf, newsize, "%s%-*s ", *buf ? *buf : "",
10887c478bd9Sstevel@tonic-gate 	    DESC_COLUMN_WIDTH, common_name_buf);
10897c478bd9Sstevel@tonic-gate 	if (*buf)
10907c478bd9Sstevel@tonic-gate 		free(*buf);
10917c478bd9Sstevel@tonic-gate 	*buf = newbuf;
10927c478bd9Sstevel@tonic-gate }
10937c478bd9Sstevel@tonic-gate 
10947c478bd9Sstevel@tonic-gate /* ARGSUSED */
10957c478bd9Sstevel@tonic-gate static void
10967c478bd9Sstevel@tonic-gate sortkey_desc(char *buf, int reverse, scf_walkinfo_t *wip)
10977c478bd9Sstevel@tonic-gate {
10987c478bd9Sstevel@tonic-gate 	bzero(buf, DESC_COLUMN_WIDTH);
10997c478bd9Sstevel@tonic-gate }
11007c478bd9Sstevel@tonic-gate 
11017c478bd9Sstevel@tonic-gate /* State columns (STATE, NSTATE, S, N, SN, STA, NSTA) */
11027c478bd9Sstevel@tonic-gate 
11037c478bd9Sstevel@tonic-gate static char
11047c478bd9Sstevel@tonic-gate state_to_char(const char *state)
11057c478bd9Sstevel@tonic-gate {
11067c478bd9Sstevel@tonic-gate 	if (strcmp(state, SCF_STATE_STRING_UNINIT) == 0)
11077c478bd9Sstevel@tonic-gate 		return ('u');
11087c478bd9Sstevel@tonic-gate 
11097c478bd9Sstevel@tonic-gate 	if (strcmp(state, SCF_STATE_STRING_OFFLINE) == 0)
11107c478bd9Sstevel@tonic-gate 		return ('0');
11117c478bd9Sstevel@tonic-gate 
11127c478bd9Sstevel@tonic-gate 	if (strcmp(state, SCF_STATE_STRING_ONLINE) == 0)
11137c478bd9Sstevel@tonic-gate 		return ('1');
11147c478bd9Sstevel@tonic-gate 
11157c478bd9Sstevel@tonic-gate 	if (strcmp(state, SCF_STATE_STRING_MAINT) == 0)
11167c478bd9Sstevel@tonic-gate 		return ('m');
11177c478bd9Sstevel@tonic-gate 
11187c478bd9Sstevel@tonic-gate 	if (strcmp(state, SCF_STATE_STRING_DISABLED) == 0)
11197c478bd9Sstevel@tonic-gate 		return ('d');
11207c478bd9Sstevel@tonic-gate 
11217c478bd9Sstevel@tonic-gate 	if (strcmp(state, SCF_STATE_STRING_DEGRADED) == 0)
11227c478bd9Sstevel@tonic-gate 		return ('D');
11237c478bd9Sstevel@tonic-gate 
11247c478bd9Sstevel@tonic-gate 	if (strcmp(state, SCF_STATE_STRING_LEGACY) == 0)
11257c478bd9Sstevel@tonic-gate 		return ('L');
11267c478bd9Sstevel@tonic-gate 
11277c478bd9Sstevel@tonic-gate 	return ('?');
11287c478bd9Sstevel@tonic-gate }
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate /* Return true if inst is transitioning. */
11317c478bd9Sstevel@tonic-gate static int
11327c478bd9Sstevel@tonic-gate transitioning(scf_instance_t *inst)
11337c478bd9Sstevel@tonic-gate {
11347c478bd9Sstevel@tonic-gate 	char nstate_name[MAX_SCF_STATE_STRING_SZ];
11357c478bd9Sstevel@tonic-gate 
11367c478bd9Sstevel@tonic-gate 	get_restarter_string_prop(inst, scf_property_next_state, nstate_name,
11377c478bd9Sstevel@tonic-gate 	    sizeof (nstate_name));
11387c478bd9Sstevel@tonic-gate 
11397c478bd9Sstevel@tonic-gate 	return (state_to_char(nstate_name) != '?');
11407c478bd9Sstevel@tonic-gate }
11417c478bd9Sstevel@tonic-gate 
11427c478bd9Sstevel@tonic-gate /* ARGSUSED */
11437c478bd9Sstevel@tonic-gate static void
11447c478bd9Sstevel@tonic-gate sortkey_states(const char *pname, char *buf, int reverse, scf_walkinfo_t *wip)
11457c478bd9Sstevel@tonic-gate {
11467c478bd9Sstevel@tonic-gate 	char state_name[MAX_SCF_STATE_STRING_SZ];
11477c478bd9Sstevel@tonic-gate 
11487c478bd9Sstevel@tonic-gate 	/*
11497c478bd9Sstevel@tonic-gate 	 * Lower numbers are printed first, so these are arranged from least
11507c478bd9Sstevel@tonic-gate 	 * interesting ("legacy run") to most interesting (unknown).
11517c478bd9Sstevel@tonic-gate 	 */
11527c478bd9Sstevel@tonic-gate 	if (wip->pg == NULL) {
11537c478bd9Sstevel@tonic-gate 		get_restarter_string_prop(wip->inst, pname, state_name,
11547c478bd9Sstevel@tonic-gate 		    sizeof (state_name));
11557c478bd9Sstevel@tonic-gate 
11567c478bd9Sstevel@tonic-gate 		if (strcmp(state_name, SCF_STATE_STRING_ONLINE) == 0)
11577c478bd9Sstevel@tonic-gate 			*buf = 2;
11587c478bd9Sstevel@tonic-gate 		else if (strcmp(state_name, SCF_STATE_STRING_DEGRADED) == 0)
11597c478bd9Sstevel@tonic-gate 			*buf = 3;
11607c478bd9Sstevel@tonic-gate 		else if (strcmp(state_name, SCF_STATE_STRING_OFFLINE) == 0)
11617c478bd9Sstevel@tonic-gate 			*buf = 4;
11627c478bd9Sstevel@tonic-gate 		else if (strcmp(state_name, SCF_STATE_STRING_MAINT) == 0)
11637c478bd9Sstevel@tonic-gate 			*buf = 5;
11647c478bd9Sstevel@tonic-gate 		else if (strcmp(state_name, SCF_STATE_STRING_DISABLED) == 0)
11657c478bd9Sstevel@tonic-gate 			*buf = 1;
11667c478bd9Sstevel@tonic-gate 		else if (strcmp(state_name, SCF_STATE_STRING_UNINIT) == 0)
11677c478bd9Sstevel@tonic-gate 			*buf = 6;
11687c478bd9Sstevel@tonic-gate 		else
11697c478bd9Sstevel@tonic-gate 			*buf = 7;
11707c478bd9Sstevel@tonic-gate 	} else
11717c478bd9Sstevel@tonic-gate 		*buf = 0;
11727c478bd9Sstevel@tonic-gate 
11737c478bd9Sstevel@tonic-gate 	if (reverse)
11747c478bd9Sstevel@tonic-gate 		*buf = 255 - *buf;
11757c478bd9Sstevel@tonic-gate }
11767c478bd9Sstevel@tonic-gate 
11777c478bd9Sstevel@tonic-gate static void
11787c478bd9Sstevel@tonic-gate sprint_state(char **buf, scf_walkinfo_t *wip)
11797c478bd9Sstevel@tonic-gate {
11807c478bd9Sstevel@tonic-gate 	char state_name[MAX_SCF_STATE_STRING_SZ + 1];
11817c478bd9Sstevel@tonic-gate 	size_t newsize;
11827c478bd9Sstevel@tonic-gate 	char *newbuf;
11837c478bd9Sstevel@tonic-gate 
11847c478bd9Sstevel@tonic-gate 	if (wip->pg == NULL) {
11857c478bd9Sstevel@tonic-gate 		get_restarter_string_prop(wip->inst, scf_property_state,
11867c478bd9Sstevel@tonic-gate 		    state_name, sizeof (state_name));
11877c478bd9Sstevel@tonic-gate 
11887c478bd9Sstevel@tonic-gate 		/* Don't print blank fields, to ease parsing. */
11897c478bd9Sstevel@tonic-gate 		if (state_name[0] == '\0') {
11907c478bd9Sstevel@tonic-gate 			state_name[0] = '-';
11917c478bd9Sstevel@tonic-gate 			state_name[1] = '\0';
11927c478bd9Sstevel@tonic-gate 		}
11937c478bd9Sstevel@tonic-gate 
11947c478bd9Sstevel@tonic-gate 		if (!opt_nstate_shown && transitioning(wip->inst)) {
11957c478bd9Sstevel@tonic-gate 			/* Append an asterisk if nstate is valid. */
11967c478bd9Sstevel@tonic-gate 			(void) strcat(state_name, "*");
11977c478bd9Sstevel@tonic-gate 		}
11987c478bd9Sstevel@tonic-gate 	} else
11997c478bd9Sstevel@tonic-gate 		(void) strcpy(state_name, SCF_STATE_STRING_LEGACY);
12007c478bd9Sstevel@tonic-gate 
12017c478bd9Sstevel@tonic-gate 	newsize = (*buf ? strlen(*buf) : 0) + MAX_SCF_STATE_STRING_SZ + 2;
12027c478bd9Sstevel@tonic-gate 	newbuf = safe_malloc(newsize);
12037c478bd9Sstevel@tonic-gate 	(void) snprintf(newbuf, newsize, "%s%-*s ", *buf ? *buf : "",
12047c478bd9Sstevel@tonic-gate 	    MAX_SCF_STATE_STRING_SZ + 1, state_name);
12057c478bd9Sstevel@tonic-gate 
12067c478bd9Sstevel@tonic-gate 	if (*buf)
12077c478bd9Sstevel@tonic-gate 		free(*buf);
12087c478bd9Sstevel@tonic-gate 	*buf = newbuf;
12097c478bd9Sstevel@tonic-gate }
12107c478bd9Sstevel@tonic-gate 
12117c478bd9Sstevel@tonic-gate static void
12127c478bd9Sstevel@tonic-gate sortkey_state(char *buf, int reverse, scf_walkinfo_t *wip)
12137c478bd9Sstevel@tonic-gate {
12147c478bd9Sstevel@tonic-gate 	sortkey_states(scf_property_state, buf, reverse, wip);
12157c478bd9Sstevel@tonic-gate }
12167c478bd9Sstevel@tonic-gate 
12177c478bd9Sstevel@tonic-gate static void
12187c478bd9Sstevel@tonic-gate sprint_nstate(char **buf, scf_walkinfo_t *wip)
12197c478bd9Sstevel@tonic-gate {
12207c478bd9Sstevel@tonic-gate 	char next_state_name[MAX_SCF_STATE_STRING_SZ];
12217c478bd9Sstevel@tonic-gate 	boolean_t blank = 0;
12227c478bd9Sstevel@tonic-gate 	size_t newsize;
12237c478bd9Sstevel@tonic-gate 	char *newbuf;
12247c478bd9Sstevel@tonic-gate 
12257c478bd9Sstevel@tonic-gate 	if (wip->pg == NULL) {
12267c478bd9Sstevel@tonic-gate 		get_restarter_string_prop(wip->inst, scf_property_next_state,
12277c478bd9Sstevel@tonic-gate 		    next_state_name, sizeof (next_state_name));
12287c478bd9Sstevel@tonic-gate 
12297c478bd9Sstevel@tonic-gate 		/* Don't print blank fields, to ease parsing. */
12307c478bd9Sstevel@tonic-gate 		if (next_state_name[0] == '\0' ||
12317c478bd9Sstevel@tonic-gate 		    strcmp(next_state_name, SCF_STATE_STRING_NONE) == 0)
12327c478bd9Sstevel@tonic-gate 			blank = 1;
12337c478bd9Sstevel@tonic-gate 	} else
12347c478bd9Sstevel@tonic-gate 		blank = 1;
12357c478bd9Sstevel@tonic-gate 
12367c478bd9Sstevel@tonic-gate 	if (blank) {
12377c478bd9Sstevel@tonic-gate 		next_state_name[0] = '-';
12387c478bd9Sstevel@tonic-gate 		next_state_name[1] = '\0';
12397c478bd9Sstevel@tonic-gate 	}
12407c478bd9Sstevel@tonic-gate 
12417c478bd9Sstevel@tonic-gate 	newsize = (*buf ? strlen(*buf) : 0) + MAX_SCF_STATE_STRING_SZ + 1;
12427c478bd9Sstevel@tonic-gate 	newbuf = safe_malloc(newsize);
12437c478bd9Sstevel@tonic-gate 	(void) snprintf(newbuf, newsize, "%s%-*s ", *buf ? *buf : "",
12447c478bd9Sstevel@tonic-gate 	    MAX_SCF_STATE_STRING_SZ - 1, next_state_name);
12457c478bd9Sstevel@tonic-gate 	if (*buf)
12467c478bd9Sstevel@tonic-gate 		free(*buf);
12477c478bd9Sstevel@tonic-gate 	*buf = newbuf;
12487c478bd9Sstevel@tonic-gate }
12497c478bd9Sstevel@tonic-gate 
12507c478bd9Sstevel@tonic-gate static void
12517c478bd9Sstevel@tonic-gate sortkey_nstate(char *buf, int reverse, scf_walkinfo_t *wip)
12527c478bd9Sstevel@tonic-gate {
12537c478bd9Sstevel@tonic-gate 	sortkey_states(scf_property_next_state, buf, reverse, wip);
12547c478bd9Sstevel@tonic-gate }
12557c478bd9Sstevel@tonic-gate 
12567c478bd9Sstevel@tonic-gate static void
12577c478bd9Sstevel@tonic-gate sprint_s(char **buf, scf_walkinfo_t *wip)
12587c478bd9Sstevel@tonic-gate {
12597c478bd9Sstevel@tonic-gate 	char tmp[3];
12607c478bd9Sstevel@tonic-gate 	char state_name[MAX_SCF_STATE_STRING_SZ];
12617c478bd9Sstevel@tonic-gate 	size_t newsize = (*buf ? strlen(*buf) : 0) + 4;
12627c478bd9Sstevel@tonic-gate 	char *newbuf = safe_malloc(newsize);
12637c478bd9Sstevel@tonic-gate 
12647c478bd9Sstevel@tonic-gate 	if (wip->pg == NULL) {
12657c478bd9Sstevel@tonic-gate 		get_restarter_string_prop(wip->inst, scf_property_state,
12667c478bd9Sstevel@tonic-gate 		    state_name, sizeof (state_name));
12677c478bd9Sstevel@tonic-gate 		tmp[0] = state_to_char(state_name);
12687c478bd9Sstevel@tonic-gate 
12697c478bd9Sstevel@tonic-gate 		if (!opt_nstate_shown && transitioning(wip->inst))
12707c478bd9Sstevel@tonic-gate 			tmp[1] = '*';
12717c478bd9Sstevel@tonic-gate 		else
12727c478bd9Sstevel@tonic-gate 			tmp[1] = ' ';
12737c478bd9Sstevel@tonic-gate 	} else {
12747c478bd9Sstevel@tonic-gate 		tmp[0] = 'L';
12757c478bd9Sstevel@tonic-gate 		tmp[1] = ' ';
12767c478bd9Sstevel@tonic-gate 	}
12777c478bd9Sstevel@tonic-gate 	tmp[2] = ' ';
12787c478bd9Sstevel@tonic-gate 	(void) snprintf(newbuf, newsize, "%s%-*s", *buf ? *buf : "",
12797c478bd9Sstevel@tonic-gate 	    3, tmp);
12807c478bd9Sstevel@tonic-gate 	if (*buf)
12817c478bd9Sstevel@tonic-gate 		free(*buf);
12827c478bd9Sstevel@tonic-gate 	*buf = newbuf;
12837c478bd9Sstevel@tonic-gate }
12847c478bd9Sstevel@tonic-gate 
12857c478bd9Sstevel@tonic-gate static void
12867c478bd9Sstevel@tonic-gate sprint_n(char **buf, scf_walkinfo_t *wip)
12877c478bd9Sstevel@tonic-gate {
12887c478bd9Sstevel@tonic-gate 	char tmp[2];
12897c478bd9Sstevel@tonic-gate 	size_t newsize = (*buf ? strlen(*buf) : 0) + 3;
12907c478bd9Sstevel@tonic-gate 	char *newbuf = safe_malloc(newsize);
12917c478bd9Sstevel@tonic-gate 	char nstate_name[MAX_SCF_STATE_STRING_SZ];
12927c478bd9Sstevel@tonic-gate 
12937c478bd9Sstevel@tonic-gate 	if (wip->pg == NULL) {
12947c478bd9Sstevel@tonic-gate 		get_restarter_string_prop(wip->inst, scf_property_next_state,
12957c478bd9Sstevel@tonic-gate 		    nstate_name, sizeof (nstate_name));
12967c478bd9Sstevel@tonic-gate 
12977c478bd9Sstevel@tonic-gate 		if (strcmp(nstate_name, SCF_STATE_STRING_NONE) == 0)
12987c478bd9Sstevel@tonic-gate 			tmp[0] = '-';
12997c478bd9Sstevel@tonic-gate 		else
13007c478bd9Sstevel@tonic-gate 			tmp[0] = state_to_char(nstate_name);
13017c478bd9Sstevel@tonic-gate 	} else
13027c478bd9Sstevel@tonic-gate 		tmp[0] = '-';
13037c478bd9Sstevel@tonic-gate 
13047c478bd9Sstevel@tonic-gate 	(void) snprintf(newbuf, newsize, "%s%-*s ", *buf ? *buf : "",
13057c478bd9Sstevel@tonic-gate 	    2, tmp);
13067c478bd9Sstevel@tonic-gate 	if (*buf)
13077c478bd9Sstevel@tonic-gate 		free(*buf);
13087c478bd9Sstevel@tonic-gate 	*buf = newbuf;
13097c478bd9Sstevel@tonic-gate }
13107c478bd9Sstevel@tonic-gate 
13117c478bd9Sstevel@tonic-gate static void
13127c478bd9Sstevel@tonic-gate sprint_sn(char **buf, scf_walkinfo_t *wip)
13137c478bd9Sstevel@tonic-gate {
13147c478bd9Sstevel@tonic-gate 	char tmp[3];
13157c478bd9Sstevel@tonic-gate 	size_t newsize = (*buf ? strlen(*buf) : 0) + 4;
13167c478bd9Sstevel@tonic-gate 	char *newbuf = safe_malloc(newsize);
13177c478bd9Sstevel@tonic-gate 	char nstate_name[MAX_SCF_STATE_STRING_SZ];
13187c478bd9Sstevel@tonic-gate 	char state_name[MAX_SCF_STATE_STRING_SZ];
13197c478bd9Sstevel@tonic-gate 
13207c478bd9Sstevel@tonic-gate 	if (wip->pg == NULL) {
13217c478bd9Sstevel@tonic-gate 		get_restarter_string_prop(wip->inst, scf_property_state,
13227c478bd9Sstevel@tonic-gate 		    state_name, sizeof (state_name));
13237c478bd9Sstevel@tonic-gate 		get_restarter_string_prop(wip->inst, scf_property_next_state,
13247c478bd9Sstevel@tonic-gate 		    nstate_name, sizeof (nstate_name));
13257c478bd9Sstevel@tonic-gate 		tmp[0] = state_to_char(state_name);
13267c478bd9Sstevel@tonic-gate 
13277c478bd9Sstevel@tonic-gate 		if (strcmp(nstate_name, SCF_STATE_STRING_NONE) == 0)
13287c478bd9Sstevel@tonic-gate 			tmp[1] = '-';
13297c478bd9Sstevel@tonic-gate 		else
13307c478bd9Sstevel@tonic-gate 			tmp[1] = state_to_char(nstate_name);
13317c478bd9Sstevel@tonic-gate 	} else {
13327c478bd9Sstevel@tonic-gate 		tmp[0] = 'L';
13337c478bd9Sstevel@tonic-gate 		tmp[1] = '-';
13347c478bd9Sstevel@tonic-gate 	}
13357c478bd9Sstevel@tonic-gate 
13367c478bd9Sstevel@tonic-gate 	tmp[2] = ' ';
13377c478bd9Sstevel@tonic-gate 	(void) snprintf(newbuf, newsize, "%s%-*s ", *buf ? *buf : "",
13387c478bd9Sstevel@tonic-gate 	    3, tmp);
13397c478bd9Sstevel@tonic-gate 	if (*buf)
13407c478bd9Sstevel@tonic-gate 		free(*buf);
13417c478bd9Sstevel@tonic-gate 	*buf = newbuf;
13427c478bd9Sstevel@tonic-gate }
13437c478bd9Sstevel@tonic-gate 
13447c478bd9Sstevel@tonic-gate /* ARGSUSED */
13457c478bd9Sstevel@tonic-gate static void
13467c478bd9Sstevel@tonic-gate sortkey_sn(char *buf, int reverse, scf_walkinfo_t *wip)
13477c478bd9Sstevel@tonic-gate {
13487c478bd9Sstevel@tonic-gate 	sortkey_state(buf, reverse, wip);
13497c478bd9Sstevel@tonic-gate 	sortkey_nstate(buf + 1, reverse, wip);
13507c478bd9Sstevel@tonic-gate }
13517c478bd9Sstevel@tonic-gate 
13527c478bd9Sstevel@tonic-gate static const char *
13537c478bd9Sstevel@tonic-gate state_abbrev(const char *state)
13547c478bd9Sstevel@tonic-gate {
13557c478bd9Sstevel@tonic-gate 	if (strcmp(state, SCF_STATE_STRING_UNINIT) == 0)
13567c478bd9Sstevel@tonic-gate 		return ("UN");
13577c478bd9Sstevel@tonic-gate 	if (strcmp(state, SCF_STATE_STRING_OFFLINE) == 0)
13587c478bd9Sstevel@tonic-gate 		return ("OFF");
13597c478bd9Sstevel@tonic-gate 	if (strcmp(state, SCF_STATE_STRING_ONLINE) == 0)
13607c478bd9Sstevel@tonic-gate 		return ("ON");
13617c478bd9Sstevel@tonic-gate 	if (strcmp(state, SCF_STATE_STRING_MAINT) == 0)
13627c478bd9Sstevel@tonic-gate 		return ("MNT");
13637c478bd9Sstevel@tonic-gate 	if (strcmp(state, SCF_STATE_STRING_DISABLED) == 0)
13647c478bd9Sstevel@tonic-gate 		return ("DIS");
13657c478bd9Sstevel@tonic-gate 	if (strcmp(state, SCF_STATE_STRING_DEGRADED) == 0)
13667c478bd9Sstevel@tonic-gate 		return ("DGD");
13677c478bd9Sstevel@tonic-gate 	if (strcmp(state, SCF_STATE_STRING_LEGACY) == 0)
13687c478bd9Sstevel@tonic-gate 		return ("LRC");
13697c478bd9Sstevel@tonic-gate 
13707c478bd9Sstevel@tonic-gate 	return ("?");
13717c478bd9Sstevel@tonic-gate }
13727c478bd9Sstevel@tonic-gate 
13737c478bd9Sstevel@tonic-gate static void
13747c478bd9Sstevel@tonic-gate sprint_sta(char **buf, scf_walkinfo_t *wip)
13757c478bd9Sstevel@tonic-gate {
13767c478bd9Sstevel@tonic-gate 	char state_name[MAX_SCF_STATE_STRING_SZ];
13777c478bd9Sstevel@tonic-gate 	char sta[5];
13787c478bd9Sstevel@tonic-gate 	size_t newsize = (*buf ? strlen(*buf) : 0) + 6;
13797c478bd9Sstevel@tonic-gate 	char *newbuf = safe_malloc(newsize);
13807c478bd9Sstevel@tonic-gate 
13817c478bd9Sstevel@tonic-gate 	if (wip->pg == NULL)
13827c478bd9Sstevel@tonic-gate 		get_restarter_string_prop(wip->inst, scf_property_state,
13837c478bd9Sstevel@tonic-gate 		    state_name, sizeof (state_name));
13847c478bd9Sstevel@tonic-gate 	else
13857c478bd9Sstevel@tonic-gate 		(void) strcpy(state_name, SCF_STATE_STRING_LEGACY);
13867c478bd9Sstevel@tonic-gate 
13877c478bd9Sstevel@tonic-gate 	(void) strcpy(sta, state_abbrev(state_name));
13887c478bd9Sstevel@tonic-gate 
13897c478bd9Sstevel@tonic-gate 	if (wip->pg == NULL && !opt_nstate_shown && transitioning(wip->inst))
13907c478bd9Sstevel@tonic-gate 		(void) strcat(sta, "*");
13917c478bd9Sstevel@tonic-gate 
13927c478bd9Sstevel@tonic-gate 	(void) snprintf(newbuf, newsize, "%s%-4s ", *buf ? *buf : "", sta);
13937c478bd9Sstevel@tonic-gate 	if (*buf)
13947c478bd9Sstevel@tonic-gate 		free(*buf);
13957c478bd9Sstevel@tonic-gate 	*buf = newbuf;
13967c478bd9Sstevel@tonic-gate }
13977c478bd9Sstevel@tonic-gate 
13987c478bd9Sstevel@tonic-gate static void
13997c478bd9Sstevel@tonic-gate sprint_nsta(char **buf, scf_walkinfo_t *wip)
14007c478bd9Sstevel@tonic-gate {
14017c478bd9Sstevel@tonic-gate 	char state_name[MAX_SCF_STATE_STRING_SZ];
14027c478bd9Sstevel@tonic-gate 	size_t newsize = (*buf ? strlen(*buf) : 0) + 6;
14037c478bd9Sstevel@tonic-gate 	char *newbuf = safe_malloc(newsize);
14047c478bd9Sstevel@tonic-gate 
14057c478bd9Sstevel@tonic-gate 	if (wip->pg == NULL)
14067c478bd9Sstevel@tonic-gate 		get_restarter_string_prop(wip->inst, scf_property_next_state,
14077c478bd9Sstevel@tonic-gate 		    state_name, sizeof (state_name));
14087c478bd9Sstevel@tonic-gate 	else
14097c478bd9Sstevel@tonic-gate 		(void) strcpy(state_name, SCF_STATE_STRING_NONE);
14107c478bd9Sstevel@tonic-gate 
14117c478bd9Sstevel@tonic-gate 	if (strcmp(state_name, SCF_STATE_STRING_NONE) == 0)
14127c478bd9Sstevel@tonic-gate 		(void) snprintf(newbuf, newsize, "%s%-4s ", *buf ? *buf : "",
14137c478bd9Sstevel@tonic-gate 		    "-");
14147c478bd9Sstevel@tonic-gate 	else
14157c478bd9Sstevel@tonic-gate 		(void) snprintf(newbuf, newsize, "%s%-4s ", *buf ? *buf : "",
14167c478bd9Sstevel@tonic-gate 		    state_abbrev(state_name));
14177c478bd9Sstevel@tonic-gate 	if (*buf)
14187c478bd9Sstevel@tonic-gate 		free(*buf);
14197c478bd9Sstevel@tonic-gate 	*buf = newbuf;
14207c478bd9Sstevel@tonic-gate }
14217c478bd9Sstevel@tonic-gate 
14227c478bd9Sstevel@tonic-gate /* FMRI */
14237c478bd9Sstevel@tonic-gate #define	FMRI_COLUMN_WIDTH	50
14247c478bd9Sstevel@tonic-gate static void
14257c478bd9Sstevel@tonic-gate sprint_fmri(char **buf, scf_walkinfo_t *wip)
14267c478bd9Sstevel@tonic-gate {
14277c478bd9Sstevel@tonic-gate 	char *fmri_buf = safe_malloc(max_scf_fmri_length + 1);
14287c478bd9Sstevel@tonic-gate 	size_t newsize;
14297c478bd9Sstevel@tonic-gate 	char *newbuf;
14307c478bd9Sstevel@tonic-gate 
14317c478bd9Sstevel@tonic-gate 	if (wip->pg == NULL) {
14327c478bd9Sstevel@tonic-gate 		if (scf_instance_to_fmri(wip->inst, fmri_buf,
14337c478bd9Sstevel@tonic-gate 		    max_scf_fmri_length + 1) == -1)
14347c478bd9Sstevel@tonic-gate 			scfdie();
14357c478bd9Sstevel@tonic-gate 	} else {
14367b209c2cSacruz 		(void) strcpy(fmri_buf, SCF_FMRI_LEGACY_PREFIX);
14377c478bd9Sstevel@tonic-gate 		if (pg_get_single_val(wip->pg, SCF_LEGACY_PROPERTY_NAME,
14387b209c2cSacruz 		    SCF_TYPE_ASTRING, fmri_buf +
14397b209c2cSacruz 		    sizeof (SCF_FMRI_LEGACY_PREFIX) - 1,
14407b209c2cSacruz 		    max_scf_fmri_length + 1 -
14417b209c2cSacruz 		    (sizeof (SCF_FMRI_LEGACY_PREFIX) - 1), 0) != 0)
14427c478bd9Sstevel@tonic-gate 			(void) strcat(fmri_buf, LEGACY_UNKNOWN);
14437c478bd9Sstevel@tonic-gate 	}
14447c478bd9Sstevel@tonic-gate 
14457c478bd9Sstevel@tonic-gate 	if (strlen(fmri_buf) > FMRI_COLUMN_WIDTH)
14467c478bd9Sstevel@tonic-gate 		newsize = (*buf ? strlen(*buf) : 0) + strlen(fmri_buf) + 2;
14477c478bd9Sstevel@tonic-gate 	else
14487c478bd9Sstevel@tonic-gate 		newsize = (*buf ? strlen(*buf) : 0) + FMRI_COLUMN_WIDTH + 2;
14497c478bd9Sstevel@tonic-gate 	newbuf = safe_malloc(newsize);
14507c478bd9Sstevel@tonic-gate 	(void) snprintf(newbuf, newsize, "%s%-*s ", *buf ? *buf : "",
14517c478bd9Sstevel@tonic-gate 	    FMRI_COLUMN_WIDTH, fmri_buf);
14527c478bd9Sstevel@tonic-gate 	free(fmri_buf);
14537c478bd9Sstevel@tonic-gate 	if (*buf)
14547c478bd9Sstevel@tonic-gate 		free(*buf);
14557c478bd9Sstevel@tonic-gate 	*buf = newbuf;
14567c478bd9Sstevel@tonic-gate }
14577c478bd9Sstevel@tonic-gate 
14587c478bd9Sstevel@tonic-gate static void
14597c478bd9Sstevel@tonic-gate sortkey_fmri(char *buf, int reverse, scf_walkinfo_t *wip)
14607c478bd9Sstevel@tonic-gate {
14617c478bd9Sstevel@tonic-gate 	char *tmp = NULL;
14627c478bd9Sstevel@tonic-gate 
14637c478bd9Sstevel@tonic-gate 	sprint_fmri(&tmp, wip);
14647c478bd9Sstevel@tonic-gate 	bcopy(tmp, buf, FMRI_COLUMN_WIDTH);
14657c478bd9Sstevel@tonic-gate 	free(tmp);
14667c478bd9Sstevel@tonic-gate 	if (reverse)
14677c478bd9Sstevel@tonic-gate 		reverse_bytes(buf, FMRI_COLUMN_WIDTH);
14687c478bd9Sstevel@tonic-gate }
14697c478bd9Sstevel@tonic-gate 
14707c478bd9Sstevel@tonic-gate /* Component columns */
14717c478bd9Sstevel@tonic-gate #define	COMPONENT_COLUMN_WIDTH	20
14727c478bd9Sstevel@tonic-gate static void
14737c478bd9Sstevel@tonic-gate sprint_scope(char **buf, scf_walkinfo_t *wip)
14747c478bd9Sstevel@tonic-gate {
14757c478bd9Sstevel@tonic-gate 	char *scope_buf = safe_malloc(max_scf_name_length + 1);
14767c478bd9Sstevel@tonic-gate 	size_t newsize = (*buf ? strlen(*buf) : 0) + COMPONENT_COLUMN_WIDTH + 2;
14777c478bd9Sstevel@tonic-gate 	char *newbuf = safe_malloc(newsize);
14787c478bd9Sstevel@tonic-gate 
14797c478bd9Sstevel@tonic-gate 	assert(wip->scope != NULL);
14807c478bd9Sstevel@tonic-gate 
14817c478bd9Sstevel@tonic-gate 	if (scf_scope_get_name(wip->scope, scope_buf, max_scf_name_length) < 0)
14827c478bd9Sstevel@tonic-gate 		scfdie();
14837c478bd9Sstevel@tonic-gate 
14847c478bd9Sstevel@tonic-gate 	(void) snprintf(newbuf, newsize, "%s%-*s ", *buf ? *buf : "",
14857c478bd9Sstevel@tonic-gate 	    COMPONENT_COLUMN_WIDTH, scope_buf);
14867c478bd9Sstevel@tonic-gate 	if (*buf)
14877c478bd9Sstevel@tonic-gate 		free(*buf);
14887c478bd9Sstevel@tonic-gate 	*buf = newbuf;
14897c478bd9Sstevel@tonic-gate 	free(scope_buf);
14907c478bd9Sstevel@tonic-gate }
14917c478bd9Sstevel@tonic-gate 
14927c478bd9Sstevel@tonic-gate static void
14937c478bd9Sstevel@tonic-gate sortkey_scope(char *buf, int reverse, scf_walkinfo_t *wip)
14947c478bd9Sstevel@tonic-gate {
14957c478bd9Sstevel@tonic-gate 	char *tmp = NULL;
14967c478bd9Sstevel@tonic-gate 
14977c478bd9Sstevel@tonic-gate 	sprint_scope(&tmp, wip);
14987c478bd9Sstevel@tonic-gate 	bcopy(tmp, buf, COMPONENT_COLUMN_WIDTH);
14997c478bd9Sstevel@tonic-gate 	free(tmp);
15007c478bd9Sstevel@tonic-gate 	if (reverse)
15017c478bd9Sstevel@tonic-gate 		reverse_bytes(buf, COMPONENT_COLUMN_WIDTH);
15027c478bd9Sstevel@tonic-gate }
15037c478bd9Sstevel@tonic-gate 
15047c478bd9Sstevel@tonic-gate static void
15057c478bd9Sstevel@tonic-gate sprint_service(char **buf, scf_walkinfo_t *wip)
15067c478bd9Sstevel@tonic-gate {
15077c478bd9Sstevel@tonic-gate 	char *svc_buf = safe_malloc(max_scf_name_length + 1);
15087c478bd9Sstevel@tonic-gate 	char *newbuf;
15097c478bd9Sstevel@tonic-gate 	size_t newsize;
15107c478bd9Sstevel@tonic-gate 
15117c478bd9Sstevel@tonic-gate 	if (wip->pg == NULL) {
15127c478bd9Sstevel@tonic-gate 		if (scf_service_get_name(wip->svc, svc_buf,
15137c478bd9Sstevel@tonic-gate 		    max_scf_name_length + 1) < 0)
15147c478bd9Sstevel@tonic-gate 			scfdie();
15157c478bd9Sstevel@tonic-gate 	} else {
15167c478bd9Sstevel@tonic-gate 		if (pg_get_single_val(wip->pg, "name", SCF_TYPE_ASTRING,
15177c478bd9Sstevel@tonic-gate 		    svc_buf, max_scf_name_length + 1, EMPTY_OK) != 0)
15187c478bd9Sstevel@tonic-gate 			(void) strcpy(svc_buf, LEGACY_UNKNOWN);
15197c478bd9Sstevel@tonic-gate 	}
15207c478bd9Sstevel@tonic-gate 
15217c478bd9Sstevel@tonic-gate 
15227c478bd9Sstevel@tonic-gate 	if (strlen(svc_buf) > COMPONENT_COLUMN_WIDTH)
15237c478bd9Sstevel@tonic-gate 		newsize = (*buf ? strlen(*buf) : 0) + strlen(svc_buf) + 2;
15247c478bd9Sstevel@tonic-gate 	else
15257c478bd9Sstevel@tonic-gate 		newsize = (*buf ? strlen(*buf) : 0) +
15267c478bd9Sstevel@tonic-gate 		    COMPONENT_COLUMN_WIDTH + 2;
15277c478bd9Sstevel@tonic-gate 	newbuf = safe_malloc(newsize);
15287c478bd9Sstevel@tonic-gate 	(void) snprintf(newbuf, newsize, "%s%-*s ", *buf ? *buf : "",
15297c478bd9Sstevel@tonic-gate 	    COMPONENT_COLUMN_WIDTH, svc_buf);
15307c478bd9Sstevel@tonic-gate 	free(svc_buf);
15317c478bd9Sstevel@tonic-gate 	if (*buf)
15327c478bd9Sstevel@tonic-gate 		free(*buf);
15337c478bd9Sstevel@tonic-gate 	*buf = newbuf;
15347c478bd9Sstevel@tonic-gate }
15357c478bd9Sstevel@tonic-gate 
15367c478bd9Sstevel@tonic-gate static void
15377c478bd9Sstevel@tonic-gate sortkey_service(char *buf, int reverse, scf_walkinfo_t *wip)
15387c478bd9Sstevel@tonic-gate {
15397c478bd9Sstevel@tonic-gate 	char *tmp = NULL;
15407c478bd9Sstevel@tonic-gate 
15417c478bd9Sstevel@tonic-gate 	sprint_service(&tmp, wip);
15427c478bd9Sstevel@tonic-gate 	bcopy(tmp, buf, COMPONENT_COLUMN_WIDTH);
15437c478bd9Sstevel@tonic-gate 	free(tmp);
15447c478bd9Sstevel@tonic-gate 	if (reverse)
15457c478bd9Sstevel@tonic-gate 		reverse_bytes(buf, COMPONENT_COLUMN_WIDTH);
15467c478bd9Sstevel@tonic-gate }
15477c478bd9Sstevel@tonic-gate 
15487c478bd9Sstevel@tonic-gate /* INST */
15497c478bd9Sstevel@tonic-gate static void
15507c478bd9Sstevel@tonic-gate sprint_instance(char **buf, scf_walkinfo_t *wip)
15517c478bd9Sstevel@tonic-gate {
15527c478bd9Sstevel@tonic-gate 	char *tmp = safe_malloc(max_scf_name_length + 1);
15537c478bd9Sstevel@tonic-gate 	size_t newsize = (*buf ? strlen(*buf) : 0) + COMPONENT_COLUMN_WIDTH + 2;
15547c478bd9Sstevel@tonic-gate 	char *newbuf = safe_malloc(newsize);
15557c478bd9Sstevel@tonic-gate 
15567c478bd9Sstevel@tonic-gate 	if (wip->pg == NULL) {
15577c478bd9Sstevel@tonic-gate 		if (scf_instance_get_name(wip->inst, tmp,
15587c478bd9Sstevel@tonic-gate 		    max_scf_name_length + 1) < 0)
15597c478bd9Sstevel@tonic-gate 			scfdie();
15607c478bd9Sstevel@tonic-gate 	} else {
15617c478bd9Sstevel@tonic-gate 		tmp[0] = '-';
15627c478bd9Sstevel@tonic-gate 		tmp[1] = '\0';
15637c478bd9Sstevel@tonic-gate 	}
15647c478bd9Sstevel@tonic-gate 
15657c478bd9Sstevel@tonic-gate 	(void) snprintf(newbuf, newsize, "%s%-*s ", *buf ? *buf : "",
15667c478bd9Sstevel@tonic-gate 	    COMPONENT_COLUMN_WIDTH, tmp);
15677c478bd9Sstevel@tonic-gate 	if (*buf)
15687c478bd9Sstevel@tonic-gate 		free(*buf);
15697c478bd9Sstevel@tonic-gate 	*buf = newbuf;
15707c478bd9Sstevel@tonic-gate 	free(tmp);
15717c478bd9Sstevel@tonic-gate }
15727c478bd9Sstevel@tonic-gate 
15737c478bd9Sstevel@tonic-gate static void
15747c478bd9Sstevel@tonic-gate sortkey_instance(char *buf, int reverse, scf_walkinfo_t *wip)
15757c478bd9Sstevel@tonic-gate {
15767c478bd9Sstevel@tonic-gate 	char *tmp = NULL;
15777c478bd9Sstevel@tonic-gate 
15787c478bd9Sstevel@tonic-gate 	sprint_instance(&tmp, wip);
15797c478bd9Sstevel@tonic-gate 	bcopy(tmp, buf, COMPONENT_COLUMN_WIDTH);
15807c478bd9Sstevel@tonic-gate 	free(tmp);
15817c478bd9Sstevel@tonic-gate 	if (reverse)
15827c478bd9Sstevel@tonic-gate 		reverse_bytes(buf, COMPONENT_COLUMN_WIDTH);
15837c478bd9Sstevel@tonic-gate }
15847c478bd9Sstevel@tonic-gate 
15857c478bd9Sstevel@tonic-gate /* STIME */
15867c478bd9Sstevel@tonic-gate #define	STIME_COLUMN_WIDTH		8
15877c478bd9Sstevel@tonic-gate #define	FORMAT_TIME			"%k:%M:%S"
15887c478bd9Sstevel@tonic-gate #define	FORMAT_DATE			"%b_%d  "
15897c478bd9Sstevel@tonic-gate #define	FORMAT_YEAR			"%Y    "
15907c478bd9Sstevel@tonic-gate 
159161402014Slianep /*
159261402014Slianep  * sprint_stime() will allocate a new buffer and snprintf the services's
159361402014Slianep  * state timestamp.  If the timestamp is unavailable for some reason
159461402014Slianep  * a '-' is given instead.
159561402014Slianep  */
15967c478bd9Sstevel@tonic-gate static void
15977c478bd9Sstevel@tonic-gate sprint_stime(char **buf, scf_walkinfo_t *wip)
15987c478bd9Sstevel@tonic-gate {
15997c478bd9Sstevel@tonic-gate 	int r;
16007c478bd9Sstevel@tonic-gate 	struct timeval tv;
16017c478bd9Sstevel@tonic-gate 	time_t then;
16027c478bd9Sstevel@tonic-gate 	struct tm *tm;
16037c478bd9Sstevel@tonic-gate 	char st_buf[STIME_COLUMN_WIDTH + 1];
16047c478bd9Sstevel@tonic-gate 	size_t newsize = (*buf ? strlen(*buf) : 0) + STIME_COLUMN_WIDTH + 2;
16057c478bd9Sstevel@tonic-gate 	char *newbuf = safe_malloc(newsize);
16067c478bd9Sstevel@tonic-gate 
16077c478bd9Sstevel@tonic-gate 	if (wip->pg == NULL) {
16087c478bd9Sstevel@tonic-gate 		r = get_restarter_time_prop(wip->inst,
16097c478bd9Sstevel@tonic-gate 		    SCF_PROPERTY_STATE_TIMESTAMP, &tv, 0);
16107c478bd9Sstevel@tonic-gate 	} else {
16117c478bd9Sstevel@tonic-gate 		r = pg_get_single_val(wip->pg, SCF_PROPERTY_STATE_TIMESTAMP,
16127c478bd9Sstevel@tonic-gate 		    SCF_TYPE_TIME, &tv, NULL, 0);
16137c478bd9Sstevel@tonic-gate 	}
16147c478bd9Sstevel@tonic-gate 
16157c478bd9Sstevel@tonic-gate 	if (r != 0) {
161661402014Slianep 		/*
161761402014Slianep 		 * There's something amiss with our service
161861402014Slianep 		 * so we'll print a '-' for STIME.
161961402014Slianep 		 */
16207c478bd9Sstevel@tonic-gate 		(void) snprintf(newbuf, newsize, "%s%-*s", *buf ? *buf : "",
162161402014Slianep 		    STIME_COLUMN_WIDTH + 1, "-");
162261402014Slianep 	} else {
162361402014Slianep 		/* tv should be valid so we'll format it */
16247c478bd9Sstevel@tonic-gate 		then = (time_t)tv.tv_sec;
16257c478bd9Sstevel@tonic-gate 
16267c478bd9Sstevel@tonic-gate 		tm = localtime(&then);
16277c478bd9Sstevel@tonic-gate 		/*
16287c478bd9Sstevel@tonic-gate 		 * Print time if started within the past 24 hours, print date
162961402014Slianep 		 * if within the past 12 months or, finally, print year if
163061402014Slianep 		 * started greater than 12 months ago.
16317c478bd9Sstevel@tonic-gate 		 */
163261402014Slianep 		if (now - then < 24 * 60 * 60) {
163361402014Slianep 			(void) strftime(st_buf, sizeof (st_buf),
163461402014Slianep 			    gettext(FORMAT_TIME), tm);
163561402014Slianep 		} else if (now - then < 12 * 30 * 24 * 60 * 60) {
163661402014Slianep 			(void) strftime(st_buf, sizeof (st_buf),
163761402014Slianep 			    gettext(FORMAT_DATE), tm);
163861402014Slianep 		} else {
163961402014Slianep 			(void) strftime(st_buf, sizeof (st_buf),
164061402014Slianep 			    gettext(FORMAT_YEAR), tm);
164161402014Slianep 		}
16427c478bd9Sstevel@tonic-gate 		(void) snprintf(newbuf, newsize, "%s%-*s ", *buf ? *buf : "",
16437c478bd9Sstevel@tonic-gate 		    STIME_COLUMN_WIDTH + 1, st_buf);
164461402014Slianep 	}
16457c478bd9Sstevel@tonic-gate 	if (*buf)
16467c478bd9Sstevel@tonic-gate 		free(*buf);
16477c478bd9Sstevel@tonic-gate 	*buf = newbuf;
16487c478bd9Sstevel@tonic-gate }
16497c478bd9Sstevel@tonic-gate 
16507c478bd9Sstevel@tonic-gate #define	STIME_SORTKEY_WIDTH		(sizeof (uint64_t) + sizeof (uint32_t))
16517c478bd9Sstevel@tonic-gate 
16527c478bd9Sstevel@tonic-gate /* ARGSUSED */
16537c478bd9Sstevel@tonic-gate static void
16547c478bd9Sstevel@tonic-gate sortkey_stime(char *buf, int reverse, scf_walkinfo_t *wip)
16557c478bd9Sstevel@tonic-gate {
16567c478bd9Sstevel@tonic-gate 	struct timeval tv;
16577c478bd9Sstevel@tonic-gate 	int r;
16587c478bd9Sstevel@tonic-gate 
16597c478bd9Sstevel@tonic-gate 	if (wip->pg == NULL)
16607c478bd9Sstevel@tonic-gate 		r = get_restarter_time_prop(wip->inst,
16617c478bd9Sstevel@tonic-gate 		    SCF_PROPERTY_STATE_TIMESTAMP, &tv, 0);
16627c478bd9Sstevel@tonic-gate 	else
16637c478bd9Sstevel@tonic-gate 		r = pg_get_single_val(wip->pg, SCF_PROPERTY_STATE_TIMESTAMP,
16647c478bd9Sstevel@tonic-gate 		    SCF_TYPE_TIME, &tv, NULL, 0);
16657c478bd9Sstevel@tonic-gate 
16667c478bd9Sstevel@tonic-gate 	if (r == 0) {
16677c478bd9Sstevel@tonic-gate 		int64_t sec;
16687c478bd9Sstevel@tonic-gate 		int32_t us;
16697c478bd9Sstevel@tonic-gate 
16707c478bd9Sstevel@tonic-gate 		/* Stick it straight into the buffer. */
16717c478bd9Sstevel@tonic-gate 		sec = tv.tv_sec;
16727c478bd9Sstevel@tonic-gate 		us = tv.tv_usec;
16737c478bd9Sstevel@tonic-gate 
16747c478bd9Sstevel@tonic-gate 		sec = BE_64(sec);
16757c478bd9Sstevel@tonic-gate 		us = BE_32(us);
16767c478bd9Sstevel@tonic-gate 		bcopy(&sec, buf, sizeof (sec));
16777c478bd9Sstevel@tonic-gate 		bcopy(&us, buf + sizeof (sec), sizeof (us));
16787c478bd9Sstevel@tonic-gate 	} else {
16797c478bd9Sstevel@tonic-gate 		bzero(buf, STIME_SORTKEY_WIDTH);
16807c478bd9Sstevel@tonic-gate 	}
16817c478bd9Sstevel@tonic-gate 
16827c478bd9Sstevel@tonic-gate 	if (reverse)
16837c478bd9Sstevel@tonic-gate 		reverse_bytes(buf, STIME_SORTKEY_WIDTH);
16847c478bd9Sstevel@tonic-gate }
16857c478bd9Sstevel@tonic-gate 
16867c478bd9Sstevel@tonic-gate 
16877c478bd9Sstevel@tonic-gate /*
16887c478bd9Sstevel@tonic-gate  * Information about columns which can be displayed.  If you add something,
16897c478bd9Sstevel@tonic-gate  * check MAX_COLUMN_NAME_LENGTH_STR & update description_of_column() below.
16907c478bd9Sstevel@tonic-gate  */
16917c478bd9Sstevel@tonic-gate static const struct column columns[] = {
16927c478bd9Sstevel@tonic-gate 	{ "CTID", CTID_COLUMN_WIDTH, sprint_ctid,
16937c478bd9Sstevel@tonic-gate 		CTID_SORTKEY_WIDTH, sortkey_ctid },
16947c478bd9Sstevel@tonic-gate 	{ "DESC", DESC_COLUMN_WIDTH, sprint_desc,
16957c478bd9Sstevel@tonic-gate 		DESC_COLUMN_WIDTH, sortkey_desc },
16967c478bd9Sstevel@tonic-gate 	{ "FMRI", FMRI_COLUMN_WIDTH, sprint_fmri,
16977c478bd9Sstevel@tonic-gate 		FMRI_COLUMN_WIDTH, sortkey_fmri },
16987c478bd9Sstevel@tonic-gate 	{ "INST", COMPONENT_COLUMN_WIDTH, sprint_instance,
16997c478bd9Sstevel@tonic-gate 		COMPONENT_COLUMN_WIDTH, sortkey_instance },
17007c478bd9Sstevel@tonic-gate 	{ "N", 1,  sprint_n, 1, sortkey_nstate },
17017c478bd9Sstevel@tonic-gate 	{ "NSTA", 4, sprint_nsta, 1, sortkey_nstate },
17027c478bd9Sstevel@tonic-gate 	{ "NSTATE", MAX_SCF_STATE_STRING_SZ - 1, sprint_nstate,
17037c478bd9Sstevel@tonic-gate 		1, sortkey_nstate },
17047c478bd9Sstevel@tonic-gate 	{ "S", 2, sprint_s, 1, sortkey_state },
17057c478bd9Sstevel@tonic-gate 	{ "SCOPE", COMPONENT_COLUMN_WIDTH, sprint_scope,
17067c478bd9Sstevel@tonic-gate 		COMPONENT_COLUMN_WIDTH, sortkey_scope },
17077c478bd9Sstevel@tonic-gate 	{ "SN", 2, sprint_sn, 2, sortkey_sn },
17087c478bd9Sstevel@tonic-gate 	{ "SVC", COMPONENT_COLUMN_WIDTH, sprint_service,
17097c478bd9Sstevel@tonic-gate 		COMPONENT_COLUMN_WIDTH, sortkey_service },
17107c478bd9Sstevel@tonic-gate 	{ "STA", 4, sprint_sta, 1, sortkey_state },
17117c478bd9Sstevel@tonic-gate 	{ "STATE", MAX_SCF_STATE_STRING_SZ - 1 + 1, sprint_state,
17127c478bd9Sstevel@tonic-gate 		1, sortkey_state },
17137c478bd9Sstevel@tonic-gate 	{ "STIME", STIME_COLUMN_WIDTH, sprint_stime,
17147c478bd9Sstevel@tonic-gate 		STIME_SORTKEY_WIDTH, sortkey_stime },
17157c478bd9Sstevel@tonic-gate };
17167c478bd9Sstevel@tonic-gate 
17177c478bd9Sstevel@tonic-gate #define	MAX_COLUMN_NAME_LENGTH_STR	"6"
17187c478bd9Sstevel@tonic-gate 
17197c478bd9Sstevel@tonic-gate static const int ncolumns = sizeof (columns) / sizeof (columns[0]);
17207c478bd9Sstevel@tonic-gate 
17217c478bd9Sstevel@tonic-gate /*
17227c478bd9Sstevel@tonic-gate  * Necessary thanks to gettext() & xgettext.
17237c478bd9Sstevel@tonic-gate  */
17247c478bd9Sstevel@tonic-gate static const char *
17257c478bd9Sstevel@tonic-gate description_of_column(int c)
17267c478bd9Sstevel@tonic-gate {
17277c478bd9Sstevel@tonic-gate 	const char *s = NULL;
17287c478bd9Sstevel@tonic-gate 
17297c478bd9Sstevel@tonic-gate 	switch (c) {
17307c478bd9Sstevel@tonic-gate 	case 0:
17317c478bd9Sstevel@tonic-gate 		s = gettext("contract ID for service (see contract(4))");
17327c478bd9Sstevel@tonic-gate 		break;
17337c478bd9Sstevel@tonic-gate 	case 1:
17347c478bd9Sstevel@tonic-gate 		s = gettext("human-readable description of the service");
17357c478bd9Sstevel@tonic-gate 		break;
17367c478bd9Sstevel@tonic-gate 	case 2:
17377c478bd9Sstevel@tonic-gate 		s = gettext("Fault Managed Resource Identifier for service");
17387c478bd9Sstevel@tonic-gate 		break;
17397c478bd9Sstevel@tonic-gate 	case 3:
17407c478bd9Sstevel@tonic-gate 		s = gettext("portion of the FMRI indicating service instance");
17417c478bd9Sstevel@tonic-gate 		break;
17427c478bd9Sstevel@tonic-gate 	case 4:
17437c478bd9Sstevel@tonic-gate 		s = gettext("abbreviation for next state (if in transition)");
17447c478bd9Sstevel@tonic-gate 		break;
17457c478bd9Sstevel@tonic-gate 	case 5:
17467c478bd9Sstevel@tonic-gate 		s = gettext("abbreviation for next state (if in transition)");
17477c478bd9Sstevel@tonic-gate 		break;
17487c478bd9Sstevel@tonic-gate 	case 6:
17497c478bd9Sstevel@tonic-gate 		s = gettext("name for next state (if in transition)");
17507c478bd9Sstevel@tonic-gate 		break;
17517c478bd9Sstevel@tonic-gate 	case 7:
17527c478bd9Sstevel@tonic-gate 		s = gettext("abbreviation for current state");
17537c478bd9Sstevel@tonic-gate 		break;
17547c478bd9Sstevel@tonic-gate 	case 8:
17557c478bd9Sstevel@tonic-gate 		s = gettext("name for scope associated with service");
17567c478bd9Sstevel@tonic-gate 		break;
17577c478bd9Sstevel@tonic-gate 	case 9:
17587c478bd9Sstevel@tonic-gate 		s = gettext("abbreviation for current state and next state");
17597c478bd9Sstevel@tonic-gate 		break;
17607c478bd9Sstevel@tonic-gate 	case 10:
17617c478bd9Sstevel@tonic-gate 		s = gettext("portion of the FMRI representing service name");
17627c478bd9Sstevel@tonic-gate 		break;
17637c478bd9Sstevel@tonic-gate 	case 11:
17647c478bd9Sstevel@tonic-gate 		s = gettext("abbreviation for current state");
17657c478bd9Sstevel@tonic-gate 		break;
17667c478bd9Sstevel@tonic-gate 	case 12:
17677c478bd9Sstevel@tonic-gate 		s = gettext("name for current state");
17687c478bd9Sstevel@tonic-gate 		break;
17697c478bd9Sstevel@tonic-gate 	case 13:
17707c478bd9Sstevel@tonic-gate 		s = gettext("time of last state change");
17717c478bd9Sstevel@tonic-gate 		break;
17727c478bd9Sstevel@tonic-gate 	}
17737c478bd9Sstevel@tonic-gate 
17747c478bd9Sstevel@tonic-gate 	assert(s != NULL);
17757c478bd9Sstevel@tonic-gate 	return (s);
17767c478bd9Sstevel@tonic-gate }
17777c478bd9Sstevel@tonic-gate 
17787c478bd9Sstevel@tonic-gate 
17797c478bd9Sstevel@tonic-gate static void
17807c478bd9Sstevel@tonic-gate print_usage(const char *progname, FILE *f, boolean_t do_exit)
17817c478bd9Sstevel@tonic-gate {
17827c478bd9Sstevel@tonic-gate 	(void) fprintf(f, gettext(
17837c478bd9Sstevel@tonic-gate 	    "Usage: %1$s [-aHpv] [-o col[,col ... ]] [-R restarter] "
17847c478bd9Sstevel@tonic-gate 	    "[-sS col] [<service> ...]\n"
17857c478bd9Sstevel@tonic-gate 	    "       %1$s -d | -D [-Hpv] [-o col[,col ... ]] [-sS col] "
17867c478bd9Sstevel@tonic-gate 	    "[<service> ...]\n"
17877c478bd9Sstevel@tonic-gate 	    "       %1$s -l <service> ...\n"
17887c478bd9Sstevel@tonic-gate 	    "       %1$s -x [-v] [<service> ...]\n"
17897c478bd9Sstevel@tonic-gate 	    "       %1$s -?\n"), progname);
17907c478bd9Sstevel@tonic-gate 
17917c478bd9Sstevel@tonic-gate 	if (do_exit)
17927c478bd9Sstevel@tonic-gate 		exit(UU_EXIT_USAGE);
17937c478bd9Sstevel@tonic-gate }
17947c478bd9Sstevel@tonic-gate 
17957c478bd9Sstevel@tonic-gate #define	argserr(progname)	print_usage(progname, stderr, B_TRUE)
17967c478bd9Sstevel@tonic-gate 
17977c478bd9Sstevel@tonic-gate static void
17987c478bd9Sstevel@tonic-gate print_help(const char *progname)
17997c478bd9Sstevel@tonic-gate {
18007c478bd9Sstevel@tonic-gate 	int i;
18017c478bd9Sstevel@tonic-gate 
18027c478bd9Sstevel@tonic-gate 	print_usage(progname, stdout, B_FALSE);
18037c478bd9Sstevel@tonic-gate 
18047c478bd9Sstevel@tonic-gate 	(void) printf(gettext("\n"
18057c478bd9Sstevel@tonic-gate 	"\t-a  list all service instances rather than "
18067c478bd9Sstevel@tonic-gate 	"only those that are enabled\n"
18077c478bd9Sstevel@tonic-gate 	"\t-d  list dependencies of the specified service(s)\n"
18087c478bd9Sstevel@tonic-gate 	"\t-D  list dependents of the specified service(s)\n"
18097c478bd9Sstevel@tonic-gate 	"\t-H  omit header line from output\n"
18107c478bd9Sstevel@tonic-gate 	"\t-l  list detailed information about the specified service(s)\n"
18117c478bd9Sstevel@tonic-gate 	"\t-o  list only the specified columns in the output\n"
18127c478bd9Sstevel@tonic-gate 	"\t-p  list process IDs and names associated with each service\n"
18137c478bd9Sstevel@tonic-gate 	"\t-R  list only those services with the specified restarter\n"
18147c478bd9Sstevel@tonic-gate 	"\t-s  sort output in ascending order by the specified column(s)\n"
18157c478bd9Sstevel@tonic-gate 	"\t-S  sort output in descending order by the specified column(s)\n"
18167c478bd9Sstevel@tonic-gate 	"\t-v  list verbose information appropriate to the type of output\n"
18177c478bd9Sstevel@tonic-gate 	"\t-x  explain the status of services that might require maintenance,\n"
18187c478bd9Sstevel@tonic-gate 	"\t    or explain the status of the specified service(s)\n"
18197c478bd9Sstevel@tonic-gate 	"\n\t"
18207c478bd9Sstevel@tonic-gate 	"Services can be specified using an FMRI, abbreviation, or fnmatch(5)\n"
18217c478bd9Sstevel@tonic-gate 	"\tpattern, as shown in these examples for svc:/network/smtp:sendmail\n"
18227c478bd9Sstevel@tonic-gate 	"\n"
18237c478bd9Sstevel@tonic-gate 	"\t%1$s [opts] svc:/network/smtp:sendmail\n"
18247c478bd9Sstevel@tonic-gate 	"\t%1$s [opts] network/smtp:sendmail\n"
18257c478bd9Sstevel@tonic-gate 	"\t%1$s [opts] network/*mail\n"
18267c478bd9Sstevel@tonic-gate 	"\t%1$s [opts] network/smtp\n"
18277c478bd9Sstevel@tonic-gate 	"\t%1$s [opts] smtp:sendmail\n"
18287c478bd9Sstevel@tonic-gate 	"\t%1$s [opts] smtp\n"
18297c478bd9Sstevel@tonic-gate 	"\t%1$s [opts] sendmail\n"
18307c478bd9Sstevel@tonic-gate 	"\n\t"
18317c478bd9Sstevel@tonic-gate 	"Columns for output or sorting can be specified using these names:\n"
18327c478bd9Sstevel@tonic-gate 	"\n"), progname);
18337c478bd9Sstevel@tonic-gate 
18347c478bd9Sstevel@tonic-gate 	for (i = 0; i < ncolumns; i++) {
18357c478bd9Sstevel@tonic-gate 		(void) printf("\t%-" MAX_COLUMN_NAME_LENGTH_STR "s  %s\n",
18367c478bd9Sstevel@tonic-gate 		    columns[i].name, description_of_column(i));
18377c478bd9Sstevel@tonic-gate 	}
18387c478bd9Sstevel@tonic-gate }
18397c478bd9Sstevel@tonic-gate 
18407c478bd9Sstevel@tonic-gate 
18417c478bd9Sstevel@tonic-gate /*
18427c478bd9Sstevel@tonic-gate  * A getsubopt()-like function which returns an index into the columns table.
18437c478bd9Sstevel@tonic-gate  * On success, *optionp is set to point to the next sub-option, or the
18447c478bd9Sstevel@tonic-gate  * terminating null if there are none.
18457c478bd9Sstevel@tonic-gate  */
18467c478bd9Sstevel@tonic-gate static int
18477c478bd9Sstevel@tonic-gate getcolumnopt(char **optionp)
18487c478bd9Sstevel@tonic-gate {
18497c478bd9Sstevel@tonic-gate 	char *str = *optionp, *cp;
18507c478bd9Sstevel@tonic-gate 	int i;
18517c478bd9Sstevel@tonic-gate 
18527c478bd9Sstevel@tonic-gate 	assert(optionp != NULL);
18537c478bd9Sstevel@tonic-gate 	assert(*optionp != NULL);
18547c478bd9Sstevel@tonic-gate 
18557c478bd9Sstevel@tonic-gate 	cp = strchr(*optionp, ',');
18567c478bd9Sstevel@tonic-gate 	if (cp != NULL)
18577c478bd9Sstevel@tonic-gate 		*cp = '\0';
18587c478bd9Sstevel@tonic-gate 
18597c478bd9Sstevel@tonic-gate 	for (i = 0; i < ncolumns; ++i) {
18607c478bd9Sstevel@tonic-gate 		if (strcasecmp(str, columns[i].name) == 0) {
18617c478bd9Sstevel@tonic-gate 			if (cp != NULL)
18627c478bd9Sstevel@tonic-gate 				*optionp = cp + 1;
18637c478bd9Sstevel@tonic-gate 			else
18647c478bd9Sstevel@tonic-gate 				*optionp = strchr(*optionp, '\0');
18657c478bd9Sstevel@tonic-gate 
18667c478bd9Sstevel@tonic-gate 			return (i);
18677c478bd9Sstevel@tonic-gate 		}
18687c478bd9Sstevel@tonic-gate 	}
18697c478bd9Sstevel@tonic-gate 
18707c478bd9Sstevel@tonic-gate 	return (-1);
18717c478bd9Sstevel@tonic-gate }
18727c478bd9Sstevel@tonic-gate 
18737c478bd9Sstevel@tonic-gate static void
18747c478bd9Sstevel@tonic-gate print_header()
18757c478bd9Sstevel@tonic-gate {
18767c478bd9Sstevel@tonic-gate 	int i;
18777c478bd9Sstevel@tonic-gate 	char *line_buf, *cp;
18787c478bd9Sstevel@tonic-gate 
18797c478bd9Sstevel@tonic-gate 	line_buf = safe_malloc(line_sz);
18807c478bd9Sstevel@tonic-gate 	cp = line_buf;
18817c478bd9Sstevel@tonic-gate 	for (i = 0; i < opt_cnum; ++i) {
18827c478bd9Sstevel@tonic-gate 		const struct column * const colp = &columns[opt_columns[i]];
18837c478bd9Sstevel@tonic-gate 
18847c478bd9Sstevel@tonic-gate 		(void) snprintf(cp, colp->width + 1, "%-*s", colp->width,
18857c478bd9Sstevel@tonic-gate 		    colp->name);
18867c478bd9Sstevel@tonic-gate 		cp += colp->width;
18877c478bd9Sstevel@tonic-gate 		*cp++ = ' ';
18887c478bd9Sstevel@tonic-gate 	}
18897c478bd9Sstevel@tonic-gate 
18907c478bd9Sstevel@tonic-gate 	/* Trim the trailing whitespace */
18917c478bd9Sstevel@tonic-gate 	--cp;
18927c478bd9Sstevel@tonic-gate 	while (*cp == ' ')
18937c478bd9Sstevel@tonic-gate 		--cp;
18947c478bd9Sstevel@tonic-gate 	*(cp+1) = '\0';
18957c478bd9Sstevel@tonic-gate 	(void) puts(line_buf);
18967c478bd9Sstevel@tonic-gate 
18977c478bd9Sstevel@tonic-gate 	free(line_buf);
18987c478bd9Sstevel@tonic-gate }
18997c478bd9Sstevel@tonic-gate 
19007c478bd9Sstevel@tonic-gate 
19017c478bd9Sstevel@tonic-gate 
19027c478bd9Sstevel@tonic-gate /*
19037c478bd9Sstevel@tonic-gate  * Long listing (-l) functions.
19047c478bd9Sstevel@tonic-gate  */
19057c478bd9Sstevel@tonic-gate 
19067c478bd9Sstevel@tonic-gate static int
19077c478bd9Sstevel@tonic-gate pidcmp(const void *l, const void *r)
19087c478bd9Sstevel@tonic-gate {
19097c478bd9Sstevel@tonic-gate 	pid_t lp = *(pid_t *)l, rp = *(pid_t *)r;
19107c478bd9Sstevel@tonic-gate 
19117c478bd9Sstevel@tonic-gate 	if (lp < rp)
19127c478bd9Sstevel@tonic-gate 		return (-1);
19137c478bd9Sstevel@tonic-gate 	if (lp > rp)
19147c478bd9Sstevel@tonic-gate 		return (1);
19157c478bd9Sstevel@tonic-gate 	return (0);
19167c478bd9Sstevel@tonic-gate }
19177c478bd9Sstevel@tonic-gate 
19187c478bd9Sstevel@tonic-gate /*
19197c478bd9Sstevel@tonic-gate  * This is the strlen() of the longest label ("description"), plus intercolumn
19207c478bd9Sstevel@tonic-gate  * space.
19217c478bd9Sstevel@tonic-gate  */
19227c478bd9Sstevel@tonic-gate #define	DETAILED_WIDTH	(11 + 2)
19237c478bd9Sstevel@tonic-gate 
192494501b61Sskamm /*
192594501b61Sskamm  * Callback routine to print header for contract id.
192694501b61Sskamm  * Called by ctids_by_restarter and print_detailed.
192794501b61Sskamm  */
19287c478bd9Sstevel@tonic-gate static void
192994501b61Sskamm print_ctid_header()
193094501b61Sskamm {
193194501b61Sskamm 	(void) printf("%-*s", DETAILED_WIDTH, "contract_id");
193294501b61Sskamm }
193394501b61Sskamm 
193494501b61Sskamm /*
193594501b61Sskamm  * Callback routine to print a contract id.
193694501b61Sskamm  * Called by ctids_by_restarter and print_detailed.
193794501b61Sskamm  */
193894501b61Sskamm static void
193994501b61Sskamm print_ctid_detailed(uint64_t c)
194094501b61Sskamm {
194194501b61Sskamm 	(void) printf("%lu ", (ctid_t)c);
194294501b61Sskamm }
194394501b61Sskamm 
194494501b61Sskamm static void
194594501b61Sskamm detailed_list_processes(scf_walkinfo_t *wip)
19467c478bd9Sstevel@tonic-gate {
19477c478bd9Sstevel@tonic-gate 	uint64_t c;
19487c478bd9Sstevel@tonic-gate 	pid_t *pids;
19497c478bd9Sstevel@tonic-gate 	uint_t i, n;
19507c478bd9Sstevel@tonic-gate 	psinfo_t psi;
19517c478bd9Sstevel@tonic-gate 
195294501b61Sskamm 	if (get_restarter_count_prop(wip->inst, scf_property_contract, &c,
19537c478bd9Sstevel@tonic-gate 	    EMPTY_OK) != 0)
19547c478bd9Sstevel@tonic-gate 		return;
19557c478bd9Sstevel@tonic-gate 
195694501b61Sskamm 	if (instance_processes(wip->inst, wip->fmri, &pids, &n) != 0)
19577c478bd9Sstevel@tonic-gate 		return;
19587c478bd9Sstevel@tonic-gate 
19597c478bd9Sstevel@tonic-gate 	qsort(pids, n, sizeof (*pids), pidcmp);
19607c478bd9Sstevel@tonic-gate 
19617c478bd9Sstevel@tonic-gate 	for (i = 0; i < n; ++i) {
19627c478bd9Sstevel@tonic-gate 		(void) printf("%-*s%lu", DETAILED_WIDTH, gettext("process"),
19637c478bd9Sstevel@tonic-gate 		    pids[i]);
19647c478bd9Sstevel@tonic-gate 
19657c478bd9Sstevel@tonic-gate 		if (get_psinfo(pids[i], &psi) == 0)
19667c478bd9Sstevel@tonic-gate 			(void) printf(" %.*s", PRARGSZ, psi.pr_psargs);
19677c478bd9Sstevel@tonic-gate 
19687c478bd9Sstevel@tonic-gate 		(void) putchar('\n');
19697c478bd9Sstevel@tonic-gate 	}
19707c478bd9Sstevel@tonic-gate 
19717c478bd9Sstevel@tonic-gate 	free(pids);
19727c478bd9Sstevel@tonic-gate }
19737c478bd9Sstevel@tonic-gate 
19747c478bd9Sstevel@tonic-gate /*
19757c478bd9Sstevel@tonic-gate  * Determines the state of a dependency.  If the FMRI specifies a file, then we
19767c478bd9Sstevel@tonic-gate  * fake up a state based on whether we can access the file.
19777c478bd9Sstevel@tonic-gate  */
19787c478bd9Sstevel@tonic-gate static void
19797c478bd9Sstevel@tonic-gate get_fmri_state(char *fmri, char *state, size_t state_sz)
19807c478bd9Sstevel@tonic-gate {
19817c478bd9Sstevel@tonic-gate 	char *lfmri;
19827c478bd9Sstevel@tonic-gate 	const char *svc_name, *inst_name, *pg_name, *path;
19837c478bd9Sstevel@tonic-gate 	scf_service_t *svc;
19847c478bd9Sstevel@tonic-gate 	scf_instance_t *inst;
19857c478bd9Sstevel@tonic-gate 	scf_iter_t *iter;
19867c478bd9Sstevel@tonic-gate 
19877c478bd9Sstevel@tonic-gate 	lfmri = safe_strdup(fmri);
19887c478bd9Sstevel@tonic-gate 
19897c478bd9Sstevel@tonic-gate 	/*
19907c478bd9Sstevel@tonic-gate 	 * Check for file:// dependencies
19917c478bd9Sstevel@tonic-gate 	 */
19927c478bd9Sstevel@tonic-gate 	if (scf_parse_file_fmri(lfmri, NULL, &path) == SCF_SUCCESS) {
19937c478bd9Sstevel@tonic-gate 		struct stat64 statbuf;
19947c478bd9Sstevel@tonic-gate 		const char *msg;
19957c478bd9Sstevel@tonic-gate 
19967c478bd9Sstevel@tonic-gate 		if (stat64(path, &statbuf) == 0)
19977c478bd9Sstevel@tonic-gate 			msg = "online";
19987c478bd9Sstevel@tonic-gate 		else if (errno == ENOENT)
19997c478bd9Sstevel@tonic-gate 			msg = "absent";
20007c478bd9Sstevel@tonic-gate 		else
20017c478bd9Sstevel@tonic-gate 			msg = "unknown";
20027c478bd9Sstevel@tonic-gate 
20037c478bd9Sstevel@tonic-gate 		(void) strlcpy(state, msg, state_sz);
20047c478bd9Sstevel@tonic-gate 		return;
20057c478bd9Sstevel@tonic-gate 	}
20067c478bd9Sstevel@tonic-gate 
20077c478bd9Sstevel@tonic-gate 	/*
20087c478bd9Sstevel@tonic-gate 	 * scf_parse_file_fmri() may have overwritten part of the string, so
20097c478bd9Sstevel@tonic-gate 	 * copy it back.
20107c478bd9Sstevel@tonic-gate 	 */
20117c478bd9Sstevel@tonic-gate 	(void) strcpy(lfmri, fmri);
20127c478bd9Sstevel@tonic-gate 
20137c478bd9Sstevel@tonic-gate 	if (scf_parse_svc_fmri(lfmri, NULL, &svc_name, &inst_name,
20147c478bd9Sstevel@tonic-gate 	    &pg_name, NULL) != SCF_SUCCESS) {
20157c478bd9Sstevel@tonic-gate 		free(lfmri);
20167c478bd9Sstevel@tonic-gate 		(void) strlcpy(state, "invalid", state_sz);
20177c478bd9Sstevel@tonic-gate 		return;
20187c478bd9Sstevel@tonic-gate 	}
20197c478bd9Sstevel@tonic-gate 
20207c478bd9Sstevel@tonic-gate 	free(lfmri);
20217c478bd9Sstevel@tonic-gate 
20227c478bd9Sstevel@tonic-gate 	if (svc_name == NULL || pg_name != NULL) {
20237c478bd9Sstevel@tonic-gate 		(void) strlcpy(state, "invalid", state_sz);
20247c478bd9Sstevel@tonic-gate 		return;
20257c478bd9Sstevel@tonic-gate 	}
20267c478bd9Sstevel@tonic-gate 
20277c478bd9Sstevel@tonic-gate 	if (inst_name != NULL) {
20287c478bd9Sstevel@tonic-gate 		/* instance: get state */
20297c478bd9Sstevel@tonic-gate 		inst = scf_instance_create(h);
20307c478bd9Sstevel@tonic-gate 		if (inst == NULL)
20317c478bd9Sstevel@tonic-gate 			scfdie();
20327c478bd9Sstevel@tonic-gate 
20337c478bd9Sstevel@tonic-gate 		if (scf_handle_decode_fmri(h, fmri, NULL, NULL, inst, NULL,
20347c478bd9Sstevel@tonic-gate 		    NULL, SCF_DECODE_FMRI_EXACT) == SCF_SUCCESS)
20357c478bd9Sstevel@tonic-gate 			get_restarter_string_prop(inst, scf_property_state,
20367c478bd9Sstevel@tonic-gate 			    state, state_sz);
20377c478bd9Sstevel@tonic-gate 		else {
20387c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
20397c478bd9Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
20407c478bd9Sstevel@tonic-gate 				(void) strlcpy(state, "invalid", state_sz);
20417c478bd9Sstevel@tonic-gate 				break;
20427c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_FOUND:
20437c478bd9Sstevel@tonic-gate 				(void) strlcpy(state, "absent", state_sz);
20447c478bd9Sstevel@tonic-gate 				break;
20457c478bd9Sstevel@tonic-gate 
20467c478bd9Sstevel@tonic-gate 			default:
20477c478bd9Sstevel@tonic-gate 				scfdie();
20487c478bd9Sstevel@tonic-gate 			}
20497c478bd9Sstevel@tonic-gate 		}
20507c478bd9Sstevel@tonic-gate 
20517c478bd9Sstevel@tonic-gate 		scf_instance_destroy(inst);
20527c478bd9Sstevel@tonic-gate 		return;
20537c478bd9Sstevel@tonic-gate 	}
20547c478bd9Sstevel@tonic-gate 
20557c478bd9Sstevel@tonic-gate 	/*
20567c478bd9Sstevel@tonic-gate 	 * service: If only one instance, use that state.  Otherwise, say
20577c478bd9Sstevel@tonic-gate 	 * "multiple".
20587c478bd9Sstevel@tonic-gate 	 */
20597c478bd9Sstevel@tonic-gate 	if ((svc = scf_service_create(h)) == NULL ||
20607c478bd9Sstevel@tonic-gate 	    (inst = scf_instance_create(h)) == NULL ||
20617c478bd9Sstevel@tonic-gate 	    (iter = scf_iter_create(h)) == NULL)
20627c478bd9Sstevel@tonic-gate 		scfdie();
20637c478bd9Sstevel@tonic-gate 
20647c478bd9Sstevel@tonic-gate 	if (scf_handle_decode_fmri(h, fmri, NULL, svc, NULL, NULL, NULL,
20657c478bd9Sstevel@tonic-gate 	    SCF_DECODE_FMRI_EXACT) != SCF_SUCCESS) {
20667c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
20677c478bd9Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
20687c478bd9Sstevel@tonic-gate 			(void) strlcpy(state, "invalid", state_sz);
20697c478bd9Sstevel@tonic-gate 			goto out;
20707c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
20717c478bd9Sstevel@tonic-gate 			(void) strlcpy(state, "absent", state_sz);
20727c478bd9Sstevel@tonic-gate 			goto out;
20737c478bd9Sstevel@tonic-gate 
20747c478bd9Sstevel@tonic-gate 		default:
20757c478bd9Sstevel@tonic-gate 			scfdie();
20767c478bd9Sstevel@tonic-gate 		}
20777c478bd9Sstevel@tonic-gate 	}
20787c478bd9Sstevel@tonic-gate 
20797c478bd9Sstevel@tonic-gate 	if (scf_iter_service_instances(iter, svc) != SCF_SUCCESS)
20807c478bd9Sstevel@tonic-gate 		scfdie();
20817c478bd9Sstevel@tonic-gate 
20827c478bd9Sstevel@tonic-gate 	switch (scf_iter_next_instance(iter, inst)) {
20837c478bd9Sstevel@tonic-gate 	case 0:
20847c478bd9Sstevel@tonic-gate 		(void) strlcpy(state, "absent", state_sz);
20857c478bd9Sstevel@tonic-gate 		goto out;
20867c478bd9Sstevel@tonic-gate 
20877c478bd9Sstevel@tonic-gate 	case 1:
20887c478bd9Sstevel@tonic-gate 		break;
20897c478bd9Sstevel@tonic-gate 
20907c478bd9Sstevel@tonic-gate 	default:
20917c478bd9Sstevel@tonic-gate 		scfdie();
20927c478bd9Sstevel@tonic-gate 	}
20937c478bd9Sstevel@tonic-gate 
20947c478bd9Sstevel@tonic-gate 	/* Get the state in case this is the only instance. */
20957c478bd9Sstevel@tonic-gate 	get_restarter_string_prop(inst, scf_property_state, state, state_sz);
20967c478bd9Sstevel@tonic-gate 
20977c478bd9Sstevel@tonic-gate 	switch (scf_iter_next_instance(iter, inst)) {
20987c478bd9Sstevel@tonic-gate 	case 0:
20997c478bd9Sstevel@tonic-gate 		break;
21007c478bd9Sstevel@tonic-gate 
21017c478bd9Sstevel@tonic-gate 	case 1:
21027c478bd9Sstevel@tonic-gate 		/* Nope, multiple instances. */
21037c478bd9Sstevel@tonic-gate 		(void) strlcpy(state, "multiple", state_sz);
21047c478bd9Sstevel@tonic-gate 		goto out;
21057c478bd9Sstevel@tonic-gate 
21067c478bd9Sstevel@tonic-gate 	default:
21077c478bd9Sstevel@tonic-gate 		scfdie();
21087c478bd9Sstevel@tonic-gate 	}
21097c478bd9Sstevel@tonic-gate 
21107c478bd9Sstevel@tonic-gate out:
21117c478bd9Sstevel@tonic-gate 	scf_iter_destroy(iter);
21127c478bd9Sstevel@tonic-gate 	scf_instance_destroy(inst);
21137c478bd9Sstevel@tonic-gate 	scf_service_destroy(svc);
21147c478bd9Sstevel@tonic-gate }
21157c478bd9Sstevel@tonic-gate 
21167c478bd9Sstevel@tonic-gate static void
21171f6eb021SLiane Praza print_application_properties(scf_walkinfo_t *wip, scf_snapshot_t *snap)
21181f6eb021SLiane Praza {
21191f6eb021SLiane Praza 	scf_iter_t *pg_iter, *prop_iter, *val_iter;
21201f6eb021SLiane Praza 	scf_propertygroup_t *pg;
21211f6eb021SLiane Praza 	scf_property_t *prop;
21221f6eb021SLiane Praza 	scf_value_t *val;
21231f6eb021SLiane Praza 	scf_pg_tmpl_t *pt;
21241f6eb021SLiane Praza 	scf_prop_tmpl_t *prt;
21251f6eb021SLiane Praza 	char *pg_name_buf = safe_malloc(max_scf_name_length + 1);
21261f6eb021SLiane Praza 	char *prop_name_buf = safe_malloc(max_scf_name_length + 1);
21271f6eb021SLiane Praza 	char *snap_name = safe_malloc(max_scf_name_length + 1);
21281f6eb021SLiane Praza 	char *val_buf = safe_malloc(max_scf_value_length + 1);
21291f6eb021SLiane Praza 	char *desc, *cp;
21301f6eb021SLiane Praza 	scf_type_t type;
21311f6eb021SLiane Praza 	int i, j, k;
21321f6eb021SLiane Praza 	uint8_t vis;
21331f6eb021SLiane Praza 
21341f6eb021SLiane Praza 	if ((pg_iter = scf_iter_create(h)) == NULL ||
21351f6eb021SLiane Praza 	    (prop_iter = scf_iter_create(h)) == NULL ||
21361f6eb021SLiane Praza 	    (val_iter = scf_iter_create(h)) == NULL ||
21371f6eb021SLiane Praza 	    (val = scf_value_create(h)) == NULL ||
21381f6eb021SLiane Praza 	    (prop = scf_property_create(h)) == NULL ||
21391f6eb021SLiane Praza 	    (pt = scf_tmpl_pg_create(h)) == NULL ||
21401f6eb021SLiane Praza 	    (prt = scf_tmpl_prop_create(h)) == NULL ||
21411f6eb021SLiane Praza 	    (pg = scf_pg_create(h)) == NULL)
21421f6eb021SLiane Praza 		scfdie();
21431f6eb021SLiane Praza 
21441f6eb021SLiane Praza 	if (scf_iter_instance_pgs_typed_composed(pg_iter, wip->inst, snap,
21451f6eb021SLiane Praza 	    SCF_PG_APP_DEFAULT) == -1)
21461f6eb021SLiane Praza 		scfdie();
21471f6eb021SLiane Praza 
21481f6eb021SLiane Praza 	/*
21491f6eb021SLiane Praza 	 * Format for output:
21501f6eb021SLiane Praza 	 *	pg (pgtype)
21511f6eb021SLiane Praza 	 *	 description
21521f6eb021SLiane Praza 	 *	pg/prop (proptype) = <value> <value>
21531f6eb021SLiane Praza 	 *	 description
21541f6eb021SLiane Praza 	 */
21551f6eb021SLiane Praza 	while ((i = scf_iter_next_pg(pg_iter, pg)) == 1) {
21561f6eb021SLiane Praza 		int tmpl = 0;
21571f6eb021SLiane Praza 
21581f6eb021SLiane Praza 		if (scf_pg_get_name(pg, pg_name_buf, max_scf_name_length) < 0)
21591f6eb021SLiane Praza 			scfdie();
21601f6eb021SLiane Praza 		if (scf_snapshot_get_name(snap, snap_name,
21611f6eb021SLiane Praza 		    max_scf_name_length) < 0)
21621f6eb021SLiane Praza 			scfdie();
21631f6eb021SLiane Praza 
21641f6eb021SLiane Praza 		if (scf_tmpl_get_by_pg_name(wip->fmri, snap_name, pg_name_buf,
21651f6eb021SLiane Praza 		    SCF_PG_APP_DEFAULT, pt, 0) == 0)
21661f6eb021SLiane Praza 			tmpl = 1;
21671f6eb021SLiane Praza 		else
21681f6eb021SLiane Praza 			tmpl = 0;
21691f6eb021SLiane Praza 
21701f6eb021SLiane Praza 		(void) printf("%s (%s)\n", pg_name_buf, SCF_PG_APP_DEFAULT);
21711f6eb021SLiane Praza 
21721f6eb021SLiane Praza 		if (tmpl == 1 && scf_tmpl_pg_description(pt, NULL, &desc) > 0) {
21731f6eb021SLiane Praza 			(void) printf("  %s\n", desc);
21741f6eb021SLiane Praza 			free(desc);
21751f6eb021SLiane Praza 		}
21761f6eb021SLiane Praza 
21771f6eb021SLiane Praza 		if (scf_iter_pg_properties(prop_iter, pg) == -1)
21781f6eb021SLiane Praza 			scfdie();
21791f6eb021SLiane Praza 		while ((j = scf_iter_next_property(prop_iter, prop)) == 1) {
21801f6eb021SLiane Praza 			if (scf_property_get_name(prop, prop_name_buf,
21811f6eb021SLiane Praza 			    max_scf_name_length) < 0)
21821f6eb021SLiane Praza 				scfdie();
21831f6eb021SLiane Praza 			if (scf_property_type(prop, &type) == -1)
21841f6eb021SLiane Praza 				scfdie();
21851f6eb021SLiane Praza 
21861f6eb021SLiane Praza 			if ((tmpl == 1) &&
21871f6eb021SLiane Praza 			    (scf_tmpl_get_by_prop(pt, prop_name_buf, prt,
21881f6eb021SLiane Praza 			    0) != 0))
21891f6eb021SLiane Praza 				tmpl = 0;
21901f6eb021SLiane Praza 
21911f6eb021SLiane Praza 			if (tmpl == 1 &&
21921f6eb021SLiane Praza 			    scf_tmpl_prop_visibility(prt, &vis) != -1 &&
21931f6eb021SLiane Praza 			    vis == SCF_TMPL_VISIBILITY_HIDDEN)
21941f6eb021SLiane Praza 				continue;
21951f6eb021SLiane Praza 
21961f6eb021SLiane Praza 			(void) printf("%s/%s (%s) = ", pg_name_buf,
21971f6eb021SLiane Praza 			    prop_name_buf, scf_type_to_string(type));
21981f6eb021SLiane Praza 
21991f6eb021SLiane Praza 			if (scf_iter_property_values(val_iter, prop) == -1)
22001f6eb021SLiane Praza 				scfdie();
22011f6eb021SLiane Praza 
22021f6eb021SLiane Praza 			while ((k = scf_iter_next_value(val_iter, val)) == 1) {
22031f6eb021SLiane Praza 				if (scf_value_get_as_string(val, val_buf,
22041f6eb021SLiane Praza 				    max_scf_value_length + 1) < 0)
22051f6eb021SLiane Praza 					scfdie();
22061f6eb021SLiane Praza 				if (strpbrk(val_buf, " \t\n\"()") != NULL) {
22071f6eb021SLiane Praza 					(void) printf("\"");
22081f6eb021SLiane Praza 					for (cp = val_buf; *cp != '\0'; ++cp) {
22091f6eb021SLiane Praza 						if (*cp == '"' || *cp == '\\')
22101f6eb021SLiane Praza 							(void) putc('\\',
22111f6eb021SLiane Praza 							    stdout);
22121f6eb021SLiane Praza 
22131f6eb021SLiane Praza 						(void) putc(*cp, stdout);
22141f6eb021SLiane Praza 					}
22151f6eb021SLiane Praza 					(void) printf("\"");
22161f6eb021SLiane Praza 				} else {
22171f6eb021SLiane Praza 					(void) printf("%s ", val_buf);
22181f6eb021SLiane Praza 				}
22191f6eb021SLiane Praza 			}
22201f6eb021SLiane Praza 
22211f6eb021SLiane Praza 			(void) printf("\n");
22221f6eb021SLiane Praza 
22231f6eb021SLiane Praza 			if (k == -1)
22241f6eb021SLiane Praza 				scfdie();
22251f6eb021SLiane Praza 
22261f6eb021SLiane Praza 			if (tmpl == 1 && scf_tmpl_prop_description(prt, NULL,
22271f6eb021SLiane Praza 			    &desc) > 0) {
22281f6eb021SLiane Praza 				(void) printf("  %s\n", desc);
22291f6eb021SLiane Praza 				free(desc);
22301f6eb021SLiane Praza 			}
22311f6eb021SLiane Praza 		}
22321f6eb021SLiane Praza 		if (j == -1)
22331f6eb021SLiane Praza 			scfdie();
22341f6eb021SLiane Praza 	}
22351f6eb021SLiane Praza 	if (i == -1)
22361f6eb021SLiane Praza 		scfdie();
22371f6eb021SLiane Praza 
22381f6eb021SLiane Praza 
22391f6eb021SLiane Praza 	scf_iter_destroy(pg_iter);
22401f6eb021SLiane Praza 	scf_iter_destroy(prop_iter);
22411f6eb021SLiane Praza 	scf_iter_destroy(val_iter);
22421f6eb021SLiane Praza 	scf_value_destroy(val);
22431f6eb021SLiane Praza 	scf_property_destroy(prop);
22441f6eb021SLiane Praza 	scf_tmpl_pg_destroy(pt);
22451f6eb021SLiane Praza 	scf_tmpl_prop_destroy(prt);
22461f6eb021SLiane Praza 	scf_pg_destroy(pg);
22471f6eb021SLiane Praza 	free(pg_name_buf);
22481f6eb021SLiane Praza 	free(prop_name_buf);
22491f6eb021SLiane Praza 	free(snap_name);
22501f6eb021SLiane Praza 	free(val_buf);
22511f6eb021SLiane Praza }
22521f6eb021SLiane Praza 
22531f6eb021SLiane Praza static void
22547c478bd9Sstevel@tonic-gate print_detailed_dependency(scf_propertygroup_t *pg)
22557c478bd9Sstevel@tonic-gate {
22567c478bd9Sstevel@tonic-gate 	scf_property_t *eprop;
22577c478bd9Sstevel@tonic-gate 	scf_iter_t *iter;
22587c478bd9Sstevel@tonic-gate 	scf_type_t ty;
22597c478bd9Sstevel@tonic-gate 	char *val_buf;
22607c478bd9Sstevel@tonic-gate 	int i;
22617c478bd9Sstevel@tonic-gate 
22627c478bd9Sstevel@tonic-gate 	if ((eprop = scf_property_create(h)) == NULL ||
22637c478bd9Sstevel@tonic-gate 	    (iter = scf_iter_create(h)) == NULL)
22647c478bd9Sstevel@tonic-gate 		scfdie();
22657c478bd9Sstevel@tonic-gate 
22667c478bd9Sstevel@tonic-gate 	val_buf = safe_malloc(max_scf_value_length + 1);
22677c478bd9Sstevel@tonic-gate 
22687c478bd9Sstevel@tonic-gate 	if (scf_pg_get_property(pg, SCF_PROPERTY_ENTITIES, eprop) !=
22697c478bd9Sstevel@tonic-gate 	    SCF_SUCCESS ||
22707c478bd9Sstevel@tonic-gate 	    scf_property_type(eprop, &ty) != SCF_SUCCESS ||
22717c478bd9Sstevel@tonic-gate 	    ty != SCF_TYPE_FMRI)
22727c478bd9Sstevel@tonic-gate 		return;
22737c478bd9Sstevel@tonic-gate 
22747c478bd9Sstevel@tonic-gate 	(void) printf("%-*s", DETAILED_WIDTH, gettext("dependency"));
22757c478bd9Sstevel@tonic-gate 
22767c478bd9Sstevel@tonic-gate 	/* Print the grouping */
22777c478bd9Sstevel@tonic-gate 	if (pg_get_single_val(pg, SCF_PROPERTY_GROUPING, SCF_TYPE_ASTRING,
22787c478bd9Sstevel@tonic-gate 	    val_buf, max_scf_value_length + 1, 0) == 0)
22797c478bd9Sstevel@tonic-gate 		(void) fputs(val_buf, stdout);
22807c478bd9Sstevel@tonic-gate 	else
22817c478bd9Sstevel@tonic-gate 		(void) putchar('?');
22827c478bd9Sstevel@tonic-gate 
22837c478bd9Sstevel@tonic-gate 	(void) putchar('/');
22847c478bd9Sstevel@tonic-gate 
22857c478bd9Sstevel@tonic-gate 	if (pg_get_single_val(pg, SCF_PROPERTY_RESTART_ON, SCF_TYPE_ASTRING,
22867c478bd9Sstevel@tonic-gate 	    val_buf, max_scf_value_length + 1, 0) == 0)
22877c478bd9Sstevel@tonic-gate 		(void) fputs(val_buf, stdout);
22887c478bd9Sstevel@tonic-gate 	else
22897c478bd9Sstevel@tonic-gate 		(void) putchar('?');
22907c478bd9Sstevel@tonic-gate 
22917c478bd9Sstevel@tonic-gate 	/* Print the dependency entities. */
22927c478bd9Sstevel@tonic-gate 	if (scf_iter_property_values(iter, eprop) == -1)
22937c478bd9Sstevel@tonic-gate 		scfdie();
22947c478bd9Sstevel@tonic-gate 
22957c478bd9Sstevel@tonic-gate 	while ((i = scf_iter_next_value(iter, g_val)) == 1) {
22967c478bd9Sstevel@tonic-gate 		char state[MAX_SCF_STATE_STRING_SZ];
22977c478bd9Sstevel@tonic-gate 
22987c478bd9Sstevel@tonic-gate 		if (scf_value_get_astring(g_val, val_buf,
22997c478bd9Sstevel@tonic-gate 		    max_scf_value_length + 1) < 0)
23007c478bd9Sstevel@tonic-gate 			scfdie();
23017c478bd9Sstevel@tonic-gate 
23027c478bd9Sstevel@tonic-gate 		(void) putchar(' ');
23037c478bd9Sstevel@tonic-gate 		(void) fputs(val_buf, stdout);
23047c478bd9Sstevel@tonic-gate 
23057c478bd9Sstevel@tonic-gate 		/* Print the state. */
23067c478bd9Sstevel@tonic-gate 		state[0] = '-';
23077c478bd9Sstevel@tonic-gate 		state[1] = '\0';
23087c478bd9Sstevel@tonic-gate 
23097c478bd9Sstevel@tonic-gate 		get_fmri_state(val_buf, state, sizeof (state));
23107c478bd9Sstevel@tonic-gate 
23117c478bd9Sstevel@tonic-gate 		(void) printf(" (%s)", state);
23127c478bd9Sstevel@tonic-gate 	}
23137c478bd9Sstevel@tonic-gate 	if (i == -1)
23147c478bd9Sstevel@tonic-gate 		scfdie();
23157c478bd9Sstevel@tonic-gate 
23167c478bd9Sstevel@tonic-gate 	(void) putchar('\n');
23177c478bd9Sstevel@tonic-gate 
23187c478bd9Sstevel@tonic-gate 	free(val_buf);
23197c478bd9Sstevel@tonic-gate 	scf_iter_destroy(iter);
23207c478bd9Sstevel@tonic-gate 	scf_property_destroy(eprop);
23217c478bd9Sstevel@tonic-gate }
23227c478bd9Sstevel@tonic-gate 
23237c478bd9Sstevel@tonic-gate /* ARGSUSED */
23247c478bd9Sstevel@tonic-gate static int
23257c478bd9Sstevel@tonic-gate print_detailed(void *unused, scf_walkinfo_t *wip)
23267c478bd9Sstevel@tonic-gate {
23277c478bd9Sstevel@tonic-gate 	scf_snapshot_t *snap;
23287c478bd9Sstevel@tonic-gate 	scf_propertygroup_t *rpg;
23297c478bd9Sstevel@tonic-gate 	scf_iter_t *pg_iter;
23307c478bd9Sstevel@tonic-gate 
23317c478bd9Sstevel@tonic-gate 	char *buf;
23327c478bd9Sstevel@tonic-gate 	char *timebuf;
23337c478bd9Sstevel@tonic-gate 	size_t tbsz;
23347c478bd9Sstevel@tonic-gate 	int ret;
23357c478bd9Sstevel@tonic-gate 	uint64_t c;
23367c478bd9Sstevel@tonic-gate 	int temp, perm;
23377c478bd9Sstevel@tonic-gate 	struct timeval tv;
23387c478bd9Sstevel@tonic-gate 	time_t stime;
23397c478bd9Sstevel@tonic-gate 	struct tm *tmp;
234094501b61Sskamm 	int restarter_spec;
234194501b61Sskamm 	int restarter_ret;
23427c478bd9Sstevel@tonic-gate 
23437c478bd9Sstevel@tonic-gate 	const char * const fmt = "%-*s%s\n";
23447c478bd9Sstevel@tonic-gate 
23457c478bd9Sstevel@tonic-gate 	assert(wip->pg == NULL);
23467c478bd9Sstevel@tonic-gate 
23477c478bd9Sstevel@tonic-gate 	rpg = scf_pg_create(h);
23487c478bd9Sstevel@tonic-gate 	if (rpg == NULL)
23497c478bd9Sstevel@tonic-gate 		scfdie();
23507c478bd9Sstevel@tonic-gate 
23517c478bd9Sstevel@tonic-gate 	if (first_paragraph)
23527c478bd9Sstevel@tonic-gate 		first_paragraph = 0;
23537c478bd9Sstevel@tonic-gate 	else
23547c478bd9Sstevel@tonic-gate 		(void) putchar('\n');
23557c478bd9Sstevel@tonic-gate 
23567c478bd9Sstevel@tonic-gate 	buf = safe_malloc(max_scf_fmri_length + 1);
23577c478bd9Sstevel@tonic-gate 
23587c478bd9Sstevel@tonic-gate 	if (scf_instance_to_fmri(wip->inst, buf, max_scf_fmri_length + 1) != -1)
23597c478bd9Sstevel@tonic-gate 		(void) printf(fmt, DETAILED_WIDTH, "fmri", buf);
23607c478bd9Sstevel@tonic-gate 
23617c478bd9Sstevel@tonic-gate 	if (common_name_buf == NULL)
23627c478bd9Sstevel@tonic-gate 		common_name_buf = safe_malloc(max_scf_value_length + 1);
23637c478bd9Sstevel@tonic-gate 
23647c478bd9Sstevel@tonic-gate 	if (inst_get_single_val(wip->inst, SCF_PG_TM_COMMON_NAME, locale,
23657c478bd9Sstevel@tonic-gate 	    SCF_TYPE_USTRING, common_name_buf, max_scf_value_length, 0, 1, 1)
23667c478bd9Sstevel@tonic-gate 	    == 0)
23677c478bd9Sstevel@tonic-gate 		(void) printf(fmt, DETAILED_WIDTH, gettext("name"),
23687c478bd9Sstevel@tonic-gate 		    common_name_buf);
23697c478bd9Sstevel@tonic-gate 	else if (inst_get_single_val(wip->inst, SCF_PG_TM_COMMON_NAME, "C",
23707c478bd9Sstevel@tonic-gate 	    SCF_TYPE_USTRING, common_name_buf, max_scf_value_length, 0, 1, 1)
23717c478bd9Sstevel@tonic-gate 	    == 0)
23727c478bd9Sstevel@tonic-gate 		(void) printf(fmt, DETAILED_WIDTH, gettext("name"),
23737c478bd9Sstevel@tonic-gate 		    common_name_buf);
23747c478bd9Sstevel@tonic-gate 
23757c478bd9Sstevel@tonic-gate 	/*
23767c478bd9Sstevel@tonic-gate 	 * Synthesize an 'enabled' property that hides the enabled_ovr
23777c478bd9Sstevel@tonic-gate 	 * implementation from the user.  If the service has been temporarily
23787c478bd9Sstevel@tonic-gate 	 * set to a state other than its permanent value, alert the user with
23797c478bd9Sstevel@tonic-gate 	 * a '(temporary)' message.
23807c478bd9Sstevel@tonic-gate 	 */
23817c478bd9Sstevel@tonic-gate 	perm = instance_enabled(wip->inst, B_FALSE);
23827c478bd9Sstevel@tonic-gate 	temp = instance_enabled(wip->inst, B_TRUE);
23837c478bd9Sstevel@tonic-gate 	if (temp != -1) {
23847c478bd9Sstevel@tonic-gate 		if (temp != perm)
23857c478bd9Sstevel@tonic-gate 			(void) printf(gettext("%-*s%s (temporary)\n"),
23867c478bd9Sstevel@tonic-gate 			    DETAILED_WIDTH, gettext("enabled"),
23877c478bd9Sstevel@tonic-gate 			    temp ? gettext("true") : gettext("false"));
23887c478bd9Sstevel@tonic-gate 		else
23897c478bd9Sstevel@tonic-gate 			(void) printf(fmt, DETAILED_WIDTH,
23907c478bd9Sstevel@tonic-gate 			    gettext("enabled"), temp ? gettext("true") :
23917c478bd9Sstevel@tonic-gate 			    gettext("false"));
23927c478bd9Sstevel@tonic-gate 	} else if (perm != -1) {
23937c478bd9Sstevel@tonic-gate 		(void) printf(fmt, DETAILED_WIDTH, gettext("enabled"),
23947c478bd9Sstevel@tonic-gate 		    perm ? gettext("true") : gettext("false"));
23957c478bd9Sstevel@tonic-gate 	}
23967c478bd9Sstevel@tonic-gate 
23977c478bd9Sstevel@tonic-gate 	/*
23987c478bd9Sstevel@tonic-gate 	 * Property values may be longer than max_scf_fmri_length, but these
23997c478bd9Sstevel@tonic-gate 	 * shouldn't be, so we'll just reuse buf.  The user can use svcprop if
24007c478bd9Sstevel@tonic-gate 	 * he suspects something fishy.
24017c478bd9Sstevel@tonic-gate 	 */
24027c478bd9Sstevel@tonic-gate 	if (scf_instance_get_pg(wip->inst, SCF_PG_RESTARTER, rpg) != 0) {
24037c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_NOT_FOUND)
24047c478bd9Sstevel@tonic-gate 			scfdie();
24057c478bd9Sstevel@tonic-gate 
24067c478bd9Sstevel@tonic-gate 		scf_pg_destroy(rpg);
24077c478bd9Sstevel@tonic-gate 		rpg = NULL;
24087c478bd9Sstevel@tonic-gate 	}
24097c478bd9Sstevel@tonic-gate 
24107c478bd9Sstevel@tonic-gate 	if (rpg) {
24117c478bd9Sstevel@tonic-gate 		if (pg_get_single_val(rpg, scf_property_state, SCF_TYPE_ASTRING,
24127c478bd9Sstevel@tonic-gate 		    buf, max_scf_fmri_length + 1, 0) == 0)
24137c478bd9Sstevel@tonic-gate 			(void) printf(fmt, DETAILED_WIDTH, gettext("state"),
24147c478bd9Sstevel@tonic-gate 			    buf);
24157c478bd9Sstevel@tonic-gate 
24167c478bd9Sstevel@tonic-gate 		if (pg_get_single_val(rpg, scf_property_next_state,
24177c478bd9Sstevel@tonic-gate 		    SCF_TYPE_ASTRING, buf, max_scf_fmri_length + 1, 0) == 0)
24187c478bd9Sstevel@tonic-gate 			(void) printf(fmt, DETAILED_WIDTH,
24197c478bd9Sstevel@tonic-gate 			    gettext("next_state"), buf);
24207c478bd9Sstevel@tonic-gate 
24217c478bd9Sstevel@tonic-gate 		if (pg_get_single_val(rpg, SCF_PROPERTY_STATE_TIMESTAMP,
24227c478bd9Sstevel@tonic-gate 		    SCF_TYPE_TIME, &tv, NULL, 0) == 0) {
24237c478bd9Sstevel@tonic-gate 			stime = tv.tv_sec;
24247c478bd9Sstevel@tonic-gate 			tmp = localtime(&stime);
24257c478bd9Sstevel@tonic-gate 			for (tbsz = 50; ; tbsz *= 2) {
24267c478bd9Sstevel@tonic-gate 				timebuf = safe_malloc(tbsz);
24277c478bd9Sstevel@tonic-gate 				if (strftime(timebuf, tbsz, NULL, tmp) != 0)
24287c478bd9Sstevel@tonic-gate 					break;
24297c478bd9Sstevel@tonic-gate 				free(timebuf);
24307c478bd9Sstevel@tonic-gate 			}
24317c478bd9Sstevel@tonic-gate 			(void) printf(fmt, DETAILED_WIDTH,
24327c478bd9Sstevel@tonic-gate 			    gettext("state_time"),
24337c478bd9Sstevel@tonic-gate 			    timebuf);
24347c478bd9Sstevel@tonic-gate 			free(timebuf);
24357c478bd9Sstevel@tonic-gate 		}
24367c478bd9Sstevel@tonic-gate 
24377c478bd9Sstevel@tonic-gate 		if (pg_get_single_val(rpg, SCF_PROPERTY_ALT_LOGFILE,
24387c478bd9Sstevel@tonic-gate 		    SCF_TYPE_ASTRING, buf, max_scf_fmri_length + 1, 0) == 0)
2439b7185059Srm88369 			(void) printf(fmt, DETAILED_WIDTH,
2440b7185059Srm88369 			    gettext("alt_logfile"), buf);
24417c478bd9Sstevel@tonic-gate 
24427c478bd9Sstevel@tonic-gate 		if (pg_get_single_val(rpg, SCF_PROPERTY_LOGFILE,
24437c478bd9Sstevel@tonic-gate 		    SCF_TYPE_ASTRING, buf, max_scf_fmri_length + 1, 0) == 0)
2444b7185059Srm88369 			(void) printf(fmt, DETAILED_WIDTH, gettext("logfile"),
2445b7185059Srm88369 			    buf);
2446b7185059Srm88369 	}
24477c478bd9Sstevel@tonic-gate 
24487c478bd9Sstevel@tonic-gate 	if (inst_get_single_val(wip->inst, SCF_PG_GENERAL,
24497c478bd9Sstevel@tonic-gate 	    SCF_PROPERTY_RESTARTER, SCF_TYPE_ASTRING, buf,
24507c478bd9Sstevel@tonic-gate 	    max_scf_fmri_length + 1, 0, 0, 1) == 0)
24517c478bd9Sstevel@tonic-gate 		(void) printf(fmt, DETAILED_WIDTH, gettext("restarter"), buf);
24527c478bd9Sstevel@tonic-gate 	else
24537c478bd9Sstevel@tonic-gate 		(void) printf(fmt, DETAILED_WIDTH, gettext("restarter"),
24547c478bd9Sstevel@tonic-gate 		    SCF_SERVICE_STARTD);
24557c478bd9Sstevel@tonic-gate 
24567c478bd9Sstevel@tonic-gate 	free(buf);
24577c478bd9Sstevel@tonic-gate 
245894501b61Sskamm 	/*
245994501b61Sskamm 	 * Use the restarter specific routine to print the ctids, if available.
246094501b61Sskamm 	 * If restarter specific action is available and it fails, then die.
246194501b61Sskamm 	 */
246294501b61Sskamm 	restarter_ret = ctids_by_restarter(wip, &c, 1, 0,
246394501b61Sskamm 	    &restarter_spec, print_ctid_header, print_ctid_detailed);
246494501b61Sskamm 	if (restarter_spec == 1) {
246594501b61Sskamm 		if (restarter_ret != 0)
246694501b61Sskamm 			uu_die(gettext("Unable to get restarter for %s"),
246794501b61Sskamm 			    wip->fmri);
246894501b61Sskamm 		goto restarter_common;
246994501b61Sskamm 	}
247094501b61Sskamm 
24717c478bd9Sstevel@tonic-gate 	if (rpg) {
24727c478bd9Sstevel@tonic-gate 		scf_iter_t *iter;
24737c478bd9Sstevel@tonic-gate 
24747c478bd9Sstevel@tonic-gate 		if ((iter = scf_iter_create(h)) == NULL)
24757c478bd9Sstevel@tonic-gate 			scfdie();
24767c478bd9Sstevel@tonic-gate 
24777c478bd9Sstevel@tonic-gate 		if (scf_pg_get_property(rpg, scf_property_contract, g_prop) ==
24787c478bd9Sstevel@tonic-gate 		    0) {
24797c478bd9Sstevel@tonic-gate 			if (scf_property_is_type(g_prop, SCF_TYPE_COUNT) == 0) {
248094501b61Sskamm 
248194501b61Sskamm 				/* Callback to print ctid header */
248294501b61Sskamm 				print_ctid_header();
24837c478bd9Sstevel@tonic-gate 
24847c478bd9Sstevel@tonic-gate 				if (scf_iter_property_values(iter, g_prop) != 0)
24857c478bd9Sstevel@tonic-gate 					scfdie();
24867c478bd9Sstevel@tonic-gate 
24877c478bd9Sstevel@tonic-gate 				for (;;) {
24887c478bd9Sstevel@tonic-gate 					ret = scf_iter_next_value(iter, g_val);
24897c478bd9Sstevel@tonic-gate 					if (ret == -1)
24907c478bd9Sstevel@tonic-gate 						scfdie();
24917c478bd9Sstevel@tonic-gate 					if (ret == 0)
24927c478bd9Sstevel@tonic-gate 						break;
24937c478bd9Sstevel@tonic-gate 
24947c478bd9Sstevel@tonic-gate 					if (scf_value_get_count(g_val, &c) != 0)
24957c478bd9Sstevel@tonic-gate 						scfdie();
249694501b61Sskamm 
249794501b61Sskamm 					/* Callback to print contract id. */
249894501b61Sskamm 					print_ctid_detailed(c);
24997c478bd9Sstevel@tonic-gate 				}
25007c478bd9Sstevel@tonic-gate 
25017c478bd9Sstevel@tonic-gate 				(void) putchar('\n');
25027c478bd9Sstevel@tonic-gate 			} else {
25037c478bd9Sstevel@tonic-gate 				if (scf_error() != SCF_ERROR_TYPE_MISMATCH)
25047c478bd9Sstevel@tonic-gate 					scfdie();
25057c478bd9Sstevel@tonic-gate 			}
25067c478bd9Sstevel@tonic-gate 		} else {
25077c478bd9Sstevel@tonic-gate 			if (scf_error() != SCF_ERROR_NOT_FOUND)
25087c478bd9Sstevel@tonic-gate 				scfdie();
25097c478bd9Sstevel@tonic-gate 		}
25107c478bd9Sstevel@tonic-gate 
25117c478bd9Sstevel@tonic-gate 		scf_iter_destroy(iter);
25127c478bd9Sstevel@tonic-gate 	} else {
25137c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_NOT_FOUND)
25147c478bd9Sstevel@tonic-gate 			scfdie();
25157c478bd9Sstevel@tonic-gate 	}
25167c478bd9Sstevel@tonic-gate 
251794501b61Sskamm restarter_common:
25187c478bd9Sstevel@tonic-gate 	scf_pg_destroy(rpg);
25197c478bd9Sstevel@tonic-gate 
25207c478bd9Sstevel@tonic-gate 	/* Dependencies. */
25217c478bd9Sstevel@tonic-gate 	if ((pg_iter = scf_iter_create(h)) == NULL)
25227c478bd9Sstevel@tonic-gate 		scfdie();
25237c478bd9Sstevel@tonic-gate 
25247c478bd9Sstevel@tonic-gate 	snap = get_running_snapshot(wip->inst);
25257c478bd9Sstevel@tonic-gate 
25267c478bd9Sstevel@tonic-gate 	if (scf_iter_instance_pgs_typed_composed(pg_iter, wip->inst, snap,
25277c478bd9Sstevel@tonic-gate 	    SCF_GROUP_DEPENDENCY) != SCF_SUCCESS)
25287c478bd9Sstevel@tonic-gate 		scfdie();
25297c478bd9Sstevel@tonic-gate 
25307c478bd9Sstevel@tonic-gate 	while ((ret = scf_iter_next_pg(pg_iter, g_pg)) == 1)
25317c478bd9Sstevel@tonic-gate 		print_detailed_dependency(g_pg);
25327c478bd9Sstevel@tonic-gate 	if (ret == -1)
25337c478bd9Sstevel@tonic-gate 		scfdie();
25347c478bd9Sstevel@tonic-gate 
25357c478bd9Sstevel@tonic-gate 	scf_iter_destroy(pg_iter);
25367c478bd9Sstevel@tonic-gate 
25377c478bd9Sstevel@tonic-gate 	if (opt_processes)
253894501b61Sskamm 		detailed_list_processes(wip);
25397c478bd9Sstevel@tonic-gate 
25401f6eb021SLiane Praza 	/* "application" type property groups */
25411f6eb021SLiane Praza 	if (opt_verbose == 1)
25421f6eb021SLiane Praza 		print_application_properties(wip, snap);
25431f6eb021SLiane Praza 
25441f6eb021SLiane Praza 	scf_snapshot_destroy(snap);
25451f6eb021SLiane Praza 
25467c478bd9Sstevel@tonic-gate 	return (0);
25477c478bd9Sstevel@tonic-gate }
25487c478bd9Sstevel@tonic-gate 
2549*f6e214c7SGavin Maltby int
2550*f6e214c7SGavin Maltby qsort_str_compare(const void *p1, const void *p2)
2551*f6e214c7SGavin Maltby {
2552*f6e214c7SGavin Maltby 	return (strcmp((const char *)p1, (const char *)p2));
2553*f6e214c7SGavin Maltby }
2554*f6e214c7SGavin Maltby 
2555*f6e214c7SGavin Maltby /*
2556*f6e214c7SGavin Maltby  * get_notify_param_classes()
2557*f6e214c7SGavin Maltby  * return the fma classes that don't have a tag in fma_tags[], otherwise NULL
2558*f6e214c7SGavin Maltby  */
2559*f6e214c7SGavin Maltby static char **
2560*f6e214c7SGavin Maltby get_notify_param_classes()
2561*f6e214c7SGavin Maltby {
2562*f6e214c7SGavin Maltby 	scf_handle_t		*h = _scf_handle_create_and_bind(SCF_VERSION);
2563*f6e214c7SGavin Maltby 	scf_instance_t		*inst = scf_instance_create(h);
2564*f6e214c7SGavin Maltby 	scf_snapshot_t		*snap = scf_snapshot_create(h);
2565*f6e214c7SGavin Maltby 	scf_snaplevel_t		*slvl = scf_snaplevel_create(h);
2566*f6e214c7SGavin Maltby 	scf_propertygroup_t	*pg = scf_pg_create(h);
2567*f6e214c7SGavin Maltby 	scf_iter_t		*iter = scf_iter_create(h);
2568*f6e214c7SGavin Maltby 	int size = 4;
2569*f6e214c7SGavin Maltby 	int n = 0;
2570*f6e214c7SGavin Maltby 	size_t sz = scf_limit(SCF_LIMIT_MAX_NAME_LENGTH) + 1;
2571*f6e214c7SGavin Maltby 	int err;
2572*f6e214c7SGavin Maltby 	char *pgname = safe_malloc(sz);
2573*f6e214c7SGavin Maltby 	char **buf = safe_malloc(size * sizeof (char *));
2574*f6e214c7SGavin Maltby 
2575*f6e214c7SGavin Maltby 	if (h == NULL || inst == NULL || snap == NULL || slvl == NULL ||
2576*f6e214c7SGavin Maltby 	    pg == NULL || iter == NULL) {
2577*f6e214c7SGavin Maltby 		uu_die(gettext("Failed object creation: %s\n"),
2578*f6e214c7SGavin Maltby 		    scf_strerror(scf_error()));
2579*f6e214c7SGavin Maltby 	}
2580*f6e214c7SGavin Maltby 
2581*f6e214c7SGavin Maltby 	if (scf_handle_decode_fmri(h, SCF_NOTIFY_PARAMS_INST, NULL, NULL, inst,
2582*f6e214c7SGavin Maltby 	    NULL, NULL, SCF_DECODE_FMRI_EXACT) != 0)
2583*f6e214c7SGavin Maltby 		uu_die(gettext("Failed to decode %s: %s\n"),
2584*f6e214c7SGavin Maltby 		    SCF_NOTIFY_PARAMS_INST, scf_strerror(scf_error()));
2585*f6e214c7SGavin Maltby 
2586*f6e214c7SGavin Maltby 	if (scf_instance_get_snapshot(inst, "running", snap) != 0)
2587*f6e214c7SGavin Maltby 		uu_die(gettext("Failed to get snapshot: %s\n"),
2588*f6e214c7SGavin Maltby 		    scf_strerror(scf_error()));
2589*f6e214c7SGavin Maltby 
2590*f6e214c7SGavin Maltby 	if (scf_snapshot_get_base_snaplevel(snap, slvl) != 0)
2591*f6e214c7SGavin Maltby 		uu_die(gettext("Failed to get base snaplevel: %s\n"),
2592*f6e214c7SGavin Maltby 		    scf_strerror(scf_error()));
2593*f6e214c7SGavin Maltby 
2594*f6e214c7SGavin Maltby 	if (scf_iter_snaplevel_pgs_typed(iter, slvl,
2595*f6e214c7SGavin Maltby 	    SCF_NOTIFY_PARAMS_PG_TYPE) != 0)
2596*f6e214c7SGavin Maltby 		uu_die(gettext("Failed to get iterator: %s\n"),
2597*f6e214c7SGavin Maltby 		    scf_strerror(scf_error()));
2598*f6e214c7SGavin Maltby 
2599*f6e214c7SGavin Maltby 	while ((err = scf_iter_next_pg(iter, pg)) == 1) {
2600*f6e214c7SGavin Maltby 		char *c;
2601*f6e214c7SGavin Maltby 
2602*f6e214c7SGavin Maltby 		if (scf_pg_get_name(pg, pgname, sz) == -1)
2603*f6e214c7SGavin Maltby 			uu_die(gettext("Failed to get pg name: %s\n"),
2604*f6e214c7SGavin Maltby 			    scf_strerror(scf_error()));
2605*f6e214c7SGavin Maltby 		if ((c = strrchr(pgname, ',')) != NULL)
2606*f6e214c7SGavin Maltby 			*c = '\0';
2607*f6e214c7SGavin Maltby 		if (has_fma_tag(pgname))
2608*f6e214c7SGavin Maltby 			continue;
2609*f6e214c7SGavin Maltby 		if (!is_fma_token(pgname))
2610*f6e214c7SGavin Maltby 			/*
2611*f6e214c7SGavin Maltby 			 * We don't emmit a warning here so that we don't
2612*f6e214c7SGavin Maltby 			 * pollute the output
2613*f6e214c7SGavin Maltby 			 */
2614*f6e214c7SGavin Maltby 			continue;
2615*f6e214c7SGavin Maltby 
2616*f6e214c7SGavin Maltby 		if (n + 1 >= size) {
2617*f6e214c7SGavin Maltby 			size *= 2;
2618*f6e214c7SGavin Maltby 			buf = realloc(buf, size * sizeof (char *));
2619*f6e214c7SGavin Maltby 			if (buf == NULL)
2620*f6e214c7SGavin Maltby 				uu_die(gettext("Out of memory.\n"));
2621*f6e214c7SGavin Maltby 		}
2622*f6e214c7SGavin Maltby 		buf[n] = safe_strdup(pgname);
2623*f6e214c7SGavin Maltby 		++n;
2624*f6e214c7SGavin Maltby 	}
2625*f6e214c7SGavin Maltby 	/*
2626*f6e214c7SGavin Maltby 	 * NULL terminate buf
2627*f6e214c7SGavin Maltby 	 */
2628*f6e214c7SGavin Maltby 	buf[n] = NULL;
2629*f6e214c7SGavin Maltby 	if (err == -1)
2630*f6e214c7SGavin Maltby 		uu_die(gettext("Failed to iterate pgs: %s\n"),
2631*f6e214c7SGavin Maltby 		    scf_strerror(scf_error()));
2632*f6e214c7SGavin Maltby 
2633*f6e214c7SGavin Maltby 	/* sort the classes */
2634*f6e214c7SGavin Maltby 	qsort((void *)buf, n, sizeof (char *), qsort_str_compare);
2635*f6e214c7SGavin Maltby 
2636*f6e214c7SGavin Maltby 	free(pgname);
2637*f6e214c7SGavin Maltby 	scf_iter_destroy(iter);
2638*f6e214c7SGavin Maltby 	scf_pg_destroy(pg);
2639*f6e214c7SGavin Maltby 	scf_snaplevel_destroy(slvl);
2640*f6e214c7SGavin Maltby 	scf_snapshot_destroy(snap);
2641*f6e214c7SGavin Maltby 	scf_instance_destroy(inst);
2642*f6e214c7SGavin Maltby 	scf_handle_destroy(h);
2643*f6e214c7SGavin Maltby 
2644*f6e214c7SGavin Maltby 	return (buf);
2645*f6e214c7SGavin Maltby }
2646*f6e214c7SGavin Maltby 
2647*f6e214c7SGavin Maltby /*
2648*f6e214c7SGavin Maltby  * get_fma_notify_params()
2649*f6e214c7SGavin Maltby  * populates an nvlist_t with notifycation parameters for a given FMA class
2650*f6e214c7SGavin Maltby  * returns 0 if the nvlist is populated, 1 otherwise;
2651*f6e214c7SGavin Maltby  */
2652*f6e214c7SGavin Maltby int
2653*f6e214c7SGavin Maltby get_fma_notify_params(nvlist_t *nvl, const char *class)
2654*f6e214c7SGavin Maltby {
2655*f6e214c7SGavin Maltby 	if (_scf_get_fma_notify_params(class, nvl, 0) != 0) {
2656*f6e214c7SGavin Maltby 		/*
2657*f6e214c7SGavin Maltby 		 * if the preferences have just been deleted
2658*f6e214c7SGavin Maltby 		 * or does not exist, just skip.
2659*f6e214c7SGavin Maltby 		 */
2660*f6e214c7SGavin Maltby 		if (scf_error() != SCF_ERROR_NOT_FOUND &&
2661*f6e214c7SGavin Maltby 		    scf_error() != SCF_ERROR_DELETED)
2662*f6e214c7SGavin Maltby 			uu_warn(gettext(
2663*f6e214c7SGavin Maltby 			    "Failed get_fma_notify_params %s\n"),
2664*f6e214c7SGavin Maltby 			    scf_strerror(scf_error()));
2665*f6e214c7SGavin Maltby 
2666*f6e214c7SGavin Maltby 		return (1);
2667*f6e214c7SGavin Maltby 	}
2668*f6e214c7SGavin Maltby 
2669*f6e214c7SGavin Maltby 	return (0);
2670*f6e214c7SGavin Maltby }
2671*f6e214c7SGavin Maltby 
2672*f6e214c7SGavin Maltby /*
2673*f6e214c7SGavin Maltby  * print_notify_fma()
2674*f6e214c7SGavin Maltby  * outputs the notification paramets of FMA events.
2675*f6e214c7SGavin Maltby  * It first outputs classes in fma_tags[], then outputs the other classes
2676*f6e214c7SGavin Maltby  * sorted alphabetically
2677*f6e214c7SGavin Maltby  */
2678*f6e214c7SGavin Maltby static void
2679*f6e214c7SGavin Maltby print_notify_fma(void)
2680*f6e214c7SGavin Maltby {
2681*f6e214c7SGavin Maltby 	nvlist_t *nvl;
2682*f6e214c7SGavin Maltby 	char **tmp = NULL;
2683*f6e214c7SGavin Maltby 	char **classes, *p;
2684*f6e214c7SGavin Maltby 	const char *class;
2685*f6e214c7SGavin Maltby 	uint32_t i;
2686*f6e214c7SGavin Maltby 
2687*f6e214c7SGavin Maltby 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
2688*f6e214c7SGavin Maltby 		uu_die(gettext("Out of memory.\n"));
2689*f6e214c7SGavin Maltby 
2690*f6e214c7SGavin Maltby 	for (i = 0; (class = get_fma_class(i)) != NULL; ++i) {
2691*f6e214c7SGavin Maltby 		if (get_fma_notify_params(nvl, class) == 0)
2692*f6e214c7SGavin Maltby 			listnotify_print(nvl, get_fma_tag(i));
2693*f6e214c7SGavin Maltby 	}
2694*f6e214c7SGavin Maltby 
2695*f6e214c7SGavin Maltby 	if ((classes = get_notify_param_classes()) == NULL)
2696*f6e214c7SGavin Maltby 		goto cleanup;
2697*f6e214c7SGavin Maltby 
2698*f6e214c7SGavin Maltby 	tmp = classes;
2699*f6e214c7SGavin Maltby 	for (p = *tmp; p; ++tmp, p = *tmp) {
2700*f6e214c7SGavin Maltby 		if (get_fma_notify_params(nvl, p) == 0)
2701*f6e214c7SGavin Maltby 			listnotify_print(nvl, re_tag(p));
2702*f6e214c7SGavin Maltby 
2703*f6e214c7SGavin Maltby 		free(p);
2704*f6e214c7SGavin Maltby 	}
2705*f6e214c7SGavin Maltby 
2706*f6e214c7SGavin Maltby 	free(classes);
2707*f6e214c7SGavin Maltby 
2708*f6e214c7SGavin Maltby cleanup:
2709*f6e214c7SGavin Maltby 	nvlist_free(nvl);
2710*f6e214c7SGavin Maltby }
2711*f6e214c7SGavin Maltby 
2712*f6e214c7SGavin Maltby /*
2713*f6e214c7SGavin Maltby  * print_notify_fmri()
2714*f6e214c7SGavin Maltby  * prints notifycation parameters for an SMF instance.
2715*f6e214c7SGavin Maltby  */
2716*f6e214c7SGavin Maltby static void
2717*f6e214c7SGavin Maltby print_notify_fmri(const char *fmri)
2718*f6e214c7SGavin Maltby {
2719*f6e214c7SGavin Maltby 	nvlist_t *nvl;
2720*f6e214c7SGavin Maltby 
2721*f6e214c7SGavin Maltby 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
2722*f6e214c7SGavin Maltby 		uu_die(gettext("Out of memory.\n"));
2723*f6e214c7SGavin Maltby 
2724*f6e214c7SGavin Maltby 	if (_scf_get_svc_notify_params(fmri, nvl, SCF_TRANSITION_ALL, 0, 0) !=
2725*f6e214c7SGavin Maltby 	    SCF_SUCCESS) {
2726*f6e214c7SGavin Maltby 		if (scf_error() != SCF_ERROR_NOT_FOUND &&
2727*f6e214c7SGavin Maltby 		    scf_error() != SCF_ERROR_DELETED)
2728*f6e214c7SGavin Maltby 			uu_warn(gettext(
2729*f6e214c7SGavin Maltby 			    "Failed _scf_get_svc_notify_params: %s\n"),
2730*f6e214c7SGavin Maltby 			    scf_strerror(scf_error()));
2731*f6e214c7SGavin Maltby 	} else {
2732*f6e214c7SGavin Maltby 		if (strcmp(SCF_INSTANCE_GLOBAL, fmri) == 0)
2733*f6e214c7SGavin Maltby 			safe_printf(
2734*f6e214c7SGavin Maltby 			    gettext("System wide notification parameters:\n"));
2735*f6e214c7SGavin Maltby 		safe_printf("%s:\n", fmri);
2736*f6e214c7SGavin Maltby 		listnotify_print(nvl, NULL);
2737*f6e214c7SGavin Maltby 	}
2738*f6e214c7SGavin Maltby 	nvlist_free(nvl);
2739*f6e214c7SGavin Maltby }
2740*f6e214c7SGavin Maltby 
2741*f6e214c7SGavin Maltby /*
2742*f6e214c7SGavin Maltby  * print_notify_special()
2743*f6e214c7SGavin Maltby  * prints notification parameters for FMA events and system wide SMF state
2744*f6e214c7SGavin Maltby  * transitions parameters
2745*f6e214c7SGavin Maltby  */
2746*f6e214c7SGavin Maltby static void
2747*f6e214c7SGavin Maltby print_notify_special()
2748*f6e214c7SGavin Maltby {
2749*f6e214c7SGavin Maltby 	safe_printf("Notification parameters for FMA Events\n");
2750*f6e214c7SGavin Maltby 	print_notify_fma();
2751*f6e214c7SGavin Maltby 	print_notify_fmri(SCF_INSTANCE_GLOBAL);
2752*f6e214c7SGavin Maltby }
2753*f6e214c7SGavin Maltby 
2754*f6e214c7SGavin Maltby /*
2755*f6e214c7SGavin Maltby  * print_notify()
2756*f6e214c7SGavin Maltby  * callback function to print notification parameters for SMF state transition
2757*f6e214c7SGavin Maltby  * instances. It skips global and notify-params instances as they should be
2758*f6e214c7SGavin Maltby  * printed by print_notify_special()
2759*f6e214c7SGavin Maltby  */
2760*f6e214c7SGavin Maltby /* ARGSUSED */
2761*f6e214c7SGavin Maltby static int
2762*f6e214c7SGavin Maltby print_notify(void *unused, scf_walkinfo_t *wip)
2763*f6e214c7SGavin Maltby {
2764*f6e214c7SGavin Maltby 	if (strcmp(SCF_INSTANCE_GLOBAL, wip->fmri) == 0 ||
2765*f6e214c7SGavin Maltby 	    strcmp(SCF_NOTIFY_PARAMS_INST, wip->fmri) == 0)
2766*f6e214c7SGavin Maltby 		return (0);
2767*f6e214c7SGavin Maltby 
2768*f6e214c7SGavin Maltby 	print_notify_fmri(wip->fmri);
2769*f6e214c7SGavin Maltby 
2770*f6e214c7SGavin Maltby 	return (0);
2771*f6e214c7SGavin Maltby }
2772*f6e214c7SGavin Maltby 
27737c478bd9Sstevel@tonic-gate /*
27747c478bd9Sstevel@tonic-gate  * Append a one-lined description of each process in inst's contract(s) and
27757c478bd9Sstevel@tonic-gate  * return the augmented string.
27767c478bd9Sstevel@tonic-gate  */
27777c478bd9Sstevel@tonic-gate static char *
277894501b61Sskamm add_processes(scf_walkinfo_t *wip, char *line, scf_propertygroup_t *lpg)
27797c478bd9Sstevel@tonic-gate {
27807c478bd9Sstevel@tonic-gate 	pid_t *pids = NULL;
27817c478bd9Sstevel@tonic-gate 	uint_t i, n = 0;
27827c478bd9Sstevel@tonic-gate 
27837c478bd9Sstevel@tonic-gate 	if (lpg == NULL) {
278494501b61Sskamm 		if (instance_processes(wip->inst, wip->fmri, &pids, &n) != 0)
27857c478bd9Sstevel@tonic-gate 			return (line);
27867c478bd9Sstevel@tonic-gate 	} else {
278794501b61Sskamm 		/* Legacy services */
27887c478bd9Sstevel@tonic-gate 		scf_iter_t *iter;
27897c478bd9Sstevel@tonic-gate 
27907c478bd9Sstevel@tonic-gate 		if ((iter = scf_iter_create(h)) == NULL)
27917c478bd9Sstevel@tonic-gate 			scfdie();
27927c478bd9Sstevel@tonic-gate 
27937c478bd9Sstevel@tonic-gate 		(void) propvals_to_pids(lpg, scf_property_contract, &pids, &n,
27947c478bd9Sstevel@tonic-gate 		    g_prop, g_val, iter);
27957c478bd9Sstevel@tonic-gate 
27967c478bd9Sstevel@tonic-gate 		scf_iter_destroy(iter);
27977c478bd9Sstevel@tonic-gate 	}
27987c478bd9Sstevel@tonic-gate 
27997c478bd9Sstevel@tonic-gate 	if (n == 0)
28007c478bd9Sstevel@tonic-gate 		return (line);
28017c478bd9Sstevel@tonic-gate 
28027c478bd9Sstevel@tonic-gate 	qsort(pids, n, sizeof (*pids), pidcmp);
28037c478bd9Sstevel@tonic-gate 
28047c478bd9Sstevel@tonic-gate 	for (i = 0; i < n; ++i) {
28057c478bd9Sstevel@tonic-gate 		char *cp, stime[9];
28067c478bd9Sstevel@tonic-gate 		psinfo_t psi;
28077c478bd9Sstevel@tonic-gate 		struct tm *tm;
28087c478bd9Sstevel@tonic-gate 		int len = 1 + 15 + 8 + 3 + 6 + 1 + PRFNSZ;
28097c478bd9Sstevel@tonic-gate 
28107c478bd9Sstevel@tonic-gate 		if (get_psinfo(pids[i], &psi) != 0)
28117c478bd9Sstevel@tonic-gate 			continue;
28127c478bd9Sstevel@tonic-gate 
28137c478bd9Sstevel@tonic-gate 		line = realloc(line, strlen(line) + len);
28147c478bd9Sstevel@tonic-gate 		if (line == NULL)
28157c478bd9Sstevel@tonic-gate 			uu_die(gettext("Out of memory.\n"));
28167c478bd9Sstevel@tonic-gate 
28177c478bd9Sstevel@tonic-gate 		cp = strchr(line, '\0');
28187c478bd9Sstevel@tonic-gate 
28197c478bd9Sstevel@tonic-gate 		tm = localtime(&psi.pr_start.tv_sec);
28207c478bd9Sstevel@tonic-gate 
28217c478bd9Sstevel@tonic-gate 		/*
28227c478bd9Sstevel@tonic-gate 		 * Print time if started within the past 24 hours, print date
28237c478bd9Sstevel@tonic-gate 		 * if within the past 12 months, print year if started greater
28247c478bd9Sstevel@tonic-gate 		 * than 12 months ago.
28257c478bd9Sstevel@tonic-gate 		 */
28267c478bd9Sstevel@tonic-gate 		if (now - psi.pr_start.tv_sec < 24 * 60 * 60)
28273eae19d9Swesolows 			(void) strftime(stime, sizeof (stime),
28283eae19d9Swesolows 			    gettext(FORMAT_TIME), tm);
28297c478bd9Sstevel@tonic-gate 		else if (now - psi.pr_start.tv_sec < 12 * 30 * 24 * 60 * 60)
28303eae19d9Swesolows 			(void) strftime(stime, sizeof (stime),
28313eae19d9Swesolows 			    gettext(FORMAT_DATE), tm);
28327c478bd9Sstevel@tonic-gate 		else
28333eae19d9Swesolows 			(void) strftime(stime, sizeof (stime),
28343eae19d9Swesolows 			    gettext(FORMAT_YEAR), tm);
28357c478bd9Sstevel@tonic-gate 
28367c478bd9Sstevel@tonic-gate 		(void) snprintf(cp, len, "\n               %-8s   %6ld %.*s",
28377c478bd9Sstevel@tonic-gate 		    stime, pids[i], PRFNSZ, psi.pr_fname);
28387c478bd9Sstevel@tonic-gate 	}
28397c478bd9Sstevel@tonic-gate 
28407c478bd9Sstevel@tonic-gate 	free(pids);
28417c478bd9Sstevel@tonic-gate 
28427c478bd9Sstevel@tonic-gate 	return (line);
28437c478bd9Sstevel@tonic-gate }
28447c478bd9Sstevel@tonic-gate 
28457c478bd9Sstevel@tonic-gate /*ARGSUSED*/
28467c478bd9Sstevel@tonic-gate static int
28477c478bd9Sstevel@tonic-gate list_instance(void *unused, scf_walkinfo_t *wip)
28487c478bd9Sstevel@tonic-gate {
28497c478bd9Sstevel@tonic-gate 	struct avl_string *lp;
28507c478bd9Sstevel@tonic-gate 	char *cp;
28517c478bd9Sstevel@tonic-gate 	int i;
28527c478bd9Sstevel@tonic-gate 	uu_avl_index_t idx;
28537c478bd9Sstevel@tonic-gate 
28547c478bd9Sstevel@tonic-gate 	/*
28557c478bd9Sstevel@tonic-gate 	 * If the user has specified a restarter, check for a match first
28567c478bd9Sstevel@tonic-gate 	 */
28577c478bd9Sstevel@tonic-gate 	if (restarters != NULL) {
28587c478bd9Sstevel@tonic-gate 		struct pfmri_list *rest;
28597c478bd9Sstevel@tonic-gate 		int match;
28607c478bd9Sstevel@tonic-gate 		char *restarter_fmri;
28617c478bd9Sstevel@tonic-gate 		const char *scope_name, *svc_name, *inst_name, *pg_name;
28627c478bd9Sstevel@tonic-gate 
28637c478bd9Sstevel@tonic-gate 		/* legacy services don't have restarters */
28647c478bd9Sstevel@tonic-gate 		if (wip->pg != NULL)
28657c478bd9Sstevel@tonic-gate 			return (0);
28667c478bd9Sstevel@tonic-gate 
28677c478bd9Sstevel@tonic-gate 		restarter_fmri = safe_malloc(max_scf_fmri_length + 1);
28687c478bd9Sstevel@tonic-gate 
28697c478bd9Sstevel@tonic-gate 		if (inst_get_single_val(wip->inst, SCF_PG_GENERAL,
28707c478bd9Sstevel@tonic-gate 		    SCF_PROPERTY_RESTARTER, SCF_TYPE_ASTRING, restarter_fmri,
28717c478bd9Sstevel@tonic-gate 		    max_scf_fmri_length + 1, 0, 0, 1) != 0)
28727c478bd9Sstevel@tonic-gate 			(void) strcpy(restarter_fmri, SCF_SERVICE_STARTD);
28737c478bd9Sstevel@tonic-gate 
28747c478bd9Sstevel@tonic-gate 		if (scf_parse_svc_fmri(restarter_fmri, &scope_name, &svc_name,
28757c478bd9Sstevel@tonic-gate 		    &inst_name, &pg_name, NULL) != SCF_SUCCESS) {
28767c478bd9Sstevel@tonic-gate 			free(restarter_fmri);
28777c478bd9Sstevel@tonic-gate 			return (0);
28787c478bd9Sstevel@tonic-gate 		}
28797c478bd9Sstevel@tonic-gate 
28807c478bd9Sstevel@tonic-gate 		match = 0;
28817c478bd9Sstevel@tonic-gate 		for (rest = restarters; rest != NULL; rest = rest->next) {
28827c478bd9Sstevel@tonic-gate 			if (strcmp(rest->scope, scope_name) == 0 &&
28837c478bd9Sstevel@tonic-gate 			    strcmp(rest->service, svc_name) == 0 &&
28847c478bd9Sstevel@tonic-gate 			    strcmp(rest->instance, inst_name) == 0)
28857c478bd9Sstevel@tonic-gate 				match = 1;
28867c478bd9Sstevel@tonic-gate 		}
28877c478bd9Sstevel@tonic-gate 
28887c478bd9Sstevel@tonic-gate 		free(restarter_fmri);
28897c478bd9Sstevel@tonic-gate 
28907c478bd9Sstevel@tonic-gate 		if (!match)
28917c478bd9Sstevel@tonic-gate 			return (0);
28927c478bd9Sstevel@tonic-gate 	}
28937c478bd9Sstevel@tonic-gate 
28947c478bd9Sstevel@tonic-gate 	if (wip->pg == NULL && ht_buckets != NULL && ht_add(wip->fmri)) {
28957c478bd9Sstevel@tonic-gate 		/* It was already there. */
28967c478bd9Sstevel@tonic-gate 		return (0);
28977c478bd9Sstevel@tonic-gate 	}
28987c478bd9Sstevel@tonic-gate 
28997c478bd9Sstevel@tonic-gate 	lp = safe_malloc(sizeof (*lp));
29007c478bd9Sstevel@tonic-gate 
29017c478bd9Sstevel@tonic-gate 	lp->str = NULL;
29027c478bd9Sstevel@tonic-gate 	for (i = 0; i < opt_cnum; ++i) {
29037c478bd9Sstevel@tonic-gate 		columns[opt_columns[i]].sprint(&lp->str, wip);
29047c478bd9Sstevel@tonic-gate 	}
29057c478bd9Sstevel@tonic-gate 	cp = lp->str + strlen(lp->str);
29067c478bd9Sstevel@tonic-gate 	cp--;
29077c478bd9Sstevel@tonic-gate 	while (*cp == ' ')
29087c478bd9Sstevel@tonic-gate 		cp--;
29097c478bd9Sstevel@tonic-gate 	*(cp+1) = '\0';
29107c478bd9Sstevel@tonic-gate 
29117c478bd9Sstevel@tonic-gate 	/* If we're supposed to list the processes, too, do that now. */
29127c478bd9Sstevel@tonic-gate 	if (opt_processes)
291394501b61Sskamm 		lp->str = add_processes(wip, lp->str, wip->pg);
29147c478bd9Sstevel@tonic-gate 
29157c478bd9Sstevel@tonic-gate 	/* Create the sort key. */
29167c478bd9Sstevel@tonic-gate 	cp = lp->key = safe_malloc(sortkey_sz);
29177c478bd9Sstevel@tonic-gate 	for (i = 0; i < opt_snum; ++i) {
29187c478bd9Sstevel@tonic-gate 		int j = opt_sort[i] & 0xff;
29197c478bd9Sstevel@tonic-gate 
29207c478bd9Sstevel@tonic-gate 		assert(columns[j].get_sortkey != NULL);
29217c478bd9Sstevel@tonic-gate 		columns[j].get_sortkey(cp, opt_sort[i] & ~0xff, wip);
29227c478bd9Sstevel@tonic-gate 		cp += columns[j].sortkey_width;
29237c478bd9Sstevel@tonic-gate 	}
29247c478bd9Sstevel@tonic-gate 
29257c478bd9Sstevel@tonic-gate 	/* Insert into AVL tree. */
29267c478bd9Sstevel@tonic-gate 	uu_avl_node_init(lp, &lp->node, lines_pool);
29277c478bd9Sstevel@tonic-gate 	(void) uu_avl_find(lines, lp, NULL, &idx);
29287c478bd9Sstevel@tonic-gate 	uu_avl_insert(lines, lp, idx);
29297c478bd9Sstevel@tonic-gate 
29307c478bd9Sstevel@tonic-gate 	return (0);
29317c478bd9Sstevel@tonic-gate }
29327c478bd9Sstevel@tonic-gate 
29337c478bd9Sstevel@tonic-gate static int
29347c478bd9Sstevel@tonic-gate list_if_enabled(void *unused, scf_walkinfo_t *wip)
29357c478bd9Sstevel@tonic-gate {
29367c478bd9Sstevel@tonic-gate 	if (wip->pg != NULL ||
29377c478bd9Sstevel@tonic-gate 	    instance_enabled(wip->inst, B_FALSE) == 1 ||
29387c478bd9Sstevel@tonic-gate 	    instance_enabled(wip->inst, B_TRUE) == 1)
29397c478bd9Sstevel@tonic-gate 		return (list_instance(unused, wip));
29407c478bd9Sstevel@tonic-gate 
29417c478bd9Sstevel@tonic-gate 	return (0);
29427c478bd9Sstevel@tonic-gate }
29437c478bd9Sstevel@tonic-gate 
29447c478bd9Sstevel@tonic-gate /*
29457c478bd9Sstevel@tonic-gate  * Service FMRI selection: Lookup and call list_instance() for the instances.
29467c478bd9Sstevel@tonic-gate  * Instance FMRI selection: Lookup and call list_instance().
29477c478bd9Sstevel@tonic-gate  *
29487c478bd9Sstevel@tonic-gate  * Note: This is shoehorned into a walk_dependencies() callback prototype so
29497c478bd9Sstevel@tonic-gate  * it can be used in list_dependencies.
29507c478bd9Sstevel@tonic-gate  */
29517c478bd9Sstevel@tonic-gate static int
29527c478bd9Sstevel@tonic-gate list_svc_or_inst_fmri(void *complain, scf_walkinfo_t *wip)
29537c478bd9Sstevel@tonic-gate {
29547c478bd9Sstevel@tonic-gate 	char *fmri;
29557c478bd9Sstevel@tonic-gate 	const char *svc_name, *inst_name, *pg_name, *save;
29567c478bd9Sstevel@tonic-gate 	scf_iter_t *iter;
29577c478bd9Sstevel@tonic-gate 	int ret;
29587c478bd9Sstevel@tonic-gate 
29597c478bd9Sstevel@tonic-gate 	fmri = safe_strdup(wip->fmri);
29607c478bd9Sstevel@tonic-gate 
29617c478bd9Sstevel@tonic-gate 	if (scf_parse_svc_fmri(fmri, NULL, &svc_name, &inst_name, &pg_name,
29627c478bd9Sstevel@tonic-gate 	    NULL) != SCF_SUCCESS) {
29637c478bd9Sstevel@tonic-gate 		if (complain)
29647c478bd9Sstevel@tonic-gate 			uu_warn(gettext("FMRI \"%s\" is invalid.\n"),
29657c478bd9Sstevel@tonic-gate 			    wip->fmri);
29667c478bd9Sstevel@tonic-gate 		exit_status = UU_EXIT_FATAL;
29677c478bd9Sstevel@tonic-gate 		free(fmri);
29687c478bd9Sstevel@tonic-gate 		return (0);
29697c478bd9Sstevel@tonic-gate 	}
29707c478bd9Sstevel@tonic-gate 
29717c478bd9Sstevel@tonic-gate 	/*
29727c478bd9Sstevel@tonic-gate 	 * Yes, this invalidates *_name, but we only care whether they're NULL
29737c478bd9Sstevel@tonic-gate 	 * or not.
29747c478bd9Sstevel@tonic-gate 	 */
29757c478bd9Sstevel@tonic-gate 	free(fmri);
29767c478bd9Sstevel@tonic-gate 
29777c478bd9Sstevel@tonic-gate 	if (svc_name == NULL || pg_name != NULL) {
29787c478bd9Sstevel@tonic-gate 		if (complain)
29797c478bd9Sstevel@tonic-gate 			uu_warn(gettext("FMRI \"%s\" does not designate a "
29807c478bd9Sstevel@tonic-gate 			    "service or instance.\n"), wip->fmri);
29817c478bd9Sstevel@tonic-gate 		return (0);
29827c478bd9Sstevel@tonic-gate 	}
29837c478bd9Sstevel@tonic-gate 
29847c478bd9Sstevel@tonic-gate 	if (inst_name != NULL) {
29857c478bd9Sstevel@tonic-gate 		/* instance */
29867c478bd9Sstevel@tonic-gate 		if (scf_handle_decode_fmri(h, wip->fmri, wip->scope, wip->svc,
29877c478bd9Sstevel@tonic-gate 		    wip->inst, NULL, NULL, 0) != SCF_SUCCESS) {
29887c478bd9Sstevel@tonic-gate 			if (scf_error() != SCF_ERROR_NOT_FOUND)
29897c478bd9Sstevel@tonic-gate 				scfdie();
29907c478bd9Sstevel@tonic-gate 
29917c478bd9Sstevel@tonic-gate 			if (complain)
29927c478bd9Sstevel@tonic-gate 				uu_warn(gettext(
29937c478bd9Sstevel@tonic-gate 				    "Instance \"%s\" does not exist.\n"),
29947c478bd9Sstevel@tonic-gate 				    wip->fmri);
29957c478bd9Sstevel@tonic-gate 			return (0);
29967c478bd9Sstevel@tonic-gate 		}
29977c478bd9Sstevel@tonic-gate 
29987c478bd9Sstevel@tonic-gate 		return (list_instance(NULL, wip));
29997c478bd9Sstevel@tonic-gate 	}
30007c478bd9Sstevel@tonic-gate 
30017c478bd9Sstevel@tonic-gate 	/* service: Walk the instances. */
30027c478bd9Sstevel@tonic-gate 	if (scf_handle_decode_fmri(h, wip->fmri, wip->scope, wip->svc, NULL,
30037c478bd9Sstevel@tonic-gate 	    NULL, NULL, 0) != SCF_SUCCESS) {
30047c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_NOT_FOUND)
30057c478bd9Sstevel@tonic-gate 			scfdie();
30067c478bd9Sstevel@tonic-gate 
30077c478bd9Sstevel@tonic-gate 		if (complain)
30087c478bd9Sstevel@tonic-gate 			uu_warn(gettext("Service \"%s\" does not exist.\n"),
30097c478bd9Sstevel@tonic-gate 			    wip->fmri);
30107c478bd9Sstevel@tonic-gate 
30117c478bd9Sstevel@tonic-gate 		exit_status = UU_EXIT_FATAL;
30127c478bd9Sstevel@tonic-gate 
30137c478bd9Sstevel@tonic-gate 		return (0);
30147c478bd9Sstevel@tonic-gate 	}
30157c478bd9Sstevel@tonic-gate 
30167c478bd9Sstevel@tonic-gate 	iter = scf_iter_create(h);
30177c478bd9Sstevel@tonic-gate 	if (iter == NULL)
30187c478bd9Sstevel@tonic-gate 		scfdie();
30197c478bd9Sstevel@tonic-gate 
30207c478bd9Sstevel@tonic-gate 	if (scf_iter_service_instances(iter, wip->svc) != SCF_SUCCESS)
30217c478bd9Sstevel@tonic-gate 		scfdie();
30227c478bd9Sstevel@tonic-gate 
30237c478bd9Sstevel@tonic-gate 	if ((fmri = malloc(max_scf_fmri_length + 1)) == NULL) {
30247c478bd9Sstevel@tonic-gate 		scf_iter_destroy(iter);
30257c478bd9Sstevel@tonic-gate 		exit_status = UU_EXIT_FATAL;
30267c478bd9Sstevel@tonic-gate 		return (0);
30277c478bd9Sstevel@tonic-gate 	}
30287c478bd9Sstevel@tonic-gate 
30297c478bd9Sstevel@tonic-gate 	save = wip->fmri;
30307c478bd9Sstevel@tonic-gate 	wip->fmri = fmri;
30317c478bd9Sstevel@tonic-gate 	while ((ret = scf_iter_next_instance(iter, wip->inst)) == 1) {
30327c478bd9Sstevel@tonic-gate 		if (scf_instance_to_fmri(wip->inst, fmri,
30337c478bd9Sstevel@tonic-gate 		    max_scf_fmri_length + 1) <= 0)
30347c478bd9Sstevel@tonic-gate 			scfdie();
30357c478bd9Sstevel@tonic-gate 		(void) list_instance(NULL, wip);
30367c478bd9Sstevel@tonic-gate 	}
30377c478bd9Sstevel@tonic-gate 	free(fmri);
30387c478bd9Sstevel@tonic-gate 	wip->fmri = save;
30397c478bd9Sstevel@tonic-gate 	if (ret == -1)
30407c478bd9Sstevel@tonic-gate 		scfdie();
30417c478bd9Sstevel@tonic-gate 
30427c478bd9Sstevel@tonic-gate 	exit_status = UU_EXIT_OK;
30437c478bd9Sstevel@tonic-gate 
30447c478bd9Sstevel@tonic-gate 	scf_iter_destroy(iter);
30457c478bd9Sstevel@tonic-gate 
30467c478bd9Sstevel@tonic-gate 	return (0);
30477c478bd9Sstevel@tonic-gate }
30487c478bd9Sstevel@tonic-gate 
30497c478bd9Sstevel@tonic-gate /*
30507c478bd9Sstevel@tonic-gate  * Dependency selection: Straightforward since each instance lists the
30517c478bd9Sstevel@tonic-gate  * services it depends on.
30527c478bd9Sstevel@tonic-gate  */
30537c478bd9Sstevel@tonic-gate 
30547c478bd9Sstevel@tonic-gate static void
30557c478bd9Sstevel@tonic-gate walk_dependencies(scf_walkinfo_t *wip, scf_walk_callback callback, void *data)
30567c478bd9Sstevel@tonic-gate {
30577c478bd9Sstevel@tonic-gate 	scf_snapshot_t *snap;
30587c478bd9Sstevel@tonic-gate 	scf_iter_t *iter, *viter;
30597c478bd9Sstevel@tonic-gate 	int ret, vret;
30607c478bd9Sstevel@tonic-gate 	char *dep;
30617c478bd9Sstevel@tonic-gate 
30627c478bd9Sstevel@tonic-gate 	assert(wip->inst != NULL);
30637c478bd9Sstevel@tonic-gate 
30647c478bd9Sstevel@tonic-gate 	if ((iter = scf_iter_create(h)) == NULL ||
30657c478bd9Sstevel@tonic-gate 	    (viter = scf_iter_create(h)) == NULL)
30667c478bd9Sstevel@tonic-gate 		scfdie();
30677c478bd9Sstevel@tonic-gate 
30687c478bd9Sstevel@tonic-gate 	snap = get_running_snapshot(wip->inst);
30697c478bd9Sstevel@tonic-gate 
30707c478bd9Sstevel@tonic-gate 	if (scf_iter_instance_pgs_typed_composed(iter, wip->inst, snap,
30717c478bd9Sstevel@tonic-gate 	    SCF_GROUP_DEPENDENCY) != SCF_SUCCESS)
30727c478bd9Sstevel@tonic-gate 		scfdie();
30737c478bd9Sstevel@tonic-gate 
30747c478bd9Sstevel@tonic-gate 	dep = safe_malloc(max_scf_value_length + 1);
30757c478bd9Sstevel@tonic-gate 
30767c478bd9Sstevel@tonic-gate 	while ((ret = scf_iter_next_pg(iter, g_pg)) == 1) {
30777c478bd9Sstevel@tonic-gate 		scf_type_t ty;
30787c478bd9Sstevel@tonic-gate 
30797c478bd9Sstevel@tonic-gate 		/* Ignore exclude_any dependencies. */
30807c478bd9Sstevel@tonic-gate 		if (scf_pg_get_property(g_pg, SCF_PROPERTY_GROUPING, g_prop) !=
30817c478bd9Sstevel@tonic-gate 		    SCF_SUCCESS) {
30827c478bd9Sstevel@tonic-gate 			if (scf_error() != SCF_ERROR_NOT_FOUND)
30837c478bd9Sstevel@tonic-gate 				scfdie();
30847c478bd9Sstevel@tonic-gate 
30857c478bd9Sstevel@tonic-gate 			continue;
30867c478bd9Sstevel@tonic-gate 		}
30877c478bd9Sstevel@tonic-gate 
30887c478bd9Sstevel@tonic-gate 		if (scf_property_type(g_prop, &ty) != SCF_SUCCESS)
30897c478bd9Sstevel@tonic-gate 			scfdie();
30907c478bd9Sstevel@tonic-gate 
30917c478bd9Sstevel@tonic-gate 		if (ty != SCF_TYPE_ASTRING)
30927c478bd9Sstevel@tonic-gate 			continue;
30937c478bd9Sstevel@tonic-gate 
30947c478bd9Sstevel@tonic-gate 		if (scf_property_get_value(g_prop, g_val) != SCF_SUCCESS) {
30957c478bd9Sstevel@tonic-gate 			if (scf_error() != SCF_ERROR_CONSTRAINT_VIOLATED)
30967c478bd9Sstevel@tonic-gate 				scfdie();
30977c478bd9Sstevel@tonic-gate 
30987c478bd9Sstevel@tonic-gate 			continue;
30997c478bd9Sstevel@tonic-gate 		}
31007c478bd9Sstevel@tonic-gate 
31017c478bd9Sstevel@tonic-gate 		if (scf_value_get_astring(g_val, dep,
31027c478bd9Sstevel@tonic-gate 		    max_scf_value_length + 1) < 0)
31037c478bd9Sstevel@tonic-gate 			scfdie();
31047c478bd9Sstevel@tonic-gate 
31057c478bd9Sstevel@tonic-gate 		if (strcmp(dep, SCF_DEP_EXCLUDE_ALL) == 0)
31067c478bd9Sstevel@tonic-gate 			continue;
31077c478bd9Sstevel@tonic-gate 
31087c478bd9Sstevel@tonic-gate 		if (scf_pg_get_property(g_pg, SCF_PROPERTY_ENTITIES, g_prop) !=
31097c478bd9Sstevel@tonic-gate 		    SCF_SUCCESS) {
31107c478bd9Sstevel@tonic-gate 			if (scf_error() != SCF_ERROR_NOT_FOUND)
31117c478bd9Sstevel@tonic-gate 				scfdie();
31127c478bd9Sstevel@tonic-gate 
31137c478bd9Sstevel@tonic-gate 			continue;
31147c478bd9Sstevel@tonic-gate 		}
31157c478bd9Sstevel@tonic-gate 
31167c478bd9Sstevel@tonic-gate 		if (scf_iter_property_values(viter, g_prop) != SCF_SUCCESS)
31177c478bd9Sstevel@tonic-gate 			scfdie();
31187c478bd9Sstevel@tonic-gate 
31197c478bd9Sstevel@tonic-gate 		while ((vret = scf_iter_next_value(viter, g_val)) == 1) {
31207c478bd9Sstevel@tonic-gate 			if (scf_value_get_astring(g_val, dep,
31217c478bd9Sstevel@tonic-gate 			    max_scf_value_length + 1) < 0)
31227c478bd9Sstevel@tonic-gate 				scfdie();
31237c478bd9Sstevel@tonic-gate 
31247c478bd9Sstevel@tonic-gate 			wip->fmri = dep;
31257c478bd9Sstevel@tonic-gate 			if (callback(data, wip) != 0)
31267c478bd9Sstevel@tonic-gate 				goto out;
31277c478bd9Sstevel@tonic-gate 		}
31287c478bd9Sstevel@tonic-gate 		if (vret == -1)
31297c478bd9Sstevel@tonic-gate 			scfdie();
31307c478bd9Sstevel@tonic-gate 	}
31317c478bd9Sstevel@tonic-gate 	if (ret == -1)
31327c478bd9Sstevel@tonic-gate 		scfdie();
31337c478bd9Sstevel@tonic-gate 
31347c478bd9Sstevel@tonic-gate out:
31357c478bd9Sstevel@tonic-gate 	scf_iter_destroy(viter);
31367c478bd9Sstevel@tonic-gate 	scf_iter_destroy(iter);
31377c478bd9Sstevel@tonic-gate 	scf_snapshot_destroy(snap);
31387c478bd9Sstevel@tonic-gate }
31397c478bd9Sstevel@tonic-gate 
31407c478bd9Sstevel@tonic-gate static int
31417c478bd9Sstevel@tonic-gate list_dependencies(void *data, scf_walkinfo_t *wip)
31427c478bd9Sstevel@tonic-gate {
31437c478bd9Sstevel@tonic-gate 	walk_dependencies(wip, list_svc_or_inst_fmri, data);
31447c478bd9Sstevel@tonic-gate 	return (0);
31457c478bd9Sstevel@tonic-gate }
31467c478bd9Sstevel@tonic-gate 
31477c478bd9Sstevel@tonic-gate 
31487c478bd9Sstevel@tonic-gate /*
31497c478bd9Sstevel@tonic-gate  * Dependent selection: The "providing" service's or instance's FMRI is parsed
31507c478bd9Sstevel@tonic-gate  * into the provider_* variables, the instances are walked, and any instance
31517c478bd9Sstevel@tonic-gate  * which lists an FMRI which parses to these components is selected.  This is
31527c478bd9Sstevel@tonic-gate  * inefficient in the face of multiple operands, but that should be uncommon.
31537c478bd9Sstevel@tonic-gate  */
31547c478bd9Sstevel@tonic-gate 
31557c478bd9Sstevel@tonic-gate static char *provider_scope;
31567c478bd9Sstevel@tonic-gate static char *provider_svc;
31577c478bd9Sstevel@tonic-gate static char *provider_inst;	/* NULL for services */
31587c478bd9Sstevel@tonic-gate 
31597c478bd9Sstevel@tonic-gate /*ARGSUSED*/
31607c478bd9Sstevel@tonic-gate static int
31617c478bd9Sstevel@tonic-gate check_against_provider(void *arg, scf_walkinfo_t *wip)
31627c478bd9Sstevel@tonic-gate {
31637c478bd9Sstevel@tonic-gate 	char *cfmri;
31647c478bd9Sstevel@tonic-gate 	const char *scope_name, *svc_name, *inst_name, *pg_name;
31657c478bd9Sstevel@tonic-gate 	int *matchp = arg;
31667c478bd9Sstevel@tonic-gate 
31677c478bd9Sstevel@tonic-gate 	cfmri = safe_strdup(wip->fmri);
31687c478bd9Sstevel@tonic-gate 
31697c478bd9Sstevel@tonic-gate 	if (scf_parse_svc_fmri(cfmri, &scope_name, &svc_name, &inst_name,
31707c478bd9Sstevel@tonic-gate 	    &pg_name, NULL) != SCF_SUCCESS) {
31717c478bd9Sstevel@tonic-gate 		free(cfmri);
31727c478bd9Sstevel@tonic-gate 		return (0);
31737c478bd9Sstevel@tonic-gate 	}
31747c478bd9Sstevel@tonic-gate 
31757c478bd9Sstevel@tonic-gate 	if (svc_name == NULL || pg_name != NULL) {
31767c478bd9Sstevel@tonic-gate 		free(cfmri);
31777c478bd9Sstevel@tonic-gate 		return (0);
31787c478bd9Sstevel@tonic-gate 	}
31797c478bd9Sstevel@tonic-gate 
31807c478bd9Sstevel@tonic-gate 	/*
31817c478bd9Sstevel@tonic-gate 	 * If the user has specified an instance, then also match dependencies
31827c478bd9Sstevel@tonic-gate 	 * on the service itself.
31837c478bd9Sstevel@tonic-gate 	 */
31847c478bd9Sstevel@tonic-gate 	*matchp = (strcmp(provider_scope, scope_name) == 0 &&
31857c478bd9Sstevel@tonic-gate 	    strcmp(provider_svc, svc_name) == 0 &&
31867c478bd9Sstevel@tonic-gate 	    (provider_inst == NULL ? (inst_name == NULL) :
31877c478bd9Sstevel@tonic-gate 	    (inst_name == NULL || strcmp(provider_inst, inst_name) == 0)));
31887c478bd9Sstevel@tonic-gate 
31897c478bd9Sstevel@tonic-gate 	free(cfmri);
31907c478bd9Sstevel@tonic-gate 
31917c478bd9Sstevel@tonic-gate 	/* Stop on matches. */
31927c478bd9Sstevel@tonic-gate 	return (*matchp);
31937c478bd9Sstevel@tonic-gate }
31947c478bd9Sstevel@tonic-gate 
31957c478bd9Sstevel@tonic-gate static int
31967c478bd9Sstevel@tonic-gate list_if_dependent(void *unused, scf_walkinfo_t *wip)
31977c478bd9Sstevel@tonic-gate {
31987c478bd9Sstevel@tonic-gate 	/* Only proceed if this instance depends on provider_*. */
31997c478bd9Sstevel@tonic-gate 	int match = 0;
32007c478bd9Sstevel@tonic-gate 
32017c478bd9Sstevel@tonic-gate 	(void) walk_dependencies(wip, check_against_provider, &match);
32027c478bd9Sstevel@tonic-gate 
32037c478bd9Sstevel@tonic-gate 	if (match)
32047c478bd9Sstevel@tonic-gate 		return (list_instance(unused, wip));
32057c478bd9Sstevel@tonic-gate 
32067c478bd9Sstevel@tonic-gate 	return (0);
32077c478bd9Sstevel@tonic-gate }
32087c478bd9Sstevel@tonic-gate 
32097c478bd9Sstevel@tonic-gate /*ARGSUSED*/
32107c478bd9Sstevel@tonic-gate static int
32117c478bd9Sstevel@tonic-gate list_dependents(void *unused, scf_walkinfo_t *wip)
32127c478bd9Sstevel@tonic-gate {
32137c478bd9Sstevel@tonic-gate 	char *save;
32147c478bd9Sstevel@tonic-gate 	int ret;
32157c478bd9Sstevel@tonic-gate 
32167c478bd9Sstevel@tonic-gate 	if (scf_scope_get_name(wip->scope, provider_scope,
32177c478bd9Sstevel@tonic-gate 	    max_scf_fmri_length) <= 0 ||
32187c478bd9Sstevel@tonic-gate 	    scf_service_get_name(wip->svc, provider_svc,
32197c478bd9Sstevel@tonic-gate 	    max_scf_fmri_length) <= 0)
32207c478bd9Sstevel@tonic-gate 		scfdie();
32217c478bd9Sstevel@tonic-gate 
32227c478bd9Sstevel@tonic-gate 	save = provider_inst;
32237c478bd9Sstevel@tonic-gate 	if (wip->inst == NULL)
32247c478bd9Sstevel@tonic-gate 		provider_inst = NULL;
32257c478bd9Sstevel@tonic-gate 	else if (scf_instance_get_name(wip->inst, provider_inst,
32267c478bd9Sstevel@tonic-gate 	    max_scf_fmri_length) <= 0)
32277c478bd9Sstevel@tonic-gate 		scfdie();
32287c478bd9Sstevel@tonic-gate 
32297c478bd9Sstevel@tonic-gate 	ret = scf_walk_fmri(h, 0, NULL, 0, list_if_dependent, NULL, NULL,
32307c478bd9Sstevel@tonic-gate 	    uu_warn);
32317c478bd9Sstevel@tonic-gate 
32327c478bd9Sstevel@tonic-gate 	provider_inst = save;
32337c478bd9Sstevel@tonic-gate 
32347c478bd9Sstevel@tonic-gate 	return (ret);
32357c478bd9Sstevel@tonic-gate }
32367c478bd9Sstevel@tonic-gate 
32377c478bd9Sstevel@tonic-gate /*
32387c478bd9Sstevel@tonic-gate  * main() & helpers
32397c478bd9Sstevel@tonic-gate  */
32407c478bd9Sstevel@tonic-gate 
32417c478bd9Sstevel@tonic-gate static void
32427c478bd9Sstevel@tonic-gate add_sort_column(const char *col, int reverse)
32437c478bd9Sstevel@tonic-gate {
32447c478bd9Sstevel@tonic-gate 	int i;
32457c478bd9Sstevel@tonic-gate 
32467c478bd9Sstevel@tonic-gate 	++opt_snum;
32477c478bd9Sstevel@tonic-gate 
32487c478bd9Sstevel@tonic-gate 	opt_sort = realloc(opt_sort, opt_snum * sizeof (*opt_sort));
32497c478bd9Sstevel@tonic-gate 	if (opt_sort == NULL)
32507c478bd9Sstevel@tonic-gate 		uu_die(gettext("Too many sort criteria: out of memory.\n"));
32517c478bd9Sstevel@tonic-gate 
32527c478bd9Sstevel@tonic-gate 	for (i = 0; i < ncolumns; ++i) {
32537c478bd9Sstevel@tonic-gate 		if (strcasecmp(col, columns[i].name) == 0)
32547c478bd9Sstevel@tonic-gate 			break;
32557c478bd9Sstevel@tonic-gate 	}
32567c478bd9Sstevel@tonic-gate 
32577c478bd9Sstevel@tonic-gate 	if (i < ncolumns)
32587c478bd9Sstevel@tonic-gate 		opt_sort[opt_snum - 1] = (reverse ? i | 0x100 : i);
32597c478bd9Sstevel@tonic-gate 	else
32607c478bd9Sstevel@tonic-gate 		uu_die(gettext("Unrecognized sort column \"%s\".\n"), col);
32617c478bd9Sstevel@tonic-gate 
32627c478bd9Sstevel@tonic-gate 	sortkey_sz += columns[i].sortkey_width;
32637c478bd9Sstevel@tonic-gate }
32647c478bd9Sstevel@tonic-gate 
32657c478bd9Sstevel@tonic-gate static void
32667c478bd9Sstevel@tonic-gate add_restarter(const char *fmri)
32677c478bd9Sstevel@tonic-gate {
32687c478bd9Sstevel@tonic-gate 	char *cfmri;
32697c478bd9Sstevel@tonic-gate 	const char *pg_name;
32707c478bd9Sstevel@tonic-gate 	struct pfmri_list *rest;
32717c478bd9Sstevel@tonic-gate 
32727c478bd9Sstevel@tonic-gate 	cfmri = safe_strdup(fmri);
32737c478bd9Sstevel@tonic-gate 	rest = safe_malloc(sizeof (*rest));
32747c478bd9Sstevel@tonic-gate 
32757c478bd9Sstevel@tonic-gate 	if (scf_parse_svc_fmri(cfmri, &rest->scope, &rest->service,
32767c478bd9Sstevel@tonic-gate 	    &rest->instance, &pg_name, NULL) != SCF_SUCCESS)
32777c478bd9Sstevel@tonic-gate 		uu_die(gettext("Restarter FMRI \"%s\" is invalid.\n"), fmri);
32787c478bd9Sstevel@tonic-gate 
32797c478bd9Sstevel@tonic-gate 	if (rest->instance == NULL || pg_name != NULL)
32807c478bd9Sstevel@tonic-gate 		uu_die(gettext("Restarter FMRI \"%s\" does not designate an "
32817c478bd9Sstevel@tonic-gate 		    "instance.\n"), fmri);
32827c478bd9Sstevel@tonic-gate 
32837c478bd9Sstevel@tonic-gate 	rest->next = restarters;
32847c478bd9Sstevel@tonic-gate 	restarters = rest;
32857c478bd9Sstevel@tonic-gate 	return;
32867c478bd9Sstevel@tonic-gate 
32877c478bd9Sstevel@tonic-gate err:
32887c478bd9Sstevel@tonic-gate 	free(cfmri);
32897c478bd9Sstevel@tonic-gate 	free(rest);
32907c478bd9Sstevel@tonic-gate }
32917c478bd9Sstevel@tonic-gate 
32927c478bd9Sstevel@tonic-gate /* ARGSUSED */
32937c478bd9Sstevel@tonic-gate static int
32947c478bd9Sstevel@tonic-gate line_cmp(const void *l_arg, const void *r_arg, void *private)
32957c478bd9Sstevel@tonic-gate {
32967c478bd9Sstevel@tonic-gate 	const struct avl_string *l = l_arg;
32977c478bd9Sstevel@tonic-gate 	const struct avl_string *r = r_arg;
32987c478bd9Sstevel@tonic-gate 
32997c478bd9Sstevel@tonic-gate 	return (memcmp(l->key, r->key, sortkey_sz));
33007c478bd9Sstevel@tonic-gate }
33017c478bd9Sstevel@tonic-gate 
33027c478bd9Sstevel@tonic-gate /* ARGSUSED */
33037c478bd9Sstevel@tonic-gate static int
33047c478bd9Sstevel@tonic-gate print_line(void *e, void *private)
33057c478bd9Sstevel@tonic-gate {
33067c478bd9Sstevel@tonic-gate 	struct avl_string *lp = e;
33077c478bd9Sstevel@tonic-gate 
33087c478bd9Sstevel@tonic-gate 	(void) puts(lp->str);
33097c478bd9Sstevel@tonic-gate 
33107c478bd9Sstevel@tonic-gate 	return (UU_WALK_NEXT);
33117c478bd9Sstevel@tonic-gate }
33127c478bd9Sstevel@tonic-gate 
33137c478bd9Sstevel@tonic-gate int
33147c478bd9Sstevel@tonic-gate main(int argc, char **argv)
33157c478bd9Sstevel@tonic-gate {
33167c478bd9Sstevel@tonic-gate 	char opt, opt_mode;
33177c478bd9Sstevel@tonic-gate 	int i, n;
33187c478bd9Sstevel@tonic-gate 	char *columns_str = NULL;
33197c478bd9Sstevel@tonic-gate 	char *cp;
33207c478bd9Sstevel@tonic-gate 	const char *progname;
33217c478bd9Sstevel@tonic-gate 	int err;
33227c478bd9Sstevel@tonic-gate 
33237c478bd9Sstevel@tonic-gate 	int show_all = 0;
33247c478bd9Sstevel@tonic-gate 	int show_header = 1;
33257c478bd9Sstevel@tonic-gate 
3326*f6e214c7SGavin Maltby 	const char * const options = "aHpvno:R:s:S:dDl?x";
33277c478bd9Sstevel@tonic-gate 
33287c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
33297c478bd9Sstevel@tonic-gate 
33307c478bd9Sstevel@tonic-gate 	locale = setlocale(LC_MESSAGES, "");
33317c478bd9Sstevel@tonic-gate 	if (locale) {
33327c478bd9Sstevel@tonic-gate 		locale = safe_strdup(locale);
33331f6eb021SLiane Praza 		_scf_sanitize_locale(locale);
33347c478bd9Sstevel@tonic-gate 	}
33357c478bd9Sstevel@tonic-gate 
33367c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
33377c478bd9Sstevel@tonic-gate 	progname = uu_setpname(argv[0]);
33387c478bd9Sstevel@tonic-gate 
33397c478bd9Sstevel@tonic-gate 	exit_status = UU_EXIT_OK;
33407c478bd9Sstevel@tonic-gate 
33417c478bd9Sstevel@tonic-gate 	max_scf_name_length = scf_limit(SCF_LIMIT_MAX_NAME_LENGTH);
33427c478bd9Sstevel@tonic-gate 	max_scf_value_length = scf_limit(SCF_LIMIT_MAX_VALUE_LENGTH);
33437c478bd9Sstevel@tonic-gate 	max_scf_fmri_length = scf_limit(SCF_LIMIT_MAX_FMRI_LENGTH);
33441f6eb021SLiane Praza 	max_scf_type_length = scf_limit(SCF_LIMIT_MAX_PG_TYPE_LENGTH);
33457c478bd9Sstevel@tonic-gate 
33467c478bd9Sstevel@tonic-gate 	if (max_scf_name_length == -1 || max_scf_value_length == -1 ||
33471f6eb021SLiane Praza 	    max_scf_fmri_length == -1 || max_scf_type_length == -1)
33487c478bd9Sstevel@tonic-gate 		scfdie();
33497c478bd9Sstevel@tonic-gate 
33507c478bd9Sstevel@tonic-gate 	now = time(NULL);
33517c478bd9Sstevel@tonic-gate 	assert(now != -1);
33527c478bd9Sstevel@tonic-gate 
33537c478bd9Sstevel@tonic-gate 	/*
33547c478bd9Sstevel@tonic-gate 	 * opt_mode is the mode of operation.  0 for plain, 'd' for
33557c478bd9Sstevel@tonic-gate 	 * dependencies, 'D' for dependents, and 'l' for detailed (long).  We
33567c478bd9Sstevel@tonic-gate 	 * need to know now so we know which options are valid.
33577c478bd9Sstevel@tonic-gate 	 */
33587c478bd9Sstevel@tonic-gate 	opt_mode = 0;
33597c478bd9Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, options)) != -1) {
33607c478bd9Sstevel@tonic-gate 		switch (opt) {
33617c478bd9Sstevel@tonic-gate 		case '?':
33627c478bd9Sstevel@tonic-gate 			if (optopt == '?') {
33637c478bd9Sstevel@tonic-gate 				print_help(progname);
33647c478bd9Sstevel@tonic-gate 				return (UU_EXIT_OK);
33657c478bd9Sstevel@tonic-gate 			} else {
33667c478bd9Sstevel@tonic-gate 				argserr(progname);
33677c478bd9Sstevel@tonic-gate 				/* NOTREACHED */
33687c478bd9Sstevel@tonic-gate 			}
33697c478bd9Sstevel@tonic-gate 
33707c478bd9Sstevel@tonic-gate 		case 'd':
33717c478bd9Sstevel@tonic-gate 		case 'D':
33727c478bd9Sstevel@tonic-gate 		case 'l':
33737c478bd9Sstevel@tonic-gate 			if (opt_mode != 0)
33747c478bd9Sstevel@tonic-gate 				argserr(progname);
33757c478bd9Sstevel@tonic-gate 
33767c478bd9Sstevel@tonic-gate 			opt_mode = opt;
33777c478bd9Sstevel@tonic-gate 			break;
33787c478bd9Sstevel@tonic-gate 
3379*f6e214c7SGavin Maltby 		case 'n':
3380*f6e214c7SGavin Maltby 			if (opt_mode != 0)
3381*f6e214c7SGavin Maltby 				argserr(progname);
3382*f6e214c7SGavin Maltby 
3383*f6e214c7SGavin Maltby 			opt_mode = opt;
3384*f6e214c7SGavin Maltby 			break;
3385*f6e214c7SGavin Maltby 
33867c478bd9Sstevel@tonic-gate 		case 'x':
33877c478bd9Sstevel@tonic-gate 			if (opt_mode != 0)
33887c478bd9Sstevel@tonic-gate 				argserr(progname);
33897c478bd9Sstevel@tonic-gate 
33907c478bd9Sstevel@tonic-gate 			opt_mode = opt;
33917c478bd9Sstevel@tonic-gate 			break;
33927c478bd9Sstevel@tonic-gate 
33937c478bd9Sstevel@tonic-gate 		default:
33947c478bd9Sstevel@tonic-gate 			break;
33957c478bd9Sstevel@tonic-gate 		}
33967c478bd9Sstevel@tonic-gate 	}
33977c478bd9Sstevel@tonic-gate 
33987c478bd9Sstevel@tonic-gate 	sortkey_sz = 0;
33997c478bd9Sstevel@tonic-gate 
34007c478bd9Sstevel@tonic-gate 	optind = 1;	/* Reset getopt() */
34017c478bd9Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, options)) != -1) {
34027c478bd9Sstevel@tonic-gate 		switch (opt) {
34037c478bd9Sstevel@tonic-gate 		case 'a':
34047c478bd9Sstevel@tonic-gate 			if (opt_mode != 0)
34057c478bd9Sstevel@tonic-gate 				argserr(progname);
34067c478bd9Sstevel@tonic-gate 			show_all = 1;
34077c478bd9Sstevel@tonic-gate 			break;
34087c478bd9Sstevel@tonic-gate 
34097c478bd9Sstevel@tonic-gate 		case 'H':
34107c478bd9Sstevel@tonic-gate 			if (opt_mode == 'l' || opt_mode == 'x')
34117c478bd9Sstevel@tonic-gate 				argserr(progname);
34127c478bd9Sstevel@tonic-gate 			show_header = 0;
34137c478bd9Sstevel@tonic-gate 			break;
34147c478bd9Sstevel@tonic-gate 
34157c478bd9Sstevel@tonic-gate 		case 'p':
34167c478bd9Sstevel@tonic-gate 			if (opt_mode == 'x')
34177c478bd9Sstevel@tonic-gate 				argserr(progname);
34187c478bd9Sstevel@tonic-gate 			opt_processes = 1;
34197c478bd9Sstevel@tonic-gate 			break;
34207c478bd9Sstevel@tonic-gate 
34217c478bd9Sstevel@tonic-gate 		case 'v':
34227c478bd9Sstevel@tonic-gate 			opt_verbose = 1;
34237c478bd9Sstevel@tonic-gate 			break;
34247c478bd9Sstevel@tonic-gate 
34257c478bd9Sstevel@tonic-gate 		case 'o':
34267c478bd9Sstevel@tonic-gate 			if (opt_mode == 'l' || opt_mode == 'x')
34277c478bd9Sstevel@tonic-gate 				argserr(progname);
34287c478bd9Sstevel@tonic-gate 			columns_str = optarg;
34297c478bd9Sstevel@tonic-gate 			break;
34307c478bd9Sstevel@tonic-gate 
34317c478bd9Sstevel@tonic-gate 		case 'R':
34327c478bd9Sstevel@tonic-gate 			if (opt_mode != 0 || opt_mode == 'x')
34337c478bd9Sstevel@tonic-gate 				argserr(progname);
34347c478bd9Sstevel@tonic-gate 
34357c478bd9Sstevel@tonic-gate 			add_restarter(optarg);
34367c478bd9Sstevel@tonic-gate 			break;
34377c478bd9Sstevel@tonic-gate 
34387c478bd9Sstevel@tonic-gate 		case 's':
34397c478bd9Sstevel@tonic-gate 		case 'S':
34407c478bd9Sstevel@tonic-gate 			if (opt_mode != 0)
34417c478bd9Sstevel@tonic-gate 				argserr(progname);
34427c478bd9Sstevel@tonic-gate 
34437c478bd9Sstevel@tonic-gate 			add_sort_column(optarg, optopt == 'S');
34447c478bd9Sstevel@tonic-gate 			break;
34457c478bd9Sstevel@tonic-gate 
34467c478bd9Sstevel@tonic-gate 		case 'd':
34477c478bd9Sstevel@tonic-gate 		case 'D':
34487c478bd9Sstevel@tonic-gate 		case 'l':
3449*f6e214c7SGavin Maltby 		case 'n':
34507c478bd9Sstevel@tonic-gate 		case 'x':
34517c478bd9Sstevel@tonic-gate 			assert(opt_mode == optopt);
34527c478bd9Sstevel@tonic-gate 			break;
34537c478bd9Sstevel@tonic-gate 
34547c478bd9Sstevel@tonic-gate 		case '?':
34557c478bd9Sstevel@tonic-gate 			argserr(progname);
34567c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
34577c478bd9Sstevel@tonic-gate 
34587c478bd9Sstevel@tonic-gate 		default:
34597c478bd9Sstevel@tonic-gate 			assert(0);
34607c478bd9Sstevel@tonic-gate 			abort();
34617c478bd9Sstevel@tonic-gate 		}
34627c478bd9Sstevel@tonic-gate 	}
34637c478bd9Sstevel@tonic-gate 
34647c478bd9Sstevel@tonic-gate 	/*
34657c478bd9Sstevel@tonic-gate 	 * -a is only meaningful when given no arguments
34667c478bd9Sstevel@tonic-gate 	 */
34677c478bd9Sstevel@tonic-gate 	if (show_all && optind != argc)
34687c478bd9Sstevel@tonic-gate 		uu_warn(gettext("-a ignored when used with arguments.\n"));
34697c478bd9Sstevel@tonic-gate 
34707c478bd9Sstevel@tonic-gate 	h = scf_handle_create(SCF_VERSION);
34717c478bd9Sstevel@tonic-gate 	if (h == NULL)
34727c478bd9Sstevel@tonic-gate 		scfdie();
34737c478bd9Sstevel@tonic-gate 
34747c478bd9Sstevel@tonic-gate 	if (scf_handle_bind(h) == -1)
34757c478bd9Sstevel@tonic-gate 		uu_die(gettext("Could not bind to repository server: %s.  "
34767c478bd9Sstevel@tonic-gate 		    "Exiting.\n"), scf_strerror(scf_error()));
34777c478bd9Sstevel@tonic-gate 
34787c478bd9Sstevel@tonic-gate 	if ((g_pg = scf_pg_create(h)) == NULL ||
34797c478bd9Sstevel@tonic-gate 	    (g_prop = scf_property_create(h)) == NULL ||
34807c478bd9Sstevel@tonic-gate 	    (g_val = scf_value_create(h)) == NULL)
34817c478bd9Sstevel@tonic-gate 		scfdie();
34827c478bd9Sstevel@tonic-gate 
34837c478bd9Sstevel@tonic-gate 	argc -= optind;
34847c478bd9Sstevel@tonic-gate 	argv += optind;
34857c478bd9Sstevel@tonic-gate 
34867c478bd9Sstevel@tonic-gate 	/*
34877c478bd9Sstevel@tonic-gate 	 * If we're in long mode, take care of it now before we deal with the
34887c478bd9Sstevel@tonic-gate 	 * sorting and the columns, since we won't use them anyway.
34897c478bd9Sstevel@tonic-gate 	 */
34907c478bd9Sstevel@tonic-gate 	if (opt_mode == 'l') {
34917c478bd9Sstevel@tonic-gate 		if (argc == 0)
34927c478bd9Sstevel@tonic-gate 			argserr(progname);
34937c478bd9Sstevel@tonic-gate 
34947c478bd9Sstevel@tonic-gate 		if ((err = scf_walk_fmri(h, argc, argv, SCF_WALK_MULTIPLE,
34957c478bd9Sstevel@tonic-gate 		    print_detailed, NULL, &exit_status, uu_warn)) != 0) {
34967c478bd9Sstevel@tonic-gate 			uu_warn(gettext("failed to iterate over "
34977c478bd9Sstevel@tonic-gate 			    "instances: %s\n"), scf_strerror(err));
34987c478bd9Sstevel@tonic-gate 			exit_status = UU_EXIT_FATAL;
34997c478bd9Sstevel@tonic-gate 		}
35007c478bd9Sstevel@tonic-gate 
35017c478bd9Sstevel@tonic-gate 		return (exit_status);
35027c478bd9Sstevel@tonic-gate 	}
35037c478bd9Sstevel@tonic-gate 
3504*f6e214c7SGavin Maltby 	if (opt_mode == 'n') {
3505*f6e214c7SGavin Maltby 		print_notify_special();
3506*f6e214c7SGavin Maltby 		if ((err = scf_walk_fmri(h, argc, argv, SCF_WALK_MULTIPLE,
3507*f6e214c7SGavin Maltby 		    print_notify, NULL, &exit_status, uu_warn)) != 0) {
3508*f6e214c7SGavin Maltby 			uu_warn(gettext("failed to iterate over "
3509*f6e214c7SGavin Maltby 			    "instances: %s\n"), scf_strerror(err));
3510*f6e214c7SGavin Maltby 			exit_status = UU_EXIT_FATAL;
3511*f6e214c7SGavin Maltby 		}
3512*f6e214c7SGavin Maltby 
3513*f6e214c7SGavin Maltby 		return (exit_status);
3514*f6e214c7SGavin Maltby 	}
3515*f6e214c7SGavin Maltby 
35167c478bd9Sstevel@tonic-gate 	if (opt_mode == 'x') {
35177c478bd9Sstevel@tonic-gate 		explain(opt_verbose, argc, argv);
35187c478bd9Sstevel@tonic-gate 
35197c478bd9Sstevel@tonic-gate 		return (exit_status);
35207c478bd9Sstevel@tonic-gate 	}
35217c478bd9Sstevel@tonic-gate 
35227c478bd9Sstevel@tonic-gate 
35237c478bd9Sstevel@tonic-gate 	if (opt_snum == 0) {
35247c478bd9Sstevel@tonic-gate 		/* Default sort. */
35257c478bd9Sstevel@tonic-gate 		add_sort_column("state", 0);
35267c478bd9Sstevel@tonic-gate 		add_sort_column("stime", 0);
35277c478bd9Sstevel@tonic-gate 		add_sort_column("fmri", 0);
35287c478bd9Sstevel@tonic-gate 	}
35297c478bd9Sstevel@tonic-gate 
35307c478bd9Sstevel@tonic-gate 	if (columns_str == NULL) {
35317c478bd9Sstevel@tonic-gate 		if (!opt_verbose)
35327c478bd9Sstevel@tonic-gate 			columns_str = safe_strdup("state,stime,fmri");
35337c478bd9Sstevel@tonic-gate 		else
35347c478bd9Sstevel@tonic-gate 			columns_str =
35357c478bd9Sstevel@tonic-gate 			    safe_strdup("state,nstate,stime,ctid,fmri");
35367c478bd9Sstevel@tonic-gate 	}
35377c478bd9Sstevel@tonic-gate 
35387c478bd9Sstevel@tonic-gate 	/* Decode columns_str into opt_columns. */
35397c478bd9Sstevel@tonic-gate 	line_sz = 0;
35407c478bd9Sstevel@tonic-gate 
35417c478bd9Sstevel@tonic-gate 	opt_cnum = 1;
35427c478bd9Sstevel@tonic-gate 	for (cp = columns_str; *cp != '\0'; ++cp)
35437c478bd9Sstevel@tonic-gate 		if (*cp == ',')
35447c478bd9Sstevel@tonic-gate 			++opt_cnum;
35457c478bd9Sstevel@tonic-gate 
35467c478bd9Sstevel@tonic-gate 	opt_columns = malloc(opt_cnum * sizeof (*opt_columns));
35477c478bd9Sstevel@tonic-gate 	if (opt_columns == NULL)
35487c478bd9Sstevel@tonic-gate 		uu_die(gettext("Too many columns.\n"));
35497c478bd9Sstevel@tonic-gate 
35507c478bd9Sstevel@tonic-gate 	for (n = 0; *columns_str != '\0'; ++n) {
35517c478bd9Sstevel@tonic-gate 		i = getcolumnopt(&columns_str);
35527c478bd9Sstevel@tonic-gate 		if (i == -1)
35537c478bd9Sstevel@tonic-gate 			uu_die(gettext("Unknown column \"%s\".\n"),
35547c478bd9Sstevel@tonic-gate 			    columns_str);
35557c478bd9Sstevel@tonic-gate 
35567c478bd9Sstevel@tonic-gate 		if (strcmp(columns[i].name, "N") == 0 ||
35577c478bd9Sstevel@tonic-gate 		    strcmp(columns[i].name, "SN") == 0 ||
35587c478bd9Sstevel@tonic-gate 		    strcmp(columns[i].name, "NSTA") == 0 ||
35597c478bd9Sstevel@tonic-gate 		    strcmp(columns[i].name, "NSTATE") == 0)
35607c478bd9Sstevel@tonic-gate 			opt_nstate_shown = 1;
35617c478bd9Sstevel@tonic-gate 
35627c478bd9Sstevel@tonic-gate 		opt_columns[n] = i;
35637c478bd9Sstevel@tonic-gate 		line_sz += columns[i].width + 1;
35647c478bd9Sstevel@tonic-gate 	}
35657c478bd9Sstevel@tonic-gate 
35667c478bd9Sstevel@tonic-gate 
35677c478bd9Sstevel@tonic-gate 	if ((lines_pool = uu_avl_pool_create("lines_pool",
35687c478bd9Sstevel@tonic-gate 	    sizeof (struct avl_string), offsetof(struct avl_string, node),
35697c478bd9Sstevel@tonic-gate 	    line_cmp, UU_AVL_DEBUG)) == NULL ||
35707c478bd9Sstevel@tonic-gate 	    (lines = uu_avl_create(lines_pool, NULL, 0)) == NULL)
35717c478bd9Sstevel@tonic-gate 		uu_die(gettext("Unexpected libuutil error: %s.  Exiting.\n"),
35727c478bd9Sstevel@tonic-gate 		    uu_strerror(uu_error()));
35737c478bd9Sstevel@tonic-gate 
35747c478bd9Sstevel@tonic-gate 	switch (opt_mode) {
35757c478bd9Sstevel@tonic-gate 	case 0:
35767c478bd9Sstevel@tonic-gate 		ht_init();
35777c478bd9Sstevel@tonic-gate 
35787c478bd9Sstevel@tonic-gate 		/* Always show all FMRIs when given arguments or restarters */
35797c478bd9Sstevel@tonic-gate 		if (argc != 0 || restarters != NULL)
35807c478bd9Sstevel@tonic-gate 			show_all =  1;
35817c478bd9Sstevel@tonic-gate 
35827c478bd9Sstevel@tonic-gate 		if ((err = scf_walk_fmri(h, argc, argv,
35837c478bd9Sstevel@tonic-gate 		    SCF_WALK_MULTIPLE | SCF_WALK_LEGACY,
35847c478bd9Sstevel@tonic-gate 		    show_all ? list_instance : list_if_enabled, NULL,
35857c478bd9Sstevel@tonic-gate 		    &exit_status, uu_warn)) != 0) {
35867c478bd9Sstevel@tonic-gate 			uu_warn(gettext("failed to iterate over "
35877c478bd9Sstevel@tonic-gate 			    "instances: %s\n"), scf_strerror(err));
35887c478bd9Sstevel@tonic-gate 			exit_status = UU_EXIT_FATAL;
35897c478bd9Sstevel@tonic-gate 		}
35907c478bd9Sstevel@tonic-gate 		break;
35917c478bd9Sstevel@tonic-gate 
35927c478bd9Sstevel@tonic-gate 	case 'd':
35937c478bd9Sstevel@tonic-gate 		if (argc == 0)
35947c478bd9Sstevel@tonic-gate 			argserr(progname);
35957c478bd9Sstevel@tonic-gate 
35967c478bd9Sstevel@tonic-gate 		if ((err = scf_walk_fmri(h, argc, argv,
35977c478bd9Sstevel@tonic-gate 		    SCF_WALK_MULTIPLE, list_dependencies, NULL,
35987c478bd9Sstevel@tonic-gate 		    &exit_status, uu_warn)) != 0) {
35997c478bd9Sstevel@tonic-gate 			uu_warn(gettext("failed to iterate over "
36007c478bd9Sstevel@tonic-gate 			    "instances: %s\n"), scf_strerror(err));
36017c478bd9Sstevel@tonic-gate 			exit_status = UU_EXIT_FATAL;
36027c478bd9Sstevel@tonic-gate 		}
36037c478bd9Sstevel@tonic-gate 		break;
36047c478bd9Sstevel@tonic-gate 
36057c478bd9Sstevel@tonic-gate 	case 'D':
36067c478bd9Sstevel@tonic-gate 		if (argc == 0)
36077c478bd9Sstevel@tonic-gate 			argserr(progname);
36087c478bd9Sstevel@tonic-gate 
36097c478bd9Sstevel@tonic-gate 		provider_scope = safe_malloc(max_scf_fmri_length);
36107c478bd9Sstevel@tonic-gate 		provider_svc = safe_malloc(max_scf_fmri_length);
36117c478bd9Sstevel@tonic-gate 		provider_inst = safe_malloc(max_scf_fmri_length);
36127c478bd9Sstevel@tonic-gate 
36137c478bd9Sstevel@tonic-gate 		if ((err = scf_walk_fmri(h, argc, argv,
36147c478bd9Sstevel@tonic-gate 		    SCF_WALK_MULTIPLE | SCF_WALK_SERVICE,
36157c478bd9Sstevel@tonic-gate 		    list_dependents, NULL, &exit_status, uu_warn)) != 0) {
36167c478bd9Sstevel@tonic-gate 			uu_warn(gettext("failed to iterate over "
36177c478bd9Sstevel@tonic-gate 			    "instances: %s\n"), scf_strerror(err));
36187c478bd9Sstevel@tonic-gate 			exit_status = UU_EXIT_FATAL;
36197c478bd9Sstevel@tonic-gate 		}
36207c478bd9Sstevel@tonic-gate 
36217c478bd9Sstevel@tonic-gate 		free(provider_scope);
36227c478bd9Sstevel@tonic-gate 		free(provider_svc);
36237c478bd9Sstevel@tonic-gate 		free(provider_inst);
36247c478bd9Sstevel@tonic-gate 		break;
36257c478bd9Sstevel@tonic-gate 
3626*f6e214c7SGavin Maltby 	case 'n':
3627*f6e214c7SGavin Maltby 		break;
3628*f6e214c7SGavin Maltby 
36297c478bd9Sstevel@tonic-gate 	default:
36307c478bd9Sstevel@tonic-gate 		assert(0);
36317c478bd9Sstevel@tonic-gate 		abort();
36327c478bd9Sstevel@tonic-gate 	}
36337c478bd9Sstevel@tonic-gate 
36347c478bd9Sstevel@tonic-gate 	if (show_header)
36357c478bd9Sstevel@tonic-gate 		print_header();
36367c478bd9Sstevel@tonic-gate 
36377c478bd9Sstevel@tonic-gate 	(void) uu_avl_walk(lines, print_line, NULL, 0);
36387c478bd9Sstevel@tonic-gate 
36397c478bd9Sstevel@tonic-gate 	return (exit_status);
36407c478bd9Sstevel@tonic-gate }
3641