xref: /illumos-gate/usr/src/cmd/svc/svcs/svcs.c (revision 81ff72c5f2cea16235aa4a42d83d07e20090498d)
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 /*
23f6e214c7SGavin Maltby  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
24*81ff72c5SJason King  * Copyright 2019 Joyent, Inc.
2548bbca81SDaniel Hoffman  * Copyright (c) 2015, 2016 by Delphix. All rights reserved.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * svcs - display attributes of service instances
307c478bd9Sstevel@tonic-gate  *
317c478bd9Sstevel@tonic-gate  * We have two output formats and six instance selection mechanisms.  The
327c478bd9Sstevel@tonic-gate  * primary output format is a line of attributes (selected by -o), possibly
337c478bd9Sstevel@tonic-gate  * followed by process description lines (if -p is specified), for each
347c478bd9Sstevel@tonic-gate  * instance selected.  The columns available to display are described by the
357c478bd9Sstevel@tonic-gate  * struct column columns array.  The columns to actually display are kept in
367c478bd9Sstevel@tonic-gate  * the opt_columns array as indicies into the columns array.  The selection
377c478bd9Sstevel@tonic-gate  * mechanisms available for this format are service FMRIs (selects all child
387c478bd9Sstevel@tonic-gate  * instances), instance FMRIs, instance FMRI glob patterns, instances with
397c478bd9Sstevel@tonic-gate  * a certain restarter (-R), dependencies of instances (-d), and dependents of
407c478bd9Sstevel@tonic-gate  * instances (-D).  Since the lines must be sorted (per -sS), we'll just stick
417c478bd9Sstevel@tonic-gate  * each into a data structure and print them in order when we're done.  To
427c478bd9Sstevel@tonic-gate  * avoid listing the same instance twice (when -d and -D aren't given), we'll
437c478bd9Sstevel@tonic-gate  * use a hash table of FMRIs to record that we've listed (added to the tree)
447c478bd9Sstevel@tonic-gate  * an instance.
457c478bd9Sstevel@tonic-gate  *
467c478bd9Sstevel@tonic-gate  * The secondary output format (-l "long") is a paragraph of text for the
477c478bd9Sstevel@tonic-gate  * services or instances selected.  Not needing to be sorted, it's implemented
487c478bd9Sstevel@tonic-gate  * by just calling print_detailed() for each FMRI given.
497c478bd9Sstevel@tonic-gate  */
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate #include "svcs.h"
52f6e214c7SGavin Maltby #include "notify_params.h"
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate /* Get the byteorder macros to ease sorting. */
557c478bd9Sstevel@tonic-gate #include <sys/types.h>
567c478bd9Sstevel@tonic-gate #include <netinet/in.h>
577c478bd9Sstevel@tonic-gate #include <inttypes.h>
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate #include <sys/contract.h>
607c478bd9Sstevel@tonic-gate #include <sys/ctfs.h>
617c478bd9Sstevel@tonic-gate #include <sys/stat.h>
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate #include <assert.h>
647c478bd9Sstevel@tonic-gate #include <errno.h>
657c478bd9Sstevel@tonic-gate #include <fcntl.h>
667c478bd9Sstevel@tonic-gate #include <fnmatch.h>
677c478bd9Sstevel@tonic-gate #include <libcontract.h>
687c478bd9Sstevel@tonic-gate #include <libcontract_priv.h>
697c478bd9Sstevel@tonic-gate #include <libintl.h>
707c478bd9Sstevel@tonic-gate #include <libscf.h>
717c478bd9Sstevel@tonic-gate #include <libscf_priv.h>
727c478bd9Sstevel@tonic-gate #include <libuutil.h>
73f6e214c7SGavin Maltby #include <libnvpair.h>
74*81ff72c5SJason King #include <libproc.h>
757c478bd9Sstevel@tonic-gate #include <locale.h>
767c478bd9Sstevel@tonic-gate #include <stdarg.h>
777c478bd9Sstevel@tonic-gate #include <stdio.h>
787c478bd9Sstevel@tonic-gate #include <stdlib.h>
797c478bd9Sstevel@tonic-gate #include <strings.h>
807c478bd9Sstevel@tonic-gate #include <time.h>
81048b0279SBryan Cantrill #include <libzonecfg.h>
82048b0279SBryan Cantrill #include <zone.h>
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate #ifndef TEXT_DOMAIN
857c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SUNW_OST_OSCMD"
867c478bd9Sstevel@tonic-gate #endif /* TEXT_DOMAIN */
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate #define	LEGACY_UNKNOWN	"unknown"
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate /* Flags for pg_get_single_val() */
917c478bd9Sstevel@tonic-gate #define	EMPTY_OK	0x01
927c478bd9Sstevel@tonic-gate #define	MULTI_OK	0x02
937c478bd9Sstevel@tonic-gate 
94*81ff72c5SJason King /*
95*81ff72c5SJason King  * Per proc(4) when pr_nlwp, pr_nzomb, and pr_lwp.pr_lwpid are all 0,
96*81ff72c5SJason King  * the process is a zombie.
97*81ff72c5SJason King  */
98*81ff72c5SJason King #define	IS_ZOMBIE(_psip) \
99*81ff72c5SJason King 	((_psip)->pr_nlwp == 0 && (_psip)->pr_nzomb == 0 && \
100*81ff72c5SJason King 	(_psip)->pr_lwp.pr_lwpid == 0)
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate /*
1037c478bd9Sstevel@tonic-gate  * An AVL-storable node for output lines and the keys to sort them by.
1047c478bd9Sstevel@tonic-gate  */
1057c478bd9Sstevel@tonic-gate struct avl_string {
1067c478bd9Sstevel@tonic-gate 	uu_avl_node_t	node;
1077c478bd9Sstevel@tonic-gate 	char		*key;
1087c478bd9Sstevel@tonic-gate 	char		*str;
1097c478bd9Sstevel@tonic-gate };
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate /*
1127c478bd9Sstevel@tonic-gate  * For lists of parsed restarter FMRIs.
1137c478bd9Sstevel@tonic-gate  */
1147c478bd9Sstevel@tonic-gate struct pfmri_list {
1157c478bd9Sstevel@tonic-gate 	const char		*scope;
1167c478bd9Sstevel@tonic-gate 	const char		*service;
1177c478bd9Sstevel@tonic-gate 	const char		*instance;
1187c478bd9Sstevel@tonic-gate 	struct pfmri_list	*next;
1197c478bd9Sstevel@tonic-gate };
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate /*
1237c478bd9Sstevel@tonic-gate  * Globals
1247c478bd9Sstevel@tonic-gate  */
1257c478bd9Sstevel@tonic-gate scf_handle_t *h;
1267c478bd9Sstevel@tonic-gate static scf_propertygroup_t *g_pg;
1277c478bd9Sstevel@tonic-gate static scf_property_t *g_prop;
1287c478bd9Sstevel@tonic-gate static scf_value_t *g_val;
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate static size_t line_sz;			/* Bytes in the header line. */
1317c478bd9Sstevel@tonic-gate static size_t sortkey_sz;		/* Bytes in sort keys. */
1327c478bd9Sstevel@tonic-gate static uu_avl_pool_t *lines_pool;
1337c478bd9Sstevel@tonic-gate static uu_avl_t *lines;			/* Output lines. */
1347c478bd9Sstevel@tonic-gate int exit_status;
1357c478bd9Sstevel@tonic-gate ssize_t max_scf_name_length;
1367c478bd9Sstevel@tonic-gate ssize_t max_scf_value_length;
1377c478bd9Sstevel@tonic-gate ssize_t max_scf_fmri_length;
1381f6eb021SLiane Praza static ssize_t max_scf_type_length;
1397c478bd9Sstevel@tonic-gate static time_t now;
1407c478bd9Sstevel@tonic-gate static struct pfmri_list *restarters = NULL;
1417c478bd9Sstevel@tonic-gate static int first_paragraph = 1;		/* For -l mode. */
1427c478bd9Sstevel@tonic-gate static char *common_name_buf;		/* Sized for maximal length value. */
1437c478bd9Sstevel@tonic-gate char *locale;				/* Current locale. */
144048b0279SBryan Cantrill char *g_zonename;			/* zone being operated upon */
1457c478bd9Sstevel@tonic-gate 
14694501b61Sskamm /*
14794501b61Sskamm  * Pathname storage for path generated from the fmri.
14894501b61Sskamm  * Used for reading the ctid and (start) pid files for an inetd service.
14994501b61Sskamm  */
15094501b61Sskamm static char genfmri_filename[MAXPATHLEN] = "";
15194501b61Sskamm 
1527c478bd9Sstevel@tonic-gate /* Options */
1537c478bd9Sstevel@tonic-gate static int *opt_columns = NULL;		/* Indices into columns to display. */
1547c478bd9Sstevel@tonic-gate static int opt_cnum = 0;
1557c478bd9Sstevel@tonic-gate static int opt_processes = 0;		/* Print processes? */
1567c478bd9Sstevel@tonic-gate static int *opt_sort = NULL;		/* Indices into columns to sort. */
1577c478bd9Sstevel@tonic-gate static int opt_snum = 0;
1587c478bd9Sstevel@tonic-gate static int opt_nstate_shown = 0;	/* Will nstate be shown? */
1597c478bd9Sstevel@tonic-gate static int opt_verbose = 0;
160048b0279SBryan Cantrill static char *opt_zone;			/* zone selected, if any */
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate /* Minimize string constants. */
1637c478bd9Sstevel@tonic-gate static const char * const scf_property_state = SCF_PROPERTY_STATE;
1647c478bd9Sstevel@tonic-gate static const char * const scf_property_next_state = SCF_PROPERTY_NEXT_STATE;
1657c478bd9Sstevel@tonic-gate static const char * const scf_property_contract = SCF_PROPERTY_CONTRACT;
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate /*
1697c478bd9Sstevel@tonic-gate  * Utility functions
1707c478bd9Sstevel@tonic-gate  */
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate /*
1737c478bd9Sstevel@tonic-gate  * For unexpected libscf errors.  The ending newline is necessary to keep
1747c478bd9Sstevel@tonic-gate  * uu_die() from appending the errno error.
1757c478bd9Sstevel@tonic-gate  */
1767c478bd9Sstevel@tonic-gate #ifndef NDEBUG
1777c478bd9Sstevel@tonic-gate void
1787c478bd9Sstevel@tonic-gate do_scfdie(const char *file, int line)
1797c478bd9Sstevel@tonic-gate {
1807c478bd9Sstevel@tonic-gate 	uu_die(gettext("%s:%d: Unexpected libscf error: %s.  Exiting.\n"),
1817c478bd9Sstevel@tonic-gate 	    file, line, scf_strerror(scf_error()));
1827c478bd9Sstevel@tonic-gate }
1837c478bd9Sstevel@tonic-gate #else
1847c478bd9Sstevel@tonic-gate void
1857c478bd9Sstevel@tonic-gate scfdie(void)
1867c478bd9Sstevel@tonic-gate {
1877c478bd9Sstevel@tonic-gate 	uu_die(gettext("Unexpected libscf error: %s.  Exiting.\n"),
1887c478bd9Sstevel@tonic-gate 	    scf_strerror(scf_error()));
1897c478bd9Sstevel@tonic-gate }
1907c478bd9Sstevel@tonic-gate #endif
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate void *
1937c478bd9Sstevel@tonic-gate safe_malloc(size_t sz)
1947c478bd9Sstevel@tonic-gate {
1957c478bd9Sstevel@tonic-gate 	void *ptr;
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	ptr = malloc(sz);
1987c478bd9Sstevel@tonic-gate 	if (ptr == NULL)
1997c478bd9Sstevel@tonic-gate 		uu_die(gettext("Out of memory"));
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	return (ptr);
2027c478bd9Sstevel@tonic-gate }
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate char *
2057c478bd9Sstevel@tonic-gate safe_strdup(const char *str)
2067c478bd9Sstevel@tonic-gate {
2077c478bd9Sstevel@tonic-gate 	char *cp;
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	cp = strdup(str);
2107c478bd9Sstevel@tonic-gate 	if (cp == NULL)
2117c478bd9Sstevel@tonic-gate 		uu_die(gettext("Out of memory.\n"));
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 	return (cp);
2147c478bd9Sstevel@tonic-gate }
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate /*
2177c478bd9Sstevel@tonic-gate  * FMRI hashtable.  For uniquifing listings.
2187c478bd9Sstevel@tonic-gate  */
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate struct ht_elem {
2217c478bd9Sstevel@tonic-gate 	const char	*fmri;
2227c478bd9Sstevel@tonic-gate 	struct ht_elem	*next;
2237c478bd9Sstevel@tonic-gate };
2247c478bd9Sstevel@tonic-gate 
225048b0279SBryan Cantrill static struct ht_elem	**ht_buckets = NULL;
226048b0279SBryan Cantrill static uint_t		ht_buckets_num = 0;
2277c478bd9Sstevel@tonic-gate static uint_t		ht_num;
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate static void
230048b0279SBryan Cantrill ht_free(void)
231048b0279SBryan Cantrill {
232048b0279SBryan Cantrill 	struct ht_elem *elem, *next;
233048b0279SBryan Cantrill 	int i;
234048b0279SBryan Cantrill 
235048b0279SBryan Cantrill 	for (i = 0; i < ht_buckets_num; i++) {
236048b0279SBryan Cantrill 		for (elem = ht_buckets[i]; elem != NULL; elem = next) {
237048b0279SBryan Cantrill 			next = elem->next;
238048b0279SBryan Cantrill 			free((char *)elem->fmri);
239048b0279SBryan Cantrill 			free(elem);
240048b0279SBryan Cantrill 		}
241048b0279SBryan Cantrill 	}
242048b0279SBryan Cantrill 
243048b0279SBryan Cantrill 	free(ht_buckets);
244048b0279SBryan Cantrill 	ht_buckets_num = 0;
245048b0279SBryan Cantrill 	ht_buckets = NULL;
246048b0279SBryan Cantrill }
247048b0279SBryan Cantrill 
248048b0279SBryan Cantrill static void
249048b0279SBryan Cantrill ht_init(void)
2507c478bd9Sstevel@tonic-gate {
251048b0279SBryan Cantrill 	assert(ht_buckets == NULL);
252048b0279SBryan Cantrill 
2537c478bd9Sstevel@tonic-gate 	ht_buckets_num = 8;
2547c478bd9Sstevel@tonic-gate 	ht_buckets = safe_malloc(sizeof (*ht_buckets) * ht_buckets_num);
2557c478bd9Sstevel@tonic-gate 	bzero(ht_buckets, sizeof (*ht_buckets) * ht_buckets_num);
2567c478bd9Sstevel@tonic-gate 	ht_num = 0;
2577c478bd9Sstevel@tonic-gate }
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate static uint_t
2607c478bd9Sstevel@tonic-gate ht_hash_fmri(const char *fmri)
2617c478bd9Sstevel@tonic-gate {
2627c478bd9Sstevel@tonic-gate 	uint_t h = 0, g;
2637c478bd9Sstevel@tonic-gate 	const char *p, *k;
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	/* All FMRIs begin with svc:/, so skip that part. */
2667c478bd9Sstevel@tonic-gate 	assert(strncmp(fmri, "svc:/", sizeof ("svc:/") - 1) == 0);
2677c478bd9Sstevel@tonic-gate 	k = fmri + sizeof ("svc:/") - 1;
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 	/*
2707c478bd9Sstevel@tonic-gate 	 * Generic hash function from uts/common/os/modhash.c.
2717c478bd9Sstevel@tonic-gate 	 */
2727c478bd9Sstevel@tonic-gate 	for (p = k; *p != '\0'; ++p) {
2737c478bd9Sstevel@tonic-gate 		h = (h << 4) + *p;
2747c478bd9Sstevel@tonic-gate 		if ((g = (h & 0xf0000000)) != 0) {
2757c478bd9Sstevel@tonic-gate 			h ^= (g >> 24);
2767c478bd9Sstevel@tonic-gate 			h ^= g;
2777c478bd9Sstevel@tonic-gate 		}
2787c478bd9Sstevel@tonic-gate 	}
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 	return (h);
2817c478bd9Sstevel@tonic-gate }
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate static void
2847c478bd9Sstevel@tonic-gate ht_grow()
2857c478bd9Sstevel@tonic-gate {
2867c478bd9Sstevel@tonic-gate 	uint_t new_ht_buckets_num;
2877c478bd9Sstevel@tonic-gate 	struct ht_elem **new_ht_buckets;
2887c478bd9Sstevel@tonic-gate 	int i;
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 	new_ht_buckets_num = ht_buckets_num * 2;
2917c478bd9Sstevel@tonic-gate 	assert(new_ht_buckets_num > ht_buckets_num);
2927c478bd9Sstevel@tonic-gate 	new_ht_buckets =
2937c478bd9Sstevel@tonic-gate 	    safe_malloc(sizeof (*new_ht_buckets) * new_ht_buckets_num);
2947c478bd9Sstevel@tonic-gate 	bzero(new_ht_buckets, sizeof (*new_ht_buckets) * new_ht_buckets_num);
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 	for (i = 0; i < ht_buckets_num; ++i) {
2977c478bd9Sstevel@tonic-gate 		struct ht_elem *elem, *next;
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 		for (elem = ht_buckets[i]; elem != NULL; elem = next) {
3007c478bd9Sstevel@tonic-gate 			uint_t h;
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 			next = elem->next;
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 			h = ht_hash_fmri(elem->fmri);
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 			elem->next =
3077c478bd9Sstevel@tonic-gate 			    new_ht_buckets[h & (new_ht_buckets_num - 1)];
3087c478bd9Sstevel@tonic-gate 			new_ht_buckets[h & (new_ht_buckets_num - 1)] = elem;
3097c478bd9Sstevel@tonic-gate 		}
3107c478bd9Sstevel@tonic-gate 	}
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 	free(ht_buckets);
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	ht_buckets = new_ht_buckets;
3157c478bd9Sstevel@tonic-gate 	ht_buckets_num = new_ht_buckets_num;
3167c478bd9Sstevel@tonic-gate }
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate /*
3197c478bd9Sstevel@tonic-gate  * Add an FMRI to the hash table.  Returns 1 if it was already there,
3207c478bd9Sstevel@tonic-gate  * 0 otherwise.
3217c478bd9Sstevel@tonic-gate  */
3227c478bd9Sstevel@tonic-gate static int
3237c478bd9Sstevel@tonic-gate ht_add(const char *fmri)
3247c478bd9Sstevel@tonic-gate {
3257c478bd9Sstevel@tonic-gate 	uint_t h;
3267c478bd9Sstevel@tonic-gate 	struct ht_elem *elem;
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 	h = ht_hash_fmri(fmri);
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	elem = ht_buckets[h & (ht_buckets_num - 1)];
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 	for (; elem != NULL; elem = elem->next) {
3337c478bd9Sstevel@tonic-gate 		if (strcmp(elem->fmri, fmri) == 0)
3347c478bd9Sstevel@tonic-gate 			return (1);
3357c478bd9Sstevel@tonic-gate 	}
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	/* Grow when average chain length is over 3. */
3387c478bd9Sstevel@tonic-gate 	if (ht_num > 3 * ht_buckets_num)
3397c478bd9Sstevel@tonic-gate 		ht_grow();
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	++ht_num;
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 	elem = safe_malloc(sizeof (*elem));
3447c478bd9Sstevel@tonic-gate 	elem->fmri = strdup(fmri);
3457c478bd9Sstevel@tonic-gate 	elem->next = ht_buckets[h & (ht_buckets_num - 1)];
3467c478bd9Sstevel@tonic-gate 	ht_buckets[h & (ht_buckets_num - 1)] = elem;
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 	return (0);
3497c478bd9Sstevel@tonic-gate }
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate /*
3547c478bd9Sstevel@tonic-gate  * Convenience libscf wrapper functions.
3557c478bd9Sstevel@tonic-gate  */
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate /*
3587c478bd9Sstevel@tonic-gate  * Get the single value of the named property in the given property group,
3597c478bd9Sstevel@tonic-gate  * which must have type ty, and put it in *vp.  If ty is SCF_TYPE_ASTRING, vp
3607c478bd9Sstevel@tonic-gate  * is taken to be a char **, and sz is the size of the buffer.  sz is unused
3617c478bd9Sstevel@tonic-gate  * otherwise.  Return 0 on success, -1 if the property doesn't exist, has the
3627c478bd9Sstevel@tonic-gate  * wrong type, or doesn't have a single value.  If flags has EMPTY_OK, don't
3637c478bd9Sstevel@tonic-gate  * complain if the property has no values (but return nonzero).  If flags has
3647c478bd9Sstevel@tonic-gate  * MULTI_OK and the property has multiple values, succeed with E2BIG.
3657c478bd9Sstevel@tonic-gate  */
3667c478bd9Sstevel@tonic-gate int
3677c478bd9Sstevel@tonic-gate pg_get_single_val(scf_propertygroup_t *pg, const char *propname, scf_type_t ty,
3687c478bd9Sstevel@tonic-gate     void *vp, size_t sz, uint_t flags)
3697c478bd9Sstevel@tonic-gate {
370048b0279SBryan Cantrill 	char *buf, root[MAXPATHLEN];
3717c478bd9Sstevel@tonic-gate 	size_t buf_sz;
3727c478bd9Sstevel@tonic-gate 	int ret = -1, r;
3737c478bd9Sstevel@tonic-gate 	boolean_t multi = B_FALSE;
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 	assert((flags & ~(EMPTY_OK | MULTI_OK)) == 0);
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 	if (scf_pg_get_property(pg, propname, g_prop) == -1) {
3787c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_NOT_FOUND)
3797c478bd9Sstevel@tonic-gate 			scfdie();
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 		goto out;
3827c478bd9Sstevel@tonic-gate 	}
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 	if (scf_property_is_type(g_prop, ty) != SCF_SUCCESS) {
3857c478bd9Sstevel@tonic-gate 		if (scf_error() == SCF_ERROR_TYPE_MISMATCH)
3867c478bd9Sstevel@tonic-gate 			goto misconfigured;
3877c478bd9Sstevel@tonic-gate 		scfdie();
3887c478bd9Sstevel@tonic-gate 	}
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 	if (scf_property_get_value(g_prop, g_val) != SCF_SUCCESS) {
3917c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
3927c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
3937c478bd9Sstevel@tonic-gate 			if (flags & EMPTY_OK)
3947c478bd9Sstevel@tonic-gate 				goto out;
3957c478bd9Sstevel@tonic-gate 			goto misconfigured;
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
3987c478bd9Sstevel@tonic-gate 			if (flags & MULTI_OK) {
3997c478bd9Sstevel@tonic-gate 				multi = B_TRUE;
4007c478bd9Sstevel@tonic-gate 				break;
4017c478bd9Sstevel@tonic-gate 			}
4027c478bd9Sstevel@tonic-gate 			goto misconfigured;
4037c478bd9Sstevel@tonic-gate 
4043eae19d9Swesolows 		case SCF_ERROR_PERMISSION_DENIED:
4057c478bd9Sstevel@tonic-gate 		default:
4067c478bd9Sstevel@tonic-gate 			scfdie();
4077c478bd9Sstevel@tonic-gate 		}
4087c478bd9Sstevel@tonic-gate 	}
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 	switch (ty) {
4117c478bd9Sstevel@tonic-gate 	case SCF_TYPE_ASTRING:
4127c478bd9Sstevel@tonic-gate 		r = scf_value_get_astring(g_val, vp, sz) > 0 ? SCF_SUCCESS : -1;
4137c478bd9Sstevel@tonic-gate 		break;
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	case SCF_TYPE_BOOLEAN:
4167c478bd9Sstevel@tonic-gate 		r = scf_value_get_boolean(g_val, (uint8_t *)vp);
4177c478bd9Sstevel@tonic-gate 		break;
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate 	case SCF_TYPE_COUNT:
4207c478bd9Sstevel@tonic-gate 		r = scf_value_get_count(g_val, (uint64_t *)vp);
4217c478bd9Sstevel@tonic-gate 		break;
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 	case SCF_TYPE_INTEGER:
4247c478bd9Sstevel@tonic-gate 		r = scf_value_get_integer(g_val, (int64_t *)vp);
4257c478bd9Sstevel@tonic-gate 		break;
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 	case SCF_TYPE_TIME: {
4287c478bd9Sstevel@tonic-gate 		int64_t sec;
4297c478bd9Sstevel@tonic-gate 		int32_t ns;
4307c478bd9Sstevel@tonic-gate 		r = scf_value_get_time(g_val, &sec, &ns);
4317c478bd9Sstevel@tonic-gate 		((struct timeval *)vp)->tv_sec = sec;
4327c478bd9Sstevel@tonic-gate 		((struct timeval *)vp)->tv_usec = ns / 1000;
4337c478bd9Sstevel@tonic-gate 		break;
4347c478bd9Sstevel@tonic-gate 	}
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 	case SCF_TYPE_USTRING:
4377c478bd9Sstevel@tonic-gate 		r = scf_value_get_ustring(g_val, vp, sz) > 0 ? SCF_SUCCESS : -1;
4387c478bd9Sstevel@tonic-gate 		break;
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 	default:
4417c478bd9Sstevel@tonic-gate #ifndef NDEBUG
4427c478bd9Sstevel@tonic-gate 		uu_warn("%s:%d: Unknown type %d.\n", __FILE__, __LINE__, ty);
4437c478bd9Sstevel@tonic-gate #endif
4447c478bd9Sstevel@tonic-gate 		abort();
4457c478bd9Sstevel@tonic-gate 	}
4467c478bd9Sstevel@tonic-gate 	if (r != SCF_SUCCESS)
4477c478bd9Sstevel@tonic-gate 		scfdie();
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 	ret = multi ? E2BIG : 0;
4507c478bd9Sstevel@tonic-gate 	goto out;
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate misconfigured:
4537c478bd9Sstevel@tonic-gate 	buf_sz = max_scf_fmri_length + 1;
4547c478bd9Sstevel@tonic-gate 	buf = safe_malloc(buf_sz);
4557c478bd9Sstevel@tonic-gate 	if (scf_property_to_fmri(g_prop, buf, buf_sz) == -1)
4567c478bd9Sstevel@tonic-gate 		scfdie();
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 	uu_warn(gettext("Property \"%s\" is misconfigured.\n"), buf);
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 	free(buf);
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate out:
463048b0279SBryan Cantrill 	if (ret != 0 || g_zonename == NULL ||
464048b0279SBryan Cantrill 	    (strcmp(propname, SCF_PROPERTY_LOGFILE) != 0 &&
465048b0279SBryan Cantrill 	    strcmp(propname, SCF_PROPERTY_ALT_LOGFILE) != 0))
466048b0279SBryan Cantrill 		return (ret);
467048b0279SBryan Cantrill 
468048b0279SBryan Cantrill 	/*
469048b0279SBryan Cantrill 	 * If we're here, we have a log file and we have specified a zone.
470048b0279SBryan Cantrill 	 * As a convenience, we're going to prepend the zone path to the
471048b0279SBryan Cantrill 	 * name of the log file.
472048b0279SBryan Cantrill 	 */
473048b0279SBryan Cantrill 	root[0] = '\0';
474048b0279SBryan Cantrill 	(void) zone_get_rootpath(g_zonename, root, sizeof (root));
475048b0279SBryan Cantrill 	(void) strlcat(root, vp, sizeof (root));
476048b0279SBryan Cantrill 	(void) snprintf(vp, sz, "%s", root);
477048b0279SBryan Cantrill 
4787c478bd9Sstevel@tonic-gate 	return (ret);
4797c478bd9Sstevel@tonic-gate }
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate static scf_snapshot_t *
4827c478bd9Sstevel@tonic-gate get_running_snapshot(scf_instance_t *inst)
4837c478bd9Sstevel@tonic-gate {
4847c478bd9Sstevel@tonic-gate 	scf_snapshot_t *snap;
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate 	snap = scf_snapshot_create(h);
4877c478bd9Sstevel@tonic-gate 	if (snap == NULL)
4887c478bd9Sstevel@tonic-gate 		scfdie();
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate 	if (scf_instance_get_snapshot(inst, "running", snap) == 0)
4917c478bd9Sstevel@tonic-gate 		return (snap);
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 	if (scf_error() != SCF_ERROR_NOT_FOUND)
4947c478bd9Sstevel@tonic-gate 		scfdie();
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 	scf_snapshot_destroy(snap);
4977c478bd9Sstevel@tonic-gate 	return (NULL);
4987c478bd9Sstevel@tonic-gate }
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate /*
5017c478bd9Sstevel@tonic-gate  * As pg_get_single_val(), except look the property group up in an
5027c478bd9Sstevel@tonic-gate  * instance.  If "use_running" is set, and the running snapshot exists,
5037c478bd9Sstevel@tonic-gate  * do a composed lookup there.  Otherwise, do an (optionally composed)
5047c478bd9Sstevel@tonic-gate  * lookup on the current values.  Note that lookups using snapshots are
5057c478bd9Sstevel@tonic-gate  * always composed.
5067c478bd9Sstevel@tonic-gate  */
5077c478bd9Sstevel@tonic-gate int
5087c478bd9Sstevel@tonic-gate inst_get_single_val(scf_instance_t *inst, const char *pgname,
5097c478bd9Sstevel@tonic-gate     const char *propname, scf_type_t ty, void *vp, size_t sz, uint_t flags,
5107c478bd9Sstevel@tonic-gate     int use_running, int composed)
5117c478bd9Sstevel@tonic-gate {
5127c478bd9Sstevel@tonic-gate 	scf_snapshot_t *snap = NULL;
5137c478bd9Sstevel@tonic-gate 	int r;
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 	if (use_running)
5167c478bd9Sstevel@tonic-gate 		snap = get_running_snapshot(inst);
5177c478bd9Sstevel@tonic-gate 	if (composed || use_running)
5187c478bd9Sstevel@tonic-gate 		r = scf_instance_get_pg_composed(inst, snap, pgname, g_pg);
5197c478bd9Sstevel@tonic-gate 	else
5207c478bd9Sstevel@tonic-gate 		r = scf_instance_get_pg(inst, pgname, g_pg);
5217c478bd9Sstevel@tonic-gate 	if (snap)
5227c478bd9Sstevel@tonic-gate 		scf_snapshot_destroy(snap);
5237c478bd9Sstevel@tonic-gate 	if (r == -1)
5247c478bd9Sstevel@tonic-gate 		return (-1);
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate 	r = pg_get_single_val(g_pg, propname, ty, vp, sz, flags);
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate 	return (r);
5297c478bd9Sstevel@tonic-gate }
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate static int
5327c478bd9Sstevel@tonic-gate instance_enabled(scf_instance_t *inst, boolean_t temp)
5337c478bd9Sstevel@tonic-gate {
5347c478bd9Sstevel@tonic-gate 	uint8_t b;
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 	if (inst_get_single_val(inst,
5377c478bd9Sstevel@tonic-gate 	    temp ? SCF_PG_GENERAL_OVR : SCF_PG_GENERAL, SCF_PROPERTY_ENABLED,
5387c478bd9Sstevel@tonic-gate 	    SCF_TYPE_BOOLEAN, &b, 0, 0, 0, 0) != 0)
5397c478bd9Sstevel@tonic-gate 		return (-1);
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 	return (b ? 1 : 0);
5427c478bd9Sstevel@tonic-gate }
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate /*
5457c478bd9Sstevel@tonic-gate  * Get a string property from the restarter property group of the given
5467c478bd9Sstevel@tonic-gate  * instance.  Return an empty string on normal problems.
5477c478bd9Sstevel@tonic-gate  */
5487c478bd9Sstevel@tonic-gate static void
5497c478bd9Sstevel@tonic-gate get_restarter_string_prop(scf_instance_t *inst, const char *pname,
5507c478bd9Sstevel@tonic-gate     char *buf, size_t buf_sz)
5517c478bd9Sstevel@tonic-gate {
5527c478bd9Sstevel@tonic-gate 	if (inst_get_single_val(inst, SCF_PG_RESTARTER, pname,
5537c478bd9Sstevel@tonic-gate 	    SCF_TYPE_ASTRING, buf, buf_sz, 0, 0, 1) != 0)
5547c478bd9Sstevel@tonic-gate 		*buf = '\0';
5557c478bd9Sstevel@tonic-gate }
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate static int
5587c478bd9Sstevel@tonic-gate get_restarter_time_prop(scf_instance_t *inst, const char *pname,
5597c478bd9Sstevel@tonic-gate     struct timeval *tvp, int ok_if_empty)
5607c478bd9Sstevel@tonic-gate {
5617c478bd9Sstevel@tonic-gate 	int r;
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 	r = inst_get_single_val(inst, SCF_PG_RESTARTER, pname, SCF_TYPE_TIME,
5645f8fc372SToomas Soome 	    tvp, 0, ok_if_empty ? EMPTY_OK : 0, 0, 1);
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 	return (r == 0 ? 0 : -1);
5677c478bd9Sstevel@tonic-gate }
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate static int
5707c478bd9Sstevel@tonic-gate get_restarter_count_prop(scf_instance_t *inst, const char *pname, uint64_t *cp,
5717c478bd9Sstevel@tonic-gate     uint_t flags)
5727c478bd9Sstevel@tonic-gate {
5737c478bd9Sstevel@tonic-gate 	return (inst_get_single_val(inst, SCF_PG_RESTARTER, pname,
5747c478bd9Sstevel@tonic-gate 	    SCF_TYPE_COUNT, cp, 0, flags, 0, 1));
5757c478bd9Sstevel@tonic-gate }
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate /*
5797c478bd9Sstevel@tonic-gate  * Generic functions
5807c478bd9Sstevel@tonic-gate  */
5817c478bd9Sstevel@tonic-gate 
58294501b61Sskamm /*
58394501b61Sskamm  * Return an array of pids associated with the given contract id.
58494501b61Sskamm  * Returned pids are added to the end of the pidsp array.
58594501b61Sskamm  */
58694501b61Sskamm static void
58794501b61Sskamm ctid_to_pids(uint64_t c, pid_t **pidsp, uint_t *np)
58894501b61Sskamm {
58994501b61Sskamm 	ct_stathdl_t ctst;
59094501b61Sskamm 	uint_t m;
59194501b61Sskamm 	int fd;
59294501b61Sskamm 	int r, err;
59394501b61Sskamm 	pid_t *pids;
59494501b61Sskamm 
59594501b61Sskamm 	fd = contract_open(c, NULL, "status", O_RDONLY);
59694501b61Sskamm 	if (fd < 0)
59794501b61Sskamm 		return;
59894501b61Sskamm 
59994501b61Sskamm 	err = ct_status_read(fd, CTD_ALL, &ctst);
60094501b61Sskamm 	if (err != 0) {
60194501b61Sskamm 		uu_warn(gettext("Could not read status of contract "
60294501b61Sskamm 		    "%ld: %s.\n"), c, strerror(err));
60394501b61Sskamm 		(void) close(fd);
60494501b61Sskamm 		return;
60594501b61Sskamm 	}
60694501b61Sskamm 
60794501b61Sskamm 	(void) close(fd);
60894501b61Sskamm 
60994501b61Sskamm 	r = ct_pr_status_get_members(ctst, &pids, &m);
61094501b61Sskamm 	assert(r == 0);
61194501b61Sskamm 
61294501b61Sskamm 	if (m == 0) {
61394501b61Sskamm 		ct_status_free(ctst);
61494501b61Sskamm 		return;
61594501b61Sskamm 	}
61694501b61Sskamm 
61794501b61Sskamm 	*pidsp = realloc(*pidsp, (*np + m) * sizeof (*pidsp));
61894501b61Sskamm 	if (*pidsp == NULL)
61994501b61Sskamm 		uu_die(gettext("Out of memory"));
62094501b61Sskamm 
62194501b61Sskamm 	bcopy(pids, *pidsp + *np, m * sizeof (*pids));
62294501b61Sskamm 	*np += m;
62394501b61Sskamm 
62494501b61Sskamm 	ct_status_free(ctst);
62594501b61Sskamm }
62694501b61Sskamm 
6277c478bd9Sstevel@tonic-gate static int
6287c478bd9Sstevel@tonic-gate propvals_to_pids(scf_propertygroup_t *pg, const char *pname, pid_t **pidsp,
6297c478bd9Sstevel@tonic-gate     uint_t *np, scf_property_t *prop, scf_value_t *val, scf_iter_t *iter)
6307c478bd9Sstevel@tonic-gate {
6317c478bd9Sstevel@tonic-gate 	scf_type_t ty;
6327c478bd9Sstevel@tonic-gate 	uint64_t c;
63394501b61Sskamm 	int r;
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate 	if (scf_pg_get_property(pg, pname, prop) != 0) {
6367c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_NOT_FOUND)
6377c478bd9Sstevel@tonic-gate 			scfdie();
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate 		return (ENOENT);
6407c478bd9Sstevel@tonic-gate 	}
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate 	if (scf_property_type(prop, &ty) != 0)
6437c478bd9Sstevel@tonic-gate 		scfdie();
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate 	if (ty != SCF_TYPE_COUNT)
6467c478bd9Sstevel@tonic-gate 		return (EINVAL);
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 	if (scf_iter_property_values(iter, prop) != 0)
6497c478bd9Sstevel@tonic-gate 		scfdie();
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 	for (;;) {
6527c478bd9Sstevel@tonic-gate 		r = scf_iter_next_value(iter, val);
6537c478bd9Sstevel@tonic-gate 		if (r == -1)
6547c478bd9Sstevel@tonic-gate 			scfdie();
6557c478bd9Sstevel@tonic-gate 		if (r == 0)
6567c478bd9Sstevel@tonic-gate 			break;
6577c478bd9Sstevel@tonic-gate 
6587c478bd9Sstevel@tonic-gate 		if (scf_value_get_count(val, &c) != 0)
6597c478bd9Sstevel@tonic-gate 			scfdie();
6607c478bd9Sstevel@tonic-gate 
66194501b61Sskamm 		ctid_to_pids(c, pidsp, np);
66294501b61Sskamm 	}
6637c478bd9Sstevel@tonic-gate 
66494501b61Sskamm 	return (0);
66594501b61Sskamm }
6667c478bd9Sstevel@tonic-gate 
66794501b61Sskamm /*
66894501b61Sskamm  * Check if instance has general/restarter property that matches
66994501b61Sskamm  * given string.  Restarter string must be in canonified form.
67094501b61Sskamm  * Returns 0 for success; -1 otherwise.
67194501b61Sskamm  */
67294501b61Sskamm static int
67394501b61Sskamm check_for_restarter(scf_instance_t *inst, const char *restarter)
67494501b61Sskamm {
67594501b61Sskamm 	char	*fmri_buf;
67694501b61Sskamm 	char	*fmri_buf_canonified = NULL;
67794501b61Sskamm 	int	ret = -1;
6787c478bd9Sstevel@tonic-gate 
67994501b61Sskamm 	if (inst == NULL)
68094501b61Sskamm 		return (-1);
6817c478bd9Sstevel@tonic-gate 
68294501b61Sskamm 	/* Get restarter */
68394501b61Sskamm 	fmri_buf = safe_malloc(max_scf_fmri_length + 1);
68494501b61Sskamm 	if (inst_get_single_val(inst, SCF_PG_GENERAL,
68594501b61Sskamm 	    SCF_PROPERTY_RESTARTER, SCF_TYPE_ASTRING, fmri_buf,
68694501b61Sskamm 	    max_scf_fmri_length + 1, 0, 0, 1) != 0)
68794501b61Sskamm 		goto out;
68894501b61Sskamm 
68994501b61Sskamm 	fmri_buf_canonified = safe_malloc(max_scf_fmri_length + 1);
69094501b61Sskamm 	if (scf_canonify_fmri(fmri_buf, fmri_buf_canonified,
69194501b61Sskamm 	    (max_scf_fmri_length + 1)) < 0)
69294501b61Sskamm 		goto out;
69394501b61Sskamm 
69494501b61Sskamm 	if (strcmp(fmri_buf, restarter) == 0)
69594501b61Sskamm 		ret = 0;
69694501b61Sskamm 
69794501b61Sskamm out:
69894501b61Sskamm 	free(fmri_buf);
69994501b61Sskamm 	if (fmri_buf_canonified)
70094501b61Sskamm 		free(fmri_buf_canonified);
70194501b61Sskamm 	return (ret);
70294501b61Sskamm }
70394501b61Sskamm 
70494501b61Sskamm /*
70594501b61Sskamm  * Common code that is used by ctids_by_restarter and pids_by_restarter.
70694501b61Sskamm  * Checks for a common restarter and if one is available, it generates
70794501b61Sskamm  * the appropriate filename using wip->fmri and stores that in the
70894501b61Sskamm  * global genfmri_filename.
70994501b61Sskamm  *
71094501b61Sskamm  * Restarters currently supported are: svc:/network/inetd:default
71194501b61Sskamm  * If a restarter specific action is available, then restarter_spec
71294501b61Sskamm  * is set to 1.  If a restarter specific action is not available, then
71394501b61Sskamm  * restarter_spec is set to 0 and a -1 is returned.
71494501b61Sskamm  *
71594501b61Sskamm  * Returns:
71694501b61Sskamm  * 0 if success: restarter specific action found and filename generated
71794501b61Sskamm  * -1 if restarter specific action not found,
71894501b61Sskamm  *    if restarter specific action found but an error was encountered
71994501b61Sskamm  *    during the generation of the wip->fmri based filename
72094501b61Sskamm  */
72194501b61Sskamm static int
72294501b61Sskamm common_by_restarter(scf_instance_t *inst, const char *fmri,
72394501b61Sskamm     int *restarter_specp)
72494501b61Sskamm {
72594501b61Sskamm 	int		ret = -1;
72694501b61Sskamm 	int		r;
72794501b61Sskamm 
72894501b61Sskamm 	/* Check for inetd specific restarter */
72994501b61Sskamm 	if (check_for_restarter(inst, "svc:/network/inetd:default") != 0) {
73094501b61Sskamm 		*restarter_specp = 0;
73194501b61Sskamm 		return (ret);
73294501b61Sskamm 	}
73394501b61Sskamm 
73494501b61Sskamm 	*restarter_specp = 1;
73594501b61Sskamm 
73694501b61Sskamm 	/* Get the ctid filename associated with this instance */
73794501b61Sskamm 	r = gen_filenms_from_fmri(fmri, "ctid", genfmri_filename, NULL);
73894501b61Sskamm 
73994501b61Sskamm 	switch (r) {
74094501b61Sskamm 	case 0:
74194501b61Sskamm 		break;
74294501b61Sskamm 
74394501b61Sskamm 	case -1:
74494501b61Sskamm 		/*
74594501b61Sskamm 		 * Unable to get filename from fmri.  Print warning
74694501b61Sskamm 		 * and return failure with no ctids.
74794501b61Sskamm 		 */
74894501b61Sskamm 		uu_warn(gettext("Unable to read contract ids for %s -- "
74994501b61Sskamm 		    "FMRI is too long\n"), fmri);
75094501b61Sskamm 		return (ret);
75194501b61Sskamm 
75294501b61Sskamm 	case -2:
75394501b61Sskamm 		/*
75494501b61Sskamm 		 * The directory didn't exist, so no contracts.
75594501b61Sskamm 		 * Return failure with no ctids.
75694501b61Sskamm 		 */
75794501b61Sskamm 		return (ret);
75894501b61Sskamm 
75994501b61Sskamm 	default:
76094501b61Sskamm 		uu_warn(gettext("%s:%d: gen_filenms_from_fmri() failed with "
76194501b61Sskamm 		    "unknown error %d\n"), __FILE__, __LINE__, r);
76294501b61Sskamm 		abort();
76394501b61Sskamm 	}
76494501b61Sskamm 
76594501b61Sskamm 	return (0);
76694501b61Sskamm 
76794501b61Sskamm }
76894501b61Sskamm 
76994501b61Sskamm /*
77094501b61Sskamm  * Get or print a contract id using a restarter specific action.
77194501b61Sskamm  *
77294501b61Sskamm  * If the print_flag is not set, this routine gets the single contract
77394501b61Sskamm  * id associated with this instance.
77494501b61Sskamm  * If the print flag is set, then print each contract id found.
77594501b61Sskamm  *
77694501b61Sskamm  * Returns:
77794501b61Sskamm  * 0 if success: restarter specific action found and used with no error
77894501b61Sskamm  * -1 if restarter specific action not found
77994501b61Sskamm  * -1 if restarter specific action found, but there was a failure
78094501b61Sskamm  * -1 if print flag is not set and no contract id is found or multiple
78194501b61Sskamm  *    contract ids were found
78294501b61Sskamm  * E2BIG if print flag is not set, MULTI_OK bit in flag is set and multiple
78394501b61Sskamm  *    contract ids were found
78494501b61Sskamm  */
78594501b61Sskamm static int
78694501b61Sskamm ctids_by_restarter(scf_walkinfo_t *wip, uint64_t *cp, int print_flag,
78794501b61Sskamm     uint_t flags, int *restarter_specp, void (*callback_header)(),
78894501b61Sskamm     void (*callback_ctid)(uint64_t))
78994501b61Sskamm {
79094501b61Sskamm 	FILE		*fp;
79194501b61Sskamm 	int		ret = -1;
79294501b61Sskamm 	int		fscanf_ret;
79394501b61Sskamm 	uint64_t	cp2;
79494501b61Sskamm 	int		rest_ret;
79594501b61Sskamm 
79694501b61Sskamm 	/* Check if callbacks are needed and were passed in */
79794501b61Sskamm 	if (print_flag) {
79894501b61Sskamm 		if ((callback_header == NULL) || (callback_ctid == NULL))
79994501b61Sskamm 			return (ret);
80094501b61Sskamm 	}
80194501b61Sskamm 
80294501b61Sskamm 	/* Check for restarter specific action and generation of filename */
80394501b61Sskamm 	rest_ret = common_by_restarter(wip->inst, wip->fmri, restarter_specp);
80494501b61Sskamm 	if (rest_ret != 0)
80594501b61Sskamm 		return (rest_ret);
80694501b61Sskamm 
80794501b61Sskamm 	/*
80894501b61Sskamm 	 * If fopen fails, then ctid file hasn't been created yet.
80994501b61Sskamm 	 * If print_flag is set, this is ok; otherwise fail.
81094501b61Sskamm 	 */
81194501b61Sskamm 	if ((fp = fopen(genfmri_filename, "r")) == NULL) {
81294501b61Sskamm 		if (print_flag)
81394501b61Sskamm 			return (0);
81494501b61Sskamm 		goto out;
81594501b61Sskamm 	}
81694501b61Sskamm 
81794501b61Sskamm 	if (print_flag) {
81894501b61Sskamm 		/*
81994501b61Sskamm 		 * Print all contract ids that are found.
82094501b61Sskamm 		 * First callback to print ctid header.
82194501b61Sskamm 		 */
82294501b61Sskamm 		callback_header();
82394501b61Sskamm 
82494501b61Sskamm 		/* fscanf may not set errno, so be sure to clear it first */
82594501b61Sskamm 		errno = 0;
82694501b61Sskamm 		while ((fscanf_ret = fscanf(fp, "%llu", cp)) == 1) {
82794501b61Sskamm 			/* Callback to print contract id */
82894501b61Sskamm 			callback_ctid(*cp);
82994501b61Sskamm 			errno = 0;
83094501b61Sskamm 		}
83194501b61Sskamm 		/* EOF is not a failure when no errno. */
83294501b61Sskamm 		if ((fscanf_ret != EOF) || (errno != 0)) {
83394501b61Sskamm 			uu_die(gettext("Unable to read ctid file for %s"),
83494501b61Sskamm 			    wip->fmri);
83594501b61Sskamm 		}
83694501b61Sskamm 		(void) putchar('\n');
83794501b61Sskamm 		ret = 0;
83894501b61Sskamm 	} else {
83994501b61Sskamm 		/* Must find 1 ctid or fail */
84094501b61Sskamm 		if (fscanf(fp, "%llu", cp) == 1) {
84194501b61Sskamm 			/* If 2nd ctid found - fail */
84294501b61Sskamm 			if (fscanf(fp, "%llu", &cp2) == 1) {
84394501b61Sskamm 				if (flags & MULTI_OK)
84494501b61Sskamm 					ret = E2BIG;
84594501b61Sskamm 			} else {
84694501b61Sskamm 				/* Success - found only 1 ctid */
84794501b61Sskamm 				ret = 0;
84894501b61Sskamm 			}
8497c478bd9Sstevel@tonic-gate 		}
85094501b61Sskamm 	}
85194501b61Sskamm 	(void) fclose(fp);
8527c478bd9Sstevel@tonic-gate 
85394501b61Sskamm out:
85494501b61Sskamm 	return (ret);
85594501b61Sskamm }
85694501b61Sskamm 
85794501b61Sskamm /*
85894501b61Sskamm  * Get the process ids associated with an instance using a restarter
85994501b61Sskamm  * specific action.
86094501b61Sskamm  *
86194501b61Sskamm  * Returns:
86294501b61Sskamm  *	0 if success: restarter specific action found and used with no error
86394501b61Sskamm  *	-1 restarter specific action not found or if failure
86494501b61Sskamm  */
86594501b61Sskamm static int
86694501b61Sskamm pids_by_restarter(scf_instance_t *inst, const char *fmri,
86794501b61Sskamm     pid_t **pids, uint_t *np, int *restarter_specp)
86894501b61Sskamm {
86994501b61Sskamm 	uint64_t	c;
87094501b61Sskamm 	FILE		*fp;
87194501b61Sskamm 	int		fscanf_ret;
87294501b61Sskamm 	int		rest_ret;
8737c478bd9Sstevel@tonic-gate 
87494501b61Sskamm 	/* Check for restarter specific action and generation of filename */
87594501b61Sskamm 	rest_ret = common_by_restarter(inst, fmri, restarter_specp);
87694501b61Sskamm 	if (rest_ret != 0)
87794501b61Sskamm 		return (rest_ret);
8787c478bd9Sstevel@tonic-gate 
87994501b61Sskamm 	/*
88094501b61Sskamm 	 * If fopen fails with ENOENT then the ctid file hasn't been
88194501b61Sskamm 	 * created yet so return success.
88294501b61Sskamm 	 * For all other errors - fail with uu_die.
88394501b61Sskamm 	 */
88494501b61Sskamm 	if ((fp = fopen(genfmri_filename, "r")) == NULL) {
88594501b61Sskamm 		if (errno == ENOENT)
88694501b61Sskamm 			return (0);
88794501b61Sskamm 		uu_die(gettext("Unable to open ctid file for %s"), fmri);
88894501b61Sskamm 	}
88994501b61Sskamm 
89094501b61Sskamm 	/* fscanf may not set errno, so be sure to clear it first */
89194501b61Sskamm 	errno = 0;
89294501b61Sskamm 	while ((fscanf_ret = fscanf(fp, "%llu", &c)) == 1) {
89394501b61Sskamm 		if (c == 0) {
89494501b61Sskamm 			(void) fclose(fp);
89594501b61Sskamm 			uu_die(gettext("ctid file for %s has corrupt data"),
89694501b61Sskamm 			    fmri);
89794501b61Sskamm 		}
89894501b61Sskamm 		ctid_to_pids(c, pids, np);
89994501b61Sskamm 		errno = 0;
90094501b61Sskamm 	}
90194501b61Sskamm 	/* EOF is not a failure when no errno. */
90294501b61Sskamm 	if ((fscanf_ret != EOF) || (errno != 0)) {
90394501b61Sskamm 		uu_die(gettext("Unable to read ctid file for %s"), fmri);
9047c478bd9Sstevel@tonic-gate 	}
9057c478bd9Sstevel@tonic-gate 
90694501b61Sskamm 	(void) fclose(fp);
9077c478bd9Sstevel@tonic-gate 	return (0);
9087c478bd9Sstevel@tonic-gate }
9097c478bd9Sstevel@tonic-gate 
9107c478bd9Sstevel@tonic-gate static int
91194501b61Sskamm instance_processes(scf_instance_t *inst, const char *fmri,
91294501b61Sskamm     pid_t **pids, uint_t *np)
9137c478bd9Sstevel@tonic-gate {
9147c478bd9Sstevel@tonic-gate 	scf_iter_t *iter;
9157c478bd9Sstevel@tonic-gate 	int ret;
91694501b61Sskamm 	int restarter_spec;
91794501b61Sskamm 
91894501b61Sskamm 	/* Use the restarter specific get pids routine, if available. */
91994501b61Sskamm 	ret = pids_by_restarter(inst, fmri, pids, np, &restarter_spec);
92094501b61Sskamm 	if (restarter_spec == 1)
92194501b61Sskamm 		return (ret);
9227c478bd9Sstevel@tonic-gate 
9237c478bd9Sstevel@tonic-gate 	if ((iter = scf_iter_create(h)) == NULL)
9247c478bd9Sstevel@tonic-gate 		scfdie();
9257c478bd9Sstevel@tonic-gate 
9267c478bd9Sstevel@tonic-gate 	if (scf_instance_get_pg(inst, SCF_PG_RESTARTER, g_pg) == 0) {
9277c478bd9Sstevel@tonic-gate 		*pids = NULL;
9287c478bd9Sstevel@tonic-gate 		*np = 0;
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate 		(void) propvals_to_pids(g_pg, scf_property_contract, pids, np,
9317c478bd9Sstevel@tonic-gate 		    g_prop, g_val, iter);
9327c478bd9Sstevel@tonic-gate 
9337c478bd9Sstevel@tonic-gate 		(void) propvals_to_pids(g_pg, SCF_PROPERTY_TRANSIENT_CONTRACT,
9347c478bd9Sstevel@tonic-gate 		    pids, np, g_prop, g_val, iter);
9357c478bd9Sstevel@tonic-gate 
9367c478bd9Sstevel@tonic-gate 		ret = 0;
9377c478bd9Sstevel@tonic-gate 	} else {
9387c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_NOT_FOUND)
9397c478bd9Sstevel@tonic-gate 			scfdie();
9407c478bd9Sstevel@tonic-gate 
9417c478bd9Sstevel@tonic-gate 		ret = -1;
9427c478bd9Sstevel@tonic-gate 	}
9437c478bd9Sstevel@tonic-gate 
9447c478bd9Sstevel@tonic-gate 	scf_iter_destroy(iter);
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate 	return (ret);
9477c478bd9Sstevel@tonic-gate }
9487c478bd9Sstevel@tonic-gate 
9497c478bd9Sstevel@tonic-gate /*
9507c478bd9Sstevel@tonic-gate  * Column sprint and sortkey functions
9517c478bd9Sstevel@tonic-gate  */
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate struct column {
9547c478bd9Sstevel@tonic-gate 	const char *name;
9557c478bd9Sstevel@tonic-gate 	int width;
9567c478bd9Sstevel@tonic-gate 
9577c478bd9Sstevel@tonic-gate 	/*
9587c478bd9Sstevel@tonic-gate 	 * This function should write the value for the column into buf, and
9597c478bd9Sstevel@tonic-gate 	 * grow or allocate buf accordingly.  It should always write at least
9607c478bd9Sstevel@tonic-gate 	 * width bytes, blanking unused bytes with spaces.  If the field is
9617c478bd9Sstevel@tonic-gate 	 * greater than the column width we allow it to overlap other columns.
9627c478bd9Sstevel@tonic-gate 	 * In particular, it shouldn't write any null bytes.  (Though an extra
9637c478bd9Sstevel@tonic-gate 	 * null byte past the end is currently tolerated.)  If the property
9647c478bd9Sstevel@tonic-gate 	 * group is non-NULL, then we are dealing with a legacy service.
9657c478bd9Sstevel@tonic-gate 	 */
9667c478bd9Sstevel@tonic-gate 	void (*sprint)(char **, scf_walkinfo_t *);
9677c478bd9Sstevel@tonic-gate 
9687c478bd9Sstevel@tonic-gate 	int sortkey_width;
9697c478bd9Sstevel@tonic-gate 
9707c478bd9Sstevel@tonic-gate 	/*
9717c478bd9Sstevel@tonic-gate 	 * This function should write sortkey_width bytes into buf which will
9727c478bd9Sstevel@tonic-gate 	 * cause memcmp() to sort it properly.  (Unlike sprint() above,
9737c478bd9Sstevel@tonic-gate 	 * however, an extra null byte may overrun the buffer.)  The second
9747c478bd9Sstevel@tonic-gate 	 * argument controls whether the results are sorted in forward or
9757c478bd9Sstevel@tonic-gate 	 * reverse order.
9767c478bd9Sstevel@tonic-gate 	 */
9777c478bd9Sstevel@tonic-gate 	void (*get_sortkey)(char *, int, scf_walkinfo_t *);
9787c478bd9Sstevel@tonic-gate };
9797c478bd9Sstevel@tonic-gate 
9807c478bd9Sstevel@tonic-gate static void
9817c478bd9Sstevel@tonic-gate reverse_bytes(char *buf, size_t len)
9827c478bd9Sstevel@tonic-gate {
9837c478bd9Sstevel@tonic-gate 	int i;
9847c478bd9Sstevel@tonic-gate 
9857c478bd9Sstevel@tonic-gate 	for (i = 0; i < len; ++i)
9867c478bd9Sstevel@tonic-gate 		buf[i] = ~buf[i];
9877c478bd9Sstevel@tonic-gate }
9887c478bd9Sstevel@tonic-gate 
9897c478bd9Sstevel@tonic-gate /* CTID */
9907c478bd9Sstevel@tonic-gate #define	CTID_COLUMN_WIDTH		6
99151f82540SEric Sproul #define	CTID_COLUMN_BUFSIZE		20	/* max ctid_t + space + \0 */
9927c478bd9Sstevel@tonic-gate 
9937c478bd9Sstevel@tonic-gate static void
9947c478bd9Sstevel@tonic-gate sprint_ctid(char **buf, scf_walkinfo_t *wip)
9957c478bd9Sstevel@tonic-gate {
9967c478bd9Sstevel@tonic-gate 	int r;
9977c478bd9Sstevel@tonic-gate 	uint64_t c;
99851f82540SEric Sproul 	size_t newsize = (*buf ? strlen(*buf) : 0) + CTID_COLUMN_BUFSIZE;
9997c478bd9Sstevel@tonic-gate 	char *newbuf = safe_malloc(newsize);
100094501b61Sskamm 	int restarter_spec;
10017c478bd9Sstevel@tonic-gate 
100294501b61Sskamm 	/*
100394501b61Sskamm 	 * Use the restarter specific get pids routine, if available.
100494501b61Sskamm 	 * Only check for non-legacy services (wip->pg == 0).
100594501b61Sskamm 	 */
100694501b61Sskamm 	if (wip->pg != NULL) {
10077c478bd9Sstevel@tonic-gate 		r = pg_get_single_val(wip->pg, scf_property_contract,
10087c478bd9Sstevel@tonic-gate 		    SCF_TYPE_COUNT, &c, 0, EMPTY_OK | MULTI_OK);
100994501b61Sskamm 	} else {
101094501b61Sskamm 		r = ctids_by_restarter(wip, &c, 0, MULTI_OK, &restarter_spec,
101194501b61Sskamm 		    NULL, NULL);
101294501b61Sskamm 		if (restarter_spec == 0) {
101394501b61Sskamm 			/* No restarter specific routine */
101494501b61Sskamm 			r = get_restarter_count_prop(wip->inst,
101594501b61Sskamm 			    scf_property_contract, &c, EMPTY_OK | MULTI_OK);
101694501b61Sskamm 		}
101794501b61Sskamm 	}
10187c478bd9Sstevel@tonic-gate 
10197c478bd9Sstevel@tonic-gate 	if (r == 0)
10207c478bd9Sstevel@tonic-gate 		(void) snprintf(newbuf, newsize, "%s%*lu ",
10217c478bd9Sstevel@tonic-gate 		    *buf ? *buf : "", CTID_COLUMN_WIDTH, (ctid_t)c);
10227c478bd9Sstevel@tonic-gate 	else if (r == E2BIG)
10237c478bd9Sstevel@tonic-gate 		(void) snprintf(newbuf, newsize, "%s%*lu* ",
10247c478bd9Sstevel@tonic-gate 		    *buf ? *buf : "", CTID_COLUMN_WIDTH - 1, (ctid_t)c);
10257c478bd9Sstevel@tonic-gate 	else
10267c478bd9Sstevel@tonic-gate 		(void) snprintf(newbuf, newsize, "%s%*s ",
10277c478bd9Sstevel@tonic-gate 		    *buf ? *buf : "", CTID_COLUMN_WIDTH, "-");
10287c478bd9Sstevel@tonic-gate 	if (*buf)
10297c478bd9Sstevel@tonic-gate 		free(*buf);
10307c478bd9Sstevel@tonic-gate 	*buf = newbuf;
10317c478bd9Sstevel@tonic-gate }
10327c478bd9Sstevel@tonic-gate 
10337c478bd9Sstevel@tonic-gate #define	CTID_SORTKEY_WIDTH		(sizeof (uint64_t))
10347c478bd9Sstevel@tonic-gate 
10357c478bd9Sstevel@tonic-gate static void
10367c478bd9Sstevel@tonic-gate sortkey_ctid(char *buf, int reverse, scf_walkinfo_t *wip)
10377c478bd9Sstevel@tonic-gate {
10387c478bd9Sstevel@tonic-gate 	int r;
10397c478bd9Sstevel@tonic-gate 	uint64_t c;
104094501b61Sskamm 	int restarter_spec;
10417c478bd9Sstevel@tonic-gate 
104294501b61Sskamm 	/*
104394501b61Sskamm 	 * Use the restarter specific get pids routine, if available.
104494501b61Sskamm 	 * Only check for non-legacy services (wip->pg == 0).
104594501b61Sskamm 	 */
104694501b61Sskamm 	if (wip->pg != NULL) {
10477c478bd9Sstevel@tonic-gate 		r = pg_get_single_val(wip->pg, scf_property_contract,
10487c478bd9Sstevel@tonic-gate 		    SCF_TYPE_COUNT, &c, 0, EMPTY_OK);
104994501b61Sskamm 	} else {
105094501b61Sskamm 		r = ctids_by_restarter(wip, &c, 0, MULTI_OK, &restarter_spec,
105194501b61Sskamm 		    NULL, NULL);
105294501b61Sskamm 		if (restarter_spec == 0) {
105394501b61Sskamm 			/* No restarter specific routine */
105494501b61Sskamm 			r = get_restarter_count_prop(wip->inst,
105594501b61Sskamm 			    scf_property_contract, &c, EMPTY_OK);
105694501b61Sskamm 		}
105794501b61Sskamm 	}
10587c478bd9Sstevel@tonic-gate 
10597c478bd9Sstevel@tonic-gate 	if (r == 0) {
10607c478bd9Sstevel@tonic-gate 		/*
10617c478bd9Sstevel@tonic-gate 		 * Use the id itself, but it must be big-endian for this to
10627c478bd9Sstevel@tonic-gate 		 * work.
10637c478bd9Sstevel@tonic-gate 		 */
10647c478bd9Sstevel@tonic-gate 		c = BE_64(c);
10657c478bd9Sstevel@tonic-gate 
10667c478bd9Sstevel@tonic-gate 		bcopy(&c, buf, CTID_SORTKEY_WIDTH);
10677c478bd9Sstevel@tonic-gate 	} else {
10687c478bd9Sstevel@tonic-gate 		bzero(buf, CTID_SORTKEY_WIDTH);
10697c478bd9Sstevel@tonic-gate 	}
10707c478bd9Sstevel@tonic-gate 
10717c478bd9Sstevel@tonic-gate 	if (reverse)
10727c478bd9Sstevel@tonic-gate 		reverse_bytes(buf, CTID_SORTKEY_WIDTH);
10737c478bd9Sstevel@tonic-gate }
10747c478bd9Sstevel@tonic-gate 
10757c478bd9Sstevel@tonic-gate /* DESC */
10767c478bd9Sstevel@tonic-gate #define	DESC_COLUMN_WIDTH	100
10777c478bd9Sstevel@tonic-gate 
10787c478bd9Sstevel@tonic-gate static void
10797c478bd9Sstevel@tonic-gate sprint_desc(char **buf, scf_walkinfo_t *wip)
10807c478bd9Sstevel@tonic-gate {
10817c478bd9Sstevel@tonic-gate 	char *x;
10827c478bd9Sstevel@tonic-gate 	size_t newsize;
10837c478bd9Sstevel@tonic-gate 	char *newbuf;
10847c478bd9Sstevel@tonic-gate 
10857c478bd9Sstevel@tonic-gate 	if (common_name_buf == NULL)
10867c478bd9Sstevel@tonic-gate 		common_name_buf = safe_malloc(max_scf_value_length + 1);
10877c478bd9Sstevel@tonic-gate 
10887c478bd9Sstevel@tonic-gate 	bzero(common_name_buf, max_scf_value_length + 1);
10897c478bd9Sstevel@tonic-gate 
10907c478bd9Sstevel@tonic-gate 	if (wip->pg != NULL) {
10917c478bd9Sstevel@tonic-gate 		common_name_buf[0] = '-';
10927c478bd9Sstevel@tonic-gate 	} else if (inst_get_single_val(wip->inst, SCF_PG_TM_COMMON_NAME, locale,
10937c478bd9Sstevel@tonic-gate 	    SCF_TYPE_USTRING, common_name_buf, max_scf_value_length, 0,
10947c478bd9Sstevel@tonic-gate 	    1, 1) == -1 &&
10957c478bd9Sstevel@tonic-gate 	    inst_get_single_val(wip->inst, SCF_PG_TM_COMMON_NAME, "C",
10967c478bd9Sstevel@tonic-gate 	    SCF_TYPE_USTRING, common_name_buf, max_scf_value_length, 0,
10977c478bd9Sstevel@tonic-gate 	    1, 1) == -1) {
10987c478bd9Sstevel@tonic-gate 		common_name_buf[0] = '-';
10997c478bd9Sstevel@tonic-gate 	}
11007c478bd9Sstevel@tonic-gate 
11017c478bd9Sstevel@tonic-gate 	/*
11027c478bd9Sstevel@tonic-gate 	 * Collapse multi-line tm_common_name values into a single line.
11037c478bd9Sstevel@tonic-gate 	 */
11047c478bd9Sstevel@tonic-gate 	for (x = common_name_buf; *x != '\0'; x++)
11057c478bd9Sstevel@tonic-gate 		if (*x == '\n')
11067c478bd9Sstevel@tonic-gate 			*x = ' ';
11077c478bd9Sstevel@tonic-gate 
11087c478bd9Sstevel@tonic-gate 	if (strlen(common_name_buf) > DESC_COLUMN_WIDTH)
11097c478bd9Sstevel@tonic-gate 		newsize = (*buf ? strlen(*buf) : 0) +
11107c478bd9Sstevel@tonic-gate 		    strlen(common_name_buf) + 1;
11117c478bd9Sstevel@tonic-gate 	else
11127c478bd9Sstevel@tonic-gate 		newsize = (*buf ? strlen(*buf) : 0) + DESC_COLUMN_WIDTH + 1;
11137c478bd9Sstevel@tonic-gate 	newbuf = safe_malloc(newsize);
11147c478bd9Sstevel@tonic-gate 	(void) snprintf(newbuf, newsize, "%s%-*s ", *buf ? *buf : "",
11153eae19d9Swesolows 	    DESC_COLUMN_WIDTH, common_name_buf);
11167c478bd9Sstevel@tonic-gate 	if (*buf)
11177c478bd9Sstevel@tonic-gate 		free(*buf);
11187c478bd9Sstevel@tonic-gate 	*buf = newbuf;
11197c478bd9Sstevel@tonic-gate }
11207c478bd9Sstevel@tonic-gate 
11217c478bd9Sstevel@tonic-gate /* ARGSUSED */
11227c478bd9Sstevel@tonic-gate static void
11237c478bd9Sstevel@tonic-gate sortkey_desc(char *buf, int reverse, scf_walkinfo_t *wip)
11247c478bd9Sstevel@tonic-gate {
11257c478bd9Sstevel@tonic-gate 	bzero(buf, DESC_COLUMN_WIDTH);
11267c478bd9Sstevel@tonic-gate }
11277c478bd9Sstevel@tonic-gate 
11287c478bd9Sstevel@tonic-gate /* State columns (STATE, NSTATE, S, N, SN, STA, NSTA) */
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate static char
11317c478bd9Sstevel@tonic-gate state_to_char(const char *state)
11327c478bd9Sstevel@tonic-gate {
11337c478bd9Sstevel@tonic-gate 	if (strcmp(state, SCF_STATE_STRING_UNINIT) == 0)
11347c478bd9Sstevel@tonic-gate 		return ('u');
11357c478bd9Sstevel@tonic-gate 
11367c478bd9Sstevel@tonic-gate 	if (strcmp(state, SCF_STATE_STRING_OFFLINE) == 0)
11377c478bd9Sstevel@tonic-gate 		return ('0');
11387c478bd9Sstevel@tonic-gate 
11397c478bd9Sstevel@tonic-gate 	if (strcmp(state, SCF_STATE_STRING_ONLINE) == 0)
11407c478bd9Sstevel@tonic-gate 		return ('1');
11417c478bd9Sstevel@tonic-gate 
11427c478bd9Sstevel@tonic-gate 	if (strcmp(state, SCF_STATE_STRING_MAINT) == 0)
11437c478bd9Sstevel@tonic-gate 		return ('m');
11447c478bd9Sstevel@tonic-gate 
11457c478bd9Sstevel@tonic-gate 	if (strcmp(state, SCF_STATE_STRING_DISABLED) == 0)
11467c478bd9Sstevel@tonic-gate 		return ('d');
11477c478bd9Sstevel@tonic-gate 
11487c478bd9Sstevel@tonic-gate 	if (strcmp(state, SCF_STATE_STRING_DEGRADED) == 0)
11497c478bd9Sstevel@tonic-gate 		return ('D');
11507c478bd9Sstevel@tonic-gate 
11517c478bd9Sstevel@tonic-gate 	if (strcmp(state, SCF_STATE_STRING_LEGACY) == 0)
11527c478bd9Sstevel@tonic-gate 		return ('L');
11537c478bd9Sstevel@tonic-gate 
11547c478bd9Sstevel@tonic-gate 	return ('?');
11557c478bd9Sstevel@tonic-gate }
11567c478bd9Sstevel@tonic-gate 
11577c478bd9Sstevel@tonic-gate /* Return true if inst is transitioning. */
11587c478bd9Sstevel@tonic-gate static int
11597c478bd9Sstevel@tonic-gate transitioning(scf_instance_t *inst)
11607c478bd9Sstevel@tonic-gate {
11617c478bd9Sstevel@tonic-gate 	char nstate_name[MAX_SCF_STATE_STRING_SZ];
11627c478bd9Sstevel@tonic-gate 
11637c478bd9Sstevel@tonic-gate 	get_restarter_string_prop(inst, scf_property_next_state, nstate_name,
11647c478bd9Sstevel@tonic-gate 	    sizeof (nstate_name));
11657c478bd9Sstevel@tonic-gate 
11667c478bd9Sstevel@tonic-gate 	return (state_to_char(nstate_name) != '?');
11677c478bd9Sstevel@tonic-gate }
11687c478bd9Sstevel@tonic-gate 
11697c478bd9Sstevel@tonic-gate /* ARGSUSED */
11707c478bd9Sstevel@tonic-gate static void
11717c478bd9Sstevel@tonic-gate sortkey_states(const char *pname, char *buf, int reverse, scf_walkinfo_t *wip)
11727c478bd9Sstevel@tonic-gate {
11737c478bd9Sstevel@tonic-gate 	char state_name[MAX_SCF_STATE_STRING_SZ];
11747c478bd9Sstevel@tonic-gate 
11757c478bd9Sstevel@tonic-gate 	/*
11767c478bd9Sstevel@tonic-gate 	 * Lower numbers are printed first, so these are arranged from least
11777c478bd9Sstevel@tonic-gate 	 * interesting ("legacy run") to most interesting (unknown).
11787c478bd9Sstevel@tonic-gate 	 */
11797c478bd9Sstevel@tonic-gate 	if (wip->pg == NULL) {
11807c478bd9Sstevel@tonic-gate 		get_restarter_string_prop(wip->inst, pname, state_name,
11817c478bd9Sstevel@tonic-gate 		    sizeof (state_name));
11827c478bd9Sstevel@tonic-gate 
11837c478bd9Sstevel@tonic-gate 		if (strcmp(state_name, SCF_STATE_STRING_ONLINE) == 0)
11847c478bd9Sstevel@tonic-gate 			*buf = 2;
11857c478bd9Sstevel@tonic-gate 		else if (strcmp(state_name, SCF_STATE_STRING_DEGRADED) == 0)
11867c478bd9Sstevel@tonic-gate 			*buf = 3;
11877c478bd9Sstevel@tonic-gate 		else if (strcmp(state_name, SCF_STATE_STRING_OFFLINE) == 0)
11887c478bd9Sstevel@tonic-gate 			*buf = 4;
11897c478bd9Sstevel@tonic-gate 		else if (strcmp(state_name, SCF_STATE_STRING_MAINT) == 0)
11907c478bd9Sstevel@tonic-gate 			*buf = 5;
11917c478bd9Sstevel@tonic-gate 		else if (strcmp(state_name, SCF_STATE_STRING_DISABLED) == 0)
11927c478bd9Sstevel@tonic-gate 			*buf = 1;
11937c478bd9Sstevel@tonic-gate 		else if (strcmp(state_name, SCF_STATE_STRING_UNINIT) == 0)
11947c478bd9Sstevel@tonic-gate 			*buf = 6;
11957c478bd9Sstevel@tonic-gate 		else
11967c478bd9Sstevel@tonic-gate 			*buf = 7;
11977c478bd9Sstevel@tonic-gate 	} else
11987c478bd9Sstevel@tonic-gate 		*buf = 0;
11997c478bd9Sstevel@tonic-gate 
12007c478bd9Sstevel@tonic-gate 	if (reverse)
12017c478bd9Sstevel@tonic-gate 		*buf = 255 - *buf;
12027c478bd9Sstevel@tonic-gate }
12037c478bd9Sstevel@tonic-gate 
12047c478bd9Sstevel@tonic-gate static void
12057c478bd9Sstevel@tonic-gate sprint_state(char **buf, scf_walkinfo_t *wip)
12067c478bd9Sstevel@tonic-gate {
12077c478bd9Sstevel@tonic-gate 	char state_name[MAX_SCF_STATE_STRING_SZ + 1];
12087c478bd9Sstevel@tonic-gate 	size_t newsize;
12097c478bd9Sstevel@tonic-gate 	char *newbuf;
12107c478bd9Sstevel@tonic-gate 
12117c478bd9Sstevel@tonic-gate 	if (wip->pg == NULL) {
12127c478bd9Sstevel@tonic-gate 		get_restarter_string_prop(wip->inst, scf_property_state,
12137c478bd9Sstevel@tonic-gate 		    state_name, sizeof (state_name));
12147c478bd9Sstevel@tonic-gate 
12157c478bd9Sstevel@tonic-gate 		/* Don't print blank fields, to ease parsing. */
12167c478bd9Sstevel@tonic-gate 		if (state_name[0] == '\0') {
12177c478bd9Sstevel@tonic-gate 			state_name[0] = '-';
12187c478bd9Sstevel@tonic-gate 			state_name[1] = '\0';
12197c478bd9Sstevel@tonic-gate 		}
12207c478bd9Sstevel@tonic-gate 
12217c478bd9Sstevel@tonic-gate 		if (!opt_nstate_shown && transitioning(wip->inst)) {
12227c478bd9Sstevel@tonic-gate 			/* Append an asterisk if nstate is valid. */
12237c478bd9Sstevel@tonic-gate 			(void) strcat(state_name, "*");
12247c478bd9Sstevel@tonic-gate 		}
12257c478bd9Sstevel@tonic-gate 	} else
12267c478bd9Sstevel@tonic-gate 		(void) strcpy(state_name, SCF_STATE_STRING_LEGACY);
12277c478bd9Sstevel@tonic-gate 
12287c478bd9Sstevel@tonic-gate 	newsize = (*buf ? strlen(*buf) : 0) + MAX_SCF_STATE_STRING_SZ + 2;
12297c478bd9Sstevel@tonic-gate 	newbuf = safe_malloc(newsize);
12307c478bd9Sstevel@tonic-gate 	(void) snprintf(newbuf, newsize, "%s%-*s ", *buf ? *buf : "",
12317c478bd9Sstevel@tonic-gate 	    MAX_SCF_STATE_STRING_SZ + 1, state_name);
12327c478bd9Sstevel@tonic-gate 
12337c478bd9Sstevel@tonic-gate 	if (*buf)
12347c478bd9Sstevel@tonic-gate 		free(*buf);
12357c478bd9Sstevel@tonic-gate 	*buf = newbuf;
12367c478bd9Sstevel@tonic-gate }
12377c478bd9Sstevel@tonic-gate 
12387c478bd9Sstevel@tonic-gate static void
12397c478bd9Sstevel@tonic-gate sortkey_state(char *buf, int reverse, scf_walkinfo_t *wip)
12407c478bd9Sstevel@tonic-gate {
12417c478bd9Sstevel@tonic-gate 	sortkey_states(scf_property_state, buf, reverse, wip);
12427c478bd9Sstevel@tonic-gate }
12437c478bd9Sstevel@tonic-gate 
12447c478bd9Sstevel@tonic-gate static void
12457c478bd9Sstevel@tonic-gate sprint_nstate(char **buf, scf_walkinfo_t *wip)
12467c478bd9Sstevel@tonic-gate {
12477c478bd9Sstevel@tonic-gate 	char next_state_name[MAX_SCF_STATE_STRING_SZ];
12487c478bd9Sstevel@tonic-gate 	boolean_t blank = 0;
12497c478bd9Sstevel@tonic-gate 	size_t newsize;
12507c478bd9Sstevel@tonic-gate 	char *newbuf;
12517c478bd9Sstevel@tonic-gate 
12527c478bd9Sstevel@tonic-gate 	if (wip->pg == NULL) {
12537c478bd9Sstevel@tonic-gate 		get_restarter_string_prop(wip->inst, scf_property_next_state,
12547c478bd9Sstevel@tonic-gate 		    next_state_name, sizeof (next_state_name));
12557c478bd9Sstevel@tonic-gate 
12567c478bd9Sstevel@tonic-gate 		/* Don't print blank fields, to ease parsing. */
12577c478bd9Sstevel@tonic-gate 		if (next_state_name[0] == '\0' ||
12587c478bd9Sstevel@tonic-gate 		    strcmp(next_state_name, SCF_STATE_STRING_NONE) == 0)
12597c478bd9Sstevel@tonic-gate 			blank = 1;
12607c478bd9Sstevel@tonic-gate 	} else
12617c478bd9Sstevel@tonic-gate 		blank = 1;
12627c478bd9Sstevel@tonic-gate 
12637c478bd9Sstevel@tonic-gate 	if (blank) {
12647c478bd9Sstevel@tonic-gate 		next_state_name[0] = '-';
12657c478bd9Sstevel@tonic-gate 		next_state_name[1] = '\0';
12667c478bd9Sstevel@tonic-gate 	}
12677c478bd9Sstevel@tonic-gate 
12687c478bd9Sstevel@tonic-gate 	newsize = (*buf ? strlen(*buf) : 0) + MAX_SCF_STATE_STRING_SZ + 1;
12697c478bd9Sstevel@tonic-gate 	newbuf = safe_malloc(newsize);
12707c478bd9Sstevel@tonic-gate 	(void) snprintf(newbuf, newsize, "%s%-*s ", *buf ? *buf : "",
12717c478bd9Sstevel@tonic-gate 	    MAX_SCF_STATE_STRING_SZ - 1, next_state_name);
12727c478bd9Sstevel@tonic-gate 	if (*buf)
12737c478bd9Sstevel@tonic-gate 		free(*buf);
12747c478bd9Sstevel@tonic-gate 	*buf = newbuf;
12757c478bd9Sstevel@tonic-gate }
12767c478bd9Sstevel@tonic-gate 
12777c478bd9Sstevel@tonic-gate static void
12787c478bd9Sstevel@tonic-gate sortkey_nstate(char *buf, int reverse, scf_walkinfo_t *wip)
12797c478bd9Sstevel@tonic-gate {
12807c478bd9Sstevel@tonic-gate 	sortkey_states(scf_property_next_state, buf, reverse, wip);
12817c478bd9Sstevel@tonic-gate }
12827c478bd9Sstevel@tonic-gate 
12837c478bd9Sstevel@tonic-gate static void
12847c478bd9Sstevel@tonic-gate sprint_s(char **buf, scf_walkinfo_t *wip)
12857c478bd9Sstevel@tonic-gate {
12867c478bd9Sstevel@tonic-gate 	char tmp[3];
12877c478bd9Sstevel@tonic-gate 	char state_name[MAX_SCF_STATE_STRING_SZ];
12887c478bd9Sstevel@tonic-gate 	size_t newsize = (*buf ? strlen(*buf) : 0) + 4;
12897c478bd9Sstevel@tonic-gate 	char *newbuf = safe_malloc(newsize);
12907c478bd9Sstevel@tonic-gate 
12917c478bd9Sstevel@tonic-gate 	if (wip->pg == NULL) {
12927c478bd9Sstevel@tonic-gate 		get_restarter_string_prop(wip->inst, scf_property_state,
12937c478bd9Sstevel@tonic-gate 		    state_name, sizeof (state_name));
12947c478bd9Sstevel@tonic-gate 		tmp[0] = state_to_char(state_name);
12957c478bd9Sstevel@tonic-gate 
12967c478bd9Sstevel@tonic-gate 		if (!opt_nstate_shown && transitioning(wip->inst))
12977c478bd9Sstevel@tonic-gate 			tmp[1] = '*';
12987c478bd9Sstevel@tonic-gate 		else
12997c478bd9Sstevel@tonic-gate 			tmp[1] = ' ';
13007c478bd9Sstevel@tonic-gate 	} else {
13017c478bd9Sstevel@tonic-gate 		tmp[0] = 'L';
13027c478bd9Sstevel@tonic-gate 		tmp[1] = ' ';
13037c478bd9Sstevel@tonic-gate 	}
13047c478bd9Sstevel@tonic-gate 	tmp[2] = ' ';
13057c478bd9Sstevel@tonic-gate 	(void) snprintf(newbuf, newsize, "%s%-*s", *buf ? *buf : "",
13067c478bd9Sstevel@tonic-gate 	    3, tmp);
13077c478bd9Sstevel@tonic-gate 	if (*buf)
13087c478bd9Sstevel@tonic-gate 		free(*buf);
13097c478bd9Sstevel@tonic-gate 	*buf = newbuf;
13107c478bd9Sstevel@tonic-gate }
13117c478bd9Sstevel@tonic-gate 
13127c478bd9Sstevel@tonic-gate static void
13137c478bd9Sstevel@tonic-gate sprint_n(char **buf, scf_walkinfo_t *wip)
13147c478bd9Sstevel@tonic-gate {
13157c478bd9Sstevel@tonic-gate 	char tmp[2];
13167c478bd9Sstevel@tonic-gate 	size_t newsize = (*buf ? strlen(*buf) : 0) + 3;
13177c478bd9Sstevel@tonic-gate 	char *newbuf = safe_malloc(newsize);
13187c478bd9Sstevel@tonic-gate 	char nstate_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_next_state,
13227c478bd9Sstevel@tonic-gate 		    nstate_name, sizeof (nstate_name));
13237c478bd9Sstevel@tonic-gate 
13247c478bd9Sstevel@tonic-gate 		if (strcmp(nstate_name, SCF_STATE_STRING_NONE) == 0)
13257c478bd9Sstevel@tonic-gate 			tmp[0] = '-';
13267c478bd9Sstevel@tonic-gate 		else
13277c478bd9Sstevel@tonic-gate 			tmp[0] = state_to_char(nstate_name);
13287c478bd9Sstevel@tonic-gate 	} else
13297c478bd9Sstevel@tonic-gate 		tmp[0] = '-';
13307c478bd9Sstevel@tonic-gate 
13317c478bd9Sstevel@tonic-gate 	(void) snprintf(newbuf, newsize, "%s%-*s ", *buf ? *buf : "",
13327c478bd9Sstevel@tonic-gate 	    2, tmp);
13337c478bd9Sstevel@tonic-gate 	if (*buf)
13347c478bd9Sstevel@tonic-gate 		free(*buf);
13357c478bd9Sstevel@tonic-gate 	*buf = newbuf;
13367c478bd9Sstevel@tonic-gate }
13377c478bd9Sstevel@tonic-gate 
13387c478bd9Sstevel@tonic-gate static void
13397c478bd9Sstevel@tonic-gate sprint_sn(char **buf, scf_walkinfo_t *wip)
13407c478bd9Sstevel@tonic-gate {
13417c478bd9Sstevel@tonic-gate 	char tmp[3];
13427c478bd9Sstevel@tonic-gate 	size_t newsize = (*buf ? strlen(*buf) : 0) + 4;
13437c478bd9Sstevel@tonic-gate 	char *newbuf = safe_malloc(newsize);
13447c478bd9Sstevel@tonic-gate 	char nstate_name[MAX_SCF_STATE_STRING_SZ];
13457c478bd9Sstevel@tonic-gate 	char state_name[MAX_SCF_STATE_STRING_SZ];
13467c478bd9Sstevel@tonic-gate 
13477c478bd9Sstevel@tonic-gate 	if (wip->pg == NULL) {
13487c478bd9Sstevel@tonic-gate 		get_restarter_string_prop(wip->inst, scf_property_state,
13497c478bd9Sstevel@tonic-gate 		    state_name, sizeof (state_name));
13507c478bd9Sstevel@tonic-gate 		get_restarter_string_prop(wip->inst, scf_property_next_state,
13517c478bd9Sstevel@tonic-gate 		    nstate_name, sizeof (nstate_name));
13527c478bd9Sstevel@tonic-gate 		tmp[0] = state_to_char(state_name);
13537c478bd9Sstevel@tonic-gate 
13547c478bd9Sstevel@tonic-gate 		if (strcmp(nstate_name, SCF_STATE_STRING_NONE) == 0)
13557c478bd9Sstevel@tonic-gate 			tmp[1] = '-';
13567c478bd9Sstevel@tonic-gate 		else
13577c478bd9Sstevel@tonic-gate 			tmp[1] = state_to_char(nstate_name);
13587c478bd9Sstevel@tonic-gate 	} else {
13597c478bd9Sstevel@tonic-gate 		tmp[0] = 'L';
13607c478bd9Sstevel@tonic-gate 		tmp[1] = '-';
13617c478bd9Sstevel@tonic-gate 	}
13627c478bd9Sstevel@tonic-gate 
13637c478bd9Sstevel@tonic-gate 	tmp[2] = ' ';
13647c478bd9Sstevel@tonic-gate 	(void) snprintf(newbuf, newsize, "%s%-*s ", *buf ? *buf : "",
13657c478bd9Sstevel@tonic-gate 	    3, tmp);
13667c478bd9Sstevel@tonic-gate 	if (*buf)
13677c478bd9Sstevel@tonic-gate 		free(*buf);
13687c478bd9Sstevel@tonic-gate 	*buf = newbuf;
13697c478bd9Sstevel@tonic-gate }
13707c478bd9Sstevel@tonic-gate 
13717c478bd9Sstevel@tonic-gate /* ARGSUSED */
13727c478bd9Sstevel@tonic-gate static void
13737c478bd9Sstevel@tonic-gate sortkey_sn(char *buf, int reverse, scf_walkinfo_t *wip)
13747c478bd9Sstevel@tonic-gate {
13757c478bd9Sstevel@tonic-gate 	sortkey_state(buf, reverse, wip);
13767c478bd9Sstevel@tonic-gate 	sortkey_nstate(buf + 1, reverse, wip);
13777c478bd9Sstevel@tonic-gate }
13787c478bd9Sstevel@tonic-gate 
13797c478bd9Sstevel@tonic-gate static const char *
13807c478bd9Sstevel@tonic-gate state_abbrev(const char *state)
13817c478bd9Sstevel@tonic-gate {
13827c478bd9Sstevel@tonic-gate 	if (strcmp(state, SCF_STATE_STRING_UNINIT) == 0)
13837c478bd9Sstevel@tonic-gate 		return ("UN");
13847c478bd9Sstevel@tonic-gate 	if (strcmp(state, SCF_STATE_STRING_OFFLINE) == 0)
13857c478bd9Sstevel@tonic-gate 		return ("OFF");
13867c478bd9Sstevel@tonic-gate 	if (strcmp(state, SCF_STATE_STRING_ONLINE) == 0)
13877c478bd9Sstevel@tonic-gate 		return ("ON");
13887c478bd9Sstevel@tonic-gate 	if (strcmp(state, SCF_STATE_STRING_MAINT) == 0)
13897c478bd9Sstevel@tonic-gate 		return ("MNT");
13907c478bd9Sstevel@tonic-gate 	if (strcmp(state, SCF_STATE_STRING_DISABLED) == 0)
13917c478bd9Sstevel@tonic-gate 		return ("DIS");
13927c478bd9Sstevel@tonic-gate 	if (strcmp(state, SCF_STATE_STRING_DEGRADED) == 0)
13937c478bd9Sstevel@tonic-gate 		return ("DGD");
13947c478bd9Sstevel@tonic-gate 	if (strcmp(state, SCF_STATE_STRING_LEGACY) == 0)
13957c478bd9Sstevel@tonic-gate 		return ("LRC");
13967c478bd9Sstevel@tonic-gate 
13977c478bd9Sstevel@tonic-gate 	return ("?");
13987c478bd9Sstevel@tonic-gate }
13997c478bd9Sstevel@tonic-gate 
14007c478bd9Sstevel@tonic-gate static void
14017c478bd9Sstevel@tonic-gate sprint_sta(char **buf, scf_walkinfo_t *wip)
14027c478bd9Sstevel@tonic-gate {
14037c478bd9Sstevel@tonic-gate 	char state_name[MAX_SCF_STATE_STRING_SZ];
14047c478bd9Sstevel@tonic-gate 	char sta[5];
14057c478bd9Sstevel@tonic-gate 	size_t newsize = (*buf ? strlen(*buf) : 0) + 6;
14067c478bd9Sstevel@tonic-gate 	char *newbuf = safe_malloc(newsize);
14077c478bd9Sstevel@tonic-gate 
14087c478bd9Sstevel@tonic-gate 	if (wip->pg == NULL)
14097c478bd9Sstevel@tonic-gate 		get_restarter_string_prop(wip->inst, scf_property_state,
14107c478bd9Sstevel@tonic-gate 		    state_name, sizeof (state_name));
14117c478bd9Sstevel@tonic-gate 	else
14127c478bd9Sstevel@tonic-gate 		(void) strcpy(state_name, SCF_STATE_STRING_LEGACY);
14137c478bd9Sstevel@tonic-gate 
14147c478bd9Sstevel@tonic-gate 	(void) strcpy(sta, state_abbrev(state_name));
14157c478bd9Sstevel@tonic-gate 
14167c478bd9Sstevel@tonic-gate 	if (wip->pg == NULL && !opt_nstate_shown && transitioning(wip->inst))
14177c478bd9Sstevel@tonic-gate 		(void) strcat(sta, "*");
14187c478bd9Sstevel@tonic-gate 
14197c478bd9Sstevel@tonic-gate 	(void) snprintf(newbuf, newsize, "%s%-4s ", *buf ? *buf : "", sta);
14207c478bd9Sstevel@tonic-gate 	if (*buf)
14217c478bd9Sstevel@tonic-gate 		free(*buf);
14227c478bd9Sstevel@tonic-gate 	*buf = newbuf;
14237c478bd9Sstevel@tonic-gate }
14247c478bd9Sstevel@tonic-gate 
14257c478bd9Sstevel@tonic-gate static void
14267c478bd9Sstevel@tonic-gate sprint_nsta(char **buf, scf_walkinfo_t *wip)
14277c478bd9Sstevel@tonic-gate {
14287c478bd9Sstevel@tonic-gate 	char state_name[MAX_SCF_STATE_STRING_SZ];
14297c478bd9Sstevel@tonic-gate 	size_t newsize = (*buf ? strlen(*buf) : 0) + 6;
14307c478bd9Sstevel@tonic-gate 	char *newbuf = safe_malloc(newsize);
14317c478bd9Sstevel@tonic-gate 
14327c478bd9Sstevel@tonic-gate 	if (wip->pg == NULL)
14337c478bd9Sstevel@tonic-gate 		get_restarter_string_prop(wip->inst, scf_property_next_state,
14347c478bd9Sstevel@tonic-gate 		    state_name, sizeof (state_name));
14357c478bd9Sstevel@tonic-gate 	else
14367c478bd9Sstevel@tonic-gate 		(void) strcpy(state_name, SCF_STATE_STRING_NONE);
14377c478bd9Sstevel@tonic-gate 
14387c478bd9Sstevel@tonic-gate 	if (strcmp(state_name, SCF_STATE_STRING_NONE) == 0)
14397c478bd9Sstevel@tonic-gate 		(void) snprintf(newbuf, newsize, "%s%-4s ", *buf ? *buf : "",
14407c478bd9Sstevel@tonic-gate 		    "-");
14417c478bd9Sstevel@tonic-gate 	else
14427c478bd9Sstevel@tonic-gate 		(void) snprintf(newbuf, newsize, "%s%-4s ", *buf ? *buf : "",
14437c478bd9Sstevel@tonic-gate 		    state_abbrev(state_name));
14447c478bd9Sstevel@tonic-gate 	if (*buf)
14457c478bd9Sstevel@tonic-gate 		free(*buf);
14467c478bd9Sstevel@tonic-gate 	*buf = newbuf;
14477c478bd9Sstevel@tonic-gate }
14487c478bd9Sstevel@tonic-gate 
14497c478bd9Sstevel@tonic-gate /* FMRI */
14507c478bd9Sstevel@tonic-gate #define	FMRI_COLUMN_WIDTH	50
14517c478bd9Sstevel@tonic-gate static void
14527c478bd9Sstevel@tonic-gate sprint_fmri(char **buf, scf_walkinfo_t *wip)
14537c478bd9Sstevel@tonic-gate {
14547c478bd9Sstevel@tonic-gate 	char *fmri_buf = safe_malloc(max_scf_fmri_length + 1);
14557c478bd9Sstevel@tonic-gate 	size_t newsize;
14567c478bd9Sstevel@tonic-gate 	char *newbuf;
14577c478bd9Sstevel@tonic-gate 
14587c478bd9Sstevel@tonic-gate 	if (wip->pg == NULL) {
14597c478bd9Sstevel@tonic-gate 		if (scf_instance_to_fmri(wip->inst, fmri_buf,
14607c478bd9Sstevel@tonic-gate 		    max_scf_fmri_length + 1) == -1)
14617c478bd9Sstevel@tonic-gate 			scfdie();
14627c478bd9Sstevel@tonic-gate 	} else {
14637b209c2cSacruz 		(void) strcpy(fmri_buf, SCF_FMRI_LEGACY_PREFIX);
14647c478bd9Sstevel@tonic-gate 		if (pg_get_single_val(wip->pg, SCF_LEGACY_PROPERTY_NAME,
14657b209c2cSacruz 		    SCF_TYPE_ASTRING, fmri_buf +
14667b209c2cSacruz 		    sizeof (SCF_FMRI_LEGACY_PREFIX) - 1,
14677b209c2cSacruz 		    max_scf_fmri_length + 1 -
14687b209c2cSacruz 		    (sizeof (SCF_FMRI_LEGACY_PREFIX) - 1), 0) != 0)
14697c478bd9Sstevel@tonic-gate 			(void) strcat(fmri_buf, LEGACY_UNKNOWN);
14707c478bd9Sstevel@tonic-gate 	}
14717c478bd9Sstevel@tonic-gate 
14727c478bd9Sstevel@tonic-gate 	if (strlen(fmri_buf) > FMRI_COLUMN_WIDTH)
14737c478bd9Sstevel@tonic-gate 		newsize = (*buf ? strlen(*buf) : 0) + strlen(fmri_buf) + 2;
14747c478bd9Sstevel@tonic-gate 	else
14757c478bd9Sstevel@tonic-gate 		newsize = (*buf ? strlen(*buf) : 0) + FMRI_COLUMN_WIDTH + 2;
14767c478bd9Sstevel@tonic-gate 	newbuf = safe_malloc(newsize);
14777c478bd9Sstevel@tonic-gate 	(void) snprintf(newbuf, newsize, "%s%-*s ", *buf ? *buf : "",
14787c478bd9Sstevel@tonic-gate 	    FMRI_COLUMN_WIDTH, fmri_buf);
14797c478bd9Sstevel@tonic-gate 	free(fmri_buf);
14807c478bd9Sstevel@tonic-gate 	if (*buf)
14817c478bd9Sstevel@tonic-gate 		free(*buf);
14827c478bd9Sstevel@tonic-gate 	*buf = newbuf;
14837c478bd9Sstevel@tonic-gate }
14847c478bd9Sstevel@tonic-gate 
14857c478bd9Sstevel@tonic-gate static void
14867c478bd9Sstevel@tonic-gate sortkey_fmri(char *buf, int reverse, scf_walkinfo_t *wip)
14877c478bd9Sstevel@tonic-gate {
14887c478bd9Sstevel@tonic-gate 	char *tmp = NULL;
14897c478bd9Sstevel@tonic-gate 
14907c478bd9Sstevel@tonic-gate 	sprint_fmri(&tmp, wip);
14917c478bd9Sstevel@tonic-gate 	bcopy(tmp, buf, FMRI_COLUMN_WIDTH);
14927c478bd9Sstevel@tonic-gate 	free(tmp);
14937c478bd9Sstevel@tonic-gate 	if (reverse)
14947c478bd9Sstevel@tonic-gate 		reverse_bytes(buf, FMRI_COLUMN_WIDTH);
14957c478bd9Sstevel@tonic-gate }
14967c478bd9Sstevel@tonic-gate 
14977c478bd9Sstevel@tonic-gate /* Component columns */
14987c478bd9Sstevel@tonic-gate #define	COMPONENT_COLUMN_WIDTH	20
14997c478bd9Sstevel@tonic-gate static void
15007c478bd9Sstevel@tonic-gate sprint_scope(char **buf, scf_walkinfo_t *wip)
15017c478bd9Sstevel@tonic-gate {
15027c478bd9Sstevel@tonic-gate 	char *scope_buf = safe_malloc(max_scf_name_length + 1);
15037c478bd9Sstevel@tonic-gate 	size_t newsize = (*buf ? strlen(*buf) : 0) + COMPONENT_COLUMN_WIDTH + 2;
15047c478bd9Sstevel@tonic-gate 	char *newbuf = safe_malloc(newsize);
15057c478bd9Sstevel@tonic-gate 
15067c478bd9Sstevel@tonic-gate 	assert(wip->scope != NULL);
15077c478bd9Sstevel@tonic-gate 
15087c478bd9Sstevel@tonic-gate 	if (scf_scope_get_name(wip->scope, scope_buf, max_scf_name_length) < 0)
15097c478bd9Sstevel@tonic-gate 		scfdie();
15107c478bd9Sstevel@tonic-gate 
15117c478bd9Sstevel@tonic-gate 	(void) snprintf(newbuf, newsize, "%s%-*s ", *buf ? *buf : "",
15127c478bd9Sstevel@tonic-gate 	    COMPONENT_COLUMN_WIDTH, scope_buf);
15137c478bd9Sstevel@tonic-gate 	if (*buf)
15147c478bd9Sstevel@tonic-gate 		free(*buf);
15157c478bd9Sstevel@tonic-gate 	*buf = newbuf;
15167c478bd9Sstevel@tonic-gate 	free(scope_buf);
15177c478bd9Sstevel@tonic-gate }
15187c478bd9Sstevel@tonic-gate 
15197c478bd9Sstevel@tonic-gate static void
15207c478bd9Sstevel@tonic-gate sortkey_scope(char *buf, int reverse, scf_walkinfo_t *wip)
15217c478bd9Sstevel@tonic-gate {
15227c478bd9Sstevel@tonic-gate 	char *tmp = NULL;
15237c478bd9Sstevel@tonic-gate 
15247c478bd9Sstevel@tonic-gate 	sprint_scope(&tmp, wip);
15257c478bd9Sstevel@tonic-gate 	bcopy(tmp, buf, COMPONENT_COLUMN_WIDTH);
15267c478bd9Sstevel@tonic-gate 	free(tmp);
15277c478bd9Sstevel@tonic-gate 	if (reverse)
15287c478bd9Sstevel@tonic-gate 		reverse_bytes(buf, COMPONENT_COLUMN_WIDTH);
15297c478bd9Sstevel@tonic-gate }
15307c478bd9Sstevel@tonic-gate 
15317c478bd9Sstevel@tonic-gate static void
15327c478bd9Sstevel@tonic-gate sprint_service(char **buf, scf_walkinfo_t *wip)
15337c478bd9Sstevel@tonic-gate {
15347c478bd9Sstevel@tonic-gate 	char *svc_buf = safe_malloc(max_scf_name_length + 1);
15357c478bd9Sstevel@tonic-gate 	char *newbuf;
15367c478bd9Sstevel@tonic-gate 	size_t newsize;
15377c478bd9Sstevel@tonic-gate 
15387c478bd9Sstevel@tonic-gate 	if (wip->pg == NULL) {
15397c478bd9Sstevel@tonic-gate 		if (scf_service_get_name(wip->svc, svc_buf,
15407c478bd9Sstevel@tonic-gate 		    max_scf_name_length + 1) < 0)
15417c478bd9Sstevel@tonic-gate 			scfdie();
15427c478bd9Sstevel@tonic-gate 	} else {
15437c478bd9Sstevel@tonic-gate 		if (pg_get_single_val(wip->pg, "name", SCF_TYPE_ASTRING,
15447c478bd9Sstevel@tonic-gate 		    svc_buf, max_scf_name_length + 1, EMPTY_OK) != 0)
15457c478bd9Sstevel@tonic-gate 			(void) strcpy(svc_buf, LEGACY_UNKNOWN);
15467c478bd9Sstevel@tonic-gate 	}
15477c478bd9Sstevel@tonic-gate 
15487c478bd9Sstevel@tonic-gate 
15497c478bd9Sstevel@tonic-gate 	if (strlen(svc_buf) > COMPONENT_COLUMN_WIDTH)
15507c478bd9Sstevel@tonic-gate 		newsize = (*buf ? strlen(*buf) : 0) + strlen(svc_buf) + 2;
15517c478bd9Sstevel@tonic-gate 	else
15527c478bd9Sstevel@tonic-gate 		newsize = (*buf ? strlen(*buf) : 0) +
15537c478bd9Sstevel@tonic-gate 		    COMPONENT_COLUMN_WIDTH + 2;
15547c478bd9Sstevel@tonic-gate 	newbuf = safe_malloc(newsize);
15557c478bd9Sstevel@tonic-gate 	(void) snprintf(newbuf, newsize, "%s%-*s ", *buf ? *buf : "",
15567c478bd9Sstevel@tonic-gate 	    COMPONENT_COLUMN_WIDTH, svc_buf);
15577c478bd9Sstevel@tonic-gate 	free(svc_buf);
15587c478bd9Sstevel@tonic-gate 	if (*buf)
15597c478bd9Sstevel@tonic-gate 		free(*buf);
15607c478bd9Sstevel@tonic-gate 	*buf = newbuf;
15617c478bd9Sstevel@tonic-gate }
15627c478bd9Sstevel@tonic-gate 
15637c478bd9Sstevel@tonic-gate static void
15647c478bd9Sstevel@tonic-gate sortkey_service(char *buf, int reverse, scf_walkinfo_t *wip)
15657c478bd9Sstevel@tonic-gate {
15667c478bd9Sstevel@tonic-gate 	char *tmp = NULL;
15677c478bd9Sstevel@tonic-gate 
15687c478bd9Sstevel@tonic-gate 	sprint_service(&tmp, wip);
15697c478bd9Sstevel@tonic-gate 	bcopy(tmp, buf, COMPONENT_COLUMN_WIDTH);
15707c478bd9Sstevel@tonic-gate 	free(tmp);
15717c478bd9Sstevel@tonic-gate 	if (reverse)
15727c478bd9Sstevel@tonic-gate 		reverse_bytes(buf, COMPONENT_COLUMN_WIDTH);
15737c478bd9Sstevel@tonic-gate }
15747c478bd9Sstevel@tonic-gate 
15757c478bd9Sstevel@tonic-gate /* INST */
15767c478bd9Sstevel@tonic-gate static void
15777c478bd9Sstevel@tonic-gate sprint_instance(char **buf, scf_walkinfo_t *wip)
15787c478bd9Sstevel@tonic-gate {
15797c478bd9Sstevel@tonic-gate 	char *tmp = safe_malloc(max_scf_name_length + 1);
15807c478bd9Sstevel@tonic-gate 	size_t newsize = (*buf ? strlen(*buf) : 0) + COMPONENT_COLUMN_WIDTH + 2;
15817c478bd9Sstevel@tonic-gate 	char *newbuf = safe_malloc(newsize);
15827c478bd9Sstevel@tonic-gate 
15837c478bd9Sstevel@tonic-gate 	if (wip->pg == NULL) {
15847c478bd9Sstevel@tonic-gate 		if (scf_instance_get_name(wip->inst, tmp,
15857c478bd9Sstevel@tonic-gate 		    max_scf_name_length + 1) < 0)
15867c478bd9Sstevel@tonic-gate 			scfdie();
15877c478bd9Sstevel@tonic-gate 	} else {
15887c478bd9Sstevel@tonic-gate 		tmp[0] = '-';
15897c478bd9Sstevel@tonic-gate 		tmp[1] = '\0';
15907c478bd9Sstevel@tonic-gate 	}
15917c478bd9Sstevel@tonic-gate 
15927c478bd9Sstevel@tonic-gate 	(void) snprintf(newbuf, newsize, "%s%-*s ", *buf ? *buf : "",
15937c478bd9Sstevel@tonic-gate 	    COMPONENT_COLUMN_WIDTH, tmp);
15947c478bd9Sstevel@tonic-gate 	if (*buf)
15957c478bd9Sstevel@tonic-gate 		free(*buf);
15967c478bd9Sstevel@tonic-gate 	*buf = newbuf;
15977c478bd9Sstevel@tonic-gate 	free(tmp);
15987c478bd9Sstevel@tonic-gate }
15997c478bd9Sstevel@tonic-gate 
16007c478bd9Sstevel@tonic-gate static void
16017c478bd9Sstevel@tonic-gate sortkey_instance(char *buf, int reverse, scf_walkinfo_t *wip)
16027c478bd9Sstevel@tonic-gate {
16037c478bd9Sstevel@tonic-gate 	char *tmp = NULL;
16047c478bd9Sstevel@tonic-gate 
16057c478bd9Sstevel@tonic-gate 	sprint_instance(&tmp, wip);
16067c478bd9Sstevel@tonic-gate 	bcopy(tmp, buf, COMPONENT_COLUMN_WIDTH);
16077c478bd9Sstevel@tonic-gate 	free(tmp);
16087c478bd9Sstevel@tonic-gate 	if (reverse)
16097c478bd9Sstevel@tonic-gate 		reverse_bytes(buf, COMPONENT_COLUMN_WIDTH);
16107c478bd9Sstevel@tonic-gate }
16117c478bd9Sstevel@tonic-gate 
16127c478bd9Sstevel@tonic-gate /* STIME */
16137c478bd9Sstevel@tonic-gate #define	STIME_COLUMN_WIDTH		8
16147c478bd9Sstevel@tonic-gate #define	FORMAT_TIME			"%k:%M:%S"
16157c478bd9Sstevel@tonic-gate #define	FORMAT_DATE			"%b_%d  "
16167c478bd9Sstevel@tonic-gate #define	FORMAT_YEAR			"%Y    "
16177c478bd9Sstevel@tonic-gate 
161861402014Slianep /*
161961402014Slianep  * sprint_stime() will allocate a new buffer and snprintf the services's
162061402014Slianep  * state timestamp.  If the timestamp is unavailable for some reason
162161402014Slianep  * a '-' is given instead.
162261402014Slianep  */
16237c478bd9Sstevel@tonic-gate static void
16247c478bd9Sstevel@tonic-gate sprint_stime(char **buf, scf_walkinfo_t *wip)
16257c478bd9Sstevel@tonic-gate {
16267c478bd9Sstevel@tonic-gate 	int r;
16277c478bd9Sstevel@tonic-gate 	struct timeval tv;
16287c478bd9Sstevel@tonic-gate 	time_t then;
16297c478bd9Sstevel@tonic-gate 	struct tm *tm;
16307c478bd9Sstevel@tonic-gate 	char st_buf[STIME_COLUMN_WIDTH + 1];
16317c478bd9Sstevel@tonic-gate 	size_t newsize = (*buf ? strlen(*buf) : 0) + STIME_COLUMN_WIDTH + 2;
16327c478bd9Sstevel@tonic-gate 	char *newbuf = safe_malloc(newsize);
16337c478bd9Sstevel@tonic-gate 
16347c478bd9Sstevel@tonic-gate 	if (wip->pg == NULL) {
16357c478bd9Sstevel@tonic-gate 		r = get_restarter_time_prop(wip->inst,
16367c478bd9Sstevel@tonic-gate 		    SCF_PROPERTY_STATE_TIMESTAMP, &tv, 0);
16377c478bd9Sstevel@tonic-gate 	} else {
16387c478bd9Sstevel@tonic-gate 		r = pg_get_single_val(wip->pg, SCF_PROPERTY_STATE_TIMESTAMP,
16395f8fc372SToomas Soome 		    SCF_TYPE_TIME, &tv, 0, 0);
16407c478bd9Sstevel@tonic-gate 	}
16417c478bd9Sstevel@tonic-gate 
16427c478bd9Sstevel@tonic-gate 	if (r != 0) {
164361402014Slianep 		/*
164461402014Slianep 		 * There's something amiss with our service
164561402014Slianep 		 * so we'll print a '-' for STIME.
164661402014Slianep 		 */
16477c478bd9Sstevel@tonic-gate 		(void) snprintf(newbuf, newsize, "%s%-*s", *buf ? *buf : "",
164861402014Slianep 		    STIME_COLUMN_WIDTH + 1, "-");
164961402014Slianep 	} else {
165061402014Slianep 		/* tv should be valid so we'll format it */
165161402014Slianep 		then = (time_t)tv.tv_sec;
16527c478bd9Sstevel@tonic-gate 
165361402014Slianep 		tm = localtime(&then);
165461402014Slianep 		/*
165561402014Slianep 		 * Print time if started within the past 24 hours, print date
165661402014Slianep 		 * if within the past 12 months or, finally, print year if
165761402014Slianep 		 * started greater than 12 months ago.
165861402014Slianep 		 */
165961402014Slianep 		if (now - then < 24 * 60 * 60) {
166061402014Slianep 			(void) strftime(st_buf, sizeof (st_buf),
166161402014Slianep 			    gettext(FORMAT_TIME), tm);
166261402014Slianep 		} else if (now - then < 12 * 30 * 24 * 60 * 60) {
166361402014Slianep 			(void) strftime(st_buf, sizeof (st_buf),
166461402014Slianep 			    gettext(FORMAT_DATE), tm);
166561402014Slianep 		} else {
166661402014Slianep 			(void) strftime(st_buf, sizeof (st_buf),
166761402014Slianep 			    gettext(FORMAT_YEAR), tm);
166861402014Slianep 		}
166961402014Slianep 		(void) snprintf(newbuf, newsize, "%s%-*s ", *buf ? *buf : "",
167061402014Slianep 		    STIME_COLUMN_WIDTH + 1, st_buf);
167161402014Slianep 	}
16727c478bd9Sstevel@tonic-gate 	if (*buf)
16737c478bd9Sstevel@tonic-gate 		free(*buf);
16747c478bd9Sstevel@tonic-gate 	*buf = newbuf;
16757c478bd9Sstevel@tonic-gate }
16767c478bd9Sstevel@tonic-gate 
16777c478bd9Sstevel@tonic-gate #define	STIME_SORTKEY_WIDTH		(sizeof (uint64_t) + sizeof (uint32_t))
16787c478bd9Sstevel@tonic-gate 
16797c478bd9Sstevel@tonic-gate /* ARGSUSED */
16807c478bd9Sstevel@tonic-gate static void
16817c478bd9Sstevel@tonic-gate sortkey_stime(char *buf, int reverse, scf_walkinfo_t *wip)
16827c478bd9Sstevel@tonic-gate {
16837c478bd9Sstevel@tonic-gate 	struct timeval tv;
16847c478bd9Sstevel@tonic-gate 	int r;
16857c478bd9Sstevel@tonic-gate 
16867c478bd9Sstevel@tonic-gate 	if (wip->pg == NULL)
16877c478bd9Sstevel@tonic-gate 		r = get_restarter_time_prop(wip->inst,
16887c478bd9Sstevel@tonic-gate 		    SCF_PROPERTY_STATE_TIMESTAMP, &tv, 0);
16897c478bd9Sstevel@tonic-gate 	else
16907c478bd9Sstevel@tonic-gate 		r = pg_get_single_val(wip->pg, SCF_PROPERTY_STATE_TIMESTAMP,
16915f8fc372SToomas Soome 		    SCF_TYPE_TIME, &tv, 0, 0);
16927c478bd9Sstevel@tonic-gate 
16937c478bd9Sstevel@tonic-gate 	if (r == 0) {
16947c478bd9Sstevel@tonic-gate 		int64_t sec;
16957c478bd9Sstevel@tonic-gate 		int32_t us;
16967c478bd9Sstevel@tonic-gate 
16977c478bd9Sstevel@tonic-gate 		/* Stick it straight into the buffer. */
16987c478bd9Sstevel@tonic-gate 		sec = tv.tv_sec;
16997c478bd9Sstevel@tonic-gate 		us = tv.tv_usec;
17007c478bd9Sstevel@tonic-gate 
17017c478bd9Sstevel@tonic-gate 		sec = BE_64(sec);
17027c478bd9Sstevel@tonic-gate 		us = BE_32(us);
17037c478bd9Sstevel@tonic-gate 		bcopy(&sec, buf, sizeof (sec));
17047c478bd9Sstevel@tonic-gate 		bcopy(&us, buf + sizeof (sec), sizeof (us));
17057c478bd9Sstevel@tonic-gate 	} else {
17067c478bd9Sstevel@tonic-gate 		bzero(buf, STIME_SORTKEY_WIDTH);
17077c478bd9Sstevel@tonic-gate 	}
17087c478bd9Sstevel@tonic-gate 
17097c478bd9Sstevel@tonic-gate 	if (reverse)
17107c478bd9Sstevel@tonic-gate 		reverse_bytes(buf, STIME_SORTKEY_WIDTH);
17117c478bd9Sstevel@tonic-gate }
17127c478bd9Sstevel@tonic-gate 
1713048b0279SBryan Cantrill /* ZONE */
1714048b0279SBryan Cantrill #define	ZONE_COLUMN_WIDTH	16
1715048b0279SBryan Cantrill /*ARGSUSED*/
1716048b0279SBryan Cantrill static void
1717048b0279SBryan Cantrill sprint_zone(char **buf, scf_walkinfo_t *wip)
1718048b0279SBryan Cantrill {
1719048b0279SBryan Cantrill 	size_t newsize;
1720048b0279SBryan Cantrill 	char *newbuf, *zonename = g_zonename, b[ZONENAME_MAX];
1721048b0279SBryan Cantrill 
1722048b0279SBryan Cantrill 	if (zonename == NULL) {
1723048b0279SBryan Cantrill 		zoneid_t zoneid = getzoneid();
1724048b0279SBryan Cantrill 
1725048b0279SBryan Cantrill 		if (getzonenamebyid(zoneid, b, sizeof (b)) < 0)
1726048b0279SBryan Cantrill 			uu_die(gettext("could not determine zone name"));
1727048b0279SBryan Cantrill 
1728048b0279SBryan Cantrill 		zonename = b;
1729048b0279SBryan Cantrill 	}
1730048b0279SBryan Cantrill 
1731048b0279SBryan Cantrill 	if (strlen(zonename) > ZONE_COLUMN_WIDTH)
1732048b0279SBryan Cantrill 		newsize = (*buf ? strlen(*buf) : 0) + strlen(zonename) + 2;
1733048b0279SBryan Cantrill 	else
1734048b0279SBryan Cantrill 		newsize = (*buf ? strlen(*buf) : 0) + ZONE_COLUMN_WIDTH + 2;
1735048b0279SBryan Cantrill 
1736048b0279SBryan Cantrill 	newbuf = safe_malloc(newsize);
1737048b0279SBryan Cantrill 	(void) snprintf(newbuf, newsize, "%s%-*s ", *buf ? *buf : "",
1738048b0279SBryan Cantrill 	    ZONE_COLUMN_WIDTH, zonename);
1739048b0279SBryan Cantrill 
1740048b0279SBryan Cantrill 	if (*buf)
1741048b0279SBryan Cantrill 		free(*buf);
1742048b0279SBryan Cantrill 	*buf = newbuf;
1743048b0279SBryan Cantrill }
1744048b0279SBryan Cantrill 
1745048b0279SBryan Cantrill static void
1746048b0279SBryan Cantrill sortkey_zone(char *buf, int reverse, scf_walkinfo_t *wip)
1747048b0279SBryan Cantrill {
1748048b0279SBryan Cantrill 	char *tmp = NULL;
1749048b0279SBryan Cantrill 
1750048b0279SBryan Cantrill 	sprint_zone(&tmp, wip);
1751048b0279SBryan Cantrill 	bcopy(tmp, buf, ZONE_COLUMN_WIDTH);
1752048b0279SBryan Cantrill 	free(tmp);
1753048b0279SBryan Cantrill 	if (reverse)
1754048b0279SBryan Cantrill 		reverse_bytes(buf, ZONE_COLUMN_WIDTH);
1755048b0279SBryan Cantrill }
17567c478bd9Sstevel@tonic-gate 
17577c478bd9Sstevel@tonic-gate /*
17587c478bd9Sstevel@tonic-gate  * Information about columns which can be displayed.  If you add something,
17597c478bd9Sstevel@tonic-gate  * check MAX_COLUMN_NAME_LENGTH_STR & update description_of_column() below.
17607c478bd9Sstevel@tonic-gate  */
17617c478bd9Sstevel@tonic-gate static const struct column columns[] = {
17627c478bd9Sstevel@tonic-gate 	{ "CTID", CTID_COLUMN_WIDTH, sprint_ctid,
17637c478bd9Sstevel@tonic-gate 		CTID_SORTKEY_WIDTH, sortkey_ctid },
17647c478bd9Sstevel@tonic-gate 	{ "DESC", DESC_COLUMN_WIDTH, sprint_desc,
17657c478bd9Sstevel@tonic-gate 		DESC_COLUMN_WIDTH, sortkey_desc },
17667c478bd9Sstevel@tonic-gate 	{ "FMRI", FMRI_COLUMN_WIDTH, sprint_fmri,
17677c478bd9Sstevel@tonic-gate 		FMRI_COLUMN_WIDTH, sortkey_fmri },
17687c478bd9Sstevel@tonic-gate 	{ "INST", COMPONENT_COLUMN_WIDTH, sprint_instance,
17697c478bd9Sstevel@tonic-gate 		COMPONENT_COLUMN_WIDTH, sortkey_instance },
17707c478bd9Sstevel@tonic-gate 	{ "N", 1,  sprint_n, 1, sortkey_nstate },
17717c478bd9Sstevel@tonic-gate 	{ "NSTA", 4, sprint_nsta, 1, sortkey_nstate },
17727c478bd9Sstevel@tonic-gate 	{ "NSTATE", MAX_SCF_STATE_STRING_SZ - 1, sprint_nstate,
17737c478bd9Sstevel@tonic-gate 		1, sortkey_nstate },
17747c478bd9Sstevel@tonic-gate 	{ "S", 2, sprint_s, 1, sortkey_state },
17757c478bd9Sstevel@tonic-gate 	{ "SCOPE", COMPONENT_COLUMN_WIDTH, sprint_scope,
17767c478bd9Sstevel@tonic-gate 		COMPONENT_COLUMN_WIDTH, sortkey_scope },
17777c478bd9Sstevel@tonic-gate 	{ "SN", 2, sprint_sn, 2, sortkey_sn },
17787c478bd9Sstevel@tonic-gate 	{ "SVC", COMPONENT_COLUMN_WIDTH, sprint_service,
17797c478bd9Sstevel@tonic-gate 		COMPONENT_COLUMN_WIDTH, sortkey_service },
17807c478bd9Sstevel@tonic-gate 	{ "STA", 4, sprint_sta, 1, sortkey_state },
17817c478bd9Sstevel@tonic-gate 	{ "STATE", MAX_SCF_STATE_STRING_SZ - 1 + 1, sprint_state,
17827c478bd9Sstevel@tonic-gate 		1, sortkey_state },
17837c478bd9Sstevel@tonic-gate 	{ "STIME", STIME_COLUMN_WIDTH, sprint_stime,
17847c478bd9Sstevel@tonic-gate 		STIME_SORTKEY_WIDTH, sortkey_stime },
1785048b0279SBryan Cantrill 	{ "ZONE", ZONE_COLUMN_WIDTH, sprint_zone,
1786048b0279SBryan Cantrill 		ZONE_COLUMN_WIDTH, sortkey_zone },
17877c478bd9Sstevel@tonic-gate };
17887c478bd9Sstevel@tonic-gate 
17897c478bd9Sstevel@tonic-gate #define	MAX_COLUMN_NAME_LENGTH_STR	"6"
17907c478bd9Sstevel@tonic-gate 
17917c478bd9Sstevel@tonic-gate static const int ncolumns = sizeof (columns) / sizeof (columns[0]);
17927c478bd9Sstevel@tonic-gate 
17937c478bd9Sstevel@tonic-gate /*
17947c478bd9Sstevel@tonic-gate  * Necessary thanks to gettext() & xgettext.
17957c478bd9Sstevel@tonic-gate  */
17967c478bd9Sstevel@tonic-gate static const char *
17977c478bd9Sstevel@tonic-gate description_of_column(int c)
17987c478bd9Sstevel@tonic-gate {
17997c478bd9Sstevel@tonic-gate 	const char *s = NULL;
18007c478bd9Sstevel@tonic-gate 
18017c478bd9Sstevel@tonic-gate 	switch (c) {
18027c478bd9Sstevel@tonic-gate 	case 0:
18037c478bd9Sstevel@tonic-gate 		s = gettext("contract ID for service (see contract(4))");
18047c478bd9Sstevel@tonic-gate 		break;
18057c478bd9Sstevel@tonic-gate 	case 1:
18067c478bd9Sstevel@tonic-gate 		s = gettext("human-readable description of the service");
18077c478bd9Sstevel@tonic-gate 		break;
18087c478bd9Sstevel@tonic-gate 	case 2:
18097c478bd9Sstevel@tonic-gate 		s = gettext("Fault Managed Resource Identifier for service");
18107c478bd9Sstevel@tonic-gate 		break;
18117c478bd9Sstevel@tonic-gate 	case 3:
18127c478bd9Sstevel@tonic-gate 		s = gettext("portion of the FMRI indicating service instance");
18137c478bd9Sstevel@tonic-gate 		break;
18147c478bd9Sstevel@tonic-gate 	case 4:
18157c478bd9Sstevel@tonic-gate 		s = gettext("abbreviation for next state (if in transition)");
18167c478bd9Sstevel@tonic-gate 		break;
18177c478bd9Sstevel@tonic-gate 	case 5:
18187c478bd9Sstevel@tonic-gate 		s = gettext("abbreviation for next state (if in transition)");
18197c478bd9Sstevel@tonic-gate 		break;
18207c478bd9Sstevel@tonic-gate 	case 6:
18217c478bd9Sstevel@tonic-gate 		s = gettext("name for next state (if in transition)");
18227c478bd9Sstevel@tonic-gate 		break;
18237c478bd9Sstevel@tonic-gate 	case 7:
18247c478bd9Sstevel@tonic-gate 		s = gettext("abbreviation for current state");
18257c478bd9Sstevel@tonic-gate 		break;
18267c478bd9Sstevel@tonic-gate 	case 8:
18277c478bd9Sstevel@tonic-gate 		s = gettext("name for scope associated with service");
18287c478bd9Sstevel@tonic-gate 		break;
18297c478bd9Sstevel@tonic-gate 	case 9:
18307c478bd9Sstevel@tonic-gate 		s = gettext("abbreviation for current state and next state");
18317c478bd9Sstevel@tonic-gate 		break;
18327c478bd9Sstevel@tonic-gate 	case 10:
18337c478bd9Sstevel@tonic-gate 		s = gettext("portion of the FMRI representing service name");
18347c478bd9Sstevel@tonic-gate 		break;
18357c478bd9Sstevel@tonic-gate 	case 11:
18367c478bd9Sstevel@tonic-gate 		s = gettext("abbreviation for current state");
18377c478bd9Sstevel@tonic-gate 		break;
18387c478bd9Sstevel@tonic-gate 	case 12:
18397c478bd9Sstevel@tonic-gate 		s = gettext("name for current state");
18407c478bd9Sstevel@tonic-gate 		break;
18417c478bd9Sstevel@tonic-gate 	case 13:
18427c478bd9Sstevel@tonic-gate 		s = gettext("time of last state change");
18437c478bd9Sstevel@tonic-gate 		break;
1844048b0279SBryan Cantrill 	case 14:
1845048b0279SBryan Cantrill 		s = gettext("name of zone");
1846048b0279SBryan Cantrill 		break;
18477c478bd9Sstevel@tonic-gate 	}
18487c478bd9Sstevel@tonic-gate 
18497c478bd9Sstevel@tonic-gate 	assert(s != NULL);
18507c478bd9Sstevel@tonic-gate 	return (s);
18517c478bd9Sstevel@tonic-gate }
18527c478bd9Sstevel@tonic-gate 
18537c478bd9Sstevel@tonic-gate 
18547c478bd9Sstevel@tonic-gate static void
1855a5f0d1fcSToomas Soome print_usage(const char *progname, FILE *f)
18567c478bd9Sstevel@tonic-gate {
18577c478bd9Sstevel@tonic-gate 	(void) fprintf(f, gettext(
18587c478bd9Sstevel@tonic-gate 	    "Usage: %1$s [-aHpv] [-o col[,col ... ]] [-R restarter] "
1859048b0279SBryan Cantrill 	    "[-sS col] [-Z | -z zone ]\n            [<service> ...]\n"
18607c478bd9Sstevel@tonic-gate 	    "       %1$s -d | -D [-Hpv] [-o col[,col ... ]] [-sS col] "
1861048b0279SBryan Cantrill 	    "[-Z | -z zone ]\n            [<service> ...]\n"
1862d5c6878fSBryan Cantrill 	    "       %1$s [-l | -L] [-Z | -z zone] <service> ...\n"
1863048b0279SBryan Cantrill 	    "       %1$s -x [-v] [-Z | -z zone] [<service> ...]\n"
18647c478bd9Sstevel@tonic-gate 	    "       %1$s -?\n"), progname);
18657c478bd9Sstevel@tonic-gate }
18667c478bd9Sstevel@tonic-gate 
1867a5f0d1fcSToomas Soome static __NORETURN void
1868a5f0d1fcSToomas Soome argserr(const char *progname)
1869a5f0d1fcSToomas Soome {
1870a5f0d1fcSToomas Soome 	print_usage(progname, stderr);
1871a5f0d1fcSToomas Soome 	exit(UU_EXIT_USAGE);
1872a5f0d1fcSToomas Soome }
18737c478bd9Sstevel@tonic-gate 
18747c478bd9Sstevel@tonic-gate static void
18757c478bd9Sstevel@tonic-gate print_help(const char *progname)
18767c478bd9Sstevel@tonic-gate {
18777c478bd9Sstevel@tonic-gate 	int i;
18787c478bd9Sstevel@tonic-gate 
1879a5f0d1fcSToomas Soome 	print_usage(progname, stdout);
18807c478bd9Sstevel@tonic-gate 
18817c478bd9Sstevel@tonic-gate 	(void) printf(gettext("\n"
18827c478bd9Sstevel@tonic-gate 	"\t-a  list all service instances rather than "
18837c478bd9Sstevel@tonic-gate 	"only those that are enabled\n"
18847c478bd9Sstevel@tonic-gate 	"\t-d  list dependencies of the specified service(s)\n"
18857c478bd9Sstevel@tonic-gate 	"\t-D  list dependents of the specified service(s)\n"
18867c478bd9Sstevel@tonic-gate 	"\t-H  omit header line from output\n"
18877c478bd9Sstevel@tonic-gate 	"\t-l  list detailed information about the specified service(s)\n"
1888d5c6878fSBryan Cantrill 	"\t-L  list the log file associated with the specified service(s)\n"
18897c478bd9Sstevel@tonic-gate 	"\t-o  list only the specified columns in the output\n"
18907c478bd9Sstevel@tonic-gate 	"\t-p  list process IDs and names associated with each service\n"
18917c478bd9Sstevel@tonic-gate 	"\t-R  list only those services with the specified restarter\n"
18927c478bd9Sstevel@tonic-gate 	"\t-s  sort output in ascending order by the specified column(s)\n"
18937c478bd9Sstevel@tonic-gate 	"\t-S  sort output in descending order by the specified column(s)\n"
18947c478bd9Sstevel@tonic-gate 	"\t-v  list verbose information appropriate to the type of output\n"
18957c478bd9Sstevel@tonic-gate 	"\t-x  explain the status of services that might require maintenance,\n"
18967c478bd9Sstevel@tonic-gate 	"\t    or explain the status of the specified service(s)\n"
1897048b0279SBryan Cantrill 	"\t-z  from global zone, show services in a specified zone\n"
1898048b0279SBryan Cantrill 	"\t-Z  from global zone, show services in all zones\n"
18997c478bd9Sstevel@tonic-gate 	"\n\t"
19007c478bd9Sstevel@tonic-gate 	"Services can be specified using an FMRI, abbreviation, or fnmatch(5)\n"
19017c478bd9Sstevel@tonic-gate 	"\tpattern, as shown in these examples for svc:/network/smtp:sendmail\n"
19027c478bd9Sstevel@tonic-gate 	"\n"
19037c478bd9Sstevel@tonic-gate 	"\t%1$s [opts] svc:/network/smtp:sendmail\n"
19047c478bd9Sstevel@tonic-gate 	"\t%1$s [opts] network/smtp:sendmail\n"
19057c478bd9Sstevel@tonic-gate 	"\t%1$s [opts] network/*mail\n"
19067c478bd9Sstevel@tonic-gate 	"\t%1$s [opts] network/smtp\n"
19077c478bd9Sstevel@tonic-gate 	"\t%1$s [opts] smtp:sendmail\n"
19087c478bd9Sstevel@tonic-gate 	"\t%1$s [opts] smtp\n"
19097c478bd9Sstevel@tonic-gate 	"\t%1$s [opts] sendmail\n"
19107c478bd9Sstevel@tonic-gate 	"\n\t"
19117c478bd9Sstevel@tonic-gate 	"Columns for output or sorting can be specified using these names:\n"
19127c478bd9Sstevel@tonic-gate 	"\n"), progname);
19137c478bd9Sstevel@tonic-gate 
19147c478bd9Sstevel@tonic-gate 	for (i = 0; i < ncolumns; i++) {
19157c478bd9Sstevel@tonic-gate 		(void) printf("\t%-" MAX_COLUMN_NAME_LENGTH_STR "s  %s\n",
19167c478bd9Sstevel@tonic-gate 		    columns[i].name, description_of_column(i));
19177c478bd9Sstevel@tonic-gate 	}
19187c478bd9Sstevel@tonic-gate }
19197c478bd9Sstevel@tonic-gate 
19207c478bd9Sstevel@tonic-gate 
19217c478bd9Sstevel@tonic-gate /*
19227c478bd9Sstevel@tonic-gate  * A getsubopt()-like function which returns an index into the columns table.
19237c478bd9Sstevel@tonic-gate  * On success, *optionp is set to point to the next sub-option, or the
19247c478bd9Sstevel@tonic-gate  * terminating null if there are none.
19257c478bd9Sstevel@tonic-gate  */
19267c478bd9Sstevel@tonic-gate static int
19277c478bd9Sstevel@tonic-gate getcolumnopt(char **optionp)
19287c478bd9Sstevel@tonic-gate {
19297c478bd9Sstevel@tonic-gate 	char *str = *optionp, *cp;
19307c478bd9Sstevel@tonic-gate 	int i;
19317c478bd9Sstevel@tonic-gate 
19327c478bd9Sstevel@tonic-gate 	assert(optionp != NULL);
19337c478bd9Sstevel@tonic-gate 	assert(*optionp != NULL);
19347c478bd9Sstevel@tonic-gate 
19357c478bd9Sstevel@tonic-gate 	cp = strchr(*optionp, ',');
19367c478bd9Sstevel@tonic-gate 	if (cp != NULL)
19377c478bd9Sstevel@tonic-gate 		*cp = '\0';
19387c478bd9Sstevel@tonic-gate 
19397c478bd9Sstevel@tonic-gate 	for (i = 0; i < ncolumns; ++i) {
19407c478bd9Sstevel@tonic-gate 		if (strcasecmp(str, columns[i].name) == 0) {
19417c478bd9Sstevel@tonic-gate 			if (cp != NULL)
19427c478bd9Sstevel@tonic-gate 				*optionp = cp + 1;
19437c478bd9Sstevel@tonic-gate 			else
19447c478bd9Sstevel@tonic-gate 				*optionp = strchr(*optionp, '\0');
19457c478bd9Sstevel@tonic-gate 
19467c478bd9Sstevel@tonic-gate 			return (i);
19477c478bd9Sstevel@tonic-gate 		}
19487c478bd9Sstevel@tonic-gate 	}
19497c478bd9Sstevel@tonic-gate 
19507c478bd9Sstevel@tonic-gate 	return (-1);
19517c478bd9Sstevel@tonic-gate }
19527c478bd9Sstevel@tonic-gate 
19537c478bd9Sstevel@tonic-gate static void
19547c478bd9Sstevel@tonic-gate print_header()
19557c478bd9Sstevel@tonic-gate {
19567c478bd9Sstevel@tonic-gate 	int i;
19577c478bd9Sstevel@tonic-gate 	char *line_buf, *cp;
19587c478bd9Sstevel@tonic-gate 
19597c478bd9Sstevel@tonic-gate 	line_buf = safe_malloc(line_sz);
19607c478bd9Sstevel@tonic-gate 	cp = line_buf;
19617c478bd9Sstevel@tonic-gate 	for (i = 0; i < opt_cnum; ++i) {
19627c478bd9Sstevel@tonic-gate 		const struct column * const colp = &columns[opt_columns[i]];
19637c478bd9Sstevel@tonic-gate 
19647c478bd9Sstevel@tonic-gate 		(void) snprintf(cp, colp->width + 1, "%-*s", colp->width,
19657c478bd9Sstevel@tonic-gate 		    colp->name);
19667c478bd9Sstevel@tonic-gate 		cp += colp->width;
19677c478bd9Sstevel@tonic-gate 		*cp++ = ' ';
19687c478bd9Sstevel@tonic-gate 	}
19697c478bd9Sstevel@tonic-gate 
19707c478bd9Sstevel@tonic-gate 	/* Trim the trailing whitespace */
19717c478bd9Sstevel@tonic-gate 	--cp;
19727c478bd9Sstevel@tonic-gate 	while (*cp == ' ')
19737c478bd9Sstevel@tonic-gate 		--cp;
19747c478bd9Sstevel@tonic-gate 	*(cp+1) = '\0';
19757c478bd9Sstevel@tonic-gate 	(void) puts(line_buf);
19767c478bd9Sstevel@tonic-gate 
19777c478bd9Sstevel@tonic-gate 	free(line_buf);
19787c478bd9Sstevel@tonic-gate }
19797c478bd9Sstevel@tonic-gate 
19807c478bd9Sstevel@tonic-gate 
19817c478bd9Sstevel@tonic-gate 
19827c478bd9Sstevel@tonic-gate /*
19837c478bd9Sstevel@tonic-gate  * Long listing (-l) functions.
19847c478bd9Sstevel@tonic-gate  */
19857c478bd9Sstevel@tonic-gate 
19867c478bd9Sstevel@tonic-gate static int
19877c478bd9Sstevel@tonic-gate pidcmp(const void *l, const void *r)
19887c478bd9Sstevel@tonic-gate {
19897c478bd9Sstevel@tonic-gate 	pid_t lp = *(pid_t *)l, rp = *(pid_t *)r;
19907c478bd9Sstevel@tonic-gate 
19917c478bd9Sstevel@tonic-gate 	if (lp < rp)
19927c478bd9Sstevel@tonic-gate 		return (-1);
19937c478bd9Sstevel@tonic-gate 	if (lp > rp)
19947c478bd9Sstevel@tonic-gate 		return (1);
19957c478bd9Sstevel@tonic-gate 	return (0);
19967c478bd9Sstevel@tonic-gate }
19977c478bd9Sstevel@tonic-gate 
19987c478bd9Sstevel@tonic-gate /*
19997c478bd9Sstevel@tonic-gate  * This is the strlen() of the longest label ("description"), plus intercolumn
20007c478bd9Sstevel@tonic-gate  * space.
20017c478bd9Sstevel@tonic-gate  */
20027c478bd9Sstevel@tonic-gate #define	DETAILED_WIDTH	(11 + 2)
20037c478bd9Sstevel@tonic-gate 
200494501b61Sskamm /*
200594501b61Sskamm  * Callback routine to print header for contract id.
200694501b61Sskamm  * Called by ctids_by_restarter and print_detailed.
200794501b61Sskamm  */
200894501b61Sskamm static void
200994501b61Sskamm print_ctid_header()
201094501b61Sskamm {
201194501b61Sskamm 	(void) printf("%-*s", DETAILED_WIDTH, "contract_id");
201294501b61Sskamm }
201394501b61Sskamm 
201494501b61Sskamm /*
201594501b61Sskamm  * Callback routine to print a contract id.
201694501b61Sskamm  * Called by ctids_by_restarter and print_detailed.
201794501b61Sskamm  */
201894501b61Sskamm static void
201994501b61Sskamm print_ctid_detailed(uint64_t c)
202094501b61Sskamm {
202194501b61Sskamm 	(void) printf("%lu ", (ctid_t)c);
202294501b61Sskamm }
202394501b61Sskamm 
20247c478bd9Sstevel@tonic-gate static void
202594501b61Sskamm detailed_list_processes(scf_walkinfo_t *wip)
20267c478bd9Sstevel@tonic-gate {
20277c478bd9Sstevel@tonic-gate 	uint64_t c;
20287c478bd9Sstevel@tonic-gate 	pid_t *pids;
20297c478bd9Sstevel@tonic-gate 	uint_t i, n;
20307c478bd9Sstevel@tonic-gate 	psinfo_t psi;
20317c478bd9Sstevel@tonic-gate 
203294501b61Sskamm 	if (get_restarter_count_prop(wip->inst, scf_property_contract, &c,
20337c478bd9Sstevel@tonic-gate 	    EMPTY_OK) != 0)
20347c478bd9Sstevel@tonic-gate 		return;
20357c478bd9Sstevel@tonic-gate 
203694501b61Sskamm 	if (instance_processes(wip->inst, wip->fmri, &pids, &n) != 0)
20377c478bd9Sstevel@tonic-gate 		return;
20387c478bd9Sstevel@tonic-gate 
20397c478bd9Sstevel@tonic-gate 	qsort(pids, n, sizeof (*pids), pidcmp);
20407c478bd9Sstevel@tonic-gate 
20417c478bd9Sstevel@tonic-gate 	for (i = 0; i < n; ++i) {
20427c478bd9Sstevel@tonic-gate 		(void) printf("%-*s%lu", DETAILED_WIDTH, gettext("process"),
20437c478bd9Sstevel@tonic-gate 		    pids[i]);
20447c478bd9Sstevel@tonic-gate 
2045*81ff72c5SJason King 		if (proc_get_psinfo(pids[i], &psi) == 0 && !IS_ZOMBIE(&psi))
20467c478bd9Sstevel@tonic-gate 			(void) printf(" %.*s", PRARGSZ, psi.pr_psargs);
20477c478bd9Sstevel@tonic-gate 
20487c478bd9Sstevel@tonic-gate 		(void) putchar('\n');
20497c478bd9Sstevel@tonic-gate 	}
20507c478bd9Sstevel@tonic-gate 
20517c478bd9Sstevel@tonic-gate 	free(pids);
20527c478bd9Sstevel@tonic-gate }
20537c478bd9Sstevel@tonic-gate 
20547c478bd9Sstevel@tonic-gate /*
20557c478bd9Sstevel@tonic-gate  * Determines the state of a dependency.  If the FMRI specifies a file, then we
20567c478bd9Sstevel@tonic-gate  * fake up a state based on whether we can access the file.
20577c478bd9Sstevel@tonic-gate  */
20587c478bd9Sstevel@tonic-gate static void
20597c478bd9Sstevel@tonic-gate get_fmri_state(char *fmri, char *state, size_t state_sz)
20607c478bd9Sstevel@tonic-gate {
20617c478bd9Sstevel@tonic-gate 	char *lfmri;
20627c478bd9Sstevel@tonic-gate 	const char *svc_name, *inst_name, *pg_name, *path;
20637c478bd9Sstevel@tonic-gate 	scf_service_t *svc;
20647c478bd9Sstevel@tonic-gate 	scf_instance_t *inst;
20657c478bd9Sstevel@tonic-gate 	scf_iter_t *iter;
20667c478bd9Sstevel@tonic-gate 
20677c478bd9Sstevel@tonic-gate 	lfmri = safe_strdup(fmri);
20687c478bd9Sstevel@tonic-gate 
20697c478bd9Sstevel@tonic-gate 	/*
20707c478bd9Sstevel@tonic-gate 	 * Check for file:// dependencies
20717c478bd9Sstevel@tonic-gate 	 */
20727c478bd9Sstevel@tonic-gate 	if (scf_parse_file_fmri(lfmri, NULL, &path) == SCF_SUCCESS) {
20737c478bd9Sstevel@tonic-gate 		struct stat64 statbuf;
20747c478bd9Sstevel@tonic-gate 		const char *msg;
20757c478bd9Sstevel@tonic-gate 
20767c478bd9Sstevel@tonic-gate 		if (stat64(path, &statbuf) == 0)
20777c478bd9Sstevel@tonic-gate 			msg = "online";
20787c478bd9Sstevel@tonic-gate 		else if (errno == ENOENT)
20797c478bd9Sstevel@tonic-gate 			msg = "absent";
20807c478bd9Sstevel@tonic-gate 		else
20817c478bd9Sstevel@tonic-gate 			msg = "unknown";
20827c478bd9Sstevel@tonic-gate 
20837c478bd9Sstevel@tonic-gate 		(void) strlcpy(state, msg, state_sz);
20847c478bd9Sstevel@tonic-gate 		return;
20857c478bd9Sstevel@tonic-gate 	}
20867c478bd9Sstevel@tonic-gate 
20877c478bd9Sstevel@tonic-gate 	/*
20887c478bd9Sstevel@tonic-gate 	 * scf_parse_file_fmri() may have overwritten part of the string, so
20897c478bd9Sstevel@tonic-gate 	 * copy it back.
20907c478bd9Sstevel@tonic-gate 	 */
20917c478bd9Sstevel@tonic-gate 	(void) strcpy(lfmri, fmri);
20927c478bd9Sstevel@tonic-gate 
20937c478bd9Sstevel@tonic-gate 	if (scf_parse_svc_fmri(lfmri, NULL, &svc_name, &inst_name,
20947c478bd9Sstevel@tonic-gate 	    &pg_name, NULL) != SCF_SUCCESS) {
20957c478bd9Sstevel@tonic-gate 		free(lfmri);
20967c478bd9Sstevel@tonic-gate 		(void) strlcpy(state, "invalid", state_sz);
20977c478bd9Sstevel@tonic-gate 		return;
20987c478bd9Sstevel@tonic-gate 	}
20997c478bd9Sstevel@tonic-gate 
21007c478bd9Sstevel@tonic-gate 	free(lfmri);
21017c478bd9Sstevel@tonic-gate 
21027c478bd9Sstevel@tonic-gate 	if (svc_name == NULL || pg_name != NULL) {
21037c478bd9Sstevel@tonic-gate 		(void) strlcpy(state, "invalid", state_sz);
21047c478bd9Sstevel@tonic-gate 		return;
21057c478bd9Sstevel@tonic-gate 	}
21067c478bd9Sstevel@tonic-gate 
21077c478bd9Sstevel@tonic-gate 	if (inst_name != NULL) {
21087c478bd9Sstevel@tonic-gate 		/* instance: get state */
21097c478bd9Sstevel@tonic-gate 		inst = scf_instance_create(h);
21107c478bd9Sstevel@tonic-gate 		if (inst == NULL)
21117c478bd9Sstevel@tonic-gate 			scfdie();
21127c478bd9Sstevel@tonic-gate 
21137c478bd9Sstevel@tonic-gate 		if (scf_handle_decode_fmri(h, fmri, NULL, NULL, inst, NULL,
21147c478bd9Sstevel@tonic-gate 		    NULL, SCF_DECODE_FMRI_EXACT) == SCF_SUCCESS)
21157c478bd9Sstevel@tonic-gate 			get_restarter_string_prop(inst, scf_property_state,
21167c478bd9Sstevel@tonic-gate 			    state, state_sz);
21177c478bd9Sstevel@tonic-gate 		else {
21187c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
21197c478bd9Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
21207c478bd9Sstevel@tonic-gate 				(void) strlcpy(state, "invalid", state_sz);
21217c478bd9Sstevel@tonic-gate 				break;
21227c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_FOUND:
21237c478bd9Sstevel@tonic-gate 				(void) strlcpy(state, "absent", state_sz);
21247c478bd9Sstevel@tonic-gate 				break;
21257c478bd9Sstevel@tonic-gate 
21267c478bd9Sstevel@tonic-gate 			default:
21277c478bd9Sstevel@tonic-gate 				scfdie();
21287c478bd9Sstevel@tonic-gate 			}
21297c478bd9Sstevel@tonic-gate 		}
21307c478bd9Sstevel@tonic-gate 
21317c478bd9Sstevel@tonic-gate 		scf_instance_destroy(inst);
21327c478bd9Sstevel@tonic-gate 		return;
21337c478bd9Sstevel@tonic-gate 	}
21347c478bd9Sstevel@tonic-gate 
21357c478bd9Sstevel@tonic-gate 	/*
21367c478bd9Sstevel@tonic-gate 	 * service: If only one instance, use that state.  Otherwise, say
21377c478bd9Sstevel@tonic-gate 	 * "multiple".
21387c478bd9Sstevel@tonic-gate 	 */
21397c478bd9Sstevel@tonic-gate 	if ((svc = scf_service_create(h)) == NULL ||
21407c478bd9Sstevel@tonic-gate 	    (inst = scf_instance_create(h)) == NULL ||
21417c478bd9Sstevel@tonic-gate 	    (iter = scf_iter_create(h)) == NULL)
21427c478bd9Sstevel@tonic-gate 		scfdie();
21437c478bd9Sstevel@tonic-gate 
21447c478bd9Sstevel@tonic-gate 	if (scf_handle_decode_fmri(h, fmri, NULL, svc, NULL, NULL, NULL,
21457c478bd9Sstevel@tonic-gate 	    SCF_DECODE_FMRI_EXACT) != SCF_SUCCESS) {
21467c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
21477c478bd9Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
21487c478bd9Sstevel@tonic-gate 			(void) strlcpy(state, "invalid", state_sz);
21497c478bd9Sstevel@tonic-gate 			goto out;
21507c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
21517c478bd9Sstevel@tonic-gate 			(void) strlcpy(state, "absent", state_sz);
21527c478bd9Sstevel@tonic-gate 			goto out;
21537c478bd9Sstevel@tonic-gate 
21547c478bd9Sstevel@tonic-gate 		default:
21557c478bd9Sstevel@tonic-gate 			scfdie();
21567c478bd9Sstevel@tonic-gate 		}
21577c478bd9Sstevel@tonic-gate 	}
21587c478bd9Sstevel@tonic-gate 
21597c478bd9Sstevel@tonic-gate 	if (scf_iter_service_instances(iter, svc) != SCF_SUCCESS)
21607c478bd9Sstevel@tonic-gate 		scfdie();
21617c478bd9Sstevel@tonic-gate 
21627c478bd9Sstevel@tonic-gate 	switch (scf_iter_next_instance(iter, inst)) {
21637c478bd9Sstevel@tonic-gate 	case 0:
21647c478bd9Sstevel@tonic-gate 		(void) strlcpy(state, "absent", state_sz);
21657c478bd9Sstevel@tonic-gate 		goto out;
21667c478bd9Sstevel@tonic-gate 
21677c478bd9Sstevel@tonic-gate 	case 1:
21687c478bd9Sstevel@tonic-gate 		break;
21697c478bd9Sstevel@tonic-gate 
21707c478bd9Sstevel@tonic-gate 	default:
21717c478bd9Sstevel@tonic-gate 		scfdie();
21727c478bd9Sstevel@tonic-gate 	}
21737c478bd9Sstevel@tonic-gate 
21747c478bd9Sstevel@tonic-gate 	/* Get the state in case this is the only instance. */
21757c478bd9Sstevel@tonic-gate 	get_restarter_string_prop(inst, scf_property_state, state, state_sz);
21767c478bd9Sstevel@tonic-gate 
21777c478bd9Sstevel@tonic-gate 	switch (scf_iter_next_instance(iter, inst)) {
21787c478bd9Sstevel@tonic-gate 	case 0:
21797c478bd9Sstevel@tonic-gate 		break;
21807c478bd9Sstevel@tonic-gate 
21817c478bd9Sstevel@tonic-gate 	case 1:
21827c478bd9Sstevel@tonic-gate 		/* Nope, multiple instances. */
21837c478bd9Sstevel@tonic-gate 		(void) strlcpy(state, "multiple", state_sz);
21847c478bd9Sstevel@tonic-gate 		goto out;
21857c478bd9Sstevel@tonic-gate 
21867c478bd9Sstevel@tonic-gate 	default:
21877c478bd9Sstevel@tonic-gate 		scfdie();
21887c478bd9Sstevel@tonic-gate 	}
21897c478bd9Sstevel@tonic-gate 
21907c478bd9Sstevel@tonic-gate out:
21917c478bd9Sstevel@tonic-gate 	scf_iter_destroy(iter);
21927c478bd9Sstevel@tonic-gate 	scf_instance_destroy(inst);
21937c478bd9Sstevel@tonic-gate 	scf_service_destroy(svc);
21947c478bd9Sstevel@tonic-gate }
21957c478bd9Sstevel@tonic-gate 
21961f6eb021SLiane Praza static void
21971f6eb021SLiane Praza print_application_properties(scf_walkinfo_t *wip, scf_snapshot_t *snap)
21981f6eb021SLiane Praza {
21991f6eb021SLiane Praza 	scf_iter_t *pg_iter, *prop_iter, *val_iter;
22001f6eb021SLiane Praza 	scf_propertygroup_t *pg;
22011f6eb021SLiane Praza 	scf_property_t *prop;
22021f6eb021SLiane Praza 	scf_value_t *val;
22031f6eb021SLiane Praza 	scf_pg_tmpl_t *pt;
22041f6eb021SLiane Praza 	scf_prop_tmpl_t *prt;
22051f6eb021SLiane Praza 	char *pg_name_buf = safe_malloc(max_scf_name_length + 1);
22061f6eb021SLiane Praza 	char *prop_name_buf = safe_malloc(max_scf_name_length + 1);
22071f6eb021SLiane Praza 	char *snap_name = safe_malloc(max_scf_name_length + 1);
22081f6eb021SLiane Praza 	char *val_buf = safe_malloc(max_scf_value_length + 1);
22091f6eb021SLiane Praza 	char *desc, *cp;
22101f6eb021SLiane Praza 	scf_type_t type;
22111f6eb021SLiane Praza 	int i, j, k;
22121f6eb021SLiane Praza 	uint8_t vis;
22131f6eb021SLiane Praza 
22141f6eb021SLiane Praza 	if ((pg_iter = scf_iter_create(h)) == NULL ||
22151f6eb021SLiane Praza 	    (prop_iter = scf_iter_create(h)) == NULL ||
22161f6eb021SLiane Praza 	    (val_iter = scf_iter_create(h)) == NULL ||
22171f6eb021SLiane Praza 	    (val = scf_value_create(h)) == NULL ||
22181f6eb021SLiane Praza 	    (prop = scf_property_create(h)) == NULL ||
22191f6eb021SLiane Praza 	    (pt = scf_tmpl_pg_create(h)) == NULL ||
22201f6eb021SLiane Praza 	    (prt = scf_tmpl_prop_create(h)) == NULL ||
22211f6eb021SLiane Praza 	    (pg = scf_pg_create(h)) == NULL)
22221f6eb021SLiane Praza 		scfdie();
22231f6eb021SLiane Praza 
22241f6eb021SLiane Praza 	if (scf_iter_instance_pgs_typed_composed(pg_iter, wip->inst, snap,
22251f6eb021SLiane Praza 	    SCF_PG_APP_DEFAULT) == -1)
22261f6eb021SLiane Praza 		scfdie();
22271f6eb021SLiane Praza 
22281f6eb021SLiane Praza 	/*
22291f6eb021SLiane Praza 	 * Format for output:
22301f6eb021SLiane Praza 	 *	pg (pgtype)
22311f6eb021SLiane Praza 	 *	 description
22321f6eb021SLiane Praza 	 *	pg/prop (proptype) = <value> <value>
22331f6eb021SLiane Praza 	 *	 description
22341f6eb021SLiane Praza 	 */
22351f6eb021SLiane Praza 	while ((i = scf_iter_next_pg(pg_iter, pg)) == 1) {
22361f6eb021SLiane Praza 		int tmpl = 0;
22371f6eb021SLiane Praza 
22381f6eb021SLiane Praza 		if (scf_pg_get_name(pg, pg_name_buf, max_scf_name_length) < 0)
22391f6eb021SLiane Praza 			scfdie();
22401f6eb021SLiane Praza 		if (scf_snapshot_get_name(snap, snap_name,
22411f6eb021SLiane Praza 		    max_scf_name_length) < 0)
22421f6eb021SLiane Praza 			scfdie();
22431f6eb021SLiane Praza 
22441f6eb021SLiane Praza 		if (scf_tmpl_get_by_pg_name(wip->fmri, snap_name, pg_name_buf,
22451f6eb021SLiane Praza 		    SCF_PG_APP_DEFAULT, pt, 0) == 0)
22461f6eb021SLiane Praza 			tmpl = 1;
22471f6eb021SLiane Praza 		else
22481f6eb021SLiane Praza 			tmpl = 0;
22491f6eb021SLiane Praza 
22501f6eb021SLiane Praza 		(void) printf("%s (%s)\n", pg_name_buf, SCF_PG_APP_DEFAULT);
22511f6eb021SLiane Praza 
22521f6eb021SLiane Praza 		if (tmpl == 1 && scf_tmpl_pg_description(pt, NULL, &desc) > 0) {
22531f6eb021SLiane Praza 			(void) printf("  %s\n", desc);
22541f6eb021SLiane Praza 			free(desc);
22551f6eb021SLiane Praza 		}
22561f6eb021SLiane Praza 
22571f6eb021SLiane Praza 		if (scf_iter_pg_properties(prop_iter, pg) == -1)
22581f6eb021SLiane Praza 			scfdie();
22591f6eb021SLiane Praza 		while ((j = scf_iter_next_property(prop_iter, prop)) == 1) {
22601f6eb021SLiane Praza 			if (scf_property_get_name(prop, prop_name_buf,
22611f6eb021SLiane Praza 			    max_scf_name_length) < 0)
22621f6eb021SLiane Praza 				scfdie();
22631f6eb021SLiane Praza 			if (scf_property_type(prop, &type) == -1)
22641f6eb021SLiane Praza 				scfdie();
22651f6eb021SLiane Praza 
22661f6eb021SLiane Praza 			if ((tmpl == 1) &&
22671f6eb021SLiane Praza 			    (scf_tmpl_get_by_prop(pt, prop_name_buf, prt,
22681f6eb021SLiane Praza 			    0) != 0))
22691f6eb021SLiane Praza 				tmpl = 0;
22701f6eb021SLiane Praza 
22711f6eb021SLiane Praza 			if (tmpl == 1 &&
22721f6eb021SLiane Praza 			    scf_tmpl_prop_visibility(prt, &vis) != -1 &&
22731f6eb021SLiane Praza 			    vis == SCF_TMPL_VISIBILITY_HIDDEN)
22741f6eb021SLiane Praza 				continue;
22751f6eb021SLiane Praza 
22761f6eb021SLiane Praza 			(void) printf("%s/%s (%s) = ", pg_name_buf,
22771f6eb021SLiane Praza 			    prop_name_buf, scf_type_to_string(type));
22781f6eb021SLiane Praza 
22791f6eb021SLiane Praza 			if (scf_iter_property_values(val_iter, prop) == -1)
22801f6eb021SLiane Praza 				scfdie();
22811f6eb021SLiane Praza 
22821f6eb021SLiane Praza 			while ((k = scf_iter_next_value(val_iter, val)) == 1) {
22831f6eb021SLiane Praza 				if (scf_value_get_as_string(val, val_buf,
22841f6eb021SLiane Praza 				    max_scf_value_length + 1) < 0)
22851f6eb021SLiane Praza 					scfdie();
22861f6eb021SLiane Praza 				if (strpbrk(val_buf, " \t\n\"()") != NULL) {
22871f6eb021SLiane Praza 					(void) printf("\"");
22881f6eb021SLiane Praza 					for (cp = val_buf; *cp != '\0'; ++cp) {
22891f6eb021SLiane Praza 						if (*cp == '"' || *cp == '\\')
22901f6eb021SLiane Praza 							(void) putc('\\',
22911f6eb021SLiane Praza 							    stdout);
22921f6eb021SLiane Praza 
22931f6eb021SLiane Praza 						(void) putc(*cp, stdout);
22941f6eb021SLiane Praza 					}
22951f6eb021SLiane Praza 					(void) printf("\"");
22961f6eb021SLiane Praza 				} else {
22971f6eb021SLiane Praza 					(void) printf("%s ", val_buf);
22981f6eb021SLiane Praza 				}
22991f6eb021SLiane Praza 			}
23001f6eb021SLiane Praza 
23011f6eb021SLiane Praza 			(void) printf("\n");
23021f6eb021SLiane Praza 
23031f6eb021SLiane Praza 			if (k == -1)
23041f6eb021SLiane Praza 				scfdie();
23051f6eb021SLiane Praza 
23061f6eb021SLiane Praza 			if (tmpl == 1 && scf_tmpl_prop_description(prt, NULL,
23071f6eb021SLiane Praza 			    &desc) > 0) {
23081f6eb021SLiane Praza 				(void) printf("  %s\n", desc);
23091f6eb021SLiane Praza 				free(desc);
23101f6eb021SLiane Praza 			}
23111f6eb021SLiane Praza 		}
23121f6eb021SLiane Praza 		if (j == -1)
23131f6eb021SLiane Praza 			scfdie();
23141f6eb021SLiane Praza 	}
23151f6eb021SLiane Praza 	if (i == -1)
23161f6eb021SLiane Praza 		scfdie();
23171f6eb021SLiane Praza 
23181f6eb021SLiane Praza 
23191f6eb021SLiane Praza 	scf_iter_destroy(pg_iter);
23201f6eb021SLiane Praza 	scf_iter_destroy(prop_iter);
23211f6eb021SLiane Praza 	scf_iter_destroy(val_iter);
23221f6eb021SLiane Praza 	scf_value_destroy(val);
23231f6eb021SLiane Praza 	scf_property_destroy(prop);
23241f6eb021SLiane Praza 	scf_tmpl_pg_destroy(pt);
23251f6eb021SLiane Praza 	scf_tmpl_prop_destroy(prt);
23261f6eb021SLiane Praza 	scf_pg_destroy(pg);
23271f6eb021SLiane Praza 	free(pg_name_buf);
23281f6eb021SLiane Praza 	free(prop_name_buf);
23291f6eb021SLiane Praza 	free(snap_name);
23301f6eb021SLiane Praza 	free(val_buf);
23311f6eb021SLiane Praza }
23321f6eb021SLiane Praza 
23337c478bd9Sstevel@tonic-gate static void
23347c478bd9Sstevel@tonic-gate print_detailed_dependency(scf_propertygroup_t *pg)
23357c478bd9Sstevel@tonic-gate {
23367c478bd9Sstevel@tonic-gate 	scf_property_t *eprop;
23377c478bd9Sstevel@tonic-gate 	scf_iter_t *iter;
23387c478bd9Sstevel@tonic-gate 	scf_type_t ty;
23397c478bd9Sstevel@tonic-gate 	char *val_buf;
23407c478bd9Sstevel@tonic-gate 	int i;
23417c478bd9Sstevel@tonic-gate 
23427c478bd9Sstevel@tonic-gate 	if ((eprop = scf_property_create(h)) == NULL ||
23437c478bd9Sstevel@tonic-gate 	    (iter = scf_iter_create(h)) == NULL)
23447c478bd9Sstevel@tonic-gate 		scfdie();
23457c478bd9Sstevel@tonic-gate 
23467c478bd9Sstevel@tonic-gate 	val_buf = safe_malloc(max_scf_value_length + 1);
23477c478bd9Sstevel@tonic-gate 
23487c478bd9Sstevel@tonic-gate 	if (scf_pg_get_property(pg, SCF_PROPERTY_ENTITIES, eprop) !=
23497c478bd9Sstevel@tonic-gate 	    SCF_SUCCESS ||
23507c478bd9Sstevel@tonic-gate 	    scf_property_type(eprop, &ty) != SCF_SUCCESS ||
23517c478bd9Sstevel@tonic-gate 	    ty != SCF_TYPE_FMRI)
23527c478bd9Sstevel@tonic-gate 		return;
23537c478bd9Sstevel@tonic-gate 
23547c478bd9Sstevel@tonic-gate 	(void) printf("%-*s", DETAILED_WIDTH, gettext("dependency"));
23557c478bd9Sstevel@tonic-gate 
23567c478bd9Sstevel@tonic-gate 	/* Print the grouping */
23577c478bd9Sstevel@tonic-gate 	if (pg_get_single_val(pg, SCF_PROPERTY_GROUPING, SCF_TYPE_ASTRING,
23587c478bd9Sstevel@tonic-gate 	    val_buf, max_scf_value_length + 1, 0) == 0)
23597c478bd9Sstevel@tonic-gate 		(void) fputs(val_buf, stdout);
23607c478bd9Sstevel@tonic-gate 	else
23617c478bd9Sstevel@tonic-gate 		(void) putchar('?');
23627c478bd9Sstevel@tonic-gate 
23637c478bd9Sstevel@tonic-gate 	(void) putchar('/');
23647c478bd9Sstevel@tonic-gate 
23657c478bd9Sstevel@tonic-gate 	if (pg_get_single_val(pg, SCF_PROPERTY_RESTART_ON, SCF_TYPE_ASTRING,
23667c478bd9Sstevel@tonic-gate 	    val_buf, max_scf_value_length + 1, 0) == 0)
23677c478bd9Sstevel@tonic-gate 		(void) fputs(val_buf, stdout);
23687c478bd9Sstevel@tonic-gate 	else
23697c478bd9Sstevel@tonic-gate 		(void) putchar('?');
23707c478bd9Sstevel@tonic-gate 
23717c478bd9Sstevel@tonic-gate 	/* Print the dependency entities. */
23727c478bd9Sstevel@tonic-gate 	if (scf_iter_property_values(iter, eprop) == -1)
23737c478bd9Sstevel@tonic-gate 		scfdie();
23747c478bd9Sstevel@tonic-gate 
23757c478bd9Sstevel@tonic-gate 	while ((i = scf_iter_next_value(iter, g_val)) == 1) {
23767c478bd9Sstevel@tonic-gate 		char state[MAX_SCF_STATE_STRING_SZ];
23777c478bd9Sstevel@tonic-gate 
23787c478bd9Sstevel@tonic-gate 		if (scf_value_get_astring(g_val, val_buf,
23797c478bd9Sstevel@tonic-gate 		    max_scf_value_length + 1) < 0)
23807c478bd9Sstevel@tonic-gate 			scfdie();
23817c478bd9Sstevel@tonic-gate 
23827c478bd9Sstevel@tonic-gate 		(void) putchar(' ');
23837c478bd9Sstevel@tonic-gate 		(void) fputs(val_buf, stdout);
23847c478bd9Sstevel@tonic-gate 
23857c478bd9Sstevel@tonic-gate 		/* Print the state. */
23867c478bd9Sstevel@tonic-gate 		state[0] = '-';
23877c478bd9Sstevel@tonic-gate 		state[1] = '\0';
23887c478bd9Sstevel@tonic-gate 
23897c478bd9Sstevel@tonic-gate 		get_fmri_state(val_buf, state, sizeof (state));
23907c478bd9Sstevel@tonic-gate 
23917c478bd9Sstevel@tonic-gate 		(void) printf(" (%s)", state);
23927c478bd9Sstevel@tonic-gate 	}
23937c478bd9Sstevel@tonic-gate 	if (i == -1)
23947c478bd9Sstevel@tonic-gate 		scfdie();
23957c478bd9Sstevel@tonic-gate 
23967c478bd9Sstevel@tonic-gate 	(void) putchar('\n');
23977c478bd9Sstevel@tonic-gate 
23987c478bd9Sstevel@tonic-gate 	free(val_buf);
23997c478bd9Sstevel@tonic-gate 	scf_iter_destroy(iter);
24007c478bd9Sstevel@tonic-gate 	scf_property_destroy(eprop);
24017c478bd9Sstevel@tonic-gate }
24027c478bd9Sstevel@tonic-gate 
24037c478bd9Sstevel@tonic-gate /* ARGSUSED */
24047c478bd9Sstevel@tonic-gate static int
24057c478bd9Sstevel@tonic-gate print_detailed(void *unused, scf_walkinfo_t *wip)
24067c478bd9Sstevel@tonic-gate {
24077c478bd9Sstevel@tonic-gate 	scf_snapshot_t *snap;
24087c478bd9Sstevel@tonic-gate 	scf_propertygroup_t *rpg;
24097c478bd9Sstevel@tonic-gate 	scf_iter_t *pg_iter;
24107c478bd9Sstevel@tonic-gate 
24117c478bd9Sstevel@tonic-gate 	char *buf;
24127c478bd9Sstevel@tonic-gate 	char *timebuf;
24137c478bd9Sstevel@tonic-gate 	size_t tbsz;
24147c478bd9Sstevel@tonic-gate 	int ret;
24157c478bd9Sstevel@tonic-gate 	uint64_t c;
24167c478bd9Sstevel@tonic-gate 	int temp, perm;
24177c478bd9Sstevel@tonic-gate 	struct timeval tv;
24187c478bd9Sstevel@tonic-gate 	time_t stime;
24197c478bd9Sstevel@tonic-gate 	struct tm *tmp;
242094501b61Sskamm 	int restarter_spec;
242194501b61Sskamm 	int restarter_ret;
24227c478bd9Sstevel@tonic-gate 
24237c478bd9Sstevel@tonic-gate 	const char * const fmt = "%-*s%s\n";
24247c478bd9Sstevel@tonic-gate 
24257c478bd9Sstevel@tonic-gate 	assert(wip->pg == NULL);
24267c478bd9Sstevel@tonic-gate 
24277c478bd9Sstevel@tonic-gate 	rpg = scf_pg_create(h);
24287c478bd9Sstevel@tonic-gate 	if (rpg == NULL)
24297c478bd9Sstevel@tonic-gate 		scfdie();
24307c478bd9Sstevel@tonic-gate 
24317c478bd9Sstevel@tonic-gate 	if (first_paragraph)
24327c478bd9Sstevel@tonic-gate 		first_paragraph = 0;
24337c478bd9Sstevel@tonic-gate 	else
24347c478bd9Sstevel@tonic-gate 		(void) putchar('\n');
24357c478bd9Sstevel@tonic-gate 
24367c478bd9Sstevel@tonic-gate 	buf = safe_malloc(max_scf_fmri_length + 1);
24377c478bd9Sstevel@tonic-gate 
24387c478bd9Sstevel@tonic-gate 	if (scf_instance_to_fmri(wip->inst, buf, max_scf_fmri_length + 1) != -1)
24397c478bd9Sstevel@tonic-gate 		(void) printf(fmt, DETAILED_WIDTH, "fmri", buf);
24407c478bd9Sstevel@tonic-gate 
24417c478bd9Sstevel@tonic-gate 	if (common_name_buf == NULL)
24427c478bd9Sstevel@tonic-gate 		common_name_buf = safe_malloc(max_scf_value_length + 1);
24437c478bd9Sstevel@tonic-gate 
24447c478bd9Sstevel@tonic-gate 	if (inst_get_single_val(wip->inst, SCF_PG_TM_COMMON_NAME, locale,
24457c478bd9Sstevel@tonic-gate 	    SCF_TYPE_USTRING, common_name_buf, max_scf_value_length, 0, 1, 1)
24467c478bd9Sstevel@tonic-gate 	    == 0)
24477c478bd9Sstevel@tonic-gate 		(void) printf(fmt, DETAILED_WIDTH, gettext("name"),
24487c478bd9Sstevel@tonic-gate 		    common_name_buf);
24497c478bd9Sstevel@tonic-gate 	else if (inst_get_single_val(wip->inst, SCF_PG_TM_COMMON_NAME, "C",
24507c478bd9Sstevel@tonic-gate 	    SCF_TYPE_USTRING, common_name_buf, max_scf_value_length, 0, 1, 1)
24517c478bd9Sstevel@tonic-gate 	    == 0)
24527c478bd9Sstevel@tonic-gate 		(void) printf(fmt, DETAILED_WIDTH, gettext("name"),
24537c478bd9Sstevel@tonic-gate 		    common_name_buf);
24547c478bd9Sstevel@tonic-gate 
2455048b0279SBryan Cantrill 	if (g_zonename != NULL)
2456048b0279SBryan Cantrill 		(void) printf(fmt, DETAILED_WIDTH, gettext("zone"), g_zonename);
2457048b0279SBryan Cantrill 
24587c478bd9Sstevel@tonic-gate 	/*
24597c478bd9Sstevel@tonic-gate 	 * Synthesize an 'enabled' property that hides the enabled_ovr
24607c478bd9Sstevel@tonic-gate 	 * implementation from the user.  If the service has been temporarily
24617c478bd9Sstevel@tonic-gate 	 * set to a state other than its permanent value, alert the user with
24627c478bd9Sstevel@tonic-gate 	 * a '(temporary)' message.
24637c478bd9Sstevel@tonic-gate 	 */
24647c478bd9Sstevel@tonic-gate 	perm = instance_enabled(wip->inst, B_FALSE);
24657c478bd9Sstevel@tonic-gate 	temp = instance_enabled(wip->inst, B_TRUE);
24667c478bd9Sstevel@tonic-gate 	if (temp != -1) {
24677c478bd9Sstevel@tonic-gate 		if (temp != perm)
24687c478bd9Sstevel@tonic-gate 			(void) printf(gettext("%-*s%s (temporary)\n"),
24697c478bd9Sstevel@tonic-gate 			    DETAILED_WIDTH, gettext("enabled"),
24707c478bd9Sstevel@tonic-gate 			    temp ? gettext("true") : gettext("false"));
24717c478bd9Sstevel@tonic-gate 		else
24727c478bd9Sstevel@tonic-gate 			(void) printf(fmt, DETAILED_WIDTH,
24737c478bd9Sstevel@tonic-gate 			    gettext("enabled"), temp ? gettext("true") :
24747c478bd9Sstevel@tonic-gate 			    gettext("false"));
24757c478bd9Sstevel@tonic-gate 	} else if (perm != -1) {
24767c478bd9Sstevel@tonic-gate 		(void) printf(fmt, DETAILED_WIDTH, gettext("enabled"),
24777c478bd9Sstevel@tonic-gate 		    perm ? gettext("true") : gettext("false"));
24787c478bd9Sstevel@tonic-gate 	}
24797c478bd9Sstevel@tonic-gate 
24807c478bd9Sstevel@tonic-gate 	/*
24817c478bd9Sstevel@tonic-gate 	 * Property values may be longer than max_scf_fmri_length, but these
24827c478bd9Sstevel@tonic-gate 	 * shouldn't be, so we'll just reuse buf.  The user can use svcprop if
248348bbca81SDaniel Hoffman 	 * they suspect something fishy.
24847c478bd9Sstevel@tonic-gate 	 */
24857c478bd9Sstevel@tonic-gate 	if (scf_instance_get_pg(wip->inst, SCF_PG_RESTARTER, rpg) != 0) {
24867c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_NOT_FOUND)
24877c478bd9Sstevel@tonic-gate 			scfdie();
24887c478bd9Sstevel@tonic-gate 
24897c478bd9Sstevel@tonic-gate 		scf_pg_destroy(rpg);
24907c478bd9Sstevel@tonic-gate 		rpg = NULL;
24917c478bd9Sstevel@tonic-gate 	}
24927c478bd9Sstevel@tonic-gate 
24937c478bd9Sstevel@tonic-gate 	if (rpg) {
24947c478bd9Sstevel@tonic-gate 		if (pg_get_single_val(rpg, scf_property_state, SCF_TYPE_ASTRING,
24957c478bd9Sstevel@tonic-gate 		    buf, max_scf_fmri_length + 1, 0) == 0)
24967c478bd9Sstevel@tonic-gate 			(void) printf(fmt, DETAILED_WIDTH, gettext("state"),
24977c478bd9Sstevel@tonic-gate 			    buf);
24987c478bd9Sstevel@tonic-gate 
24997c478bd9Sstevel@tonic-gate 		if (pg_get_single_val(rpg, scf_property_next_state,
25007c478bd9Sstevel@tonic-gate 		    SCF_TYPE_ASTRING, buf, max_scf_fmri_length + 1, 0) == 0)
25017c478bd9Sstevel@tonic-gate 			(void) printf(fmt, DETAILED_WIDTH,
25027c478bd9Sstevel@tonic-gate 			    gettext("next_state"), buf);
25037c478bd9Sstevel@tonic-gate 
25047c478bd9Sstevel@tonic-gate 		if (pg_get_single_val(rpg, SCF_PROPERTY_STATE_TIMESTAMP,
25055f8fc372SToomas Soome 		    SCF_TYPE_TIME, &tv, 0, 0) == 0) {
25067c478bd9Sstevel@tonic-gate 			stime = tv.tv_sec;
25077c478bd9Sstevel@tonic-gate 			tmp = localtime(&stime);
25087c478bd9Sstevel@tonic-gate 			for (tbsz = 50; ; tbsz *= 2) {
25097c478bd9Sstevel@tonic-gate 				timebuf = safe_malloc(tbsz);
25107c478bd9Sstevel@tonic-gate 				if (strftime(timebuf, tbsz, NULL, tmp) != 0)
25117c478bd9Sstevel@tonic-gate 					break;
25127c478bd9Sstevel@tonic-gate 				free(timebuf);
25137c478bd9Sstevel@tonic-gate 			}
25147c478bd9Sstevel@tonic-gate 			(void) printf(fmt, DETAILED_WIDTH,
25157c478bd9Sstevel@tonic-gate 			    gettext("state_time"),
25167c478bd9Sstevel@tonic-gate 			    timebuf);
25177c478bd9Sstevel@tonic-gate 			free(timebuf);
25187c478bd9Sstevel@tonic-gate 		}
25197c478bd9Sstevel@tonic-gate 
2520b7185059Srm 		if (pg_get_single_val(rpg, SCF_PROPERTY_ALT_LOGFILE,
2521b7185059Srm 		    SCF_TYPE_ASTRING, buf, max_scf_fmri_length + 1, 0) == 0)
2522b7185059Srm 			(void) printf(fmt, DETAILED_WIDTH,
2523b7185059Srm 			    gettext("alt_logfile"), buf);
25247c478bd9Sstevel@tonic-gate 
2525b7185059Srm 		if (pg_get_single_val(rpg, SCF_PROPERTY_LOGFILE,
2526b7185059Srm 		    SCF_TYPE_ASTRING, buf, max_scf_fmri_length + 1, 0) == 0)
2527b7185059Srm 			(void) printf(fmt, DETAILED_WIDTH, gettext("logfile"),
2528b7185059Srm 			    buf);
2529b7185059Srm 	}
25307c478bd9Sstevel@tonic-gate 
25317c478bd9Sstevel@tonic-gate 	if (inst_get_single_val(wip->inst, SCF_PG_GENERAL,
25327c478bd9Sstevel@tonic-gate 	    SCF_PROPERTY_RESTARTER, SCF_TYPE_ASTRING, buf,
25337c478bd9Sstevel@tonic-gate 	    max_scf_fmri_length + 1, 0, 0, 1) == 0)
25347c478bd9Sstevel@tonic-gate 		(void) printf(fmt, DETAILED_WIDTH, gettext("restarter"), buf);
25357c478bd9Sstevel@tonic-gate 	else
25367c478bd9Sstevel@tonic-gate 		(void) printf(fmt, DETAILED_WIDTH, gettext("restarter"),
25377c478bd9Sstevel@tonic-gate 		    SCF_SERVICE_STARTD);
25387c478bd9Sstevel@tonic-gate 
25397c478bd9Sstevel@tonic-gate 	free(buf);
25407c478bd9Sstevel@tonic-gate 
254194501b61Sskamm 	/*
254294501b61Sskamm 	 * Use the restarter specific routine to print the ctids, if available.
254394501b61Sskamm 	 * If restarter specific action is available and it fails, then die.
254494501b61Sskamm 	 */
254594501b61Sskamm 	restarter_ret = ctids_by_restarter(wip, &c, 1, 0,
254694501b61Sskamm 	    &restarter_spec, print_ctid_header, print_ctid_detailed);
254794501b61Sskamm 	if (restarter_spec == 1) {
254894501b61Sskamm 		if (restarter_ret != 0)
254994501b61Sskamm 			uu_die(gettext("Unable to get restarter for %s"),
255094501b61Sskamm 			    wip->fmri);
255194501b61Sskamm 		goto restarter_common;
255294501b61Sskamm 	}
255394501b61Sskamm 
25547c478bd9Sstevel@tonic-gate 	if (rpg) {
25557c478bd9Sstevel@tonic-gate 		scf_iter_t *iter;
25567c478bd9Sstevel@tonic-gate 
25577c478bd9Sstevel@tonic-gate 		if ((iter = scf_iter_create(h)) == NULL)
25587c478bd9Sstevel@tonic-gate 			scfdie();
25597c478bd9Sstevel@tonic-gate 
25607c478bd9Sstevel@tonic-gate 		if (scf_pg_get_property(rpg, scf_property_contract, g_prop) ==
25617c478bd9Sstevel@tonic-gate 		    0) {
25627c478bd9Sstevel@tonic-gate 			if (scf_property_is_type(g_prop, SCF_TYPE_COUNT) == 0) {
256394501b61Sskamm 
256494501b61Sskamm 				/* Callback to print ctid header */
256594501b61Sskamm 				print_ctid_header();
25667c478bd9Sstevel@tonic-gate 
25677c478bd9Sstevel@tonic-gate 				if (scf_iter_property_values(iter, g_prop) != 0)
25687c478bd9Sstevel@tonic-gate 					scfdie();
25697c478bd9Sstevel@tonic-gate 
25707c478bd9Sstevel@tonic-gate 				for (;;) {
25717c478bd9Sstevel@tonic-gate 					ret = scf_iter_next_value(iter, g_val);
25727c478bd9Sstevel@tonic-gate 					if (ret == -1)
25737c478bd9Sstevel@tonic-gate 						scfdie();
25747c478bd9Sstevel@tonic-gate 					if (ret == 0)
25757c478bd9Sstevel@tonic-gate 						break;
25767c478bd9Sstevel@tonic-gate 
25777c478bd9Sstevel@tonic-gate 					if (scf_value_get_count(g_val, &c) != 0)
25787c478bd9Sstevel@tonic-gate 						scfdie();
257994501b61Sskamm 
258094501b61Sskamm 					/* Callback to print contract id. */
258194501b61Sskamm 					print_ctid_detailed(c);
25827c478bd9Sstevel@tonic-gate 				}
25837c478bd9Sstevel@tonic-gate 
25847c478bd9Sstevel@tonic-gate 				(void) putchar('\n');
25857c478bd9Sstevel@tonic-gate 			} else {
25867c478bd9Sstevel@tonic-gate 				if (scf_error() != SCF_ERROR_TYPE_MISMATCH)
25877c478bd9Sstevel@tonic-gate 					scfdie();
25887c478bd9Sstevel@tonic-gate 			}
25897c478bd9Sstevel@tonic-gate 		} else {
25907c478bd9Sstevel@tonic-gate 			if (scf_error() != SCF_ERROR_NOT_FOUND)
25917c478bd9Sstevel@tonic-gate 				scfdie();
25927c478bd9Sstevel@tonic-gate 		}
25937c478bd9Sstevel@tonic-gate 
25947c478bd9Sstevel@tonic-gate 		scf_iter_destroy(iter);
25957c478bd9Sstevel@tonic-gate 	} else {
25967c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_NOT_FOUND)
25977c478bd9Sstevel@tonic-gate 			scfdie();
25987c478bd9Sstevel@tonic-gate 	}
25997c478bd9Sstevel@tonic-gate 
260094501b61Sskamm restarter_common:
26017c478bd9Sstevel@tonic-gate 	scf_pg_destroy(rpg);
26027c478bd9Sstevel@tonic-gate 
26037c478bd9Sstevel@tonic-gate 	/* Dependencies. */
26047c478bd9Sstevel@tonic-gate 	if ((pg_iter = scf_iter_create(h)) == NULL)
26057c478bd9Sstevel@tonic-gate 		scfdie();
26067c478bd9Sstevel@tonic-gate 
26077c478bd9Sstevel@tonic-gate 	snap = get_running_snapshot(wip->inst);
26087c478bd9Sstevel@tonic-gate 
26097c478bd9Sstevel@tonic-gate 	if (scf_iter_instance_pgs_typed_composed(pg_iter, wip->inst, snap,
26107c478bd9Sstevel@tonic-gate 	    SCF_GROUP_DEPENDENCY) != SCF_SUCCESS)
26117c478bd9Sstevel@tonic-gate 		scfdie();
26127c478bd9Sstevel@tonic-gate 
26137c478bd9Sstevel@tonic-gate 	while ((ret = scf_iter_next_pg(pg_iter, g_pg)) == 1)
26147c478bd9Sstevel@tonic-gate 		print_detailed_dependency(g_pg);
26157c478bd9Sstevel@tonic-gate 	if (ret == -1)
26167c478bd9Sstevel@tonic-gate 		scfdie();
26177c478bd9Sstevel@tonic-gate 
26187c478bd9Sstevel@tonic-gate 	scf_iter_destroy(pg_iter);
26197c478bd9Sstevel@tonic-gate 
26207c478bd9Sstevel@tonic-gate 	if (opt_processes)
262194501b61Sskamm 		detailed_list_processes(wip);
26227c478bd9Sstevel@tonic-gate 
26231f6eb021SLiane Praza 	/* "application" type property groups */
26241f6eb021SLiane Praza 	if (opt_verbose == 1)
26251f6eb021SLiane Praza 		print_application_properties(wip, snap);
26261f6eb021SLiane Praza 
26271f6eb021SLiane Praza 	scf_snapshot_destroy(snap);
26281f6eb021SLiane Praza 
26297c478bd9Sstevel@tonic-gate 	return (0);
26307c478bd9Sstevel@tonic-gate }
26317c478bd9Sstevel@tonic-gate 
2632d5c6878fSBryan Cantrill /* ARGSUSED */
2633d5c6878fSBryan Cantrill static int
2634d5c6878fSBryan Cantrill print_log(void *unused, scf_walkinfo_t *wip)
2635d5c6878fSBryan Cantrill {
2636d5c6878fSBryan Cantrill 	scf_propertygroup_t *rpg;
2637d5c6878fSBryan Cantrill 	char buf[MAXPATHLEN];
2638d5c6878fSBryan Cantrill 
2639d5c6878fSBryan Cantrill 	if ((rpg = scf_pg_create(h)) == NULL)
2640d5c6878fSBryan Cantrill 		scfdie();
2641d5c6878fSBryan Cantrill 
2642d5c6878fSBryan Cantrill 	if (scf_instance_get_pg(wip->inst, SCF_PG_RESTARTER, rpg) != 0) {
2643d5c6878fSBryan Cantrill 		if (scf_error() != SCF_ERROR_NOT_FOUND)
2644d5c6878fSBryan Cantrill 			scfdie();
2645d5c6878fSBryan Cantrill 
2646d5c6878fSBryan Cantrill 		goto out;
2647d5c6878fSBryan Cantrill 	}
2648d5c6878fSBryan Cantrill 
2649d5c6878fSBryan Cantrill 	if (pg_get_single_val(rpg, SCF_PROPERTY_LOGFILE,
2650d5c6878fSBryan Cantrill 	    SCF_TYPE_ASTRING, buf, sizeof (buf), 0) == 0) {
2651d5c6878fSBryan Cantrill 		(void) printf("%s\n", buf);
2652d5c6878fSBryan Cantrill 	}
2653d5c6878fSBryan Cantrill 
2654d5c6878fSBryan Cantrill out:
2655d5c6878fSBryan Cantrill 	scf_pg_destroy(rpg);
2656d5c6878fSBryan Cantrill 
2657d5c6878fSBryan Cantrill 	return (0);
2658d5c6878fSBryan Cantrill }
2659d5c6878fSBryan Cantrill 
2660f6e214c7SGavin Maltby int
2661f6e214c7SGavin Maltby qsort_str_compare(const void *p1, const void *p2)
2662f6e214c7SGavin Maltby {
2663f6e214c7SGavin Maltby 	return (strcmp((const char *)p1, (const char *)p2));
2664f6e214c7SGavin Maltby }
2665f6e214c7SGavin Maltby 
2666f6e214c7SGavin Maltby /*
2667f6e214c7SGavin Maltby  * get_notify_param_classes()
2668f6e214c7SGavin Maltby  * return the fma classes that don't have a tag in fma_tags[], otherwise NULL
2669f6e214c7SGavin Maltby  */
2670f6e214c7SGavin Maltby static char **
2671f6e214c7SGavin Maltby get_notify_param_classes()
2672f6e214c7SGavin Maltby {
2673f6e214c7SGavin Maltby 	scf_handle_t		*h = _scf_handle_create_and_bind(SCF_VERSION);
2674f6e214c7SGavin Maltby 	scf_instance_t		*inst = scf_instance_create(h);
2675f6e214c7SGavin Maltby 	scf_snapshot_t		*snap = scf_snapshot_create(h);
2676f6e214c7SGavin Maltby 	scf_snaplevel_t		*slvl = scf_snaplevel_create(h);
2677f6e214c7SGavin Maltby 	scf_propertygroup_t	*pg = scf_pg_create(h);
2678f6e214c7SGavin Maltby 	scf_iter_t		*iter = scf_iter_create(h);
2679f6e214c7SGavin Maltby 	int size = 4;
2680f6e214c7SGavin Maltby 	int n = 0;
2681f6e214c7SGavin Maltby 	size_t sz = scf_limit(SCF_LIMIT_MAX_NAME_LENGTH) + 1;
2682f6e214c7SGavin Maltby 	int err;
2683f6e214c7SGavin Maltby 	char *pgname = safe_malloc(sz);
2684f6e214c7SGavin Maltby 	char **buf = safe_malloc(size * sizeof (char *));
2685f6e214c7SGavin Maltby 
2686f6e214c7SGavin Maltby 	if (h == NULL || inst == NULL || snap == NULL || slvl == NULL ||
2687f6e214c7SGavin Maltby 	    pg == NULL || iter == NULL) {
2688f6e214c7SGavin Maltby 		uu_die(gettext("Failed object creation: %s\n"),
2689f6e214c7SGavin Maltby 		    scf_strerror(scf_error()));
2690f6e214c7SGavin Maltby 	}
2691f6e214c7SGavin Maltby 
2692f6e214c7SGavin Maltby 	if (scf_handle_decode_fmri(h, SCF_NOTIFY_PARAMS_INST, NULL, NULL, inst,
2693f6e214c7SGavin Maltby 	    NULL, NULL, SCF_DECODE_FMRI_EXACT) != 0)
2694f6e214c7SGavin Maltby 		uu_die(gettext("Failed to decode %s: %s\n"),
2695f6e214c7SGavin Maltby 		    SCF_NOTIFY_PARAMS_INST, scf_strerror(scf_error()));
2696f6e214c7SGavin Maltby 
2697f6e214c7SGavin Maltby 	if (scf_instance_get_snapshot(inst, "running", snap) != 0)
2698f6e214c7SGavin Maltby 		uu_die(gettext("Failed to get snapshot: %s\n"),
2699f6e214c7SGavin Maltby 		    scf_strerror(scf_error()));
2700f6e214c7SGavin Maltby 
2701f6e214c7SGavin Maltby 	if (scf_snapshot_get_base_snaplevel(snap, slvl) != 0)
2702f6e214c7SGavin Maltby 		uu_die(gettext("Failed to get base snaplevel: %s\n"),
2703f6e214c7SGavin Maltby 		    scf_strerror(scf_error()));
2704f6e214c7SGavin Maltby 
2705f6e214c7SGavin Maltby 	if (scf_iter_snaplevel_pgs_typed(iter, slvl,
2706f6e214c7SGavin Maltby 	    SCF_NOTIFY_PARAMS_PG_TYPE) != 0)
2707f6e214c7SGavin Maltby 		uu_die(gettext("Failed to get iterator: %s\n"),
2708f6e214c7SGavin Maltby 		    scf_strerror(scf_error()));
2709f6e214c7SGavin Maltby 
2710f6e214c7SGavin Maltby 	while ((err = scf_iter_next_pg(iter, pg)) == 1) {
2711f6e214c7SGavin Maltby 		char *c;
2712f6e214c7SGavin Maltby 
2713f6e214c7SGavin Maltby 		if (scf_pg_get_name(pg, pgname, sz) == -1)
2714f6e214c7SGavin Maltby 			uu_die(gettext("Failed to get pg name: %s\n"),
2715f6e214c7SGavin Maltby 			    scf_strerror(scf_error()));
2716f6e214c7SGavin Maltby 		if ((c = strrchr(pgname, ',')) != NULL)
2717f6e214c7SGavin Maltby 			*c = '\0';
2718f6e214c7SGavin Maltby 		if (has_fma_tag(pgname))
2719f6e214c7SGavin Maltby 			continue;
2720f6e214c7SGavin Maltby 		if (!is_fma_token(pgname))
2721f6e214c7SGavin Maltby 			/*
2722f6e214c7SGavin Maltby 			 * We don't emmit a warning here so that we don't
2723f6e214c7SGavin Maltby 			 * pollute the output
2724f6e214c7SGavin Maltby 			 */
2725f6e214c7SGavin Maltby 			continue;
2726f6e214c7SGavin Maltby 
2727f6e214c7SGavin Maltby 		if (n + 1 >= size) {
2728f6e214c7SGavin Maltby 			size *= 2;
2729f6e214c7SGavin Maltby 			buf = realloc(buf, size * sizeof (char *));
2730f6e214c7SGavin Maltby 			if (buf == NULL)
2731f6e214c7SGavin Maltby 				uu_die(gettext("Out of memory.\n"));
2732f6e214c7SGavin Maltby 		}
2733f6e214c7SGavin Maltby 		buf[n] = safe_strdup(pgname);
2734f6e214c7SGavin Maltby 		++n;
2735f6e214c7SGavin Maltby 	}
2736f6e214c7SGavin Maltby 	/*
2737f6e214c7SGavin Maltby 	 * NULL terminate buf
2738f6e214c7SGavin Maltby 	 */
2739f6e214c7SGavin Maltby 	buf[n] = NULL;
2740f6e214c7SGavin Maltby 	if (err == -1)
2741f6e214c7SGavin Maltby 		uu_die(gettext("Failed to iterate pgs: %s\n"),
2742f6e214c7SGavin Maltby 		    scf_strerror(scf_error()));
2743f6e214c7SGavin Maltby 
2744f6e214c7SGavin Maltby 	/* sort the classes */
2745f6e214c7SGavin Maltby 	qsort((void *)buf, n, sizeof (char *), qsort_str_compare);
2746f6e214c7SGavin Maltby 
2747f6e214c7SGavin Maltby 	free(pgname);
2748f6e214c7SGavin Maltby 	scf_iter_destroy(iter);
2749f6e214c7SGavin Maltby 	scf_pg_destroy(pg);
2750f6e214c7SGavin Maltby 	scf_snaplevel_destroy(slvl);
2751f6e214c7SGavin Maltby 	scf_snapshot_destroy(snap);
2752f6e214c7SGavin Maltby 	scf_instance_destroy(inst);
2753f6e214c7SGavin Maltby 	scf_handle_destroy(h);
2754f6e214c7SGavin Maltby 
2755f6e214c7SGavin Maltby 	return (buf);
2756f6e214c7SGavin Maltby }
2757f6e214c7SGavin Maltby 
2758f6e214c7SGavin Maltby /*
2759f6e214c7SGavin Maltby  * get_fma_notify_params()
2760f6e214c7SGavin Maltby  * populates an nvlist_t with notifycation parameters for a given FMA class
2761f6e214c7SGavin Maltby  * returns 0 if the nvlist is populated, 1 otherwise;
2762f6e214c7SGavin Maltby  */
2763f6e214c7SGavin Maltby int
2764f6e214c7SGavin Maltby get_fma_notify_params(nvlist_t *nvl, const char *class)
2765f6e214c7SGavin Maltby {
2766f6e214c7SGavin Maltby 	if (_scf_get_fma_notify_params(class, nvl, 0) != 0) {
2767f6e214c7SGavin Maltby 		/*
2768f6e214c7SGavin Maltby 		 * if the preferences have just been deleted
2769f6e214c7SGavin Maltby 		 * or does not exist, just skip.
2770f6e214c7SGavin Maltby 		 */
2771f6e214c7SGavin Maltby 		if (scf_error() != SCF_ERROR_NOT_FOUND &&
2772f6e214c7SGavin Maltby 		    scf_error() != SCF_ERROR_DELETED)
2773f6e214c7SGavin Maltby 			uu_warn(gettext(
2774f6e214c7SGavin Maltby 			    "Failed get_fma_notify_params %s\n"),
2775f6e214c7SGavin Maltby 			    scf_strerror(scf_error()));
2776f6e214c7SGavin Maltby 
2777f6e214c7SGavin Maltby 		return (1);
2778f6e214c7SGavin Maltby 	}
2779f6e214c7SGavin Maltby 
2780f6e214c7SGavin Maltby 	return (0);
2781f6e214c7SGavin Maltby }
2782f6e214c7SGavin Maltby 
2783f6e214c7SGavin Maltby /*
2784f6e214c7SGavin Maltby  * print_notify_fma()
2785f6e214c7SGavin Maltby  * outputs the notification paramets of FMA events.
2786f6e214c7SGavin Maltby  * It first outputs classes in fma_tags[], then outputs the other classes
2787f6e214c7SGavin Maltby  * sorted alphabetically
2788f6e214c7SGavin Maltby  */
2789f6e214c7SGavin Maltby static void
2790f6e214c7SGavin Maltby print_notify_fma(void)
2791f6e214c7SGavin Maltby {
2792f6e214c7SGavin Maltby 	nvlist_t *nvl;
2793f6e214c7SGavin Maltby 	char **tmp = NULL;
2794f6e214c7SGavin Maltby 	char **classes, *p;
2795f6e214c7SGavin Maltby 	const char *class;
2796f6e214c7SGavin Maltby 	uint32_t i;
2797f6e214c7SGavin Maltby 
2798f6e214c7SGavin Maltby 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
2799f6e214c7SGavin Maltby 		uu_die(gettext("Out of memory.\n"));
2800f6e214c7SGavin Maltby 
2801f6e214c7SGavin Maltby 	for (i = 0; (class = get_fma_class(i)) != NULL; ++i) {
2802f6e214c7SGavin Maltby 		if (get_fma_notify_params(nvl, class) == 0)
2803f6e214c7SGavin Maltby 			listnotify_print(nvl, get_fma_tag(i));
2804f6e214c7SGavin Maltby 	}
2805f6e214c7SGavin Maltby 
2806f6e214c7SGavin Maltby 	if ((classes = get_notify_param_classes()) == NULL)
2807f6e214c7SGavin Maltby 		goto cleanup;
2808f6e214c7SGavin Maltby 
2809f6e214c7SGavin Maltby 	tmp = classes;
2810f6e214c7SGavin Maltby 	for (p = *tmp; p; ++tmp, p = *tmp) {
2811f6e214c7SGavin Maltby 		if (get_fma_notify_params(nvl, p) == 0)
2812f6e214c7SGavin Maltby 			listnotify_print(nvl, re_tag(p));
2813f6e214c7SGavin Maltby 
2814f6e214c7SGavin Maltby 		free(p);
2815f6e214c7SGavin Maltby 	}
2816f6e214c7SGavin Maltby 
2817f6e214c7SGavin Maltby 	free(classes);
2818f6e214c7SGavin Maltby 
2819f6e214c7SGavin Maltby cleanup:
2820f6e214c7SGavin Maltby 	nvlist_free(nvl);
2821f6e214c7SGavin Maltby }
2822f6e214c7SGavin Maltby 
2823f6e214c7SGavin Maltby /*
2824f6e214c7SGavin Maltby  * print_notify_fmri()
2825f6e214c7SGavin Maltby  * prints notifycation parameters for an SMF instance.
2826f6e214c7SGavin Maltby  */
2827f6e214c7SGavin Maltby static void
2828f6e214c7SGavin Maltby print_notify_fmri(const char *fmri)
2829f6e214c7SGavin Maltby {
2830f6e214c7SGavin Maltby 	nvlist_t *nvl;
2831f6e214c7SGavin Maltby 
2832f6e214c7SGavin Maltby 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
2833f6e214c7SGavin Maltby 		uu_die(gettext("Out of memory.\n"));
2834f6e214c7SGavin Maltby 
2835f6e214c7SGavin Maltby 	if (_scf_get_svc_notify_params(fmri, nvl, SCF_TRANSITION_ALL, 0, 0) !=
2836f6e214c7SGavin Maltby 	    SCF_SUCCESS) {
2837f6e214c7SGavin Maltby 		if (scf_error() != SCF_ERROR_NOT_FOUND &&
2838f6e214c7SGavin Maltby 		    scf_error() != SCF_ERROR_DELETED)
2839f6e214c7SGavin Maltby 			uu_warn(gettext(
2840f6e214c7SGavin Maltby 			    "Failed _scf_get_svc_notify_params: %s\n"),
2841f6e214c7SGavin Maltby 			    scf_strerror(scf_error()));
2842f6e214c7SGavin Maltby 	} else {
2843f6e214c7SGavin Maltby 		if (strcmp(SCF_INSTANCE_GLOBAL, fmri) == 0)
2844f6e214c7SGavin Maltby 			safe_printf(
2845f6e214c7SGavin Maltby 			    gettext("System wide notification parameters:\n"));
2846f6e214c7SGavin Maltby 		safe_printf("%s:\n", fmri);
2847f6e214c7SGavin Maltby 		listnotify_print(nvl, NULL);
2848f6e214c7SGavin Maltby 	}
2849f6e214c7SGavin Maltby 	nvlist_free(nvl);
2850f6e214c7SGavin Maltby }
2851f6e214c7SGavin Maltby 
2852f6e214c7SGavin Maltby /*
2853f6e214c7SGavin Maltby  * print_notify_special()
2854f6e214c7SGavin Maltby  * prints notification parameters for FMA events and system wide SMF state
2855f6e214c7SGavin Maltby  * transitions parameters
2856f6e214c7SGavin Maltby  */
2857f6e214c7SGavin Maltby static void
2858f6e214c7SGavin Maltby print_notify_special()
2859f6e214c7SGavin Maltby {
2860f6e214c7SGavin Maltby 	safe_printf("Notification parameters for FMA Events\n");
2861f6e214c7SGavin Maltby 	print_notify_fma();
2862f6e214c7SGavin Maltby 	print_notify_fmri(SCF_INSTANCE_GLOBAL);
2863f6e214c7SGavin Maltby }
2864f6e214c7SGavin Maltby 
2865f6e214c7SGavin Maltby /*
2866f6e214c7SGavin Maltby  * print_notify()
2867f6e214c7SGavin Maltby  * callback function to print notification parameters for SMF state transition
2868f6e214c7SGavin Maltby  * instances. It skips global and notify-params instances as they should be
2869f6e214c7SGavin Maltby  * printed by print_notify_special()
2870f6e214c7SGavin Maltby  */
2871f6e214c7SGavin Maltby /* ARGSUSED */
2872f6e214c7SGavin Maltby static int
2873f6e214c7SGavin Maltby print_notify(void *unused, scf_walkinfo_t *wip)
2874f6e214c7SGavin Maltby {
2875f6e214c7SGavin Maltby 	if (strcmp(SCF_INSTANCE_GLOBAL, wip->fmri) == 0 ||
2876f6e214c7SGavin Maltby 	    strcmp(SCF_NOTIFY_PARAMS_INST, wip->fmri) == 0)
2877f6e214c7SGavin Maltby 		return (0);
2878f6e214c7SGavin Maltby 
2879f6e214c7SGavin Maltby 	print_notify_fmri(wip->fmri);
2880f6e214c7SGavin Maltby 
2881f6e214c7SGavin Maltby 	return (0);
2882f6e214c7SGavin Maltby }
2883f6e214c7SGavin Maltby 
28847c478bd9Sstevel@tonic-gate /*
28857c478bd9Sstevel@tonic-gate  * Append a one-lined description of each process in inst's contract(s) and
28867c478bd9Sstevel@tonic-gate  * return the augmented string.
28877c478bd9Sstevel@tonic-gate  */
28887c478bd9Sstevel@tonic-gate static char *
288994501b61Sskamm add_processes(scf_walkinfo_t *wip, char *line, scf_propertygroup_t *lpg)
28907c478bd9Sstevel@tonic-gate {
28917c478bd9Sstevel@tonic-gate 	pid_t *pids = NULL;
28927c478bd9Sstevel@tonic-gate 	uint_t i, n = 0;
28937c478bd9Sstevel@tonic-gate 
28947c478bd9Sstevel@tonic-gate 	if (lpg == NULL) {
289594501b61Sskamm 		if (instance_processes(wip->inst, wip->fmri, &pids, &n) != 0)
28967c478bd9Sstevel@tonic-gate 			return (line);
28977c478bd9Sstevel@tonic-gate 	} else {
289894501b61Sskamm 		/* Legacy services */
28997c478bd9Sstevel@tonic-gate 		scf_iter_t *iter;
29007c478bd9Sstevel@tonic-gate 
29017c478bd9Sstevel@tonic-gate 		if ((iter = scf_iter_create(h)) == NULL)
29027c478bd9Sstevel@tonic-gate 			scfdie();
29037c478bd9Sstevel@tonic-gate 
29047c478bd9Sstevel@tonic-gate 		(void) propvals_to_pids(lpg, scf_property_contract, &pids, &n,
29057c478bd9Sstevel@tonic-gate 		    g_prop, g_val, iter);
29067c478bd9Sstevel@tonic-gate 
29077c478bd9Sstevel@tonic-gate 		scf_iter_destroy(iter);
29087c478bd9Sstevel@tonic-gate 	}
29097c478bd9Sstevel@tonic-gate 
29107c478bd9Sstevel@tonic-gate 	if (n == 0)
29117c478bd9Sstevel@tonic-gate 		return (line);
29127c478bd9Sstevel@tonic-gate 
29137c478bd9Sstevel@tonic-gate 	qsort(pids, n, sizeof (*pids), pidcmp);
29147c478bd9Sstevel@tonic-gate 
29157c478bd9Sstevel@tonic-gate 	for (i = 0; i < n; ++i) {
29167c478bd9Sstevel@tonic-gate 		char *cp, stime[9];
29177c478bd9Sstevel@tonic-gate 		psinfo_t psi;
2918*81ff72c5SJason King 		const char *name = NULL;
29197c478bd9Sstevel@tonic-gate 		int len = 1 + 15 + 8 + 3 + 6 + 1 + PRFNSZ;
29207c478bd9Sstevel@tonic-gate 
2921*81ff72c5SJason King 		if (proc_get_psinfo(pids[i], &psi) != 0)
29227c478bd9Sstevel@tonic-gate 			continue;
29237c478bd9Sstevel@tonic-gate 
29247c478bd9Sstevel@tonic-gate 		line = realloc(line, strlen(line) + len);
29257c478bd9Sstevel@tonic-gate 		if (line == NULL)
29267c478bd9Sstevel@tonic-gate 			uu_die(gettext("Out of memory.\n"));
29277c478bd9Sstevel@tonic-gate 
29287c478bd9Sstevel@tonic-gate 		cp = strchr(line, '\0');
29297c478bd9Sstevel@tonic-gate 
2930*81ff72c5SJason King 		if (!IS_ZOMBIE(&psi)) {
2931*81ff72c5SJason King #define	DAY (24 * 60 * 60)
2932*81ff72c5SJason King #define	YEAR (12 * 30 * 24 * 60 * 60)
29337c478bd9Sstevel@tonic-gate 
2934*81ff72c5SJason King 			struct tm *tm;
2935*81ff72c5SJason King 
2936*81ff72c5SJason King 			tm = localtime(&psi.pr_start.tv_sec);
2937*81ff72c5SJason King 
2938*81ff72c5SJason King 			/*
2939*81ff72c5SJason King 			 * Print time if started within the past 24 hours,
2940*81ff72c5SJason King 			 * print date if within the past 12 months, print year
2941*81ff72c5SJason King 			 * if started greater than 12 months ago.
2942*81ff72c5SJason King 			 */
2943*81ff72c5SJason King 			if (now - psi.pr_start.tv_sec < DAY) {
2944*81ff72c5SJason King 				(void) strftime(stime, sizeof (stime),
2945*81ff72c5SJason King 				    gettext(FORMAT_TIME), tm);
2946*81ff72c5SJason King 			} else if (now - psi.pr_start.tv_sec < YEAR) {
2947*81ff72c5SJason King 				(void) strftime(stime, sizeof (stime),
2948*81ff72c5SJason King 				    gettext(FORMAT_DATE), tm);
2949*81ff72c5SJason King 			} else {
2950*81ff72c5SJason King 				(void) strftime(stime, sizeof (stime),
2951*81ff72c5SJason King 				    gettext(FORMAT_YEAR), tm);
2952*81ff72c5SJason King 			}
2953*81ff72c5SJason King 
2954*81ff72c5SJason King 			name = psi.pr_fname;
2955*81ff72c5SJason King #undef DAY
2956*81ff72c5SJason King #undef YEAR
2957*81ff72c5SJason King 		} else {
2958*81ff72c5SJason King 			(void) snprintf(stime, sizeof (stime), "-");
2959*81ff72c5SJason King 			name = "<defunct>";
2960*81ff72c5SJason King 		}
29617c478bd9Sstevel@tonic-gate 
29627c478bd9Sstevel@tonic-gate 		(void) snprintf(cp, len, "\n               %-8s   %6ld %.*s",
2963*81ff72c5SJason King 		    stime, pids[i], PRFNSZ, name);
29647c478bd9Sstevel@tonic-gate 	}
29657c478bd9Sstevel@tonic-gate 
29667c478bd9Sstevel@tonic-gate 	free(pids);
29677c478bd9Sstevel@tonic-gate 
29687c478bd9Sstevel@tonic-gate 	return (line);
29697c478bd9Sstevel@tonic-gate }
29707c478bd9Sstevel@tonic-gate 
29717c478bd9Sstevel@tonic-gate /*ARGSUSED*/
29727c478bd9Sstevel@tonic-gate static int
29737c478bd9Sstevel@tonic-gate list_instance(void *unused, scf_walkinfo_t *wip)
29747c478bd9Sstevel@tonic-gate {
29757c478bd9Sstevel@tonic-gate 	struct avl_string *lp;
29767c478bd9Sstevel@tonic-gate 	char *cp;
29777c478bd9Sstevel@tonic-gate 	int i;
29787c478bd9Sstevel@tonic-gate 	uu_avl_index_t idx;
29797c478bd9Sstevel@tonic-gate 
29807c478bd9Sstevel@tonic-gate 	/*
29817c478bd9Sstevel@tonic-gate 	 * If the user has specified a restarter, check for a match first
29827c478bd9Sstevel@tonic-gate 	 */
29837c478bd9Sstevel@tonic-gate 	if (restarters != NULL) {
29847c478bd9Sstevel@tonic-gate 		struct pfmri_list *rest;
29857c478bd9Sstevel@tonic-gate 		int match;
29867c478bd9Sstevel@tonic-gate 		char *restarter_fmri;
29877c478bd9Sstevel@tonic-gate 		const char *scope_name, *svc_name, *inst_name, *pg_name;
29887c478bd9Sstevel@tonic-gate 
29897c478bd9Sstevel@tonic-gate 		/* legacy services don't have restarters */
29907c478bd9Sstevel@tonic-gate 		if (wip->pg != NULL)
29917c478bd9Sstevel@tonic-gate 			return (0);
29927c478bd9Sstevel@tonic-gate 
29937c478bd9Sstevel@tonic-gate 		restarter_fmri = safe_malloc(max_scf_fmri_length + 1);
29947c478bd9Sstevel@tonic-gate 
29957c478bd9Sstevel@tonic-gate 		if (inst_get_single_val(wip->inst, SCF_PG_GENERAL,
29967c478bd9Sstevel@tonic-gate 		    SCF_PROPERTY_RESTARTER, SCF_TYPE_ASTRING, restarter_fmri,
29977c478bd9Sstevel@tonic-gate 		    max_scf_fmri_length + 1, 0, 0, 1) != 0)
29987c478bd9Sstevel@tonic-gate 			(void) strcpy(restarter_fmri, SCF_SERVICE_STARTD);
29997c478bd9Sstevel@tonic-gate 
30007c478bd9Sstevel@tonic-gate 		if (scf_parse_svc_fmri(restarter_fmri, &scope_name, &svc_name,
30017c478bd9Sstevel@tonic-gate 		    &inst_name, &pg_name, NULL) != SCF_SUCCESS) {
30027c478bd9Sstevel@tonic-gate 			free(restarter_fmri);
30037c478bd9Sstevel@tonic-gate 			return (0);
30047c478bd9Sstevel@tonic-gate 		}
30057c478bd9Sstevel@tonic-gate 
30067c478bd9Sstevel@tonic-gate 		match = 0;
30077c478bd9Sstevel@tonic-gate 		for (rest = restarters; rest != NULL; rest = rest->next) {
30087c478bd9Sstevel@tonic-gate 			if (strcmp(rest->scope, scope_name) == 0 &&
30097c478bd9Sstevel@tonic-gate 			    strcmp(rest->service, svc_name) == 0 &&
30107c478bd9Sstevel@tonic-gate 			    strcmp(rest->instance, inst_name) == 0)
30117c478bd9Sstevel@tonic-gate 				match = 1;
30127c478bd9Sstevel@tonic-gate 		}
30137c478bd9Sstevel@tonic-gate 
30147c478bd9Sstevel@tonic-gate 		free(restarter_fmri);
30157c478bd9Sstevel@tonic-gate 
30167c478bd9Sstevel@tonic-gate 		if (!match)
30177c478bd9Sstevel@tonic-gate 			return (0);
30187c478bd9Sstevel@tonic-gate 	}
30197c478bd9Sstevel@tonic-gate 
30207c478bd9Sstevel@tonic-gate 	if (wip->pg == NULL && ht_buckets != NULL && ht_add(wip->fmri)) {
30217c478bd9Sstevel@tonic-gate 		/* It was already there. */
30227c478bd9Sstevel@tonic-gate 		return (0);
30237c478bd9Sstevel@tonic-gate 	}
30247c478bd9Sstevel@tonic-gate 
30257c478bd9Sstevel@tonic-gate 	lp = safe_malloc(sizeof (*lp));
30267c478bd9Sstevel@tonic-gate 
30277c478bd9Sstevel@tonic-gate 	lp->str = NULL;
30287c478bd9Sstevel@tonic-gate 	for (i = 0; i < opt_cnum; ++i) {
30297c478bd9Sstevel@tonic-gate 		columns[opt_columns[i]].sprint(&lp->str, wip);
30307c478bd9Sstevel@tonic-gate 	}
30317c478bd9Sstevel@tonic-gate 	cp = lp->str + strlen(lp->str);
30327c478bd9Sstevel@tonic-gate 	cp--;
30337c478bd9Sstevel@tonic-gate 	while (*cp == ' ')
30347c478bd9Sstevel@tonic-gate 		cp--;
30357c478bd9Sstevel@tonic-gate 	*(cp+1) = '\0';
30367c478bd9Sstevel@tonic-gate 
30377c478bd9Sstevel@tonic-gate 	/* If we're supposed to list the processes, too, do that now. */
30387c478bd9Sstevel@tonic-gate 	if (opt_processes)
303994501b61Sskamm 		lp->str = add_processes(wip, lp->str, wip->pg);
30407c478bd9Sstevel@tonic-gate 
30417c478bd9Sstevel@tonic-gate 	/* Create the sort key. */
30427c478bd9Sstevel@tonic-gate 	cp = lp->key = safe_malloc(sortkey_sz);
30437c478bd9Sstevel@tonic-gate 	for (i = 0; i < opt_snum; ++i) {
30447c478bd9Sstevel@tonic-gate 		int j = opt_sort[i] & 0xff;
30457c478bd9Sstevel@tonic-gate 
30467c478bd9Sstevel@tonic-gate 		assert(columns[j].get_sortkey != NULL);
30477c478bd9Sstevel@tonic-gate 		columns[j].get_sortkey(cp, opt_sort[i] & ~0xff, wip);
30487c478bd9Sstevel@tonic-gate 		cp += columns[j].sortkey_width;
30497c478bd9Sstevel@tonic-gate 	}
30507c478bd9Sstevel@tonic-gate 
30517c478bd9Sstevel@tonic-gate 	/* Insert into AVL tree. */
30527c478bd9Sstevel@tonic-gate 	uu_avl_node_init(lp, &lp->node, lines_pool);
30537c478bd9Sstevel@tonic-gate 	(void) uu_avl_find(lines, lp, NULL, &idx);
30547c478bd9Sstevel@tonic-gate 	uu_avl_insert(lines, lp, idx);
30557c478bd9Sstevel@tonic-gate 
30567c478bd9Sstevel@tonic-gate 	return (0);
30577c478bd9Sstevel@tonic-gate }
30587c478bd9Sstevel@tonic-gate 
30597c478bd9Sstevel@tonic-gate static int
30607c478bd9Sstevel@tonic-gate list_if_enabled(void *unused, scf_walkinfo_t *wip)
30617c478bd9Sstevel@tonic-gate {
30627c478bd9Sstevel@tonic-gate 	if (wip->pg != NULL ||
30637c478bd9Sstevel@tonic-gate 	    instance_enabled(wip->inst, B_FALSE) == 1 ||
30647c478bd9Sstevel@tonic-gate 	    instance_enabled(wip->inst, B_TRUE) == 1)
30657c478bd9Sstevel@tonic-gate 		return (list_instance(unused, wip));
30667c478bd9Sstevel@tonic-gate 
30677c478bd9Sstevel@tonic-gate 	return (0);
30687c478bd9Sstevel@tonic-gate }
30697c478bd9Sstevel@tonic-gate 
30707c478bd9Sstevel@tonic-gate /*
30717c478bd9Sstevel@tonic-gate  * Service FMRI selection: Lookup and call list_instance() for the instances.
30727c478bd9Sstevel@tonic-gate  * Instance FMRI selection: Lookup and call list_instance().
30737c478bd9Sstevel@tonic-gate  *
30747c478bd9Sstevel@tonic-gate  * Note: This is shoehorned into a walk_dependencies() callback prototype so
30757c478bd9Sstevel@tonic-gate  * it can be used in list_dependencies.
30767c478bd9Sstevel@tonic-gate  */
30777c478bd9Sstevel@tonic-gate static int
30787c478bd9Sstevel@tonic-gate list_svc_or_inst_fmri(void *complain, scf_walkinfo_t *wip)
30797c478bd9Sstevel@tonic-gate {
30807c478bd9Sstevel@tonic-gate 	char *fmri;
30817c478bd9Sstevel@tonic-gate 	const char *svc_name, *inst_name, *pg_name, *save;
30827c478bd9Sstevel@tonic-gate 	scf_iter_t *iter;
30837c478bd9Sstevel@tonic-gate 	int ret;
30847c478bd9Sstevel@tonic-gate 
30857c478bd9Sstevel@tonic-gate 	fmri = safe_strdup(wip->fmri);
30867c478bd9Sstevel@tonic-gate 
30877c478bd9Sstevel@tonic-gate 	if (scf_parse_svc_fmri(fmri, NULL, &svc_name, &inst_name, &pg_name,
30887c478bd9Sstevel@tonic-gate 	    NULL) != SCF_SUCCESS) {
30897c478bd9Sstevel@tonic-gate 		if (complain)
30907c478bd9Sstevel@tonic-gate 			uu_warn(gettext("FMRI \"%s\" is invalid.\n"),
30917c478bd9Sstevel@tonic-gate 			    wip->fmri);
30927c478bd9Sstevel@tonic-gate 		exit_status = UU_EXIT_FATAL;
30937c478bd9Sstevel@tonic-gate 		free(fmri);
30947c478bd9Sstevel@tonic-gate 		return (0);
30957c478bd9Sstevel@tonic-gate 	}
30967c478bd9Sstevel@tonic-gate 
30977c478bd9Sstevel@tonic-gate 	/*
30987c478bd9Sstevel@tonic-gate 	 * Yes, this invalidates *_name, but we only care whether they're NULL
30997c478bd9Sstevel@tonic-gate 	 * or not.
31007c478bd9Sstevel@tonic-gate 	 */
31017c478bd9Sstevel@tonic-gate 	free(fmri);
31027c478bd9Sstevel@tonic-gate 
31037c478bd9Sstevel@tonic-gate 	if (svc_name == NULL || pg_name != NULL) {
31047c478bd9Sstevel@tonic-gate 		if (complain)
31057c478bd9Sstevel@tonic-gate 			uu_warn(gettext("FMRI \"%s\" does not designate a "
31067c478bd9Sstevel@tonic-gate 			    "service or instance.\n"), wip->fmri);
31077c478bd9Sstevel@tonic-gate 		return (0);
31087c478bd9Sstevel@tonic-gate 	}
31097c478bd9Sstevel@tonic-gate 
31107c478bd9Sstevel@tonic-gate 	if (inst_name != NULL) {
31117c478bd9Sstevel@tonic-gate 		/* instance */
31127c478bd9Sstevel@tonic-gate 		if (scf_handle_decode_fmri(h, wip->fmri, wip->scope, wip->svc,
31137c478bd9Sstevel@tonic-gate 		    wip->inst, NULL, NULL, 0) != SCF_SUCCESS) {
31147c478bd9Sstevel@tonic-gate 			if (scf_error() != SCF_ERROR_NOT_FOUND)
31157c478bd9Sstevel@tonic-gate 				scfdie();
31167c478bd9Sstevel@tonic-gate 
31177c478bd9Sstevel@tonic-gate 			if (complain)
31187c478bd9Sstevel@tonic-gate 				uu_warn(gettext(
31197c478bd9Sstevel@tonic-gate 				    "Instance \"%s\" does not exist.\n"),
31207c478bd9Sstevel@tonic-gate 				    wip->fmri);
31217c478bd9Sstevel@tonic-gate 			return (0);
31227c478bd9Sstevel@tonic-gate 		}
31237c478bd9Sstevel@tonic-gate 
31247c478bd9Sstevel@tonic-gate 		return (list_instance(NULL, wip));
31257c478bd9Sstevel@tonic-gate 	}
31267c478bd9Sstevel@tonic-gate 
31277c478bd9Sstevel@tonic-gate 	/* service: Walk the instances. */
31287c478bd9Sstevel@tonic-gate 	if (scf_handle_decode_fmri(h, wip->fmri, wip->scope, wip->svc, NULL,
31297c478bd9Sstevel@tonic-gate 	    NULL, NULL, 0) != SCF_SUCCESS) {
31307c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_NOT_FOUND)
31317c478bd9Sstevel@tonic-gate 			scfdie();
31327c478bd9Sstevel@tonic-gate 
31337c478bd9Sstevel@tonic-gate 		if (complain)
31347c478bd9Sstevel@tonic-gate 			uu_warn(gettext("Service \"%s\" does not exist.\n"),
31357c478bd9Sstevel@tonic-gate 			    wip->fmri);
31367c478bd9Sstevel@tonic-gate 
31377c478bd9Sstevel@tonic-gate 		exit_status = UU_EXIT_FATAL;
31387c478bd9Sstevel@tonic-gate 
31397c478bd9Sstevel@tonic-gate 		return (0);
31407c478bd9Sstevel@tonic-gate 	}
31417c478bd9Sstevel@tonic-gate 
31427c478bd9Sstevel@tonic-gate 	iter = scf_iter_create(h);
31437c478bd9Sstevel@tonic-gate 	if (iter == NULL)
31447c478bd9Sstevel@tonic-gate 		scfdie();
31457c478bd9Sstevel@tonic-gate 
31467c478bd9Sstevel@tonic-gate 	if (scf_iter_service_instances(iter, wip->svc) != SCF_SUCCESS)
31477c478bd9Sstevel@tonic-gate 		scfdie();
31487c478bd9Sstevel@tonic-gate 
31497c478bd9Sstevel@tonic-gate 	if ((fmri = malloc(max_scf_fmri_length + 1)) == NULL) {
31507c478bd9Sstevel@tonic-gate 		scf_iter_destroy(iter);
31517c478bd9Sstevel@tonic-gate 		exit_status = UU_EXIT_FATAL;
31527c478bd9Sstevel@tonic-gate 		return (0);
31537c478bd9Sstevel@tonic-gate 	}
31547c478bd9Sstevel@tonic-gate 
31557c478bd9Sstevel@tonic-gate 	save = wip->fmri;
31567c478bd9Sstevel@tonic-gate 	wip->fmri = fmri;
31577c478bd9Sstevel@tonic-gate 	while ((ret = scf_iter_next_instance(iter, wip->inst)) == 1) {
31587c478bd9Sstevel@tonic-gate 		if (scf_instance_to_fmri(wip->inst, fmri,
31597c478bd9Sstevel@tonic-gate 		    max_scf_fmri_length + 1) <= 0)
31607c478bd9Sstevel@tonic-gate 			scfdie();
31617c478bd9Sstevel@tonic-gate 		(void) list_instance(NULL, wip);
31627c478bd9Sstevel@tonic-gate 	}
31637c478bd9Sstevel@tonic-gate 	free(fmri);
31647c478bd9Sstevel@tonic-gate 	wip->fmri = save;
31657c478bd9Sstevel@tonic-gate 	if (ret == -1)
31667c478bd9Sstevel@tonic-gate 		scfdie();
31677c478bd9Sstevel@tonic-gate 
31687c478bd9Sstevel@tonic-gate 	exit_status = UU_EXIT_OK;
31697c478bd9Sstevel@tonic-gate 
31707c478bd9Sstevel@tonic-gate 	scf_iter_destroy(iter);
31717c478bd9Sstevel@tonic-gate 
31727c478bd9Sstevel@tonic-gate 	return (0);
31737c478bd9Sstevel@tonic-gate }
31747c478bd9Sstevel@tonic-gate 
31757c478bd9Sstevel@tonic-gate /*
31767c478bd9Sstevel@tonic-gate  * Dependency selection: Straightforward since each instance lists the
31777c478bd9Sstevel@tonic-gate  * services it depends on.
31787c478bd9Sstevel@tonic-gate  */
31797c478bd9Sstevel@tonic-gate 
31807c478bd9Sstevel@tonic-gate static void
31817c478bd9Sstevel@tonic-gate walk_dependencies(scf_walkinfo_t *wip, scf_walk_callback callback, void *data)
31827c478bd9Sstevel@tonic-gate {
31837c478bd9Sstevel@tonic-gate 	scf_snapshot_t *snap;
31847c478bd9Sstevel@tonic-gate 	scf_iter_t *iter, *viter;
31857c478bd9Sstevel@tonic-gate 	int ret, vret;
31867c478bd9Sstevel@tonic-gate 	char *dep;
31877c478bd9Sstevel@tonic-gate 
31887c478bd9Sstevel@tonic-gate 	assert(wip->inst != NULL);
31897c478bd9Sstevel@tonic-gate 
31907c478bd9Sstevel@tonic-gate 	if ((iter = scf_iter_create(h)) == NULL ||
31917c478bd9Sstevel@tonic-gate 	    (viter = scf_iter_create(h)) == NULL)
31927c478bd9Sstevel@tonic-gate 		scfdie();
31937c478bd9Sstevel@tonic-gate 
31947c478bd9Sstevel@tonic-gate 	snap = get_running_snapshot(wip->inst);
31957c478bd9Sstevel@tonic-gate 
31967c478bd9Sstevel@tonic-gate 	if (scf_iter_instance_pgs_typed_composed(iter, wip->inst, snap,
31977c478bd9Sstevel@tonic-gate 	    SCF_GROUP_DEPENDENCY) != SCF_SUCCESS)
31987c478bd9Sstevel@tonic-gate 		scfdie();
31997c478bd9Sstevel@tonic-gate 
32007c478bd9Sstevel@tonic-gate 	dep = safe_malloc(max_scf_value_length + 1);
32017c478bd9Sstevel@tonic-gate 
32027c478bd9Sstevel@tonic-gate 	while ((ret = scf_iter_next_pg(iter, g_pg)) == 1) {
32037c478bd9Sstevel@tonic-gate 		scf_type_t ty;
32047c478bd9Sstevel@tonic-gate 
32057c478bd9Sstevel@tonic-gate 		/* Ignore exclude_any dependencies. */
32067c478bd9Sstevel@tonic-gate 		if (scf_pg_get_property(g_pg, SCF_PROPERTY_GROUPING, g_prop) !=
32077c478bd9Sstevel@tonic-gate 		    SCF_SUCCESS) {
32087c478bd9Sstevel@tonic-gate 			if (scf_error() != SCF_ERROR_NOT_FOUND)
32097c478bd9Sstevel@tonic-gate 				scfdie();
32107c478bd9Sstevel@tonic-gate 
32117c478bd9Sstevel@tonic-gate 			continue;
32127c478bd9Sstevel@tonic-gate 		}
32137c478bd9Sstevel@tonic-gate 
32147c478bd9Sstevel@tonic-gate 		if (scf_property_type(g_prop, &ty) != SCF_SUCCESS)
32157c478bd9Sstevel@tonic-gate 			scfdie();
32167c478bd9Sstevel@tonic-gate 
32177c478bd9Sstevel@tonic-gate 		if (ty != SCF_TYPE_ASTRING)
32187c478bd9Sstevel@tonic-gate 			continue;
32197c478bd9Sstevel@tonic-gate 
32207c478bd9Sstevel@tonic-gate 		if (scf_property_get_value(g_prop, g_val) != SCF_SUCCESS) {
32217c478bd9Sstevel@tonic-gate 			if (scf_error() != SCF_ERROR_CONSTRAINT_VIOLATED)
32227c478bd9Sstevel@tonic-gate 				scfdie();
32237c478bd9Sstevel@tonic-gate 
32247c478bd9Sstevel@tonic-gate 			continue;
32257c478bd9Sstevel@tonic-gate 		}
32267c478bd9Sstevel@tonic-gate 
32277c478bd9Sstevel@tonic-gate 		if (scf_value_get_astring(g_val, dep,
32287c478bd9Sstevel@tonic-gate 		    max_scf_value_length + 1) < 0)
32297c478bd9Sstevel@tonic-gate 			scfdie();
32307c478bd9Sstevel@tonic-gate 
32317c478bd9Sstevel@tonic-gate 		if (strcmp(dep, SCF_DEP_EXCLUDE_ALL) == 0)
32327c478bd9Sstevel@tonic-gate 			continue;
32337c478bd9Sstevel@tonic-gate 
32347c478bd9Sstevel@tonic-gate 		if (scf_pg_get_property(g_pg, SCF_PROPERTY_ENTITIES, g_prop) !=
32357c478bd9Sstevel@tonic-gate 		    SCF_SUCCESS) {
32367c478bd9Sstevel@tonic-gate 			if (scf_error() != SCF_ERROR_NOT_FOUND)
32377c478bd9Sstevel@tonic-gate 				scfdie();
32387c478bd9Sstevel@tonic-gate 
32397c478bd9Sstevel@tonic-gate 			continue;
32407c478bd9Sstevel@tonic-gate 		}
32417c478bd9Sstevel@tonic-gate 
32427c478bd9Sstevel@tonic-gate 		if (scf_iter_property_values(viter, g_prop) != SCF_SUCCESS)
32437c478bd9Sstevel@tonic-gate 			scfdie();
32447c478bd9Sstevel@tonic-gate 
32457c478bd9Sstevel@tonic-gate 		while ((vret = scf_iter_next_value(viter, g_val)) == 1) {
32467c478bd9Sstevel@tonic-gate 			if (scf_value_get_astring(g_val, dep,
32477c478bd9Sstevel@tonic-gate 			    max_scf_value_length + 1) < 0)
32487c478bd9Sstevel@tonic-gate 				scfdie();
32497c478bd9Sstevel@tonic-gate 
32507c478bd9Sstevel@tonic-gate 			wip->fmri = dep;
32517c478bd9Sstevel@tonic-gate 			if (callback(data, wip) != 0)
32527c478bd9Sstevel@tonic-gate 				goto out;
32537c478bd9Sstevel@tonic-gate 		}
32547c478bd9Sstevel@tonic-gate 		if (vret == -1)
32557c478bd9Sstevel@tonic-gate 			scfdie();
32567c478bd9Sstevel@tonic-gate 	}
32577c478bd9Sstevel@tonic-gate 	if (ret == -1)
32587c478bd9Sstevel@tonic-gate 		scfdie();
32597c478bd9Sstevel@tonic-gate 
32607c478bd9Sstevel@tonic-gate out:
32617c478bd9Sstevel@tonic-gate 	scf_iter_destroy(viter);
32627c478bd9Sstevel@tonic-gate 	scf_iter_destroy(iter);
32637c478bd9Sstevel@tonic-gate 	scf_snapshot_destroy(snap);
32647c478bd9Sstevel@tonic-gate }
32657c478bd9Sstevel@tonic-gate 
32667c478bd9Sstevel@tonic-gate static int
32677c478bd9Sstevel@tonic-gate list_dependencies(void *data, scf_walkinfo_t *wip)
32687c478bd9Sstevel@tonic-gate {
32697c478bd9Sstevel@tonic-gate 	walk_dependencies(wip, list_svc_or_inst_fmri, data);
32707c478bd9Sstevel@tonic-gate 	return (0);
32717c478bd9Sstevel@tonic-gate }
32727c478bd9Sstevel@tonic-gate 
32737c478bd9Sstevel@tonic-gate 
32747c478bd9Sstevel@tonic-gate /*
32757c478bd9Sstevel@tonic-gate  * Dependent selection: The "providing" service's or instance's FMRI is parsed
32767c478bd9Sstevel@tonic-gate  * into the provider_* variables, the instances are walked, and any instance
32777c478bd9Sstevel@tonic-gate  * which lists an FMRI which parses to these components is selected.  This is
32787c478bd9Sstevel@tonic-gate  * inefficient in the face of multiple operands, but that should be uncommon.
32797c478bd9Sstevel@tonic-gate  */
32807c478bd9Sstevel@tonic-gate 
32817c478bd9Sstevel@tonic-gate static char *provider_scope;
32827c478bd9Sstevel@tonic-gate static char *provider_svc;
32837c478bd9Sstevel@tonic-gate static char *provider_inst;	/* NULL for services */
32847c478bd9Sstevel@tonic-gate 
32857c478bd9Sstevel@tonic-gate /*ARGSUSED*/
32867c478bd9Sstevel@tonic-gate static int
32877c478bd9Sstevel@tonic-gate check_against_provider(void *arg, scf_walkinfo_t *wip)
32887c478bd9Sstevel@tonic-gate {
32897c478bd9Sstevel@tonic-gate 	char *cfmri;
32907c478bd9Sstevel@tonic-gate 	const char *scope_name, *svc_name, *inst_name, *pg_name;
32917c478bd9Sstevel@tonic-gate 	int *matchp = arg;
32927c478bd9Sstevel@tonic-gate 
32937c478bd9Sstevel@tonic-gate 	cfmri = safe_strdup(wip->fmri);
32947c478bd9Sstevel@tonic-gate 
32957c478bd9Sstevel@tonic-gate 	if (scf_parse_svc_fmri(cfmri, &scope_name, &svc_name, &inst_name,
32967c478bd9Sstevel@tonic-gate 	    &pg_name, NULL) != SCF_SUCCESS) {
32977c478bd9Sstevel@tonic-gate 		free(cfmri);
32987c478bd9Sstevel@tonic-gate 		return (0);
32997c478bd9Sstevel@tonic-gate 	}
33007c478bd9Sstevel@tonic-gate 
33017c478bd9Sstevel@tonic-gate 	if (svc_name == NULL || pg_name != NULL) {
33027c478bd9Sstevel@tonic-gate 		free(cfmri);
33037c478bd9Sstevel@tonic-gate 		return (0);
33047c478bd9Sstevel@tonic-gate 	}
33057c478bd9Sstevel@tonic-gate 
33067c478bd9Sstevel@tonic-gate 	/*
33077c478bd9Sstevel@tonic-gate 	 * If the user has specified an instance, then also match dependencies
33087c478bd9Sstevel@tonic-gate 	 * on the service itself.
33097c478bd9Sstevel@tonic-gate 	 */
33107c478bd9Sstevel@tonic-gate 	*matchp = (strcmp(provider_scope, scope_name) == 0 &&
33117c478bd9Sstevel@tonic-gate 	    strcmp(provider_svc, svc_name) == 0 &&
33127c478bd9Sstevel@tonic-gate 	    (provider_inst == NULL ? (inst_name == NULL) :
33137c478bd9Sstevel@tonic-gate 	    (inst_name == NULL || strcmp(provider_inst, inst_name) == 0)));
33147c478bd9Sstevel@tonic-gate 
33157c478bd9Sstevel@tonic-gate 	free(cfmri);
33167c478bd9Sstevel@tonic-gate 
33177c478bd9Sstevel@tonic-gate 	/* Stop on matches. */
33187c478bd9Sstevel@tonic-gate 	return (*matchp);
33197c478bd9Sstevel@tonic-gate }
33207c478bd9Sstevel@tonic-gate 
33217c478bd9Sstevel@tonic-gate static int
33227c478bd9Sstevel@tonic-gate list_if_dependent(void *unused, scf_walkinfo_t *wip)
33237c478bd9Sstevel@tonic-gate {
33247c478bd9Sstevel@tonic-gate 	/* Only proceed if this instance depends on provider_*. */
33257c478bd9Sstevel@tonic-gate 	int match = 0;
33267c478bd9Sstevel@tonic-gate 
33277c478bd9Sstevel@tonic-gate 	(void) walk_dependencies(wip, check_against_provider, &match);
33287c478bd9Sstevel@tonic-gate 
33297c478bd9Sstevel@tonic-gate 	if (match)
33307c478bd9Sstevel@tonic-gate 		return (list_instance(unused, wip));
33317c478bd9Sstevel@tonic-gate 
33327c478bd9Sstevel@tonic-gate 	return (0);
33337c478bd9Sstevel@tonic-gate }
33347c478bd9Sstevel@tonic-gate 
33357c478bd9Sstevel@tonic-gate /*ARGSUSED*/
33367c478bd9Sstevel@tonic-gate static int
33377c478bd9Sstevel@tonic-gate list_dependents(void *unused, scf_walkinfo_t *wip)
33387c478bd9Sstevel@tonic-gate {
33397c478bd9Sstevel@tonic-gate 	char *save;
33407c478bd9Sstevel@tonic-gate 	int ret;
33417c478bd9Sstevel@tonic-gate 
33427c478bd9Sstevel@tonic-gate 	if (scf_scope_get_name(wip->scope, provider_scope,
33437c478bd9Sstevel@tonic-gate 	    max_scf_fmri_length) <= 0 ||
33447c478bd9Sstevel@tonic-gate 	    scf_service_get_name(wip->svc, provider_svc,
33457c478bd9Sstevel@tonic-gate 	    max_scf_fmri_length) <= 0)
33467c478bd9Sstevel@tonic-gate 		scfdie();
33477c478bd9Sstevel@tonic-gate 
33487c478bd9Sstevel@tonic-gate 	save = provider_inst;
33497c478bd9Sstevel@tonic-gate 	if (wip->inst == NULL)
33507c478bd9Sstevel@tonic-gate 		provider_inst = NULL;
33517c478bd9Sstevel@tonic-gate 	else if (scf_instance_get_name(wip->inst, provider_inst,
33527c478bd9Sstevel@tonic-gate 	    max_scf_fmri_length) <= 0)
33537c478bd9Sstevel@tonic-gate 		scfdie();
33547c478bd9Sstevel@tonic-gate 
33557c478bd9Sstevel@tonic-gate 	ret = scf_walk_fmri(h, 0, NULL, 0, list_if_dependent, NULL, NULL,
33567c478bd9Sstevel@tonic-gate 	    uu_warn);
33577c478bd9Sstevel@tonic-gate 
33587c478bd9Sstevel@tonic-gate 	provider_inst = save;
33597c478bd9Sstevel@tonic-gate 
33607c478bd9Sstevel@tonic-gate 	return (ret);
33617c478bd9Sstevel@tonic-gate }
33627c478bd9Sstevel@tonic-gate 
33637c478bd9Sstevel@tonic-gate /*
33647c478bd9Sstevel@tonic-gate  * main() & helpers
33657c478bd9Sstevel@tonic-gate  */
33667c478bd9Sstevel@tonic-gate 
33677c478bd9Sstevel@tonic-gate static void
33687c478bd9Sstevel@tonic-gate add_sort_column(const char *col, int reverse)
33697c478bd9Sstevel@tonic-gate {
33707c478bd9Sstevel@tonic-gate 	int i;
33717c478bd9Sstevel@tonic-gate 
33727c478bd9Sstevel@tonic-gate 	++opt_snum;
33737c478bd9Sstevel@tonic-gate 
33747c478bd9Sstevel@tonic-gate 	opt_sort = realloc(opt_sort, opt_snum * sizeof (*opt_sort));
33757c478bd9Sstevel@tonic-gate 	if (opt_sort == NULL)
33767c478bd9Sstevel@tonic-gate 		uu_die(gettext("Too many sort criteria: out of memory.\n"));
33777c478bd9Sstevel@tonic-gate 
33787c478bd9Sstevel@tonic-gate 	for (i = 0; i < ncolumns; ++i) {
33797c478bd9Sstevel@tonic-gate 		if (strcasecmp(col, columns[i].name) == 0)
33807c478bd9Sstevel@tonic-gate 			break;
33817c478bd9Sstevel@tonic-gate 	}
33827c478bd9Sstevel@tonic-gate 
33837c478bd9Sstevel@tonic-gate 	if (i < ncolumns)
33847c478bd9Sstevel@tonic-gate 		opt_sort[opt_snum - 1] = (reverse ? i | 0x100 : i);
33857c478bd9Sstevel@tonic-gate 	else
33867c478bd9Sstevel@tonic-gate 		uu_die(gettext("Unrecognized sort column \"%s\".\n"), col);
33877c478bd9Sstevel@tonic-gate 
33887c478bd9Sstevel@tonic-gate 	sortkey_sz += columns[i].sortkey_width;
33897c478bd9Sstevel@tonic-gate }
33907c478bd9Sstevel@tonic-gate 
33917c478bd9Sstevel@tonic-gate static void
33927c478bd9Sstevel@tonic-gate add_restarter(const char *fmri)
33937c478bd9Sstevel@tonic-gate {
33947c478bd9Sstevel@tonic-gate 	char *cfmri;
33957c478bd9Sstevel@tonic-gate 	const char *pg_name;
33967c478bd9Sstevel@tonic-gate 	struct pfmri_list *rest;
33977c478bd9Sstevel@tonic-gate 
33987c478bd9Sstevel@tonic-gate 	cfmri = safe_strdup(fmri);
33997c478bd9Sstevel@tonic-gate 	rest = safe_malloc(sizeof (*rest));
34007c478bd9Sstevel@tonic-gate 
34017c478bd9Sstevel@tonic-gate 	if (scf_parse_svc_fmri(cfmri, &rest->scope, &rest->service,
34027c478bd9Sstevel@tonic-gate 	    &rest->instance, &pg_name, NULL) != SCF_SUCCESS)
34037c478bd9Sstevel@tonic-gate 		uu_die(gettext("Restarter FMRI \"%s\" is invalid.\n"), fmri);
34047c478bd9Sstevel@tonic-gate 
34057c478bd9Sstevel@tonic-gate 	if (rest->instance == NULL || pg_name != NULL)
34067c478bd9Sstevel@tonic-gate 		uu_die(gettext("Restarter FMRI \"%s\" does not designate an "
34077c478bd9Sstevel@tonic-gate 		    "instance.\n"), fmri);
34087c478bd9Sstevel@tonic-gate 
34097c478bd9Sstevel@tonic-gate 	rest->next = restarters;
34107c478bd9Sstevel@tonic-gate 	restarters = rest;
34117c478bd9Sstevel@tonic-gate 	return;
34127c478bd9Sstevel@tonic-gate 
34137c478bd9Sstevel@tonic-gate err:
34147c478bd9Sstevel@tonic-gate 	free(cfmri);
34157c478bd9Sstevel@tonic-gate 	free(rest);
34167c478bd9Sstevel@tonic-gate }
34177c478bd9Sstevel@tonic-gate 
34187c478bd9Sstevel@tonic-gate /* ARGSUSED */
34197c478bd9Sstevel@tonic-gate static int
34207c478bd9Sstevel@tonic-gate line_cmp(const void *l_arg, const void *r_arg, void *private)
34217c478bd9Sstevel@tonic-gate {
34227c478bd9Sstevel@tonic-gate 	const struct avl_string *l = l_arg;
34237c478bd9Sstevel@tonic-gate 	const struct avl_string *r = r_arg;
34247c478bd9Sstevel@tonic-gate 
34257c478bd9Sstevel@tonic-gate 	return (memcmp(l->key, r->key, sortkey_sz));
34267c478bd9Sstevel@tonic-gate }
34277c478bd9Sstevel@tonic-gate 
34287c478bd9Sstevel@tonic-gate /* ARGSUSED */
34297c478bd9Sstevel@tonic-gate static int
34307c478bd9Sstevel@tonic-gate print_line(void *e, void *private)
34317c478bd9Sstevel@tonic-gate {
34327c478bd9Sstevel@tonic-gate 	struct avl_string *lp = e;
34337c478bd9Sstevel@tonic-gate 
34347c478bd9Sstevel@tonic-gate 	(void) puts(lp->str);
34357c478bd9Sstevel@tonic-gate 
34367c478bd9Sstevel@tonic-gate 	return (UU_WALK_NEXT);
34377c478bd9Sstevel@tonic-gate }
34387c478bd9Sstevel@tonic-gate 
3439048b0279SBryan Cantrill /* ARGSUSED */
3440048b0279SBryan Cantrill static void
3441048b0279SBryan Cantrill errignore(const char *str, ...)
3442048b0279SBryan Cantrill {}
3443048b0279SBryan Cantrill 
34447c478bd9Sstevel@tonic-gate int
34457c478bd9Sstevel@tonic-gate main(int argc, char **argv)
34467c478bd9Sstevel@tonic-gate {
34477c478bd9Sstevel@tonic-gate 	char opt, opt_mode;
34487c478bd9Sstevel@tonic-gate 	int i, n;
34497c478bd9Sstevel@tonic-gate 	char *columns_str = NULL;
34507c478bd9Sstevel@tonic-gate 	char *cp;
34517c478bd9Sstevel@tonic-gate 	const char *progname;
3452048b0279SBryan Cantrill 	int err, missing = 1, ignored, *errarg;
3453048b0279SBryan Cantrill 	uint_t nzents = 0, zent = 0;
3454048b0279SBryan Cantrill 	zoneid_t *zids = NULL;
3455048b0279SBryan Cantrill 	char zonename[ZONENAME_MAX];
3456048b0279SBryan Cantrill 	void (*errfunc)(const char *, ...);
34577c478bd9Sstevel@tonic-gate 
34587c478bd9Sstevel@tonic-gate 	int show_all = 0;
34597c478bd9Sstevel@tonic-gate 	int show_header = 1;
3460048b0279SBryan Cantrill 	int show_zones = 0;
34617c478bd9Sstevel@tonic-gate 
3462d5c6878fSBryan Cantrill 	const char * const options = "aHpvno:R:s:S:dDlL?xZz:";
34637c478bd9Sstevel@tonic-gate 
34647c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
34657c478bd9Sstevel@tonic-gate 
3466dc86508eSVineeth Pillai 	locale = setlocale(LC_MESSAGES, NULL);
34677c478bd9Sstevel@tonic-gate 	if (locale) {
34687c478bd9Sstevel@tonic-gate 		locale = safe_strdup(locale);
34691f6eb021SLiane Praza 		_scf_sanitize_locale(locale);
34707c478bd9Sstevel@tonic-gate 	}
34717c478bd9Sstevel@tonic-gate 
34727c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
34737c478bd9Sstevel@tonic-gate 	progname = uu_setpname(argv[0]);
34747c478bd9Sstevel@tonic-gate 
34757c478bd9Sstevel@tonic-gate 	exit_status = UU_EXIT_OK;
34767c478bd9Sstevel@tonic-gate 
34777c478bd9Sstevel@tonic-gate 	max_scf_name_length = scf_limit(SCF_LIMIT_MAX_NAME_LENGTH);
34787c478bd9Sstevel@tonic-gate 	max_scf_value_length = scf_limit(SCF_LIMIT_MAX_VALUE_LENGTH);
34797c478bd9Sstevel@tonic-gate 	max_scf_fmri_length = scf_limit(SCF_LIMIT_MAX_FMRI_LENGTH);
34801f6eb021SLiane Praza 	max_scf_type_length = scf_limit(SCF_LIMIT_MAX_PG_TYPE_LENGTH);
34817c478bd9Sstevel@tonic-gate 
34827c478bd9Sstevel@tonic-gate 	if (max_scf_name_length == -1 || max_scf_value_length == -1 ||
34831f6eb021SLiane Praza 	    max_scf_fmri_length == -1 || max_scf_type_length == -1)
34847c478bd9Sstevel@tonic-gate 		scfdie();
34857c478bd9Sstevel@tonic-gate 
34867c478bd9Sstevel@tonic-gate 	now = time(NULL);
34877c478bd9Sstevel@tonic-gate 	assert(now != -1);
34887c478bd9Sstevel@tonic-gate 
34897c478bd9Sstevel@tonic-gate 	/*
34907c478bd9Sstevel@tonic-gate 	 * opt_mode is the mode of operation.  0 for plain, 'd' for
34917c478bd9Sstevel@tonic-gate 	 * dependencies, 'D' for dependents, and 'l' for detailed (long).  We
34927c478bd9Sstevel@tonic-gate 	 * need to know now so we know which options are valid.
34937c478bd9Sstevel@tonic-gate 	 */
34947c478bd9Sstevel@tonic-gate 	opt_mode = 0;
34957c478bd9Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, options)) != -1) {
34967c478bd9Sstevel@tonic-gate 		switch (opt) {
34977c478bd9Sstevel@tonic-gate 		case '?':
34987c478bd9Sstevel@tonic-gate 			if (optopt == '?') {
34997c478bd9Sstevel@tonic-gate 				print_help(progname);
35007c478bd9Sstevel@tonic-gate 				return (UU_EXIT_OK);
35017c478bd9Sstevel@tonic-gate 			} else {
35027c478bd9Sstevel@tonic-gate 				argserr(progname);
35037c478bd9Sstevel@tonic-gate 				/* NOTREACHED */
35047c478bd9Sstevel@tonic-gate 			}
35057c478bd9Sstevel@tonic-gate 
35067c478bd9Sstevel@tonic-gate 		case 'd':
35077c478bd9Sstevel@tonic-gate 		case 'D':
35087c478bd9Sstevel@tonic-gate 		case 'l':
3509d5c6878fSBryan Cantrill 		case 'L':
35107c478bd9Sstevel@tonic-gate 			if (opt_mode != 0)
35117c478bd9Sstevel@tonic-gate 				argserr(progname);
35127c478bd9Sstevel@tonic-gate 
35137c478bd9Sstevel@tonic-gate 			opt_mode = opt;
35147c478bd9Sstevel@tonic-gate 			break;
35157c478bd9Sstevel@tonic-gate 
3516f6e214c7SGavin Maltby 		case 'n':
3517f6e214c7SGavin Maltby 			if (opt_mode != 0)
3518f6e214c7SGavin Maltby 				argserr(progname);
3519f6e214c7SGavin Maltby 
3520f6e214c7SGavin Maltby 			opt_mode = opt;
3521f6e214c7SGavin Maltby 			break;
3522f6e214c7SGavin Maltby 
35237c478bd9Sstevel@tonic-gate 		case 'x':
35247c478bd9Sstevel@tonic-gate 			if (opt_mode != 0)
35257c478bd9Sstevel@tonic-gate 				argserr(progname);
35267c478bd9Sstevel@tonic-gate 
35277c478bd9Sstevel@tonic-gate 			opt_mode = opt;
35287c478bd9Sstevel@tonic-gate 			break;
35297c478bd9Sstevel@tonic-gate 
35307c478bd9Sstevel@tonic-gate 		default:
35317c478bd9Sstevel@tonic-gate 			break;
35327c478bd9Sstevel@tonic-gate 		}
35337c478bd9Sstevel@tonic-gate 	}
35347c478bd9Sstevel@tonic-gate 
35357c478bd9Sstevel@tonic-gate 	sortkey_sz = 0;
35367c478bd9Sstevel@tonic-gate 
35377c478bd9Sstevel@tonic-gate 	optind = 1;	/* Reset getopt() */
35387c478bd9Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, options)) != -1) {
35397c478bd9Sstevel@tonic-gate 		switch (opt) {
35407c478bd9Sstevel@tonic-gate 		case 'a':
35417c478bd9Sstevel@tonic-gate 			if (opt_mode != 0)
35427c478bd9Sstevel@tonic-gate 				argserr(progname);
35437c478bd9Sstevel@tonic-gate 			show_all = 1;
35447c478bd9Sstevel@tonic-gate 			break;
35457c478bd9Sstevel@tonic-gate 
35467c478bd9Sstevel@tonic-gate 		case 'H':
35477c478bd9Sstevel@tonic-gate 			if (opt_mode == 'l' || opt_mode == 'x')
35487c478bd9Sstevel@tonic-gate 				argserr(progname);
35497c478bd9Sstevel@tonic-gate 			show_header = 0;
35507c478bd9Sstevel@tonic-gate 			break;
35517c478bd9Sstevel@tonic-gate 
35527c478bd9Sstevel@tonic-gate 		case 'p':
35537c478bd9Sstevel@tonic-gate 			if (opt_mode == 'x')
35547c478bd9Sstevel@tonic-gate 				argserr(progname);
35557c478bd9Sstevel@tonic-gate 			opt_processes = 1;
35567c478bd9Sstevel@tonic-gate 			break;
35577c478bd9Sstevel@tonic-gate 
35587c478bd9Sstevel@tonic-gate 		case 'v':
35597c478bd9Sstevel@tonic-gate 			opt_verbose = 1;
35607c478bd9Sstevel@tonic-gate 			break;
35617c478bd9Sstevel@tonic-gate 
35627c478bd9Sstevel@tonic-gate 		case 'o':
35637c478bd9Sstevel@tonic-gate 			if (opt_mode == 'l' || opt_mode == 'x')
35647c478bd9Sstevel@tonic-gate 				argserr(progname);
35657c478bd9Sstevel@tonic-gate 			columns_str = optarg;
35667c478bd9Sstevel@tonic-gate 			break;
35677c478bd9Sstevel@tonic-gate 
35687c478bd9Sstevel@tonic-gate 		case 'R':
35697c478bd9Sstevel@tonic-gate 			if (opt_mode != 0 || opt_mode == 'x')
35707c478bd9Sstevel@tonic-gate 				argserr(progname);
35717c478bd9Sstevel@tonic-gate 
35727c478bd9Sstevel@tonic-gate 			add_restarter(optarg);
35737c478bd9Sstevel@tonic-gate 			break;
35747c478bd9Sstevel@tonic-gate 
35757c478bd9Sstevel@tonic-gate 		case 's':
35767c478bd9Sstevel@tonic-gate 		case 'S':
35777c478bd9Sstevel@tonic-gate 			if (opt_mode != 0)
35787c478bd9Sstevel@tonic-gate 				argserr(progname);
35797c478bd9Sstevel@tonic-gate 
35807c478bd9Sstevel@tonic-gate 			add_sort_column(optarg, optopt == 'S');
35817c478bd9Sstevel@tonic-gate 			break;
35827c478bd9Sstevel@tonic-gate 
35837c478bd9Sstevel@tonic-gate 		case 'd':
35847c478bd9Sstevel@tonic-gate 		case 'D':
35857c478bd9Sstevel@tonic-gate 		case 'l':
3586d5c6878fSBryan Cantrill 		case 'L':
3587f6e214c7SGavin Maltby 		case 'n':
35887c478bd9Sstevel@tonic-gate 		case 'x':
35897c478bd9Sstevel@tonic-gate 			assert(opt_mode == optopt);
35907c478bd9Sstevel@tonic-gate 			break;
35917c478bd9Sstevel@tonic-gate 
3592048b0279SBryan Cantrill 		case 'z':
3593048b0279SBryan Cantrill 			if (getzoneid() != GLOBAL_ZONEID)
3594048b0279SBryan Cantrill 				uu_die(gettext("svcs -z may only be used from "
3595048b0279SBryan Cantrill 				    "the global zone\n"));
3596048b0279SBryan Cantrill 			if (show_zones)
3597048b0279SBryan Cantrill 				argserr(progname);
3598048b0279SBryan Cantrill 
3599048b0279SBryan Cantrill 			opt_zone = optarg;
3600048b0279SBryan Cantrill 			break;
3601048b0279SBryan Cantrill 
3602048b0279SBryan Cantrill 		case 'Z':
3603048b0279SBryan Cantrill 			if (getzoneid() != GLOBAL_ZONEID)
3604048b0279SBryan Cantrill 				uu_die(gettext("svcs -Z may only be used from "
3605048b0279SBryan Cantrill 				    "the global zone\n"));
3606048b0279SBryan Cantrill 			if (opt_zone != NULL)
3607048b0279SBryan Cantrill 				argserr(progname);
3608048b0279SBryan Cantrill 
3609048b0279SBryan Cantrill 			show_zones = 1;
3610048b0279SBryan Cantrill 			break;
3611048b0279SBryan Cantrill 
36127c478bd9Sstevel@tonic-gate 		case '?':
36137c478bd9Sstevel@tonic-gate 			argserr(progname);
36147c478bd9Sstevel@tonic-gate 
36157c478bd9Sstevel@tonic-gate 		default:
36167c478bd9Sstevel@tonic-gate 			assert(0);
36177c478bd9Sstevel@tonic-gate 			abort();
36187c478bd9Sstevel@tonic-gate 		}
36197c478bd9Sstevel@tonic-gate 	}
36207c478bd9Sstevel@tonic-gate 
36217c478bd9Sstevel@tonic-gate 	/*
36227c478bd9Sstevel@tonic-gate 	 * -a is only meaningful when given no arguments
36237c478bd9Sstevel@tonic-gate 	 */
36247c478bd9Sstevel@tonic-gate 	if (show_all && optind != argc)
36257c478bd9Sstevel@tonic-gate 		uu_warn(gettext("-a ignored when used with arguments.\n"));
36267c478bd9Sstevel@tonic-gate 
3627048b0279SBryan Cantrill 	while (show_zones) {
3628048b0279SBryan Cantrill 		uint_t found;
3629048b0279SBryan Cantrill 
3630048b0279SBryan Cantrill 		if (zone_list(NULL, &nzents) != 0)
3631048b0279SBryan Cantrill 			uu_die(gettext("could not get number of zones"));
3632048b0279SBryan Cantrill 
3633048b0279SBryan Cantrill 		if ((zids = malloc(nzents * sizeof (zoneid_t))) == NULL) {
3634048b0279SBryan Cantrill 			uu_die(gettext("could not allocate array for "
3635048b0279SBryan Cantrill 			    "%d zone IDs"), nzents);
3636048b0279SBryan Cantrill 		}
3637048b0279SBryan Cantrill 
3638048b0279SBryan Cantrill 		found = nzents;
3639048b0279SBryan Cantrill 
3640048b0279SBryan Cantrill 		if (zone_list(zids, &found) != 0)
3641048b0279SBryan Cantrill 			uu_die(gettext("could not get zone list"));
3642048b0279SBryan Cantrill 
3643048b0279SBryan Cantrill 		/*
3644048b0279SBryan Cantrill 		 * If the number of zones has not changed between our calls to
3645048b0279SBryan Cantrill 		 * zone_list(), we're done -- otherwise, we must free our array
3646048b0279SBryan Cantrill 		 * of zone IDs and take another lap.
3647048b0279SBryan Cantrill 		 */
3648048b0279SBryan Cantrill 		if (found == nzents)
3649048b0279SBryan Cantrill 			break;
3650048b0279SBryan Cantrill 
3651048b0279SBryan Cantrill 		free(zids);
3652048b0279SBryan Cantrill 	}
3653048b0279SBryan Cantrill 
3654048b0279SBryan Cantrill 	argc -= optind;
3655048b0279SBryan Cantrill 	argv += optind;
3656048b0279SBryan Cantrill 
3657048b0279SBryan Cantrill again:
36587c478bd9Sstevel@tonic-gate 	h = scf_handle_create(SCF_VERSION);
36597c478bd9Sstevel@tonic-gate 	if (h == NULL)
36607c478bd9Sstevel@tonic-gate 		scfdie();
36617c478bd9Sstevel@tonic-gate 
3662048b0279SBryan Cantrill 	if (opt_zone != NULL || zids != NULL) {
3663048b0279SBryan Cantrill 		scf_value_t *zone;
3664048b0279SBryan Cantrill 
3665048b0279SBryan Cantrill 		assert(opt_zone == NULL || zids == NULL);
3666048b0279SBryan Cantrill 
3667048b0279SBryan Cantrill 		if (opt_zone == NULL) {
3668048b0279SBryan Cantrill 			if (getzonenamebyid(zids[zent++],
3669048b0279SBryan Cantrill 			    zonename, sizeof (zonename)) < 0) {
3670048b0279SBryan Cantrill 				uu_warn(gettext("could not get name for "
3671048b0279SBryan Cantrill 				    "zone %d; ignoring"), zids[zent - 1]);
3672048b0279SBryan Cantrill 				goto nextzone;
3673048b0279SBryan Cantrill 			}
3674048b0279SBryan Cantrill 
3675048b0279SBryan Cantrill 			g_zonename = zonename;
3676048b0279SBryan Cantrill 		} else {
3677048b0279SBryan Cantrill 			g_zonename = opt_zone;
3678048b0279SBryan Cantrill 		}
3679048b0279SBryan Cantrill 
3680048b0279SBryan Cantrill 		if ((zone = scf_value_create(h)) == NULL)
3681048b0279SBryan Cantrill 			scfdie();
3682048b0279SBryan Cantrill 
3683048b0279SBryan Cantrill 		if (scf_value_set_astring(zone, g_zonename) != SCF_SUCCESS)
3684048b0279SBryan Cantrill 			scfdie();
3685048b0279SBryan Cantrill 
3686048b0279SBryan Cantrill 		if (scf_handle_decorate(h, "zone", zone) != SCF_SUCCESS)
3687048b0279SBryan Cantrill 			uu_die(gettext("invalid zone '%s'\n"), g_zonename);
3688048b0279SBryan Cantrill 
3689048b0279SBryan Cantrill 		scf_value_destroy(zone);
3690048b0279SBryan Cantrill 	}
3691048b0279SBryan Cantrill 
3692048b0279SBryan Cantrill 	if (scf_handle_bind(h) == -1) {
3693048b0279SBryan Cantrill 		if (g_zonename != NULL) {
3694048b0279SBryan Cantrill 			uu_warn(gettext("Could not bind to repository "
3695048b0279SBryan Cantrill 			    "server for zone %s: %s\n"), g_zonename,
3696048b0279SBryan Cantrill 			    scf_strerror(scf_error()));
3697048b0279SBryan Cantrill 
3698048b0279SBryan Cantrill 			if (!show_zones)
3699048b0279SBryan Cantrill 				return (UU_EXIT_FATAL);
3700048b0279SBryan Cantrill 
3701048b0279SBryan Cantrill 			goto nextzone;
3702048b0279SBryan Cantrill 		}
3703048b0279SBryan Cantrill 
37047c478bd9Sstevel@tonic-gate 		uu_die(gettext("Could not bind to repository server: %s.  "
37057c478bd9Sstevel@tonic-gate 		    "Exiting.\n"), scf_strerror(scf_error()));
3706048b0279SBryan Cantrill 	}
37077c478bd9Sstevel@tonic-gate 
37087c478bd9Sstevel@tonic-gate 	if ((g_pg = scf_pg_create(h)) == NULL ||
37097c478bd9Sstevel@tonic-gate 	    (g_prop = scf_property_create(h)) == NULL ||
37107c478bd9Sstevel@tonic-gate 	    (g_val = scf_value_create(h)) == NULL)
37117c478bd9Sstevel@tonic-gate 		scfdie();
37127c478bd9Sstevel@tonic-gate 
3713048b0279SBryan Cantrill 	if (show_zones) {
3714048b0279SBryan Cantrill 		/*
3715048b0279SBryan Cantrill 		 * It's hard to avoid editorializing here, but suffice it to
3716048b0279SBryan Cantrill 		 * say that scf_walk_fmri() takes an error handler, the
3717048b0279SBryan Cantrill 		 * interface to which has been regrettably misdesigned:  the
3718048b0279SBryan Cantrill 		 * handler itself takes exclusively a string -- even though
3719048b0279SBryan Cantrill 		 * scf_walk_fmri() has detailed, programmatic knowledge
3720048b0279SBryan Cantrill 		 * of the error condition at the time it calls its errfunc.
3721048b0279SBryan Cantrill 		 * That is, only the error message and not the error semantics
3722048b0279SBryan Cantrill 		 * are given to the handler.  This is poor interface at best,
3723048b0279SBryan Cantrill 		 * but it is particularly problematic when we are talking to
3724048b0279SBryan Cantrill 		 * multiple repository servers (as when we are iterating over
3725048b0279SBryan Cantrill 		 * all zones) as we do not want to treat failure to find a
3726048b0279SBryan Cantrill 		 * match in one zone as overall failure.  Ideally, we would
3727048b0279SBryan Cantrill 		 * simply ignore SCF_MSG_PATTERN_NOINSTANCE and correctly
3728048b0279SBryan Cantrill 		 * process the others, but alas, no such interface exists --
3729048b0279SBryan Cantrill 		 * and we must settle for instead ignoring all errfunc-called
3730048b0279SBryan Cantrill 		 * errors in the case that we are iterating over all zones...
3731048b0279SBryan Cantrill 		 */
3732048b0279SBryan Cantrill 		errfunc = errignore;
3733048b0279SBryan Cantrill 		errarg = missing ? &missing : &ignored;
3734048b0279SBryan Cantrill 		missing = 0;
3735048b0279SBryan Cantrill 	} else {
3736048b0279SBryan Cantrill 		errfunc = uu_warn;
3737048b0279SBryan Cantrill 		errarg = &exit_status;
3738048b0279SBryan Cantrill 	}
37397c478bd9Sstevel@tonic-gate 
37407c478bd9Sstevel@tonic-gate 	/*
37417c478bd9Sstevel@tonic-gate 	 * If we're in long mode, take care of it now before we deal with the
37427c478bd9Sstevel@tonic-gate 	 * sorting and the columns, since we won't use them anyway.
37437c478bd9Sstevel@tonic-gate 	 */
37447c478bd9Sstevel@tonic-gate 	if (opt_mode == 'l') {
37457c478bd9Sstevel@tonic-gate 		if (argc == 0)
37467c478bd9Sstevel@tonic-gate 			argserr(progname);
37477c478bd9Sstevel@tonic-gate 
37487c478bd9Sstevel@tonic-gate 		if ((err = scf_walk_fmri(h, argc, argv, SCF_WALK_MULTIPLE,
3749048b0279SBryan Cantrill 		    print_detailed, NULL, errarg, errfunc)) != 0) {
37507c478bd9Sstevel@tonic-gate 			uu_warn(gettext("failed to iterate over "
37517c478bd9Sstevel@tonic-gate 			    "instances: %s\n"), scf_strerror(err));
37527c478bd9Sstevel@tonic-gate 			exit_status = UU_EXIT_FATAL;
37537c478bd9Sstevel@tonic-gate 		}
37547c478bd9Sstevel@tonic-gate 
3755048b0279SBryan Cantrill 		goto nextzone;
37567c478bd9Sstevel@tonic-gate 	}
37577c478bd9Sstevel@tonic-gate 
3758d5c6878fSBryan Cantrill 	if (opt_mode == 'L') {
3759d5c6878fSBryan Cantrill 		if ((err = scf_walk_fmri(h, argc, argv, SCF_WALK_MULTIPLE,
3760d5c6878fSBryan Cantrill 		    print_log, NULL, &exit_status, uu_warn)) != 0) {
3761d5c6878fSBryan Cantrill 			uu_warn(gettext("failed to iterate over "
3762d5c6878fSBryan Cantrill 			    "instances: %s\n"), scf_strerror(err));
3763d5c6878fSBryan Cantrill 			exit_status = UU_EXIT_FATAL;
3764d5c6878fSBryan Cantrill 		}
3765d5c6878fSBryan Cantrill 
3766d5c6878fSBryan Cantrill 		goto nextzone;
3767d5c6878fSBryan Cantrill 	}
3768d5c6878fSBryan Cantrill 
3769f6e214c7SGavin Maltby 	if (opt_mode == 'n') {
3770f6e214c7SGavin Maltby 		print_notify_special();
3771f6e214c7SGavin Maltby 		if ((err = scf_walk_fmri(h, argc, argv, SCF_WALK_MULTIPLE,
3772048b0279SBryan Cantrill 		    print_notify, NULL, errarg, errfunc)) != 0) {
3773f6e214c7SGavin Maltby 			uu_warn(gettext("failed to iterate over "
3774f6e214c7SGavin Maltby 			    "instances: %s\n"), scf_strerror(err));
3775f6e214c7SGavin Maltby 			exit_status = UU_EXIT_FATAL;
3776f6e214c7SGavin Maltby 		}
3777f6e214c7SGavin Maltby 
3778048b0279SBryan Cantrill 		goto nextzone;
3779f6e214c7SGavin Maltby 	}
3780f6e214c7SGavin Maltby 
37817c478bd9Sstevel@tonic-gate 	if (opt_mode == 'x') {
37827c478bd9Sstevel@tonic-gate 		explain(opt_verbose, argc, argv);
3783048b0279SBryan Cantrill 		goto nextzone;
37847c478bd9Sstevel@tonic-gate 	}
37857c478bd9Sstevel@tonic-gate 
3786048b0279SBryan Cantrill 	if (columns_str == NULL) {
3787048b0279SBryan Cantrill 		if (opt_snum == 0) {
3788048b0279SBryan Cantrill 			if (show_zones)
3789048b0279SBryan Cantrill 				add_sort_column("zone", 0);
3790048b0279SBryan Cantrill 
3791048b0279SBryan Cantrill 			/* Default sort. */
3792048b0279SBryan Cantrill 			add_sort_column("state", 0);
3793048b0279SBryan Cantrill 			add_sort_column("stime", 0);
3794048b0279SBryan Cantrill 			add_sort_column("fmri", 0);
3795048b0279SBryan Cantrill 		}
37967c478bd9Sstevel@tonic-gate 
3797048b0279SBryan Cantrill 		if (!opt_verbose) {
3798048b0279SBryan Cantrill 			columns_str = safe_strdup(show_zones ?
3799048b0279SBryan Cantrill 			    "zone,state,stime,fmri" : "state,stime,fmri");
3800048b0279SBryan Cantrill 		} else {
3801048b0279SBryan Cantrill 			columns_str = safe_strdup(show_zones ?
3802048b0279SBryan Cantrill 			    "zone,state,nstate,stime,ctid,fmri" :
3803048b0279SBryan Cantrill 			    "state,nstate,stime,ctid,fmri");
3804048b0279SBryan Cantrill 		}
38057c478bd9Sstevel@tonic-gate 	}
38067c478bd9Sstevel@tonic-gate 
3807048b0279SBryan Cantrill 	if (opt_columns == NULL) {
3808048b0279SBryan Cantrill 		/* Decode columns_str into opt_columns. */
3809048b0279SBryan Cantrill 		line_sz = 0;
38107c478bd9Sstevel@tonic-gate 
3811048b0279SBryan Cantrill 		opt_cnum = 1;
3812048b0279SBryan Cantrill 		for (cp = columns_str; *cp != '\0'; ++cp)
3813048b0279SBryan Cantrill 			if (*cp == ',')
3814048b0279SBryan Cantrill 				++opt_cnum;
38157c478bd9Sstevel@tonic-gate 
3816be1977d8SNathan Burnett 		if (*columns_str == '\0')
3817be1977d8SNathan Burnett 			uu_die(gettext("No columns specified.\n"));
3818be1977d8SNathan Burnett 
3819048b0279SBryan Cantrill 		opt_columns = malloc(opt_cnum * sizeof (*opt_columns));
3820048b0279SBryan Cantrill 		if (opt_columns == NULL)
3821048b0279SBryan Cantrill 			uu_die(gettext("Too many columns.\n"));
38227c478bd9Sstevel@tonic-gate 
3823048b0279SBryan Cantrill 		for (n = 0; *columns_str != '\0'; ++n) {
3824048b0279SBryan Cantrill 			i = getcolumnopt(&columns_str);
3825048b0279SBryan Cantrill 			if (i == -1)
3826048b0279SBryan Cantrill 				uu_die(gettext("Unknown column \"%s\".\n"),
3827048b0279SBryan Cantrill 				    columns_str);
38287c478bd9Sstevel@tonic-gate 
3829048b0279SBryan Cantrill 			if (strcmp(columns[i].name, "N") == 0 ||
3830048b0279SBryan Cantrill 			    strcmp(columns[i].name, "SN") == 0 ||
3831048b0279SBryan Cantrill 			    strcmp(columns[i].name, "NSTA") == 0 ||
3832048b0279SBryan Cantrill 			    strcmp(columns[i].name, "NSTATE") == 0)
3833048b0279SBryan Cantrill 				opt_nstate_shown = 1;
38347c478bd9Sstevel@tonic-gate 
3835048b0279SBryan Cantrill 			opt_columns[n] = i;
3836048b0279SBryan Cantrill 			line_sz += columns[i].width + 1;
3837048b0279SBryan Cantrill 		}
38387c478bd9Sstevel@tonic-gate 
3839048b0279SBryan Cantrill 		if ((lines_pool = uu_avl_pool_create("lines_pool",
3840048b0279SBryan Cantrill 		    sizeof (struct avl_string), offsetof(struct avl_string,
3841048b0279SBryan Cantrill 		    node), line_cmp, UU_AVL_DEBUG)) == NULL ||
3842048b0279SBryan Cantrill 		    (lines = uu_avl_create(lines_pool, NULL, 0)) == NULL)
3843048b0279SBryan Cantrill 			uu_die(gettext("Unexpected libuutil error: %s\n"),
3844048b0279SBryan Cantrill 			    uu_strerror(uu_error()));
38457c478bd9Sstevel@tonic-gate 	}
38467c478bd9Sstevel@tonic-gate 
38477c478bd9Sstevel@tonic-gate 	switch (opt_mode) {
38487c478bd9Sstevel@tonic-gate 	case 0:
3849048b0279SBryan Cantrill 		/*
3850048b0279SBryan Cantrill 		 * If we already have a hash table (e.g., because we are
3851048b0279SBryan Cantrill 		 * processing multiple zones), destroy it before creating
3852048b0279SBryan Cantrill 		 * a new one.
3853048b0279SBryan Cantrill 		 */
3854048b0279SBryan Cantrill 		if (ht_buckets != NULL)
3855048b0279SBryan Cantrill 			ht_free();
3856048b0279SBryan Cantrill 
38577c478bd9Sstevel@tonic-gate 		ht_init();
38587c478bd9Sstevel@tonic-gate 
38597c478bd9Sstevel@tonic-gate 		/* Always show all FMRIs when given arguments or restarters */
38607c478bd9Sstevel@tonic-gate 		if (argc != 0 || restarters != NULL)
38617c478bd9Sstevel@tonic-gate 			show_all =  1;
38627c478bd9Sstevel@tonic-gate 
38637c478bd9Sstevel@tonic-gate 		if ((err = scf_walk_fmri(h, argc, argv,
38647c478bd9Sstevel@tonic-gate 		    SCF_WALK_MULTIPLE | SCF_WALK_LEGACY,
38657c478bd9Sstevel@tonic-gate 		    show_all ? list_instance : list_if_enabled, NULL,
3866048b0279SBryan Cantrill 		    errarg, errfunc)) != 0) {
38677c478bd9Sstevel@tonic-gate 			uu_warn(gettext("failed to iterate over "
38687c478bd9Sstevel@tonic-gate 			    "instances: %s\n"), scf_strerror(err));
38697c478bd9Sstevel@tonic-gate 			exit_status = UU_EXIT_FATAL;
38707c478bd9Sstevel@tonic-gate 		}
38717c478bd9Sstevel@tonic-gate 		break;
38727c478bd9Sstevel@tonic-gate 
38737c478bd9Sstevel@tonic-gate 	case 'd':
38747c478bd9Sstevel@tonic-gate 		if (argc == 0)
38757c478bd9Sstevel@tonic-gate 			argserr(progname);
38767c478bd9Sstevel@tonic-gate 
38777c478bd9Sstevel@tonic-gate 		if ((err = scf_walk_fmri(h, argc, argv,
38787c478bd9Sstevel@tonic-gate 		    SCF_WALK_MULTIPLE, list_dependencies, NULL,
3879048b0279SBryan Cantrill 		    errarg, errfunc)) != 0) {
38807c478bd9Sstevel@tonic-gate 			uu_warn(gettext("failed to iterate over "
38817c478bd9Sstevel@tonic-gate 			    "instances: %s\n"), scf_strerror(err));
38827c478bd9Sstevel@tonic-gate 			exit_status = UU_EXIT_FATAL;
38837c478bd9Sstevel@tonic-gate 		}
38847c478bd9Sstevel@tonic-gate 		break;
38857c478bd9Sstevel@tonic-gate 
38867c478bd9Sstevel@tonic-gate 	case 'D':
38877c478bd9Sstevel@tonic-gate 		if (argc == 0)
38887c478bd9Sstevel@tonic-gate 			argserr(progname);
38897c478bd9Sstevel@tonic-gate 
38907c478bd9Sstevel@tonic-gate 		provider_scope = safe_malloc(max_scf_fmri_length);
38917c478bd9Sstevel@tonic-gate 		provider_svc = safe_malloc(max_scf_fmri_length);
38927c478bd9Sstevel@tonic-gate 		provider_inst = safe_malloc(max_scf_fmri_length);
38937c478bd9Sstevel@tonic-gate 
38947c478bd9Sstevel@tonic-gate 		if ((err = scf_walk_fmri(h, argc, argv,
38957c478bd9Sstevel@tonic-gate 		    SCF_WALK_MULTIPLE | SCF_WALK_SERVICE,
38967c478bd9Sstevel@tonic-gate 		    list_dependents, NULL, &exit_status, uu_warn)) != 0) {
38977c478bd9Sstevel@tonic-gate 			uu_warn(gettext("failed to iterate over "
38987c478bd9Sstevel@tonic-gate 			    "instances: %s\n"), scf_strerror(err));
38997c478bd9Sstevel@tonic-gate 			exit_status = UU_EXIT_FATAL;
39007c478bd9Sstevel@tonic-gate 		}
39017c478bd9Sstevel@tonic-gate 
39027c478bd9Sstevel@tonic-gate 		free(provider_scope);
39037c478bd9Sstevel@tonic-gate 		free(provider_svc);
39047c478bd9Sstevel@tonic-gate 		free(provider_inst);
39057c478bd9Sstevel@tonic-gate 		break;
39067c478bd9Sstevel@tonic-gate 
3907f6e214c7SGavin Maltby 	case 'n':
3908f6e214c7SGavin Maltby 		break;
3909f6e214c7SGavin Maltby 
39107c478bd9Sstevel@tonic-gate 	default:
39117c478bd9Sstevel@tonic-gate 		assert(0);
39127c478bd9Sstevel@tonic-gate 		abort();
39137c478bd9Sstevel@tonic-gate 	}
39147c478bd9Sstevel@tonic-gate 
3915048b0279SBryan Cantrill nextzone:
3916048b0279SBryan Cantrill 	if (show_zones && zent < nzents && exit_status == 0) {
3917048b0279SBryan Cantrill 		scf_handle_destroy(h);
3918048b0279SBryan Cantrill 		goto again;
3919048b0279SBryan Cantrill 	}
3920048b0279SBryan Cantrill 
3921048b0279SBryan Cantrill 	if (show_zones && exit_status == 0)
3922048b0279SBryan Cantrill 		exit_status = missing;
3923048b0279SBryan Cantrill 
3924048b0279SBryan Cantrill 	if (opt_columns == NULL)
3925048b0279SBryan Cantrill 		return (exit_status);
3926048b0279SBryan Cantrill 
39277c478bd9Sstevel@tonic-gate 	if (show_header)
39287c478bd9Sstevel@tonic-gate 		print_header();
39297c478bd9Sstevel@tonic-gate 
39307c478bd9Sstevel@tonic-gate 	(void) uu_avl_walk(lines, print_line, NULL, 0);
39317c478bd9Sstevel@tonic-gate 
39327c478bd9Sstevel@tonic-gate 	return (exit_status);
39337c478bd9Sstevel@tonic-gate }
3934