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