]> granicus.if.org Git - strace/blob - tests/socketcall.c
Update copyright headers
[strace] / tests / socketcall.c
1 /*
2  * Check decoding of socketcall syscall.
3  *
4  * Copyright (c) 2016-2018 Dmitry V. Levin <ldv@altlinux.org>
5  * All rights reserved.
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  */
9
10 #include "tests.h"
11 #include <asm/unistd.h>
12
13 #ifdef __NR_socketcall
14
15 # include <assert.h>
16 # include <stdio.h>
17 # include <unistd.h>
18
19 # include "xlat.h"
20 # include "xlat/socketcalls.h"
21
22 static const char *
23 xlookup_uint(const struct xlat *xlat, const unsigned int val)
24 {
25         for (; xlat->str != NULL; xlat++)
26                 if (xlat->val == val)
27                         return xlat->str;
28         return NULL;
29 }
30
31 static const int sc_min = 1, sc_max = 20;
32 static void *efault;
33
34 static void
35 test_socketcall(const int i, const void *const addr)
36 {
37         const unsigned long call =
38                 (unsigned long) 0xfacefeed00000000ULL | (unsigned int) i;
39
40         long rc = syscall(__NR_socketcall, call, addr);
41
42         if (i < sc_min || i > sc_max) {
43                 printf("socketcall(%d, %p) = %ld %s (%m)\n",
44                        (int) call, addr, rc, errno2name());
45         } else if (addr == efault) {
46                 const char *const str = xlookup_uint(socketcalls, i);
47                 assert(str);
48                 printf("socketcall(%s, %p) = %ld %s (%m)\n",
49                        str, addr, rc, errno2name());
50         }
51 }
52 int
53 main(void)
54 {
55         assert((unsigned) sc_min == socketcalls[0].val);
56         assert((unsigned) sc_max == socketcalls[ARRAY_SIZE(socketcalls) - 2].val);
57
58         const unsigned long *const args = tail_alloc(sizeof(*args) * 6);
59         efault = tail_alloc(1) + 1;
60
61         int i;
62         for (i = sc_min - 3; i <= sc_max + 3; ++i) {
63                 test_socketcall(i, efault);
64                 test_socketcall(i, args);
65         }
66
67         puts("+++ exited with 0 +++");
68         return 0;
69 }
70
71 #else
72
73 SKIP_MAIN_UNDEFINED("__NR_socketcall")
74
75 #endif