xref: /titanic_50/usr/src/cmd/auditstat/auditstat.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #ifndef lint
30*7c478bd9Sstevel@tonic-gate static char	sccsid[] = "%Z%%M% %I% %E% SMI;";
31*7c478bd9Sstevel@tonic-gate static char	cmw_sccsid[] = "@(#)auditstat.c 2.3 92/01/30 SMI; SunOS CMW";
32*7c478bd9Sstevel@tonic-gate #endif
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
35*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
36*7c478bd9Sstevel@tonic-gate #include <ctype.h>
37*7c478bd9Sstevel@tonic-gate #include <stdio.h>
38*7c478bd9Sstevel@tonic-gate #include <bsm/audit.h>
39*7c478bd9Sstevel@tonic-gate #include <bsm/libbsm.h>
40*7c478bd9Sstevel@tonic-gate #include <unistd.h>
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate /*
43*7c478bd9Sstevel@tonic-gate  * Display header every HEADER_MOD lines printed
44*7c478bd9Sstevel@tonic-gate  */
45*7c478bd9Sstevel@tonic-gate #define		DFLT_HEADER_MOD (20)
46*7c478bd9Sstevel@tonic-gate #define		ONEK (1024)
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate #define		CFLG (0x01)
49*7c478bd9Sstevel@tonic-gate #define		HFLG (0x02)
50*7c478bd9Sstevel@tonic-gate #define		IFLG (0x04)
51*7c478bd9Sstevel@tonic-gate #define		NFLG (0x08)
52*7c478bd9Sstevel@tonic-gate #define		VFLG (0x10)
53*7c478bd9Sstevel@tonic-gate 
54*7c478bd9Sstevel@tonic-gate extern char	*optarg;
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate static int	count;
57*7c478bd9Sstevel@tonic-gate static int	flags;
58*7c478bd9Sstevel@tonic-gate static int	header_mod = DFLT_HEADER_MOD;
59*7c478bd9Sstevel@tonic-gate static int	interval;
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate static void	display_stats();
62*7c478bd9Sstevel@tonic-gate static void	eauditon();
63*7c478bd9Sstevel@tonic-gate static void	parse_args();
64*7c478bd9Sstevel@tonic-gate static void	usage_exit();
65*7c478bd9Sstevel@tonic-gate static int	strisdigit();
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate int
68*7c478bd9Sstevel@tonic-gate main(argc, argv)
69*7c478bd9Sstevel@tonic-gate int	argc;
70*7c478bd9Sstevel@tonic-gate char	**argv;
71*7c478bd9Sstevel@tonic-gate {
72*7c478bd9Sstevel@tonic-gate 	register int	i;
73*7c478bd9Sstevel@tonic-gate 	au_stat_t s;
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate 	(void) setbuf(stdout, (char *)0);
76*7c478bd9Sstevel@tonic-gate 	(void) setbuf(stderr, (char *)0);
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate 	parse_args(argc, argv);
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate 	if (!flags) {
81*7c478bd9Sstevel@tonic-gate 		eauditon(A_GETSTAT, (caddr_t)&s, NULL);
82*7c478bd9Sstevel@tonic-gate 		display_stats(&s, 0);
83*7c478bd9Sstevel@tonic-gate 		exit(0);
84*7c478bd9Sstevel@tonic-gate 	}
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate 	if (flags & VFLG || flags & NFLG)
87*7c478bd9Sstevel@tonic-gate 		eauditon(A_GETSTAT, (caddr_t)&s, NULL);
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate 	if (flags & VFLG)
90*7c478bd9Sstevel@tonic-gate 		(void) printf("version = %d\n", s.as_version);
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate 	if (flags & NFLG)
93*7c478bd9Sstevel@tonic-gate 		(void) printf("number of kernel events = %d\n", s.as_numevent);
94*7c478bd9Sstevel@tonic-gate 
95*7c478bd9Sstevel@tonic-gate 	if (!(flags & IFLG))
96*7c478bd9Sstevel@tonic-gate 		exit(0);
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate 	/* CSTYLED */
99*7c478bd9Sstevel@tonic-gate 	for (i = 0;; i++) {
100*7c478bd9Sstevel@tonic-gate 		eauditon(A_GETSTAT, (caddr_t)&s, NULL);
101*7c478bd9Sstevel@tonic-gate 		display_stats(&s, i);
102*7c478bd9Sstevel@tonic-gate 		if ((flags & CFLG) && count)
103*7c478bd9Sstevel@tonic-gate 			if (i == count - 1)
104*7c478bd9Sstevel@tonic-gate 				break;
105*7c478bd9Sstevel@tonic-gate 		(void) sleep(interval);
106*7c478bd9Sstevel@tonic-gate 	}
107*7c478bd9Sstevel@tonic-gate 
108*7c478bd9Sstevel@tonic-gate 	return (0);
109*7c478bd9Sstevel@tonic-gate }
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate 
112*7c478bd9Sstevel@tonic-gate static void
113*7c478bd9Sstevel@tonic-gate display_stats(s, cnt)
114*7c478bd9Sstevel@tonic-gate au_stat_t *s;
115*7c478bd9Sstevel@tonic-gate {
116*7c478bd9Sstevel@tonic-gate 	int	offset[12];   /* used to line the header up correctly */
117*7c478bd9Sstevel@tonic-gate 	char	buf[512];
118*7c478bd9Sstevel@tonic-gate 
119*7c478bd9Sstevel@tonic-gate 	(void) sprintf(buf,
120*7c478bd9Sstevel@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",
121*7c478bd9Sstevel@tonic-gate 		s->as_generated, 	&(offset[0]),
122*7c478bd9Sstevel@tonic-gate 		s->as_nonattrib, 	&(offset[1]),
123*7c478bd9Sstevel@tonic-gate 		s->as_kernel, 		&(offset[2]),
124*7c478bd9Sstevel@tonic-gate 		s->as_audit, 		&(offset[3]),
125*7c478bd9Sstevel@tonic-gate 		s->as_auditctl, 	&(offset[4]),
126*7c478bd9Sstevel@tonic-gate 		s->as_enqueue, 		&(offset[5]),
127*7c478bd9Sstevel@tonic-gate 		s->as_written, 		&(offset[6]),
128*7c478bd9Sstevel@tonic-gate 		s->as_wblocked, 	&(offset[7]),
129*7c478bd9Sstevel@tonic-gate 		s->as_rblocked, 	&(offset[8]),
130*7c478bd9Sstevel@tonic-gate 		s->as_dropped, 		&(offset[9]),
131*7c478bd9Sstevel@tonic-gate 		s->as_totalsize / ONEK,	&(offset[10]),
132*7c478bd9Sstevel@tonic-gate 		s->as_memused / ONEK, 	&(offset[11]));
133*7c478bd9Sstevel@tonic-gate 
134*7c478bd9Sstevel@tonic-gate 	/* print a properly aligned header every HEADER_MOD lines */
135*7c478bd9Sstevel@tonic-gate 	if (header_mod && (!cnt || !(cnt % header_mod))) {
136*7c478bd9Sstevel@tonic-gate 		(void) printf(
137*7c478bd9Sstevel@tonic-gate 			"%*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s\n",
138*7c478bd9Sstevel@tonic-gate 			offset[0] - 1,			"gen",
139*7c478bd9Sstevel@tonic-gate 			offset[1] - offset[0] - 1,	"nona",
140*7c478bd9Sstevel@tonic-gate 			offset[2] - offset[1] - 1,	"kern",
141*7c478bd9Sstevel@tonic-gate 			offset[3] - offset[2] - 1,	"aud",
142*7c478bd9Sstevel@tonic-gate 			offset[4] - offset[3] - 1,	"ctl",
143*7c478bd9Sstevel@tonic-gate 			offset[5] - offset[4] - 1,	"enq",
144*7c478bd9Sstevel@tonic-gate 			offset[6] - offset[5] - 1,	"wrtn",
145*7c478bd9Sstevel@tonic-gate 			offset[7] - offset[6] - 1,	"wblk",
146*7c478bd9Sstevel@tonic-gate 			offset[8] - offset[7] - 1,	"rblk",
147*7c478bd9Sstevel@tonic-gate 			offset[9] - offset[8] - 1,	"drop",
148*7c478bd9Sstevel@tonic-gate 			offset[10] - offset[9] - 1,	"tot",
149*7c478bd9Sstevel@tonic-gate 			offset[11] - offset[10],	"mem");
150*7c478bd9Sstevel@tonic-gate 	}
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate 	(void) puts(buf);
153*7c478bd9Sstevel@tonic-gate }
154*7c478bd9Sstevel@tonic-gate 
155*7c478bd9Sstevel@tonic-gate 
156*7c478bd9Sstevel@tonic-gate static void
157*7c478bd9Sstevel@tonic-gate eauditon(cmd, data, length)
158*7c478bd9Sstevel@tonic-gate int	cmd;
159*7c478bd9Sstevel@tonic-gate caddr_t data;
160*7c478bd9Sstevel@tonic-gate int	length;
161*7c478bd9Sstevel@tonic-gate {
162*7c478bd9Sstevel@tonic-gate 	if (auditon(cmd, data, length) == -1) {
163*7c478bd9Sstevel@tonic-gate 		perror("auditstat: auditon");
164*7c478bd9Sstevel@tonic-gate 		exit(1);
165*7c478bd9Sstevel@tonic-gate 	}
166*7c478bd9Sstevel@tonic-gate }
167*7c478bd9Sstevel@tonic-gate 
168*7c478bd9Sstevel@tonic-gate 
169*7c478bd9Sstevel@tonic-gate static void
170*7c478bd9Sstevel@tonic-gate parse_args(argc, argv)
171*7c478bd9Sstevel@tonic-gate int	argc;
172*7c478bd9Sstevel@tonic-gate char	**argv;
173*7c478bd9Sstevel@tonic-gate {
174*7c478bd9Sstevel@tonic-gate 	int	c;
175*7c478bd9Sstevel@tonic-gate 
176*7c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "c:h:i:vn")) != -1) {
177*7c478bd9Sstevel@tonic-gate 		switch (c) {
178*7c478bd9Sstevel@tonic-gate 		case 'c':
179*7c478bd9Sstevel@tonic-gate 			if (flags & CFLG)
180*7c478bd9Sstevel@tonic-gate 				usage_exit();
181*7c478bd9Sstevel@tonic-gate 			flags |= CFLG;
182*7c478bd9Sstevel@tonic-gate 			if (strisdigit(optarg)) {
183*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
184*7c478bd9Sstevel@tonic-gate 				"auditstat: invalid count specified.\n");
185*7c478bd9Sstevel@tonic-gate 				exit(1);
186*7c478bd9Sstevel@tonic-gate 			}
187*7c478bd9Sstevel@tonic-gate 			count = atoi(optarg);
188*7c478bd9Sstevel@tonic-gate 			break;
189*7c478bd9Sstevel@tonic-gate 		case 'h':
190*7c478bd9Sstevel@tonic-gate 			if (flags & HFLG)
191*7c478bd9Sstevel@tonic-gate 				usage_exit();
192*7c478bd9Sstevel@tonic-gate 			flags |= HFLG;
193*7c478bd9Sstevel@tonic-gate 			if (strisdigit(optarg)) {
194*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
195*7c478bd9Sstevel@tonic-gate 				"auditstat: invalid header arg specified.\n");
196*7c478bd9Sstevel@tonic-gate 				exit(1);
197*7c478bd9Sstevel@tonic-gate 			}
198*7c478bd9Sstevel@tonic-gate 			header_mod = atoi(optarg);
199*7c478bd9Sstevel@tonic-gate 			break;
200*7c478bd9Sstevel@tonic-gate 		case 'i':
201*7c478bd9Sstevel@tonic-gate 			if (flags & IFLG)
202*7c478bd9Sstevel@tonic-gate 				usage_exit();
203*7c478bd9Sstevel@tonic-gate 			flags |= IFLG;
204*7c478bd9Sstevel@tonic-gate 			if (strisdigit(optarg)) {
205*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
206*7c478bd9Sstevel@tonic-gate 				"auditstat: invalid interval specified.\n");
207*7c478bd9Sstevel@tonic-gate 				exit(1);
208*7c478bd9Sstevel@tonic-gate 			}
209*7c478bd9Sstevel@tonic-gate 			interval = atoi(optarg);
210*7c478bd9Sstevel@tonic-gate 			break;
211*7c478bd9Sstevel@tonic-gate 		case 'n':
212*7c478bd9Sstevel@tonic-gate 			if (flags & NFLG)
213*7c478bd9Sstevel@tonic-gate 				usage_exit();
214*7c478bd9Sstevel@tonic-gate 			flags |= NFLG;
215*7c478bd9Sstevel@tonic-gate 			break;
216*7c478bd9Sstevel@tonic-gate 		case 'v':
217*7c478bd9Sstevel@tonic-gate 			if (flags & VFLG)
218*7c478bd9Sstevel@tonic-gate 				usage_exit();
219*7c478bd9Sstevel@tonic-gate 			flags |= VFLG;
220*7c478bd9Sstevel@tonic-gate 			break;
221*7c478bd9Sstevel@tonic-gate 		case '?':
222*7c478bd9Sstevel@tonic-gate 		default:
223*7c478bd9Sstevel@tonic-gate 			usage_exit();
224*7c478bd9Sstevel@tonic-gate 			break;
225*7c478bd9Sstevel@tonic-gate 		}
226*7c478bd9Sstevel@tonic-gate 	}
227*7c478bd9Sstevel@tonic-gate }
228*7c478bd9Sstevel@tonic-gate 
229*7c478bd9Sstevel@tonic-gate 
230*7c478bd9Sstevel@tonic-gate static void
231*7c478bd9Sstevel@tonic-gate usage_exit()
232*7c478bd9Sstevel@tonic-gate {
233*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
234*7c478bd9Sstevel@tonic-gate 		"auditstat: usage: auditstat [-c count] [-h lines] \
235*7c478bd9Sstevel@tonic-gate 		[-i interval] [-n] [-v]\n");
236*7c478bd9Sstevel@tonic-gate 	exit(1);
237*7c478bd9Sstevel@tonic-gate }
238*7c478bd9Sstevel@tonic-gate 
239*7c478bd9Sstevel@tonic-gate 
240*7c478bd9Sstevel@tonic-gate static int
241*7c478bd9Sstevel@tonic-gate strisdigit(s)
242*7c478bd9Sstevel@tonic-gate char	*s;
243*7c478bd9Sstevel@tonic-gate {
244*7c478bd9Sstevel@tonic-gate 	for (; *s; s++)
245*7c478bd9Sstevel@tonic-gate 		if (!isdigit(*s))
246*7c478bd9Sstevel@tonic-gate 			return (1);
247*7c478bd9Sstevel@tonic-gate 
248*7c478bd9Sstevel@tonic-gate 	return (0);
249*7c478bd9Sstevel@tonic-gate }
250