chiark / gitweb /
disorder.h: more consistent approach to function attributes
[disorder] / lib / syscalls.c
... / ...
CommitLineData
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
42int mustnotbeminus1(const char *what, int ret) {
43 if(ret == -1)
44 disorder_fatal(errno, "error calling %s", what);
45 return ret;
46}
47
48#if !_WIN32
49pid_t xfork(void) {
50 pid_t pid;
51
52 if((pid = fork()) < 0)
53 disorder_fatal(errno, "error calling fork");
54 return pid;
55}
56
57void xclose_guts(const char *path, int line, int fd) {
58 if(close(fd) < 0)
59 disorder_fatal(errno, "%s:%d: close %d", path, line, fd);
60}
61
62void xdup2(int fd1, int fd2) {
63 mustnotbeminus1("dup2", dup2(fd1, fd2));
64}
65
66void xpipe(int *fdp) {
67 mustnotbeminus1("pipe", pipe(fdp));
68}
69
70void nonblock(int fd) {
71 mustnotbeminus1("fcntl F_SETFL",
72 fcntl(fd, F_SETFL,
73 mustnotbeminus1("fcntl F_GETFL",
74 fcntl(fd, F_GETFL)) | O_NONBLOCK));
75}
76
77void blocking(int fd) {
78 mustnotbeminus1("fcntl F_SETFL",
79 fcntl(fd, F_SETFL,
80 mustnotbeminus1("fcntl F_GETFL",
81 fcntl(fd, F_GETFL)) & ~O_NONBLOCK));
82}
83
84void cloexec(int fd) {
85 mustnotbeminus1("fcntl F_SETFD",
86 fcntl(fd, F_SETFD,
87 mustnotbeminus1("fcntl F_GETFD",
88 fcntl(fd, F_GETFD)) | FD_CLOEXEC));
89}
90#endif
91
92void xlisten(SOCKET fd, int q) {
93 mustnotbeminus1("listen", listen(fd, q));
94}
95
96void xshutdown(SOCKET fd, int how) {
97 mustnotbeminus1("shutdown", shutdown(fd, how));
98}
99
100void xsetsockopt(SOCKET fd, int l, int o, const void *v, socklen_t vl) {
101 mustnotbeminus1("setsockopt", setsockopt(fd, l, o, v, vl));
102}
103
104SOCKET xsocket(int d, int t, int p) {
105 SOCKET s = socket(d, t, p);
106 if(s == INVALID_SOCKET)
107 disorder_fatal(errno, "error calling socket");
108 return s;
109}
110
111void xconnect(SOCKET fd, const struct sockaddr *sa, socklen_t sl) {
112 mustnotbeminus1("connect", connect(fd, sa, sl));
113}
114
115#if !_WIN32
116void xsigprocmask(int how, const sigset_t *set, sigset_t *oldset) {
117 mustnotbeminus1("sigprocmask", sigprocmask(how, set, oldset));
118}
119
120void xsigaction(int sig, const struct sigaction *sa, struct sigaction *oldsa) {
121 mustnotbeminus1("sigaction", sigaction(sig, sa, oldsa));
122}
123#endif
124
125int xprintf(const char *fmt, ...) {
126 va_list ap;
127 int n;
128
129 va_start(ap, fmt);
130 n = mustnotbeminus1("byte_vfprintf", byte_vfprintf(stdout, fmt, ap));
131 va_end(ap);
132 return n;
133}
134
135void xfclose(FILE *fp) {
136 mustnotbeminus1("fclose", fclose(fp));
137}
138
139int xstrtol(long *n, const char *s, char **ep, int base) {
140 errno = 0;
141 *n = strtol(s, ep, base);
142 return errno;
143}
144
145int xstrtoll(long_long *n, const char *s, char **ep, int base) {
146 errno = 0;
147 *n = strtoll(s, ep, base);
148 return errno;
149}
150
151#if !_WIN32
152int xnice(int inc) {
153 int ret;
154
155 /* some versions of nice() return the new nice value which in principle could
156 * be -1 */
157 errno = 0;
158 ret = nice(inc);
159 if(errno)
160 disorder_fatal(errno, "error calling nice");
161 return ret;
162}
163#endif
164
165void xgettimeofday(struct timeval *tv, struct timezone *tz) {
166 mustnotbeminus1("gettimeofday", gettimeofday(tv, tz));
167}
168
169time_t xtime(time_t *whenp) {
170 struct timeval tv;
171
172 xgettimeofday(&tv, NULL);
173 if(whenp)
174 *whenp = tv.tv_sec;
175 return tv.tv_sec;
176}
177
178#if !_WIN32
179void xnanosleep(const struct timespec *req, struct timespec *rem) {
180 mustnotbeminus1("nanosleep", nanosleep(req, rem));
181}
182#endif
183
184/*
185Local Variables:
186c-basic-offset:2
187comment-column:40
188End:
189*/