chiark / gitweb /
Cope with various header files being missing.
[disorder] / lib / syscalls.c
1 /*
2  * This file is part of DisOrder.
3  * Copyright (C) 2004, 2005, 2007-9, 2013 Richard Kettlewell
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 /** @file lib/syscalls.c
19  * @brief Error-checking library call wrappers
20  */
21 #include "common.h"
22
23 #if HAVE_UNISTD_H
24 # include <unistd.h>
25 #endif
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <sys/types.h>
29 #if HAVE_SYS_SOCKET_H
30 # include <sys/socket.h>
31 #endif
32 #if HAVE_SYS_TIME_H
33 # include <sys/time.h>
34 #endif
35 #include <signal.h>
36 #include <time.h>
37
38 #include "syscalls.h"
39 #include "log.h"
40 #include "printf.h"
41
42 int mustnotbeminus1(const char *what, int ret) {
43   if(ret == -1)
44     disorder_fatal(errno, "error calling %s", what);
45   return ret;
46 }
47
48 pid_t xfork(void) {
49   pid_t pid;
50
51   if((pid = fork()) < 0)
52     disorder_fatal(errno, "error calling fork");
53   return pid;
54 }
55
56 void xclose_guts(const char *path, int line, int fd) {
57   if(close(fd) < 0)
58     disorder_fatal(errno, "%s:%d: close %d", path, line, fd);
59 }
60
61 void xdup2(int fd1, int fd2) {
62   mustnotbeminus1("dup2", dup2(fd1, fd2));
63 }
64
65 void xpipe(int *fdp) {
66   mustnotbeminus1("pipe", pipe(fdp));
67 }
68
69 void nonblock(int fd) {
70   mustnotbeminus1("fcntl F_SETFL",
71                   fcntl(fd, F_SETFL,
72                         mustnotbeminus1("fcntl F_GETFL",
73                                         fcntl(fd, F_GETFL)) | O_NONBLOCK));
74 }
75
76 void blocking(int fd) {
77   mustnotbeminus1("fcntl F_SETFL",
78                   fcntl(fd, F_SETFL,
79                         mustnotbeminus1("fcntl F_GETFL",
80                                         fcntl(fd, F_GETFL)) & ~O_NONBLOCK));
81 }
82
83 void cloexec(int fd) {
84   mustnotbeminus1("fcntl F_SETFD",
85                   fcntl(fd, F_SETFD,
86                         mustnotbeminus1("fcntl F_GETFD",
87                                         fcntl(fd, F_GETFD)) | FD_CLOEXEC));
88 }
89
90 void xlisten(int fd, int q) {
91   mustnotbeminus1("listen", listen(fd, q));
92 }
93
94 void xshutdown(int fd, int how) {
95   mustnotbeminus1("shutdown", shutdown(fd, how));
96 }
97
98 void xsetsockopt(int fd, int l, int o, const void *v, socklen_t vl) {
99   mustnotbeminus1("setsockopt", setsockopt(fd, l, o, v, vl));
100 }
101
102 int xsocket(int d, int t, int p) {
103   return mustnotbeminus1("socket", socket(d, t, p));
104 }
105
106 void xconnect(int fd, const struct sockaddr *sa, socklen_t sl) {
107   mustnotbeminus1("connect", connect(fd, sa, sl));
108 }
109
110 void xsigprocmask(int how, const sigset_t *set, sigset_t *oldset) {
111   mustnotbeminus1("sigprocmask", sigprocmask(how, set, oldset));
112 }
113
114 void xsigaction(int sig, const struct sigaction *sa, struct sigaction *oldsa) {
115   mustnotbeminus1("sigaction", sigaction(sig, sa, oldsa));
116 }
117
118 int xprintf(const char *fmt, ...) {
119   va_list ap;
120   int n;
121
122   va_start(ap, fmt);
123   n = mustnotbeminus1("byte_vfprintf", byte_vfprintf(stdout, fmt, ap));
124   va_end(ap);
125   return n;
126 }
127
128 void xfclose(FILE *fp) {
129   mustnotbeminus1("fclose", fclose(fp));
130 }
131
132 int xstrtol(long *n, const char *s, char **ep, int base) {
133   errno = 0;
134   *n = strtol(s, ep, base);
135   return errno;
136 }
137
138 int xstrtoll(long_long *n, const char *s, char **ep, int base) {
139   errno = 0;
140   *n = strtoll(s, ep, base);
141   return errno;
142 }
143
144 int xnice(int inc) {
145   int ret;
146
147   /* some versions of nice() return the new nice value which in principle could
148    * be -1 */
149   errno = 0;
150   ret = nice(inc);
151   if(errno)
152     disorder_fatal(errno, "error calling nice");
153   return ret;
154 }
155
156 void xgettimeofday(struct timeval *tv, struct timezone *tz) {
157   mustnotbeminus1("gettimeofday", gettimeofday(tv, tz));
158 }
159
160 time_t xtime(time_t *whenp) {
161   struct timeval tv;
162
163   xgettimeofday(&tv, NULL);
164   if(whenp)
165     *whenp = tv.tv_sec;
166   return tv.tv_sec;
167 }
168
169 void xnanosleep(const struct timespec *req, struct timespec *rem) {
170   mustnotbeminus1("nanosleep", nanosleep(req, rem));
171 }
172
173 /*
174 Local Variables:
175 c-basic-offset:2
176 comment-column:40
177 End:
178 */