summaryrefslogtreecommitdiff
path: root/src/unique_pipe.cc
blob: 3ab187194fb8e46d426b0055517811746695717b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include "common.hh"

#include "io.hh"
#include "unique_pipe.hh"

#include <fcntl.h>
#include <unistd.h>

unique_pipe::unique_pipe(bool non_blocking_reader, bool non_blocking_writer) {
  int fd[2];
#if HAVE_PIPE2
  if (non_blocking_reader == non_blocking_writer) {
    if (pipe2(fd, non_blocking_reader ? O_NONBLOCK : 0) == 0) {
      fd_[0] = unique_fd(fd[0]);
      fd_[1] = unique_fd(fd[1]);
    }
    return;
  }
#endif
  if (pipe(fd))
    return;
  if (non_blocking_reader)
    io::make_nonblocking(fd[0]);
  if (non_blocking_writer)
    io::make_nonblocking(fd[1]);
  fd_[0] = unique_fd(fd[0]);
  fd_[1] = unique_fd(fd[1]);
}

unique_pipe::unique_pipe(unique_pipe&& fd) noexcept {
  fd_[0] = unique_fd(fd.fd_[0].release());
  fd_[1] = unique_fd(fd.fd_[1].release());
}

void unique_pipe::reset() {
  fd_[0].reset();
  fd_[1].reset();
}

unique_fd unique_pipe::release_reader() {
  return std::move(fd_[0]);
}

unique_fd unique_pipe::release_writer() {
  return std::move(fd_[1]);
}