source: git/libpolys/reporter/si_signals.h @ 52dcfd

spielwiese
Last change on this file since 52dcfd was 52dcfd, checked in by Hans Schoenemann <hannes@…>, 2 months ago
interrupt handling in system calls
  • Property mode set to 100644
File size: 6.0 KB
Line 
1/*****************************************
2*  Computer Algebra System SINGULAR      *
3*****************************************/
4/*
5* ABSTRACT: wrappijng signal-interuptable system calls
6* AUTHOR: Alexander Dreyer, The PolyBoRi Team, 2013
7*/
8
9#include <signal.h>
10#include <errno.h>
11#include <sys/types.h>
12#include <sys/wait.h>
13#include <sys/select.h>
14
15#include <unistd.h>
16#include <sys/uio.h>
17#include <sys/stat.h>
18#include <fcntl.h>
19#include <sys/socket.h>
20#include <time.h>
21#include <stdio.h>
22#include <semaphore.h>
23#include <stdarg.h>
24
25#ifndef SINGULAR_SI_SIGNALS_H
26#define SINGULAR_SI_SIGNALS_H
27
28#define SI_EINTR_SAVE_FUNC_TEMPLATE(return_type, newfunc, func, decl, args, err_domain) \
29static inline return_type newfunc decl   \
30{                                        \
31  int res = -1;                                 \
32  do                                     \
33  {                                      \
34    res = func args;                         \
35  } while((res err_domain) && (errno == EINTR));\
36  return res;                            \
37}
38
39#define SI_EINTR_SAVE_FUNC(return_type, func, decl, args) \
40  SI_EINTR_SAVE_FUNC_TEMPLATE(return_type, si_##func, func, decl, args, < 0)
41
42#define SI_EINTR_SAVE_SCANF(return_type, func, decl, args) \
43  SI_EINTR_SAVE_FUNC_TEMPLATE(return_type, si_##func, func, decl, args, == EOF)
44
45SI_EINTR_SAVE_FUNC(int, select,
46                   (int nfds, fd_set *readfds, fd_set *writefds,
47                    fd_set *exceptfds, struct timeval *timeout),
48                   (nfds,readfds, writefds, exceptfds, timeout)
49                   )
50
51SI_EINTR_SAVE_FUNC(pid_t, wait, (int *status), (status))
52SI_EINTR_SAVE_FUNC(pid_t, waitpid, (pid_t pid, int *status, int options),
53                   (pid, status, options))
54
55//SI_EINTR_SAVE_FUNC(int, waitid,
56//                   (idtype_t idtype, id_t id, siginfo_t *infop, int options),
57//                   (idtype, id, infop, options))
58
59SI_EINTR_SAVE_FUNC(ssize_t,  read, (int fd, void *buf, size_t count),
60                   (fd, buf, count))
61
62
63//SI_EINTR_SAVE_FUNC(ssize_t, readv,
64//                   (int fd, const struct iovec *iov, int iovcnt),
65//                   (fd, iov,iovcnt))
66
67SI_EINTR_SAVE_FUNC(ssize_t, write, (int fd, const void *buf, size_t count),
68                   (fd, buf, count))
69
70//SI_EINTR_SAVE_FUNC(ssize_t, writev, (int fd, const struct iovec *iov, int iovcnt),
71//                   (fd, iov, iovcnt) )
72
73SI_EINTR_SAVE_FUNC_TEMPLATE(int, si_open1, open, (const char *pathname, int flags),
74                            (pathname, flags), < 0)
75SI_EINTR_SAVE_FUNC_TEMPLATE(int, si_open2, open,
76                            (const char *pathname, int flags, mode_t mode),
77                            (pathname, flags, mode), < 0)
78
79/* Enulate overloading usung preprocessor (we need to be C-compatible) */
80#define SI_GET_FIFTH(_4,_3, _2, _1, N, ...) N
81#define si_open(...) SI_GET_FIFTH(X,##__VA_ARGS__, si_open2, si_open1)(__VA_ARGS__)
82
83//SI_EINTR_SAVE_FUNC(int, creat, (const char *pathname, mode_t mode),
84//                   (pathname, mode))
85
86
87SI_EINTR_SAVE_FUNC(int, close, (int fd), (fd))
88
89SI_EINTR_SAVE_FUNC(int, accept,
90                   (int sockfd, struct sockaddr *addr, socklen_t *addrlen),
91                   (sockfd, addr, addrlen))
92
93SI_EINTR_SAVE_FUNC(int, connect,
94                   (int sockfd, const struct sockaddr *addr, socklen_t addrlen),
95                   (sockfd, addr, addrlen))
96
97#if 0
98/* @note: We respect that the user may explictely deactivate the
99 * restart feature by setting the second argumetn to NULL.
100 */
101static inline int
102si_nanosleep(const struct timespec *req, struct timespec *rem)
103{
104  int res = -1;
105  do
106  {
107    res = nanosleep(req, rem);
108  } while((rem != NULL) && (res < 0) && (errno == EINTR));
109  return res;
110}
111#endif
112
113static inline unsigned int
114si_sleep(unsigned int seconds)
115{
116  do
117  {
118    seconds = sleep(seconds);
119  } while(seconds != 0);
120  return 0;
121}
122
123//SI_EINTR_SAVE_FUNC(int, dup, (int oldfd), (oldfd))
124SI_EINTR_SAVE_FUNC(int, dup2, (int oldfd, int newfd), (oldfd, newfd))
125//SI_EINTR_SAVE_FUNC(int, dup3, (int oldfd, int newfd, int flags),
126//                   (oldfd, newfd, flags))
127
128SI_EINTR_SAVE_FUNC(int, unlink, (const char *pathname), (pathname))
129
130SI_EINTR_SAVE_SCANF(int, vscanf,
131                   (const char *format, va_list ap),
132                   (format, ap))
133
134static inline
135int si_scanf(const char *format, ...)
136{
137  va_list argptr;
138  va_start(argptr, format);
139  int res = si_vscanf(format, argptr);
140  va_end(argptr);
141  return res;
142}
143
144SI_EINTR_SAVE_SCANF(int, vfscanf,
145                   (FILE *stream, const char *format, va_list ap),
146                   (stream, format, ap))
147
148static inline int
149si_fscanf(FILE *stream, const char *format, ...)
150{
151  va_list argptr;
152  va_start(argptr, format);
153  int res = si_vfscanf(stream, format, argptr);
154  va_end(argptr);
155  return res;
156}
157
158SI_EINTR_SAVE_SCANF(int, vsscanf,
159                   (const char *str, const char *format, va_list ap),
160                   (str, format, ap))
161
162static inline int
163si_sscanf(const char *str, const char *format, ...)
164{
165  va_list argptr;
166  va_start(argptr, format);
167  int res = si_vsscanf(str, format, argptr);
168  va_end(argptr);
169  return res;
170}
171
172SI_EINTR_SAVE_FUNC(int, stat, (const char *path, struct stat *buf),
173                   (path, buf))
174SI_EINTR_SAVE_FUNC(int, fstat, (int fd, struct stat *buf),
175                   (fd, buf))
176SI_EINTR_SAVE_FUNC(int, lstat, (const char *path, struct stat *buf),
177                   (path, buf))
178
179
180SI_EINTR_SAVE_FUNC(int, sigaction,
181                   (int signum, const struct sigaction *act,
182                    struct sigaction *oldact),
183                   (signum, act, oldact))
184
185
186#ifdef HAVE_SIGINTERRUPT
187SI_EINTR_SAVE_FUNC(int, siginterrupt, (int sig, int flag),
188                   (sig, flag))
189#else
190#define si_siginterrupt(arg1, arg2)
191#endif
192
193
194SI_EINTR_SAVE_FUNC(int, sem_wait, (sem_t *sem), (sem))
195SI_EINTR_SAVE_FUNC(int, sem_trywait, (sem_t *sem), (sem))
196//SI_EINTR_SAVE_FUNC(int, sem_timedwait,
197//                   (sem_t *sem, const struct timespec *abs_timeout),
198//                   (sem, abs_timeout))
199
200
201#undef SI_EINTR_SAVE_FUNC
202
203
204#endif /* SINGULAR_SI_SIGNALS_H */
Note: See TracBrowser for help on using the repository browser.