chiark / gitweb /
distcheck needs fiddled cgiexecdir too
[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 */
18
05b75f8d 19#include "common.h"
460b9539 20
21#include <unistd.h>
22#include <errno.h>
23#include <fcntl.h>
24#include <sys/types.h>
25#include <sys/socket.h>
26#include <sys/time.h>
27#include <signal.h>
460b9539 28
29#include "syscalls.h"
30#include "log.h"
31#include "printf.h"
32
33int mustnotbeminus1(const char *what, int ret) {
34 if(ret == -1)
35 fatal(errno, "error calling %s", what);
36 return ret;
37}
38
39pid_t xfork(void) {
40 pid_t pid;
41
42 if((pid = fork()) < 0) fatal(errno, "error calling fork");
43 return pid;
44}
45
8bb67afe
RK
46void xclose_guts(const char *path, int line, int fd) {
47 if(close(fd) < 0)
48 fatal(errno, "%s:%d: close %d", path, line, fd);
460b9539 49}
50
51void xdup2(int fd1, int fd2) {
52 mustnotbeminus1("dup2", dup2(fd1, fd2));
53}
54
55void xpipe(int *fdp) {
56 mustnotbeminus1("pipe", pipe(fdp));
57}
58
59void nonblock(int fd) {
60 mustnotbeminus1("fcntl F_SETFL",
61 fcntl(fd, F_SETFL,
62 mustnotbeminus1("fcntl F_GETFL",
63 fcntl(fd, F_GETFL)) | O_NONBLOCK));
64}
65
937be4c0
RK
66void blocking(int fd) {
67 mustnotbeminus1("fcntl F_SETFL",
68 fcntl(fd, F_SETFL,
69 mustnotbeminus1("fcntl F_GETFL",
70 fcntl(fd, F_GETFL)) & ~O_NONBLOCK));
71}
72
460b9539 73void cloexec(int fd) {
74 mustnotbeminus1("fcntl F_SETFD",
75 fcntl(fd, F_SETFD,
76 mustnotbeminus1("fcntl F_GETFD",
77 fcntl(fd, F_GETFD)) | FD_CLOEXEC));
78}
79
80void xlisten(int fd, int q) {
81 mustnotbeminus1("listen", listen(fd, q));
82}
83
84void xshutdown(int fd, int how) {
85 mustnotbeminus1("shutdown", shutdown(fd, how));
86}
87
88void xsetsockopt(int fd, int l, int o, const void *v, socklen_t vl) {
89 mustnotbeminus1("setsockopt", setsockopt(fd, l, o, v, vl));
90}
91
92int xsocket(int d, int t, int p) {
93 return mustnotbeminus1("socket", socket(d, t, p));
94}
95
96void xconnect(int fd, const struct sockaddr *sa, socklen_t sl) {
97 mustnotbeminus1("connect", connect(fd, sa, sl));
98}
99
100void xsigprocmask(int how, const sigset_t *set, sigset_t *oldset) {
101 mustnotbeminus1("sigprocmask", sigprocmask(how, set, oldset));
102}
103
104void xsigaction(int sig, const struct sigaction *sa, struct sigaction *oldsa) {
105 mustnotbeminus1("sigaction", sigaction(sig, sa, oldsa));
106}
107
108int xprintf(const char *fmt, ...) {
109 va_list ap;
110 int n;
111
112 va_start(ap, fmt);
113 n = mustnotbeminus1("byte_vfprintf", byte_vfprintf(stdout, fmt, ap));
114 va_end(ap);
115 return n;
116}
117
118void xfclose(FILE *fp) {
119 mustnotbeminus1("fclose", fclose(fp));
120}
121
122int xstrtol(long *n, const char *s, char **ep, int base) {
123 errno = 0;
124 *n = strtol(s, ep, base);
125 return errno;
126}
127
128int xstrtoll(long_long *n, const char *s, char **ep, int base) {
129 errno = 0;
130 *n = strtoll(s, ep, base);
131 return errno;
132}
133
134int xnice(int inc) {
135 int ret;
136
137 /* some versions of nice() return the new nice value which in principle could
138 * be -1 */
139 errno = 0;
140 ret = nice(inc);
141 if(errno) fatal(errno, "error calling nice");
142 return ret;
143}
144
145void xgettimeofday(struct timeval *tv, struct timezone *tz) {
146 mustnotbeminus1("gettimeofday", gettimeofday(tv, tz));
147}
148
149/*
150Local Variables:
151c-basic-offset:2
152comment-column:40
153End:
154*/