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 /*
2226fd7700SKrishnendu 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*ba00d94aSJohn Levon /*
27*ba00d94aSJohn Levon * Copyright (c) 2018, Joyent, Inc.
28*ba00d94aSJohn Levon */
29*ba00d94aSJohn Levon
3026fd7700SKrishnendu Sadhukhan - Sun Microsystems #include "statcommon.h"
317c478bd9Sstevel@tonic-gate
327c478bd9Sstevel@tonic-gate #include <sys/types.h>
337c478bd9Sstevel@tonic-gate #include <stdlib.h>
347c478bd9Sstevel@tonic-gate #include <ctype.h>
357c478bd9Sstevel@tonic-gate #include <stdio.h>
367c478bd9Sstevel@tonic-gate #include <bsm/audit.h>
377c478bd9Sstevel@tonic-gate #include <bsm/libbsm.h>
387c478bd9Sstevel@tonic-gate #include <unistd.h>
3926fd7700SKrishnendu Sadhukhan - Sun Microsystems #include <locale.h>
4026fd7700SKrishnendu Sadhukhan - Sun Microsystems
4126fd7700SKrishnendu Sadhukhan - Sun Microsystems #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
4226fd7700SKrishnendu Sadhukhan - Sun Microsystems #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it isn't */
4326fd7700SKrishnendu Sadhukhan - Sun Microsystems #endif
4426fd7700SKrishnendu Sadhukhan - Sun Microsystems
457c478bd9Sstevel@tonic-gate
467c478bd9Sstevel@tonic-gate /*
477c478bd9Sstevel@tonic-gate * Display header every HEADER_MOD lines printed
487c478bd9Sstevel@tonic-gate */
497c478bd9Sstevel@tonic-gate #define DFLT_HEADER_MOD (20)
507c478bd9Sstevel@tonic-gate #define ONEK (1024)
517c478bd9Sstevel@tonic-gate
527c478bd9Sstevel@tonic-gate #define CFLG (0x01)
537c478bd9Sstevel@tonic-gate #define HFLG (0x02)
547c478bd9Sstevel@tonic-gate #define IFLG (0x04)
557c478bd9Sstevel@tonic-gate #define NFLG (0x08)
567c478bd9Sstevel@tonic-gate #define VFLG (0x10)
577c478bd9Sstevel@tonic-gate
587c478bd9Sstevel@tonic-gate extern char *optarg;
597c478bd9Sstevel@tonic-gate
607c478bd9Sstevel@tonic-gate static int count;
617c478bd9Sstevel@tonic-gate static int flags;
627c478bd9Sstevel@tonic-gate static int header_mod = DFLT_HEADER_MOD;
637c478bd9Sstevel@tonic-gate static int interval;
6426fd7700SKrishnendu Sadhukhan - Sun Microsystems static uint_t timestamp_fmt = NODATE;
657c478bd9Sstevel@tonic-gate
667c478bd9Sstevel@tonic-gate static void display_stats();
677c478bd9Sstevel@tonic-gate static void eauditon();
687c478bd9Sstevel@tonic-gate static void parse_args();
697c478bd9Sstevel@tonic-gate static void usage_exit();
707c478bd9Sstevel@tonic-gate static int strisdigit();
717c478bd9Sstevel@tonic-gate
727c478bd9Sstevel@tonic-gate int
main(argc,argv)737c478bd9Sstevel@tonic-gate main(argc, argv)
747c478bd9Sstevel@tonic-gate int argc;
757c478bd9Sstevel@tonic-gate char **argv;
767c478bd9Sstevel@tonic-gate {
777c478bd9Sstevel@tonic-gate register int i;
787c478bd9Sstevel@tonic-gate au_stat_t s;
797c478bd9Sstevel@tonic-gate
8026fd7700SKrishnendu Sadhukhan - Sun Microsystems (void) setlocale(LC_ALL, "");
8126fd7700SKrishnendu Sadhukhan - Sun Microsystems (void) textdomain(TEXT_DOMAIN);
8226fd7700SKrishnendu Sadhukhan - Sun Microsystems
837c478bd9Sstevel@tonic-gate (void) setbuf(stdout, (char *)0);
847c478bd9Sstevel@tonic-gate (void) setbuf(stderr, (char *)0);
857c478bd9Sstevel@tonic-gate
867c478bd9Sstevel@tonic-gate parse_args(argc, argv);
877c478bd9Sstevel@tonic-gate
887c478bd9Sstevel@tonic-gate if (!flags) {
897c478bd9Sstevel@tonic-gate eauditon(A_GETSTAT, (caddr_t)&s, NULL);
9026fd7700SKrishnendu Sadhukhan - Sun Microsystems if (timestamp_fmt != NODATE)
9126fd7700SKrishnendu Sadhukhan - Sun Microsystems print_timestamp(timestamp_fmt);
927c478bd9Sstevel@tonic-gate display_stats(&s, 0);
937c478bd9Sstevel@tonic-gate exit(0);
947c478bd9Sstevel@tonic-gate }
957c478bd9Sstevel@tonic-gate
967c478bd9Sstevel@tonic-gate if (flags & VFLG || flags & NFLG)
977c478bd9Sstevel@tonic-gate eauditon(A_GETSTAT, (caddr_t)&s, NULL);
987c478bd9Sstevel@tonic-gate
997c478bd9Sstevel@tonic-gate if (flags & VFLG)
1007c478bd9Sstevel@tonic-gate (void) printf("version = %d\n", s.as_version);
1017c478bd9Sstevel@tonic-gate
1027c478bd9Sstevel@tonic-gate if (flags & NFLG)
1037c478bd9Sstevel@tonic-gate (void) printf("number of kernel events = %d\n", s.as_numevent);
1047c478bd9Sstevel@tonic-gate
1057c478bd9Sstevel@tonic-gate if (!(flags & IFLG))
1067c478bd9Sstevel@tonic-gate exit(0);
1077c478bd9Sstevel@tonic-gate
1087c478bd9Sstevel@tonic-gate /* CSTYLED */
1097c478bd9Sstevel@tonic-gate for (i = 0;; i++) {
1107c478bd9Sstevel@tonic-gate eauditon(A_GETSTAT, (caddr_t)&s, NULL);
11126fd7700SKrishnendu Sadhukhan - Sun Microsystems if (timestamp_fmt != NODATE)
11226fd7700SKrishnendu Sadhukhan - Sun Microsystems print_timestamp(timestamp_fmt);
1137c478bd9Sstevel@tonic-gate display_stats(&s, i);
1147c478bd9Sstevel@tonic-gate if ((flags & CFLG) && count)
1157c478bd9Sstevel@tonic-gate if (i == count - 1)
1167c478bd9Sstevel@tonic-gate break;
1177c478bd9Sstevel@tonic-gate (void) sleep(interval);
1187c478bd9Sstevel@tonic-gate }
1197c478bd9Sstevel@tonic-gate
1207c478bd9Sstevel@tonic-gate return (0);
1217c478bd9Sstevel@tonic-gate }
1227c478bd9Sstevel@tonic-gate
1237c478bd9Sstevel@tonic-gate
1247c478bd9Sstevel@tonic-gate static void
display_stats(au_stat_t * s,int cnt)125*ba00d94aSJohn Levon display_stats(au_stat_t *s, int cnt)
1267c478bd9Sstevel@tonic-gate {
1277c478bd9Sstevel@tonic-gate int offset[12]; /* used to line the header up correctly */
1287c478bd9Sstevel@tonic-gate char buf[512];
1297c478bd9Sstevel@tonic-gate
1307c478bd9Sstevel@tonic-gate (void) sprintf(buf,
1317c478bd9Sstevel@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",
1327c478bd9Sstevel@tonic-gate s->as_generated, &(offset[0]),
1337c478bd9Sstevel@tonic-gate s->as_nonattrib, &(offset[1]),
1347c478bd9Sstevel@tonic-gate s->as_kernel, &(offset[2]),
1357c478bd9Sstevel@tonic-gate s->as_audit, &(offset[3]),
1367c478bd9Sstevel@tonic-gate s->as_auditctl, &(offset[4]),
1377c478bd9Sstevel@tonic-gate s->as_enqueue, &(offset[5]),
1387c478bd9Sstevel@tonic-gate s->as_written, &(offset[6]),
1397c478bd9Sstevel@tonic-gate s->as_wblocked, &(offset[7]),
1407c478bd9Sstevel@tonic-gate s->as_rblocked, &(offset[8]),
1417c478bd9Sstevel@tonic-gate s->as_dropped, &(offset[9]),
1427c478bd9Sstevel@tonic-gate s->as_totalsize / ONEK, &(offset[10]),
1437c478bd9Sstevel@tonic-gate s->as_memused / ONEK, &(offset[11]));
1447c478bd9Sstevel@tonic-gate
1457c478bd9Sstevel@tonic-gate /* print a properly aligned header every HEADER_MOD lines */
14626fd7700SKrishnendu Sadhukhan - Sun Microsystems if (header_mod && (!cnt || ((timestamp_fmt != NODATE) ?
14726fd7700SKrishnendu Sadhukhan - Sun Microsystems !(cnt % (header_mod / 2)) : !(cnt % header_mod)))) {
1487c478bd9Sstevel@tonic-gate (void) printf(
1497c478bd9Sstevel@tonic-gate "%*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s\n",
1507c478bd9Sstevel@tonic-gate offset[0] - 1, "gen",
1517c478bd9Sstevel@tonic-gate offset[1] - offset[0] - 1, "nona",
1527c478bd9Sstevel@tonic-gate offset[2] - offset[1] - 1, "kern",
1537c478bd9Sstevel@tonic-gate offset[3] - offset[2] - 1, "aud",
1547c478bd9Sstevel@tonic-gate offset[4] - offset[3] - 1, "ctl",
1557c478bd9Sstevel@tonic-gate offset[5] - offset[4] - 1, "enq",
1567c478bd9Sstevel@tonic-gate offset[6] - offset[5] - 1, "wrtn",
1577c478bd9Sstevel@tonic-gate offset[7] - offset[6] - 1, "wblk",
1587c478bd9Sstevel@tonic-gate offset[8] - offset[7] - 1, "rblk",
1597c478bd9Sstevel@tonic-gate offset[9] - offset[8] - 1, "drop",
1607c478bd9Sstevel@tonic-gate offset[10] - offset[9] - 1, "tot",
1617c478bd9Sstevel@tonic-gate offset[11] - offset[10], "mem");
1627c478bd9Sstevel@tonic-gate }
1637c478bd9Sstevel@tonic-gate
1647c478bd9Sstevel@tonic-gate (void) puts(buf);
1657c478bd9Sstevel@tonic-gate }
1667c478bd9Sstevel@tonic-gate
1677c478bd9Sstevel@tonic-gate
1687c478bd9Sstevel@tonic-gate static void
eauditon(cmd,data,length)1697c478bd9Sstevel@tonic-gate eauditon(cmd, data, length)
1707c478bd9Sstevel@tonic-gate int cmd;
1717c478bd9Sstevel@tonic-gate caddr_t data;
1727c478bd9Sstevel@tonic-gate int length;
1737c478bd9Sstevel@tonic-gate {
1747c478bd9Sstevel@tonic-gate if (auditon(cmd, data, length) == -1) {
1757c478bd9Sstevel@tonic-gate perror("auditstat: auditon");
1767c478bd9Sstevel@tonic-gate exit(1);
1777c478bd9Sstevel@tonic-gate }
1787c478bd9Sstevel@tonic-gate }
1797c478bd9Sstevel@tonic-gate
1807c478bd9Sstevel@tonic-gate
1817c478bd9Sstevel@tonic-gate static void
parse_args(argc,argv)1827c478bd9Sstevel@tonic-gate parse_args(argc, argv)
1837c478bd9Sstevel@tonic-gate int argc;
1847c478bd9Sstevel@tonic-gate char **argv;
1857c478bd9Sstevel@tonic-gate {
1867c478bd9Sstevel@tonic-gate int c;
1877c478bd9Sstevel@tonic-gate
18826fd7700SKrishnendu Sadhukhan - Sun Microsystems while ((c = getopt(argc, argv, "c:h:i:vnT:")) != -1) {
1897c478bd9Sstevel@tonic-gate switch (c) {
1907c478bd9Sstevel@tonic-gate case 'c':
1917c478bd9Sstevel@tonic-gate if (flags & CFLG)
1927c478bd9Sstevel@tonic-gate usage_exit();
1937c478bd9Sstevel@tonic-gate flags |= CFLG;
1947c478bd9Sstevel@tonic-gate if (strisdigit(optarg)) {
1957c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
1967c478bd9Sstevel@tonic-gate "auditstat: invalid count specified.\n");
1977c478bd9Sstevel@tonic-gate exit(1);
1987c478bd9Sstevel@tonic-gate }
1997c478bd9Sstevel@tonic-gate count = atoi(optarg);
2007c478bd9Sstevel@tonic-gate break;
2017c478bd9Sstevel@tonic-gate case 'h':
2027c478bd9Sstevel@tonic-gate if (flags & HFLG)
2037c478bd9Sstevel@tonic-gate usage_exit();
2047c478bd9Sstevel@tonic-gate flags |= HFLG;
2057c478bd9Sstevel@tonic-gate if (strisdigit(optarg)) {
2067c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
2077c478bd9Sstevel@tonic-gate "auditstat: invalid header arg specified.\n");
2087c478bd9Sstevel@tonic-gate exit(1);
2097c478bd9Sstevel@tonic-gate }
2107c478bd9Sstevel@tonic-gate header_mod = atoi(optarg);
2117c478bd9Sstevel@tonic-gate break;
2127c478bd9Sstevel@tonic-gate case 'i':
2137c478bd9Sstevel@tonic-gate if (flags & IFLG)
2147c478bd9Sstevel@tonic-gate usage_exit();
2157c478bd9Sstevel@tonic-gate flags |= IFLG;
2167c478bd9Sstevel@tonic-gate if (strisdigit(optarg)) {
2177c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
2187c478bd9Sstevel@tonic-gate "auditstat: invalid interval specified.\n");
2197c478bd9Sstevel@tonic-gate exit(1);
2207c478bd9Sstevel@tonic-gate }
2217c478bd9Sstevel@tonic-gate interval = atoi(optarg);
2227c478bd9Sstevel@tonic-gate break;
2237c478bd9Sstevel@tonic-gate case 'n':
2247c478bd9Sstevel@tonic-gate if (flags & NFLG)
2257c478bd9Sstevel@tonic-gate usage_exit();
2267c478bd9Sstevel@tonic-gate flags |= NFLG;
2277c478bd9Sstevel@tonic-gate break;
2287c478bd9Sstevel@tonic-gate case 'v':
2297c478bd9Sstevel@tonic-gate if (flags & VFLG)
2307c478bd9Sstevel@tonic-gate usage_exit();
2317c478bd9Sstevel@tonic-gate flags |= VFLG;
2327c478bd9Sstevel@tonic-gate break;
23326fd7700SKrishnendu Sadhukhan - Sun Microsystems case 'T':
23426fd7700SKrishnendu Sadhukhan - Sun Microsystems if (optarg) {
23526fd7700SKrishnendu Sadhukhan - Sun Microsystems if (*optarg == 'u')
23626fd7700SKrishnendu Sadhukhan - Sun Microsystems timestamp_fmt = UDATE;
23726fd7700SKrishnendu Sadhukhan - Sun Microsystems else if (*optarg == 'd')
23826fd7700SKrishnendu Sadhukhan - Sun Microsystems timestamp_fmt = DDATE;
23926fd7700SKrishnendu Sadhukhan - Sun Microsystems else
24026fd7700SKrishnendu Sadhukhan - Sun Microsystems usage_exit();
24126fd7700SKrishnendu Sadhukhan - Sun Microsystems } else {
24226fd7700SKrishnendu Sadhukhan - Sun Microsystems usage_exit();
24326fd7700SKrishnendu Sadhukhan - Sun Microsystems }
24426fd7700SKrishnendu Sadhukhan - Sun Microsystems break;
2457c478bd9Sstevel@tonic-gate case '?':
2467c478bd9Sstevel@tonic-gate default:
2477c478bd9Sstevel@tonic-gate usage_exit();
2487c478bd9Sstevel@tonic-gate break;
2497c478bd9Sstevel@tonic-gate }
2507c478bd9Sstevel@tonic-gate }
2517c478bd9Sstevel@tonic-gate }
2527c478bd9Sstevel@tonic-gate
2537c478bd9Sstevel@tonic-gate
2547c478bd9Sstevel@tonic-gate static void
usage_exit()2557c478bd9Sstevel@tonic-gate usage_exit()
2567c478bd9Sstevel@tonic-gate {
2577c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
258d75d0dc9Stz204579 "auditstat: usage: auditstat [-c count] [-h lines] "
25926fd7700SKrishnendu Sadhukhan - Sun Microsystems "[-T d|u] [-i interval] [-n] [-v]\n");
2607c478bd9Sstevel@tonic-gate exit(1);
2617c478bd9Sstevel@tonic-gate }
2627c478bd9Sstevel@tonic-gate
2637c478bd9Sstevel@tonic-gate
2647c478bd9Sstevel@tonic-gate static int
strisdigit(s)2657c478bd9Sstevel@tonic-gate strisdigit(s)
2667c478bd9Sstevel@tonic-gate char *s;
2677c478bd9Sstevel@tonic-gate {
2687c478bd9Sstevel@tonic-gate for (; *s; s++)
2697c478bd9Sstevel@tonic-gate if (!isdigit(*s))
2707c478bd9Sstevel@tonic-gate return (1);
2717c478bd9Sstevel@tonic-gate
2727c478bd9Sstevel@tonic-gate return (0);
2737c478bd9Sstevel@tonic-gate }
274