xref: /netbsd-src/external/gpl3/gdb.old/dist/gdbsupport/event-pipe.cc (revision 6881a4007f077b54e5f51159c52b9b25f57deb0d)
1*6881a400Schristos /* Event pipe for GDB, the GNU debugger.
2*6881a400Schristos 
3*6881a400Schristos    Copyright (C) 2021-2023 Free Software Foundation, Inc.
4*6881a400Schristos 
5*6881a400Schristos    This file is part of GDB.
6*6881a400Schristos 
7*6881a400Schristos    This program is free software; you can redistribute it and/or modify
8*6881a400Schristos    it under the terms of the GNU General Public License as published by
9*6881a400Schristos    the Free Software Foundation; either version 3 of the License, or
10*6881a400Schristos    (at your option) any later version.
11*6881a400Schristos 
12*6881a400Schristos    This program is distributed in the hope that it will be useful,
13*6881a400Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
14*6881a400Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15*6881a400Schristos    GNU General Public License for more details.
16*6881a400Schristos 
17*6881a400Schristos    You should have received a copy of the GNU General Public License
18*6881a400Schristos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19*6881a400Schristos 
20*6881a400Schristos #include "gdbsupport/common-defs.h"
21*6881a400Schristos #include "gdbsupport/event-pipe.h"
22*6881a400Schristos #include "gdbsupport/filestuff.h"
23*6881a400Schristos 
24*6881a400Schristos #include <errno.h>
25*6881a400Schristos #include <fcntl.h>
26*6881a400Schristos #include <unistd.h>
27*6881a400Schristos 
28*6881a400Schristos event_pipe::~event_pipe ()
29*6881a400Schristos {
30*6881a400Schristos   if (is_open ())
31*6881a400Schristos     close_pipe ();
32*6881a400Schristos }
33*6881a400Schristos 
34*6881a400Schristos /* See event-pipe.h.  */
35*6881a400Schristos 
36*6881a400Schristos bool
37*6881a400Schristos event_pipe::open_pipe ()
38*6881a400Schristos {
39*6881a400Schristos   if (is_open ())
40*6881a400Schristos     return false;
41*6881a400Schristos 
42*6881a400Schristos   if (gdb_pipe_cloexec (m_fds) == -1)
43*6881a400Schristos     return false;
44*6881a400Schristos 
45*6881a400Schristos   if (fcntl (m_fds[0], F_SETFL, O_NONBLOCK) == -1
46*6881a400Schristos       || fcntl (m_fds[1], F_SETFL, O_NONBLOCK) == -1)
47*6881a400Schristos     {
48*6881a400Schristos       close_pipe ();
49*6881a400Schristos       return false;
50*6881a400Schristos     }
51*6881a400Schristos 
52*6881a400Schristos   return true;
53*6881a400Schristos }
54*6881a400Schristos 
55*6881a400Schristos /* See event-pipe.h.  */
56*6881a400Schristos 
57*6881a400Schristos void
58*6881a400Schristos event_pipe::close_pipe ()
59*6881a400Schristos {
60*6881a400Schristos   ::close (m_fds[0]);
61*6881a400Schristos   ::close (m_fds[1]);
62*6881a400Schristos   m_fds[0] = -1;
63*6881a400Schristos   m_fds[1] = -1;
64*6881a400Schristos }
65*6881a400Schristos 
66*6881a400Schristos /* See event-pipe.h.  */
67*6881a400Schristos 
68*6881a400Schristos void
69*6881a400Schristos event_pipe::flush ()
70*6881a400Schristos {
71*6881a400Schristos   int ret;
72*6881a400Schristos   char buf;
73*6881a400Schristos 
74*6881a400Schristos   do
75*6881a400Schristos     {
76*6881a400Schristos       ret = read (m_fds[0], &buf, 1);
77*6881a400Schristos     }
78*6881a400Schristos   while (ret >= 0 || (ret == -1 && errno == EINTR));
79*6881a400Schristos }
80*6881a400Schristos 
81*6881a400Schristos /* See event-pipe.h.  */
82*6881a400Schristos 
83*6881a400Schristos void
84*6881a400Schristos event_pipe::mark ()
85*6881a400Schristos {
86*6881a400Schristos   int ret;
87*6881a400Schristos 
88*6881a400Schristos   /* It doesn't really matter what the pipe contains, as long we end
89*6881a400Schristos      up with something in it.  Might as well flush the previous
90*6881a400Schristos      left-overs.  */
91*6881a400Schristos   flush ();
92*6881a400Schristos 
93*6881a400Schristos   do
94*6881a400Schristos     {
95*6881a400Schristos       ret = write (m_fds[1], "+", 1);
96*6881a400Schristos     }
97*6881a400Schristos   while (ret == -1 && errno == EINTR);
98*6881a400Schristos 
99*6881a400Schristos   /* Ignore EAGAIN.  If the pipe is full, the event loop will already
100*6881a400Schristos      be awakened anyway.  */
101*6881a400Schristos }
102