]> granicus.if.org Git - strace/blob - tests/fcntl-common.c
Update copyright headers
[strace] / tests / fcntl-common.c
1 /*
2  * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org>
3  * Copyright (c) 2015-2018 The strace developers.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #include <stdio.h>
30 #include <stdint.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include "flock.h"
35
36 #define FILE_LEN 4096
37
38 #define TEST_FLOCK_EINVAL(cmd) test_flock_einval(cmd, #cmd)
39 #define TEST_FLOCK64_EINVAL(cmd) test_flock64_einval(cmd, #cmd)
40
41 #ifdef HAVE_TYPEOF
42 # define TYPEOF_FLOCK_OFF_T typeof(((struct_kernel_flock *) NULL)->l_len)
43 #else
44 # define TYPEOF_FLOCK_OFF_T off_t
45 #endif
46
47 static const char *errstr;
48
49 static long
50 invoke_test_syscall(const unsigned int fd, const unsigned int cmd, void *const p)
51 {
52         const kernel_ulong_t kfd = F8ILL_KULONG_MASK | fd;
53         const kernel_ulong_t op = F8ILL_KULONG_MASK | cmd;
54
55         long rc = syscall(TEST_SYSCALL_NR, kfd, op, (uintptr_t) p);
56         errstr = sprintrc(rc);
57         return rc;
58 }
59
60 static void
61 test_flock_einval(const int cmd, const char *name)
62 {
63         TAIL_ALLOC_OBJECT_CONST_PTR(struct_kernel_flock, fl);
64         memset(fl, 0, sizeof(*fl));
65         fl->l_type = F_RDLCK;
66         fl->l_start = (TYPEOF_FLOCK_OFF_T) 0xdefaced1facefeedULL;
67         fl->l_len = (TYPEOF_FLOCK_OFF_T) 0xdefaced2cafef00dULL;
68
69         invoke_test_syscall(0, cmd, fl);
70         printf("%s(0, %s, {l_type=F_RDLCK, l_whence=SEEK_SET"
71                ", l_start=%jd, l_len=%jd}) = %s\n", TEST_SYSCALL_STR, name,
72                (intmax_t) fl->l_start, (intmax_t) fl->l_len, errstr);
73
74         void *const bad_addr = (void *) fl + 1;
75         invoke_test_syscall(0, cmd, bad_addr);
76         printf("%s(0, %s, %p) = %s\n",
77                TEST_SYSCALL_STR, name, bad_addr, errstr);
78 }
79
80 /*
81  * This function is not declared static to avoid potential
82  * "defined but not used" warning when included by fcntl.c
83  */
84 void
85 test_flock64_einval(const int cmd, const char *name)
86 {
87         TAIL_ALLOC_OBJECT_CONST_PTR(struct_kernel_flock64, fl);
88         memset(fl, 0, sizeof(*fl));
89         fl->l_type = F_RDLCK;
90         fl->l_start = (TYPEOF_FLOCK_OFF_T) 0xdefaced1facefeedULL;
91         fl->l_len = (TYPEOF_FLOCK_OFF_T) 0xdefaced2cafef00dULL;
92
93         invoke_test_syscall(0, cmd, fl);
94         printf("%s(0, %s, {l_type=F_RDLCK, l_whence=SEEK_SET"
95                ", l_start=%jd, l_len=%jd}) = %s\n", TEST_SYSCALL_STR, name,
96                (intmax_t) fl->l_start, (intmax_t) fl->l_len, errstr);
97
98         void *const bad_addr = (void *) fl + 1;
99         invoke_test_syscall(0, cmd, bad_addr);
100         printf("%s(0, %s, %p) = %s\n",
101                TEST_SYSCALL_STR, name, bad_addr, errstr);
102 }
103
104 static void
105 test_flock(void)
106 {
107         TEST_FLOCK_EINVAL(F_SETLK);
108         TEST_FLOCK_EINVAL(F_SETLKW);
109
110         TAIL_ALLOC_OBJECT_CONST_PTR(struct_kernel_flock, fl);
111         memset(fl, 0, sizeof(*fl));
112         fl->l_type = F_RDLCK;
113         fl->l_len = FILE_LEN;
114
115         long rc = invoke_test_syscall(0, F_SETLK, fl);
116         printf("%s(0, F_SETLK, {l_type=F_RDLCK, l_whence=SEEK_SET"
117                ", l_start=0, l_len=%d}) = %s\n",
118                TEST_SYSCALL_STR, FILE_LEN, errstr);
119         if (rc)
120                 return;
121
122         invoke_test_syscall(0, F_GETLK, fl);
123         printf("%s(0, F_GETLK, {l_type=F_UNLCK, l_whence=SEEK_SET"
124                ", l_start=0, l_len=%d, l_pid=0}) = 0\n",
125                TEST_SYSCALL_STR, FILE_LEN);
126
127         invoke_test_syscall(0, F_SETLKW, fl);
128         printf("%s(0, F_SETLKW, {l_type=F_UNLCK, l_whence=SEEK_SET"
129                ", l_start=0, l_len=%d}) = 0\n",
130                TEST_SYSCALL_STR, FILE_LEN);
131 }
132
133 static void
134 test_flock64_ofd(void)
135 {
136 #if defined F_OFD_GETLK && defined F_OFD_SETLK && defined F_OFD_SETLKW
137         TEST_FLOCK64_EINVAL(F_OFD_SETLK);
138         TEST_FLOCK64_EINVAL(F_OFD_SETLKW);
139
140         TAIL_ALLOC_OBJECT_CONST_PTR(struct_kernel_flock64, fl);
141         memset(fl, 0, sizeof(*fl));
142         fl->l_type = F_RDLCK;
143         fl->l_len = FILE_LEN;
144
145         long rc = invoke_test_syscall(0, F_OFD_SETLK, fl);
146         printf("%s(0, F_OFD_SETLK, {l_type=F_RDLCK, l_whence=SEEK_SET"
147                ", l_start=0, l_len=%d}) = %s\n",
148                TEST_SYSCALL_STR, FILE_LEN, errstr);
149         if (rc)
150                 return;
151
152         invoke_test_syscall(0, F_OFD_GETLK, fl);
153         printf("%s(0, F_OFD_GETLK, {l_type=F_UNLCK, l_whence=SEEK_SET"
154                ", l_start=0, l_len=%d, l_pid=0}) = 0\n",
155                TEST_SYSCALL_STR, FILE_LEN);
156
157         invoke_test_syscall(0, F_OFD_SETLKW, fl);
158         printf("%s(0, F_OFD_SETLKW, {l_type=F_UNLCK, l_whence=SEEK_SET"
159                ", l_start=0, l_len=%d}) = 0\n",
160                TEST_SYSCALL_STR, FILE_LEN);
161 #endif /* F_OFD_GETLK && F_OFD_SETLK && F_OFD_SETLKW */
162 }
163
164 static void test_flock64_lk64(void);
165
166 static void
167 test_flock64(void)
168 {
169         test_flock64_ofd();
170         test_flock64_lk64();
171 }
172
173 /*
174  * F_[GS]ETOWN_EX had conflicting values with F_[SG]ETLK64
175  * in kernel revisions v2.6.32-rc1~96..v2.6.32-rc7~23.
176  */
177 #undef TEST_F_OWNER_EX
178 #if defined F_GETOWN_EX && defined F_SETOWN_EX \
179  && (F_GETOWN_EX != F_SETLK64) && (F_SETOWN_EX != F_GETLK64)
180 # define TEST_F_OWNER_EX
181 #endif
182
183 #ifdef TEST_F_OWNER_EX
184 # include "f_owner_ex.h"
185
186 static long
187 test_f_owner_ex_type_pid(const int cmd, const char *const cmd_name,
188                          const int type, const char *const type_name,
189                          pid_t pid)
190 {
191         TAIL_ALLOC_OBJECT_CONST_PTR(struct_kernel_f_owner_ex, fo);
192
193         fo->type = type;
194         fo->pid = pid;
195         long rc = invoke_test_syscall(0, cmd, fo);
196         printf("%s(0, %s, {type=%s, pid=%d}) = %s\n",
197                TEST_SYSCALL_STR, cmd_name, type_name, fo->pid, errstr);
198
199         void *bad_addr = (void *) fo + 1;
200         invoke_test_syscall(0, cmd, bad_addr);
201         printf("%s(0, %s, %p) = %s\n",
202                TEST_SYSCALL_STR, cmd_name, bad_addr, errstr);
203
204         return rc;
205 }
206
207 static void
208 test_f_owner_ex_umove_or_printaddr(const int type, const char *const type_name,
209                                    pid_t pid)
210 {
211         long rc = test_f_owner_ex_type_pid(ARG_STR(F_SETOWN_EX),
212                                            type, type_name, pid);
213         if (!rc)
214                 test_f_owner_ex_type_pid(ARG_STR(F_GETOWN_EX),
215                                          type, type_name, pid);
216 }
217
218 static void
219 test_f_owner_ex(void)
220 {
221         static const struct {
222                 int type;
223                 const char *type_name;
224                 pid_t pid[2];
225         } a[] = {
226                 { ARG_STR(F_OWNER_TID), { 1234567890, 20 } },
227                 { ARG_STR(F_OWNER_PID), { 1298126790, 30 } },
228                 { ARG_STR(F_OWNER_PGRP), { 1294567890, 40 } }
229         };
230
231         for (unsigned int i = 0; i < ARRAY_SIZE(a); i++) {
232                 for (unsigned int j = 0; j < ARRAY_SIZE(a[0].pid); j++) {
233                         test_f_owner_ex_umove_or_printaddr(a[i].type,
234                                                            a[i].type_name,
235                                                            a[i].pid[j]);
236                 }
237         }
238 }
239 #endif /* TEST_F_OWNER_EX */
240
241 static void
242 create_sample(void)
243 {
244         char fname[] = TEST_SYSCALL_STR "_XXXXXX";
245
246         (void) close(0);
247         if (mkstemp(fname))
248                 perror_msg_and_fail("mkstemp: %s", fname);
249         if (unlink(fname))
250                 perror_msg_and_fail("unlink: %s", fname);
251         if (ftruncate(0, FILE_LEN))
252                 perror_msg_and_fail("ftruncate");
253 }
254
255 int
256 main(void)
257 {
258         create_sample();
259         test_flock();
260         test_flock64();
261 #ifdef TEST_F_OWNER_EX
262         test_f_owner_ex();
263 #endif
264
265         puts("+++ exited with 0 +++");
266         return 0;
267 }