si_signals.h
Go to the documentation of this file.
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) \
28 static 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 
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 SI_EINTR_SAVE_FUNC(pid_t, wait, (int *status), (status))
51 SI_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 
58 SI_EINTR_SAVE_FUNC(ssize_t, read, (int fd, void *buf, size_t count),
59  (fd, buf, count))
60 
61 
62 SI_EINTR_SAVE_FUNC(ssize_t, readv,
63  (int fd, const struct iovec *iov, int iovcnt),
64  (fd, iov,iovcnt))
65 
66 SI_EINTR_SAVE_FUNC(ssize_t, write, (int fd, const void *buf, size_t count),
67  (fd, buf, count))
68 
69 SI_EINTR_SAVE_FUNC(ssize_t, writev, (int fd, const struct iovec *iov, int iovcnt),
70  (fd, iov, iovcnt) )
71 
73  (pathname, flags), < 0)
74 SI_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 
82 SI_EINTR_SAVE_FUNC(int, creat, (const char *pathname, mode_t mode),
83  (pathname, mode))
84 
85 
86 SI_EINTR_SAVE_FUNC(int, close, (int fd), (fd))
87 
88 SI_EINTR_SAVE_FUNC(int, accept,
89  (int sockfd, struct sockaddr *addr, socklen_t *addrlen),
90  (sockfd, addr, addrlen))
91 
92 SI_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  */
99 static inline int
100 si_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 
110 static inline unsigned int
111 si_sleep(unsigned int seconds)
112 {
113  do
114  {
115  seconds = sleep(seconds);
116  } while(seconds != 0);
117  return 0;
118 }
119 
120 SI_EINTR_SAVE_FUNC(int, dup, (int oldfd), (oldfd))
121 SI_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 
125 SI_EINTR_SAVE_FUNC(int, unlink, (const char *pathname), (pathname))
126 
127 SI_EINTR_SAVE_SCANF(int, vscanf,
128  (const char *format, va_list ap),
129  (format, ap))
130 
131 static inline
132 int 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 
141 SI_EINTR_SAVE_SCANF(int, vfscanf,
142  (FILE *stream, const char *format, va_list ap),
143  (stream, format, ap))
144 
145 static inline int
146 si_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 
155 SI_EINTR_SAVE_SCANF(int, vsscanf,
156  (const char *str, const char *format, va_list ap),
157  (str, format, ap))
158 
159 static inline int
160 si_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 
169 SI_EINTR_SAVE_FUNC(int, stat, (const char *path, struct stat *buf),
170  (path, buf))
171 SI_EINTR_SAVE_FUNC(int, fstat, (int fd, struct stat *buf),
172  (fd, buf))
173 SI_EINTR_SAVE_FUNC(int, lstat, (const char *path, struct stat *buf),
174  (path, buf))
175 
176 
177 SI_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
184 SI_EINTR_SAVE_FUNC(int, siginterrupt, (int sig, int flag),
185  (sig, flag))
186 #else
187 #define si_siginterrupt(arg1, arg2)
188 #endif
189 
190 
191 SI_EINTR_SAVE_FUNC(int, sem_wait, (sem_t *sem), (sem))
192 SI_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 */
int status int void size_t count
Definition: si_signals.h:58
int status int fd
Definition: si_signals.h:58
void rem(unsigned long *a, unsigned long *q, unsigned long p, int &dega, int degq)
Definition: minpoly.cc:574
int status int void size_t count int const void size_t count const char * pathname
Definition: si_signals.h:72
#define SI_EINTR_SAVE_FUNC_TEMPLATE(return_type, newfunc, func, decl, args, err_domain)
Definition: si_signals.h:27
int status int void size_t count write
Definition: si_signals.h:66
int status int void size_t count int const void size_t count si_open1
Definition: si_signals.h:72
poly res
Definition: myNF.cc:322
int status int void * buf
Definition: si_signals.h:58
int status read
Definition: si_signals.h:58
void select(const ListCFList &ppi, int length, ListCFList &ppi1, ListCFList &ppi2)
#define SI_EINTR_SAVE_SCANF(return_type, func, decl, args)
Definition: si_signals.h:41
#define NULL
Definition: omList.c:10
int * status
Definition: si_signals.h:50
wait
Definition: si_signals.h:50
#define SI_EINTR_SAVE_FUNC(return_type, func, decl, args)
Definition: si_signals.h:38
int status int void size_t count int const void size_t count open
Definition: si_signals.h:72
#define const
Definition: fegetopt.c:41
static scmon act
Definition: hdegree.cc:1057
int status int void size_t count int const void size_t count const char int flags
Definition: si_signals.h:72