• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "test.h"
30 #include "safe_macros.h"
31 #include "lapi/fcntl.h"
32 #include "kcmp.h"
33 
34 #define TEST_FILE "test_file"
35 #define TEST_FILE2 "test_file2"
36 
37 
38 static int fd1;
39 static int fd2;
40 static int fd3;
41 static int pid1;
42 static int pid2;
43 
44 char *TCID = "kcmp01";
45 
46 static struct test_case {
47 	int *pid1;
48 	int *pid2;
49 	int type;
50 	int *fd1;
51 	int *fd2;
52 	int exp_different;
53 } test_cases[] = {
54 	{&pid1, &pid1, KCMP_FILE, &fd1, &fd1, 0},
55 	{&pid2, &pid2, KCMP_FILE, &fd1, &fd2, 0},
56 	{&pid1, &pid2, KCMP_FILE, &fd1, &fd1, 0},
57 	{&pid1, &pid2, KCMP_FILE, &fd1, &fd2, 0},
58 	{&pid1, &pid2, KCMP_FILE, &fd1, &fd3, 1},
59 };
60 
61 int TST_TOTAL = ARRAY_SIZE(test_cases);
62 
63 static void cleanup(void);
64 static void setup(void);
65 static void do_child(const struct test_case *test);
66 static void cleanup_child(void);
67 
main(int ac,char ** av)68 int main(int ac, char **av)
69 {
70 	int lc;
71 	int i;
72 
73 	tst_parse_opts(ac, av, NULL, NULL);
74 	setup();
75 
76 	for (lc = 0; TEST_LOOPING(lc); lc++) {
77 		tst_count = 0;
78 
79 		for (i = 0; i < TST_TOTAL; ++i) {
80 			pid2 = tst_fork();
81 
82 			if (pid2 == -1)
83 				tst_brkm(TBROK, cleanup, "fork failed");
84 
85 			if (!pid2)
86 				do_child(&test_cases[i]);
87 			else
88 				tst_record_childstatus(cleanup, pid2);
89 			tst_count++;
90 		}
91 	}
92 
93 	cleanup();
94 	tst_exit();
95 }
96 
do_child(const struct test_case * test)97 static void do_child(const struct test_case *test)
98 {
99 	pid2 = getpid();
100 	fd2 = dup(fd1);
101 	fd3 = SAFE_OPEN(cleanup_child, TEST_FILE2, O_CREAT | O_RDWR, 0666);
102 
103 	TEST(kcmp(*(test->pid1), *(test->pid2), test->type,
104 			  *(test->fd1), *(test->fd2)));
105 
106 	if (TEST_RETURN == -1)
107 		tst_resm(TFAIL | TTERRNO, "kcmp() failed unexpectedly");
108 
109 	if ((test->exp_different && TEST_RETURN == 0)
110 		|| (test->exp_different == 0 && TEST_RETURN))
111 		tst_resm(TFAIL, "kcmp() returned %lu instead of %d",
112 				TEST_RETURN, test->exp_different);
113 
114 	if ((test->exp_different == 0 && TEST_RETURN == 0)
115 		|| (test->exp_different && TEST_RETURN))
116 		tst_resm(TPASS, "kcmp() returned the expected value");
117 
118 	tst_exit();
119 }
120 
cleanup_child(void)121 static void cleanup_child(void)
122 {
123 	if (fd2 > 0 && close(fd2) < 0)
124 		tst_resm(TWARN | TERRNO, "close fd2 failed");
125 	fd2 = 0;
126 	if (fd3 > 0 && close(fd3) < 0)
127 		tst_resm(TWARN | TERRNO, "close fd3 failed");
128 	fd3 = 0;
129 }
130 
setup(void)131 static void setup(void)
132 {
133 	if ((tst_kvercmp(3, 5, 0)) < 0) {
134 		tst_brkm(TCONF, NULL,
135 			"This test can only run on kernels that are 3.5. and higher");
136 	}
137 
138 	tst_tmpdir();
139 
140 	pid1 = getpid();
141 	fd1 = SAFE_OPEN(cleanup, TEST_FILE, O_CREAT | O_RDWR | O_TRUNC);
142 }
143 
cleanup(void)144 static void cleanup(void)
145 {
146 	if (fd1 > 0 && close(fd1) < 0)
147 		tst_resm(TWARN | TERRNO, "close fd1 failed");
148 	tst_rmdir();
149 }
150