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