chiark / gitweb /
log: more general error message formatting
[disorder] / lib / syscalls.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder.
cca89d7c 3 * Copyright (C) 2004, 2005, 2007-9, 2013 Richard Kettlewell
460b9539 4 *
e7eb3a27 5 * This program is free software: you can redistribute it and/or modify
460b9539 6 * it under the terms of the GNU General Public License as published by
e7eb3a27 7 * the Free Software Foundation, either version 3 of the License, or
460b9539 8 * (at your option) any later version.
e7eb3a27
RK
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 *
460b9539 15 * You should have received a copy of the GNU General Public License
e7eb3a27 16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
460b9539 17 */
132a5a4a
RK
18/** @file lib/syscalls.c
19 * @brief Error-checking library call wrappers
20 */
05b75f8d 21#include "common.h"
460b9539 22
cca89d7c
RK
23#if HAVE_UNISTD_H
24# include <unistd.h>
25#endif
460b9539 26#include <errno.h>
27#include <fcntl.h>
28#include <sys/types.h>
cca89d7c
RK
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
460b9539 35#include <signal.h>
5db8461a 36#include <time.h>
460b9539 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)
2e9ba080 44 disorder_fatal(errno, "error calling %s", what);
460b9539 45 return ret;
46}
47
48pid_t xfork(void) {
49 pid_t pid;
50
2e9ba080
RK
51 if((pid = fork()) < 0)
52 disorder_fatal(errno, "error calling fork");
460b9539 53 return pid;
54}
55
8bb67afe
RK
56void xclose_guts(const char *path, int line, int fd) {
57 if(close(fd) < 0)
2e9ba080 58 disorder_fatal(errno, "%s:%d: close %d", path, line, fd);
460b9539 59}
60
61void xdup2(int fd1, int fd2) {
62 mustnotbeminus1("dup2", dup2(fd1, fd2));
63}
64
65void xpipe(int *fdp) {
66 mustnotbeminus1("pipe", pipe(fdp));
67}
68
69void 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
937be4c0
RK
76void 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
460b9539 83void 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
90void xlisten(int fd, int q) {
91 mustnotbeminus1("listen", listen(fd, q));
92}
93
94void xshutdown(int fd, int how) {
95 mustnotbeminus1("shutdown", shutdown(fd, how));
96}
97
98void xsetsockopt(int fd, int l, int o, const void *v, socklen_t vl) {
99 mustnotbeminus1("setsockopt", setsockopt(fd, l, o, v, vl));
100}
101
102int xsocket(int d, int t, int p) {
103 return mustnotbeminus1("socket", socket(d, t, p));
104}
105
106void xconnect(int fd, const struct sockaddr *sa, socklen_t sl) {
107 mustnotbeminus1("connect", connect(fd, sa, sl));
108}
109
110void xsigprocmask(int how, const sigset_t *set, sigset_t *oldset) {
111 mustnotbeminus1("sigprocmask", sigprocmask(how, set, oldset));
112}
113
114void xsigaction(int sig, const struct sigaction *sa, struct sigaction *oldsa) {
115 mustnotbeminus1("sigaction", sigaction(sig, sa, oldsa));
116}
117
118int 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
128void xfclose(FILE *fp) {
129 mustnotbeminus1("fclose", fclose(fp));
130}
131
132int xstrtol(long *n, const char *s, char **ep, int base) {
133 errno = 0;
134 *n = strtol(s, ep, base);
135 return errno;
136}
137
138int 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
144int 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);
2e9ba080
RK
151 if(errno)
152 disorder_fatal(errno, "error calling nice");
460b9539 153 return ret;
154}
155
156void xgettimeofday(struct timeval *tv, struct timezone *tz) {
157 mustnotbeminus1("gettimeofday", gettimeofday(tv, tz));
158}
159
4265e5d3
RK
160time_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
5db8461a
RK
169void xnanosleep(const struct timespec *req, struct timespec *rem) {
170 mustnotbeminus1("nanosleep", nanosleep(req, rem));
171}
172
460b9539 173/*
174Local Variables:
175c-basic-offset:2
176comment-column:40
177End:
178*/