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) \ |
---|
29 | static 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 | |
---|
45 | SI_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 | |
---|
51 | SI_EINTR_SAVE_FUNC(pid_t, wait, (int *status), (status)) |
---|
52 | SI_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 | |
---|
59 | SI_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 | |
---|
67 | SI_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 | |
---|
73 | SI_EINTR_SAVE_FUNC_TEMPLATE(int, si_open1, open, (const char *pathname, int flags), |
---|
74 | (pathname, flags), < 0) |
---|
75 | SI_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 | |
---|
87 | SI_EINTR_SAVE_FUNC(int, close, (int fd), (fd)) |
---|
88 | |
---|
89 | SI_EINTR_SAVE_FUNC(int, accept, |
---|
90 | (int sockfd, struct sockaddr *addr, socklen_t *addrlen), |
---|
91 | (sockfd, addr, addrlen)) |
---|
92 | |
---|
93 | SI_EINTR_SAVE_FUNC(int, connect, |
---|
94 | (int sockfd, const struct sockaddr *addr, socklen_t addrlen), |
---|
95 | (sockfd, addr, addrlen)) |
---|
96 | |
---|
97 | /* @note: We respect that the user may explictely deactivate the |
---|
98 | * restart feature by setting the second argumetn to NULL. |
---|
99 | */ |
---|
100 | static inline int |
---|
101 | si_nanosleep(const struct timespec *req, struct timespec *rem) { |
---|
102 | |
---|
103 | int res = -1; |
---|
104 | do |
---|
105 | { |
---|
106 | res = nanosleep(req, rem); |
---|
107 | } while((rem != NULL) && (res < 0) && (errno == EINTR)); |
---|
108 | return res; |
---|
109 | } |
---|
110 | |
---|
111 | static inline unsigned int |
---|
112 | si_sleep(unsigned int seconds) |
---|
113 | { |
---|
114 | do |
---|
115 | { |
---|
116 | seconds = sleep(seconds); |
---|
117 | } while(seconds != 0); |
---|
118 | return 0; |
---|
119 | } |
---|
120 | |
---|
121 | SI_EINTR_SAVE_FUNC(int, dup, (int oldfd), (oldfd)) |
---|
122 | SI_EINTR_SAVE_FUNC(int, dup2, (int oldfd, int newfd), (oldfd, newfd)) |
---|
123 | //SI_EINTR_SAVE_FUNC(int, dup3, (int oldfd, int newfd, int flags), |
---|
124 | // (oldfd, newfd, flags)) |
---|
125 | |
---|
126 | SI_EINTR_SAVE_FUNC(int, unlink, (const char *pathname), (pathname)) |
---|
127 | |
---|
128 | SI_EINTR_SAVE_SCANF(int, vscanf, |
---|
129 | (const char *format, va_list ap), |
---|
130 | (format, ap)) |
---|
131 | |
---|
132 | static inline |
---|
133 | int si_scanf(const char *format, ...) |
---|
134 | { |
---|
135 | va_list argptr; |
---|
136 | va_start(argptr, format); |
---|
137 | int res = si_vscanf(format, argptr); |
---|
138 | va_end(argptr); |
---|
139 | return res; |
---|
140 | } |
---|
141 | |
---|
142 | SI_EINTR_SAVE_SCANF(int, vfscanf, |
---|
143 | (FILE *stream, const char *format, va_list ap), |
---|
144 | (stream, format, ap)) |
---|
145 | |
---|
146 | static inline int |
---|
147 | si_fscanf(FILE *stream, const char *format, ...) |
---|
148 | { |
---|
149 | va_list argptr; |
---|
150 | va_start(argptr, format); |
---|
151 | int res = si_vfscanf(stream, format, argptr); |
---|
152 | va_end(argptr); |
---|
153 | return res; |
---|
154 | } |
---|
155 | |
---|
156 | SI_EINTR_SAVE_SCANF(int, vsscanf, |
---|
157 | (const char *str, const char *format, va_list ap), |
---|
158 | (str, format, ap)) |
---|
159 | |
---|
160 | static inline int |
---|
161 | si_sscanf(const char *str, const char *format, ...) |
---|
162 | { |
---|
163 | va_list argptr; |
---|
164 | va_start(argptr, format); |
---|
165 | int res = si_vsscanf(str, format, argptr); |
---|
166 | va_end(argptr); |
---|
167 | return res; |
---|
168 | } |
---|
169 | |
---|
170 | SI_EINTR_SAVE_FUNC(int, stat, (const char *path, struct stat *buf), |
---|
171 | (path, buf)) |
---|
172 | SI_EINTR_SAVE_FUNC(int, fstat, (int fd, struct stat *buf), |
---|
173 | (fd, buf)) |
---|
174 | SI_EINTR_SAVE_FUNC(int, lstat, (const char *path, struct stat *buf), |
---|
175 | (path, buf)) |
---|
176 | |
---|
177 | |
---|
178 | SI_EINTR_SAVE_FUNC(int, sigaction, |
---|
179 | (int signum, const struct sigaction *act, |
---|
180 | struct sigaction *oldact), |
---|
181 | (signum, act, oldact)) |
---|
182 | |
---|
183 | |
---|
184 | #ifdef HAVE_SIGINTERRUPT |
---|
185 | SI_EINTR_SAVE_FUNC(int, siginterrupt, (int sig, int flag), |
---|
186 | (sig, flag)) |
---|
187 | #else |
---|
188 | #define si_siginterrupt(arg1, arg2) |
---|
189 | #endif |
---|
190 | |
---|
191 | |
---|
192 | SI_EINTR_SAVE_FUNC(int, sem_wait, (sem_t *sem), (sem)) |
---|
193 | SI_EINTR_SAVE_FUNC(int, sem_trywait, (sem_t *sem), (sem)) |
---|
194 | //SI_EINTR_SAVE_FUNC(int, sem_timedwait, |
---|
195 | // (sem_t *sem, const struct timespec *abs_timeout), |
---|
196 | // (sem, abs_timeout)) |
---|
197 | |
---|
198 | |
---|
199 | #undef SI_EINTR_SAVE_FUNC |
---|
200 | |
---|
201 | |
---|
202 | #endif /* SINGULAR_SI_SIGNALS_H */ |
---|