]> granicus.if.org Git - strace/blob - tests/quotactl.h
tests: move fill_memory and fill_memory_ex into a separate file
[strace] / tests / quotactl.h
1 /*
2  * Common definitions for Linux and XFS quota tests.
3  *
4  * Copyright (c) 2016 Eugene Syromiatnikov <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 # define ARG_STR(_arg) (_arg), #_arg
73
74 typedef void (*print_cb)(long rc, void *addr, void *arg);
75
76 enum check_quotactl_flag_bits {
77         CQF_ID_SKIP_BIT,
78         CQF_ID_STR_BIT,
79         CQF_ADDR_SKIP_BIT,
80         CQF_ADDR_STR_BIT,
81         CQF_ADDR_CB_BIT,
82 };
83
84 enum check_quotactl_flags {
85         CQF_NONE,
86         CQF_ID_SKIP   = 1 << CQF_ID_SKIP_BIT,
87         CQF_ID_STR    = 1 << CQF_ID_STR_BIT,
88         CQF_ADDR_SKIP = 1 << CQF_ADDR_SKIP_BIT,
89         CQF_ADDR_STR  = 1 << CQF_ADDR_STR_BIT,
90         CQF_ADDR_CB   = 1 << CQF_ADDR_CB_BIT,
91 };
92
93
94 static inline void
95 check_quota(uint32_t flags, int cmd, const char *cmd_str,
96         const char *special, const char *special_str, ...)
97 {
98         long rc;
99         const char *addr_str = NULL;
100         const char *id_str = NULL;
101         void *addr = NULL;
102         print_cb addr_cb = NULL;
103         void *addr_cb_arg = NULL;
104         uint32_t id = -1;
105
106         va_list ap;
107
108         va_start(ap, special_str);
109
110         if (!(flags & CQF_ID_SKIP)) {
111                 id = va_arg(ap, uint32_t);
112
113                 if (flags & CQF_ID_STR)
114                         id_str = va_arg(ap, const char *);
115         }
116
117         if (!(flags & CQF_ADDR_SKIP)) {
118                 addr = va_arg(ap, void *);
119
120                 if (flags & CQF_ADDR_CB) {
121                         addr_cb = va_arg(ap, print_cb);
122                         addr_cb_arg = va_arg(ap, void *);
123                 } else if (flags & CQF_ADDR_STR) {
124                         addr_str = va_arg(ap, const char *);
125                 }
126         }
127
128         va_end(ap);
129
130         rc = syscall(__NR_quotactl, cmd, special, id, addr);
131         printf("quotactl(%s, %s", cmd_str, special_str);
132
133         if (!(flags & CQF_ID_SKIP)) {
134                 if (flags & CQF_ID_STR) {
135                         printf(", %s", id_str);
136                 } else {
137                         if (id == (uint32_t)-1)
138                                 printf(", -1");
139                         else
140                                 printf(", %u", id);
141                 }
142         }
143
144         if (!(flags & CQF_ADDR_SKIP)) {
145                 if (flags & CQF_ADDR_CB) {
146                         printf(", ");
147                         addr_cb(rc, addr, addr_cb_arg);
148                 } else if (flags & CQF_ADDR_STR) {
149                         printf(", %s", addr_str);
150                 } else {
151                         printf(", %p", addr);
152                 }
153         }
154
155         printf(") = %s\n", sprintrc(rc));
156 }
157
158
159 static const int bogus_cmd = 0xbadc0ded;
160 static const char * const bogus_special =
161         (const char *) (unsigned long) 0xfffffca7ffffc0deULL;
162 static const int bogus_id = 0xca7faced;
163 static void * const bogus_addr =
164         (void *) (unsigned long) 0xffffda7affffdeadULL;
165
166 /* It is invalid anyway due to the flash in the end */
167 static const char *bogus_dev = "/dev/bogus/";
168 static const char *bogus_dev_str = "\"/dev/bogus/\"";
169
170 static const char unterminated_data[] = { '\1', '\2', '\3' };
171
172 #endif /* !STRACE_TESTS_QUOTACTL_H */