1 // SPDX-License-Identifier: GPL-2.0
12 #include <linux/unistd.h>
13 #include <linux/kcmp.h>
15 #include <sys/syscall.h>
16 #include <sys/types.h>
19 #include <sys/epoll.h>
21 #include "../kselftest.h"
23 static long sys_kcmp(int pid1
, int pid2
, int type
, unsigned long fd1
, unsigned long fd2
)
25 return syscall(__NR_kcmp
, pid1
, pid2
, type
, fd1
, fd2
);
28 static const unsigned int duped_num
= 64;
30 int main(int argc
, char **argv
)
32 const char kpath
[] = "kcmp-test-file";
33 struct kcmp_epoll_slot epoll_slot
;
34 struct epoll_event ev
;
42 fd1
= open(kpath
, O_RDWR
| O_CREAT
| O_TRUNC
, 0644);
46 perror("Can't create file");
51 perror("Can't create pipe");
55 epollfd
= epoll_create1(0);
57 perror("epoll_create1 failed");
61 memset(&ev
, 0xff, sizeof(ev
));
62 ev
.events
= EPOLLIN
| EPOLLOUT
;
64 if (epoll_ctl(epollfd
, EPOLL_CTL_ADD
, pipefd
[0], &ev
)) {
65 perror("epoll_ctl failed");
69 fddup
= dup2(pipefd
[1], duped_num
);
71 perror("dup2 failed");
75 if (epoll_ctl(epollfd
, EPOLL_CTL_ADD
, fddup
, &ev
)) {
76 perror("epoll_ctl failed");
83 perror("fork failed");
91 fd2
= open(kpath
, O_RDWR
, 0644);
93 perror("Can't open file");
97 /* An example of output and arguments */
98 printf("pid1: %6d pid2: %6d FD: %2ld FILES: %2ld VM: %2ld "
99 "FS: %2ld SIGHAND: %2ld IO: %2ld SYSVSEM: %2ld "
102 sys_kcmp(pid1
, pid2
, KCMP_FILE
, fd1
, fd2
),
103 sys_kcmp(pid1
, pid2
, KCMP_FILES
, 0, 0),
104 sys_kcmp(pid1
, pid2
, KCMP_VM
, 0, 0),
105 sys_kcmp(pid1
, pid2
, KCMP_FS
, 0, 0),
106 sys_kcmp(pid1
, pid2
, KCMP_SIGHAND
, 0, 0),
107 sys_kcmp(pid1
, pid2
, KCMP_IO
, 0, 0),
108 sys_kcmp(pid1
, pid2
, KCMP_SYSVSEM
, 0, 0),
110 /* This one should fail */
111 sys_kcmp(pid1
, pid2
, KCMP_TYPES
+ 1, 0, 0));
113 /* This one should return same fd */
114 ret
= sys_kcmp(pid1
, pid2
, KCMP_FILE
, fd1
, fd1
);
116 printf("FAIL: 0 expected but %d returned (%s)\n",
117 ret
, strerror(errno
));
121 printf("PASS: 0 returned as expected\n");
125 /* Compare with self */
126 ret
= sys_kcmp(pid1
, pid1
, KCMP_VM
, 0, 0);
128 printf("FAIL: 0 expected but %d returned (%s)\n",
129 ret
, strerror(errno
));
133 printf("PASS: 0 returned as expected\n");
137 /* Compare epoll target */
138 epoll_slot
= (struct kcmp_epoll_slot
) {
143 ret
= sys_kcmp(pid1
, pid1
, KCMP_EPOLL_TFD
, pipefd
[1],
144 (unsigned long)(void *)&epoll_slot
);
146 printf("FAIL: 0 expected but %d returned (%s)\n",
147 ret
, strerror(errno
));
151 printf("PASS: 0 returned as expected\n");
163 waitpid(pid2
, &status
, P_ALL
);
165 return ksft_exit_pass();