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