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  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #ifndef STRACE_TESTS_QUOTACTL_H
32 #define STRACE_TESTS_QUOTACTL_H
33 
34 # include <inttypes.h>
35 # include <stdarg.h>
36 # include <stdio.h>
37 
38 # ifdef HAVE_LINUX_QUOTA_H
39 /* Broken in CentOS 5: has extern spinlock_t dq_data_lock; declaration */
40 #  include <linux/quota.h>
41 # else
42 #  include <linux/types.h>
43 /* Broken in some new glibc versions: have Q_GETNEXTQUOTA definition but no
44  * struct nextdqblk defined. Fixed in glibc-2.24-106-g4d72808. */
45 #  include <sys/quota.h>
46 # endif
47 
48 # ifndef QCMD_CMD
49 #  define QCMD_CMD(_val) ((unsigned) (_val) >> SUBCMDSHIFT)
50 # endif /* !QCMD_CMD */
51 
52 # ifndef QCMD_TYPE
53 #  define QCMD_TYPE(_val) ((unsigned) (_val) & SUBCMDMASK)
54 # endif /* !QCMD_TYPE */
55 
56 # ifndef PRJQUOTA
57 #  define PRJQUOTA 2
58 # endif
59 
60 # define PRINT_FIELD_D(prefix, where, field)	\
61 	printf("%s%s=%lld", (prefix), #field,	\
62 	       sign_extend_unsigned_to_ll((where)->field))
63 
64 # define PRINT_FIELD_U(prefix, where, field)	\
65 	printf("%s%s=%llu", (prefix), #field,	\
66 	       zero_extend_signed_to_ull((where)->field))
67 
68 # define PRINT_FIELD_X(prefix, where, field)	\
69 	printf("%s%s=%#llx", (prefix), #field,	\
70 	       zero_extend_signed_to_ull((where)->field))
71 
72 typedef void (*print_cb)(long rc, void *addr, void *arg);
73 
74 enum check_quotactl_flag_bits {
75 	CQF_ID_SKIP_BIT,
76 	CQF_ID_STR_BIT,
77 	CQF_ADDR_SKIP_BIT,
78 	CQF_ADDR_STR_BIT,
79 	CQF_ADDR_CB_BIT,
80 };
81 
82 enum check_quotactl_flags {
83 	CQF_NONE,
84 	CQF_ID_SKIP   = 1 << CQF_ID_SKIP_BIT,
85 	CQF_ID_STR    = 1 << CQF_ID_STR_BIT,
86 	CQF_ADDR_SKIP = 1 << CQF_ADDR_SKIP_BIT,
87 	CQF_ADDR_STR  = 1 << CQF_ADDR_STR_BIT,
88 	CQF_ADDR_CB   = 1 << CQF_ADDR_CB_BIT,
89 };
90 
91 
92 static inline void
check_quota(uint32_t flags,int cmd,const char * cmd_str,const char * special,const char * special_str,...)93 check_quota(uint32_t flags, int cmd, const char *cmd_str,
94 	const char *special, const char *special_str, ...)
95 {
96 	long rc;
97 	const char *addr_str = NULL;
98 	const char *id_str = NULL;
99 	void *addr = NULL;
100 	print_cb addr_cb = NULL;
101 	void *addr_cb_arg = NULL;
102 	uint32_t id = -1;
103 
104 	va_list ap;
105 
106 	va_start(ap, special_str);
107 
108 	if (!(flags & CQF_ID_SKIP)) {
109 		id = va_arg(ap, uint32_t);
110 
111 		if (flags & CQF_ID_STR)
112 			id_str = va_arg(ap, const char *);
113 	}
114 
115 	if (!(flags & CQF_ADDR_SKIP)) {
116 		addr = va_arg(ap, void *);
117 
118 		if (flags & CQF_ADDR_CB) {
119 			addr_cb = va_arg(ap, print_cb);
120 			addr_cb_arg = va_arg(ap, void *);
121 		} else if (flags & CQF_ADDR_STR) {
122 			addr_str = va_arg(ap, const char *);
123 		}
124 	}
125 
126 	va_end(ap);
127 
128 	rc = syscall(__NR_quotactl, cmd, special, id, addr);
129 	printf("quotactl(%s, %s", cmd_str, special_str);
130 
131 	if (!(flags & CQF_ID_SKIP)) {
132 		if (flags & CQF_ID_STR) {
133 			printf(", %s", id_str);
134 		} else {
135 			if (id == (uint32_t)-1)
136 				printf(", -1");
137 			else
138 				printf(", %u", id);
139 		}
140 	}
141 
142 	if (!(flags & CQF_ADDR_SKIP)) {
143 		if (flags & CQF_ADDR_CB) {
144 			printf(", ");
145 			addr_cb(rc, addr, addr_cb_arg);
146 		} else if (flags & CQF_ADDR_STR) {
147 			printf(", %s", addr_str);
148 		} else {
149 			printf(", %p", addr);
150 		}
151 	}
152 
153 	printf(") = %s\n", sprintrc(rc));
154 }
155 
156 
157 static const int bogus_cmd = 0xbadc0ded;
158 static const int bogus_id = 0xca7faced;
159 
160 /* It is invalid anyway due to the slash in the end */
161 static const char *bogus_dev = "/dev/bogus/";
162 static const char *bogus_dev_str = "\"/dev/bogus/\"";
163 
164 static const char unterminated_data[] = { '\1', '\2', '\3' };
165 
166 #endif /* !STRACE_TESTS_QUOTACTL_H */
167