460b9539 |
1 | /* |
2 | * This file is part of DisOrder. |
3 | * Copyright (C) 2004, 2005 Richard Kettlewell |
4 | * |
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. |
9 | * |
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. |
14 | * |
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 |
18 | * USA |
19 | */ |
20 | |
21 | #include <config.h> |
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> |
30 | #include <stdio.h> |
31 | #include <stdlib.h> |
32 | |
33 | #include "syscalls.h" |
34 | #include "log.h" |
35 | #include "printf.h" |
36 | |
37 | int mustnotbeminus1(const char *what, int ret) { |
38 | if(ret == -1) |
39 | fatal(errno, "error calling %s", what); |
40 | return ret; |
41 | } |
42 | |
43 | pid_t xfork(void) { |
44 | pid_t pid; |
45 | |
46 | if((pid = fork()) < 0) fatal(errno, "error calling fork"); |
47 | return pid; |
48 | } |
49 | |
50 | void xclose(int fd) { |
51 | mustnotbeminus1("close", close(fd)); |
52 | } |
53 | |
54 | void xdup2(int fd1, int fd2) { |
55 | mustnotbeminus1("dup2", dup2(fd1, fd2)); |
56 | } |
57 | |
58 | void xpipe(int *fdp) { |
59 | mustnotbeminus1("pipe", pipe(fdp)); |
60 | } |
61 | |
62 | void 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 | |
69 | void cloexec(int fd) { |
70 | mustnotbeminus1("fcntl F_SETFD", |
71 | fcntl(fd, F_SETFD, |
72 | mustnotbeminus1("fcntl F_GETFD", |
73 | fcntl(fd, F_GETFD)) | FD_CLOEXEC)); |
74 | } |
75 | |
76 | void xlisten(int fd, int q) { |
77 | mustnotbeminus1("listen", listen(fd, q)); |
78 | } |
79 | |
80 | void xshutdown(int fd, int how) { |
81 | mustnotbeminus1("shutdown", shutdown(fd, how)); |
82 | } |
83 | |
84 | void xsetsockopt(int fd, int l, int o, const void *v, socklen_t vl) { |
85 | mustnotbeminus1("setsockopt", setsockopt(fd, l, o, v, vl)); |
86 | } |
87 | |
88 | int xsocket(int d, int t, int p) { |
89 | return mustnotbeminus1("socket", socket(d, t, p)); |
90 | } |
91 | |
92 | void xconnect(int fd, const struct sockaddr *sa, socklen_t sl) { |
93 | mustnotbeminus1("connect", connect(fd, sa, sl)); |
94 | } |
95 | |
96 | void xsigprocmask(int how, const sigset_t *set, sigset_t *oldset) { |
97 | mustnotbeminus1("sigprocmask", sigprocmask(how, set, oldset)); |
98 | } |
99 | |
100 | void xsigaction(int sig, const struct sigaction *sa, struct sigaction *oldsa) { |
101 | mustnotbeminus1("sigaction", sigaction(sig, sa, oldsa)); |
102 | } |
103 | |
104 | int xprintf(const char *fmt, ...) { |
105 | va_list ap; |
106 | int n; |
107 | |
108 | va_start(ap, fmt); |
109 | n = mustnotbeminus1("byte_vfprintf", byte_vfprintf(stdout, fmt, ap)); |
110 | va_end(ap); |
111 | return n; |
112 | } |
113 | |
114 | void xfclose(FILE *fp) { |
115 | mustnotbeminus1("fclose", fclose(fp)); |
116 | } |
117 | |
118 | int xstrtol(long *n, const char *s, char **ep, int base) { |
119 | errno = 0; |
120 | *n = strtol(s, ep, base); |
121 | return errno; |
122 | } |
123 | |
124 | int xstrtoll(long_long *n, const char *s, char **ep, int base) { |
125 | errno = 0; |
126 | *n = strtoll(s, ep, base); |
127 | return errno; |
128 | } |
129 | |
130 | int xnice(int inc) { |
131 | int ret; |
132 | |
133 | /* some versions of nice() return the new nice value which in principle could |
134 | * be -1 */ |
135 | errno = 0; |
136 | ret = nice(inc); |
137 | if(errno) fatal(errno, "error calling nice"); |
138 | return ret; |
139 | } |
140 | |
141 | void xgettimeofday(struct timeval *tv, struct timezone *tz) { |
142 | mustnotbeminus1("gettimeofday", gettimeofday(tv, tz)); |
143 | } |
144 | |
145 | /* |
146 | Local Variables: |
147 | c-basic-offset:2 |
148 | comment-column:40 |
149 | End: |
150 | */ |