xref: /titanic_51/usr/src/cmd/auditstat/auditstat.c (revision 26fd77009b17f8c8fb32eb362584cfd635e87ad9)
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
5d75d0dc9Stz204579  * Common Development and Distribution License (the "License").
6d75d0dc9Stz204579  * 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*26fd7700SKrishnendu Sadhukhan - Sun Microsystems  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
26*26fd7700SKrishnendu Sadhukhan - Sun Microsystems #include "statcommon.h"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <sys/types.h>
297c478bd9Sstevel@tonic-gate #include <stdlib.h>
307c478bd9Sstevel@tonic-gate #include <ctype.h>
317c478bd9Sstevel@tonic-gate #include <stdio.h>
327c478bd9Sstevel@tonic-gate #include <bsm/audit.h>
337c478bd9Sstevel@tonic-gate #include <bsm/libbsm.h>
347c478bd9Sstevel@tonic-gate #include <unistd.h>
35*26fd7700SKrishnendu Sadhukhan - Sun Microsystems #include <locale.h>
36*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 
37*26fd7700SKrishnendu Sadhukhan - Sun Microsystems #if !defined(TEXT_DOMAIN)		/* Should be defined by cc -D */
38*26fd7700SKrishnendu Sadhukhan - Sun Microsystems #define	TEXT_DOMAIN "SYS_TEST"		/* Use this only if it isn't */
39*26fd7700SKrishnendu Sadhukhan - Sun Microsystems #endif
40*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate /*
437c478bd9Sstevel@tonic-gate  * Display header every HEADER_MOD lines printed
447c478bd9Sstevel@tonic-gate  */
457c478bd9Sstevel@tonic-gate #define		DFLT_HEADER_MOD (20)
467c478bd9Sstevel@tonic-gate #define		ONEK (1024)
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #define		CFLG (0x01)
497c478bd9Sstevel@tonic-gate #define		HFLG (0x02)
507c478bd9Sstevel@tonic-gate #define		IFLG (0x04)
517c478bd9Sstevel@tonic-gate #define		NFLG (0x08)
527c478bd9Sstevel@tonic-gate #define		VFLG (0x10)
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate extern char	*optarg;
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate static int	count;
577c478bd9Sstevel@tonic-gate static int	flags;
587c478bd9Sstevel@tonic-gate static int	header_mod = DFLT_HEADER_MOD;
597c478bd9Sstevel@tonic-gate static int	interval;
60*26fd7700SKrishnendu Sadhukhan - Sun Microsystems static uint_t timestamp_fmt = NODATE;
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate static void	display_stats();
637c478bd9Sstevel@tonic-gate static void	eauditon();
647c478bd9Sstevel@tonic-gate static void	parse_args();
657c478bd9Sstevel@tonic-gate static void	usage_exit();
667c478bd9Sstevel@tonic-gate static int	strisdigit();
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate int
697c478bd9Sstevel@tonic-gate main(argc, argv)
707c478bd9Sstevel@tonic-gate int	argc;
717c478bd9Sstevel@tonic-gate char	**argv;
727c478bd9Sstevel@tonic-gate {
737c478bd9Sstevel@tonic-gate 	register int	i;
747c478bd9Sstevel@tonic-gate 	au_stat_t s;
757c478bd9Sstevel@tonic-gate 
76*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 	(void) setlocale(LC_ALL, "");
77*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 	(void) textdomain(TEXT_DOMAIN);
78*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 
797c478bd9Sstevel@tonic-gate 	(void) setbuf(stdout, (char *)0);
807c478bd9Sstevel@tonic-gate 	(void) setbuf(stderr, (char *)0);
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 	parse_args(argc, argv);
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	if (!flags) {
857c478bd9Sstevel@tonic-gate 		eauditon(A_GETSTAT, (caddr_t)&s, NULL);
86*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 		if (timestamp_fmt != NODATE)
87*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 			print_timestamp(timestamp_fmt);
887c478bd9Sstevel@tonic-gate 		display_stats(&s, 0);
897c478bd9Sstevel@tonic-gate 		exit(0);
907c478bd9Sstevel@tonic-gate 	}
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	if (flags & VFLG || flags & NFLG)
937c478bd9Sstevel@tonic-gate 		eauditon(A_GETSTAT, (caddr_t)&s, NULL);
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 	if (flags & VFLG)
967c478bd9Sstevel@tonic-gate 		(void) printf("version = %d\n", s.as_version);
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	if (flags & NFLG)
997c478bd9Sstevel@tonic-gate 		(void) printf("number of kernel events = %d\n", s.as_numevent);
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	if (!(flags & IFLG))
1027c478bd9Sstevel@tonic-gate 		exit(0);
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	/* CSTYLED */
1057c478bd9Sstevel@tonic-gate 	for (i = 0;; i++) {
1067c478bd9Sstevel@tonic-gate 		eauditon(A_GETSTAT, (caddr_t)&s, NULL);
107*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 		if (timestamp_fmt != NODATE)
108*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 			print_timestamp(timestamp_fmt);
1097c478bd9Sstevel@tonic-gate 		display_stats(&s, i);
1107c478bd9Sstevel@tonic-gate 		if ((flags & CFLG) && count)
1117c478bd9Sstevel@tonic-gate 			if (i == count - 1)
1127c478bd9Sstevel@tonic-gate 				break;
1137c478bd9Sstevel@tonic-gate 		(void) sleep(interval);
1147c478bd9Sstevel@tonic-gate 	}
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 	return (0);
1177c478bd9Sstevel@tonic-gate }
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate static void
1217c478bd9Sstevel@tonic-gate display_stats(s, cnt)
1227c478bd9Sstevel@tonic-gate au_stat_t *s;
1237c478bd9Sstevel@tonic-gate {
1247c478bd9Sstevel@tonic-gate 	int	offset[12];   /* used to line the header up correctly */
1257c478bd9Sstevel@tonic-gate 	char	buf[512];
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	(void) sprintf(buf,
1287c478bd9Sstevel@tonic-gate "%4u %n%4u %n%4u %n%4u %n%4u %n%4u %n%4u %n%4u %n%4u %n%4u %n%4u %n%4u%n",
1297c478bd9Sstevel@tonic-gate 		s->as_generated, 	&(offset[0]),
1307c478bd9Sstevel@tonic-gate 		s->as_nonattrib, 	&(offset[1]),
1317c478bd9Sstevel@tonic-gate 		s->as_kernel, 		&(offset[2]),
1327c478bd9Sstevel@tonic-gate 		s->as_audit, 		&(offset[3]),
1337c478bd9Sstevel@tonic-gate 		s->as_auditctl, 	&(offset[4]),
1347c478bd9Sstevel@tonic-gate 		s->as_enqueue, 		&(offset[5]),
1357c478bd9Sstevel@tonic-gate 		s->as_written, 		&(offset[6]),
1367c478bd9Sstevel@tonic-gate 		s->as_wblocked, 	&(offset[7]),
1377c478bd9Sstevel@tonic-gate 		s->as_rblocked, 	&(offset[8]),
1387c478bd9Sstevel@tonic-gate 		s->as_dropped, 		&(offset[9]),
1397c478bd9Sstevel@tonic-gate 		s->as_totalsize / ONEK,	&(offset[10]),
1407c478bd9Sstevel@tonic-gate 		s->as_memused / ONEK, 	&(offset[11]));
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	/* print a properly aligned header every HEADER_MOD lines */
143*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 	if (header_mod && (!cnt || ((timestamp_fmt != NODATE) ?
144*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 	    !(cnt % (header_mod / 2)) : !(cnt % header_mod)))) {
1457c478bd9Sstevel@tonic-gate 		(void) printf(
1467c478bd9Sstevel@tonic-gate 			"%*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s\n",
1477c478bd9Sstevel@tonic-gate 			offset[0] - 1,			"gen",
1487c478bd9Sstevel@tonic-gate 			offset[1] - offset[0] - 1,	"nona",
1497c478bd9Sstevel@tonic-gate 			offset[2] - offset[1] - 1,	"kern",
1507c478bd9Sstevel@tonic-gate 			offset[3] - offset[2] - 1,	"aud",
1517c478bd9Sstevel@tonic-gate 			offset[4] - offset[3] - 1,	"ctl",
1527c478bd9Sstevel@tonic-gate 			offset[5] - offset[4] - 1,	"enq",
1537c478bd9Sstevel@tonic-gate 			offset[6] - offset[5] - 1,	"wrtn",
1547c478bd9Sstevel@tonic-gate 			offset[7] - offset[6] - 1,	"wblk",
1557c478bd9Sstevel@tonic-gate 			offset[8] - offset[7] - 1,	"rblk",
1567c478bd9Sstevel@tonic-gate 			offset[9] - offset[8] - 1,	"drop",
1577c478bd9Sstevel@tonic-gate 			offset[10] - offset[9] - 1,	"tot",
1587c478bd9Sstevel@tonic-gate 			offset[11] - offset[10],	"mem");
1597c478bd9Sstevel@tonic-gate 	}
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	(void) puts(buf);
1627c478bd9Sstevel@tonic-gate }
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate static void
1667c478bd9Sstevel@tonic-gate eauditon(cmd, data, length)
1677c478bd9Sstevel@tonic-gate int	cmd;
1687c478bd9Sstevel@tonic-gate caddr_t data;
1697c478bd9Sstevel@tonic-gate int	length;
1707c478bd9Sstevel@tonic-gate {
1717c478bd9Sstevel@tonic-gate 	if (auditon(cmd, data, length) == -1) {
1727c478bd9Sstevel@tonic-gate 		perror("auditstat: auditon");
1737c478bd9Sstevel@tonic-gate 		exit(1);
1747c478bd9Sstevel@tonic-gate 	}
1757c478bd9Sstevel@tonic-gate }
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate static void
1797c478bd9Sstevel@tonic-gate parse_args(argc, argv)
1807c478bd9Sstevel@tonic-gate int	argc;
1817c478bd9Sstevel@tonic-gate char	**argv;
1827c478bd9Sstevel@tonic-gate {
1837c478bd9Sstevel@tonic-gate 	int	c;
1847c478bd9Sstevel@tonic-gate 
185*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 	while ((c = getopt(argc, argv, "c:h:i:vnT:")) != -1) {
1867c478bd9Sstevel@tonic-gate 		switch (c) {
1877c478bd9Sstevel@tonic-gate 		case 'c':
1887c478bd9Sstevel@tonic-gate 			if (flags & CFLG)
1897c478bd9Sstevel@tonic-gate 				usage_exit();
1907c478bd9Sstevel@tonic-gate 			flags |= CFLG;
1917c478bd9Sstevel@tonic-gate 			if (strisdigit(optarg)) {
1927c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
1937c478bd9Sstevel@tonic-gate 				"auditstat: invalid count specified.\n");
1947c478bd9Sstevel@tonic-gate 				exit(1);
1957c478bd9Sstevel@tonic-gate 			}
1967c478bd9Sstevel@tonic-gate 			count = atoi(optarg);
1977c478bd9Sstevel@tonic-gate 			break;
1987c478bd9Sstevel@tonic-gate 		case 'h':
1997c478bd9Sstevel@tonic-gate 			if (flags & HFLG)
2007c478bd9Sstevel@tonic-gate 				usage_exit();
2017c478bd9Sstevel@tonic-gate 			flags |= HFLG;
2027c478bd9Sstevel@tonic-gate 			if (strisdigit(optarg)) {
2037c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
2047c478bd9Sstevel@tonic-gate 				"auditstat: invalid header arg specified.\n");
2057c478bd9Sstevel@tonic-gate 				exit(1);
2067c478bd9Sstevel@tonic-gate 			}
2077c478bd9Sstevel@tonic-gate 			header_mod = atoi(optarg);
2087c478bd9Sstevel@tonic-gate 			break;
2097c478bd9Sstevel@tonic-gate 		case 'i':
2107c478bd9Sstevel@tonic-gate 			if (flags & IFLG)
2117c478bd9Sstevel@tonic-gate 				usage_exit();
2127c478bd9Sstevel@tonic-gate 			flags |= IFLG;
2137c478bd9Sstevel@tonic-gate 			if (strisdigit(optarg)) {
2147c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
2157c478bd9Sstevel@tonic-gate 				"auditstat: invalid interval specified.\n");
2167c478bd9Sstevel@tonic-gate 				exit(1);
2177c478bd9Sstevel@tonic-gate 			}
2187c478bd9Sstevel@tonic-gate 			interval = atoi(optarg);
2197c478bd9Sstevel@tonic-gate 			break;
2207c478bd9Sstevel@tonic-gate 		case 'n':
2217c478bd9Sstevel@tonic-gate 			if (flags & NFLG)
2227c478bd9Sstevel@tonic-gate 				usage_exit();
2237c478bd9Sstevel@tonic-gate 			flags |= NFLG;
2247c478bd9Sstevel@tonic-gate 			break;
2257c478bd9Sstevel@tonic-gate 		case 'v':
2267c478bd9Sstevel@tonic-gate 			if (flags & VFLG)
2277c478bd9Sstevel@tonic-gate 				usage_exit();
2287c478bd9Sstevel@tonic-gate 			flags |= VFLG;
2297c478bd9Sstevel@tonic-gate 			break;
230*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 		case 'T':
231*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 			if (optarg) {
232*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 				if (*optarg == 'u')
233*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 					timestamp_fmt = UDATE;
234*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 				else if (*optarg == 'd')
235*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 					timestamp_fmt = DDATE;
236*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 				else
237*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 					usage_exit();
238*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 			} else {
239*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 				usage_exit();
240*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 			}
241*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 			break;
2427c478bd9Sstevel@tonic-gate 		case '?':
2437c478bd9Sstevel@tonic-gate 		default:
2447c478bd9Sstevel@tonic-gate 			usage_exit();
2457c478bd9Sstevel@tonic-gate 			break;
2467c478bd9Sstevel@tonic-gate 		}
2477c478bd9Sstevel@tonic-gate 	}
2487c478bd9Sstevel@tonic-gate }
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate static void
2527c478bd9Sstevel@tonic-gate usage_exit()
2537c478bd9Sstevel@tonic-gate {
2547c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
255d75d0dc9Stz204579 	    "auditstat: usage: auditstat [-c count] [-h lines] "
256*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 	    "[-T d|u] [-i interval] [-n] [-v]\n");
2577c478bd9Sstevel@tonic-gate 	exit(1);
2587c478bd9Sstevel@tonic-gate }
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate static int
2627c478bd9Sstevel@tonic-gate strisdigit(s)
2637c478bd9Sstevel@tonic-gate char	*s;
2647c478bd9Sstevel@tonic-gate {
2657c478bd9Sstevel@tonic-gate 	for (; *s; s++)
2667c478bd9Sstevel@tonic-gate 		if (!isdigit(*s))
2677c478bd9Sstevel@tonic-gate 			return (1);
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 	return (0);
2707c478bd9Sstevel@tonic-gate }
271