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-2017 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 
82 static inline void
83 check_quota(uint32_t flags, int cmd, const char *cmd_str,
84 	const char *special, const char *special_str, ...)
85 {
86 	long rc;
87 	const char *addr_str = NULL;
88 	const char *id_str = NULL;
89 	void *addr = NULL;
90 	print_cb addr_cb = NULL;
91 	void *addr_cb_arg = NULL;
92 	uint32_t id = -1;
93 
94 	va_list ap;
95 
96 	va_start(ap, special_str);
97 
98 	if (!(flags & CQF_ID_SKIP)) {
99 		id = va_arg(ap, uint32_t);
100 
101 		if (flags & CQF_ID_STR)
102 			id_str = va_arg(ap, const char *);
103 	}
104 
105 	if (!(flags & CQF_ADDR_SKIP)) {
106 		addr = va_arg(ap, void *);
107 
108 		if (flags & CQF_ADDR_CB) {
109 			addr_cb = va_arg(ap, print_cb);
110 			addr_cb_arg = va_arg(ap, void *);
111 		} else if (flags & CQF_ADDR_STR) {
112 			addr_str = va_arg(ap, const char *);
113 		}
114 	}
115 
116 	va_end(ap);
117 
118 	rc = syscall(__NR_quotactl, cmd, special, id, addr);
119 	printf("quotactl(%s, %s", cmd_str, special_str);
120 
121 	if (!(flags & CQF_ID_SKIP)) {
122 		if (flags & CQF_ID_STR) {
123 			printf(", %s", id_str);
124 		} else {
125 			if (id == (uint32_t)-1)
126 				printf(", -1");
127 			else
128 				printf(", %u", id);
129 		}
130 	}
131 
132 	if (!(flags & CQF_ADDR_SKIP)) {
133 		if (flags & CQF_ADDR_CB) {
134 			printf(", ");
135 			addr_cb(rc, addr, addr_cb_arg);
136 		} else if (flags & CQF_ADDR_STR) {
137 			printf(", %s", addr_str);
138 		} else {
139 			printf(", %p", addr);
140 		}
141 	}
142 
143 	printf(") = %s\n", sprintrc(rc));
144 }
145 
146 
147 static const int bogus_cmd = 0xbadc0ded;
148 static const int bogus_id = 0xca7faced;
149 
150 /* It is invalid anyway due to the slash in the end */
151 static const char *bogus_dev = "/dev/bogus/";
152 static const char *bogus_dev_str = "\"/dev/bogus/\"";
153 
154 static const char unterminated_data[] = { '\1', '\2', '\3' };
155 
156 #endif /* !STRACE_TESTS_QUOTACTL_H */
157