14b169a6bSchristos /* Event pipe for GDB, the GNU debugger. 24b169a6bSchristos 3*5ba1f45fSchristos Copyright (C) 2021-2024 Free Software Foundation, Inc. 44b169a6bSchristos 54b169a6bSchristos This file is part of GDB. 64b169a6bSchristos 74b169a6bSchristos This program is free software; you can redistribute it and/or modify 84b169a6bSchristos it under the terms of the GNU General Public License as published by 94b169a6bSchristos the Free Software Foundation; either version 3 of the License, or 104b169a6bSchristos (at your option) any later version. 114b169a6bSchristos 124b169a6bSchristos This program is distributed in the hope that it will be useful, 134b169a6bSchristos but WITHOUT ANY WARRANTY; without even the implied warranty of 144b169a6bSchristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 154b169a6bSchristos GNU General Public License for more details. 164b169a6bSchristos 174b169a6bSchristos You should have received a copy of the GNU General Public License 184b169a6bSchristos along with this program. If not, see <http://www.gnu.org/licenses/>. */ 194b169a6bSchristos 204b169a6bSchristos #include "gdbsupport/event-pipe.h" 214b169a6bSchristos #include "gdbsupport/filestuff.h" 224b169a6bSchristos 234b169a6bSchristos #include <errno.h> 244b169a6bSchristos #include <fcntl.h> 254b169a6bSchristos #include <unistd.h> 264b169a6bSchristos 274b169a6bSchristos event_pipe::~event_pipe () 284b169a6bSchristos { 294b169a6bSchristos if (is_open ()) 304b169a6bSchristos close_pipe (); 314b169a6bSchristos } 324b169a6bSchristos 334b169a6bSchristos /* See event-pipe.h. */ 344b169a6bSchristos 354b169a6bSchristos bool 364b169a6bSchristos event_pipe::open_pipe () 374b169a6bSchristos { 384b169a6bSchristos if (is_open ()) 394b169a6bSchristos return false; 404b169a6bSchristos 414b169a6bSchristos if (gdb_pipe_cloexec (m_fds) == -1) 424b169a6bSchristos return false; 434b169a6bSchristos 444b169a6bSchristos if (fcntl (m_fds[0], F_SETFL, O_NONBLOCK) == -1 454b169a6bSchristos || fcntl (m_fds[1], F_SETFL, O_NONBLOCK) == -1) 464b169a6bSchristos { 474b169a6bSchristos close_pipe (); 484b169a6bSchristos return false; 494b169a6bSchristos } 504b169a6bSchristos 514b169a6bSchristos return true; 524b169a6bSchristos } 534b169a6bSchristos 544b169a6bSchristos /* See event-pipe.h. */ 554b169a6bSchristos 564b169a6bSchristos void 574b169a6bSchristos event_pipe::close_pipe () 584b169a6bSchristos { 594b169a6bSchristos ::close (m_fds[0]); 604b169a6bSchristos ::close (m_fds[1]); 614b169a6bSchristos m_fds[0] = -1; 624b169a6bSchristos m_fds[1] = -1; 634b169a6bSchristos } 644b169a6bSchristos 654b169a6bSchristos /* See event-pipe.h. */ 664b169a6bSchristos 674b169a6bSchristos void 684b169a6bSchristos event_pipe::flush () 694b169a6bSchristos { 704b169a6bSchristos int ret; 714b169a6bSchristos char buf; 724b169a6bSchristos 734b169a6bSchristos do 744b169a6bSchristos { 754b169a6bSchristos ret = read (m_fds[0], &buf, 1); 764b169a6bSchristos } 774b169a6bSchristos while (ret >= 0 || (ret == -1 && errno == EINTR)); 784b169a6bSchristos } 794b169a6bSchristos 804b169a6bSchristos /* See event-pipe.h. */ 814b169a6bSchristos 824b169a6bSchristos void 834b169a6bSchristos event_pipe::mark () 844b169a6bSchristos { 854b169a6bSchristos int ret; 864b169a6bSchristos 874b169a6bSchristos /* It doesn't really matter what the pipe contains, as long we end 884b169a6bSchristos up with something in it. Might as well flush the previous 894b169a6bSchristos left-overs. */ 904b169a6bSchristos flush (); 914b169a6bSchristos 924b169a6bSchristos do 934b169a6bSchristos { 944b169a6bSchristos ret = write (m_fds[1], "+", 1); 954b169a6bSchristos } 964b169a6bSchristos while (ret == -1 && errno == EINTR); 974b169a6bSchristos 984b169a6bSchristos /* Ignore EAGAIN. If the pipe is full, the event loop will already 994b169a6bSchristos be awakened anyway. */ 1004b169a6bSchristos } 101