Merge tag 'locks-v3.16-2' of git://git.samba.org/jlayton/linux
[linux/fpc-iii.git] / tools / testing / selftests / kcmp / kcmp_test.c
blobfa4f1b37e0456eb2934f825f2e0e256449de4dad
1 #define _GNU_SOURCE
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <signal.h>
6 #include <limits.h>
7 #include <unistd.h>
8 #include <errno.h>
9 #include <string.h>
10 #include <fcntl.h>
12 #include <linux/unistd.h>
13 #include <linux/kcmp.h>
15 #include <sys/syscall.h>
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <sys/wait.h>
20 static long sys_kcmp(int pid1, int pid2, int type, int fd1, int fd2)
22 return syscall(__NR_kcmp, pid1, pid2, type, fd1, fd2);
25 int main(int argc, char **argv)
27 const char kpath[] = "kcmp-test-file";
28 int pid1, pid2;
29 int fd1, fd2;
30 int status;
32 fd1 = open(kpath, O_RDWR | O_CREAT | O_TRUNC, 0644);
33 pid1 = getpid();
35 if (fd1 < 0) {
36 perror("Can't create file");
37 exit(1);
40 pid2 = fork();
41 if (pid2 < 0) {
42 perror("fork failed");
43 exit(1);
46 if (!pid2) {
47 int pid2 = getpid();
48 int ret;
50 fd2 = open(kpath, O_RDWR, 0644);
51 if (fd2 < 0) {
52 perror("Can't open file");
53 exit(1);
56 /* An example of output and arguments */
57 printf("pid1: %6d pid2: %6d FD: %2ld FILES: %2ld VM: %2ld "
58 "FS: %2ld SIGHAND: %2ld IO: %2ld SYSVSEM: %2ld "
59 "INV: %2ld\n",
60 pid1, pid2,
61 sys_kcmp(pid1, pid2, KCMP_FILE, fd1, fd2),
62 sys_kcmp(pid1, pid2, KCMP_FILES, 0, 0),
63 sys_kcmp(pid1, pid2, KCMP_VM, 0, 0),
64 sys_kcmp(pid1, pid2, KCMP_FS, 0, 0),
65 sys_kcmp(pid1, pid2, KCMP_SIGHAND, 0, 0),
66 sys_kcmp(pid1, pid2, KCMP_IO, 0, 0),
67 sys_kcmp(pid1, pid2, KCMP_SYSVSEM, 0, 0),
69 /* This one should fail */
70 sys_kcmp(pid1, pid2, KCMP_TYPES + 1, 0, 0));
72 /* This one should return same fd */
73 ret = sys_kcmp(pid1, pid2, KCMP_FILE, fd1, fd1);
74 if (ret) {
75 printf("FAIL: 0 expected but %d returned (%s)\n",
76 ret, strerror(errno));
77 ret = -1;
78 } else
79 printf("PASS: 0 returned as expected\n");
81 /* Compare with self */
82 ret = sys_kcmp(pid1, pid1, KCMP_VM, 0, 0);
83 if (ret) {
84 printf("FAIL: 0 expected but %li returned (%s)\n",
85 ret, strerror(errno));
86 ret = -1;
87 } else
88 printf("PASS: 0 returned as expected\n");
90 exit(ret);
93 waitpid(pid2, &status, P_ALL);
95 return 0;