PipeImpl_POSIX.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. //
  2. // PipeImpl_POSIX.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/PipeImpl_POSIX.cpp#1 $
  5. //
  6. // Library: Foundation
  7. // Package: Processes
  8. // Module: PipeImpl
  9. //
  10. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #include "Poco/PipeImpl_POSIX.h"
  16. #include "Poco/Exception.h"
  17. #include <sys/types.h>
  18. #include <unistd.h>
  19. #include <errno.h>
  20. namespace Poco {
  21. PipeImpl::PipeImpl()
  22. {
  23. int fds[2];
  24. int rc = pipe(fds);
  25. if (rc == 0)
  26. {
  27. _readfd = fds[0];
  28. _writefd = fds[1];
  29. }
  30. else throw CreateFileException("anonymous pipe");
  31. }
  32. PipeImpl::~PipeImpl()
  33. {
  34. closeRead();
  35. closeWrite();
  36. }
  37. int PipeImpl::writeBytes(const void* buffer, int length)
  38. {
  39. poco_assert (_writefd != -1);
  40. int n;
  41. do
  42. {
  43. n = write(_writefd, buffer, length);
  44. }
  45. while (n < 0 && errno == EINTR);
  46. if (n >= 0)
  47. return n;
  48. else
  49. throw WriteFileException("anonymous pipe");
  50. }
  51. int PipeImpl::readBytes(void* buffer, int length)
  52. {
  53. poco_assert (_readfd != -1);
  54. int n;
  55. do
  56. {
  57. n = read(_readfd, buffer, length);
  58. }
  59. while (n < 0 && errno == EINTR);
  60. if (n >= 0)
  61. return n;
  62. else
  63. throw ReadFileException("anonymous pipe");
  64. }
  65. PipeImpl::Handle PipeImpl::readHandle() const
  66. {
  67. return _readfd;
  68. }
  69. PipeImpl::Handle PipeImpl::writeHandle() const
  70. {
  71. return _writefd;
  72. }
  73. void PipeImpl::closeRead()
  74. {
  75. if (_readfd != -1)
  76. {
  77. close(_readfd);
  78. _readfd = -1;
  79. }
  80. }
  81. void PipeImpl::closeWrite()
  82. {
  83. if (_writefd != -1)
  84. {
  85. close(_writefd);
  86. _writefd = -1;
  87. }
  88. }
  89. } // namespace Poco