xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/kcmp/kcmp01.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2015 Cedric Hnyda <[email protected]>
4*49cdfc7eSAndroid Build Coastguard Worker  */
5*49cdfc7eSAndroid Build Coastguard Worker 
6*49cdfc7eSAndroid Build Coastguard Worker  /* Description:
7*49cdfc7eSAndroid Build Coastguard Worker  *   Verify that:
8*49cdfc7eSAndroid Build Coastguard Worker  *		1) kcmp returns 0 with two process and two fd refering to the
9*49cdfc7eSAndroid Build Coastguard Worker  *			same open file
10*49cdfc7eSAndroid Build Coastguard Worker  *		2) kcmp doesn't return 0 with two process and two fd not
11*49cdfc7eSAndroid Build Coastguard Worker  *		   refering to the same open file
12*49cdfc7eSAndroid Build Coastguard Worker  */
13*49cdfc7eSAndroid Build Coastguard Worker 
14*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
15*49cdfc7eSAndroid Build Coastguard Worker 
16*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
17*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/fcntl.h"
18*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/kcmp.h"
19*49cdfc7eSAndroid Build Coastguard Worker 
20*49cdfc7eSAndroid Build Coastguard Worker #define TEST_FILE "test_file"
21*49cdfc7eSAndroid Build Coastguard Worker #define TEST_FILE2 "test_file2"
22*49cdfc7eSAndroid Build Coastguard Worker 
23*49cdfc7eSAndroid Build Coastguard Worker static int fd1;
24*49cdfc7eSAndroid Build Coastguard Worker static int fd2;
25*49cdfc7eSAndroid Build Coastguard Worker static int fd3;
26*49cdfc7eSAndroid Build Coastguard Worker static int pid1;
27*49cdfc7eSAndroid Build Coastguard Worker static int pid2;
28*49cdfc7eSAndroid Build Coastguard Worker 
29*49cdfc7eSAndroid Build Coastguard Worker static struct test_case {
30*49cdfc7eSAndroid Build Coastguard Worker 	int *pid1;
31*49cdfc7eSAndroid Build Coastguard Worker 	int *pid2;
32*49cdfc7eSAndroid Build Coastguard Worker 	int type;
33*49cdfc7eSAndroid Build Coastguard Worker 	int *fd1;
34*49cdfc7eSAndroid Build Coastguard Worker 	int *fd2;
35*49cdfc7eSAndroid Build Coastguard Worker 	int exp_different;
36*49cdfc7eSAndroid Build Coastguard Worker } test_cases[] = {
37*49cdfc7eSAndroid Build Coastguard Worker 	{&pid1, &pid1, KCMP_FILE, &fd1, &fd1, 0},
38*49cdfc7eSAndroid Build Coastguard Worker 	{&pid2, &pid2, KCMP_FILE, &fd1, &fd2, 0},
39*49cdfc7eSAndroid Build Coastguard Worker 	{&pid1, &pid2, KCMP_FILE, &fd1, &fd1, 0},
40*49cdfc7eSAndroid Build Coastguard Worker 	{&pid1, &pid2, KCMP_FILE, &fd1, &fd2, 0},
41*49cdfc7eSAndroid Build Coastguard Worker 	{&pid1, &pid2, KCMP_FILE, &fd1, &fd3, 1},
42*49cdfc7eSAndroid Build Coastguard Worker };
43*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)44*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
45*49cdfc7eSAndroid Build Coastguard Worker {
46*49cdfc7eSAndroid Build Coastguard Worker 	fd1 = SAFE_OPEN(TEST_FILE, O_CREAT | O_RDWR | O_TRUNC, 0666);
47*49cdfc7eSAndroid Build Coastguard Worker }
48*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)49*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
50*49cdfc7eSAndroid Build Coastguard Worker {
51*49cdfc7eSAndroid Build Coastguard Worker 	if (fd1 > 0)
52*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(fd1);
53*49cdfc7eSAndroid Build Coastguard Worker }
54*49cdfc7eSAndroid Build Coastguard Worker 
do_child(const struct test_case * test)55*49cdfc7eSAndroid Build Coastguard Worker static void do_child(const struct test_case *test)
56*49cdfc7eSAndroid Build Coastguard Worker {
57*49cdfc7eSAndroid Build Coastguard Worker 	pid2 = getpid();
58*49cdfc7eSAndroid Build Coastguard Worker 
59*49cdfc7eSAndroid Build Coastguard Worker 	fd3 = SAFE_OPEN(TEST_FILE2, O_CREAT | O_RDWR, 0666);
60*49cdfc7eSAndroid Build Coastguard Worker 
61*49cdfc7eSAndroid Build Coastguard Worker 	fd2 = dup(fd1);
62*49cdfc7eSAndroid Build Coastguard Worker 	if (fd2 == -1) {
63*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TERRNO, "dup() failed unexpectedly");
64*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(fd3);
65*49cdfc7eSAndroid Build Coastguard Worker 		return;
66*49cdfc7eSAndroid Build Coastguard Worker 	}
67*49cdfc7eSAndroid Build Coastguard Worker 
68*49cdfc7eSAndroid Build Coastguard Worker 	TEST(kcmp(*(test->pid1), *(test->pid2), test->type,
69*49cdfc7eSAndroid Build Coastguard Worker 		  *(test->fd1), *(test->fd2)));
70*49cdfc7eSAndroid Build Coastguard Worker 
71*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(fd2);
72*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(fd3);
73*49cdfc7eSAndroid Build Coastguard Worker 
74*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET == -1) {
75*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TTERRNO, "kcmp() failed unexpectedly");
76*49cdfc7eSAndroid Build Coastguard Worker 		return;
77*49cdfc7eSAndroid Build Coastguard Worker 	}
78*49cdfc7eSAndroid Build Coastguard Worker 
79*49cdfc7eSAndroid Build Coastguard Worker 	if ((test->exp_different && TST_RET == 0)
80*49cdfc7eSAndroid Build Coastguard Worker 		|| (test->exp_different == 0 && TST_RET)) {
81*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "kcmp() returned %lu instead of %d",
82*49cdfc7eSAndroid Build Coastguard Worker 			TST_RET, test->exp_different);
83*49cdfc7eSAndroid Build Coastguard Worker 		return;
84*49cdfc7eSAndroid Build Coastguard Worker 	}
85*49cdfc7eSAndroid Build Coastguard Worker 
86*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TPASS, "kcmp() returned the expected value");
87*49cdfc7eSAndroid Build Coastguard Worker }
88*49cdfc7eSAndroid Build Coastguard Worker 
verify_kcmp(unsigned int n)89*49cdfc7eSAndroid Build Coastguard Worker static void verify_kcmp(unsigned int n)
90*49cdfc7eSAndroid Build Coastguard Worker {
91*49cdfc7eSAndroid Build Coastguard Worker 	struct test_case *tc = &test_cases[n];
92*49cdfc7eSAndroid Build Coastguard Worker 
93*49cdfc7eSAndroid Build Coastguard Worker 	pid1 = getpid();
94*49cdfc7eSAndroid Build Coastguard Worker 
95*49cdfc7eSAndroid Build Coastguard Worker 	pid2 = SAFE_FORK();
96*49cdfc7eSAndroid Build Coastguard Worker 	if (!pid2)
97*49cdfc7eSAndroid Build Coastguard Worker 		do_child(tc);
98*49cdfc7eSAndroid Build Coastguard Worker }
99*49cdfc7eSAndroid Build Coastguard Worker 
100*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
101*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(test_cases),
102*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
103*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
104*49cdfc7eSAndroid Build Coastguard Worker 	.forks_child = 1,
105*49cdfc7eSAndroid Build Coastguard Worker 	.test = verify_kcmp,
106*49cdfc7eSAndroid Build Coastguard Worker 	.needs_tmpdir = 1,
107*49cdfc7eSAndroid Build Coastguard Worker };
108