autogen.sh: use /bin/sh
[disorder] / lib / syscalls.c
... / ...
CommitLineData
1/*
2 * This file is part of DisOrder.
3 * Copyright (C) 2004, 2005, 2007, 2008, 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#include <unistd.h>
24#include <errno.h>
25#include <fcntl.h>
26#include <sys/types.h>
27#include <sys/socket.h>
28#include <sys/time.h>
29#include <signal.h>
30#include <time.h>
31
32#include "syscalls.h"
33#include "log.h"
34#include "printf.h"
35
36int mustnotbeminus1(const char *what, int ret) {
37 if(ret == -1)
38 disorder_fatal(errno, "error calling %s", what);
39 return ret;
40}
41
42pid_t xfork(void) {
43 pid_t pid;
44
45 if((pid = fork()) < 0)
46 disorder_fatal(errno, "error calling fork");
47 return pid;
48}
49
50void xclose_guts(const char *path, int line, int fd) {
51 if(close(fd) < 0)
52 disorder_fatal(errno, "%s:%d: close %d", path, line, fd);
53}
54
55void xdup2(int fd1, int fd2) {
56 mustnotbeminus1("dup2", dup2(fd1, fd2));
57}
58
59void xpipe(int *fdp) {
60 mustnotbeminus1("pipe", pipe(fdp));
61}
62
63void nonblock(int fd) {
64 mustnotbeminus1("fcntl F_SETFL",
65 fcntl(fd, F_SETFL,
66 mustnotbeminus1("fcntl F_GETFL",
67 fcntl(fd, F_GETFL)) | O_NONBLOCK));
68}
69
70void blocking(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 cloexec(int fd) {
78 mustnotbeminus1("fcntl F_SETFD",
79 fcntl(fd, F_SETFD,
80 mustnotbeminus1("fcntl F_GETFD",
81 fcntl(fd, F_GETFD)) | FD_CLOEXEC));
82}
83
84void xlisten(int fd, int q) {
85 mustnotbeminus1("listen", listen(fd, q));
86}
87
88void xshutdown(int fd, int how) {
89 mustnotbeminus1("shutdown", shutdown(fd, how));
90}
91
92void xsetsockopt(int fd, int l, int o, const void *v, socklen_t vl) {
93 mustnotbeminus1("setsockopt", setsockopt(fd, l, o, v, vl));
94}
95
96int xsocket(int d, int t, int p) {
97 return mustnotbeminus1("socket", socket(d, t, p));
98}
99
100void xconnect(int fd, const struct sockaddr *sa, socklen_t sl) {
101 mustnotbeminus1("connect", connect(fd, sa, sl));
102}
103
104void xsigprocmask(int how, const sigset_t *set, sigset_t *oldset) {
105 mustnotbeminus1("sigprocmask", sigprocmask(how, set, oldset));
106}
107
108void xsigaction(int sig, const struct sigaction *sa, struct sigaction *oldsa) {
109 mustnotbeminus1("sigaction", sigaction(sig, sa, oldsa));
110}
111
112int xprintf(const char *fmt, ...) {
113 va_list ap;
114 int n;
115
116 va_start(ap, fmt);
117 n = mustnotbeminus1("byte_vfprintf", byte_vfprintf(stdout, fmt, ap));
118 va_end(ap);
119 return n;
120}
121
122void xfclose(FILE *fp) {
123 mustnotbeminus1("fclose", fclose(fp));
124}
125
126int xstrtol(long *n, const char *s, char **ep, int base) {
127 errno = 0;
128 *n = strtol(s, ep, base);
129 return errno;
130}
131
132int xstrtoll(long_long *n, const char *s, char **ep, int base) {
133 errno = 0;
134 *n = strtoll(s, ep, base);
135 return errno;
136}
137
138int xnice(int inc) {
139 int ret;
140
141 /* some versions of nice() return the new nice value which in principle could
142 * be -1 */
143 errno = 0;
144 ret = nice(inc);
145 if(errno)
146 disorder_fatal(errno, "error calling nice");
147 return ret;
148}
149
150void xgettimeofday(struct timeval *tv, struct timezone *tz) {
151 mustnotbeminus1("gettimeofday", gettimeofday(tv, tz));
152}
153
154time_t xtime(time_t *whenp) {
155 struct timeval tv;
156
157 xgettimeofday(&tv, NULL);
158 if(whenp)
159 *whenp = tv.tv_sec;
160 return tv.tv_sec;
161}
162
163void xnanosleep(const struct timespec *req, struct timespec *rem) {
164 mustnotbeminus1("nanosleep", nanosleep(req, rem));
165}
166
167/*
168Local Variables:
169c-basic-offset:2
170comment-column:40
171End:
172*/