1/*
2 * Check decoding of kcmp syscall.
3 *
4 * Copyright (c) 2016 Eugene Syromyatnikov <evgsyr@gmail.com>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 *    derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include "tests.h"
31
32#include <asm/unistd.h>
33#include "scno.h"
34
35#ifdef __NR_kcmp
36
37# include <stdio.h>
38# include <unistd.h>
39
40# define KCMP_FILE     0
41# define KCMP_SYSVSEM  6
42
43static void
44do_kcmp(kernel_ulong_t pid1, kernel_ulong_t pid2, kernel_ulong_t type,
45	const char *type_str, kernel_ulong_t idx1, kernel_ulong_t idx2)
46{
47	long rc;
48	const char *errstr;
49
50	rc = syscall(__NR_kcmp, pid1, pid2, type, idx1, idx2);
51	errstr = sprintrc(rc);
52
53	printf("kcmp(%d, %d, ", (int) pid1, (int) pid2);
54
55	if (type_str)
56		printf("%s", type_str);
57	else
58		printf("%#x /* KCMP_??? */", (int) type);
59
60	if (type == KCMP_FILE)
61		printf(", %u, %u", (unsigned) idx1, (unsigned) idx2);
62	else if (type > KCMP_SYSVSEM)
63		printf(", %#llx, %#llx",
64		       (unsigned long long) idx1, (unsigned long long) idx2);
65
66	printf(") = %s\n", errstr);
67}
68
69int
70main(void)
71{
72	static const kernel_ulong_t bogus_pid1 =
73		(kernel_ulong_t) 0xdeadca75face1057ULL;
74	static const kernel_ulong_t bogus_pid2 =
75		(kernel_ulong_t) 0xdefaced1defaced2ULL;
76	static const kernel_ulong_t bogus_type =
77		(kernel_ulong_t) 0xbadc0dedda7adeadULL;
78	static const kernel_ulong_t bogus_idx1 =
79		(kernel_ulong_t) 0xdec0ded3dec0ded4ULL;
80	static const kernel_ulong_t bogus_idx2 =
81		(kernel_ulong_t) 0xba5e1e55deadc0deULL;
82
83	/* Invalid values */
84	do_kcmp(bogus_pid1, bogus_pid2, bogus_type, NULL, bogus_idx1,
85		bogus_idx2);
86	do_kcmp(0, 0, KCMP_SYSVSEM + 1, NULL, 0, 0);
87
88	/* KCMP_FILE is the only type which has additional args */
89	do_kcmp(3141592653U, 2718281828U, ARG_STR(KCMP_FILE), bogus_idx1,
90		bogus_idx2);
91	/* Some type without additional args */
92	do_kcmp(-1, -1, ARG_STR(KCMP_SYSVSEM), bogus_idx1, bogus_idx2);
93
94	puts("+++ exited with 0 +++");
95
96	return 0;
97}
98
99#else
100
101SKIP_MAIN_UNDEFINED("__NR_kcmp");
102
103#endif
104