Branch data Line data Source code
1 : : /* glib-unixprivate.h - Unix specific integration private functions
2 : : *
3 : : * SPDX-License-Identifier: LGPL-2.1-or-later
4 : : *
5 : : * This library is free software; you can redistribute it and/or
6 : : * modify it under the terms of the GNU Lesser General Public
7 : : * License as published by the Free Software Foundation; either
8 : : * version 2.1 of the License, or (at your option) any later version.
9 : : *
10 : : * This library is distributed in the hope that it will be useful,
11 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 : : * Lesser General Public License for more details.
14 : : *
15 : : * You should have received a copy of the GNU Lesser General Public License
16 : : * along with this library; if not, see <http://www.gnu.org/licenses/>.
17 : : */
18 : :
19 : : #ifndef __G_UNIXPRIVATE_H__
20 : : #define __G_UNIXPRIVATE_H__
21 : :
22 : : #include "config.h"
23 : :
24 : : #ifndef G_OS_UNIX
25 : : #error "This header may only be used on UNIX"
26 : : #endif
27 : :
28 : : /* To make bionic export pipe2() */
29 : : #ifndef _GNU_SOURCE
30 : : #define _GNU_SOURCE 1
31 : : #endif
32 : :
33 : : #include "gmacros.h"
34 : : #include "gtypes.h"
35 : :
36 : : #include <errno.h>
37 : : #include <fcntl.h>
38 : : #include <unistd.h>
39 : :
40 : : G_BEGIN_DECLS
41 : :
42 : : static inline gboolean
43 : 2899 : g_unix_open_pipe_internal (int *fds,
44 : : gboolean close_on_exec,
45 : : gboolean nonblock)
46 : : {
47 : : #ifdef HAVE_PIPE2
48 : : do
49 : : {
50 : : int ecode;
51 : 2899 : int flags = 0;
52 : :
53 : 2899 : if (close_on_exec)
54 : 810 : flags |= O_CLOEXEC;
55 : 2899 : if (nonblock)
56 : 56 : flags |= O_NONBLOCK;
57 : :
58 : : /* Atomic */
59 : 2899 : ecode = pipe2 (fds, flags);
60 : 2899 : if (ecode == -1 && errno != ENOSYS)
61 : 0 : return FALSE;
62 : 2899 : else if (ecode == 0)
63 : 2899 : return TRUE;
64 : : /* Fall through on -ENOSYS, we must be running on an old kernel */
65 : : }
66 : : while (FALSE);
67 : : #endif
68 : :
69 : 0 : if (pipe (fds) == -1)
70 : 0 : return FALSE;
71 : :
72 : 0 : if (close_on_exec)
73 : : {
74 : 0 : if (fcntl (fds[0], F_SETFD, FD_CLOEXEC) == -1 ||
75 : 0 : fcntl (fds[1], F_SETFD, FD_CLOEXEC) == -1)
76 : : {
77 : 0 : int saved_errno = errno;
78 : :
79 : 0 : close (fds[0]);
80 : 0 : close (fds[1]);
81 : 0 : fds[0] = -1;
82 : 0 : fds[1] = -1;
83 : :
84 : 0 : errno = saved_errno;
85 : 0 : return FALSE;
86 : : }
87 : : }
88 : :
89 : 0 : if (nonblock)
90 : : {
91 : : #ifdef O_NONBLOCK
92 : 0 : int flags = O_NONBLOCK;
93 : : #else
94 : : int flags = O_NDELAY;
95 : : #endif
96 : :
97 : 0 : if (fcntl (fds[0], F_SETFL, flags) == -1 ||
98 : 0 : fcntl (fds[1], F_SETFL, flags) == -1)
99 : : {
100 : 0 : int saved_errno = errno;
101 : :
102 : 0 : close (fds[0]);
103 : 0 : close (fds[1]);
104 : 0 : fds[0] = -1;
105 : 0 : fds[1] = -1;
106 : :
107 : 0 : errno = saved_errno;
108 : 0 : return FALSE;
109 : : }
110 : : }
111 : :
112 : 0 : return TRUE;
113 : : }
114 : :
115 : : G_END_DECLS
116 : :
117 : : #endif /* __G_UNIXPRIVATE_H__ */
|