1 /*
2  * Copyright (c) 2015 Cedric Hnyda <chnyda@suse.com>
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of
7  * the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it would be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write the Free Software Foundation,
16  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18 
19  /* Description:
20  *   Verify that:
21  *		1) kcmp returns 0 with two process and two fd refering to the
22  *			same open file
23  *		2) kcmp doesn't return 0 with two process and two fd not
24  *		   refering to the same open file
25  */
26 
27 #define _GNU_SOURCE
28 
29 #include "tst_test.h"
30 #include "lapi/fcntl.h"
31 #include "kcmp.h"
32 
33 #define TEST_FILE "test_file"
34 #define TEST_FILE2 "test_file2"
35 
36 static int fd1;
37 static int fd2;
38 static int fd3;
39 static int pid1;
40 static int pid2;
41 
42 static struct test_case {
43 	int *pid1;
44 	int *pid2;
45 	int type;
46 	int *fd1;
47 	int *fd2;
48 	int exp_different;
49 } test_cases[] = {
50 	{&pid1, &pid1, KCMP_FILE, &fd1, &fd1, 0},
51 	{&pid2, &pid2, KCMP_FILE, &fd1, &fd2, 0},
52 	{&pid1, &pid2, KCMP_FILE, &fd1, &fd1, 0},
53 	{&pid1, &pid2, KCMP_FILE, &fd1, &fd2, 0},
54 	{&pid1, &pid2, KCMP_FILE, &fd1, &fd3, 1},
55 };
56 
setup(void)57 static void setup(void)
58 {
59 	fd1 = SAFE_OPEN(TEST_FILE, O_CREAT | O_RDWR | O_TRUNC);
60 }
61 
cleanup(void)62 static void cleanup(void)
63 {
64 	if (fd1 > 0)
65 		SAFE_CLOSE(fd1);
66 }
67 
do_child(const struct test_case * test)68 static void do_child(const struct test_case *test)
69 {
70 	pid2 = getpid();
71 
72 	fd3 = SAFE_OPEN(TEST_FILE2, O_CREAT | O_RDWR, 0666);
73 
74 	fd2 = dup(fd1);
75 	if (fd2 == -1) {
76 		tst_res(TFAIL | TERRNO, "dup() failed unexpectedly");
77 		SAFE_CLOSE(fd3);
78 		return;
79 	}
80 
81 	TEST(kcmp(*(test->pid1), *(test->pid2), test->type,
82 		  *(test->fd1), *(test->fd2)));
83 
84 	SAFE_CLOSE(fd2);
85 	SAFE_CLOSE(fd3);
86 
87 	if (TST_RET == -1) {
88 		tst_res(TFAIL | TTERRNO, "kcmp() failed unexpectedly");
89 		return;
90 	}
91 
92 	if ((test->exp_different && TST_RET == 0)
93 		|| (test->exp_different == 0 && TST_RET)) {
94 		tst_res(TFAIL, "kcmp() returned %lu instead of %d",
95 			TST_RET, test->exp_different);
96 		return;
97 	}
98 
99 	tst_res(TPASS, "kcmp() returned the expected value");
100 }
101 
verify_kcmp(unsigned int n)102 static void verify_kcmp(unsigned int n)
103 {
104 	struct test_case *tc = &test_cases[n];
105 
106 	pid1 = getpid();
107 
108 	pid2 = SAFE_FORK();
109 	if (!pid2)
110 		do_child(tc);
111 }
112 
113 static struct tst_test test = {
114 	.tcnt = ARRAY_SIZE(test_cases),
115 	.setup = setup,
116 	.cleanup = cleanup,
117 	.forks_child = 1,
118 	.test = verify_kcmp,
119 	.min_kver = "3.5.0",
120 	.needs_tmpdir = 1,
121 };
122