]> git.proxmox.com Git - ceph.git/blob - ceph/src/common/pipe.c
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / common / pipe.c
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2011 New Dream Network
7 *
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
12 *
13 */
14 #include "acconfig.h"
15
16 #include "common/pipe.h"
17 #include "include/compat.h"
18
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <limits.h>
22 #include <stdint.h>
23 #include <unistd.h>
24
25 int pipe_cloexec(int pipefd[2])
26 {
27 int ret;
28
29 #if defined(HAVE_PIPE2) && defined(O_CLOEXEC)
30 ret = pipe2(pipefd, O_CLOEXEC);
31 if (ret == -1)
32 return -errno;
33 return 0;
34 #else
35 ret = pipe(pipefd);
36 if (ret == -1)
37 return -errno;
38
39 /*
40 * The old-fashioned, race-condition prone way that we have to fall
41 * back on if O_CLOEXEC does not exist.
42 */
43 ret = fcntl(pipefd[0], F_SETFD, FD_CLOEXEC);
44 if (ret == -1) {
45 ret = -errno;
46 goto out;
47 }
48
49 ret = fcntl(pipefd[1], F_SETFD, FD_CLOEXEC);
50 if (ret == -1) {
51 ret = -errno;
52 goto out;
53 }
54
55 return 0;
56
57 out:
58 VOID_TEMP_FAILURE_RETRY(close(pipefd[0]));
59 VOID_TEMP_FAILURE_RETRY(close(pipefd[1]));
60
61 return ret;
62 #endif
63 }