1 /* Utility for handling interrupted syscalls by signals. 2 3 Copyright (C) 2020-2024 Free Software Foundation, Inc. 4 5 This file is part of GDB. 6 7 This program is free software; you can redistribute it and/or modify 8 it under the terms of the GNU General Public License as published by 9 the Free Software Foundation; either version 3 of the License, or 10 (at your option) any later version. 11 12 This program is distributed in the hope that it will be useful, 13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 GNU General Public License for more details. 16 17 You should have received a copy of the GNU General Public License 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 19 20 #ifndef GDBSUPPORT_EINTR_H 21 #define GDBSUPPORT_EINTR_H 22 23 #include <cerrno> 24 #include <sys/types.h> 25 #include <sys/wait.h> 26 #include <sys/stat.h> 27 #include <fcntl.h> 28 #include <unistd.h> 29 30 namespace gdb 31 { 32 /* Repeat a system call interrupted with a signal. 33 34 A utility for handling interrupted syscalls, which return with error 35 and set the errno to EINTR. The interrupted syscalls can be repeated, 36 until successful completion. This utility avoids wrapping code with 37 manual checks for such errors which are highly repetitive. 38 39 For example, with: 40 41 ssize_t ret; 42 do 43 { 44 errno = 0; 45 ret = ::write (pipe[1], "+", 1); 46 } 47 while (ret == -1 && errno == EINTR); 48 49 You could wrap it by writing the wrapped form: 50 51 ssize_t ret = gdb::handle_eintr (-1, ::write, pipe[1], "+", 1); 52 53 ERRVAL specifies the failure value indicating that the call to the 54 F function with ARGS... arguments was possibly interrupted with a 55 signal. */ 56 57 template<typename ErrorValType, typename Fun, typename... Args> 58 inline auto 59 handle_eintr (ErrorValType errval, const Fun &f, const Args &... args) 60 -> decltype (f (args...)) 61 { 62 decltype (f (args...)) ret; 63 64 do 65 { 66 errno = 0; 67 ret = f (args...); 68 } 69 while (ret == errval && errno == EINTR); 70 71 return ret; 72 } 73 74 #ifdef HAVE_WAITPID 75 inline pid_t 76 waitpid (pid_t pid, int *wstatus, int options) 77 { 78 return gdb::handle_eintr (-1, ::waitpid, pid, wstatus, options); 79 } 80 #endif /* HAVE_WAITPID */ 81 82 inline int 83 open (const char *pathname, int flags) 84 { 85 return gdb::handle_eintr (-1, ::open, pathname, flags); 86 } 87 88 #ifdef HAVE_WAIT 89 inline pid_t 90 wait (int *wstatus) 91 { 92 return gdb::handle_eintr (-1, ::wait, wstatus); 93 } 94 #endif /* HAVE_WAIT */ 95 96 inline int 97 close (int fd) 98 { 99 return gdb::handle_eintr (-1, ::close, fd); 100 } 101 102 inline ssize_t 103 read (int fd, void *buf, size_t count) 104 { 105 return gdb::handle_eintr (-1, ::read, fd, buf, count); 106 } 107 108 template<typename... Args> int fcntl (int fd, int op, Args... args) 109 { 110 return gdb::handle_eintr (-1, ::fcntl, fd, op, args...); 111 } 112 113 inline ssize_t 114 write (int fd, const void *buf, size_t count) 115 { 116 return gdb::handle_eintr (-1, ::write, fd, buf, count); 117 } 118 119 } /* namespace gdb */ 120 121 #endif /* GDBSUPPORT_EINTR_H */ 122