Fix getauxval error at qemu
[platform/upstream/glib.git] / glib / glib-unixprivate.h
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 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       int flags = 0;
52
53       if (close_on_exec)
54         flags |= O_CLOEXEC;
55       if (nonblock)
56         flags |= O_NONBLOCK;
57
58       /* Atomic */
59       ecode = pipe2 (fds, flags);
60       if (ecode == -1 && errno != ENOSYS)
61         return FALSE;
62       else if (ecode == 0)
63         return TRUE;
64       /* Fall through on -ENOSYS, we must be running on an old kernel */
65     }
66   while (FALSE);
67 #endif
68
69   if (pipe (fds) == -1)
70     return FALSE;
71
72   if (close_on_exec)
73     {
74       if (fcntl (fds[0], F_SETFD, FD_CLOEXEC) == -1 ||
75           fcntl (fds[1], F_SETFD, FD_CLOEXEC) == -1)
76         {
77           int saved_errno = errno;
78
79           close (fds[0]);
80           close (fds[1]);
81           fds[0] = -1;
82           fds[1] = -1;
83
84           errno = saved_errno;
85           return FALSE;
86         }
87     }
88
89   if (nonblock)
90     {
91 #ifdef O_NONBLOCK
92       int flags = O_NONBLOCK;
93 #else
94       int flags = O_NDELAY;
95 #endif
96
97       if (fcntl (fds[0], F_SETFL, flags) == -1 ||
98           fcntl (fds[1], F_SETFL, flags) == -1)
99         {
100           int saved_errno = errno;
101
102           close (fds[0]);
103           close (fds[1]);
104           fds[0] = -1;
105           fds[1] = -1;
106
107           errno = saved_errno;
108           return FALSE;
109         }
110     }
111
112   return TRUE;
113 }
114
115 G_END_DECLS
116
117 #endif  /* __G_UNIXPRIVATE_H__ */