summaryrefslogtreecommitdiffstats
path: root/Source/cmGetPipes.cxx
blob: a5b64692ed1b807b615f1dc8982ea380b086a65f (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
48
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file Copyright.txt or https://cmake.org/licensing for details.  */
#include "cmGetPipes.h"

#include <cm3p/uv.h>
#include <fcntl.h>

#if defined(_WIN32) && !defined(__CYGWIN__)
#  include <io.h>

int cmGetPipes(int* fds)
{
  SECURITY_ATTRIBUTES attr;
  HANDLE readh, writeh;
  attr.nLength = sizeof(attr);
  attr.lpSecurityDescriptor = nullptr;
  attr.bInheritHandle = FALSE;
  if (!CreatePipe(&readh, &writeh, &attr, 0))
    return uv_translate_sys_error(GetLastError());
  fds[0] = _open_osfhandle((intptr_t)readh, 0);
  fds[1] = _open_osfhandle((intptr_t)writeh, 0);
  if (fds[0] == -1 || fds[1] == -1) {
    CloseHandle(readh);
    CloseHandle(writeh);
    return uv_translate_sys_error(GetLastError());
  }
  return 0;
}
#else
#  include <cerrno>

#  include <unistd.h>

int cmGetPipes(int* fds)
{
  if (pipe(fds) == -1) {
    return uv_translate_sys_error(errno);
  }

  if (fcntl(fds[0], F_SETFD, FD_CLOEXEC) == -1 ||
      fcntl(fds[1], F_SETFD, FD_CLOEXEC) == -1) {
    close(fds[0]);
    close(fds[1]);
    return uv_translate_sys_error(errno);
  }
  return 0;
}
#endif