source: git/Singular/si_signals.h @ 121d2ae

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