1 /*
2 * Common definitions for Linux and XFS quota tests.
3 *
4 * Copyright (c) 2016 Eugene Syromyatnikov <evgsyr@gmail.com>
5 * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
6 * Copyright (c) 2016-2018 The strace developers.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #ifndef STRACE_TESTS_QUOTACTL_H
33 #define STRACE_TESTS_QUOTACTL_H
34
35 # include <inttypes.h>
36 # include <stdarg.h>
37 # include <stdio.h>
38 # include "print_fields.h"
39
40 # ifdef HAVE_LINUX_QUOTA_H
41 /* Broken in CentOS 5: has extern spinlock_t dq_data_lock; declaration */
42 # include <linux/quota.h>
43 # else
44 # include <linux/types.h>
45 /* Broken in some new glibc versions: have Q_GETNEXTQUOTA definition but no
46 * struct nextdqblk defined. Fixed in glibc-2.24-106-g4d72808. */
47 # include <sys/quota.h>
48 # endif
49
50 # ifndef QCMD_CMD
51 # define QCMD_CMD(_val) ((unsigned) (_val) >> SUBCMDSHIFT)
52 # endif /* !QCMD_CMD */
53
54 # ifndef QCMD_TYPE
55 # define QCMD_TYPE(_val) ((unsigned) (_val) & SUBCMDMASK)
56 # endif /* !QCMD_TYPE */
57
58 # ifndef PRJQUOTA
59 # define PRJQUOTA 2
60 # endif
61
62 typedef void (*print_cb)(long rc, void *addr, void *arg);
63
64 enum check_quotactl_flag_bits {
65 CQF_ID_SKIP_BIT,
66 CQF_ID_STR_BIT,
67 CQF_ADDR_SKIP_BIT,
68 CQF_ADDR_STR_BIT,
69 CQF_ADDR_CB_BIT,
70 };
71
72 enum check_quotactl_flags {
73 CQF_NONE,
74 CQF_ID_SKIP = 1 << CQF_ID_SKIP_BIT,
75 CQF_ID_STR = 1 << CQF_ID_STR_BIT,
76 CQF_ADDR_SKIP = 1 << CQF_ADDR_SKIP_BIT,
77 CQF_ADDR_STR = 1 << CQF_ADDR_STR_BIT,
78 CQF_ADDR_CB = 1 << CQF_ADDR_CB_BIT,
79 };
80
81 static const char *errstr;
82
83 /**
84 * Generic quotactl syscall checker function. Call convention:
85 *
86 * check_quota(flags, cmd, cmd_str, special, special_str
87 * [, id [, id_str]]
88 * [, addr [, { addr_cb, addr_cb_arg | addr_str }]])
89 *
90 * check_quota performs a syscall invocation and prints the expected output
91 * for it.
92 *
93 * It might be useful to employ ARG_STR macro for passing cmd/cmd_str,
94 * special_special_str, id/id_str, and addr/addr_str argument pairs.
95 *
96 * @param flags Check flags:
97 * - CQF_ID_SKIP: the "id" syscall argument is ignored
98 * in the syscall invocation. No id and id_str arguments
99 * should be provided if this flag is set.
100 * This flag has priority over the CQF_ID_STR flag.
101 * - CQF_ID_STR: the "id" syscall argument has a special string
102 * representation. id_str argument should be provided if this
103 * flag is set; no id_str argument should be provided and id
104 * argument is printed as unsigned integer (with an exception
105 * for -1, which is printed as signed) if this flag is not set.
106 * - CQF_ADDR_SKIP: the "addr" syscall argument is ignored
107 * in the syscall invocation. None of the addr, addr_cb,
108 * addr_cb_arg, and/or addr_str arguments should be provided
109 * if this flag is set. This flag has priority
110 * over the CQF_ADDR_STR and CQF_ADDR_CB flags.
111 * - CQF_ADDR_CB: the "addr" syscall argument printing is handled
112 * via a callback function. addr_cb (that points to a callback
113 * function of type print_cb) and addr_cb_arg (an opaque pointer
114 * that is passed to addr_cb in the third argument) should
115 * be provided if this flag is set.
116 * This flag has priority over the CQF_ADDR_STR flag.
117 * - CQF_ADDR_STR: addr syscall argument has a special string
118 * representation. addr_str argument should be provided if this
119 * flag is set. If both CQF_ADDR_CB and CQF_ADDR_STR flags
120 * are not set, addr syscall argument is printed using "%p"
121 * printf format.
122 * @param cmd Value of the "cmd" syscall argument that should be passed
123 * in the syscall invocation.
124 * @param cmd_str String representation of the "cmd" syscall argument.
125 * @param special Value of the "special" syscall argument that should be passed
126 * in the syscall invocation.
127 * @param special_str String representation of the "special" syscall argument.
128 * @param ... Additional arguments depend on the flags being set:
129 * - id: Value of the "id" syscall argument. Provided
130 * if CQF_ID_SKIP is not set, otherwise -1 is passed
131 * in the syscall invocation and the argument is not printed
132 * in the expected output.
133 * - id_str: String representation of the "id" syscall argument.
134 * Provided if CQF_ID_SKIP is not set and CQF_ID_STR is set.
135 * - addr: Value of the "addr" syscall argument. Provided
136 * if CQF_ADDR_SKIP is not set, otherwise NULL is passed
137 * in the syscall invocation and the argument is not printed
138 * in the expected output.
139 * - addr_cb: Callback function that is called for the "addr"
140 * syscall argument printing. Should be of print_cb type.
141 * Provided if CQF_ADDR_SKIP is not set and CQF_ADDR_CB is set.
142 * - addr_cb_arg: Opaque pointer that is passed to addr_cb,
143 * Provided if CQF_ADDR_SKIP is not set and CQF_ADDR_CB is set.
144 * - addr_str: String representation of the "addr" syscall argument.
145 * Provided if CQF_ADDR_SKIP is not set, CQF_ADDR_CB is not set,
146 * and CQF_ADDR_STR is set.
147 */
148 static inline void
check_quota(uint32_t flags,int cmd,const char * cmd_str,const char * special,const char * special_str,...)149 check_quota(uint32_t flags, int cmd, const char *cmd_str,
150 const char *special, const char *special_str, ...)
151 {
152 long rc;
153 const char *addr_str = NULL;
154 const char *id_str = NULL;
155 void *addr = NULL;
156 print_cb addr_cb = NULL;
157 void *addr_cb_arg = NULL;
158 uint32_t id = -1;
159
160 va_list ap;
161
162 va_start(ap, special_str);
163
164 if (!(flags & CQF_ID_SKIP)) {
165 id = va_arg(ap, uint32_t);
166
167 if (flags & CQF_ID_STR)
168 id_str = va_arg(ap, const char *);
169 }
170
171 if (!(flags & CQF_ADDR_SKIP)) {
172 addr = va_arg(ap, void *);
173
174 if (flags & CQF_ADDR_CB) {
175 addr_cb = va_arg(ap, print_cb);
176 addr_cb_arg = va_arg(ap, void *);
177 } else if (flags & CQF_ADDR_STR) {
178 addr_str = va_arg(ap, const char *);
179 }
180 }
181
182 va_end(ap);
183
184 rc = syscall(__NR_quotactl, cmd, special, id, addr);
185
186 errstr = sprintrc(rc);
187
188 #ifdef INJECT_RETVAL
189 if (rc != INJECT_RETVAL)
190 error_msg_and_fail("Got a return value of %ld != %d",
191 rc, INJECT_RETVAL);
192
193 static char inj_errstr[4096];
194
195 snprintf(inj_errstr, sizeof(inj_errstr), "%s (INJECTED)", errstr);
196 errstr = inj_errstr;
197 #endif
198
199 printf("quotactl(%s, %s", cmd_str, special_str);
200
201 if (!(flags & CQF_ID_SKIP)) {
202 if (flags & CQF_ID_STR) {
203 printf(", %s", id_str);
204 } else {
205 if (id == (uint32_t)-1)
206 printf(", -1");
207 else
208 printf(", %u", id);
209 }
210 }
211
212 if (!(flags & CQF_ADDR_SKIP)) {
213 if (flags & CQF_ADDR_CB) {
214 printf(", ");
215 addr_cb(rc, addr, addr_cb_arg);
216 } else if (flags & CQF_ADDR_STR) {
217 printf(", %s", addr_str);
218 } else {
219 printf(", %p", addr);
220 }
221 }
222
223 printf(") = %s\n", errstr);
224 }
225
226
227 static const int bogus_cmd = 0xbadc0ded;
228 static const int bogus_id = 0xca7faced;
229
230 /* It is invalid anyway due to the slash in the end */
231 static const char *bogus_dev = "/dev/bogus/";
232 static const char *bogus_dev_str = "\"/dev/bogus/\"";
233
234 static const char unterminated_data[] = { '\1', '\2', '\3' };
235
236 #endif /* !STRACE_TESTS_QUOTACTL_H */
237