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