Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / 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 "include/compat.h"
15
16 #include <errno.h>
17 #include <fcntl.h>
18 #include <unistd.h>
19
20 int pipe_cloexec(int pipefd[2])
21 {
22         int ret;
23
24 #if defined(HAVE_PIPE2) && defined(O_CLOEXEC)
25         ret = pipe2(pipefd, O_CLOEXEC);
26         if (ret == -1)
27                 return -errno;
28         return 0;
29 #else
30         ret = pipe(pipefd);
31         if (ret == -1)
32                 return -errno;
33
34         /*
35          * The old-fashioned, race-condition prone way that we have to fall
36          * back on if O_CLOEXEC does not exist.
37          */
38         ret = fcntl(pipefd[0], F_SETFD, FD_CLOEXEC);
39         if (ret == -1) {
40                 ret = -errno;
41                 goto out;
42         }
43
44         ret = fcntl(pipefd[1], F_SETFD, FD_CLOEXEC);
45         if (ret == -1) {
46                 ret = -errno;
47                 goto out;
48         }
49
50         return 0;
51
52 out:
53         VOID_TEMP_FAILURE_RETRY(close(pipefd[0]));
54         VOID_TEMP_FAILURE_RETRY(close(pipefd[1]));
55
56         return ret;
57 #endif
58 }