kcmp_test.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. #include "../kselftest.h"
  17. static long sys_kcmp(int pid1, int pid2, int type, int fd1, int fd2)
  18. {
  19. return syscall(__NR_kcmp, pid1, pid2, type, fd1, fd2);
  20. }
  21. int main(int argc, char **argv)
  22. {
  23. const char kpath[] = "kcmp-test-file";
  24. int pid1, pid2;
  25. int fd1, fd2;
  26. int status;
  27. fd1 = open(kpath, O_RDWR | O_CREAT | O_TRUNC, 0644);
  28. pid1 = getpid();
  29. if (fd1 < 0) {
  30. perror("Can't create file");
  31. ksft_exit_fail();
  32. }
  33. pid2 = fork();
  34. if (pid2 < 0) {
  35. perror("fork failed");
  36. ksft_exit_fail();
  37. }
  38. if (!pid2) {
  39. int pid2 = getpid();
  40. int ret;
  41. fd2 = open(kpath, O_RDWR, 0644);
  42. if (fd2 < 0) {
  43. perror("Can't open file");
  44. ksft_exit_fail();
  45. }
  46. /* An example of output and arguments */
  47. printf("pid1: %6d pid2: %6d FD: %2ld FILES: %2ld VM: %2ld "
  48. "FS: %2ld SIGHAND: %2ld IO: %2ld SYSVSEM: %2ld "
  49. "INV: %2ld\n",
  50. pid1, pid2,
  51. sys_kcmp(pid1, pid2, KCMP_FILE, fd1, fd2),
  52. sys_kcmp(pid1, pid2, KCMP_FILES, 0, 0),
  53. sys_kcmp(pid1, pid2, KCMP_VM, 0, 0),
  54. sys_kcmp(pid1, pid2, KCMP_FS, 0, 0),
  55. sys_kcmp(pid1, pid2, KCMP_SIGHAND, 0, 0),
  56. sys_kcmp(pid1, pid2, KCMP_IO, 0, 0),
  57. sys_kcmp(pid1, pid2, KCMP_SYSVSEM, 0, 0),
  58. /* This one should fail */
  59. sys_kcmp(pid1, pid2, KCMP_TYPES + 1, 0, 0));
  60. /* This one should return same fd */
  61. ret = sys_kcmp(pid1, pid2, KCMP_FILE, fd1, fd1);
  62. if (ret) {
  63. printf("FAIL: 0 expected but %d returned (%s)\n",
  64. ret, strerror(errno));
  65. ksft_inc_fail_cnt();
  66. ret = -1;
  67. } else {
  68. printf("PASS: 0 returned as expected\n");
  69. ksft_inc_pass_cnt();
  70. }
  71. /* Compare with self */
  72. ret = sys_kcmp(pid1, pid1, KCMP_VM, 0, 0);
  73. if (ret) {
  74. printf("FAIL: 0 expected but %d returned (%s)\n",
  75. ret, strerror(errno));
  76. ksft_inc_fail_cnt();
  77. ret = -1;
  78. } else {
  79. printf("PASS: 0 returned as expected\n");
  80. ksft_inc_pass_cnt();
  81. }
  82. ksft_print_cnts();
  83. if (ret)
  84. ksft_exit_fail();
  85. else
  86. ksft_exit_pass();
  87. }
  88. waitpid(pid2, &status, P_ALL);
  89. return ksft_exit_pass();
  90. }