2 * This file is part of DisOrder.
3 * Copyright (C) 2004, 2005, 2007-9, 2013 Richard Kettlewell
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.
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.
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/>.
18 /** @file lib/syscalls.c
19 * @brief Error-checking library call wrappers
28 #include <sys/types.h>
30 # include <sys/socket.h>
33 # include <sys/time.h>
42 int mustnotbeminus1(const char *what, int ret) {
44 disorder_fatal(errno, "error calling %s", what);
52 if((pid = fork()) < 0)
53 disorder_fatal(errno, "error calling fork");
57 void xclose_guts(const char *path, int line, int fd) {
59 disorder_fatal(errno, "%s:%d: close %d", path, line, fd);
62 void xdup2(int fd1, int fd2) {
63 mustnotbeminus1("dup2", dup2(fd1, fd2));
66 void xpipe(int *fdp) {
67 mustnotbeminus1("pipe", pipe(fdp));
70 void nonblock(int fd) {
71 mustnotbeminus1("fcntl F_SETFL",
73 mustnotbeminus1("fcntl F_GETFL",
74 fcntl(fd, F_GETFL)) | O_NONBLOCK));
77 void blocking(int fd) {
78 mustnotbeminus1("fcntl F_SETFL",
80 mustnotbeminus1("fcntl F_GETFL",
81 fcntl(fd, F_GETFL)) & ~O_NONBLOCK));
84 void cloexec(int fd) {
85 mustnotbeminus1("fcntl F_SETFD",
87 mustnotbeminus1("fcntl F_GETFD",
88 fcntl(fd, F_GETFD)) | FD_CLOEXEC));
92 void xlisten(SOCKET fd, int q) {
93 mustnotbeminus1("listen", listen(fd, q));
96 void xshutdown(SOCKET fd, int how) {
97 mustnotbeminus1("shutdown", shutdown(fd, how));
100 void xsetsockopt(SOCKET fd, int l, int o, const void *v, socklen_t vl) {
101 mustnotbeminus1("setsockopt", setsockopt(fd, l, o, v, vl));
104 SOCKET 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");
111 void xconnect(SOCKET fd, const struct sockaddr *sa, socklen_t sl) {
112 mustnotbeminus1("connect", connect(fd, sa, sl));
116 void xsigprocmask(int how, const sigset_t *set, sigset_t *oldset) {
117 mustnotbeminus1("sigprocmask", sigprocmask(how, set, oldset));
120 void xsigaction(int sig, const struct sigaction *sa, struct sigaction *oldsa) {
121 mustnotbeminus1("sigaction", sigaction(sig, sa, oldsa));
125 int xprintf(const char *fmt, ...) {
130 n = mustnotbeminus1("byte_vfprintf", byte_vfprintf(stdout, fmt, ap));
135 void xfclose(FILE *fp) {
136 mustnotbeminus1("fclose", fclose(fp));
139 int xstrtol(long *n, const char *s, char **ep, int base) {
141 *n = strtol(s, ep, base);
145 int xstrtoll(long_long *n, const char *s, char **ep, int base) {
147 *n = strtoll(s, ep, base);
155 /* some versions of nice() return the new nice value which in principle could
160 disorder_fatal(errno, "error calling nice");
165 void xgettimeofday(struct timeval *tv, struct timezone *tz) {
166 mustnotbeminus1("gettimeofday", gettimeofday(tv, tz));
169 time_t xtime(time_t *whenp) {
172 xgettimeofday(&tv, NULL);
179 void xnanosleep(const struct timespec *req, struct timespec *rem) {
180 mustnotbeminus1("nanosleep", nanosleep(req, rem));