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