kcmp_test.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 (%s)\n",
  63. ret, strerror(errno));
  64. ret = -1;
  65. } else
  66. printf("PASS: 0 returned as expected\n");
  67. /* Compare with self */
  68. ret = sys_kcmp(pid1, pid1, KCMP_VM, 0, 0);
  69. if (ret) {
  70. printf("FAIL: 0 expected but %li returned (%s)\n",
  71. ret, strerror(errno));
  72. ret = -1;
  73. } else
  74. printf("PASS: 0 returned as expected\n");
  75. exit(ret);
  76. }
  77. waitpid(pid2, &status, P_ALL);
  78. return 0;
  79. }