kcmp_test.c 2.0 KB

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