2 * This file is part of DisOrder.
3 * Copyright (C) 2004, 2005 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 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * 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, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
26 #include <sys/types.h>
27 #include <sys/socket.h>
37 int mustnotbeminus1(const char *what, int ret) {
39 fatal(errno, "error calling %s", what);
46 if((pid = fork()) < 0) fatal(errno, "error calling fork");
50 void xclose_guts(const char *path, int line, int fd) {
52 fatal(errno, "%s:%d: close %d", path, line, fd);
55 void xdup2(int fd1, int fd2) {
56 mustnotbeminus1("dup2", dup2(fd1, fd2));
59 void xpipe(int *fdp) {
60 mustnotbeminus1("pipe", pipe(fdp));
63 void nonblock(int fd) {
64 mustnotbeminus1("fcntl F_SETFL",
66 mustnotbeminus1("fcntl F_GETFL",
67 fcntl(fd, F_GETFL)) | O_NONBLOCK));
70 void blocking(int fd) {
71 mustnotbeminus1("fcntl F_SETFL",
73 mustnotbeminus1("fcntl F_GETFL",
74 fcntl(fd, F_GETFL)) & ~O_NONBLOCK));
77 void cloexec(int fd) {
78 mustnotbeminus1("fcntl F_SETFD",
80 mustnotbeminus1("fcntl F_GETFD",
81 fcntl(fd, F_GETFD)) | FD_CLOEXEC));
84 void xlisten(int fd, int q) {
85 mustnotbeminus1("listen", listen(fd, q));
88 void xshutdown(int fd, int how) {
89 mustnotbeminus1("shutdown", shutdown(fd, how));
92 void xsetsockopt(int fd, int l, int o, const void *v, socklen_t vl) {
93 mustnotbeminus1("setsockopt", setsockopt(fd, l, o, v, vl));
96 int xsocket(int d, int t, int p) {
97 return mustnotbeminus1("socket", socket(d, t, p));
100 void xconnect(int fd, const struct sockaddr *sa, socklen_t sl) {
101 mustnotbeminus1("connect", connect(fd, sa, sl));
104 void xsigprocmask(int how, const sigset_t *set, sigset_t *oldset) {
105 mustnotbeminus1("sigprocmask", sigprocmask(how, set, oldset));
108 void xsigaction(int sig, const struct sigaction *sa, struct sigaction *oldsa) {
109 mustnotbeminus1("sigaction", sigaction(sig, sa, oldsa));
112 int xprintf(const char *fmt, ...) {
117 n = mustnotbeminus1("byte_vfprintf", byte_vfprintf(stdout, fmt, ap));
122 void xfclose(FILE *fp) {
123 mustnotbeminus1("fclose", fclose(fp));
126 int xstrtol(long *n, const char *s, char **ep, int base) {
128 *n = strtol(s, ep, base);
132 int xstrtoll(long_long *n, const char *s, char **ep, int base) {
134 *n = strtoll(s, ep, base);
141 /* some versions of nice() return the new nice value which in principle could
145 if(errno) fatal(errno, "error calling nice");
149 void xgettimeofday(struct timeval *tv, struct timezone *tz) {
150 mustnotbeminus1("gettimeofday", gettimeofday(tv, tz));