ioall.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * The Qubes OS Project, http://www.qubes-os.org
  3. *
  4. * Copyright (C) 2010 Rafal Wojtczuk <rafal@invisiblethingslab.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. *
  20. */
  21. #include <unistd.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <fcntl.h>
  25. #include <errno.h>
  26. void perror_wrapper(char * msg)
  27. {
  28. int prev=errno;
  29. perror(msg);
  30. errno=prev;
  31. }
  32. void set_nonblock(int fd)
  33. {
  34. int fl = fcntl(fd, F_GETFL, 0);
  35. fcntl(fd, F_SETFL, fl | O_NONBLOCK);
  36. }
  37. void set_block(int fd)
  38. {
  39. int fl = fcntl(fd, F_GETFL, 0);
  40. fcntl(fd, F_SETFL, fl & ~O_NONBLOCK);
  41. }
  42. int write_all(int fd, void *buf, int size)
  43. {
  44. int written = 0;
  45. int ret;
  46. while (written < size) {
  47. ret = write(fd, (char *) buf + written, size - written);
  48. if (ret == -1 && errno == EINTR)
  49. continue;
  50. if (ret <= 0) {
  51. return 0;
  52. }
  53. written += ret;
  54. }
  55. // fprintf(stderr, "sent %d bytes\n", size);
  56. return 1;
  57. }
  58. int read_all(int fd, void *buf, int size)
  59. {
  60. int got_read = 0;
  61. int ret;
  62. while (got_read < size) {
  63. ret = read(fd, (char *) buf + got_read, size - got_read);
  64. if (ret == -1 && errno == EINTR)
  65. continue;
  66. if (ret == 0) {
  67. errno = 0;
  68. fprintf(stderr, "EOF\n");
  69. return 0;
  70. }
  71. if (ret < 0) {
  72. if (errno != EAGAIN)
  73. perror_wrapper("read");
  74. return 0;
  75. }
  76. if (got_read == 0) {
  77. // force blocking operation on further reads
  78. set_block(fd);
  79. }
  80. got_read += ret;
  81. }
  82. // fprintf(stderr, "read %d bytes\n", size);
  83. return 1;
  84. }
  85. int copy_fd_all(int fdout, int fdin)
  86. {
  87. int ret;
  88. char buf[4096];
  89. for (;;) {
  90. ret = read(fdin, buf, sizeof(buf));
  91. if (ret == -1 && errno == EINTR)
  92. continue;
  93. if (!ret)
  94. break;
  95. if (ret < 0) {
  96. perror_wrapper("read");
  97. return 0;
  98. }
  99. if (!write_all(fdout, buf, ret)) {
  100. perror_wrapper("write");
  101. return 0;
  102. }
  103. }
  104. return 1;
  105. }