xref: /freebsd/tools/regression/audit/audit_pipe_ioctl/audit_pipe_ioctl.c (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
1 /*-
2  * Copyright (c) 2006 Robert N. M. Watson
3  * All rights reserved.
4  *
5  * This software was developed by Robert Watson for the TrustedBSD Project.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 /*
30  * Simple audit pipe regression test to confirm that the ioctls for queue
31  * limit information basically work.  No attempt is made to validate the
32  * queue length returned, however.
33  */
34 
35 #include <sys/types.h>
36 #include <sys/ioctl.h>
37 
38 #include <security/audit/audit_ioctl.h>
39 
40 #include <err.h>
41 #include <fcntl.h>
42 
43 int
44 main(int argc, char *argv[])
45 {
46 	u_int len, minlen, maxlen;
47 	u_int64_t astat;
48 	int fd;
49 
50 	fd = open("/dev/auditpipe", O_RDONLY);
51 	if (fd < 0)
52 		err(-1, "/dev/auditpipe");
53 
54 	/*
55 	 * First, test that we can read the queue length, queue limit, and
56 	 * bounds on queue length limits.
57 	 */
58 	len = (u_int)(-1);
59 	if (ioctl(fd, AUDITPIPE_GET_QLEN, &len) < 0)
60 		err(-1, "AUDITPIPE_GET_QLEN");
61 	if (len == (u_int)(-1))
62 		errx(-1, "AUDITPIPE_GET_QLEN: unchanged");
63 
64 	minlen = (u_int)(-1);
65 	if (ioctl(fd, AUDITPIPE_GET_QLIMIT_MIN, &minlen) < 0)
66 		err(-1, "AUDITPIPE_GET_QLIMIT_MIN");
67 	if (minlen == (u_int)(-1))
68 		errx(-1, "AUDITPIPE_GET_QLIMIT_MIN: unchanged");
69 
70 	maxlen = (u_int)(-1);
71 	if (ioctl(fd, AUDITPIPE_GET_QLIMIT_MAX, &maxlen) < 0)
72 		err(-1, "AUDITPIPE_GET_QLIMIT_MAX");
73 	if (maxlen == (u_int)(-1))
74 		errx(-1, "AUDITPIPE_GET_QLIMIT_MAX: unchanged");
75 
76 	len = (u_int)(-1);
77 	if (ioctl(fd, AUDITPIPE_GET_QLIMIT, &len) < 0)
78 		err(-1, "AUDITPIPE_GET_QLIMIT");
79 	if (len == (u_int)(-1))
80 		errx(-1, "AUDITPIPE_GET_QLIMIT: unchanged");
81 
82 	if (!(len >= minlen))
83 		errx(-1, "queue length < minlen");
84 
85 	if (!(len <= maxlen))
86 		errx(-1, "queue length > maxlen");
87 
88 	/*
89 	 * Try setting the queue length to first minimum, then maximum
90 	 * lengths.  Query after each to make sure it changed.
91 	 */
92 	len = minlen;
93 	if (ioctl(fd, AUDITPIPE_SET_QLIMIT, &len) < 0)
94 		err(-1, "AUDITPIPE_SET_QLIMIT(min)");
95 
96 	if (ioctl(fd, AUDITPIPE_GET_QLIMIT, &len) < 0)
97 		err(-1, "AUDITPIPE_GET_QLIMIT");
98 
99 	if (len != minlen)
100 		errx(-1, "set to minlen didn't work");
101 
102 	len = maxlen;
103 	if (ioctl(fd, AUDITPIPE_SET_QLIMIT, &len) < 0)
104 		err(-1, "AUDITPIPE_SET_QLIMIT(max)");
105 
106 	if (ioctl(fd, AUDITPIPE_GET_QLIMIT, &len) < 0)
107 		err(-1, "AUDITPIPE_GETQLIMIT");
108 
109 	if (len != maxlen)
110 		errx(-1, "set to maxlen didn't work");
111 
112 	/*
113 	 * Check that we can query the defined stats.  No attempt to
114 	 * validate.
115 	 */
116 	astat = (u_int64_t)(int64_t)(-1);
117 	if (ioctl(fd, AUDITPIPE_GET_INSERTS, &astat) < 0)
118 		err(-1, "AUDITPIPE_GET_INSERTS");
119 	if (astat == (u_int64_t)(int64_t)(-1))
120 		errx(-1, "AUDITPIPE_GET_INSERTS: unchanged");
121 
122 	astat = (u_int64_t)(int64_t)(-1);
123 	if (ioctl(fd, AUDITPIPE_GET_READS, &astat) < 0)
124 		err(-1, "AUDITPIPE_GET_READS");
125 	if (astat == (u_int64_t)(int64_t)(-1))
126 		errx(-1, "AUDITPIPE_GET_READS: unchanged");
127 
128 	astat = (u_int64_t)(int64_t)(-1);
129 	if (ioctl(fd, AUDITPIPE_GET_DROPS, &astat) < 0)
130 		err(-1, "AUDITPIPE_GET_DROPS");
131 	if (astat == (u_int64_t)(int64_t)(-1))
132 		errx(-1, "AUDITPIPE_GET_DROPS: unchanged");
133 
134 	astat = (u_int64_t)(int64_t)(-1);
135 	if (ioctl(fd, AUDITPIPE_GET_TRUNCATES, &astat) < 0)
136 		err(-1, "AUDITPIPE_GET_TRUNCATES");
137 	if (astat == (u_int64_t)(int64_t)(-1))
138 		errx(-1, "AUDITPIPE_GET_TRUNCATES: unchanged");
139 
140 	return (0);
141 }
142