xref: /illumos-gate/usr/src/lib/libexacct/demo/exdump.c (revision f48205be)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*f48205beScasper  * Common Development and Distribution License (the "License").
6*f48205beScasper  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*f48205beScasper  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <sys/acct.h>
297c478bd9Sstevel@tonic-gate #include <sys/wait.h>
307c478bd9Sstevel@tonic-gate #include <sys/types.h>
317c478bd9Sstevel@tonic-gate #include <sys/socket.h>
327c478bd9Sstevel@tonic-gate #include <stdio.h>
337c478bd9Sstevel@tonic-gate #include <fcntl.h>
347c478bd9Sstevel@tonic-gate #include <exacct.h>
357c478bd9Sstevel@tonic-gate #include <pwd.h>
367c478bd9Sstevel@tonic-gate #include <grp.h>
377c478bd9Sstevel@tonic-gate #include <project.h>
387c478bd9Sstevel@tonic-gate #include <stdlib.h>
397c478bd9Sstevel@tonic-gate #include <strings.h>
407c478bd9Sstevel@tonic-gate #include <netinet/in.h>
417c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
427c478bd9Sstevel@tonic-gate #include <netdb.h>
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #ifndef _LP64
457c478bd9Sstevel@tonic-gate #define	FMT_UINT64	"%-15llu"
467c478bd9Sstevel@tonic-gate #else
477c478bd9Sstevel@tonic-gate #define	FMT_UINT64	"%-15lu"
487c478bd9Sstevel@tonic-gate #endif
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate #define	MAX_DEPTH	25		/* maximum depth level */
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate static int vflag = 0;
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate typedef struct catalog_item {
557c478bd9Sstevel@tonic-gate 	int	type;
567c478bd9Sstevel@tonic-gate 	char	*name;
577c478bd9Sstevel@tonic-gate } catalog_item_t;
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate /*
607c478bd9Sstevel@tonic-gate  * The actual constants are defined in <sys/exacct_catalog.h>.
617c478bd9Sstevel@tonic-gate  */
627c478bd9Sstevel@tonic-gate static catalog_item_t catalog[] = {
637c478bd9Sstevel@tonic-gate 	{ EXD_VERSION,			"version" },
647c478bd9Sstevel@tonic-gate 	{ EXD_FILETYPE,			"filetype" },
657c478bd9Sstevel@tonic-gate 	{ EXD_CREATOR,			"creator" },
667c478bd9Sstevel@tonic-gate 	{ EXD_HOSTNAME,			"hostname" },
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate 	{ EXD_GROUP_HEADER,		"group-header" },
697c478bd9Sstevel@tonic-gate 	{ EXD_GROUP_PROC,		"group-proc" },
707c478bd9Sstevel@tonic-gate 	{ EXD_GROUP_TASK,		"group-task" },
717c478bd9Sstevel@tonic-gate 	{ EXD_GROUP_LWP,		"group-lwp" },
727c478bd9Sstevel@tonic-gate 	{ EXD_GROUP_FLOW,		"group-flow" },
737c478bd9Sstevel@tonic-gate 	{ EXD_GROUP_PROC_TAG,		"group-proc-tag" },
747c478bd9Sstevel@tonic-gate 	{ EXD_GROUP_TASK_TAG,		"group-task-tag" },
757c478bd9Sstevel@tonic-gate 	{ EXD_GROUP_LWP_TAG,		"group-lwp-tag" },
767c478bd9Sstevel@tonic-gate 	{ EXD_GROUP_PROC_PARTIAL,	"group-proc-partial" },
777c478bd9Sstevel@tonic-gate 	{ EXD_GROUP_TASK_PARTIAL,	"group-task-partial" },
787c478bd9Sstevel@tonic-gate 	{ EXD_GROUP_TASK_INTERVAL,	"group-task-interval" },
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 	{ EXD_PROC_PID,			"pid" },
817c478bd9Sstevel@tonic-gate 	{ EXD_PROC_ANCPID, 		"ppid" },
827c478bd9Sstevel@tonic-gate 	{ EXD_PROC_UID,			"uid" },
837c478bd9Sstevel@tonic-gate 	{ EXD_PROC_GID,			"gid" },
847c478bd9Sstevel@tonic-gate 	{ EXD_PROC_TASKID,		"taskid" },
857c478bd9Sstevel@tonic-gate 	{ EXD_PROC_PROJID,		"projid" },
867c478bd9Sstevel@tonic-gate 	{ EXD_PROC_HOSTNAME,		"hostname" },
877c478bd9Sstevel@tonic-gate 	{ EXD_PROC_COMMAND,		"command" },
887c478bd9Sstevel@tonic-gate 	{ EXD_PROC_WAIT_STATUS,		"wait-status" },
897c478bd9Sstevel@tonic-gate 	{ EXD_PROC_START_SEC,		"start-sec" },
907c478bd9Sstevel@tonic-gate 	{ EXD_PROC_START_NSEC,		"start-nsec" },
917c478bd9Sstevel@tonic-gate 	{ EXD_PROC_FINISH_SEC,		"finish-sec" },
927c478bd9Sstevel@tonic-gate 	{ EXD_PROC_FINISH_NSEC,		"finish-nsec" },
937c478bd9Sstevel@tonic-gate 	{ EXD_PROC_CPU_USER_SEC,	"cpu-user-sec" },
947c478bd9Sstevel@tonic-gate 	{ EXD_PROC_CPU_USER_NSEC,	"cpu-user-nsec" },
957c478bd9Sstevel@tonic-gate 	{ EXD_PROC_CPU_SYS_SEC,		"cpu-sys-sec" },
967c478bd9Sstevel@tonic-gate 	{ EXD_PROC_CPU_SYS_NSEC,	"cpu-sys-nsec" },
977c478bd9Sstevel@tonic-gate 	{ EXD_PROC_TTY_MAJOR,		"tty-major" },
987c478bd9Sstevel@tonic-gate 	{ EXD_PROC_TTY_MINOR,		"tty-minor" },
997c478bd9Sstevel@tonic-gate 	{ EXD_PROC_FAULTS_MAJOR,	"faults-major" },
1007c478bd9Sstevel@tonic-gate 	{ EXD_PROC_FAULTS_MINOR,	"faults-minor" },
1017c478bd9Sstevel@tonic-gate 	{ EXD_PROC_MESSAGES_RCV,	"msgs-recv" },
1027c478bd9Sstevel@tonic-gate 	{ EXD_PROC_MESSAGES_SND,	"msgs-snd" },
1037c478bd9Sstevel@tonic-gate 	{ EXD_PROC_BLOCKS_IN,		"blocks-in" },
1047c478bd9Sstevel@tonic-gate 	{ EXD_PROC_BLOCKS_OUT,		"blocks-out" },
1057c478bd9Sstevel@tonic-gate 	{ EXD_PROC_CHARS_RDWR,		"chars-rdwr" },
1067c478bd9Sstevel@tonic-gate 	{ EXD_PROC_CONTEXT_VOL,		"ctxt-vol" },
1077c478bd9Sstevel@tonic-gate 	{ EXD_PROC_CONTEXT_INV,		"ctxt-inv" },
1087c478bd9Sstevel@tonic-gate 	{ EXD_PROC_SIGNALS,		"signals" },
1097c478bd9Sstevel@tonic-gate 	{ EXD_PROC_SWAPS,		"swaps" },
1107c478bd9Sstevel@tonic-gate 	{ EXD_PROC_SYSCALLS,		"syscalls" },
1117c478bd9Sstevel@tonic-gate 	{ EXD_PROC_TAG,			"proc-tag" },
1127c478bd9Sstevel@tonic-gate 	{ EXD_PROC_ACCT_FLAGS,		"acctflags" },
1137c478bd9Sstevel@tonic-gate 	{ EXD_PROC_ZONENAME,		"zone" },
1147c478bd9Sstevel@tonic-gate 	{ EXD_PROC_MEM_RSS_AVG_K,	"memory-rss-avg-k" },
1157c478bd9Sstevel@tonic-gate 	{ EXD_PROC_MEM_RSS_MAX_K,	"memory-rss-max-k" },
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	{ EXD_TASK_TASKID,		"taskid" },
1187c478bd9Sstevel@tonic-gate 	{ EXD_TASK_ANCTASKID,		"anctaskid" },
1197c478bd9Sstevel@tonic-gate 	{ EXD_TASK_PROJID,		"projid" },
1207c478bd9Sstevel@tonic-gate 	{ EXD_TASK_HOSTNAME,		"hostname" },
1217c478bd9Sstevel@tonic-gate 	{ EXD_TASK_START_SEC,		"start-sec" },
1227c478bd9Sstevel@tonic-gate 	{ EXD_TASK_START_NSEC,		"start-nsec" },
1237c478bd9Sstevel@tonic-gate 	{ EXD_TASK_FINISH_SEC,		"finish-sec" },
1247c478bd9Sstevel@tonic-gate 	{ EXD_TASK_FINISH_NSEC,		"finish-nsec" },
1257c478bd9Sstevel@tonic-gate 	{ EXD_TASK_CPU_USER_SEC,	"cpu-user-sec" },
1267c478bd9Sstevel@tonic-gate 	{ EXD_TASK_CPU_USER_NSEC,	"cpu-user-nsec" },
1277c478bd9Sstevel@tonic-gate 	{ EXD_TASK_CPU_SYS_SEC,		"cpu-sys-sec" },
1287c478bd9Sstevel@tonic-gate 	{ EXD_TASK_CPU_SYS_NSEC,	"cpu-sys-nsec" },
1297c478bd9Sstevel@tonic-gate 	{ EXD_TASK_FAULTS_MAJOR,	"faults-major" },
1307c478bd9Sstevel@tonic-gate 	{ EXD_TASK_FAULTS_MINOR,	"faults-minor" },
1317c478bd9Sstevel@tonic-gate 	{ EXD_TASK_MESSAGES_RCV,	"msgs-recv" },
1327c478bd9Sstevel@tonic-gate 	{ EXD_TASK_MESSAGES_SND,	"msgs-snd" },
1337c478bd9Sstevel@tonic-gate 	{ EXD_TASK_BLOCKS_IN,		"blocks-in" },
1347c478bd9Sstevel@tonic-gate 	{ EXD_TASK_BLOCKS_OUT,		"blocks-out" },
1357c478bd9Sstevel@tonic-gate 	{ EXD_TASK_CHARS_RDWR,		"chars-rdwr" },
1367c478bd9Sstevel@tonic-gate 	{ EXD_TASK_CONTEXT_VOL,		"ctxt-vol" },
1377c478bd9Sstevel@tonic-gate 	{ EXD_TASK_CONTEXT_INV,		"ctxt-inv" },
1387c478bd9Sstevel@tonic-gate 	{ EXD_TASK_SIGNALS,		"signals" },
1397c478bd9Sstevel@tonic-gate 	{ EXD_TASK_SWAPS,		"swaps" },
1407c478bd9Sstevel@tonic-gate 	{ EXD_TASK_SYSCALLS,		"syscalls" },
1417c478bd9Sstevel@tonic-gate 	{ EXD_TASK_TAG,			"task-tag" },
1427c478bd9Sstevel@tonic-gate 	{ EXD_TASK_ZONENAME,		"zone" },
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	{ EXD_FLOW_V4SADDR,		"src-addr-v4" },
1457c478bd9Sstevel@tonic-gate 	{ EXD_FLOW_V4DADDR,		"dest-addr-v4" },
1467c478bd9Sstevel@tonic-gate 	{ EXD_FLOW_V6SADDR,		"src-addr-v6" },
1477c478bd9Sstevel@tonic-gate 	{ EXD_FLOW_V6DADDR,		"dest-addr-v6" },
1487c478bd9Sstevel@tonic-gate 	{ EXD_FLOW_SPORT,		"src-port" },
1497c478bd9Sstevel@tonic-gate 	{ EXD_FLOW_DPORT,		"dest-port" },
1507c478bd9Sstevel@tonic-gate 	{ EXD_FLOW_PROTOCOL,		"protocol" },
1517c478bd9Sstevel@tonic-gate 	{ EXD_FLOW_DSFIELD,		"diffserv-field" },
1527c478bd9Sstevel@tonic-gate 	{ EXD_FLOW_NBYTES,		"total-bytes" },
1537c478bd9Sstevel@tonic-gate 	{ EXD_FLOW_NPKTS,		"total-packets" },
1547c478bd9Sstevel@tonic-gate 	{ EXD_FLOW_CTIME,		"creation-time" },
1557c478bd9Sstevel@tonic-gate 	{ EXD_FLOW_LSEEN,		"last-seen" },
1567c478bd9Sstevel@tonic-gate 	{ EXD_FLOW_PROJID,		"projid" },
1577c478bd9Sstevel@tonic-gate 	{ EXD_FLOW_UID,			"uid" },
1587c478bd9Sstevel@tonic-gate 	{ EXD_FLOW_ANAME,		"action-name" },
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	{ EXD_NONE,			"none" }
1617c478bd9Sstevel@tonic-gate };
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate static void disp_obj(ea_object_t *o, int indent);
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate /*
1667c478bd9Sstevel@tonic-gate  * Convert catalog ID into catalog name.
1677c478bd9Sstevel@tonic-gate  */
1687c478bd9Sstevel@tonic-gate static char *
1697c478bd9Sstevel@tonic-gate catalog_name(int type)
1707c478bd9Sstevel@tonic-gate {
1717c478bd9Sstevel@tonic-gate 	int i = 0;
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	while (catalog[i].type != EXD_NONE) {
1747c478bd9Sstevel@tonic-gate 		if (catalog[i].type == type)
1757c478bd9Sstevel@tonic-gate 			return (catalog[i].name);
1767c478bd9Sstevel@tonic-gate 		i++;
1777c478bd9Sstevel@tonic-gate 	}
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	return ("unknown");
1807c478bd9Sstevel@tonic-gate }
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate /*
1837c478bd9Sstevel@tonic-gate  * Display port information, if available
1847c478bd9Sstevel@tonic-gate  */
1857c478bd9Sstevel@tonic-gate static void
1867c478bd9Sstevel@tonic-gate disp_port(uint16_t port)
1877c478bd9Sstevel@tonic-gate {
1887c478bd9Sstevel@tonic-gate 	struct servent *port_info;
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	port_info = getservbyport(htons(port), NULL);
1917c478bd9Sstevel@tonic-gate 	if (port_info != NULL) {
1927c478bd9Sstevel@tonic-gate 		(void) printf("%s", port_info->s_name);
1937c478bd9Sstevel@tonic-gate 	}
1947c478bd9Sstevel@tonic-gate }
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate /*
1977c478bd9Sstevel@tonic-gate  * Display host name for a given IP address if available.
1987c478bd9Sstevel@tonic-gate  */
1997c478bd9Sstevel@tonic-gate static void
2007c478bd9Sstevel@tonic-gate disp_host(char *addr, int family)
2017c478bd9Sstevel@tonic-gate {
2027c478bd9Sstevel@tonic-gate 	struct hostent *phe;
2037c478bd9Sstevel@tonic-gate 	uint_t len;
2047c478bd9Sstevel@tonic-gate 	int error_num;
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	len = (family == AF_INET) ? sizeof (struct in_addr) :
2077c478bd9Sstevel@tonic-gate 	    sizeof (struct in6_addr);
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	if ((phe = getipnodebyaddr(addr, len, family, &error_num)) != NULL) {
2107c478bd9Sstevel@tonic-gate 		(void) printf("%s", phe->h_name);
2117c478bd9Sstevel@tonic-gate 	}
2127c478bd9Sstevel@tonic-gate }
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate /*
2157c478bd9Sstevel@tonic-gate  * Display protocol information, if available.
2167c478bd9Sstevel@tonic-gate  */
2177c478bd9Sstevel@tonic-gate static void
2187c478bd9Sstevel@tonic-gate disp_proto(uint8_t protocol)
2197c478bd9Sstevel@tonic-gate {
2207c478bd9Sstevel@tonic-gate 	struct protoent *proto_ent;
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	proto_ent = getprotobynumber(protocol);
2237c478bd9Sstevel@tonic-gate 	if (proto_ent != NULL) {
2247c478bd9Sstevel@tonic-gate 		(void) printf("%s", proto_ent->p_name);
2257c478bd9Sstevel@tonic-gate 	}
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate }
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate /*
2307c478bd9Sstevel@tonic-gate  * Display recursively exacct objects in a given embedded group.
2317c478bd9Sstevel@tonic-gate  */
2327c478bd9Sstevel@tonic-gate static void
2337c478bd9Sstevel@tonic-gate disp_embedded_group(ea_object_t *eo, int indent)
2347c478bd9Sstevel@tonic-gate {
2357c478bd9Sstevel@tonic-gate 	while (eo != NULL) {
2367c478bd9Sstevel@tonic-gate 		disp_obj(eo, indent + 1);
2377c478bd9Sstevel@tonic-gate 		if (eo->eo_type == EO_GROUP)
2387c478bd9Sstevel@tonic-gate 			disp_embedded_group(eo->eo_group.eg_objs, indent + 1);
2397c478bd9Sstevel@tonic-gate 		eo = eo->eo_next;
2407c478bd9Sstevel@tonic-gate 	}
2417c478bd9Sstevel@tonic-gate }
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate /*
2447c478bd9Sstevel@tonic-gate  * Display the data stored in a given exacct object.
2457c478bd9Sstevel@tonic-gate  */
2467c478bd9Sstevel@tonic-gate static void
2477c478bd9Sstevel@tonic-gate disp_obj(ea_object_t *o, int indent)
2487c478bd9Sstevel@tonic-gate {
2497c478bd9Sstevel@tonic-gate 	char objname[30] = "                              ";
2507c478bd9Sstevel@tonic-gate 	int eol = 1;
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	if (indent > MAX_DEPTH) {
2537c478bd9Sstevel@tonic-gate 		objname[0] = '>';
2547c478bd9Sstevel@tonic-gate 		indent = 1;
2557c478bd9Sstevel@tonic-gate 	}
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	(void) printf("%6x\t", (o->eo_catalog & EXD_DATA_MASK));
2587c478bd9Sstevel@tonic-gate 	(void) snprintf(objname + indent, 30 - indent, "%-s",
2597c478bd9Sstevel@tonic-gate 	    catalog_name(o->eo_catalog & EXD_DATA_MASK));
2607c478bd9Sstevel@tonic-gate 	(void) printf("%-30s\t", objname);
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	switch (o->eo_catalog & EXT_TYPE_MASK) {
2637c478bd9Sstevel@tonic-gate 	case EXT_UINT8:
2647c478bd9Sstevel@tonic-gate 		(void) printf("%-15u", o->eo_item.ei_uint8);
2657c478bd9Sstevel@tonic-gate 		if (vflag &&
2667c478bd9Sstevel@tonic-gate 		    ((o->eo_catalog & EXD_DATA_MASK) == EXD_FLOW_PROTOCOL)) {
2677c478bd9Sstevel@tonic-gate 			disp_proto(o->eo_item.ei_uint8);
2687c478bd9Sstevel@tonic-gate 		}
2697c478bd9Sstevel@tonic-gate 		break;
2707c478bd9Sstevel@tonic-gate 	case EXT_UINT16:
2717c478bd9Sstevel@tonic-gate 		(void) printf("%-15u", o->eo_item.ei_uint16);
2727c478bd9Sstevel@tonic-gate 		if (vflag &&
2737c478bd9Sstevel@tonic-gate 		    (((o->eo_catalog & EXD_DATA_MASK) == EXD_FLOW_SPORT) ||
2747c478bd9Sstevel@tonic-gate 		    ((o->eo_catalog & EXD_DATA_MASK) == EXD_FLOW_DPORT))) {
2757c478bd9Sstevel@tonic-gate 			disp_port(o->eo_item.ei_uint16);
2767c478bd9Sstevel@tonic-gate 		}
2777c478bd9Sstevel@tonic-gate 		break;
2787c478bd9Sstevel@tonic-gate 	case EXT_UINT32:
2797c478bd9Sstevel@tonic-gate 		switch (o->eo_catalog & EXD_DATA_MASK) {
2807c478bd9Sstevel@tonic-gate 		case EXD_PROC_WAIT_STATUS:
2817c478bd9Sstevel@tonic-gate 			{
2827c478bd9Sstevel@tonic-gate 				int wstat = o->eo_item.ei_uint32;
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 				if (vflag) {
2857c478bd9Sstevel@tonic-gate 					if (WIFEXITED(wstat))
2867c478bd9Sstevel@tonic-gate 						(void) printf("%-14d exit",
2877c478bd9Sstevel@tonic-gate 						    WEXITSTATUS(wstat));
2887c478bd9Sstevel@tonic-gate 					else if (WIFSIGNALED(wstat))
2897c478bd9Sstevel@tonic-gate 						(void) printf("%14d, signal",
2907c478bd9Sstevel@tonic-gate 						    WTERMSIG(wstat));
2917c478bd9Sstevel@tonic-gate 					else
2927c478bd9Sstevel@tonic-gate 						(void) printf("%d", wstat);
2937c478bd9Sstevel@tonic-gate 				} else {
2947c478bd9Sstevel@tonic-gate 					(void) printf("%d", wstat);
2957c478bd9Sstevel@tonic-gate 				}
2967c478bd9Sstevel@tonic-gate 			}
2977c478bd9Sstevel@tonic-gate 			break;
2987c478bd9Sstevel@tonic-gate 		case EXD_PROC_UID:
2997c478bd9Sstevel@tonic-gate 			{
3007c478bd9Sstevel@tonic-gate 				uid_t uid = o->eo_item.ei_uint32;
3017c478bd9Sstevel@tonic-gate 
302*f48205beScasper 				(void) printf("%-15u", uid);
3037c478bd9Sstevel@tonic-gate 				if (vflag) {
3047c478bd9Sstevel@tonic-gate 					struct passwd *pwd;
3057c478bd9Sstevel@tonic-gate 					if ((pwd = getpwuid(uid)) != NULL)
3067c478bd9Sstevel@tonic-gate 						(void) printf("%s",
3077c478bd9Sstevel@tonic-gate 						    pwd->pw_name);
3087c478bd9Sstevel@tonic-gate 				}
3097c478bd9Sstevel@tonic-gate 			}
3107c478bd9Sstevel@tonic-gate 			break;
3117c478bd9Sstevel@tonic-gate 		case EXD_PROC_GID:
3127c478bd9Sstevel@tonic-gate 			{
3137c478bd9Sstevel@tonic-gate 				gid_t gid = o->eo_item.ei_uint32;
3147c478bd9Sstevel@tonic-gate 
315*f48205beScasper 				(void) printf("%-15u", gid);
3167c478bd9Sstevel@tonic-gate 				if (vflag) {
3177c478bd9Sstevel@tonic-gate 					struct group *grp;
3187c478bd9Sstevel@tonic-gate 					if ((grp = getgrgid(gid)) != NULL)
3197c478bd9Sstevel@tonic-gate 						(void) printf("%s",
3207c478bd9Sstevel@tonic-gate 						    grp->gr_name);
3217c478bd9Sstevel@tonic-gate 				}
3227c478bd9Sstevel@tonic-gate 			}
3237c478bd9Sstevel@tonic-gate 			break;
3247c478bd9Sstevel@tonic-gate 		case EXD_PROC_PROJID:
3257c478bd9Sstevel@tonic-gate 		case EXD_TASK_PROJID:
3267c478bd9Sstevel@tonic-gate 			{
3277c478bd9Sstevel@tonic-gate 				projid_t projid = o->eo_item.ei_uint32;
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 				(void) printf("%-15lu", projid);
3307c478bd9Sstevel@tonic-gate 				if (vflag) {
3317c478bd9Sstevel@tonic-gate 					struct project proj;
3327c478bd9Sstevel@tonic-gate 					char projbuf[PROJECT_BUFSZ];
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 					if (getprojbyid(projid, &proj, projbuf,
3357c478bd9Sstevel@tonic-gate 					    PROJECT_BUFSZ) != NULL)
3367c478bd9Sstevel@tonic-gate 						(void) printf("%s",
3377c478bd9Sstevel@tonic-gate 						    proj.pj_name);
3387c478bd9Sstevel@tonic-gate 				}
3397c478bd9Sstevel@tonic-gate 			}
3407c478bd9Sstevel@tonic-gate 			break;
3417c478bd9Sstevel@tonic-gate 		case EXD_PROC_ACCT_FLAGS:
3427c478bd9Sstevel@tonic-gate 			{
3437c478bd9Sstevel@tonic-gate 				int flag = o->eo_item.ei_uint32;
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 				(void) printf("%-15u", flag);
3467c478bd9Sstevel@tonic-gate 				if (vflag) {
3477c478bd9Sstevel@tonic-gate 					if (flag & AFORK)
3487c478bd9Sstevel@tonic-gate 						(void) printf("FORK ");
3497c478bd9Sstevel@tonic-gate 					if (flag & ASU)
3507c478bd9Sstevel@tonic-gate 						(void) printf("SU");
3517c478bd9Sstevel@tonic-gate 				}
3527c478bd9Sstevel@tonic-gate 			}
3537c478bd9Sstevel@tonic-gate 			break;
3547c478bd9Sstevel@tonic-gate 		case EXD_FLOW_V4SADDR:
3557c478bd9Sstevel@tonic-gate 			/* FALLTHRU */
3567c478bd9Sstevel@tonic-gate 		case EXD_FLOW_V4DADDR:
3577c478bd9Sstevel@tonic-gate 			{
3587c478bd9Sstevel@tonic-gate 				char str[INET_ADDRSTRLEN];
3597c478bd9Sstevel@tonic-gate 				uint32_t addr = htonl(o->eo_item.ei_uint32);
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 				(void) printf("%-15s",
3627c478bd9Sstevel@tonic-gate 				    inet_ntop(AF_INET, &addr, str,
3637c478bd9Sstevel@tonic-gate 				    INET_ADDRSTRLEN));
3647c478bd9Sstevel@tonic-gate 				if (vflag) {
3657c478bd9Sstevel@tonic-gate 					disp_host((char *)&addr, AF_INET);
3667c478bd9Sstevel@tonic-gate 				}
3677c478bd9Sstevel@tonic-gate 			}
3687c478bd9Sstevel@tonic-gate 			break;
3697c478bd9Sstevel@tonic-gate 		default:
3707c478bd9Sstevel@tonic-gate 			(void) printf("%u", o->eo_item.ei_uint32);
3717c478bd9Sstevel@tonic-gate 		}
3727c478bd9Sstevel@tonic-gate 		break;
3737c478bd9Sstevel@tonic-gate 	case EXT_UINT64:
3747c478bd9Sstevel@tonic-gate 		{
3757c478bd9Sstevel@tonic-gate 			time_t _time;
3767c478bd9Sstevel@tonic-gate 			char timebuf[20];
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 			(void) printf(FMT_UINT64, o->eo_item.ei_uint64);
3797c478bd9Sstevel@tonic-gate 			if (!vflag)
3807c478bd9Sstevel@tonic-gate 				break;
3817c478bd9Sstevel@tonic-gate 			if (ea_match_object_catalog(o, EXD_TASK_START_SEC) ||
3827c478bd9Sstevel@tonic-gate 			    ea_match_object_catalog(o, EXD_TASK_FINISH_SEC) ||
3837c478bd9Sstevel@tonic-gate 			    ea_match_object_catalog(o, EXD_PROC_START_SEC) ||
3847c478bd9Sstevel@tonic-gate 			    ea_match_object_catalog(o, EXD_PROC_FINISH_SEC) ||
3857c478bd9Sstevel@tonic-gate 			    ea_match_object_catalog(o, EXD_FLOW_LSEEN) ||
3867c478bd9Sstevel@tonic-gate 			    ea_match_object_catalog(o, EXD_FLOW_CTIME)) {
3877c478bd9Sstevel@tonic-gate 				_time = o->eo_item.ei_uint64;
3887c478bd9Sstevel@tonic-gate 				(void) strftime(timebuf, sizeof (timebuf),
3897c478bd9Sstevel@tonic-gate 				    "%D %T", localtime(&_time));
3907c478bd9Sstevel@tonic-gate 				(void) fputs(timebuf, stdout);
3917c478bd9Sstevel@tonic-gate 			}
3927c478bd9Sstevel@tonic-gate 		}
3937c478bd9Sstevel@tonic-gate 		break;
3947c478bd9Sstevel@tonic-gate 	case EXT_DOUBLE:
3957c478bd9Sstevel@tonic-gate 		(void) printf("%f", o->eo_item.ei_double);
3967c478bd9Sstevel@tonic-gate 		break;
3977c478bd9Sstevel@tonic-gate 	case EXT_STRING:
3987c478bd9Sstevel@tonic-gate 		(void) printf("\"%s\"", o->eo_item.ei_string);
3997c478bd9Sstevel@tonic-gate 		break;
4007c478bd9Sstevel@tonic-gate 	case EXT_RAW:
4017c478bd9Sstevel@tonic-gate 		switch (o->eo_catalog & EXD_DATA_MASK) {
4027c478bd9Sstevel@tonic-gate 		case EXD_FLOW_V6SADDR:
4037c478bd9Sstevel@tonic-gate 			/* FALLTHRU */
4047c478bd9Sstevel@tonic-gate 		case EXD_FLOW_V6DADDR:
4057c478bd9Sstevel@tonic-gate 			{
4067c478bd9Sstevel@tonic-gate 				in6_addr_t *addr;
4077c478bd9Sstevel@tonic-gate 				char str[INET6_ADDRSTRLEN];
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 				addr = (in6_addr_t *)o->eo_item.ei_raw;
4107c478bd9Sstevel@tonic-gate 				(void) printf("%-28s", inet_ntop(AF_INET6,
4117c478bd9Sstevel@tonic-gate 				    &addr->s6_addr, str, INET6_ADDRSTRLEN));
4127c478bd9Sstevel@tonic-gate 				if (vflag) {
4137c478bd9Sstevel@tonic-gate 					disp_host((char *)&addr->s6_addr,
4147c478bd9Sstevel@tonic-gate 					    AF_INET6);
4157c478bd9Sstevel@tonic-gate 				}
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 			}
4187c478bd9Sstevel@tonic-gate 			break;
4197c478bd9Sstevel@tonic-gate 		default:
4207c478bd9Sstevel@tonic-gate 			{
4217c478bd9Sstevel@tonic-gate 				ea_size_t size = o->eo_item.ei_size;
4227c478bd9Sstevel@tonic-gate 				char *buf = o->eo_item.ei_raw;
4237c478bd9Sstevel@tonic-gate 				uint64_t i;
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 				for (i = 0; i < size && i < 6; i++)
4267c478bd9Sstevel@tonic-gate 					(void) printf("0x%2X ", buf[i]);
4277c478bd9Sstevel@tonic-gate 				if (size > 6)
4287c478bd9Sstevel@tonic-gate 					(void) printf("...");
4297c478bd9Sstevel@tonic-gate 			}
4307c478bd9Sstevel@tonic-gate 		}
4317c478bd9Sstevel@tonic-gate 		break;
4327c478bd9Sstevel@tonic-gate 	case EXT_GROUP:
4337c478bd9Sstevel@tonic-gate 		(void) printf("[group of %u object(s)]", o->eo_group.eg_nobjs);
4347c478bd9Sstevel@tonic-gate 		break;
4357c478bd9Sstevel@tonic-gate 	case EXT_EXACCT_OBJECT:
4367c478bd9Sstevel@tonic-gate 		/*
4377c478bd9Sstevel@tonic-gate 		 * Embedded exacct records.
4387c478bd9Sstevel@tonic-gate 		 */
4397c478bd9Sstevel@tonic-gate 		{
4407c478bd9Sstevel@tonic-gate 			ea_object_type_t ot;
4417c478bd9Sstevel@tonic-gate 			ea_object_t *op;
4427c478bd9Sstevel@tonic-gate 			ea_object_t *eo;
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 			ot = ea_unpack_object(&op, EUP_ALLOC,
4457c478bd9Sstevel@tonic-gate 			    o->eo_item.ei_object, o->eo_item.ei_size);
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate 			if (ot == EO_ERROR) {
4487c478bd9Sstevel@tonic-gate 				(void) printf("error: couldn't unpack embedded "
4497c478bd9Sstevel@tonic-gate 				    "object\n");
4507c478bd9Sstevel@tonic-gate 				break;
4517c478bd9Sstevel@tonic-gate 			}
4527c478bd9Sstevel@tonic-gate 			eol = 0;
4537c478bd9Sstevel@tonic-gate 			if (ot == EO_GROUP) {
4547c478bd9Sstevel@tonic-gate 				(void) printf("[embedded group of %u "
4557c478bd9Sstevel@tonic-gate 				    "object(s)]\n", op->eo_group.eg_nobjs);
4567c478bd9Sstevel@tonic-gate 				eo = op->eo_group.eg_objs;
4577c478bd9Sstevel@tonic-gate 				disp_embedded_group(eo, indent);
4587c478bd9Sstevel@tonic-gate 			} else {
4597c478bd9Sstevel@tonic-gate 				(void) printf("[embedded object]\n");
4607c478bd9Sstevel@tonic-gate 				disp_obj(op, indent);
4617c478bd9Sstevel@tonic-gate 			}
4627c478bd9Sstevel@tonic-gate 			ea_free_object(op, EUP_ALLOC);
4637c478bd9Sstevel@tonic-gate 		}
4647c478bd9Sstevel@tonic-gate 		break;
4657c478bd9Sstevel@tonic-gate 	default:
4667c478bd9Sstevel@tonic-gate 		(void) printf("[complex value]");
4677c478bd9Sstevel@tonic-gate 		break;
4687c478bd9Sstevel@tonic-gate 	}
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	if (eol)
4717c478bd9Sstevel@tonic-gate 		(void) printf("\n");
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate }
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate /*
4767c478bd9Sstevel@tonic-gate  * Read and display a group of exacct objects from the file.
4777c478bd9Sstevel@tonic-gate  */
4787c478bd9Sstevel@tonic-gate static void
4797c478bd9Sstevel@tonic-gate disp_group(ea_file_t *ef, uint_t nobjs, int indent)
4807c478bd9Sstevel@tonic-gate {
4817c478bd9Sstevel@tonic-gate 	uint_t i;
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 	for (i = 0; i < nobjs; i++) {
4847c478bd9Sstevel@tonic-gate 		ea_object_t scratch;
4857c478bd9Sstevel@tonic-gate 		int res;
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 		if ((res = ea_get_object(ef, &scratch)) == -1) {
4887c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
4897c478bd9Sstevel@tonic-gate 			    "bad file: ea_get_object()==%d\n", res);
4907c478bd9Sstevel@tonic-gate 			exit(2);
4917c478bd9Sstevel@tonic-gate 		}
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 		disp_obj(&scratch, indent + 1);
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate 		if (scratch.eo_type == EO_GROUP)
4967c478bd9Sstevel@tonic-gate 			disp_group(ef, scratch.eo_group.eg_nobjs, indent + 1);
4977c478bd9Sstevel@tonic-gate 		else
4987c478bd9Sstevel@tonic-gate 			(void) ea_free_item(&scratch, EUP_ALLOC);
4997c478bd9Sstevel@tonic-gate 	}
5007c478bd9Sstevel@tonic-gate }
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate static void
5037c478bd9Sstevel@tonic-gate usage()
5047c478bd9Sstevel@tonic-gate {
5057c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "Usage: exdump [-v] <file>\n");
5067c478bd9Sstevel@tonic-gate 	exit(2);
5077c478bd9Sstevel@tonic-gate }
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate int
5107c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
5117c478bd9Sstevel@tonic-gate {
5127c478bd9Sstevel@tonic-gate 	ea_file_t ef;
5137c478bd9Sstevel@tonic-gate 	ea_object_t scratch;
5147c478bd9Sstevel@tonic-gate 	char *fname;
5157c478bd9Sstevel@tonic-gate 	int opt;
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, "v")) != EOF) {
5187c478bd9Sstevel@tonic-gate 		switch (opt) {
5197c478bd9Sstevel@tonic-gate 		case 'v':
5207c478bd9Sstevel@tonic-gate 			vflag = 1;
5217c478bd9Sstevel@tonic-gate 			break;
5227c478bd9Sstevel@tonic-gate 		default:
5237c478bd9Sstevel@tonic-gate 			usage();
5247c478bd9Sstevel@tonic-gate 		}
5257c478bd9Sstevel@tonic-gate 	}
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 	if (argc == optind)
5287c478bd9Sstevel@tonic-gate 		usage();
5297c478bd9Sstevel@tonic-gate 	if (argc > optind)
5307c478bd9Sstevel@tonic-gate 		fname = argv[optind++];
5317c478bd9Sstevel@tonic-gate 	if (argc > optind)
5327c478bd9Sstevel@tonic-gate 		usage();
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate 	if (ea_open(&ef, fname, NULL,
5357c478bd9Sstevel@tonic-gate 	    vflag ? EO_NO_VALID_HDR : 0, O_RDONLY, 0) == -1) {
5367c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "exdump: cannot open %s\n", fname);
5377c478bd9Sstevel@tonic-gate 		return (1);
5387c478bd9Sstevel@tonic-gate 	}
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 	bzero(&scratch, sizeof (ea_object_t));
5417c478bd9Sstevel@tonic-gate 	while (ea_get_object(&ef, &scratch) != -1) {
5427c478bd9Sstevel@tonic-gate 		disp_obj(&scratch, 0);
5437c478bd9Sstevel@tonic-gate 		if (scratch.eo_type == EO_GROUP)
5447c478bd9Sstevel@tonic-gate 			disp_group(&ef, scratch.eo_group.eg_nobjs, 0);
5457c478bd9Sstevel@tonic-gate 		else
5467c478bd9Sstevel@tonic-gate 			(void) ea_free_item(&scratch, EUP_ALLOC);
5477c478bd9Sstevel@tonic-gate 		(void) bzero(&scratch, sizeof (ea_object_t));
5487c478bd9Sstevel@tonic-gate 	}
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate 	(void) ea_close(&ef);
5517c478bd9Sstevel@tonic-gate 	return (0);
5527c478bd9Sstevel@tonic-gate }
553