tst-syscall6.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <sys/syscall.h>
  6. #include <sys/uio.h>
  7. #include <sys/types.h>
  8. #include <linux/fs.h> /* for RWF_HIPRI */
  9. int main()
  10. {
  11. #if defined SYS_preadv2 && defined SYS_pwritev2 && defined RWF_HIPRI
  12. char tmp[] = "/tmp/tst-preadv2-XXXXXX";
  13. int fd;
  14. struct iovec iov[2];
  15. char *str0 = "hello ";
  16. char *str1 = "world\n";
  17. char input[16];
  18. int nio;
  19. fd = mkstemp (tmp);
  20. if (fd == -1) {
  21. puts ("mkstemp failed");
  22. return 1;
  23. }
  24. iov[0].iov_base = str0;
  25. iov[0].iov_len = strlen(str0);
  26. iov[1].iov_base = str1;
  27. iov[1].iov_len = strlen(str1) + 1; /* null terminator */
  28. nio = syscall(SYS_pwritev2, fd, iov, 2, 0, 0, RWF_DSYNC);
  29. if (nio <= 0) {
  30. puts ("failed to write to fd");
  31. return 1;
  32. }
  33. /* Read in the second string into the first buffer */
  34. iov[0].iov_base = input;
  35. iov[0].iov_len = strlen(str1) + 1; /* null terminator */
  36. nio = syscall(SYS_preadv2, fd, iov, 1, strlen(str0), 0, RWF_HIPRI);
  37. if (nio <= 0) {
  38. printf ("failed to read fd %d\n", nio);
  39. return 1;
  40. }
  41. if (strncmp(iov[0].iov_base, iov[1].iov_base, strlen(str1)) == 0)
  42. printf ("syscall(SYS_preadv2) read %s", (char *) iov[0].iov_base);
  43. if (close(fd) != 0) {
  44. puts ("failed to close read fd");
  45. return 1;
  46. }
  47. if (unlink(tmp) != 0) {
  48. puts ("failed to unlink file");
  49. return 1;
  50. }
  51. return 0;
  52. #else
  53. return 23;
  54. #endif
  55. }