2 * This file is part of DisOrder.
3 * Copyright (C) 2004, 2005, 2007, 2008 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
26 #include <sys/types.h>
27 #include <sys/socket.h>
35 int mustnotbeminus1(const char *what
, int ret
) {
37 fatal(errno
, "error calling %s", what
);
44 if((pid
= fork()) < 0) fatal(errno
, "error calling fork");
48 void xclose_guts(const char *path
, int line
, int fd
) {
50 fatal(errno
, "%s:%d: close %d", path
, line
, fd
);
53 void xdup2(int fd1
, int fd2
) {
54 mustnotbeminus1("dup2", dup2(fd1
, fd2
));
57 void xpipe(int *fdp
) {
58 mustnotbeminus1("pipe", pipe(fdp
));
61 void nonblock(int fd
) {
62 mustnotbeminus1("fcntl F_SETFL",
64 mustnotbeminus1("fcntl F_GETFL",
65 fcntl(fd
, F_GETFL
)) | O_NONBLOCK
));
68 void blocking(int fd
) {
69 mustnotbeminus1("fcntl F_SETFL",
71 mustnotbeminus1("fcntl F_GETFL",
72 fcntl(fd
, F_GETFL
)) & ~O_NONBLOCK
));
75 void cloexec(int fd
) {
76 mustnotbeminus1("fcntl F_SETFD",
78 mustnotbeminus1("fcntl F_GETFD",
79 fcntl(fd
, F_GETFD
)) | FD_CLOEXEC
));
82 void xlisten(int fd
, int q
) {
83 mustnotbeminus1("listen", listen(fd
, q
));
86 void xshutdown(int fd
, int how
) {
87 mustnotbeminus1("shutdown", shutdown(fd
, how
));
90 void xsetsockopt(int fd
, int l
, int o
, const void *v
, socklen_t vl
) {
91 mustnotbeminus1("setsockopt", setsockopt(fd
, l
, o
, v
, vl
));
94 int xsocket(int d
, int t
, int p
) {
95 return mustnotbeminus1("socket", socket(d
, t
, p
));
98 void xconnect(int fd
, const struct sockaddr
*sa
, socklen_t sl
) {
99 mustnotbeminus1("connect", connect(fd
, sa
, sl
));
102 void xsigprocmask(int how
, const sigset_t
*set
, sigset_t
*oldset
) {
103 mustnotbeminus1("sigprocmask", sigprocmask(how
, set
, oldset
));
106 void xsigaction(int sig
, const struct sigaction
*sa
, struct sigaction
*oldsa
) {
107 mustnotbeminus1("sigaction", sigaction(sig
, sa
, oldsa
));
110 int xprintf(const char *fmt
, ...) {
115 n
= mustnotbeminus1("byte_vfprintf", byte_vfprintf(stdout
, fmt
, ap
));
120 void xfclose(FILE *fp
) {
121 mustnotbeminus1("fclose", fclose(fp
));
124 int xstrtol(long *n
, const char *s
, char **ep
, int base
) {
126 *n
= strtol(s
, ep
, base
);
130 int xstrtoll(long_long
*n
, const char *s
, char **ep
, int base
) {
132 *n
= strtoll(s
, ep
, base
);
139 /* some versions of nice() return the new nice value which in principle could
143 if(errno
) fatal(errno
, "error calling nice");
147 void xgettimeofday(struct timeval
*tv
, struct timezone
*tz
) {
148 mustnotbeminus1("gettimeofday", gettimeofday(tv
, tz
));