1156886Srwatson/*-
2156886Srwatson * Copyright (c) 2006 Robert N. M. Watson
3156886Srwatson * All rights reserved.
4156886Srwatson *
5156886Srwatson * This software was developed by Robert Watson for the TrustedBSD Project.
6156886Srwatson *
7156886Srwatson * Redistribution and use in source and binary forms, with or without
8156886Srwatson * modification, are permitted provided that the following conditions
9156886Srwatson * are met:
10156886Srwatson * 1. Redistributions of source code must retain the above copyright
11156886Srwatson *    notice, this list of conditions and the following disclaimer.
12156886Srwatson * 2. Redistributions in binary form must reproduce the above copyright
13156886Srwatson *    notice, this list of conditions and the following disclaimer in the
14156886Srwatson *    documentation and/or other materials provided with the distribution.
15156886Srwatson *
16156886Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17156886Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18156886Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19156886Srwatson * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20156886Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21156886Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22156886Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23156886Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24156886Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25156886Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26156886Srwatson * SUCH DAMAGE.
27156886Srwatson *
28156886Srwatson * $FreeBSD$
29156886Srwatson */
30156886Srwatson
31156886Srwatson/*
32156886Srwatson * Simple audit pipe regression test to confirm that the ioctls for queue
33156886Srwatson * limit information basically work.  No attempt is made to validate the
34156886Srwatson * queue length returned, however.
35156886Srwatson */
36156886Srwatson
37156886Srwatson#include <sys/types.h>
38156886Srwatson#include <sys/ioctl.h>
39156886Srwatson
40156886Srwatson#include <security/audit/audit_ioctl.h>
41156886Srwatson
42156886Srwatson#include <err.h>
43156886Srwatson#include <fcntl.h>
44156886Srwatson
45156886Srwatsonint
46156886Srwatsonmain(int argc, char *argv[])
47156886Srwatson{
48156886Srwatson	u_int len, minlen, maxlen;
49156886Srwatson	u_int64_t astat;
50156886Srwatson	int fd;
51156886Srwatson
52156886Srwatson	fd = open("/dev/auditpipe", O_RDONLY);
53156886Srwatson	if (fd < 0)
54156886Srwatson		err(-1, "/dev/auditpipe");
55156886Srwatson
56156886Srwatson	/*
57156886Srwatson	 * First, test that we can read the queue length, queue limit, and
58156886Srwatson	 * bounds on queue length limits.
59156886Srwatson	 */
60156886Srwatson	len = (u_int)(-1);
61156886Srwatson	if (ioctl(fd, AUDITPIPE_GET_QLEN, &len) < 0)
62156886Srwatson		err(-1, "AUDITPIPE_GET_QLEN");
63156886Srwatson	if (len == (u_int)(-1))
64156886Srwatson		errx(-1, "AUDITPIPE_GET_QLEN: unchanged");
65156886Srwatson
66156886Srwatson	minlen = (u_int)(-1);
67156886Srwatson	if (ioctl(fd, AUDITPIPE_GET_QLIMIT_MIN, &minlen) < 0)
68156886Srwatson		err(-1, "AUDITPIPE_GET_QLIMIT_MIN");
69156886Srwatson	if (minlen == (u_int)(-1))
70156886Srwatson		errx(-1, "AUDITPIPE_GET_QLIMIT_MIN: unchanged");
71156886Srwatson
72156886Srwatson	maxlen = (u_int)(-1);
73156886Srwatson	if (ioctl(fd, AUDITPIPE_GET_QLIMIT_MAX, &maxlen) < 0)
74156886Srwatson		err(-1, "AUDITPIPE_GET_QLIMIT_MAX");
75156886Srwatson	if (maxlen == (u_int)(-1))
76156886Srwatson		errx(-1, "AUDITPIPE_GET_QLIMIT_MAX: unchanged");
77156886Srwatson
78156886Srwatson	len = (u_int)(-1);
79156886Srwatson	if (ioctl(fd, AUDITPIPE_GET_QLIMIT, &len) < 0)
80156886Srwatson		err(-1, "AUDITPIPE_GET_QLIMIT");
81156886Srwatson	if (len == (u_int)(-1))
82156886Srwatson		errx(-1, "AUDITPIPE_GET_QLIMIT: unchanged");
83156886Srwatson
84156886Srwatson	if (!(len >= minlen))
85156886Srwatson		errx(-1, "queue length < minlen");
86156886Srwatson
87156886Srwatson	if (!(len <= maxlen))
88156886Srwatson		errx(-1, "queue length > maxlen");
89156886Srwatson
90156886Srwatson	/*
91156886Srwatson	 * Try setting the queue length to first minimum, then maximum
92156886Srwatson	 * lengths.  Query after each to make sure it changed.
93156886Srwatson	 */
94156886Srwatson	len = minlen;
95156886Srwatson	if (ioctl(fd, AUDITPIPE_SET_QLIMIT, &len) < 0)
96156886Srwatson		err(-1, "AUDITPIPE_SET_QLIMIT(min)");
97156886Srwatson
98156886Srwatson	if (ioctl(fd, AUDITPIPE_GET_QLIMIT, &len) < 0)
99156886Srwatson		err(-1, "AUDITPIPE_GET_QLIMIT");
100156886Srwatson
101156886Srwatson	if (len != minlen)
102156886Srwatson		errx(-1, "set to minlen didn't work");
103156886Srwatson
104156886Srwatson	len = maxlen;
105156886Srwatson	if (ioctl(fd, AUDITPIPE_SET_QLIMIT, &len) < 0)
106156886Srwatson		err(-1, "AUDITPIPE_SET_QLIMIT(max)");
107156886Srwatson
108156886Srwatson	if (ioctl(fd, AUDITPIPE_GET_QLIMIT, &len) < 0)
109156886Srwatson		err(-1, "AUDITPIPE_GETQLIMIT");
110156886Srwatson
111156886Srwatson	if (len != maxlen)
112156886Srwatson		errx(-1, "set to maxlen didn't work");
113156886Srwatson
114156886Srwatson	/*
115156886Srwatson	 * Check that we can query the defined stats.  No attempt to
116156886Srwatson	 * validate.
117156886Srwatson	 */
118156886Srwatson	astat = (u_int64_t)(int64_t)(-1);
119156886Srwatson	if (ioctl(fd, AUDITPIPE_GET_INSERTS, &astat) < 0)
120156886Srwatson		err(-1, "AUDITPIPE_GET_INSERTS");
121156886Srwatson	if (astat == (u_int64_t)(int64_t)(-1))
122156886Srwatson		errx(-1, "AUDITPIPE_GET_INSERTS: unchanged");
123156886Srwatson
124156886Srwatson	astat = (u_int64_t)(int64_t)(-1);
125156886Srwatson	if (ioctl(fd, AUDITPIPE_GET_READS, &astat) < 0)
126156886Srwatson		err(-1, "AUDITPIPE_GET_READS");
127156886Srwatson	if (astat == (u_int64_t)(int64_t)(-1))
128156886Srwatson		errx(-1, "AUDITPIPE_GET_READS: unchanged");
129156886Srwatson
130156886Srwatson	astat = (u_int64_t)(int64_t)(-1);
131156886Srwatson	if (ioctl(fd, AUDITPIPE_GET_DROPS, &astat) < 0)
132156886Srwatson		err(-1, "AUDITPIPE_GET_DROPS");
133156886Srwatson	if (astat == (u_int64_t)(int64_t)(-1))
134156886Srwatson		errx(-1, "AUDITPIPE_GET_DROPS: unchanged");
135156886Srwatson
136156886Srwatson	astat = (u_int64_t)(int64_t)(-1);
137156886Srwatson	if (ioctl(fd, AUDITPIPE_GET_TRUNCATES, &astat) < 0)
138156886Srwatson		err(-1, "AUDITPIPE_GET_TRUNCATES");
139156886Srwatson	if (astat == (u_int64_t)(int64_t)(-1))
140156886Srwatson		errx(-1, "AUDITPIPE_GET_TRUNCATES: unchanged");
141156886Srwatson
142156886Srwatson	return (0);
143156886Srwatson}
144