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