xref: /openbmc/linux/tools/testing/selftests/kcmp/kcmp_test.c (revision 66559a691a5b4a3d79852a82c8c2f8532077b301)
1d97b46a6SCyrill Gorcunov #define _GNU_SOURCE
2d97b46a6SCyrill Gorcunov 
3d97b46a6SCyrill Gorcunov #include <stdio.h>
4d97b46a6SCyrill Gorcunov #include <stdlib.h>
5d97b46a6SCyrill Gorcunov #include <signal.h>
6d97b46a6SCyrill Gorcunov #include <limits.h>
7d97b46a6SCyrill Gorcunov #include <unistd.h>
8d97b46a6SCyrill Gorcunov #include <errno.h>
9d97b46a6SCyrill Gorcunov #include <string.h>
10d97b46a6SCyrill Gorcunov #include <fcntl.h>
11d97b46a6SCyrill Gorcunov #include <linux/unistd.h>
12d97b46a6SCyrill Gorcunov #include <linux/kcmp.h>
13d97b46a6SCyrill Gorcunov 
14d97b46a6SCyrill Gorcunov #include <sys/syscall.h>
15d97b46a6SCyrill Gorcunov #include <sys/types.h>
16d97b46a6SCyrill Gorcunov #include <sys/stat.h>
17d97b46a6SCyrill Gorcunov #include <sys/wait.h>
18*66559a69SCyrill Gorcunov #include <sys/epoll.h>
19d97b46a6SCyrill Gorcunov 
20e061bcd8SShuah Khan #include "../kselftest.h"
21e061bcd8SShuah Khan 
22*66559a69SCyrill Gorcunov static long sys_kcmp(int pid1, int pid2, int type, unsigned long fd1, unsigned long fd2)
23d97b46a6SCyrill Gorcunov {
24d97b46a6SCyrill Gorcunov 	return syscall(__NR_kcmp, pid1, pid2, type, fd1, fd2);
25d97b46a6SCyrill Gorcunov }
26d97b46a6SCyrill Gorcunov 
27*66559a69SCyrill Gorcunov static const unsigned int duped_num = 64;
28*66559a69SCyrill Gorcunov 
29d97b46a6SCyrill Gorcunov int main(int argc, char **argv)
30d97b46a6SCyrill Gorcunov {
31d97b46a6SCyrill Gorcunov 	const char kpath[] = "kcmp-test-file";
32*66559a69SCyrill Gorcunov 	struct kcmp_epoll_slot epoll_slot;
33*66559a69SCyrill Gorcunov 	struct epoll_event ev;
34d97b46a6SCyrill Gorcunov 	int pid1, pid2;
35*66559a69SCyrill Gorcunov 	int pipefd[2];
36d97b46a6SCyrill Gorcunov 	int fd1, fd2;
37*66559a69SCyrill Gorcunov 	int epollfd;
38d97b46a6SCyrill Gorcunov 	int status;
39*66559a69SCyrill Gorcunov 	int fddup;
40d97b46a6SCyrill Gorcunov 
41d97b46a6SCyrill Gorcunov 	fd1 = open(kpath, O_RDWR | O_CREAT | O_TRUNC, 0644);
42d97b46a6SCyrill Gorcunov 	pid1 = getpid();
43d97b46a6SCyrill Gorcunov 
44d97b46a6SCyrill Gorcunov 	if (fd1 < 0) {
45d97b46a6SCyrill Gorcunov 		perror("Can't create file");
46e061bcd8SShuah Khan 		ksft_exit_fail();
47d97b46a6SCyrill Gorcunov 	}
48d97b46a6SCyrill Gorcunov 
49*66559a69SCyrill Gorcunov 	if (pipe(pipefd)) {
50*66559a69SCyrill Gorcunov 		perror("Can't create pipe");
51*66559a69SCyrill Gorcunov 		ksft_exit_fail();
52*66559a69SCyrill Gorcunov 	}
53*66559a69SCyrill Gorcunov 
54*66559a69SCyrill Gorcunov 	epollfd = epoll_create1(0);
55*66559a69SCyrill Gorcunov 	if (epollfd < 0) {
56*66559a69SCyrill Gorcunov 		perror("epoll_create1 failed");
57*66559a69SCyrill Gorcunov 		ksft_exit_fail();
58*66559a69SCyrill Gorcunov 	}
59*66559a69SCyrill Gorcunov 
60*66559a69SCyrill Gorcunov 	memset(&ev, 0xff, sizeof(ev));
61*66559a69SCyrill Gorcunov 	ev.events = EPOLLIN | EPOLLOUT;
62*66559a69SCyrill Gorcunov 
63*66559a69SCyrill Gorcunov 	if (epoll_ctl(epollfd, EPOLL_CTL_ADD, pipefd[0], &ev)) {
64*66559a69SCyrill Gorcunov 		perror("epoll_ctl failed");
65*66559a69SCyrill Gorcunov 		ksft_exit_fail();
66*66559a69SCyrill Gorcunov 	}
67*66559a69SCyrill Gorcunov 
68*66559a69SCyrill Gorcunov 	fddup = dup2(pipefd[1], duped_num);
69*66559a69SCyrill Gorcunov 	if (fddup < 0) {
70*66559a69SCyrill Gorcunov 		perror("dup2 failed");
71*66559a69SCyrill Gorcunov 		ksft_exit_fail();
72*66559a69SCyrill Gorcunov 	}
73*66559a69SCyrill Gorcunov 
74*66559a69SCyrill Gorcunov 	if (epoll_ctl(epollfd, EPOLL_CTL_ADD, fddup, &ev)) {
75*66559a69SCyrill Gorcunov 		perror("epoll_ctl failed");
76*66559a69SCyrill Gorcunov 		ksft_exit_fail();
77*66559a69SCyrill Gorcunov 	}
78*66559a69SCyrill Gorcunov 	close(fddup);
79*66559a69SCyrill Gorcunov 
80d97b46a6SCyrill Gorcunov 	pid2 = fork();
81d97b46a6SCyrill Gorcunov 	if (pid2 < 0) {
82d97b46a6SCyrill Gorcunov 		perror("fork failed");
83e061bcd8SShuah Khan 		ksft_exit_fail();
84d97b46a6SCyrill Gorcunov 	}
85d97b46a6SCyrill Gorcunov 
86d97b46a6SCyrill Gorcunov 	if (!pid2) {
87d97b46a6SCyrill Gorcunov 		int pid2 = getpid();
88d97b46a6SCyrill Gorcunov 		int ret;
89d97b46a6SCyrill Gorcunov 
90d97b46a6SCyrill Gorcunov 		fd2 = open(kpath, O_RDWR, 0644);
91d97b46a6SCyrill Gorcunov 		if (fd2 < 0) {
92d97b46a6SCyrill Gorcunov 			perror("Can't open file");
93e061bcd8SShuah Khan 			ksft_exit_fail();
94d97b46a6SCyrill Gorcunov 		}
95d97b46a6SCyrill Gorcunov 
96d97b46a6SCyrill Gorcunov 		/* An example of output and arguments */
97d97b46a6SCyrill Gorcunov 		printf("pid1: %6d pid2: %6d FD: %2ld FILES: %2ld VM: %2ld "
98d97b46a6SCyrill Gorcunov 		       "FS: %2ld SIGHAND: %2ld IO: %2ld SYSVSEM: %2ld "
99d97b46a6SCyrill Gorcunov 		       "INV: %2ld\n",
100d97b46a6SCyrill Gorcunov 		       pid1, pid2,
101d97b46a6SCyrill Gorcunov 		       sys_kcmp(pid1, pid2, KCMP_FILE,		fd1, fd2),
102d97b46a6SCyrill Gorcunov 		       sys_kcmp(pid1, pid2, KCMP_FILES,		0, 0),
103d97b46a6SCyrill Gorcunov 		       sys_kcmp(pid1, pid2, KCMP_VM,		0, 0),
104d97b46a6SCyrill Gorcunov 		       sys_kcmp(pid1, pid2, KCMP_FS,		0, 0),
105d97b46a6SCyrill Gorcunov 		       sys_kcmp(pid1, pid2, KCMP_SIGHAND,	0, 0),
106d97b46a6SCyrill Gorcunov 		       sys_kcmp(pid1, pid2, KCMP_IO,		0, 0),
107d97b46a6SCyrill Gorcunov 		       sys_kcmp(pid1, pid2, KCMP_SYSVSEM,	0, 0),
108d97b46a6SCyrill Gorcunov 
109d97b46a6SCyrill Gorcunov 			/* This one should fail */
110d97b46a6SCyrill Gorcunov 		       sys_kcmp(pid1, pid2, KCMP_TYPES + 1,	0, 0));
111d97b46a6SCyrill Gorcunov 
112d97b46a6SCyrill Gorcunov 		/* This one should return same fd */
113d97b46a6SCyrill Gorcunov 		ret = sys_kcmp(pid1, pid2, KCMP_FILE, fd1, fd1);
114d97b46a6SCyrill Gorcunov 		if (ret) {
1152bf1cbf1SDave Jones 			printf("FAIL: 0 expected but %d returned (%s)\n",
1162bf1cbf1SDave Jones 				ret, strerror(errno));
117e061bcd8SShuah Khan 			ksft_inc_fail_cnt();
118d97b46a6SCyrill Gorcunov 			ret = -1;
119e061bcd8SShuah Khan 		} else {
120d97b46a6SCyrill Gorcunov 			printf("PASS: 0 returned as expected\n");
121e061bcd8SShuah Khan 			ksft_inc_pass_cnt();
122e061bcd8SShuah Khan 		}
123d97b46a6SCyrill Gorcunov 
124d97b46a6SCyrill Gorcunov 		/* Compare with self */
125d97b46a6SCyrill Gorcunov 		ret = sys_kcmp(pid1, pid1, KCMP_VM, 0, 0);
126d97b46a6SCyrill Gorcunov 		if (ret) {
1276e7e6c34SShuah Khan 			printf("FAIL: 0 expected but %d returned (%s)\n",
1282bf1cbf1SDave Jones 				ret, strerror(errno));
129e061bcd8SShuah Khan 			ksft_inc_fail_cnt();
130d97b46a6SCyrill Gorcunov 			ret = -1;
131e061bcd8SShuah Khan 		} else {
132d97b46a6SCyrill Gorcunov 			printf("PASS: 0 returned as expected\n");
133e061bcd8SShuah Khan 			ksft_inc_pass_cnt();
134e061bcd8SShuah Khan 		}
135d97b46a6SCyrill Gorcunov 
136*66559a69SCyrill Gorcunov 		/* Compare epoll target */
137*66559a69SCyrill Gorcunov 		epoll_slot = (struct kcmp_epoll_slot) {
138*66559a69SCyrill Gorcunov 			.efd	= epollfd,
139*66559a69SCyrill Gorcunov 			.tfd	= duped_num,
140*66559a69SCyrill Gorcunov 			.toff	= 0,
141*66559a69SCyrill Gorcunov 		};
142*66559a69SCyrill Gorcunov 		ret = sys_kcmp(pid1, pid1, KCMP_EPOLL_TFD, pipefd[1],
143*66559a69SCyrill Gorcunov 			       (unsigned long)(void *)&epoll_slot);
144*66559a69SCyrill Gorcunov 		if (ret) {
145*66559a69SCyrill Gorcunov 			printf("FAIL: 0 expected but %d returned (%s)\n",
146*66559a69SCyrill Gorcunov 				ret, strerror(errno));
147*66559a69SCyrill Gorcunov 			ksft_inc_fail_cnt();
148*66559a69SCyrill Gorcunov 			ret = -1;
149*66559a69SCyrill Gorcunov 		} else {
150*66559a69SCyrill Gorcunov 			printf("PASS: 0 returned as expected\n");
151*66559a69SCyrill Gorcunov 			ksft_inc_pass_cnt();
152*66559a69SCyrill Gorcunov 		}
153*66559a69SCyrill Gorcunov 
154e061bcd8SShuah Khan 		ksft_print_cnts();
155e061bcd8SShuah Khan 
156e061bcd8SShuah Khan 		if (ret)
157e061bcd8SShuah Khan 			ksft_exit_fail();
158e061bcd8SShuah Khan 		else
159e061bcd8SShuah Khan 			ksft_exit_pass();
160d97b46a6SCyrill Gorcunov 	}
161d97b46a6SCyrill Gorcunov 
162d97b46a6SCyrill Gorcunov 	waitpid(pid2, &status, P_ALL);
163d97b46a6SCyrill Gorcunov 
164e061bcd8SShuah Khan 	return ksft_exit_pass();
165d97b46a6SCyrill Gorcunov }
166