source: git/Singular/si_signals.h @ 3a0c36

spielwiese
Last change on this file since 3a0c36 was 3a0c36, checked in by Andreas Steenpass <steenpass@…>, 11 years ago
chg: remove pselect
  • Property mode set to 100644
File size: 5.9 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
50SI_EINTR_SAVE_FUNC(pid_t, wait, (int *status), (status))
51SI_EINTR_SAVE_FUNC(pid_t, waitpid, (pid_t pid, int *status, int options),
52                   (pid, status, options))
53
54//SI_EINTR_SAVE_FUNC(int, waitid,
55//                   (idtype_t idtype, id_t id, siginfo_t *infop, int options),
56//                   (idtype, id, infop, options))
57
58SI_EINTR_SAVE_FUNC(ssize_t,  read, (int fd, void *buf, size_t count), 
59                   (fd, buf, count))
60
61
62SI_EINTR_SAVE_FUNC(ssize_t, readv,
63                   (int fd, const struct iovec *iov, int iovcnt),
64                   (fd, iov,iovcnt))
65
66SI_EINTR_SAVE_FUNC(ssize_t, write, (int fd, const void *buf, size_t count),
67                   (fd, buf, count))
68
69SI_EINTR_SAVE_FUNC(ssize_t, writev, (int fd, const struct iovec *iov, int iovcnt),
70                   (fd, iov, iovcnt) )
71
72SI_EINTR_SAVE_FUNC_TEMPLATE(int, si_open1, open, (const char *pathname, int flags),
73                            (pathname, flags), < 0)
74SI_EINTR_SAVE_FUNC_TEMPLATE(int, si_open2, open,
75                            (const char *pathname, int flags, mode_t mode),
76                            (pathname, flags, mode), < 0)
77
78/* Enulate overloading usung preprocessor (we need to be C-compatible) */
79#define SI_GET_FIFTH(_4,_3, _2, _1, N, ...) N
80#define si_open(...) SI_GET_FIFTH(X,##__VA_ARGS__, si_open2, si_open1)(__VA_ARGS__)
81
82SI_EINTR_SAVE_FUNC(int, creat, (const char *pathname, mode_t mode),
83                   (pathname, mode))
84
85
86SI_EINTR_SAVE_FUNC(int, close, (int fd), (fd))
87
88SI_EINTR_SAVE_FUNC(int, accept,
89                   (int sockfd, struct sockaddr *addr, socklen_t *addrlen),
90                   (sockfd, addr, addrlen))
91
92SI_EINTR_SAVE_FUNC(int, connect,
93                   (int sockfd, const struct sockaddr *addr, socklen_t addrlen),
94                   (sockfd, addr, addrlen))
95
96/* @note: We respect that the user may explictely deactivate the
97 * restart feature by setting the second argumetn to NULL.
98 */
99static inline int
100si_nanosleep(const struct timespec *req, struct timespec *rem) {
101
102  int res = -1;
103  do
104  {
105    res = nanosleep(req, rem);
106  } while((rem != NULL) && (res < 0) && (errno == EINTR));
107  return res;   
108}
109
110static inline unsigned int
111si_sleep(unsigned int seconds)
112{
113  do
114  {
115    seconds = sleep(seconds);
116  } while(seconds != 0);
117  return 0;
118}
119
120SI_EINTR_SAVE_FUNC(int, dup, (int oldfd), (oldfd))
121SI_EINTR_SAVE_FUNC(int, dup2, (int oldfd, int newfd), (oldfd, newfd))
122//SI_EINTR_SAVE_FUNC(int, dup3, (int oldfd, int newfd, int flags),
123//                 (oldfd, newfd, flags))
124
125SI_EINTR_SAVE_FUNC(int, unlink, (const char *pathname), (pathname))
126
127SI_EINTR_SAVE_SCANF(int, vscanf, 
128                   (const char *format, va_list ap),
129                   (format, ap))
130
131static inline
132int si_scanf(const char *format, ...)
133{
134  va_list argptr;
135  va_start(argptr, format);
136  int res = si_vscanf(format, argptr);
137  va_end(argptr);
138  return res;
139}
140
141SI_EINTR_SAVE_SCANF(int, vfscanf, 
142                   (FILE *stream, const char *format, va_list ap),
143                   (stream, format, ap))
144
145static inline int
146si_fscanf(FILE *stream, const char *format, ...)
147{
148  va_list argptr;
149  va_start(argptr, format);
150  int res = si_vfscanf(stream, format, argptr);
151  va_end(argptr);
152  return res;
153}
154
155SI_EINTR_SAVE_SCANF(int, vsscanf, 
156                   (const char *str, const char *format, va_list ap),
157                   (str, format, ap))
158
159static inline int 
160si_sscanf(const char *str, const char *format, ...)
161{
162  va_list argptr;
163  va_start(argptr, format);
164  int res = si_vsscanf(str, format, argptr);
165  va_end(argptr);
166  return res;
167}
168
169SI_EINTR_SAVE_FUNC(int, stat, (const char *path, struct stat *buf),
170                   (path, buf))
171SI_EINTR_SAVE_FUNC(int, fstat, (int fd, struct stat *buf),
172                   (fd, buf))
173SI_EINTR_SAVE_FUNC(int, lstat, (const char *path, struct stat *buf),
174                   (path, buf))
175
176
177SI_EINTR_SAVE_FUNC(int, sigaction,
178                   (int signum, const struct sigaction *act, 
179                    struct sigaction *oldact),
180                   (signum, act, oldact))
181
182
183#ifdef HAVE_SIGINTERRUPT
184SI_EINTR_SAVE_FUNC(int, siginterrupt, (int sig, int flag),
185                   (sig, flag))
186#else
187#define si_siginterrupt(arg1, arg2)
188#endif
189
190
191SI_EINTR_SAVE_FUNC(int, sem_wait, (sem_t *sem), (sem))
192SI_EINTR_SAVE_FUNC(int, sem_trywait, (sem_t *sem), (sem))
193//SI_EINTR_SAVE_FUNC(int, sem_timedwait,
194//                   (sem_t *sem, const struct timespec *abs_timeout),
195//                   (sem, abs_timeout))
196
197
198#undef SI_EINTR_SAVE_FUNC
199
200
201#endif /* SINGULAR_SI_SIGNALS_H */
Note: See TracBrowser for help on using the repository browser.