Automatic date update in version.in
[binutils-gdb.git] / gdbsupport / eintr.h
blob4cab8f9d48dee81a3d77a60a8632f9b1cf164195
1 /* Utility for handling interrupted syscalls by signals.
3 Copyright (C) 2020-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
20 #ifndef GDBSUPPORT_EINTR_H
21 #define GDBSUPPORT_EINTR_H
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>
30 namespace gdb
32 /* Repeat a system call interrupted with a signal.
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.
39 For example, with:
41 ssize_t ret;
44 errno = 0;
45 ret = ::write (pipe[1], "+", 1);
47 while (ret == -1 && errno == EINTR);
49 You could wrap it by writing the wrapped form:
51 ssize_t ret = gdb::handle_eintr (-1, ::write, pipe[1], "+", 1);
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. */
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...))
62 decltype (f (args...)) ret;
66 errno = 0;
67 ret = f (args...);
69 while (ret == errval && errno == EINTR);
71 return ret;
74 #ifdef HAVE_WAITPID
75 inline pid_t
76 waitpid (pid_t pid, int *wstatus, int options)
78 return gdb::handle_eintr (-1, ::waitpid, pid, wstatus, options);
80 #endif /* HAVE_WAITPID */
82 inline int
83 open (const char *pathname, int flags)
85 return gdb::handle_eintr (-1, ::open, pathname, flags);
88 #ifdef HAVE_WAIT
89 inline pid_t
90 wait (int *wstatus)
92 return gdb::handle_eintr (-1, ::wait, wstatus);
94 #endif /* HAVE_WAIT */
96 inline int
97 close (int fd)
99 return gdb::handle_eintr (-1, ::close, fd);
102 inline ssize_t
103 read (int fd, void *buf, size_t count)
105 return gdb::handle_eintr (-1, ::read, fd, buf, count);
108 template<typename... Args> int fcntl (int fd, int op, Args... args)
110 return gdb::handle_eintr (-1, ::fcntl, fd, op, args...);
113 inline ssize_t
114 write (int fd, const void *buf, size_t count)
116 return gdb::handle_eintr (-1, ::write, fd, buf, count);
119 } /* namespace gdb */
121 #endif /* GDBSUPPORT_EINTR_H */