Coverage Report

Created: 2025-07-12 07:23

/src/pango/subprojects/glib/glib/glib-unixprivate.h
Line
Count
Source (jump to first uncovered line)
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
0
  {
47
0
#ifdef HAVE_PIPE2
48
0
  do
49
0
    {
50
0
      int ecode;
51
0
      int flags = 0;
52
53
0
      if (close_on_exec)
54
0
        flags |= O_CLOEXEC;
55
0
      if (nonblock)
56
0
        flags |= O_NONBLOCK;
57
58
      /* Atomic */
59
0
      ecode = pipe2 (fds, flags);
60
0
      if (ecode == -1 && errno != ENOSYS)
61
0
        return FALSE;
62
0
      else if (ecode == 0)
63
0
        return TRUE;
64
      /* Fall through on -ENOSYS, we must be running on an old kernel */
65
0
    }
66
0
  while (FALSE);
67
0
#endif
68
69
0
  if (pipe (fds) == -1)
70
0
    return FALSE;
71
72
0
  if (close_on_exec)
73
0
    {
74
0
      if (fcntl (fds[0], F_SETFD, FD_CLOEXEC) == -1 ||
75
0
          fcntl (fds[1], F_SETFD, FD_CLOEXEC) == -1)
76
0
        {
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
0
        }
87
0
    }
88
89
0
  if (nonblock)
90
0
    {
91
0
#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
0
        {
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
0
        }
110
0
    }
111
112
0
  return TRUE;
113
0
}
114
115
G_END_DECLS
116
117
#endif  /* __G_UNIXPRIVATE_H__ */