]> granicus.if.org Git - strace/blob - tests/quotactl.h
tests: extend TEST_NETLINK_OBJECT macro
[strace] / tests / quotactl.h
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 # include "print_fields.h"
38
39 # ifdef HAVE_LINUX_QUOTA_H
40 /* Broken in CentOS 5: has extern spinlock_t dq_data_lock; declaration */
41 #  include <linux/quota.h>
42 # else
43 #  include <linux/types.h>
44 /* Broken in some new glibc versions: have Q_GETNEXTQUOTA definition but no
45  * struct nextdqblk defined. Fixed in glibc-2.24-106-g4d72808. */
46 #  include <sys/quota.h>
47 # endif
48
49 # ifndef QCMD_CMD
50 #  define QCMD_CMD(_val) ((unsigned) (_val) >> SUBCMDSHIFT)
51 # endif /* !QCMD_CMD */
52
53 # ifndef QCMD_TYPE
54 #  define QCMD_TYPE(_val) ((unsigned) (_val) & SUBCMDMASK)
55 # endif /* !QCMD_TYPE */
56
57 # ifndef PRJQUOTA
58 #  define PRJQUOTA 2
59 # endif
60
61 typedef void (*print_cb)(long rc, void *addr, void *arg);
62
63 enum check_quotactl_flag_bits {
64         CQF_ID_SKIP_BIT,
65         CQF_ID_STR_BIT,
66         CQF_ADDR_SKIP_BIT,
67         CQF_ADDR_STR_BIT,
68         CQF_ADDR_CB_BIT,
69 };
70
71 enum check_quotactl_flags {
72         CQF_NONE,
73         CQF_ID_SKIP   = 1 << CQF_ID_SKIP_BIT,
74         CQF_ID_STR    = 1 << CQF_ID_STR_BIT,
75         CQF_ADDR_SKIP = 1 << CQF_ADDR_SKIP_BIT,
76         CQF_ADDR_STR  = 1 << CQF_ADDR_STR_BIT,
77         CQF_ADDR_CB   = 1 << CQF_ADDR_CB_BIT,
78 };
79
80
81 static inline void
82 check_quota(uint32_t flags, int cmd, const char *cmd_str,
83         const char *special, const char *special_str, ...)
84 {
85         long rc;
86         const char *addr_str = NULL;
87         const char *id_str = NULL;
88         void *addr = NULL;
89         print_cb addr_cb = NULL;
90         void *addr_cb_arg = NULL;
91         uint32_t id = -1;
92
93         va_list ap;
94
95         va_start(ap, special_str);
96
97         if (!(flags & CQF_ID_SKIP)) {
98                 id = va_arg(ap, uint32_t);
99
100                 if (flags & CQF_ID_STR)
101                         id_str = va_arg(ap, const char *);
102         }
103
104         if (!(flags & CQF_ADDR_SKIP)) {
105                 addr = va_arg(ap, void *);
106
107                 if (flags & CQF_ADDR_CB) {
108                         addr_cb = va_arg(ap, print_cb);
109                         addr_cb_arg = va_arg(ap, void *);
110                 } else if (flags & CQF_ADDR_STR) {
111                         addr_str = va_arg(ap, const char *);
112                 }
113         }
114
115         va_end(ap);
116
117         rc = syscall(__NR_quotactl, cmd, special, id, addr);
118         printf("quotactl(%s, %s", cmd_str, special_str);
119
120         if (!(flags & CQF_ID_SKIP)) {
121                 if (flags & CQF_ID_STR) {
122                         printf(", %s", id_str);
123                 } else {
124                         if (id == (uint32_t)-1)
125                                 printf(", -1");
126                         else
127                                 printf(", %u", id);
128                 }
129         }
130
131         if (!(flags & CQF_ADDR_SKIP)) {
132                 if (flags & CQF_ADDR_CB) {
133                         printf(", ");
134                         addr_cb(rc, addr, addr_cb_arg);
135                 } else if (flags & CQF_ADDR_STR) {
136                         printf(", %s", addr_str);
137                 } else {
138                         printf(", %p", addr);
139                 }
140         }
141
142         printf(") = %s\n", sprintrc(rc));
143 }
144
145
146 static const int bogus_cmd = 0xbadc0ded;
147 static const int bogus_id = 0xca7faced;
148
149 /* It is invalid anyway due to the slash in the end */
150 static const char *bogus_dev = "/dev/bogus/";
151 static const char *bogus_dev_str = "\"/dev/bogus/\"";
152
153 static const char unterminated_data[] = { '\1', '\2', '\3' };
154
155 #endif /* !STRACE_TESTS_QUOTACTL_H */