1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2d97b46a6SCyrill Gorcunov #define _GNU_SOURCE
3d97b46a6SCyrill Gorcunov
4d97b46a6SCyrill Gorcunov #include <stdio.h>
5d97b46a6SCyrill Gorcunov #include <stdlib.h>
6d97b46a6SCyrill Gorcunov #include <signal.h>
7d97b46a6SCyrill Gorcunov #include <limits.h>
8d97b46a6SCyrill Gorcunov #include <unistd.h>
9d97b46a6SCyrill Gorcunov #include <errno.h>
10d97b46a6SCyrill Gorcunov #include <string.h>
11d97b46a6SCyrill Gorcunov #include <fcntl.h>
12d97b46a6SCyrill Gorcunov #include <linux/unistd.h>
13d97b46a6SCyrill Gorcunov #include <linux/kcmp.h>
14d97b46a6SCyrill Gorcunov
15d97b46a6SCyrill Gorcunov #include <sys/syscall.h>
16d97b46a6SCyrill Gorcunov #include <sys/types.h>
17d97b46a6SCyrill Gorcunov #include <sys/stat.h>
18d97b46a6SCyrill Gorcunov #include <sys/wait.h>
1966559a69SCyrill Gorcunov #include <sys/epoll.h>
20d97b46a6SCyrill Gorcunov
21e061bcd8SShuah Khan #include "../kselftest.h"
22e061bcd8SShuah Khan
sys_kcmp(int pid1,int pid2,int type,unsigned long fd1,unsigned long fd2)2366559a69SCyrill Gorcunov static long sys_kcmp(int pid1, int pid2, int type, unsigned long fd1, unsigned long fd2)
24d97b46a6SCyrill Gorcunov {
25d97b46a6SCyrill Gorcunov return syscall(__NR_kcmp, pid1, pid2, type, fd1, fd2);
26d97b46a6SCyrill Gorcunov }
27d97b46a6SCyrill Gorcunov
2866559a69SCyrill Gorcunov static const unsigned int duped_num = 64;
2966559a69SCyrill Gorcunov
main(int argc,char ** argv)30d97b46a6SCyrill Gorcunov int main(int argc, char **argv)
31d97b46a6SCyrill Gorcunov {
32d97b46a6SCyrill Gorcunov const char kpath[] = "kcmp-test-file";
3366559a69SCyrill Gorcunov struct kcmp_epoll_slot epoll_slot;
3466559a69SCyrill Gorcunov struct epoll_event ev;
35d97b46a6SCyrill Gorcunov int pid1, pid2;
3666559a69SCyrill Gorcunov int pipefd[2];
37d97b46a6SCyrill Gorcunov int fd1, fd2;
3866559a69SCyrill Gorcunov int epollfd;
39d97b46a6SCyrill Gorcunov int status;
4066559a69SCyrill Gorcunov int fddup;
41d97b46a6SCyrill Gorcunov
42d97b46a6SCyrill Gorcunov fd1 = open(kpath, O_RDWR | O_CREAT | O_TRUNC, 0644);
43d97b46a6SCyrill Gorcunov pid1 = getpid();
44d97b46a6SCyrill Gorcunov
45d97b46a6SCyrill Gorcunov if (fd1 < 0) {
46d97b46a6SCyrill Gorcunov perror("Can't create file");
47e061bcd8SShuah Khan ksft_exit_fail();
48d97b46a6SCyrill Gorcunov }
49d97b46a6SCyrill Gorcunov
5066559a69SCyrill Gorcunov if (pipe(pipefd)) {
5166559a69SCyrill Gorcunov perror("Can't create pipe");
5266559a69SCyrill Gorcunov ksft_exit_fail();
5366559a69SCyrill Gorcunov }
5466559a69SCyrill Gorcunov
5566559a69SCyrill Gorcunov epollfd = epoll_create1(0);
5666559a69SCyrill Gorcunov if (epollfd < 0) {
5766559a69SCyrill Gorcunov perror("epoll_create1 failed");
5866559a69SCyrill Gorcunov ksft_exit_fail();
5966559a69SCyrill Gorcunov }
6066559a69SCyrill Gorcunov
6166559a69SCyrill Gorcunov memset(&ev, 0xff, sizeof(ev));
6266559a69SCyrill Gorcunov ev.events = EPOLLIN | EPOLLOUT;
6366559a69SCyrill Gorcunov
6466559a69SCyrill Gorcunov if (epoll_ctl(epollfd, EPOLL_CTL_ADD, pipefd[0], &ev)) {
6566559a69SCyrill Gorcunov perror("epoll_ctl failed");
6666559a69SCyrill Gorcunov ksft_exit_fail();
6766559a69SCyrill Gorcunov }
6866559a69SCyrill Gorcunov
6966559a69SCyrill Gorcunov fddup = dup2(pipefd[1], duped_num);
7066559a69SCyrill Gorcunov if (fddup < 0) {
7166559a69SCyrill Gorcunov perror("dup2 failed");
7266559a69SCyrill Gorcunov ksft_exit_fail();
7366559a69SCyrill Gorcunov }
7466559a69SCyrill Gorcunov
7566559a69SCyrill Gorcunov if (epoll_ctl(epollfd, EPOLL_CTL_ADD, fddup, &ev)) {
7666559a69SCyrill Gorcunov perror("epoll_ctl failed");
7766559a69SCyrill Gorcunov ksft_exit_fail();
7866559a69SCyrill Gorcunov }
7966559a69SCyrill Gorcunov close(fddup);
8066559a69SCyrill Gorcunov
81d97b46a6SCyrill Gorcunov pid2 = fork();
82d97b46a6SCyrill Gorcunov if (pid2 < 0) {
83d97b46a6SCyrill Gorcunov perror("fork failed");
84e061bcd8SShuah Khan ksft_exit_fail();
85d97b46a6SCyrill Gorcunov }
86d97b46a6SCyrill Gorcunov
87d97b46a6SCyrill Gorcunov if (!pid2) {
88d97b46a6SCyrill Gorcunov int pid2 = getpid();
89d97b46a6SCyrill Gorcunov int ret;
90d97b46a6SCyrill Gorcunov
91ff682226SGautam Menghani ksft_print_header();
92ff682226SGautam Menghani ksft_set_plan(3);
93ff682226SGautam Menghani
94*382494aaSEdward Liaw fd2 = open(kpath, O_RDWR);
95d97b46a6SCyrill Gorcunov if (fd2 < 0) {
96d97b46a6SCyrill Gorcunov perror("Can't open file");
97e061bcd8SShuah Khan ksft_exit_fail();
98d97b46a6SCyrill Gorcunov }
99d97b46a6SCyrill Gorcunov
100d97b46a6SCyrill Gorcunov /* An example of output and arguments */
101d97b46a6SCyrill Gorcunov printf("pid1: %6d pid2: %6d FD: %2ld FILES: %2ld VM: %2ld "
102d97b46a6SCyrill Gorcunov "FS: %2ld SIGHAND: %2ld IO: %2ld SYSVSEM: %2ld "
103d97b46a6SCyrill Gorcunov "INV: %2ld\n",
104d97b46a6SCyrill Gorcunov pid1, pid2,
105d97b46a6SCyrill Gorcunov sys_kcmp(pid1, pid2, KCMP_FILE, fd1, fd2),
106d97b46a6SCyrill Gorcunov sys_kcmp(pid1, pid2, KCMP_FILES, 0, 0),
107d97b46a6SCyrill Gorcunov sys_kcmp(pid1, pid2, KCMP_VM, 0, 0),
108d97b46a6SCyrill Gorcunov sys_kcmp(pid1, pid2, KCMP_FS, 0, 0),
109d97b46a6SCyrill Gorcunov sys_kcmp(pid1, pid2, KCMP_SIGHAND, 0, 0),
110d97b46a6SCyrill Gorcunov sys_kcmp(pid1, pid2, KCMP_IO, 0, 0),
111d97b46a6SCyrill Gorcunov sys_kcmp(pid1, pid2, KCMP_SYSVSEM, 0, 0),
112d97b46a6SCyrill Gorcunov
113d97b46a6SCyrill Gorcunov /* This one should fail */
114d97b46a6SCyrill Gorcunov sys_kcmp(pid1, pid2, KCMP_TYPES + 1, 0, 0));
115d97b46a6SCyrill Gorcunov
116d97b46a6SCyrill Gorcunov /* This one should return same fd */
117d97b46a6SCyrill Gorcunov ret = sys_kcmp(pid1, pid2, KCMP_FILE, fd1, fd1);
118d97b46a6SCyrill Gorcunov if (ret) {
1192bf1cbf1SDave Jones printf("FAIL: 0 expected but %d returned (%s)\n",
1202bf1cbf1SDave Jones ret, strerror(errno));
121e061bcd8SShuah Khan ksft_inc_fail_cnt();
122d97b46a6SCyrill Gorcunov ret = -1;
123e061bcd8SShuah Khan } else {
124d97b46a6SCyrill Gorcunov printf("PASS: 0 returned as expected\n");
125e061bcd8SShuah Khan ksft_inc_pass_cnt();
126e061bcd8SShuah Khan }
127d97b46a6SCyrill Gorcunov
128d97b46a6SCyrill Gorcunov /* Compare with self */
129d97b46a6SCyrill Gorcunov ret = sys_kcmp(pid1, pid1, KCMP_VM, 0, 0);
130d97b46a6SCyrill Gorcunov if (ret) {
1316e7e6c34SShuah Khan printf("FAIL: 0 expected but %d returned (%s)\n",
1322bf1cbf1SDave Jones ret, strerror(errno));
133e061bcd8SShuah Khan ksft_inc_fail_cnt();
134d97b46a6SCyrill Gorcunov ret = -1;
135e061bcd8SShuah Khan } else {
136d97b46a6SCyrill Gorcunov printf("PASS: 0 returned as expected\n");
137e061bcd8SShuah Khan ksft_inc_pass_cnt();
138e061bcd8SShuah Khan }
139d97b46a6SCyrill Gorcunov
14066559a69SCyrill Gorcunov /* Compare epoll target */
14166559a69SCyrill Gorcunov epoll_slot = (struct kcmp_epoll_slot) {
14266559a69SCyrill Gorcunov .efd = epollfd,
14366559a69SCyrill Gorcunov .tfd = duped_num,
14466559a69SCyrill Gorcunov .toff = 0,
14566559a69SCyrill Gorcunov };
14666559a69SCyrill Gorcunov ret = sys_kcmp(pid1, pid1, KCMP_EPOLL_TFD, pipefd[1],
14766559a69SCyrill Gorcunov (unsigned long)(void *)&epoll_slot);
14866559a69SCyrill Gorcunov if (ret) {
14966559a69SCyrill Gorcunov printf("FAIL: 0 expected but %d returned (%s)\n",
15066559a69SCyrill Gorcunov ret, strerror(errno));
15166559a69SCyrill Gorcunov ksft_inc_fail_cnt();
15266559a69SCyrill Gorcunov ret = -1;
15366559a69SCyrill Gorcunov } else {
15466559a69SCyrill Gorcunov printf("PASS: 0 returned as expected\n");
15566559a69SCyrill Gorcunov ksft_inc_pass_cnt();
15666559a69SCyrill Gorcunov }
15766559a69SCyrill Gorcunov
158e061bcd8SShuah Khan
159e061bcd8SShuah Khan if (ret)
160e061bcd8SShuah Khan ksft_exit_fail();
161e061bcd8SShuah Khan else
162e061bcd8SShuah Khan ksft_exit_pass();
163d97b46a6SCyrill Gorcunov }
164d97b46a6SCyrill Gorcunov
165d97b46a6SCyrill Gorcunov waitpid(pid2, &status, P_ALL);
166d97b46a6SCyrill Gorcunov
167ff682226SGautam Menghani return 0;
168d97b46a6SCyrill Gorcunov }
169