ioall.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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(const 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. if (fl & O_NONBLOCK)
  36. return;
  37. fcntl(fd, F_SETFL, fl | O_NONBLOCK);
  38. }
  39. void set_block(int fd)
  40. {
  41. int fl = fcntl(fd, F_GETFL, 0);
  42. if (!(fl & O_NONBLOCK))
  43. return;
  44. fcntl(fd, F_SETFL, fl & ~O_NONBLOCK);
  45. }
  46. int write_all(int fd, const void *buf, int size)
  47. {
  48. int written = 0;
  49. int ret;
  50. while (written < size) {
  51. ret = write(fd, (char *) buf + written, size - written);
  52. if (ret == -1 && errno == EINTR)
  53. continue;
  54. if (ret <= 0) {
  55. return 0;
  56. }
  57. written += ret;
  58. }
  59. // fprintf(stderr, "sent %d bytes\n", size);
  60. return 1;
  61. }
  62. int read_all(int fd, void *buf, int size)
  63. {
  64. int got_read = 0;
  65. int ret;
  66. while (got_read < size) {
  67. ret = read(fd, (char *) buf + got_read, size - got_read);
  68. if (ret == -1 && errno == EINTR)
  69. continue;
  70. if (ret == 0) {
  71. errno = 0;
  72. fprintf(stderr, "EOF\n");
  73. return 0;
  74. }
  75. if (ret < 0) {
  76. if (errno != EAGAIN)
  77. perror_wrapper("read");
  78. return 0;
  79. }
  80. if (got_read == 0) {
  81. // force blocking operation on further reads
  82. set_block(fd);
  83. }
  84. got_read += ret;
  85. }
  86. // fprintf(stderr, "read %d bytes\n", size);
  87. return 1;
  88. }
  89. int copy_fd_all(int fdout, int fdin)
  90. {
  91. int ret;
  92. char buf[4096];
  93. for (;;) {
  94. ret = read(fdin, buf, sizeof(buf));
  95. if (ret == -1 && errno == EINTR)
  96. continue;
  97. if (!ret)
  98. break;
  99. if (ret < 0) {
  100. perror_wrapper("read");
  101. return 0;
  102. }
  103. if (!write_all(fdout, buf, ret)) {
  104. perror_wrapper("write");
  105. return 0;
  106. }
  107. }
  108. return 1;
  109. }