tst-syscall6.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. char tmp[] = "/tmp/tst-preadv2-XXXXXX";
  12. int fd;
  13. struct iovec iov[2];
  14. char *str0 = "hello ";
  15. char *str1 = "world\n";
  16. char input[16];
  17. int nio;
  18. fd = mkstemp (tmp);
  19. if (fd == -1) {
  20. puts ("mkstemp failed");
  21. return 1;
  22. }
  23. iov[0].iov_base = str0;
  24. iov[0].iov_len = strlen(str0);
  25. iov[1].iov_base = str1;
  26. iov[1].iov_len = strlen(str1) + 1; /* null terminator */
  27. nio = syscall(SYS_pwritev2, fd, iov, 2, 0, 0, RWF_DSYNC);
  28. if (nio <= 0) {
  29. puts ("failed to write to fd");
  30. return 1;
  31. }
  32. /* Read in the second string into the first buffer */
  33. iov[0].iov_base = input;
  34. iov[0].iov_len = strlen(str1) + 1; /* null terminator */
  35. nio = syscall(SYS_preadv2, fd, iov, 1, strlen(str0), 0, RWF_HIPRI);
  36. if (nio <= 0) {
  37. printf ("failed to read fd %d\n", nio);
  38. return 1;
  39. }
  40. if (strncmp(iov[0].iov_base, iov[1].iov_base, strlen(str1)) == 0)
  41. printf ("syscall(SYS_preadv2) read %s", (char *) iov[0].iov_base);
  42. if (close(fd) != 0) {
  43. puts ("failed to close read fd");
  44. return 1;
  45. }
  46. if (unlink(tmp) != 0) {
  47. puts ("failed to unlink file");
  48. return 1;
  49. }
  50. return 0;
  51. }